Skip to content

Commit

Permalink
fixup! PES-1395: settings of COD amount, weight, and order value corr…
Browse files Browse the repository at this point in the history
…ectly distinguish between manually entered and calculated values
  • Loading branch information
jan-potuznik-z committed Feb 17, 2025
1 parent e000b51 commit e1df6b5
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 112 deletions.
12 changes: 6 additions & 6 deletions public/js/admin-grid-order-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
var packeteryHeight = $packeteryModal.find( '[name="packeteryHeight"]' ).val();
var packeteryDeliverOn = $packeteryModal.find( '[name="packeteryDeliverOn"]' ).val();
var packeteryCOD = $packeteryModal.find( '[name="packeteryCOD"]' ).val();
var packeteryDynamicCOD = $packeteryModal.find( '[name="packeteryDynamicCOD"]' ).val();
var packeteryCalculatedCod = $packeteryModal.find( '[name="packeteryCalculatedCod"]' ).val();
var packeteryValue = $packeteryModal.find( '[name="packeteryValue"]' ).val();
var packeteryDynamicValue = $packeteryModal.find( '[name="packeteryDynamicValue"]' ).val();
var packeteryCalculatedValue = $packeteryModal.find( '[name="packeteryCalculatedValue"]' ).val();
var hasPacketeryAdultContent = $packeteryModal.find('[name="packeteryAdultContent"]').prop('checked');

