1
0
mirror of synced 2024-11-22 13:26:10 +03:00

fix discounts for multiple positions & annotations

This commit is contained in:
Pavel 2020-02-19 14:35:56 +03:00
parent 594feae948
commit d47709ba87
3 changed files with 85 additions and 30 deletions

View File

@ -91,7 +91,9 @@ class RetailCrmEvent
* @param $event
*
* @return bool
* @throws InvalidArgumentException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
* @throws \Bitrix\Main\ArgumentException
*/
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']);
continue;
@ -649,29 +649,59 @@ class RetailCrmHistory
if (isset($order['items'])) {
$itemUpdate = true;
$response = RCrmActions::apiMethod($api, 'orderGet', __METHOD__, $order['id']);
if (isset($response['order'])) {
$orderTemp = $response['order'];
$duplicateItems = [];
foreach ($orderTemp['items'] as $item) {
$duplicateItems[$item['id']]['externalId'] += $item['offer']['externalId'];
$duplicateItems[$item['id']]['quantity'] += $item['quantity'];
$duplicateItems[$item['id']]['discountTotal'] += $item['quantity'] * $item['discountTotal'];
$duplicateItems[$item['id']]['initialPrice'] = (float)$item['initialPrice'];
$duplicateItems[$item['id']]['price_sum'] = ($item['quantity'] * $item['initialPrice']) - ($item['quantity'] * $item['discountTotal']);
$duplicateItems[$item['id']]['discountTotal'] +=
$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);
} else {
continue;
}
$collectItems = [];
foreach ($duplicateItems as $it) {
$collectItems[$it['externalId']]['quantity'] += $it['quantity'];
$collectItems[$it['externalId']]['price_sum'] += $it['price_sum'];
$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');
@ -695,10 +725,11 @@ class RetailCrmHistory
if ($item instanceof \Bitrix\Sale\BasketItem) {
$elem = self::getInfoElement($product['offer']['externalId']);
$item->setFields(array(
'CURRENCY' => $newOrder->getCurrency(),
'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'],
'DETAIL_PAGE_URL' => $elem['URL'],
'PRODUCT_PROVIDER_CLASS' => 'CCatalogProductProvider',
@ -725,7 +756,13 @@ class RetailCrmHistory
if ($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']])) {
@ -734,7 +771,11 @@ class RetailCrmHistory
$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) {
$price = self::truncate($price, 2);

View File

@ -29,12 +29,17 @@ class RetailCrmOrder
*
* Creates order or returns order for mass upload
*
* @param array $arFields
* @param $api
* @param $arParams
* @param $send
* @param array $arFields
* @param $api
* @param $arParams
* @param bool $send
* @param null $site
* @param string $methodApi
*
* @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')
{
@ -200,23 +205,15 @@ class RetailCrmOrder
}
$discount = (double) $product['DISCOUNT_PRICE'];
//discount
$dpItem = $product['BASE_PRICE'] - $product['PRICE'];
if ( $dpItem > 0 && $discount <= 0) {
$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['initialPrice'] = $initialPrice;
$item['discountManualAmount'] = $discount;
$item['initialPrice'] = (double) $product['BASE_PRICE'];
$order['items'][] = $item;
@ -304,9 +301,16 @@ class RetailCrmOrder
/**
* 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
* @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)
{
@ -446,6 +450,14 @@ class RetailCrmOrder
return true;
}
/**
* Converts order object to array
*
* @param \Bitrix\Sale\Order $obOrder
*
* @return array
* @throws \Bitrix\Main\SystemException
*/
public static function orderObjToArr($obOrder)
{
$culture = new \Bitrix\Main\Context\Culture(array("FORMAT_DATETIME" => "Y-m-d HH:i:s"));