woo-postnl
/
includes
/
vendor
/
myparcelnl
/
sdk
/
src
/
Model
/
Consignment
/
BpostConsignment.php
BpostConsignment.php in PostNL for WooCommerce 4.0.0, at includes/vendor/myparcelnl/sdk/src/Model/Consignment/BpostConsignment.php
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace MyParcelNL\Sdk\src\Model\Consignment; |
| 4 | |
| 5 | use MyParcelNL\Sdk\src\Exception\InvalidConsignmentException; |
| 6 | |
| 7 | class BpostConsignment extends AbstractConsignment |
| 8 | { |
| 9 | /** |
| 10 | * @var int |
| 11 | */ |
| 12 | public const CARRIER_ID = 2; |
| 13 | |
| 14 | /** |
| 15 | * @var string |
| 16 | */ |
| 17 | public const CARRIER_NAME = 'bpost'; |
| 18 | |
| 19 | /** |
| 20 | * @var int |
| 21 | */ |
| 22 | public const DEFAULT_WEIGHT = 50; |
| 23 | |
| 24 | /** |
| 25 | * @var array |
| 26 | */ |
| 27 | public const INSURANCE_POSSIBILITIES_LOCAL = [0, 500, 1000, 1500, 2000]; |
| 28 | |
| 29 | /** |
| 30 | * @var array |
| 31 | */ |
| 32 | private const VALID_PACKAGE_TYPES = [ |
| 33 | self::PACKAGE_TYPE_PACKAGE |
| 34 | ]; |
| 35 | |
| 36 | /** |
| 37 | * @internal |
| 38 | * |
| 39 | * @var int |
| 40 | */ |
| 41 | public $physical_properties = ['weight' => self::DEFAULT_WEIGHT]; |
| 42 | |
| 43 | /** |
| 44 | * @var string |
| 45 | */ |
| 46 | protected $local_cc = self::CC_BE; |
| 47 | |
| 48 | /** |
| 49 | * @param array $consignmentEncoded |
| 50 | * |
| 51 | * @return array |
| 52 | * @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException |
| 53 | */ |
| 54 | public function encodeStreet(array $consignmentEncoded): array |
| 55 | { |
| 56 | if ($this->getCountry() == $this->local_cc) { |
| 57 | $consignmentEncoded = array_merge_recursive( |
| 58 | $consignmentEncoded, |
| 59 | [ |
| 60 | 'recipient' => [ |
| 61 | 'street' => $this->getStreet(true), |
| 62 | 'street_additional_info' => $this->getStreetAdditionalInfo(), |
| 63 | 'number' => $this->getNumber(), |
| 64 | 'box_number' => (string) $this->getBoxNumber(), |
| 65 | 'number_suffix' => (string) $this->getNumberSuffix(), |
| 66 | ], |
| 67 | ] |
| 68 | ); |
| 69 | |
| 70 | return $consignmentEncoded; |
| 71 | } |
| 72 | |
| 73 | if ($this->getCountry() == self::CC_NL) { |
| 74 | $consignmentEncoded = array_merge_recursive( |
| 75 | $consignmentEncoded, |
| 76 | [ |
| 77 | 'recipient' => [ |
| 78 | 'street' => $this->getStreet(true), |
| 79 | 'street_additional_info' => $this->getStreetAdditionalInfo(), |
| 80 | 'number' => $this->getNumber(), |
| 81 | 'number_suffix' => (string) $this->getNumberSuffix(), |
| 82 | ], |
| 83 | ] |
| 84 | ); |
| 85 | |
| 86 | return $consignmentEncoded; |
| 87 | } |
| 88 | |
| 89 | return parent::encodeStreet($consignmentEncoded); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Street number suffix. |
| 94 | * |
| 95 | * Required: no |
| 96 | * |
| 97 | * @param string|null $numberSuffix |
| 98 | * |
| 99 | * @return $this |
| 100 | */ |
| 101 | public function setNumberSuffix(?string $numberSuffix): AbstractConsignment |
| 102 | { |
| 103 | $this->number_suffix = $numberSuffix; |
| 104 | |
| 105 | return $this; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * The package type |
| 110 | * |
| 111 | * For international shipment only package type 1 is allowed |
| 112 | * Pattern: [1 – 3]<br> |
| 113 | * Example: |
| 114 | * 1. package |
| 115 | * 2. mailbox package |
| 116 | * 3. letter |
| 117 | * Required: Yes |
| 118 | * |
| 119 | * @param int $packageType |
| 120 | * |
| 121 | * @return $this |
| 122 | * @throws \Exception |
| 123 | */ |
| 124 | public function setPackageType(int $packageType): AbstractConsignment |
| 125 | { |
| 126 | if (! in_array($packageType, self::VALID_PACKAGE_TYPES)) { |
| 127 | throw new \Exception('Use the correct package type for shipment:' . $this->consignment_id); |
| 128 | } |
| 129 | |
| 130 | return parent::setPackageType($packageType); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * The delivery date time for this shipment |
| 135 | * Pattern: YYYY-MM-DD | YYYY-MM-DD HH:MM:SS |
| 136 | * Example: 2017-01-01 | 2017-01-01 00:00:00 |
| 137 | * Required: Yes if delivery type has been specified |
| 138 | * |
| 139 | * @param string $delivery_date |
| 140 | * |
| 141 | * @return \MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment |
| 142 | * @throws \Exception |
| 143 | */ |
| 144 | public function setDeliveryDate(?string $delivery_date): AbstractConsignment |
| 145 | { |
| 146 | return parent::setDeliveryDate(null); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Insurance price for the package. |
| 151 | * |
| 152 | * Composite type containing integer and currency. The amount is without decimal separators. |
| 153 | * Required: No |
| 154 | * |
| 155 | * @param int|null $insurance |
| 156 | * |
| 157 | * @return \MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment |
| 158 | * @throws \Exception |
| 159 | */ |
| 160 | public function setInsurance(?int $insurance): AbstractConsignment |
| 161 | { |
| 162 | if (null === $insurance) { |
| 163 | throw new \BadMethodCallException('Insurance must be one of ' . implode(', ', self::INSURANCE_POSSIBILITIES_LOCAL)); |
| 164 | } |
| 165 | |
| 166 | return parent::setInsurance($insurance); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @return bool |
| 171 | */ |
| 172 | public function isSignature(): bool |
| 173 | { |
| 174 | return (bool) $this->signature; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Package must be signed for |
| 179 | * |
| 180 | * Required: No |
| 181 | * |
| 182 | * @param bool $signature |
| 183 | * |
| 184 | * @return $this |
| 185 | * @throws \Exception |
| 186 | */ |
| 187 | public function setSignature(bool $signature): AbstractConsignment |
| 188 | { |
| 189 | $this->signature = $this->canHaveOption($signature); |
| 190 | |
| 191 | return $this; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @return string |
| 196 | * @deprecated Use setRetailNetworkId instead |
| 197 | * |
| 198 | */ |
| 199 | public function getPickupNetworkId(): string |
| 200 | { |
| 201 | return $this->getRetailNetworkId(); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * @return string |
| 206 | */ |
| 207 | public function getRetailNetworkId(): string |
| 208 | { |
| 209 | return ""; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Pattern: [0-9A-Za-z] |
| 214 | * Example: Albert Heijn |
| 215 | * Required: Yes for pickup location |
| 216 | * |
| 217 | * @param string $retailNetworkId |
| 218 | * |
| 219 | * @return \MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment |
| 220 | * @deprecated Use setRetailNetworkId instead |
| 221 | * |
| 222 | */ |
| 223 | public function setPickupNetworkId($retailNetworkId): AbstractConsignment |
| 224 | { |
| 225 | return $this->setRetailNetworkId((string) $retailNetworkId); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Pattern: [0-9A-Za-z] |
| 230 | * Example: Albert Heijn |
| 231 | * Required: Yes for pickup location |
| 232 | * |
| 233 | * @param string $retailNetworkId |
| 234 | * |
| 235 | * @return \MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment |
| 236 | */ |
| 237 | public function setRetailNetworkId(string $retailNetworkId): AbstractConsignment |
| 238 | { |
| 239 | $this->retail_network_id = $retailNetworkId; |
| 240 | |
| 241 | return $this; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * @return bool |
| 246 | * @throws \MyParcelNL\Sdk\src\Exception\InvalidConsignmentException |
| 247 | */ |
| 248 | public function validate(): bool |
| 249 | { |
| 250 | if ($this->getTotalWeight() < 50) { |
| 251 | throw new InvalidConsignmentException('It is necessary to at a minimum weight of 50 grams'); |
| 252 | } |
| 253 | |
| 254 | /** @var \MyParcelNL\Sdk\src\Model\MyParcelCustomsItem $item */ |
| 255 | $totalValue = 0; |
| 256 | foreach ((array) $this->items as $item) { |
| 257 | $totalValue += $item->getItemValue(); |
| 258 | } |
| 259 | |
| 260 | if (! empty($totalValue) && $totalValue < 100) { |
| 261 | throw new InvalidConsignmentException('It is necessary to use cents and the a minimum price is 1 euro'); |
| 262 | } |
| 263 | |
| 264 | return parent::validate(); |
| 265 | } |
| 266 | } |
| 267 |