$packeteryModal.find( '.spinner' ).addClass( 'is-active' );
Expand All @@ -88,9 +88,9 @@
packeteryHeight : packeteryHeight,
packeteryDeliverOn : packeteryDeliverOn,
packeteryCOD : packeteryCOD,
packeteryDynamicCOD : packeteryDynamicCOD,
packeteryCalculatedCod : packeteryCalculatedCod,
packeteryValue : packeteryValue,
packeteryDynamicValue : packeteryDynamicValue,
packeteryCalculatedValue : packeteryCalculatedValue,
hasPacketeryAdultContent : hasPacketeryAdultContent,
}
} ).fail( function( response ) {
Expand All @@ -107,9 +107,9 @@
orderData.packeteryHeight = response.data.packeteryHeight;
orderData.packeteryDeliverOn = response.data.packeteryDeliverOn;
orderData.packeteryCOD = response.data.packeteryCOD;
orderData.packeteryDynamicCOD = response.data.packeteryDynamicCOD;
orderData.packeteryCalculatedCod = response.data.packeteryCalculatedCod;
orderData.packeteryValue = response.data.packeteryValue;
orderData.packeteryDynamicValue = response.data.packeteryDynamicValue;
orderData.packeteryCalculatedValue = response.data.packeteryCalculatedValue;
orderData.packeteryAdultContent = response.data.packeteryAdultContent;
orderData.orderIsSubmittable = response.data.orderIsSubmittable;
orderData.orderWarningFields = response.data.orderWarningFields;
Expand Down
24 changes: 12 additions & 12 deletions src/Packetery/Core/Entity/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Order {
/**
* @var float|null
*/
private $dynamicValue;
private $calculatedValue;

/**
* @var float|null
Expand Down Expand Up @@ -130,7 +130,7 @@ class Order {
/**
* @var float|null
*/
private $dynamicCod;
private $calculatedCod;

/**
* @var float|null
Expand Down Expand Up @@ -537,16 +537,16 @@ public function setManualValue( ?float $value ): void {
$this->manualValue = $value;
}

public function setDynamicValue( ?float $value ): void {
$this->dynamicValue = $value;
public function setCalculatedValue( ?float $value ): void {
$this->calculatedValue = $value;
}

public function setManualCod( ?float $cod ): void {
$this->manualCod = $cod;
}

public function setDynamicCod( ?float $cod ): void {
$this->dynamicCod = $cod;
public function setCalculatedCod( ?float $cod ): void {
$this->calculatedCod = $cod;
}

/**
Expand Down Expand Up @@ -1039,12 +1039,12 @@ public function hasManualValue(): bool {
return $this->manualValue !== null;
}

public function getDynamicValue(): ?float {
return $this->dynamicValue;
public function getCalculatedValue(): ?float {
return $this->calculatedValue;
}

public function getFinalValue(): ?float {
return $this->manualValue ?? $this->dynamicValue;
return $this->manualValue ?? $this->calculatedValue;
}

public function hasFinalValue(): bool {
Expand All @@ -1059,12 +1059,12 @@ public function hasManualCod(): bool {
return $this->manualCod !== null;
}

public function getDynamicCod(): ?float {
return $this->dynamicCod;
public function getCalculatedCod(): ?float {
return $this->calculatedCod;
}

public function getFinalCod(): ?float {
return $this->manualCod ?? $this->dynamicCod;
return $this->manualCod ?? $this->calculatedCod;
}

/**
Expand Down
60 changes: 30 additions & 30 deletions src/Packetery/Module/Api/Internal/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,17 @@ public function saveModal( WP_REST_Request $request ) {
$form = $this->orderForm->create();
$form->setValues(
[
Form::FIELD_WEIGHT => $parameters['packeteryWeight'],
Form::FIELD_ORIGINAL_WEIGHT => $parameters['packeteryOriginalWeight'],
Form::FIELD_WIDTH => $parameters['packeteryWidth'] ?? null,
Form::FIELD_LENGTH => $parameters['packeteryLength'] ?? null,
Form::FIELD_HEIGHT => $parameters['packeteryHeight'] ?? null,
Form::FIELD_ADULT_CONTENT => isset( $parameters['hasPacketeryAdultContent'] ) && $parameters['hasPacketeryAdultContent'] === 'true',
Form::FIELD_COD => $parameters['packeteryCOD'] ?? null,
Form::FIELD_DYNAMIC_COD => $parameters['packeteryDynamicCOD'] ?? null,
Form::FIELD_VALUE => $parameters['packeteryValue'] ?? null,
Form::FIELD_DYNAMIC_VALUE => $parameters['packeteryDynamicValue'] ?? null,
Form::FIELD_DELIVER_ON => $packeteryDeliverOn,
Form::FIELD_WEIGHT => $parameters['packeteryWeight'],
Form::FIELD_ORIGINAL_WEIGHT => $parameters['packeteryOriginalWeight'],
Form::FIELD_WIDTH => $parameters['packeteryWidth'] ?? null,
Form::FIELD_LENGTH => $parameters['packeteryLength'] ?? null,
Form::FIELD_HEIGHT => $parameters['packeteryHeight'] ?? null,
Form::FIELD_ADULT_CONTENT => isset( $parameters['hasPacketeryAdultContent'] ) && $parameters['hasPacketeryAdultContent'] === 'true',
Form::FIELD_COD => $parameters['packeteryCOD'] ?? null,
Form::FIELD_CALCULATED_COD => $parameters['packeteryCalculatedCod'] ?? null,
Form::FIELD_VALUE => $parameters['packeteryValue'] ?? null,
Form::FIELD_CALCULATED_VALUE => $parameters['packeteryCalculatedValue'] ?? null,
Form::FIELD_DELIVER_ON => $packeteryDeliverOn,
]
);

Expand Down Expand Up @@ -222,13 +222,13 @@ public function saveModal( WP_REST_Request $request ) {
$order->setWeight( null );
}

if ( $orderFormData->packeteryCOD !== (float) $orderFormData->packeteryDynamicCOD ) {
if ( $orderFormData->packeteryCOD !== (float) $orderFormData->packeteryCalculatedCod ) {
$order->setManualCod( $orderFormData->packeteryCOD );
} else {
$order->setManualCod( null );
}
$order->setAdultContent( $orderFormData->packeteryAdultContent );
if ( $orderFormData->packeteryValue !== (float) $orderFormData->packeteryDynamicValue ) {
if ( $orderFormData->packeteryValue !== (float) $orderFormData->packeteryCalculatedValue ) {
$order->setManualValue( $orderFormData->packeteryValue );
} else {
$order->setManualValue( null );
Expand All @@ -240,25 +240,25 @@ public function saveModal( WP_REST_Request $request ) {

$data['message'] = __( 'Success', 'packeta' );
$data['data'] = [
'fragments' => [
'fragments' => [
sprintf( '[data-packetery-order-id="%d"][data-packetery-order-grid-cell-weight]', $orderId ) => $this->gridExtender->getWeightCellContent( $order ),
],
Form::FIELD_WEIGHT => $order->getFinalWeight(),
Form::FIELD_ORIGINAL_WEIGHT => $order->getCalculatedWeight(),
Form::FIELD_LENGTH => CoreHelper::trimDecimalPlaces( $orderFormData->packeteryLength, $this->optionsProvider->getDimensionsNumberOfDecimals() ),
Form::FIELD_WIDTH => CoreHelper::trimDecimalPlaces( $orderFormData->packeteryWidth, $this->optionsProvider->getDimensionsNumberOfDecimals() ),
Form::FIELD_HEIGHT => CoreHelper::trimDecimalPlaces( $orderFormData->packeteryHeight, $this->optionsProvider->getDimensionsNumberOfDecimals() ),
Form::FIELD_ADULT_CONTENT => $order->containsAdultContent(),
Form::FIELD_COD => $order->getFinalCod(),
Form::FIELD_DYNAMIC_COD => $order->getDynamicCod(),
Form::FIELD_VALUE => $order->getFinalValue(),
Form::FIELD_DYNAMIC_VALUE => $order->getDynamicValue(),
Form::FIELD_DELIVER_ON => $this->coreHelper->getStringFromDateTime( $order->getDeliverOn(), CoreHelper::DATEPICKER_FORMAT ),
'orderIsSubmittable' => $this->orderValidator->isValid( $order ),
'orderWarningFields' => Form::getInvalidFieldsFromValidationResult( $this->orderValidator->validate( $order ) ),
'hasOrderManualWeight' => $order->hasManualWeight(),
'hasOrderManualCod' => $order->hasManualCod(),
'hasOrderManualValue' => $order->hasManualValue(),
Form::FIELD_WEIGHT => $order->getFinalWeight(),
Form::FIELD_ORIGINAL_WEIGHT => $order->getCalculatedWeight(),
Form::FIELD_LENGTH => CoreHelper::trimDecimalPlaces( $orderFormData->packeteryLength, $this->optionsProvider->getDimensionsNumberOfDecimals() ),
Form::FIELD_WIDTH => CoreHelper::trimDecimalPlaces( $orderFormData->packeteryWidth, $this->optionsProvider->getDimensionsNumberOfDecimals() ),
Form::FIELD_HEIGHT => CoreHelper::trimDecimalPlaces( $orderFormData->packeteryHeight, $this->optionsProvider->getDimensionsNumberOfDecimals() ),
Form::FIELD_ADULT_CONTENT => $order->containsAdultContent(),
Form::FIELD_COD => $order->getFinalCod(),
Form::FIELD_CALCULATED_COD => $order->getCalculatedCod(),
Form::FIELD_VALUE => $order->getFinalValue(),
Form::FIELD_CALCULATED_VALUE => $order->getCalculatedValue(),
Form::FIELD_DELIVER_ON => $this->coreHelper->getStringFromDateTime( $order->getDeliverOn(), CoreHelper::DATEPICKER_FORMAT ),
'orderIsSubmittable' => $this->orderValidator->isValid( $order ),
'orderWarningFields' => Form::getInvalidFieldsFromValidationResult( $this->orderValidator->validate( $order ) ),
'hasOrderManualWeight' => $order->hasManualWeight(),
'hasOrderManualCod' => $order->hasManualCod(),
'hasOrderManualValue' => $order->hasManualValue(),
];

return new WP_REST_Response( $data, 200 );
Expand Down
34 changes: 17 additions & 17 deletions src/Packetery/Module/Forms/FormData/OrderFormData.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class OrderFormData {
/**
* @var string|null
*/
public $packeteryDynamicCOD;
public $packeteryCalculatedCod;

/**
* @var float|null
Expand All @@ -52,7 +52,7 @@ class OrderFormData {
/**
* @var string|null
*/
public $packeteryDynamicValue;
public $packeteryCalculatedValue;

/**
* @var string|null
Expand All @@ -67,9 +67,9 @@ class OrderFormData {
* @param int|float|null $packeteryHeight
* @param bool $packeteryAdultContent
* @param float|null $packeteryCOD
* @param string|null $packeteryDynamicCOD
* @param string|null $packeteryCalculatedCod
* @param float|null $packeteryValue
* @param string|null $packeteryDynamicValue
* @param string|null $packeteryCalculatedValue
* @param string|null $packeteryDeliverOn
*/
public function __construct(
Expand All @@ -80,21 +80,21 @@ public function __construct(
$packeteryHeight,
bool $packeteryAdultContent,
?float $packeteryCOD,
?string $packeteryDynamicCOD,
?string $packeteryCalculatedCod,
?float $packeteryValue,
?string $packeteryDynamicValue,
?string $packeteryCalculatedValue,
?string $packeteryDeliverOn
) {
$this->packeteryWeight = $packeteryWeight;
$this->packeteryOriginalWeight = $packeteryOriginalWeight;
$this->packeteryLength = $packeteryLength;
$this->packeteryWidth = $packeteryWidth;
$this->packeteryHeight = $packeteryHeight;
$this->packeteryAdultContent = $packeteryAdultContent;
$this->packeteryCOD = $packeteryCOD;
$this->packeteryDynamicCOD = $packeteryDynamicCOD;
$this->packeteryValue = $packeteryValue;
$this->packeteryDynamicValue = $packeteryDynamicValue;
$this->packeteryDeliverOn = $packeteryDeliverOn;
$this->packeteryWeight = $packeteryWeight;
$this->packeteryOriginalWeight = $packeteryOriginalWeight;
$this->packeteryLength = $packeteryLength;
$this->packeteryWidth = $packeteryWidth;
$this->packeteryHeight = $packeteryHeight;
$this->packeteryAdultContent = $packeteryAdultContent;
$this->packeteryCOD = $packeteryCOD;
$this->packeteryCalculatedCod = $packeteryCalculatedCod;
$this->packeteryValue = $packeteryValue;
$this->packeteryCalculatedValue = $packeteryCalculatedValue;
$this->packeteryDeliverOn = $packeteryDeliverOn;
}
}
6 changes: 3 additions & 3 deletions src/Packetery/Module/Order/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function build( WC_Order $wcOrder, stdClass $result ): Entity\Order {
$order->setSurname( $contactInfo['last_name'] );
$order->setEshop( $this->optionsProvider->get_sender() );

$order->setDynamicValue( (float) $wcOrder->get_total( 'raw' ) );
$order->setCalculatedValue( (float) $wcOrder->get_total( 'raw' ) );

$address = $order->getDeliveryAddress();
if ( $address === null ) {
Expand All @@ -248,10 +248,10 @@ public function build( WC_Order $wcOrder, stdClass $result ): Entity\Order {
$order->setEmail( $orderData['billing']['email'] );
$hasCodPaymentMethod = $this->paymentHelper->isCodPaymentMethod( $orderData['payment_method'] );
if ( $hasCodPaymentMethod ) {
$order->setDynamicCod( $order->getDynamicValue() );
$order->setCalculatedCod( $order->getCalculatedValue() );
// manual COD is set from DB directly
} else {
$order->setDynamicCod( null );
$order->setCalculatedCod( null );
$order->setManualCod( null );
}

Expand Down
52 changes: 26 additions & 26 deletions src/Packetery/Module/Order/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
*/
class Form {

public const FIELD_WEIGHT = 'packeteryWeight';
public const FIELD_ORIGINAL_WEIGHT = 'packeteryOriginalWeight';
public const FIELD_VALUE = 'packeteryValue';
public const FIELD_DYNAMIC_VALUE = 'packeteryDynamicValue';
public const FIELD_LENGTH = 'packeteryLength';
public const FIELD_WIDTH = 'packeteryWidth';
public const FIELD_HEIGHT = 'packeteryHeight';
public const FIELD_ADULT_CONTENT = 'packeteryAdultContent';
public const FIELD_COD = 'packeteryCOD';
public const FIELD_DYNAMIC_COD = 'packeteryDynamicCOD';
public const FIELD_DELIVER_ON = 'packeteryDeliverOn';
public const FIELD_WEIGHT = 'packeteryWeight';
public const FIELD_ORIGINAL_WEIGHT = 'packeteryOriginalWeight';
public const FIELD_VALUE = 'packeteryValue';
public const FIELD_CALCULATED_VALUE = 'packeteryCalculatedValue';
public const FIELD_LENGTH = 'packeteryLength';
public const FIELD_WIDTH = 'packeteryWidth';
public const FIELD_HEIGHT = 'packeteryHeight';
public const FIELD_ADULT_CONTENT = 'packeteryAdultContent';
public const FIELD_COD = 'packeteryCOD';
public const FIELD_CALCULATED_COD = 'packeteryCalculatedCod';
public const FIELD_DELIVER_ON = 'packeteryDeliverOn';

/**
* Class FormFactory
Expand Down Expand Up @@ -81,12 +81,12 @@ public function create(): Forms\Form {
->setRequired( false )
->setNullable()
->addRule( Forms\Form::FLOAT );
$form->addHidden( self::FIELD_DYNAMIC_COD );
$form->addHidden( self::FIELD_CALCULATED_COD );
$form->addText( self::FIELD_VALUE, __( 'Order value', 'packeta' ) )
->setRequired( false )
->setNullable()
->addRule( Forms\Form::FLOAT );
$form->addHidden( self::FIELD_DYNAMIC_VALUE );
$form->addHidden( self::FIELD_CALCULATED_VALUE );
$form->addText( self::FIELD_DELIVER_ON, __( 'Planned dispatch', 'packeta' ) )
->setHtmlAttribute( 'class', 'date-picker' )
->setHtmlAttribute( 'autocomplete', 'off' )
Expand All @@ -106,26 +106,26 @@ public function setDefaults(
?float $width,
?float $height,
?float $cod,
?float $dynamicCod,
?float $calculatedCod,
?float $orderValue,
?float $dynamicValue,
?float $calculatedValue,
?bool $adultContent,
?string $deliverOn
): void {

$form->setDefaults(
[
self::FIELD_WEIGHT => $weight,
self::FIELD_ORIGINAL_WEIGHT => $originalWeight,
self::FIELD_LENGTH => $length,
self::FIELD_WIDTH => $width,
self::FIELD_HEIGHT => $height,
self::FIELD_COD => $cod,
self::FIELD_DYNAMIC_COD => $dynamicCod,
self::FIELD_VALUE => $orderValue,
self::FIELD_DYNAMIC_VALUE => $dynamicValue,
self::FIELD_ADULT_CONTENT => $adultContent,
self::FIELD_DELIVER_ON => $deliverOn,
self::FIELD_WEIGHT => $weight,
self::FIELD_ORIGINAL_WEIGHT => $originalWeight,
self::FIELD_LENGTH => $length,
self::FIELD_WIDTH => $width,
self::FIELD_HEIGHT => $height,
self::FIELD_COD => $cod,
self::FIELD_CALCULATED_COD => $calculatedCod,
self::FIELD_VALUE => $orderValue,
self::FIELD_CALCULATED_VALUE => $calculatedValue,
self::FIELD_ADULT_CONTENT => $adultContent,
self::FIELD_DELIVER_ON => $deliverOn,
]
);
}
Expand Down
Loading

0 comments on commit e1df6b5

Please sign in to comment.