Skip to content

Commit

Permalink
fix: coupon and giftcard corrections (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
plarsson authored Mar 3, 2023
1 parent a973509 commit 0aa4ffc
Showing 1 changed file with 19 additions and 68 deletions.
87 changes: 19 additions & 68 deletions classes/requests/helpers/class-ledyer-cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public function process_data() {
$this->process_coupons();
$this->process_fees();
$this->process_customer();

//$this->adjust_order_lines();
}

/**
Expand Down Expand Up @@ -94,15 +92,20 @@ public function get_order_amount() {
}

/**
* Gets order amount for Ledyer API.
* Order amount calculated from cart items
* Gets order total amount eligible for tax for Ledyer API
* Note that this excludes multipurpose vouchers/giftcards.
* Use get_order_amount for regular total amount
*
* @return int
*/
public function get_order_lines_total_amount() {
public function get_order_taxable_amount() {
$total_amount = 0;
foreach ( $this->order_lines as $order_line ) {
$total_amount += $order_line['totalAmount'];
$lineTotal = $order_line['totalAmount'];
$multiPurposeVoucher = 'giftCard' === $order_line['type'] && $order_line['vat'] == 0 && $lineTotal < 0;
if ( !$multiPurposeVoucher ) {
$total_amount += $lineTotal;
}
}

return round( $total_amount );
Expand All @@ -118,12 +121,8 @@ public function get_order_lines_total_amount() {
public function get_order_tax_amount( $order_lines ) {
$total_tax_amount = 0;
foreach ( $order_lines as $order_line ) {
// Add all order lines but exclude gift cards.
if ( 'gift_card' !== $order_line['type'] ) {
$total_tax_amount += intval( $order_line['totalVatAmount'] );
}
$total_tax_amount += intval( $order_line['totalVatAmount'] );
}

return round( $total_tax_amount );
}
/**
Expand All @@ -134,8 +133,8 @@ public function get_order_tax_amount( $order_lines ) {
* @return int
*/
public function get_order_amount_ex_tax( $order_lines ) {
$total_amount = $this->get_order_taxable_amount();
$total_tax_amount = $this->get_order_tax_amount( $order_lines );
$total_amount = $this->get_order_lines_total_amount();

return round( $total_amount - $total_tax_amount );
}
Expand Down Expand Up @@ -278,24 +277,6 @@ public function process_coupons() {
);
$this->order_lines[] = $discount;
}

// Standard cart, product, percentage discounts and gift cards end up here
// Discount coupons are created as a separate line item only for reference that a discount has been applied to product items
// The real discount will have been calculated and applied on each individual existing product order lines as unitDiscountAmount
if ( 'smart_coupon' !== $coupon->get_discount_type() ) {
$discount = array(
'type' => 'giftCard',
'reference' => $coupon_reference,
'description' => $coupon_description . ': ' . $coupon_reference,
'quantity' => 1,
'unitPrice' => $coupon_amount,
'unitDiscountAmount' => 0,
'vat' => 0,
'totalAmount' => $coupon_amount,
'totalVatAmount' => 0,
);
$this->order_lines[] = $discount;
}
}
}

Expand Down Expand Up @@ -363,17 +344,17 @@ public function process_coupons() {
foreach ( $pw_gift_cards['gift_cards'] as $code => $value ) {
$coupon_amount = $value * 100 * - 1;
$label = esc_html__( 'Gift card', 'pw-woocommerce-gift-cards' ) . ' ' . $code;
$giftcard_sku = apply_filters( 'lco_pw_gift_card_sku', esc_html__( 'giftcard', 'ledyer-checkout-for-woocommerce' ), $code );
$gift_card_sku = apply_filters( 'lco_pw_gift_card_sku', esc_html__( 'giftcard', 'ledyer-checkout-for-woocommerce' ), $code );
$gift_card = array(
'type' => 'giftCard',
'reference' => $giftcard_sku,
'description' => $label,
'reference' => $gift_card_sku,
'description' => $label,
'quantity' => 1,
'unitDiscountAmount' => $coupon_amount,
'vat' => 0,
'totalAmount' => $coupon_amount,
'unitDiscountAmount' => 0,
'totalVatAmount' => 0,
'unitPrice' => $coupon_amount,
'vat' => 0,
'totalAmount' => $coupon_amount,
'unitDiscountAmount' => 0,
'totalVatAmount' => 0,
);
$this->order_lines[] = $gift_card;
}
Expand Down Expand Up @@ -418,36 +399,6 @@ public function process_fees() {
}
}

/**
* Adjust order lines if there is a mismatch with cart total.
*
* @return array
*/
public function adjust_order_lines() {
$amount_to_adjust = $this->get_order_amount() - $this->get_order_lines_total_amount( $this->order_lines );

// If the amount to adjust is zero, return.
if ( 0 === intval( round( $amount_to_adjust * 100 ) ) ) {
return $this->order_lines;
}

$adjust_item = array(
'type' => 'surcharge',
'reference' => 'added-surcharge',
'description' => apply_filters( 'lco_wc_surcharge_name', __( 'Surcharge', 'ledyer-checkout-for-woocommerce' ) ),
'quantity' => 1,
'unitPrice' => $amount_to_adjust,
'unitDiscountAmount' => 0,
'vat' => 0,
'totalAmount' => $amount_to_adjust,
'totalVatAmount' => 0,
);

$this->order_lines[] = $adjust_item;

return $this->order_lines;
}

// Helpers.

/**
Expand Down

0 comments on commit 0aa4ffc

Please sign in to comment.