diff --git a/src/include/api/class-wc-retailcrm-client-v5.php b/src/include/api/class-wc-retailcrm-client-v5.php index 6e7623f..f877604 100644 --- a/src/include/api/class-wc-retailcrm-client-v5.php +++ b/src/include/api/class-wc-retailcrm-client-v5.php @@ -2992,12 +2992,12 @@ class WC_Retailcrm_Client_V5 } /** Maximum discount calculation */ - public function calculateDiscountLoyalty(string $site, array $order) + public function calculateDiscountLoyalty(string $site, array $order, $bonuses = 0) { return $this->client->makeRequest( "/loyalty/calculate", WC_Retailcrm_Request::METHOD_POST, - ['site' => $site, 'order' => json_encode($order)] + ['site' => $site, 'order' => json_encode($order), 'bonuses' => $bonuses] ); } diff --git a/src/include/class-wc-retailcrm-loyalty.php b/src/include/class-wc-retailcrm-loyalty.php index 6d818cd..edd6aa6 100644 --- a/src/include/class-wc-retailcrm-loyalty.php +++ b/src/include/class-wc-retailcrm-loyalty.php @@ -351,7 +351,7 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : return !(!$wcUser || (isCorporateUserActivate($this->settings) && isCorporateOrder($wcUser, $wcOrder))); } - public function applyLoyaltyDiscount($wcOrder, $discountLp, $createdOrder) + public function applyLoyaltyDiscount($wcOrder, $createdOrder, $discountLp = 0) { $isPercentDiscount = false; $items = []; diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index deba2c8..dda273b 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -130,7 +130,7 @@ if (!class_exists('WC_Retailcrm_Orders')) : } if (isset($discountLp) && $discountLp > 0) { - $this->loyalty->applyLoyaltyDiscount($wcOrder, $discountLp, $response['order']); + $this->loyalty->applyLoyaltyDiscount($wcOrder, $response['order'], $discountLp); } } catch (Throwable $exception) { writeBaseLogs( @@ -315,21 +315,43 @@ if (!class_exists('WC_Retailcrm_Orders')) : $this->cancelBonus = false; $this->order_item->cancelBonus = false; - $this->retailcrm->cancelBonusOrder(['externalId' => $this->order['externalId']]); + $this->retailcrm->cancelBonusOrder(['externalId' => $this->order['externalId']]);// проверка response $response = $this->retailcrm->ordersEdit($this->order); - $wcOrder->calculate_totals(); + $response = apply_filters('retailcrm_order_update_after', $response, $wcOrder); + + if ($response instanceof WC_Retailcrm_Response && $response->isSuccessful()) { + $this->payment = $this->orderUpdatePaymentType($wcOrder); + + /* $responseCalculate = $this->retailcrm->calculateDiscountLoyalty( + $response['order']['site'], + $this->order, + $this->appliedBonuses + ); + + if (!$responseCalculate instanceof WC_Retailcrm_Response || !$responseCalculate->isSuccessful()) { + $this->appliedBonuses = 0; + }*/ + + $result = $this->loyalty->applyLoyaltyDiscount($wcOrder, $response['order'], $this->appliedBonuses); + + if (is_string($result)) { + writeBaseLogs($result); + $wcOrder->calculate_totals(); + } + } else { + $wcOrder->calculate_totals(); + } } else { $response = $this->retailcrm->ordersEdit($this->order); - } + // Allows you to verify order changes and perform additional actions + $response = apply_filters('retailcrm_order_update_after', $response, $wcOrder); - // Allows you to verify order changes and perform additional actions - $response = apply_filters('retailcrm_order_update_after', $response, $wcOrder); - - if ($response instanceof WC_Retailcrm_Response && $response->isSuccessful()) { - $this->payment = $this->orderUpdatePaymentType($wcOrder); + if ($response instanceof WC_Retailcrm_Response && $response->isSuccessful()) { + $this->payment = $this->orderUpdatePaymentType($wcOrder); + } } } catch (Throwable $exception) { writeBaseLogs( diff --git a/src/include/order/class-wc-retailcrm-order-item.php b/src/include/order/class-wc-retailcrm-order-item.php index a23cbe8..87de53c 100644 --- a/src/include/order/class-wc-retailcrm-order-item.php +++ b/src/include/order/class-wc-retailcrm-order-item.php @@ -137,14 +137,19 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data } if ($item->get_total()) { - $productPrice = ($item->get_total() / $item->get_quantity()) + ($loyaltyDiscount / $crmItem['quantity']); + $productPrice = ($item->get_total() / $item->get_quantity()) + ($loyaltyDiscount / $crmItem['quantity']); } else { $productPrice = 0; } - if ($this->cancelBonus) { + if ($this->cancelBonus && $productPrice > $price) { + $productPrice = $item->get_total() / $item->get_quantity(); + } elseif ($this->cancelBonus) { $item->set_total($item->get_total() + $loyaltyDiscount); $item->calculate_taxes(); + $item->save(); + } elseif ($productPrice > $price) { + $productPrice = $item->get_total() / $item->get_quantity(); } } else { $productPrice = $item->get_total() ? $item->get_total() / $item->get_quantity() : 0; @@ -173,6 +178,8 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data public function isCancelBonus($wcItems, $crmItems): bool { + $loyaltyDiscount = 0; + if (count($wcItems) !== count($crmItems)) { $this->cancelBonus = true; @@ -191,6 +198,20 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data return true; } + + foreach ($crmItems[$id]['discounts'] as $discount) { + if (in_array($discount['type'], ['bonus_charge', 'loyalty_level'])) { + $loyaltyDiscount += $discount['amount']; + + break; + } + } + + if (($item->get_total() + $loyaltyDiscount) > $item->get_subtotal()) { + $this->cancelBonus = true; + + return true; + } } return false;