Skip to content

Commit

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

$packeteryModal.find( '.spinner' ).addClass( 'is-active' );
Expand All @@ -86,7 +88,9 @@
packeteryHeight : packeteryHeight,
packeteryDeliverOn : packeteryDeliverOn,
packeteryCOD : packeteryCOD,
packeteryCalculatedCod : packeteryCalculatedCod,
packeteryValue : packeteryValue,
packeteryCalculatedValue : packeteryCalculatedValue,
hasPacketeryAdultContent : hasPacketeryAdultContent,
}
} ).fail( function( response ) {
Expand All @@ -97,17 +101,21 @@

var orderData = $lastModalButtonClicked.data( 'order-data' );
orderData.packeteryWeight = response.data.packeteryWeight;
orderData.packeteryOriginalWeight = response.data.packeteryWeight;
orderData.packeteryOriginalWeight = response.data.packeteryOriginalWeight;
orderData.packeteryLength = response.data.packeteryLength;
orderData.packeteryWidth = response.data.packeteryWidth;
orderData.packeteryHeight = response.data.packeteryHeight;
orderData.packeteryDeliverOn = response.data.packeteryDeliverOn;
orderData.packeteryCOD = response.data.packeteryCOD;
orderData.packeteryCalculatedCod = response.data.packeteryCalculatedCod;
orderData.packeteryValue = response.data.packeteryValue;
orderData.packeteryCalculatedValue = response.data.packeteryCalculatedValue;
orderData.packeteryAdultContent = response.data.packeteryAdultContent;
orderData.orderIsSubmittable = response.data.orderIsSubmittable;
orderData.orderWarningFields = response.data.orderWarningFields;
orderData.manualWeightIconExtraClass = response.data.hasOrderManualWeight === true ? '' : 'packetery-hidden ';
orderData.manualCodIconExtraClass = response.data.hasOrderManualCod === true ? '' : 'packetery-hidden ';
orderData.manualValueIconExtraClass = response.data.hasOrderManualValue === true ? '' : 'packetery-hidden ';
$lastModalButtonClicked.data( 'order-data', orderData );

replaceFragmentsWith( response.data.fragments );
Expand Down
4 changes: 2 additions & 2 deletions src/Packetery/Core/Api/Soap/CreatePacketMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function fromOrderToArray( Entity\Order $order ): array {
'number' => $order->getCustomNumberOrNumber(),
'name' => $order->getName(),
'surname' => $order->getSurname(),
'value' => $order->getValue(),
'value' => $order->getFinalValue(),
'weight' => $order->getFinalWeight(),
'addressId' => $order->getPickupPointOrCarrierId(),
'eshop' => $order->getEshop(),
Expand All @@ -71,7 +71,7 @@ public function fromOrderToArray( Entity\Order $order ): array {
'deliverOn' => $this->coreHelper->getStringFromDateTime( $order->getDeliverOn(), CoreHelper::DATEPICKER_FORMAT ),
];

$codValue = $order->getCod();
$codValue = $order->getFinalCod();
if ( $codValue !== null ) {
$roundingType = $this->carrierOptionsFactory->createByCarrierId( $order->getCarrier()->getId() )->getCodRoundingType();
$roundedCod = Rounder::roundByCurrency( $codValue, $createPacketData['currency'], $roundingType );
Expand Down
4 changes: 2 additions & 2 deletions src/Packetery/Core/Api/Soap/Request/CreatePacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ public function __construct( Entity\Order $order ) {
$this->number = $order->getCustomNumberOrNumber();
$this->name = $order->getName();
$this->surname = $order->getSurname();
$this->value = $order->getValue();
$this->value = $order->getFinalValue();
$this->weight = $order->getFinalWeight();
$this->addressId = $order->getPickupPointOrCarrierId();
$this->eshop = $order->getEshop();
// Optional attributes.
$this->adultContent = (int) $order->containsAdultContent();
$this->cod = $order->getCod();
$this->cod = $order->getFinalCod();
$this->currency = $order->getCurrency();
$this->email = $order->getEmail();
$this->note = $order->getNote();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct( Entity\Order $order ) {
$this->number = $order->getCustomNumberOrNumber();
$this->email = $order->getEmail();
$this->phone = $order->getPhone();
$this->value = $order->getValue();
$this->value = $order->getFinalValue();
$this->currency = $order->getCurrency();
$this->eshop = $order->getEshop();
$this->consignCountry = $order->getShippingCountry();
Expand Down
102 changes: 55 additions & 47 deletions src/Packetery/Core/Entity/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,14 @@ class Order {
private $phone;

/**
* Order value.
*
* @var float|null
*/
private $value;
private $calculatedValue;

/**
* @var float|null
*/
private $manualValue;

/**
* Sender label.
Expand Down Expand Up @@ -125,11 +128,14 @@ class Order {
private $calculatedWeight;

/**
* Cash on delivery value.
*
* @var float|null
*/
private $cod;
private $calculatedCod;

/**
* @var float|null
*/
private $manualCod;

/**
* Packet note.
Expand Down Expand Up @@ -527,22 +533,20 @@ public function setPhone( string $phone ): void {
$this->phone = $phone;
}

/**
* Sets value.
*
* @param float|null $value Value.
*/
public function setValue( ?float $value ): void {
$this->value = $value;
public function setManualValue( ?float $value ): void {
$this->manualValue = $value;
}

/**
* Sets COD.
*
* @param float|null $cod COD.
*/
public function setCod( ?float $cod ): void {
$this->cod = $cod;
public function setCalculatedValue( ?float $value ): void {
$this->calculatedValue = $value;
}

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

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

/**
Expand Down Expand Up @@ -1027,31 +1031,40 @@ public function getSurname(): ?string {
return $this->surname;
}

/**
* Gets order value.
*
* @return float|null
*/
public function getValue(): ?float {
return $this->value;
public function getManualValue(): ?float {
return $this->manualValue;
}

/**
* Has order value.
*
* @return bool
*/
public function hasValue(): bool {
return $this->value !== null;
public function hasManualValue(): bool {
return $this->manualValue !== null;
}

/**
* Gets order COD value.
*
* @return float|null
*/
public function getCod(): ?float {
return $this->cod;
public function getCalculatedValue(): ?float {
return $this->calculatedValue;
}

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

public function hasFinalValue(): bool {
return $this->getFinalValue() !== null;
}

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

public function hasManualCod(): bool {
return $this->manualCod !== null;
}

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

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

/**
Expand Down Expand Up @@ -1126,13 +1139,8 @@ public function getShippingCountry(): ?string {
return $this->shippingCountry;
}

/**
* Tells if order has COD.
*
* @return bool
*/
public function hasCod(): bool {
return ( $this->getCod() !== null );
return $this->getFinalCod() !== null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Packetery/Core/Validator/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function validate( Entity\Order $order ): array {
$errors = [
self::ERROR_TRANSLATION_KEY_NUMBER => ! $order->hasNumber(),
self::ERROR_TRANSLATION_KEY_NAME => ! $order->hasName(),
self::ERROR_TRANSLATION_KEY_VALUE => ! $order->hasValue(),
self::ERROR_TRANSLATION_KEY_VALUE => ! $order->hasFinalValue(),
self::ERROR_TRANSLATION_KEY_PICKUP_POINT_OR_CARRIER_ID => ! $order->hasPickupPointOrCarrierId(),
self::ERROR_TRANSLATION_KEY_ESHOP => ! $order->hasEshop(),
self::ERROR_TRANSLATION_KEY_WEIGHT => ! $this->validateFinalWeight( $order ),
Expand Down
63 changes: 40 additions & 23 deletions src/Packetery/Module/Api/Internal/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +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_VALUE => $parameters['packeteryValue'],
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 All @@ -216,32 +218,47 @@ public function saveModal( WP_REST_Request $request ) {

if ( $orderFormData->packeteryWeight !== (float) $orderFormData->packeteryOriginalWeight ) {
$order->setWeight( $orderFormData->packeteryWeight );
} else {
$order->setWeight( null );
}

$order->setCod( $orderFormData->packeteryCOD );
if ( $orderFormData->packeteryCOD !== (float) $orderFormData->packeteryCalculatedCod ) {
$order->setManualCod( $orderFormData->packeteryCOD );
} else {
$order->setManualCod( null );
}
$order->setAdultContent( $orderFormData->packeteryAdultContent );
$order->setValue( $orderFormData->packeteryValue );
if ( $orderFormData->packeteryValue !== (float) $orderFormData->packeteryCalculatedValue ) {
$order->setManualValue( $orderFormData->packeteryValue );
} else {
$order->setManualValue( null );
}
$order->setSize( $size );
$order->setDeliverOn( $this->coreHelper->getDateTimeFromString( $packeteryDeliverOn ) );

$this->orderRepository->save( $order );

$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_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->getCod(),
Form::FIELD_VALUE => $order->getValue(),
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(),
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: 25 additions & 9 deletions src/Packetery/Module/Forms/FormData/OrderFormData.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,21 @@ class OrderFormData {
*/
public $packeteryCOD;

/**
* @var string|null
*/
public $packeteryCalculatedCod;

/**
* @var float|null
*/
public $packeteryValue;

/**
* @var string|null
*/
public $packeteryCalculatedValue;

/**
* @var string|null
*/
Expand All @@ -57,7 +67,9 @@ class OrderFormData {
* @param int|float|null $packeteryHeight
* @param bool $packeteryAdultContent
* @param float|null $packeteryCOD
* @param string|null $packeteryCalculatedCod
* @param float|null $packeteryValue
* @param string|null $packeteryCalculatedValue
* @param string|null $packeteryDeliverOn
*/
public function __construct(
Expand All @@ -68,17 +80,21 @@ public function __construct(
$packeteryHeight,
bool $packeteryAdultContent,
?float $packeteryCOD,
?string $packeteryCalculatedCod,
?float $packeteryValue,
?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->packeteryValue = $packeteryValue;
$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;
}
}
Loading

0 comments on commit 9438f68

Please sign in to comment.