| @@ -6,14 +6,18 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | declare( strict_types=1 ); |
| 9 | 9 | |
| 10 | - | |
| 11 | 10 | namespace Packetery\Module\Order; |
| 12 | 11 | |
| 13 | 12 | use Packetery\Core\Api; |
| 13 | +use Packetery\Core\Entity\Order; | |
| 14 | +use Packetery\Core\Entity\PacketStatus; | |
| 14 | 15 | use Packetery\Core\Log; |
| 15 | -use Packetery\Module\Options; | |
| 16 | +use Packetery\Module\Exception\InvalidPasswordException; | |
| 17 | +use Packetery\Module\Framework\WcAdapter; | |
| 18 | +use Packetery\Module\Framework\WpAdapter; | |
| 19 | +use Packetery\Module\Options\OptionsProvider; | |
| 16 | 20 | |
| 17 | 21 | /** |
| 18 | 22 | * Class Synchronizer |
| 19 | 23 | * |
| @@ -20,8 +24,10 @@ | ||
| 20 | 24 | * @package Packetery\Module\Order |
| 21 | 25 | */ |
| 22 | 26 | class PacketSynchronizer { |
| 23 | 27 | |
| 28 | + private const HOOK_NAME_SYNC_ORDER_STATUS = 'packetery_sync_order_status'; | |
| 29 | + | |
| 24 | 30 | /** |
| 25 | 31 | * API soap client. |
| 26 | 32 | * |
| 27 | 33 | * @var Api\Soap\Client |
| @@ -30,9 +36,9 @@ | ||
| 30 | 36 | |
| 31 | 37 | /** |
| 32 | 38 | * Options provider. |
| 33 | 39 | * |
| 34 | - * @var Options\Provider | |
| 40 | + * @var OptionsProvider | |
| 35 | 41 | */ |
| 36 | 42 | private $optionsProvider; |
| 37 | 43 | |
| 38 | 44 | /** |
| @@ -49,131 +55,209 @@ | ||
| 49 | 55 | */ |
| 50 | 56 | private $orderRepository; |
| 51 | 57 | |
| 52 | 58 | /** |
| 59 | + * WC order actions. | |
| 60 | + * | |
| 61 | + * @var WcOrderActions | |
| 62 | + */ | |
| 63 | + private $wcOrderActions; | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * WC adapter. | |
| 67 | + * | |
| 68 | + * @var WcAdapter | |
| 69 | + */ | |
| 70 | + private $wcAdapter; | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * WP adapter. | |
| 74 | + * | |
| 75 | + * @var WpAdapter | |
| 76 | + */ | |
| 77 | + private $wpAdapter; | |
| 78 | + | |
| 79 | + /** | |
| 53 | 80 | * Constructor. |
| 54 | 81 | * |
| 55 | - * @param Api\Soap\Client $apiSoapClient API soap client. | |
| 56 | - * @param Log\ILogger $logger Logger. | |
| 57 | - * @param Options\Provider $optionsProvider Options provider. | |
| 58 | - * @param Repository $orderRepository Order repository. | |
| 82 | + * @param Api\Soap\Client $apiSoapClient API soap client. | |
| 83 | + * @param Log\ILogger $logger Logger. | |
| 84 | + * @param OptionsProvider $optionsProvider Options provider. | |
| 85 | + * @param Repository $orderRepository Order repository. | |
| 86 | + * @param WcOrderActions $wcOrderActions WC order actions. | |
| 87 | + * @param WcAdapter $wcAdapter WC adapter. | |
| 88 | + * @param WpAdapter $wpAdapter WP adapter. | |
| 59 | 89 | */ |
| 60 | 90 | public function __construct( |
| 61 | 91 | Api\Soap\Client $apiSoapClient, |
| 62 | 92 | Log\ILogger $logger, |
| 63 | - Options\Provider $optionsProvider, | |
| 64 | - Repository $orderRepository | |
| 93 | + OptionsProvider $optionsProvider, | |
| 94 | + Repository $orderRepository, | |
| 95 | + WcOrderActions $wcOrderActions, | |
| 96 | + WcAdapter $wcAdapter, | |
| 97 | + WpAdapter $wpAdapter | |
| 65 | 98 | ) { |
| 66 | 99 | $this->apiSoapClient = $apiSoapClient; |
| 67 | 100 | $this->logger = $logger; |
| 68 | 101 | $this->optionsProvider = $optionsProvider; |
| 69 | 102 | $this->orderRepository = $orderRepository; |
| 103 | + $this->wcOrderActions = $wcOrderActions; | |
| 104 | + $this->wcAdapter = $wcAdapter; | |
| 105 | + $this->wpAdapter = $wpAdapter; | |
| 70 | 106 | } |
| 71 | 107 | |
| 72 | 108 | /** |
| 109 | + * Register hook. | |
| 110 | + * | |
| 111 | + * @return void | |
| 112 | + */ | |
| 113 | + public function register() { | |
| 114 | + add_action( self::HOOK_NAME_SYNC_ORDER_STATUS, [ $this, 'syncStatusById' ] ); | |
| 115 | + } | |
| 116 | + | |
| 117 | + /** | |
| 73 | 118 | * Synchronizes packets. |
| 74 | 119 | * |
| 75 | 120 | * @return void |
| 121 | + * @throws \Exception Exception. | |
| 76 | 122 | */ |
| 77 | 123 | public function syncStatuses(): void { |
| 78 | - $results = $this->orderRepository->findStatusSyncingOrders( | |
| 79 | - $this->optionsProvider->getStatusSyncingPacketStatuses(), | |
| 124 | + $syncingOrderIds = $this->orderRepository->findStatusSyncingOrderIds( | |
| 125 | + $this->optionsProvider->getStatusSyncingPacketStatuses( $this->getDefaultPacketStatuses() ), | |
| 80 | 126 | $this->optionsProvider->getExistingStatusSyncingOrderStatuses(), |
| 81 | 127 | $this->optionsProvider->getMaxDaysOfPacketStatusSyncing(), |
| 82 | 128 | $this->optionsProvider->getMaxStatusSyncingPackets() |
| 83 | 129 | ); |
| 84 | 130 | |
| 85 | - foreach ( $results as $order ) { | |
| 86 | - $packetId = $order->getPacketId(); | |
| 131 | + foreach ( $syncingOrderIds as $orderId ) { | |
| 132 | + $this->wcAdapter->asScheduleSingleAction( time(), self::HOOK_NAME_SYNC_ORDER_STATUS, [ $orderId ] ); | |
| 133 | + } | |
| 134 | + } | |
| 87 | 135 | |
| 88 | - $request = new Api\Soap\Request\PacketStatus( (int) $packetId ); | |
| 89 | - $response = $this->apiSoapClient->packetStatus( $request ); | |
| 136 | + /** | |
| 137 | + * Synchronizes status for one order. | |
| 138 | + * | |
| 139 | + * @param int $orderId Order id. | |
| 140 | + * | |
| 141 | + * @return void | |
| 142 | + */ | |
| 143 | + public function syncStatusById( int $orderId ): void { | |
| 144 | + $order = $this->orderRepository->getById( $orderId ); | |
| 145 | + if ( $order === null ) { | |
| 146 | + return; | |
| 147 | + } | |
| 90 | 148 | |
| 91 | - if ( $response->hasFault() ) { | |
| 92 | - $record = new Log\Record(); | |
| 93 | - $record->action = Log\Record::ACTION_PACKET_STATUS_SYNC; | |
| 94 | - $record->status = Log\Record::STATUS_ERROR; | |
| 95 | - $record->title = __( 'Packet status could not be synchronized.', 'packeta' ); | |
| 96 | - $record->params = [ | |
| 97 | - 'orderId' => $order->getNumber(), | |
| 98 | - 'packetId' => $request->getPacketId(), | |
| 99 | - 'errorMessage' => $response->getFaultString(), | |
| 100 | - ]; | |
| 101 | - $record->orderId = $order->getNumber(); | |
| 102 | - $this->logger->add( $record ); | |
| 149 | + $this->syncStatus( $order ); | |
| 150 | + } | |
| 103 | 151 | |
| 104 | - if ( $response->hasWrongPassword() ) { | |
| 105 | - break; | |
| 106 | - } | |
| 152 | + /** | |
| 153 | + * Synchronizes status for one order. | |
| 154 | + * | |
| 155 | + * @param Order $order Order. | |
| 156 | + * | |
| 157 | + * @return void | |
| 158 | + * @throws InvalidPasswordException InvalidPasswordException. | |
| 159 | + */ | |
| 160 | + public function syncStatus( Order $order ): void { | |
| 161 | + $packetId = $order->getPacketId(); | |
| 107 | 162 | |
| 108 | - continue; | |
| 163 | + $request = new Api\Soap\Request\PacketStatus( $packetId ); | |
| 164 | + $response = $this->apiSoapClient->packetStatus( $request ); | |
| 165 | + | |
| 166 | + if ( $response->hasFault() ) { | |
| 167 | + $record = new Log\Record(); | |
| 168 | + $record->action = Log\Record::ACTION_PACKET_STATUS_SYNC; | |
| 169 | + $record->status = Log\Record::STATUS_ERROR; | |
| 170 | + $record->title = __( 'Packet status could not be synchronized.', 'packeta' ); | |
| 171 | + $record->params = [ | |
| 172 | + 'orderId' => $order->getNumber(), | |
| 173 | + 'packetId' => $request->getPacketId(), | |
| 174 | + 'errorMessage' => $response->getFaultString(), | |
| 175 | + ]; | |
| 176 | + $record->orderId = $order->getNumber(); | |
| 177 | + $this->logger->add( $record ); | |
| 178 | + | |
| 179 | + if ( $response->hasWrongPassword() ) { | |
| 180 | + throw new InvalidPasswordException( 'Wrong password.' ); | |
| 109 | 181 | } |
| 110 | 182 | |
| 111 | - if ( $response->getCodeText() === $order->getPacketStatus() ) { | |
| 112 | - continue; | |
| 113 | - } | |
| 183 | + return; | |
| 184 | + } | |
| 114 | 185 | |
| 186 | + $storedUntilResponse = $response->getStoredUntil(); | |
| 187 | + $storedUntilOrder = $order->getStoredUntil(); | |
| 188 | + | |
| 189 | + if ( $storedUntilResponse === $storedUntilOrder && $response->getCodeText() === $order->getPacketStatus() ) { | |
| 190 | + return; | |
| 191 | + } | |
| 192 | + | |
| 193 | + if ( $storedUntilResponse !== $storedUntilOrder ) { | |
| 194 | + $order->setStoredUntil( $storedUntilResponse ); | |
| 195 | + } | |
| 196 | + | |
| 197 | + if ( $response->getCodeText() !== $order->getPacketStatus() ) { | |
| 115 | 198 | $order->setPacketStatus( $response->getCodeText() ); |
| 116 | - $this->orderRepository->save( $order ); | |
| 199 | + $this->wcOrderActions->updateOrderStatus( $order->getNumber(), $response->getCodeText() ); | |
| 117 | 200 | } |
| 201 | + | |
| 202 | + $this->orderRepository->save( $order ); | |
| 118 | 203 | } |
| 119 | 204 | |
| 120 | 205 | /** |
| 121 | - * Gets code text translated. | |
| 122 | - * | |
| 123 | - * @param string|null $packetStatus Packet status. | |
| 124 | - * | |
| 125 | - * @return string|null | |
| 206 | + * @return PacketStatus[] | |
| 126 | 207 | */ |
| 127 | - public function getPacketStatusTranslated( ?string $packetStatus ): string { | |
| 128 | - switch ( $packetStatus ) { | |
| 129 | - case 'received data': | |
| 130 | - return __( 'Data received', 'packeta' ); | |
| 131 | - case 'arrived': | |
| 132 | - return __( 'Submitted', 'packeta' ); | |
| 133 | - case 'prepared for departure': | |
| 134 | - return __( 'Prepared for departure', 'packeta' ); | |
| 135 | - case 'departed': | |
| 136 | - return __( 'Departed', 'packeta' ); | |
| 137 | - case 'ready for pickup': | |
| 138 | - return __( 'Ready for pickup', 'packeta' ); | |
| 139 | - case 'handed to carrier': | |
| 140 | - return __( 'Handed to carrier', 'packeta' ); | |
| 141 | - case 'delivered': | |
| 142 | - return __( 'Delivered', 'packeta' ); | |
| 143 | - case 'posted back': | |
| 144 | - return __( 'Posted back', 'packeta' ); | |
| 145 | - case 'returned': | |
| 146 | - return __( 'Returned', 'packeta' ); | |
| 147 | - case 'cancelled': | |
| 148 | - return __( 'Cancelled', 'packeta' ); | |
| 149 | - case 'collected': | |
| 150 | - return __( 'Collected', 'packeta' ); | |
| 151 | - case 'unknown': | |
| 152 | - return __( 'Unknown', 'packeta' ); | |
| 208 | + public function getDefaultPacketStatuses(): array { | |
| 209 | + $result = []; | |
| 210 | + | |
| 211 | + foreach ( $this->getPacketStatuses() as $statusName => $status ) { | |
| 212 | + if ( ! $status->hasDefaultSynchronization() ) { | |
| 213 | + continue; | |
| 214 | + } | |
| 215 | + | |
| 216 | + $result[ $statusName ] = $status; | |
| 153 | 217 | } |
| 154 | 218 | |
| 155 | - return (string) $packetStatus; | |
| 219 | + return $result; | |
| 156 | 220 | } |
| 157 | 221 | |
| 158 | 222 | /** |
| 159 | - * Gets packet statuses. | |
| 223 | + * Gets packet statuses and default values. | |
| 160 | 224 | * |
| 161 | - * @return string[] | |
| 225 | + * @return PacketStatus[] | |
| 162 | 226 | */ |
| 163 | 227 | public function getPacketStatuses(): array { |
| 164 | 228 | return [ |
| 165 | - 'received data', | |
| 166 | - 'arrived', | |
| 167 | - 'prepared for departure', | |
| 168 | - 'departed', | |
| 169 | - 'ready for pickup', | |
| 170 | - 'handed to carrier', | |
| 171 | - 'delivered', | |
| 172 | - 'posted back', | |
| 173 | - 'returned', | |
| 174 | - 'cancelled', | |
| 175 | - 'collected', | |
| 176 | - 'unknown', | |
| 229 | + PacketStatus::RECEIVED_DATA => | |
| 230 | + new PacketStatus( PacketStatus::RECEIVED_DATA, $this->wpAdapter->__( 'Awaiting consignment', 'packeta' ), true ), | |
| 231 | + PacketStatus::ARRIVED => | |
| 232 | + new PacketStatus( PacketStatus::ARRIVED, $this->wpAdapter->__( 'Accepted at depot', 'packeta' ), true ), | |
| 233 | + PacketStatus::PREPARED_FOR_DEPARTURE => | |
| 234 | + new PacketStatus( PacketStatus::PREPARED_FOR_DEPARTURE, $this->wpAdapter->__( 'On the way', 'packeta' ), true ), | |
| 235 | + PacketStatus::DEPARTED => | |
| 236 | + new PacketStatus( PacketStatus::DEPARTED, $this->wpAdapter->__( 'Departed from depot', 'packeta' ), true ), | |
| 237 | + PacketStatus::READY_FOR_PICKUP => | |
| 238 | + new PacketStatus( PacketStatus::READY_FOR_PICKUP, $this->wpAdapter->__( 'Ready for pick-up', 'packeta' ), true ), | |
| 239 | + PacketStatus::HANDED_TO_CARRIER => | |
| 240 | + new PacketStatus( PacketStatus::HANDED_TO_CARRIER, $this->wpAdapter->__( 'Handed over to carrier company', 'packeta' ), true ), | |
| 241 | + PacketStatus::DELIVERED => | |
| 242 | + new PacketStatus( PacketStatus::DELIVERED, $this->wpAdapter->__( 'Delivered', 'packeta' ), false ), | |
| 243 | + PacketStatus::POSTED_BACK => | |
| 244 | + new PacketStatus( PacketStatus::POSTED_BACK, $this->wpAdapter->__( 'Return (on the way back)', 'packeta' ), true ), | |
| 245 | + PacketStatus::RETURNED => | |
| 246 | + new PacketStatus( PacketStatus::RETURNED, $this->wpAdapter->__( 'Returned to sender', 'packeta' ), false ), | |
| 247 | + PacketStatus::CANCELLED => | |
| 248 | + new PacketStatus( PacketStatus::CANCELLED, $this->wpAdapter->__( 'Cancelled', 'packeta' ), false ), | |
| 249 | + PacketStatus::COLLECTED => | |
| 250 | + new PacketStatus( PacketStatus::COLLECTED, $this->wpAdapter->__( 'Parcel has been collected', 'packeta' ), true ), | |
| 251 | + PacketStatus::CUSTOMS => | |
| 252 | + new PacketStatus( PacketStatus::CUSTOMS, $this->wpAdapter->__( 'Customs declaration process', 'packeta' ), true ), | |
| 253 | + PacketStatus::REVERSE_PACKET_ARRIVED => | |
| 254 | + new PacketStatus( PacketStatus::REVERSE_PACKET_ARRIVED, $this->wpAdapter->__( 'Reverse parcel has been accepted at our pick up point', 'packeta' ), true ), | |
| 255 | + PacketStatus::DELIVERY_ATTEMPT => | |
| 256 | + new PacketStatus( PacketStatus::DELIVERY_ATTEMPT, $this->wpAdapter->__( 'Unsuccessful delivery attempt of parcel', 'packeta' ), true ), | |
| 257 | + PacketStatus::REJECTED_BY_RECIPIENT => | |
| 258 | + new PacketStatus( PacketStatus::REJECTED_BY_RECIPIENT, $this->wpAdapter->__( 'Rejected by recipient response', 'packeta' ), true ), | |
| 259 | + PacketStatus::UNKNOWN => | |
| 260 | + new PacketStatus( PacketStatus::UNKNOWN, $this->wpAdapter->__( 'Unknown parcel status', 'packeta' ), false ), | |
| 177 | 261 | ]; |
| 178 | 262 | } |
| 179 | 263 | } |