Use str value when checking mismatch

Otherwise the check may fail depending on the rounding used when creating the Money objects
This commit is contained in:
Alex P 2022-07-07 12:36:14 +03:00
parent 82adf1933f
commit 2ca64739db
3 changed files with 41 additions and 24 deletions

View file

@ -331,9 +331,9 @@ class PurchaseUnit {
$remaining_item_total = array_reduce(
$items,
function ( float $total, Item $item ): float {
return $total - $item->unit_amount()->value() * (float) $item->quantity();
return $total - (float) $item->unit_amount()->value_str() * (float) $item->quantity();
},
$item_total->value()
(float) $item_total->value_str()
);
$remaining_item_total = round( $remaining_item_total, 2 );
@ -356,11 +356,11 @@ class PurchaseUnit {
function ( float $total, Item $item ): float {
$tax = $item->tax();
if ( $tax ) {
$total -= $tax->value() * (float) $item->quantity();
$total -= (float) $tax->value_str() * (float) $item->quantity();
}
return $total;
},
$tax_total->value()
(float) $tax_total->value_str()
);
$remaining_tax_total = round( $remaining_tax_total, 2 );
@ -378,29 +378,29 @@ class PurchaseUnit {
$amount_total = 0.0;
if ( $shipping ) {
$amount_total += $shipping->value();
$amount_total += (float) $shipping->value_str();
}
if ( $item_total ) {
$amount_total += $item_total->value();
$amount_total += (float) $item_total->value_str();
}
if ( $discount ) {
$amount_total -= $discount->value();
$amount_total -= (float) $discount->value_str();
}
if ( $tax_total ) {
$amount_total += $tax_total->value();
$amount_total += (float) $tax_total->value_str();
}
if ( $shipping_discount ) {
$amount_total -= $shipping_discount->value();
$amount_total -= (float) $shipping_discount->value_str();
}
if ( $handling ) {
$amount_total += $handling->value();
$amount_total += (float) $handling->value_str();
}
if ( $insurance ) {
$amount_total += $insurance->value();
$amount_total += (float) $insurance->value_str();
}
$amount_str = (string) $amount->to_array()['value'];
$amount_total_str = (string) ( new Money( $amount_total, $amount->currency_code() ) )->to_array()['value'];
$amount_str = $amount->value_str();
$amount_total_str = ( new Money( $amount_total, $amount->currency_code() ) )->value_str();
$needs_to_ditch = $amount_str !== $amount_total_str;
return $needs_to_ditch;
}