| @@ -1,261 +1,246 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Class Metabox | |
| 4 | - * | |
| 5 | - * @package Packetery\Order | |
| 6 | - */ | |
| 7 | 2 | |
| 8 | 3 | declare( strict_types=1 ); |
| 9 | 4 | |
| 10 | 5 | namespace Packetery\Module\Order; |
| 11 | 6 | |
| 7 | +use Packetery\Core\CoreHelper; | |
| 12 | 8 | use Packetery\Core\Entity; |
| 13 | -use Packetery\Core\Helper; | |
| 14 | -use Packetery\Module\FormFactory; | |
| 15 | -use Packetery\Module\FormValidators; | |
| 9 | +use Packetery\Core\Validator; | |
| 10 | +use Packetery\Latte\Engine; | |
| 11 | +use Packetery\Module\Carrier\EntityRepository; | |
| 12 | +use Packetery\Module\Exception\InvalidCarrierException; | |
| 13 | +use Packetery\Module\Framework\WpAdapter; | |
| 16 | 14 | use Packetery\Module\Log; |
| 15 | +use Packetery\Module\Log\Page; | |
| 17 | 16 | use Packetery\Module\MessageManager; |
| 18 | -use Packetery\Module\Options; | |
| 17 | +use Packetery\Module\ModuleHelper; | |
| 18 | +use Packetery\Module\Options\OptionsProvider; | |
| 19 | 19 | use Packetery\Module\Plugin; |
| 20 | +use Packetery\Module\Transients; | |
| 20 | 21 | use Packetery\Module\WidgetOptionsBuilder; |
| 21 | -use PacketeryLatte\Engine; | |
| 22 | -use PacketeryNette\Forms\Form; | |
| 23 | -use PacketeryNette\Http\Request; | |
| 22 | +use Packetery\Nette\Forms; | |
| 23 | +use Packetery\Nette\Http\Request; | |
| 24 | 24 | use WC_Data_Exception; |
| 25 | 25 | use WC_Order; |
| 26 | 26 | |
| 27 | -/** | |
| 28 | - * Class Metabox | |
| 29 | - * | |
| 30 | - * @package Packetery\Order | |
| 31 | - */ | |
| 32 | 27 | class Metabox { |
| 33 | 28 | |
| 34 | - public const FIELD_WEIGHT = 'packetery_weight'; | |
| 35 | - private const FIELD_ORIGINAL_WEIGHT = 'packetery_original_weight'; | |
| 36 | - public const FIELD_WIDTH = 'packetery_width'; | |
| 37 | - public const FIELD_LENGTH = 'packetery_length'; | |
| 38 | - public const FIELD_HEIGHT = 'packetery_height'; | |
| 39 | - public const FIELD_ADULT_CONTENT = 'packetery_adult_content'; | |
| 40 | - public const FIELD_COD = 'packetery_COD'; | |
| 41 | - public const FIELD_VALUE = 'packetery_value'; | |
| 42 | - public const FIELD_DELIVER_ON = 'packetery_deliver_on'; | |
| 29 | + private const PART_ERROR = 'error'; | |
| 30 | + private const PART_CARRIER_CHANGE = 'carrierChange'; | |
| 31 | + private const PART_MAIN = 'main'; | |
| 43 | 32 | |
| 44 | 33 | /** |
| 45 | - * PacketeryLatte engine. | |
| 46 | - * | |
| 47 | 34 | * @var Engine |
| 48 | 35 | */ |
| 49 | - private $latte_engine; | |
| 36 | + private $latteEngine; | |
| 50 | 37 | |
| 51 | 38 | /** |
| 52 | - * Message manager. | |
| 53 | - * | |
| 54 | 39 | * @var MessageManager |
| 55 | 40 | */ |
| 56 | - private $message_manager; | |
| 41 | + private $messageManager; | |
| 57 | 42 | |
| 58 | 43 | /** |
| 59 | - * Helper. | |
| 60 | - * | |
| 61 | - * @var Helper | |
| 44 | + * @var CoreHelper | |
| 62 | 45 | */ |
| 63 | - private $helper; | |
| 46 | + private $coreHelper; | |
| 64 | 47 | |
| 65 | 48 | /** |
| 66 | - * Order form. | |
| 67 | - * | |
| 68 | - * @var Form | |
| 49 | + * @var Request | |
| 69 | 50 | */ |
| 70 | - private $order_form; | |
| 51 | + private $request; | |
| 71 | 52 | |
| 72 | 53 | /** |
| 73 | - * HTTP request. | |
| 74 | - * | |
| 75 | - * @var Request | |
| 54 | + * @var Forms\Form | |
| 76 | 55 | */ |
| 77 | - private $request; | |
| 56 | + private $form; | |
| 78 | 57 | |
| 79 | 58 | /** |
| 80 | - * Options provider. | |
| 81 | - * | |
| 82 | - * @var Options\Provider | |
| 59 | + * @var Form | |
| 83 | 60 | */ |
| 84 | - private $optionsProvider; | |
| 61 | + private $orderForm; | |
| 85 | 62 | |
| 86 | 63 | /** |
| 87 | - * Form factory. | |
| 88 | - * | |
| 89 | - * @var FormFactory | |
| 64 | + * @var OptionsProvider | |
| 90 | 65 | */ |
| 91 | - private $formFactory; | |
| 66 | + private $optionsProvider; | |
| 92 | 67 | |
| 93 | 68 | /** |
| 94 | - * Order repository. | |
| 95 | - * | |
| 96 | 69 | * @var Repository |
| 97 | 70 | */ |
| 98 | 71 | private $orderRepository; |
| 99 | 72 | |
| 100 | 73 | /** |
| 101 | - * Log page. | |
| 102 | - * | |
| 103 | 74 | * @var Log\Page |
| 104 | 75 | */ |
| 105 | 76 | private $logPage; |
| 106 | 77 | |
| 107 | 78 | /** |
| 108 | - * OrderFacade. | |
| 109 | - * | |
| 110 | 79 | * @var AttributeMapper |
| 111 | 80 | */ |
| 112 | 81 | private $mapper; |
| 113 | 82 | |
| 114 | 83 | /** |
| 115 | - * Widget options builder. | |
| 116 | - * | |
| 117 | 84 | * @var WidgetOptionsBuilder |
| 118 | 85 | */ |
| 119 | 86 | private $widgetOptionsBuilder; |
| 120 | 87 | |
| 121 | 88 | /** |
| 122 | - * Metabox constructor. | |
| 123 | - * | |
| 124 | - * @param Engine $latte_engine PacketeryLatte engine. | |
| 125 | - * @param MessageManager $message_manager Message manager. | |
| 126 | - * @param Helper $helper Helper. | |
| 127 | - * @param Request $request Http request. | |
| 128 | - * @param Options\Provider $optionsProvider Options provider. | |
| 129 | - * @param FormFactory $formFactory Form factory. | |
| 130 | - * @param Repository $orderRepository Order repository. | |
| 131 | - * @param Log\Page $logPage Log page. | |
| 132 | - * @param AttributeMapper $mapper AttributeMapper. | |
| 133 | - * @param WidgetOptionsBuilder $widgetOptionsBuilder Widget options builder. | |
| 89 | + * @var EntityRepository | |
| 134 | 90 | */ |
| 91 | + private $carrierRepository; | |
| 92 | + | |
| 93 | + /** | |
| 94 | + * @var Validator\Order | |
| 95 | + */ | |
| 96 | + private $orderValidator; | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * @var DetailCommonLogic | |
| 100 | + */ | |
| 101 | + private $detailCommonLogic; | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * @var CarrierModal | |
| 105 | + */ | |
| 106 | + private $carrierModal; | |
| 107 | + | |
| 108 | + /** | |
| 109 | + * @var WpAdapter | |
| 110 | + */ | |
| 111 | + private $wpAdapter; | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * @var PacketStatusResolver | |
| 115 | + */ | |
| 116 | + private $packetStatusResolver; | |
| 117 | + | |
| 135 | 118 | public function __construct( |
| 136 | - Engine $latte_engine, | |
| 137 | - MessageManager $message_manager, | |
| 138 | - Helper $helper, | |
| 119 | + Engine $latteEngine, | |
| 120 | + MessageManager $messageManager, | |
| 121 | + CoreHelper $coreHelper, | |
| 139 | 122 | Request $request, |
| 140 | - Options\Provider $optionsProvider, | |
| 141 | - FormFactory $formFactory, | |
| 123 | + OptionsProvider $optionsProvider, | |
| 142 | 124 | Repository $orderRepository, |
| 143 | - Log\Page $logPage, | |
| 125 | + Page $logPage, | |
| 144 | 126 | AttributeMapper $mapper, |
| 145 | - WidgetOptionsBuilder $widgetOptionsBuilder | |
| 127 | + WidgetOptionsBuilder $widgetOptionsBuilder, | |
| 128 | + EntityRepository $carrierRepository, | |
| 129 | + OrderValidatorFactory $orderValidatorFactory, | |
| 130 | + DetailCommonLogic $detailCommonLogic, | |
| 131 | + Form $orderForm, | |
| 132 | + CarrierModal $carrierModal, | |
| 133 | + WpAdapter $wpAdapter, | |
| 134 | + PacketStatusResolver $packetStatusResolver | |
| 146 | 135 | ) { |
| 147 | - $this->latte_engine = $latte_engine; | |
| 148 | - $this->message_manager = $message_manager; | |
| 149 | - $this->helper = $helper; | |
| 136 | + $this->latteEngine = $latteEngine; | |
| 137 | + $this->messageManager = $messageManager; | |
| 138 | + $this->coreHelper = $coreHelper; | |
| 150 | 139 | $this->request = $request; |
| 151 | 140 | $this->optionsProvider = $optionsProvider; |
| 152 | - $this->formFactory = $formFactory; | |
| 153 | 141 | $this->orderRepository = $orderRepository; |
| 154 | 142 | $this->logPage = $logPage; |
| 155 | 143 | $this->mapper = $mapper; |
| 156 | 144 | $this->widgetOptionsBuilder = $widgetOptionsBuilder; |
| 145 | + $this->carrierRepository = $carrierRepository; | |
| 146 | + $this->orderValidator = $orderValidatorFactory->create(); | |
| 147 | + $this->detailCommonLogic = $detailCommonLogic; | |
| 148 | + $this->orderForm = $orderForm; | |
| 149 | + $this->carrierModal = $carrierModal; | |
| 150 | + $this->wpAdapter = $wpAdapter; | |
| 151 | + $this->packetStatusResolver = $packetStatusResolver; | |
| 157 | 152 | } |
| 158 | 153 | |
| 159 | - /** | |
| 160 | - * Registers related hooks. | |
| 161 | - */ | |
| 162 | 154 | public function register(): void { |
| 163 | - add_action( | |
| 164 | - 'admin_init', | |
| 165 | - function () { | |
| 166 | - $this->order_form = $this->formFactory->create(); | |
| 167 | - $this->add_fields(); | |
| 168 | - } | |
| 169 | - ); | |
| 170 | 155 | add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); |
| 171 | - add_action( 'save_post', array( $this, 'save_fields' ) ); | |
| 172 | 156 | } |
| 173 | 157 | |
| 174 | - /** | |
| 175 | - * Add metaboxes | |
| 176 | - */ | |
| 177 | 158 | public function add_meta_boxes(): void { |
| 178 | - global $post; | |
| 159 | + if ( ! $this->detailCommonLogic->isPacketeryOrder() ) { | |
| 160 | + return; | |
| 161 | + } | |
| 179 | 162 | |
| 180 | - $order = $this->orderRepository->getById( (int) $post->ID ); | |
| 181 | - if ( null === $order ) { | |
| 163 | + $this->initializeForm(); | |
| 164 | + $parts = $this->prepareMetaboxParts(); | |
| 165 | + | |
| 166 | + if ( count( $parts ) > 0 ) { | |
| 167 | + add_meta_box( | |
| 168 | + 'packetery_metabox', | |
| 169 | + $this->wpAdapter->__( 'Packeta', 'packeta' ), | |
| 170 | + array( | |
| 171 | + $this, | |
| 172 | + 'render_metabox', | |
| 173 | + ), | |
| 174 | + ModuleHelper::isHposEnabled() ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order', | |
| 175 | + 'side', | |
| 176 | + 'high' | |
| 177 | + ); | |
| 178 | + } | |
| 179 | + } | |
| 180 | + | |
| 181 | + public function render_metabox(): void { | |
| 182 | + $parts = $this->prepareMetaboxParts(); | |
| 183 | + | |
| 184 | + if ( isset( $parts[ self::PART_ERROR ] ) ) { | |
| 185 | + ModuleHelper::renderString( $parts[ self::PART_ERROR ] ); | |
| 186 | + | |
| 182 | 187 | return; |
| 183 | 188 | } |
| 184 | 189 | |
| 185 | - add_meta_box( | |
| 186 | - 'packetery_metabox', | |
| 187 | - __( 'Packeta', 'packeta' ), | |
| 188 | - array( | |
| 189 | - $this, | |
| 190 | - 'render_metabox', | |
| 191 | - ), | |
| 192 | - 'shop_order', | |
| 193 | - 'side', | |
| 194 | - 'high' | |
| 195 | - ); | |
| 190 | + if ( isset( $parts[ self::PART_CARRIER_CHANGE ] ) ) { | |
| 191 | + ModuleHelper::renderString( $parts[ self::PART_CARRIER_CHANGE ] ); | |
| 192 | + } | |
| 193 | + if ( isset( $parts[ self::PART_CARRIER_CHANGE ], $parts[ self::PART_MAIN ] ) ) { | |
| 194 | + ModuleHelper::renderString( '<hr>' ); | |
| 195 | + } | |
| 196 | + if ( isset( $parts[ self::PART_MAIN ] ) ) { | |
| 197 | + ModuleHelper::renderString( $parts[ self::PART_MAIN ] ); | |
| 198 | + } | |
| 196 | 199 | } |
| 197 | 200 | |
| 198 | 201 | /** |
| 199 | - * Adds packetery fields to order form. | |
| 202 | + * @return array<string, string> | |
| 200 | 203 | */ |
| 201 | - public function add_fields(): void { | |
| 202 | - $this->order_form->addHidden( 'packetery_order_metabox_nonce' ); | |
| 203 | - $this->order_form->addText( self::FIELD_WEIGHT, __( 'Weight (kg)', 'packeta' ) ) | |
| 204 | - ->setRequired( false ) | |
| 205 | - ->addRule( $this->order_form::FLOAT, __( 'Provide numeric value!', 'packeta' ) ); | |
| 206 | - $this->order_form->addHidden( self::FIELD_ORIGINAL_WEIGHT ); | |
| 207 | - $this->order_form->addText( self::FIELD_WIDTH, __( 'Width (mm)', 'packeta' ) ) | |
| 208 | - ->setRequired( false ) | |
| 209 | - ->addRule( $this->order_form::FLOAT, __( 'Provide numeric value!', 'packeta' ) ); | |
| 210 | - $this->order_form->addText( self::FIELD_LENGTH, __( 'Length (mm)', 'packeta' ) ) | |
| 211 | - ->setRequired( false ) | |
| 212 | - ->addRule( $this->order_form::FLOAT, __( 'Provide numeric value!', 'packeta' ) ); | |
| 213 | - $this->order_form->addText( self::FIELD_HEIGHT, __( 'Height (mm)', 'packeta' ) ) | |
| 214 | - ->setRequired( false ) | |
| 215 | - ->addRule( $this->order_form::FLOAT, __( 'Provide numeric value!', 'packeta' ) ); | |
| 216 | - $this->order_form->addCheckbox( self::FIELD_ADULT_CONTENT, __( 'Adult content', 'packeta' ) ) | |
| 217 | - ->setRequired( false ); | |
| 218 | - $this->order_form->addText( self::FIELD_COD, __( 'Cash on delivery', 'packeta' ) ) | |
| 219 | - ->setRequired( false ) | |
| 220 | - ->addRule( $this->order_form::FLOAT ); | |
| 221 | - $this->order_form->addText( self::FIELD_VALUE, __( 'Order value', 'packeta' ) ) | |
| 222 | - ->setRequired( false ) | |
| 223 | - ->addRule( $this->order_form::FLOAT ); | |
| 224 | - $this->order_form->addText( self::FIELD_DELIVER_ON, __( 'Planned dispatch', 'packeta' ) ) | |
| 225 | - ->setHtmlAttribute( 'autocomplete', 'off' ) | |
| 226 | - ->setRequired( false ) | |
| 227 | - // translators: %s: Represents minimal date for delayed delivery. | |
| 228 | - ->addRule( [ FormValidators::class, 'dateIsLater' ], __( 'Date must be later than %s', 'packeta' ), wp_date( Helper::DATEPICKER_FORMAT ) ); | |
| 204 | + private function prepareMetaboxParts(): array { | |
| 205 | + static $partsCache; | |
| 229 | 206 | |
| 230 | - foreach ( Attribute::$pickupPointAttrs as $pickupPointAttr ) { | |
| 231 | - $this->order_form->addHidden( $pickupPointAttr['name'] ); | |
| 207 | + if ( isset( $partsCache ) ) { | |
| 208 | + return $partsCache; | |
| 232 | 209 | } |
| 233 | 210 | |
| 234 | - foreach ( Attribute::$homeDeliveryAttrs as $homeDeliveryAttr ) { | |
| 235 | - $this->order_form->addHidden( $homeDeliveryAttr['name'] ); | |
| 211 | + $orderId = $this->detailCommonLogic->getOrderId(); | |
| 212 | + if ( $orderId === null ) { | |
| 213 | + $partsCache = []; | |
| 214 | + | |
| 215 | + return $partsCache; | |
| 236 | 216 | } |
| 237 | 217 | |
| 238 | - $this->order_form->addButton( 'packetery_pick_pickup_point', __( 'Choose pickup point', 'packeta' ) ); | |
| 239 | - $this->order_form->addButton( 'packetery_pick_address', __( 'Check shipping address', 'packeta' ) ); | |
| 240 | - } | |
| 218 | + try { | |
| 219 | + $order = $this->orderRepository->getById( $orderId ); | |
| 220 | + } catch ( InvalidCarrierException $exception ) { | |
| 221 | + $partsCache = [ | |
| 222 | + self::PART_ERROR => $this->latteEngine->renderToString( | |
| 223 | + PACKETERY_PLUGIN_DIR . '/template/order/metabox-form-error.latte', | |
| 224 | + [ | |
| 225 | + 'errorMessage' => $exception->getMessage(), | |
| 226 | + ] | |
| 227 | + ), | |
| 228 | + ]; | |
| 241 | 229 | |
| 242 | - /** | |
| 243 | - * Renders metabox | |
| 244 | - */ | |
| 245 | - public function render_metabox(): void { | |
| 246 | - global $post; | |
| 230 | + return $partsCache; | |
| 231 | + } | |
| 247 | 232 | |
| 248 | - $wcOrder = $this->orderRepository->getWcOrderById( (int) $post->ID ); | |
| 249 | - if ( null === $wcOrder ) { | |
| 250 | - return; | |
| 233 | + $parts = []; | |
| 234 | + if ( $this->carrierModal->canBeDisplayed() ) { | |
| 235 | + $parts[ self::PART_CARRIER_CHANGE ] = $this->carrierModal->getMetaboxHtml(); | |
| 251 | 236 | } |
| 252 | 237 | |
| 253 | - $order = $this->orderRepository->getByWcOrder( $wcOrder ); | |
| 254 | - if ( null === $order ) { | |
| 255 | - return; | |
| 238 | + if ( $order === null ) { | |
| 239 | + $partsCache = $parts; | |
| 240 | + | |
| 241 | + return $partsCache; | |
| 256 | 242 | } |
| 257 | - $packetId = $order->getPacketId(); | |
| 258 | 243 | |
| 259 | 244 | $showLogsLink = null; |
| 260 | 245 | if ( $this->logPage->hasAnyRows( (int) $order->getNumber() ) ) { |
| 261 | 246 | $showLogsLink = $this->logPage->createLogListUrl( (int) $order->getNumber() ); |
| @@ -260,183 +245,341 @@ | ||
| 260 | 245 | if ( $this->logPage->hasAnyRows( (int) $order->getNumber() ) ) { |
| 261 | 246 | $showLogsLink = $this->logPage->createLogListUrl( (int) $order->getNumber() ); |
| 262 | 247 | } |
| 263 | 248 | |
| 264 | - if ( $packetId ) { | |
| 265 | - $packetCancelLink = $this->getOrderActionLink( $order, PacketActionsCommonLogic::ACTION_CANCEL_PACKET ); | |
| 266 | - $this->latte_engine->render( | |
| 267 | - PACKETERY_PLUGIN_DIR . '/template/order/metabox-overview.latte', | |
| 249 | + $packetClaimUrl = null; | |
| 250 | + if ( $order->isPacketClaimCreationPossible() ) { | |
| 251 | + $packetClaimUrl = $this->getOrderActionLink( $order, PacketActionsCommonLogic::ACTION_SUBMIT_PACKET_CLAIM ); | |
| 252 | + } | |
| 253 | + | |
| 254 | + $packetClaimCancelUrl = null; | |
| 255 | + $packetClaimTrackingUrl = null; | |
| 256 | + if ( $order->getPacketClaimId() !== null ) { | |
| 257 | + $packetClaimCancelUrl = $this->getOrderActionLink( | |
| 258 | + $order, | |
| 259 | + PacketActionsCommonLogic::ACTION_CANCEL_PACKET, | |
| 268 | 260 | [ |
| 269 | - 'packetCancelLink' => $packetCancelLink, | |
| 270 | - 'packet_id' => $packetId, | |
| 271 | - 'packet_tracking_url' => $this->helper->get_tracking_url( $packetId ), | |
| 272 | - 'showLogsLink' => $showLogsLink, | |
| 273 | - 'translations' => [ | |
| 274 | - 'packetTrackingOnline' => __( 'Packet tracking online', 'packeta' ), | |
| 275 | - 'showLogs' => __( 'Show logs', 'packeta' ), | |
| 261 | + PacketActionsCommonLogic::PARAM_PACKET_ID => $order->getPacketClaimId(), | |
| 262 | + ] | |
| 263 | + ); | |
| 264 | + $packetClaimTrackingUrl = $this->coreHelper->getTrackingUrl( $order->getPacketClaimId() ); | |
| 265 | + } | |
| 266 | + $runWizardUrl = $this->wpAdapter->adminUrl( "admin.php?page=wc-orders&action=edit&id={$orderId}&wizard-enabled=true&wizard-order-detail-edit-packet-enabled=true" ); | |
| 267 | + | |
| 268 | + $showRunWizardButton = (bool) $this->wpAdapter->applyFilters( 'packeta_order_detail_show_run_wizard_button', true ); | |
| 269 | + | |
| 270 | + $packetId = $order->getPacketId(); | |
| 271 | + if ( $packetId !== null ) { | |
| 272 | + $packetCancelLink = $this->getOrderActionLink( | |
| 273 | + $order, | |
| 274 | + PacketActionsCommonLogic::ACTION_CANCEL_PACKET, | |
| 275 | + [ | |
| 276 | + PacketActionsCommonLogic::PARAM_PACKET_ID => $packetId, | |
| 277 | + ] | |
| 278 | + ); | |
| 279 | + | |
| 280 | + $packetStatusTranslatedName = $this->packetStatusResolver->getTranslatedName( $order->getPacketStatus() ); | |
| 281 | + /** @var array<string, string> $statusClasses */ | |
| 282 | + $statusClasses = [ | |
| 283 | + 'received data' => 'received-data', | |
| 284 | + 'unknown' => 'unknown', | |
| 285 | + 'delivered' => 'delivered', | |
| 286 | + 'cancelled' => 'cancelled', | |
| 287 | + 'returned' => 'returned', | |
| 288 | + 'rejected by recipient' => 'rejected', | |
| 289 | + ]; | |
| 290 | + | |
| 291 | + $statusClass = 'delivery-status'; | |
| 292 | + $statusType = $order->getPacketStatus(); | |
| 293 | + | |
| 294 | + if ( isset( $statusClasses[ $statusType ] ) ) { | |
| 295 | + $statusClass = $statusClasses[ $statusType ]; | |
| 296 | + } | |
| 297 | + | |
| 298 | + $invalidFields = $this->orderForm->getInvalidFieldsFromValidationResult( $this->orderValidator->validate( $order ) ); | |
| 299 | + $invalidFieldsMessage = $this->orderForm->getInvalidFieldsMessageFromValidationResult( $invalidFields, $order ); | |
| 300 | + | |
| 301 | + $parts[ self::PART_MAIN ] = $this->latteEngine->renderToString( | |
| 302 | + PACKETERY_PLUGIN_DIR . '/template/order/metabox-common.latte', | |
| 303 | + [ | |
| 304 | + 'order' => $order, | |
| 305 | + 'showConsignPasswordForZBox' => $this->optionsProvider->isShowConsignPasswordForZBoxEnabled(), | |
| 306 | + 'packetStatusTranslatedName' => $packetStatusTranslatedName, | |
| 307 | + 'statusClass' => $statusClass, | |
| 308 | + 'isPacketSubmissionPossible' => false, | |
| 309 | + 'orderWarningFields' => [], | |
| 310 | + 'invalidFieldsMessage' => $invalidFieldsMessage, | |
| 311 | + 'packetCancelLink' => $packetCancelLink, | |
| 312 | + 'packetTrackingUrl' => $this->coreHelper->getTrackingUrl( $packetId ), | |
| 313 | + 'packetClaimTrackingUrl' => $packetClaimTrackingUrl, | |
| 314 | + 'showLogsLink' => $showLogsLink, | |
| 315 | + 'packetClaimUrl' => $packetClaimUrl, | |
| 316 | + 'packetClaimCancelUrl' => $packetClaimCancelUrl, | |
| 317 | + 'runWizardUrl' => $runWizardUrl, | |
| 318 | + 'showRunWizardButton' => $showRunWizardButton, | |
| 319 | + 'storedUntil' => $this->coreHelper->getStringFromDateTime( $order->getStoredUntil(), CoreHelper::DATEPICKER_FORMAT ), | |
| 320 | + 'translations' => [ | |
| 321 | + 'packetTrackingOnline' => $this->wpAdapter->__( 'Packet tracking online', 'packeta' ), | |
| 322 | + 'packetClaimTrackingOnline' => $this->wpAdapter->__( 'Packet claim tracking', 'packeta' ), | |
| 323 | + 'showLogs' => $this->wpAdapter->__( 'Show logs', 'packeta' ), | |
| 276 | 324 | // translators: %s: Order number. |
| 277 | - 'reallyCancelPacketHeading' => sprintf( __( 'Order #%s', 'packeta' ), $order->getCustomNumber() ), | |
| 325 | + 'reallyCancelPacketHeading' => sprintf( $this->wpAdapter->__( 'Order #%s', 'packeta' ), $order->getCustomNumber() ), | |
| 278 | 326 | // translators: %s: Packet number. |
| 279 | - 'reallyCancelPacket' => sprintf( __( 'Do you really wish to cancel parcel number %s?', 'packeta' ), $packetId ), | |
| 280 | - 'cancelPacket' => __( 'Cancel packet', 'packeta' ), | |
| 327 | + 'reallyCancelPacket' => sprintf( $this->wpAdapter->__( 'Do you really wish to cancel parcel number %s?', 'packeta' ), $packetId ), | |
| 328 | + // translators: %s: Packet claim number. | |
| 329 | + 'reallyCancelPacketClaim' => sprintf( $this->wpAdapter->__( 'Do you really wish to cancel packet claim number %s?', 'packeta' ), $order->getPacketClaimId() ), | |
| 330 | + | |
| 331 | + 'cancelPacket' => $this->wpAdapter->__( 'Cancel packet', 'packeta' ), | |
| 332 | + 'createPacketClaim' => $this->wpAdapter->__( 'Create packet claim', 'packeta' ), | |
| 333 | + 'printPacketLabel' => $this->wpAdapter->__( 'Print packet label', 'packeta' ), | |
| 334 | + 'printPacketClaimLabel' => $this->wpAdapter->__( 'Print packet claim label', 'packeta' ), | |
| 335 | + 'cancelPacketClaim' => $this->wpAdapter->__( 'Cancel packet claim', 'packeta' ), | |
| 336 | + 'packetClaimPassword' => $this->wpAdapter->__( 'Packet claim password', 'packeta' ), | |
| 337 | + 'submissionPassword' => $this->wpAdapter->__( 'submission password', 'packeta' ), | |
| 338 | + 'zboxConsignPassword' => $this->wpAdapter->__( 'Consignment code', 'packeta' ), | |
| 339 | + 'setStoredUntil' => $this->wpAdapter->__( 'Set the pickup date extension', 'packeta' ), | |
| 340 | + 'runWizard' => $this->wpAdapter->__( 'Run options wizard', 'packeta' ), | |
| 281 | 341 | ], |
| 282 | 342 | ] |
| 283 | 343 | ); |
| 284 | 344 | |
| 285 | - return; | |
| 345 | + $partsCache = $parts; | |
| 346 | + | |
| 347 | + return $partsCache; | |
| 286 | 348 | } |
| 287 | 349 | |
| 288 | - $this->order_form->setDefaults( | |
| 289 | - [ | |
| 290 | - 'packetery_order_metabox_nonce' => wp_create_nonce(), | |
| 291 | - self::FIELD_WEIGHT => $order->getFinalWeight(), | |
| 292 | - self::FIELD_ORIGINAL_WEIGHT => $order->getFinalWeight(), | |
| 293 | - self::FIELD_WIDTH => $order->getWidth(), | |
| 294 | - self::FIELD_LENGTH => $order->getLength(), | |
| 295 | - self::FIELD_HEIGHT => $order->getHeight(), | |
| 296 | - self::FIELD_ADULT_CONTENT => $order->containsAdultContent(), | |
| 297 | - self::FIELD_COD => $order->getCod(), | |
| 298 | - self::FIELD_VALUE => $order->getValue(), | |
| 299 | - self::FIELD_DELIVER_ON => $this->helper->getStringFromDateTime( $order->getDeliverOn(), Helper::DATEPICKER_FORMAT ), | |
| 300 | - ] | |
| 350 | + $unit = $this->optionsProvider->getDimensionsUnit(); | |
| 351 | + $this->orderForm->setDefaults( | |
| 352 | + $this->form, | |
| 353 | + $order->getFinalWeight(), | |
| 354 | + $order->getCalculatedWeight(), | |
| 355 | + $unit === OptionsProvider::DIMENSIONS_UNIT_CM ? ModuleHelper::convertToCentimeters( (int) $order->getLength() ) : $order->getLength(), | |
| 356 | + $unit === OptionsProvider::DIMENSIONS_UNIT_CM ? ModuleHelper::convertToCentimeters( (int) $order->getWidth() ) : $order->getWidth(), | |
| 357 | + $unit === OptionsProvider::DIMENSIONS_UNIT_CM ? ModuleHelper::convertToCentimeters( (int) $order->getHeight() ) : $order->getHeight(), | |
| 358 | + $order->getFinalCod(), | |
| 359 | + $order->getCalculatedCod(), | |
| 360 | + $order->getFinalValue(), | |
| 361 | + $order->getCalculatedValue(), | |
| 362 | + $order->containsAdultContent(), | |
| 363 | + $this->coreHelper->getStringFromDateTime( $order->getDeliverOn(), CoreHelper::DATEPICKER_FORMAT ) | |
| 301 | 364 | ); |
| 302 | 365 | |
| 303 | - $prev_invalid_values = get_transient( 'packetery_metabox_nette_form_prev_invalid_values' ); | |
| 304 | - if ( $prev_invalid_values ) { | |
| 305 | - $this->order_form->setValues( $prev_invalid_values ); | |
| 306 | - $this->order_form->validate(); | |
| 366 | + $prevInvalidValues = get_transient( Transients::METABOX_NETTE_FORM_PREV_INVALID_VALUES ); | |
| 367 | + if ( $prevInvalidValues !== null && $prevInvalidValues !== false ) { | |
| 368 | + $this->form->setValues( $prevInvalidValues ); | |
| 369 | + $this->form->validate(); | |
| 307 | 370 | } |
| 308 | - delete_transient( 'packetery_metabox_nette_form_prev_invalid_values' ); | |
| 371 | + delete_transient( Transients::METABOX_NETTE_FORM_PREV_INVALID_VALUES ); | |
| 309 | 372 | |
| 310 | - $showSubmitPacketButton = null !== $order->getFinalWeight() && $order->getFinalWeight() > 0; | |
| 311 | - $packetSubmitUrl = $this->getOrderActionLink( $order, PacketActionsCommonLogic::ACTION_SUBMIT_PACKET ); | |
| 373 | + $isPacketSubmissionPossible = $this->orderValidator->isValid( $order ); | |
| 374 | + $packetSubmitUrl = $this->getOrderActionLink( $order, PacketActionsCommonLogic::ACTION_SUBMIT_PACKET ); | |
| 312 | 375 | |
| 313 | 376 | $showWidgetButton = $order->isPickupPointDelivery(); |
| 314 | 377 | $widgetButtonError = null; |
| 315 | - if ( $order->getCarrierCode() === null ) { | |
| 316 | - // This means that more accurate carrier id could not be determined. See Order\Builder. | |
| 317 | - $showWidgetButton = false; | |
| 318 | - $widgetButtonError = sprintf( | |
| 319 | - // translators: %s is country code. | |
| 320 | - __( | |
| 321 | - 'The pickup point cannot be changed because the selected carrier does not deliver to country "%s". First, change the country of delivery in the shipping address.', | |
| 322 | - 'packeta' | |
| 323 | - ), | |
| 324 | - $order->getShippingCountry() | |
| 325 | - ); | |
| 378 | + $shippingCountry = $order->getShippingCountry(); | |
| 379 | + $showHdWidget = $order->isHomeDelivery() && in_array( $shippingCountry, Entity\Carrier::ADDRESS_VALIDATION_COUNTRIES, true ); | |
| 380 | + if ( | |
| 381 | + $shippingCountry === null || | |
| 382 | + ! $this->carrierRepository->isValidForCountry( $order->getCarrier()->getId(), $shippingCountry ) | |
| 383 | + ) { | |
| 384 | + if ( $order->isPickupPointDelivery() ) { | |
| 385 | + $showWidgetButton = false; | |
| 386 | + if ( $shippingCountry === null ) { | |
| 387 | + $widgetButtonError = $this->wpAdapter->__( | |
| 388 | + 'The pickup point cannot be changed because the shipping address has no country set. First, change the country of delivery in the shipping address.', | |
| 389 | + 'packeta' | |
| 390 | + ); | |
| 391 | + } else { | |
| 392 | + $widgetButtonError = sprintf( | |
| 393 | + // translators: %s is country code. | |
| 394 | + $this->wpAdapter->__( | |
| 395 | + 'The pickup point cannot be changed because the selected carrier does not deliver to country "%s". First, change the country of delivery in the shipping address.', | |
| 396 | + 'packeta' | |
| 397 | + ), | |
| 398 | + $shippingCountry | |
| 399 | + ); | |
| 400 | + } | |
| 401 | + } elseif ( in_array( $order->getCarrier()->getCountry(), Entity\Carrier::ADDRESS_VALIDATION_COUNTRIES, true ) ) { | |
| 402 | + $showHdWidget = false; | |
| 403 | + if ( $shippingCountry === null ) { | |
| 404 | + $widgetButtonError = $this->wpAdapter->__( | |
| 405 | + 'The address cannot be validated because the shipping address has no country set. First, change the country of delivery in the shipping address.', | |
| 406 | + 'packeta' | |
| 407 | + ); | |
| 408 | + } else { | |
| 409 | + $widgetButtonError = sprintf( | |
| 410 | + // translators: %s is country code. | |
| 411 | + $this->wpAdapter->__( | |
| 412 | + 'The address cannot be validated because the selected carrier does not deliver to country "%s". First, change the country of delivery in the shipping address.', | |
| 413 | + 'packeta' | |
| 414 | + ), | |
| 415 | + $shippingCountry | |
| 416 | + ); | |
| 417 | + } | |
| 418 | + } | |
| 326 | 419 | } |
| 327 | 420 | |
| 328 | - $showHdWidget = $order->isHomeDelivery() && in_array( $order->getShippingCountry(), Entity\Carrier::ADDRESS_VALIDATION_COUNTRIES, true ); | |
| 329 | - $this->latte_engine->render( | |
| 421 | + $invalidFields = $this->orderForm->getInvalidFieldsFromValidationResult( $this->orderValidator->validate( $order ) ); | |
| 422 | + $invalidFieldsMessage = $this->orderForm->getInvalidFieldsMessageFromValidationResult( $invalidFields, $order ); | |
| 423 | + | |
| 424 | + $parts[ self::PART_MAIN ] = $this->latteEngine->renderToString( | |
| 330 | 425 | PACKETERY_PLUGIN_DIR . '/template/order/metabox-form.latte', |
| 331 | 426 | [ |
| 332 | - 'form' => $this->order_form, | |
| 333 | - 'order' => $order, | |
| 334 | - 'showWidgetButton' => $showWidgetButton, | |
| 335 | - 'widgetButtonError' => $widgetButtonError, | |
| 336 | - 'showHdWidget' => $showHdWidget, | |
| 337 | - 'showSubmitPacketButton' => $showSubmitPacketButton, | |
| 338 | - 'packetSubmitUrl' => $packetSubmitUrl, | |
| 339 | - 'orderCurrency' => get_woocommerce_currency_symbol( $order->getCurrency() ), | |
| 340 | - 'isCodPayment' => $wcOrder->get_payment_method() === $this->optionsProvider->getCodPaymentMethod(), | |
| 341 | - 'logo' => plugin_dir_url( PACKETERY_PLUGIN_DIR . '/packeta.php' ) . 'public/packeta-symbol.png', | |
| 342 | - 'showLogsLink' => $showLogsLink, | |
| 343 | - 'hasOrderManualWeight' => $order->hasManualWeight(), | |
| 344 | - 'isPacketaPickupPoint' => $order->isPacketaInternalPickupPoint(), | |
| 345 | - 'translations' => [ | |
| 346 | - 'showLogs' => __( 'Show logs', 'packeta' ), | |
| 347 | - 'weightIsManual' => __( 'Weight is manually set. To calculate weight remove field content and save.', 'packeta' ), | |
| 348 | - 'submitPacket' => __( 'Submit to packeta', 'packeta' ), | |
| 427 | + 'form' => $this->form, | |
| 428 | + 'order' => $order, | |
| 429 | + 'showConsignPasswordForZBox' => $this->optionsProvider->isShowConsignPasswordForZBoxEnabled(), | |
| 430 | + 'showWidgetButton' => $showWidgetButton, | |
| 431 | + 'widgetButtonError' => $widgetButtonError, | |
| 432 | + 'showHdWidget' => $showHdWidget, | |
| 433 | + 'isPacketSubmissionPossible' => $isPacketSubmissionPossible, | |
| 434 | + 'orderWarningFields' => $invalidFields, | |
| 435 | + 'invalidFieldsMessage' => $invalidFieldsMessage, | |
| 436 | + 'packetCancelLink' => null, | |
| 437 | + 'packetTrackingUrl' => null, | |
| 438 | + 'packetStatusTranslatedName' => null, | |
| 439 | + 'packetSubmitUrl' => $packetSubmitUrl, | |
| 440 | + 'packetClaimTrackingUrl' => $packetClaimTrackingUrl, | |
| 441 | + 'packetClaimUrl' => $packetClaimUrl, | |
| 442 | + 'packetClaimCancelUrl' => $packetClaimCancelUrl, | |
| 443 | + 'runWizardUrl' => $runWizardUrl, | |
| 444 | + 'showRunWizardButton' => $showRunWizardButton, | |
| 445 | + 'orderCurrency' => get_woocommerce_currency_symbol( $order->getCurrency() ), | |
| 446 | + 'isCodPayment' => $order->hasCod(), | |
| 447 | + 'allowsAdultContent' => $order->allowsAdultContent(), | |
| 448 | + 'requiresSizeDimensions' => $order->getCarrier()->requiresSize(), | |
| 449 | + 'logo' => plugin_dir_url( PACKETERY_PLUGIN_DIR . '/packeta.php' ) . 'public/images/packeta-symbol.png', | |
| 450 | + 'showLogsLink' => $showLogsLink, | |
| 451 | + 'hasOrderManualWeight' => $order->hasManualWeight(), | |
| 452 | + 'hasOrderManualCod' => $order->hasManualCod(), | |
| 453 | + 'hasOrderManualValue' => $order->hasManualValue(), | |
| 454 | + 'isPacketaPickupPoint' => $order->isPacketaInternalPickupPoint(), | |
| 455 | + 'pickupPointAttributes' => Attribute::$pickupPointAttributes, | |
| 456 | + 'homeDeliveryAttributes' => Attribute::$homeDeliveryAttributes, | |
| 457 | + 'translations' => [ | |
| 458 | + 'packetSubmissionValidationErrorTooltip' => $this->wpAdapter->__( 'It is not possible to submit the shipment because all the information required for this shipment is not filled:', 'packeta' ), | |
| 459 | + 'showLogs' => $this->wpAdapter->__( 'Show logs', 'packeta' ), | |
| 460 | + 'weightIsManual' => $this->wpAdapter->__( 'Weight is manually set. To calculate weight remove field content and save.', 'packeta' ), | |
| 461 | + 'codIsManual' => $this->wpAdapter->__( 'COD value is manually set. To calculate the value remove field content and save.', 'packeta' ), | |
| 462 | + 'valueIsManual' => $this->wpAdapter->__( 'Order value is manually set. To calculate the value remove field content and save.', 'packeta' ), | |
| 463 | + 'submitPacket' => $this->wpAdapter->__( 'Submit to Packeta', 'packeta' ), | |
| 464 | + 'runWizard' => $this->wpAdapter->__( 'Run options wizard', 'packeta' ), | |
| 465 | + 'packetClaimTrackingOnline' => $this->wpAdapter->__( 'Packet claim tracking', 'packeta' ), | |
| 466 | + 'printPacketClaimLabel' => $this->wpAdapter->__( 'Print packet claim label', 'packeta' ), | |
| 467 | + 'cancelPacketClaim' => $this->wpAdapter->__( 'Cancel packet claim', 'packeta' ), | |
| 468 | + 'packetClaimPassword' => $this->wpAdapter->__( 'Packet claim password', 'packeta' ), | |
| 469 | + 'submissionPassword' => $this->wpAdapter->__( 'submission password', 'packeta' ), | |
| 470 | + 'zboxConsignPassword' => $this->wpAdapter->__( 'Z-BOX consign password', 'packeta' ), | |
| 471 | + // translators: %s: Order number. | |
| 472 | + 'reallyCancelPacketHeading' => sprintf( $this->wpAdapter->__( 'Order #%s', 'packeta' ), $order->getCustomNumber() ), | |
| 473 | + // translators: %s: Packet claim number. | |
| 474 | + 'reallyCancelPacketClaim' => sprintf( $this->wpAdapter->__( 'Do you really wish to cancel packet claim number %s?', 'packeta' ), $order->getPacketClaimId() ), | |
| 349 | 475 | ], |
| 350 | 476 | ] |
| 351 | 477 | ); |
| 478 | + | |
| 479 | + $partsCache = $parts; | |
| 480 | + | |
| 481 | + return $partsCache; | |
| 352 | 482 | } |
| 353 | 483 | |
| 354 | 484 | /** |
| 355 | 485 | * Saves added packetery form fields to order metas. |
| 356 | 486 | * |
| 357 | - * @param mixed $orderId Order id. | |
| 487 | + * @param Entity\Order $order Order. | |
| 488 | + * @param WC_Order $wcOrder WC Order. | |
| 358 | 489 | * |
| 359 | - * @return mixed Order id. | |
| 490 | + * @return void | |
| 360 | 491 | * @throws WC_Data_Exception When invalid data are passed during shipping address update. |
| 361 | 492 | */ |
| 362 | - public function save_fields( $orderId ) { | |
| 363 | - $order = $this->orderRepository->getById( $orderId ); | |
| 493 | + public function saveFields( Entity\Order $order, WC_Order $wcOrder ): void { | |
| 494 | + $this->initializeForm(); | |
| 495 | + | |
| 496 | + $orderId = (int) $order->getNumber(); | |
| 364 | 497 | if ( |
| 365 | - null === $order || | |
| 366 | 498 | ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || |
| 367 | - null === $this->request->getPost( 'packetery_order_metabox_nonce' ) | |
| 499 | + $this->request->getPost( 'packetery_order_metabox_nonce' ) === null | |
| 368 | 500 | ) { |
| 369 | - return $orderId; | |
| 501 | + return; | |
| 370 | 502 | } |
| 371 | 503 | |
| 372 | - if ( false === $this->order_form->isValid() ) { | |
| 373 | - set_transient( 'packetery_metabox_nette_form_prev_invalid_values', $this->order_form->getValues( true ) ); | |
| 374 | - $this->message_manager->flash_message( __( 'Packeta: entered data is not valid!', 'packeta' ), MessageManager::TYPE_ERROR ); | |
| 504 | + if ( $this->form->isValid() === false ) { | |
| 505 | + set_transient( Transients::METABOX_NETTE_FORM_PREV_INVALID_VALUES, $this->form->getValues( 'array' ) ); | |
| 506 | + $this->messageManager->flash_message( $this->wpAdapter->__( 'Packeta: entered data is not valid!', 'packeta' ), MessageManager::TYPE_ERROR ); | |
| 375 | 507 | |
| 376 | - return $orderId; | |
| 508 | + return; | |
| 377 | 509 | } |
| 510 | + /** @var array<string, string|float|int|true|null> $formValues */ | |
| 511 | + $formValues = $this->form->getValues( 'array' ); | |
| 378 | 512 | |
| 379 | - $values = $this->order_form->getValues( 'array' ); | |
| 513 | + if ( wp_verify_nonce( $formValues['packetery_order_metabox_nonce'] ) !== 1 ) { | |
| 514 | + $this->messageManager->flash_message( $this->wpAdapter->__( 'Session has expired! Please try again.', 'packeta' ), MessageManager::TYPE_ERROR ); | |
| 380 | 515 | |
| 381 | - if ( ! wp_verify_nonce( $values['packetery_order_metabox_nonce'] ) ) { | |
| 382 | - $this->message_manager->flash_message( __( 'Session has expired! Please try again.', 'packeta' ), MessageManager::TYPE_ERROR ); | |
| 383 | - | |
| 384 | - return $orderId; | |
| 516 | + return; | |
| 385 | 517 | } |
| 386 | 518 | |
| 387 | 519 | if ( ! current_user_can( 'edit_post', $orderId ) ) { |
| 388 | - $this->message_manager->flash_message( __( 'You do not have sufficient rights to make changes!', 'packeta' ), MessageManager::TYPE_ERROR ); | |
| 520 | + $this->messageManager->flash_message( $this->wpAdapter->__( 'You do not have sufficient rights to make changes!', 'packeta' ), MessageManager::TYPE_ERROR ); | |
| 389 | 521 | |
| 390 | - return $orderId; | |
| 522 | + return; | |
| 391 | 523 | } |
| 392 | 524 | |
| 393 | - $propsToSave = [ | |
| 394 | - self::FIELD_WIDTH => ( is_numeric( $values[ self::FIELD_WIDTH ] ) ? (float) number_format( $values[ self::FIELD_WIDTH ], 0, '.', '' ) : null ), | |
| 395 | - self::FIELD_LENGTH => ( is_numeric( $values[ self::FIELD_LENGTH ] ) ? (float) number_format( $values[ self::FIELD_LENGTH ], 0, '.', '' ) : null ), | |
| 396 | - self::FIELD_HEIGHT => ( is_numeric( $values[ self::FIELD_HEIGHT ] ) ? (float) number_format( $values[ self::FIELD_HEIGHT ], 0, '.', '' ) : null ), | |
| 397 | - ]; | |
| 525 | + $propsToSave = []; | |
| 526 | + foreach ( [ Form::FIELD_LENGTH, Form::FIELD_WIDTH, Form::FIELD_HEIGHT ] as $dimension ) { | |
| 527 | + $propsToSave[ $dimension ] = $this->optionsProvider->getSanitizedDimensionValueInMm( $formValues[ $dimension ] ); | |
| 528 | + } | |
| 398 | 529 | |
| 399 | - if ( ! is_numeric( $values[ self::FIELD_WEIGHT ] ) ) { | |
| 400 | - $propsToSave[ self::FIELD_WEIGHT ] = null; | |
| 401 | - } elseif ( (float) $values[ self::FIELD_WEIGHT ] !== (float) $values[ self::FIELD_ORIGINAL_WEIGHT ] ) { | |
| 402 | - $propsToSave[ self::FIELD_WEIGHT ] = (float) $values[ self::FIELD_WEIGHT ]; | |
| 530 | + $formWeightEqualsToCalculated = (float) $formValues[ Form::FIELD_WEIGHT ] === (float) $formValues[ Form::FIELD_ORIGINAL_WEIGHT ]; | |
| 531 | + if ( ! is_numeric( $formValues[ Form::FIELD_WEIGHT ] ) || $formWeightEqualsToCalculated ) { | |
| 532 | + $propsToSave[ Form::FIELD_WEIGHT ] = null; | |
| 533 | + } else { | |
| 534 | + $propsToSave[ Form::FIELD_WEIGHT ] = (float) $formValues[ Form::FIELD_WEIGHT ]; | |
| 403 | 535 | } |
| 536 | + $order->setWeight( $propsToSave[ Form::FIELD_WEIGHT ] ); | |
| 404 | 537 | |
| 405 | - if ( $values[ Attribute::POINT_ID ] && $order->isPickupPointDelivery() ) { | |
| 406 | - /** | |
| 407 | - * Cannot be null due to the condition at the beginning of the method. | |
| 408 | - * | |
| 409 | - * @var WC_Order $wcOrder | |
| 410 | - */ | |
| 411 | - $wcOrder = $this->orderRepository->getWcOrderById( (int) $orderId ); | |
| 412 | - foreach ( Attribute::$pickupPointAttrs as $pickupPointAttr ) { | |
| 413 | - $value = $values[ $pickupPointAttr['name'] ]; | |
| 538 | + if ( $formValues[ Attribute::POINT_ID ] && $order->isPickupPointDelivery() ) { | |
| 539 | + foreach ( Attribute::$pickupPointAttributes as $pickupPointAttr ) { | |
| 540 | + $pickupPointValue = $formValues[ $pickupPointAttr['name'] ]; | |
| 414 | 541 | |
| 415 | - if ( Attribute::CARRIER_ID === $pickupPointAttr['name'] ) { | |
| 416 | - $value = ( ! empty( $values[ Attribute::CARRIER_ID ] ) ? $values[ Attribute::CARRIER_ID ] : $order->getCarrierId() ); | |
| 542 | + if ( $pickupPointAttr['name'] === Attribute::CARRIER_ID ) { | |
| 543 | + if ( isset( $formValues[ Attribute::CARRIER_ID ] ) && $formValues[ Attribute::CARRIER_ID ] !== '' ) { | |
| 544 | + $pickupPointValue = $formValues[ Attribute::CARRIER_ID ]; | |
| 545 | + } else { | |
| 546 | + $pickupPointValue = $order->getCarrier()->getId(); | |
| 547 | + } | |
| 417 | 548 | } |
| 418 | 549 | |
| 419 | - $propsToSave[ $pickupPointAttr['name'] ] = $value; | |
| 550 | + $propsToSave[ $pickupPointAttr['name'] ] = $pickupPointValue; | |
| 420 | 551 | |
| 421 | 552 | if ( $this->optionsProvider->replaceShippingAddressWithPickupPointAddress() ) { |
| 422 | - $this->mapper->toWcOrderShippingAddress( $wcOrder, $pickupPointAttr['name'], (string) $value ); | |
| 553 | + $this->mapper->toWcOrderShippingAddress( $wcOrder, $pickupPointAttr['name'], (string) $pickupPointValue ); | |
| 423 | 554 | } |
| 424 | 555 | } |
| 425 | - $wcOrder->save(); | |
| 426 | 556 | } |
| 427 | 557 | |
| 428 | - if ( '1' === $values[ Attribute::ADDRESS_IS_VALIDATED ] && $order->isHomeDelivery() ) { | |
| 429 | - $address = $this->mapper->toValidatedAddress( $values ); | |
| 558 | + if ( $formValues[ Attribute::ADDRESS_IS_VALIDATED ] === '1' && $order->isHomeDelivery() ) { | |
| 559 | + $address = $this->mapper->toValidatedAddress( $formValues ); | |
| 430 | 560 | $order->setDeliveryAddress( $address ); |
| 431 | 561 | $order->setAddressValidated( true ); |
| 432 | 562 | } |
| 433 | 563 | |
| 434 | - $order->setAdultContent( $values[ self::FIELD_ADULT_CONTENT ] ); | |
| 435 | - $order->setCod( is_numeric( $values[ self::FIELD_COD ] ) ? Helper::simplifyFloat( $values[ self::FIELD_COD ], 10 ) : null ); | |
| 436 | - $order->setValue( is_numeric( $values[ self::FIELD_VALUE ] ) ? Helper::simplifyFloat( $values[ self::FIELD_VALUE ], 10 ) : null ); | |
| 437 | - $order->setDeliverOn( $this->helper->getDateTimeFromString( $values[ self::FIELD_DELIVER_ON ] ) ); | |
| 564 | + $order->setAdultContent( $formValues[ Form::FIELD_ADULT_CONTENT ] ); | |
| 438 | 565 | |
| 566 | + $formCodEqualsToCalculated = (float) $formValues[ Form::FIELD_COD ] === (float) $formValues[ Form::FIELD_CALCULATED_COD ]; | |
| 567 | + if ( ! is_numeric( $formValues[ Form::FIELD_COD ] ) || $formCodEqualsToCalculated ) { | |
| 568 | + $order->setManualCod( null ); | |
| 569 | + } else { | |
| 570 | + $order->setManualCod( is_numeric( $formValues[ Form::FIELD_COD ] ) ? CoreHelper::simplifyFloat( $formValues[ Form::FIELD_COD ], 10 ) : null ); | |
| 571 | + } | |
| 572 | + | |
| 573 | + $formValueEqualsToCalculated = (float) $formValues[ Form::FIELD_VALUE ] === (float) $formValues[ Form::FIELD_CALCULATED_VALUE ]; | |
| 574 | + if ( ! is_numeric( $formValues[ Form::FIELD_VALUE ] ) || $formValueEqualsToCalculated ) { | |
| 575 | + $order->setManualValue( null ); | |
| 576 | + } else { | |
| 577 | + $order->setManualValue( is_numeric( $formValues[ Form::FIELD_VALUE ] ) ? CoreHelper::simplifyFloat( $formValues[ Form::FIELD_VALUE ], 10 ) : null ); | |
| 578 | + } | |
| 579 | + | |
| 580 | + $order->setDeliverOn( $this->coreHelper->getDateTimeFromString( $formValues[ Form::FIELD_DELIVER_ON ] ) ); | |
| 581 | + | |
| 439 | 582 | $orderSize = $this->mapper->toOrderSize( $order, $propsToSave ); |
| 440 | 583 | $order->setSize( $orderSize ); |
| 441 | 584 | |
| 442 | 585 | $pickupPoint = $this->mapper->toOrderEntityPickupPoint( $order, $propsToSave ); |
| @@ -441,23 +584,25 @@ | ||
| 441 | 584 | |
| 442 | 585 | $pickupPoint = $this->mapper->toOrderEntityPickupPoint( $order, $propsToSave ); |
| 443 | 586 | $order->setPickupPoint( $pickupPoint ); |
| 444 | 587 | |
| 445 | - $this->orderRepository->save( $order ); | |
| 446 | - | |
| 447 | - return $orderId; | |
| 588 | + $updatedRowCount = $this->orderRepository->save( $order ); | |
| 589 | + if ( $updatedRowCount === false ) { | |
| 590 | + $this->messageManager->flash_message( | |
| 591 | + (string) $this->wpAdapter->__( 'An error occurred while saving the order. More details in WC log.', 'packeta' ), | |
| 592 | + MessageManager::TYPE_ERROR | |
| 593 | + ); | |
| 594 | + } | |
| 448 | 595 | } |
| 449 | 596 | |
| 450 | 597 | /** |
| 451 | 598 | * Creates pickup point picker settings. |
| 452 | 599 | * |
| 453 | - * @return array|null | |
| 600 | + * @return array<string, array|string|null> | |
| 454 | 601 | */ |
| 455 | 602 | public function getPickupPointWidgetSettings(): ?array { |
| 456 | - global $post; | |
| 457 | - | |
| 458 | - $order = $this->orderRepository->getById( (int) $post->ID ); | |
| 459 | - if ( null === $order || false === $order->isPickupPointDelivery() ) { | |
| 603 | + $order = $this->detailCommonLogic->getOrder(); | |
| 604 | + if ( $order === null || $order->isPickupPointDelivery() === false || $order->getShippingCountry() === null ) { | |
| 460 | 605 | return null; |
| 461 | 606 | } |
| 462 | 607 | |
| 463 | 608 | $widgetOptions = $this->widgetOptionsBuilder->createPickupPointForAdmin( $order ); |
| @@ -463,9 +608,9 @@ | ||
| 463 | 608 | $widgetOptions = $this->widgetOptionsBuilder->createPickupPointForAdmin( $order ); |
| 464 | 609 | |
| 465 | 610 | return [ |
| 466 | 611 | 'packeteryApiKey' => $this->optionsProvider->get_api_key(), |
| 467 | - 'pickupPointAttrs' => Attribute::$pickupPointAttrs, | |
| 612 | + 'pickupPointAttrs' => Attribute::$pickupPointAttributes, | |
| 468 | 613 | 'widgetOptions' => $widgetOptions, |
| 469 | 614 | ]; |
| 470 | 615 | } |
| 471 | 616 | |
| @@ -471,15 +616,13 @@ | ||
| 471 | 616 | |
| 472 | 617 | /** |
| 473 | 618 | * Creates address picker settings. |
| 474 | 619 | * |
| 475 | - * @return array|null | |
| 620 | + * @return mixed[]|null | |
| 476 | 621 | */ |
| 477 | 622 | public function getAddressWidgetSettings(): ?array { |
| 478 | - global $post; | |
| 479 | - | |
| 480 | - $order = $this->orderRepository->getById( (int) $post->ID ); | |
| 481 | - if ( null === $order || false === $order->isHomeDelivery() ) { | |
| 623 | + $order = $this->detailCommonLogic->getOrder(); | |
| 624 | + if ( $order === null || $order->isHomeDelivery() === false ) { | |
| 482 | 625 | return null; |
| 483 | 626 | } |
| 484 | 627 | |
| 485 | 628 | $widgetOptions = $this->widgetOptionsBuilder->createAddressForAdmin( $order ); |
| @@ -485,13 +628,13 @@ | ||
| 485 | 628 | $widgetOptions = $this->widgetOptionsBuilder->createAddressForAdmin( $order ); |
| 486 | 629 | |
| 487 | 630 | return [ |
| 488 | 631 | 'packeteryApiKey' => $this->optionsProvider->get_api_key(), |
| 489 | - 'homeDeliveryAttrs' => Attribute::$homeDeliveryAttrs, | |
| 632 | + 'homeDeliveryAttrs' => Attribute::$homeDeliveryAttributes, | |
| 490 | 633 | 'widgetOptions' => $widgetOptions, |
| 491 | 634 | 'translations' => [ |
| 492 | - 'addressValidationIsOutOfOrder' => __( 'Address validation is out of order.', 'packeta' ), | |
| 493 | - 'invalidAddressCountrySelected' => __( 'The selected country does not correspond to the destination country.', 'packeta' ), | |
| 635 | + 'addressValidationIsOutOfOrder' => $this->wpAdapter->__( 'Address validation is out of order.', 'packeta' ), | |
| 636 | + 'invalidAddressCountrySelected' => $this->wpAdapter->__( 'The selected country does not correspond to the destination country.', 'packeta' ), | |
| 494 | 637 | ], |
| 495 | 638 | ]; |
| 496 | 639 | } |
| 497 | 640 | |
| @@ -499,20 +642,50 @@ | ||
| 499 | 642 | * Gets order action link. |
| 500 | 643 | * |
| 501 | 644 | * @param Entity\Order $order Order. |
| 502 | 645 | * @param string $action Action. |
| 646 | + * @param array $extraParams Extra params. | |
| 503 | 647 | * |
| 504 | 648 | * @return string |
| 505 | 649 | */ |
| 506 | - private function getOrderActionLink( Entity\Order $order, string $action ): string { | |
| 650 | + private function getOrderActionLink( Entity\Order $order, string $action, array $extraParams = [] ): string { | |
| 651 | + $baseParams = [ | |
| 652 | + PacketActionsCommonLogic::PARAM_ORDER_ID => $order->getNumber(), | |
| 653 | + PacketActionsCommonLogic::PARAM_REDIRECT_TO => PacketActionsCommonLogic::REDIRECT_TO_ORDER_DETAIL, | |
| 654 | + Plugin::PARAM_PACKETERY_ACTION => $action, | |
| 655 | + Plugin::PARAM_NONCE => wp_create_nonce( PacketActionsCommonLogic::createNonceAction( $action, $order->getNumber() ) ), | |
| 656 | + ]; | |
| 657 | + | |
| 507 | 658 | return add_query_arg( |
| 508 | - [ | |
| 509 | - PacketActionsCommonLogic::PARAM_ORDER_ID => $order->getNumber(), | |
| 510 | - PacketActionsCommonLogic::PARAM_REDIRECT_TO => PacketActionsCommonLogic::REDIRECT_TO_ORDER_DETAIL, | |
| 511 | - Plugin::PARAM_PACKETERY_ACTION => $action, | |
| 512 | - Plugin::PARAM_NONCE => wp_create_nonce( PacketActionsCommonLogic::createNonceAction( $action, $order->getNumber() ) ), | |
| 513 | - ], | |
| 659 | + array_merge( $baseParams, $extraParams ), | |
| 514 | 660 | admin_url( 'admin.php' ) |
| 515 | 661 | ); |
| 516 | 662 | } |
| 517 | 663 | |
| 664 | + /** | |
| 665 | + * Initializes form to render or process. | |
| 666 | + * | |
| 667 | + * @return void | |
| 668 | + */ | |
| 669 | + private function initializeForm(): void { | |
| 670 | + $this->form = $this->orderForm->create(); | |
| 671 | + $this->form->addHidden( 'packetery_order_metabox_nonce' ); | |
| 672 | + $this->form->setDefaults( [ 'packetery_order_metabox_nonce' => wp_create_nonce() ] ); | |
| 673 | + | |
| 674 | + foreach ( Attribute::$pickupPointAttributes as $pickupPointAttr ) { | |
| 675 | + $this->form->addHidden( $pickupPointAttr['name'] ); | |
| 676 | + } | |
| 677 | + | |
| 678 | + foreach ( Attribute::$homeDeliveryAttributes as $homeDeliveryAttr ) { | |
| 679 | + $this->form->addHidden( $homeDeliveryAttr['name'] ); | |
| 680 | + } | |
| 681 | + | |
| 682 | + $this->form->addButton( | |
| 683 | + 'packetery_pick_pickup_point', | |
| 684 | + $this->wpAdapter->__( 'Choose pickup point', 'packeta' ) | |
| 685 | + ); | |
| 686 | + $this->form->addButton( | |
| 687 | + 'packetery_pick_address', | |
| 688 | + $this->wpAdapter->__( 'Check shipping address', 'packeta' ) | |
| 689 | + ); | |
| 690 | + } | |
| 518 | 691 | } |