1
0
mirror of synced 2024-11-22 21:36:10 +03:00

Merge pull request #1 from Neur0toxine/fix_discount

fix discounts for multiple positions & annotations
This commit is contained in:
Evgeniy-Goroh 2020-02-19 14:59:03 +03:00 committed by GitHub
commit 7630dedc84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 31 deletions

View File

@ -91,7 +91,9 @@ class RetailCrmEvent
* @param $event * @param $event
* *
* @return bool * @return bool
* @throws InvalidArgumentException * @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
* @throws \Bitrix\Main\ArgumentException
*/ */
function orderSave($event) function orderSave($event)
{ {

View File

@ -434,7 +434,7 @@ class RetailCrmHistory
} }
} }
if ($newOrder === null) { if (!isset($newOrder) || $newOrder === null) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'Bitrix\Sale\Order::load', 'Error order load number=' . $order['number']); RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'Bitrix\Sale\Order::load', 'Error order load number=' . $order['number']);
continue; continue;
@ -649,29 +649,59 @@ class RetailCrmHistory
if (isset($order['items'])) { if (isset($order['items'])) {
$itemUpdate = true; $itemUpdate = true;
$response = RCrmActions::apiMethod($api, 'orderGet', __METHOD__, $order['id']); $response = RCrmActions::apiMethod($api, 'orderGet', __METHOD__, $order['id']);
if (isset($response['order'])) { if (isset($response['order'])) {
$orderTemp = $response['order']; $orderTemp = $response['order'];
$duplicateItems = []; $duplicateItems = [];
foreach ($orderTemp['items'] as $item) { foreach ($orderTemp['items'] as $item) {
$duplicateItems[$item['id']]['externalId'] += $item['offer']['externalId']; $duplicateItems[$item['id']]['externalId'] += $item['offer']['externalId'];
$duplicateItems[$item['id']]['quantity'] += $item['quantity']; $duplicateItems[$item['id']]['quantity'] += $item['quantity'];
$duplicateItems[$item['id']]['discountTotal'] += $item['quantity'] * $item['discountTotal']; $duplicateItems[$item['id']]['discountTotal'] +=
$duplicateItems[$item['id']]['initialPrice'] = (float)$item['initialPrice']; $item['quantity'] * $item['discountTotal'];
$duplicateItems[$item['id']]['price_sum'] = ($item['quantity'] * $item['initialPrice']) - ($item['quantity'] * $item['discountTotal']); $duplicateItems[$item['id']]['initialPrice'] = (float) $item['initialPrice'];
$duplicateItems[$item['id']]['price_sum'] = ($item['quantity'] * $item['initialPrice'])
- ($item['quantity'] * $item['discountTotal']);
} }
unset($orderTemp); unset($orderTemp);
} else {
continue;
} }
$collectItems = []; $collectItems = [];
foreach ($duplicateItems as $it) { foreach ($duplicateItems as $it) {
$collectItems[$it['externalId']]['quantity'] += $it['quantity']; $collectItems[$it['externalId']]['quantity'] += $it['quantity'];
$collectItems[$it['externalId']]['price_sum'] += $it['price_sum']; $collectItems[$it['externalId']]['price_sum'] += $it['price_sum'];
$collectItems[$it['externalId']]['discountTotal_sum'] += $it['discountTotal']; $collectItems[$it['externalId']]['discountTotal_sum'] += $it['discountTotal'];
$collectItems[$it['externalId']]['initialPrice_sum'] += $it['quantity'] * $it['initialPrice'];
if (isset($collectItems[$it['externalId']]['initialPrice_max'])) {
if ($collectItems[$it['externalId']]['initialPrice_max'] < $it['initialPrice']) {
$collectItems[$it['externalId']]['initialPrice_max'] = $it['initialPrice'];
}
} else {
$collectItems[$it['externalId']]['initialPrice_max'] = $it['initialPrice'];
}
$collectItems[$it['externalId']]['initialPricesList'][] = $it['initialPrice'];
}
foreach ($collectItems as $key => $itemData) {
if (count($itemData['initialPricesList']) > 1) {
$discountDelta = 0;
foreach ($itemData['initialPrices'] as $initialPriceItem) {
$delta = $itemData['initialPrice_max'] - (float) $initialPriceItem;
if ($delta !== 0) {
$discountDelta += $delta;
}
}
$collectItems[$key]['discountTotal_sum'] += $discountDelta;
}
} }
$log->write($duplicateItems, 'duplicateItemsOrderHistory'); $log->write($duplicateItems, 'duplicateItemsOrderHistory');
@ -695,10 +725,11 @@ class RetailCrmHistory
if ($item instanceof \Bitrix\Sale\BasketItem) { if ($item instanceof \Bitrix\Sale\BasketItem) {
$elem = self::getInfoElement($product['offer']['externalId']); $elem = self::getInfoElement($product['offer']['externalId']);
$item->setFields(array( $item->setFields(array(
'CURRENCY' => $newOrder->getCurrency(), 'CURRENCY' => $newOrder->getCurrency(),
'LID' => $site, 'LID' => $site,
'BASE_PRICE' => $collectItems[$product['offer']['externalId']]['initialPrice_sum'] / $collectItems[$product['offer']['externalId']]['quantity'], 'BASE_PRICE' => $collectItems[$product['offer']['externalId']]['initialPrice_max'],
'NAME' => $product['offer']['name'] ? RCrmActions::fromJSON($product['offer']['name']) : $elem['NAME'], 'NAME' => $product['offer']['name'] ? RCrmActions::fromJSON($product['offer']['name']) : $elem['NAME'],
'DETAIL_PAGE_URL' => $elem['URL'], 'DETAIL_PAGE_URL' => $elem['URL'],
'PRODUCT_PROVIDER_CLASS' => 'CCatalogProductProvider', 'PRODUCT_PROVIDER_CLASS' => 'CCatalogProductProvider',
@ -725,16 +756,25 @@ class RetailCrmHistory
if ($product['quantity']) { if ($product['quantity']) {
$item->setFieldNoDemand('QUANTITY', $product['quantity']); $item->setFieldNoDemand('QUANTITY', $product['quantity']);
$item->setField('PRICE', $collectItems[$product['offer']['externalId']]['initialPrice_sum'] / $collectItems[$product['offer']['externalId']]['quantity']); }
if (array_key_exists('initialPrice_max', $collectItems[$product['offer']['externalId']])) {
$item->setField(
'BASE_PRICE',
$collectItems[$product['offer']['externalId']]['initialPrice_max']
);
} }
if (array_key_exists('discountTotal_sum', $collectItems[$product['offer']['externalId']])) { if (array_key_exists('discountTotal_sum', $collectItems[$product['offer']['externalId']])) {
$item->setField('CUSTOM_PRICE', 'Y'); $item->setField('CUSTOM_PRICE', 'Y');
$item->setField('DISCOUNT_NAME', ''); $item->setField('DISCOUNT_NAME', '');
$item->setField('DISCOUNT_VALUE', ''); $item->setField('DISCOUNT_VALUE', '');
$item->setField('DISCOUNT_PRICE', $product['discountTotal_sum']);
$price = $collectItems[$product['offer']['externalId']]['price_sum'] / $collectItems[$product['offer']['externalId']]['quantity']; // Полную цену позиции с учётом скидок делим на количество - получаем цену каждой единицы
// товара с учётом скидок.
$price = $collectItems[
$product['offer']['externalId']
]['price_sum'] / $collectItems[$product['offer']['externalId']]['quantity'];
if ('Y' == $optionDiscRound) { if ('Y' == $optionDiscRound) {
$price = self::truncate($price, 2); $price = self::truncate($price, 2);

View File

@ -29,12 +29,17 @@ class RetailCrmOrder
* *
* Creates order or returns order for mass upload * Creates order or returns order for mass upload
* *
* @param array $arFields * @param array $arFields
* @param $api * @param $api
* @param $arParams * @param $arParams
* @param $send * @param bool $send
* @param null $site
* @param string $methodApi
*
* @return boolean * @return boolean
* @return array - array('order' = $order, 'customer' => $customer) * @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
*/ */
public static function orderSend($arFields, $api, $arParams, $send = false, $site = null, $methodApi = 'ordersEdit') public static function orderSend($arFields, $api, $arParams, $send = false, $site = null, $methodApi = 'ordersEdit')
{ {
@ -200,23 +205,15 @@ class RetailCrmOrder
} }
$discount = (double) $product['DISCOUNT_PRICE']; $discount = (double) $product['DISCOUNT_PRICE'];
//discount
$dpItem = $product['BASE_PRICE'] - $product['PRICE']; $dpItem = $product['BASE_PRICE'] - $product['PRICE'];
if ( $dpItem > 0 && $discount <= 0) { if ( $dpItem > 0 && $discount <= 0) {
$discount = $dpItem; $discount = $dpItem;
} }
if ($discount <= 0) {
$item['discountManualAmount'] = 0;
$initialPrice = (double) $product['PRICE'];
} else {
$item['discountManualAmount'] = $discount;
$initialPrice = (double) $product['PRICE'] + $discount;
}
$item['discountManualPercent'] = 0; $item['discountManualPercent'] = 0;
$item['initialPrice'] = $initialPrice; $item['discountManualAmount'] = $discount;
$item['initialPrice'] = (double) $product['BASE_PRICE'];
$order['items'][] = $item; $order['items'][] = $item;
@ -304,9 +301,16 @@ class RetailCrmOrder
/** /**
* Mass order uploading, without repeating; always returns true, but writes error log * Mass order uploading, without repeating; always returns true, but writes error log
* @param $pSize *
* @param $failed -- flag to export failed orders * @param int $pSize
* @param bool $failed -- flag to export failed orders
* @param bool $orderList
*
* @return boolean * @return boolean
* @throws \Bitrix\Main\ArgumentNullException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
* @throws \Bitrix\Main\ArgumentException
*/ */
public static function uploadOrders($pSize = 50, $failed = false, $orderList = false) public static function uploadOrders($pSize = 50, $failed = false, $orderList = false)
{ {
@ -446,6 +450,14 @@ class RetailCrmOrder
return true; return true;
} }
/**
* Converts order object to array
*
* @param \Bitrix\Sale\Order $obOrder
*
* @return array
* @throws \Bitrix\Main\SystemException
*/
public static function orderObjToArr($obOrder) public static function orderObjToArr($obOrder)
{ {
$culture = new \Bitrix\Main\Context\Culture(array("FORMAT_DATETIME" => "Y-m-d HH:i:s")); $culture = new \Bitrix\Main\Context\Culture(array("FORMAT_DATETIME" => "Y-m-d HH:i:s"));