1
0
mirror of synced 2024-11-29 08:46:09 +03:00

fix discount

This commit is contained in:
gorokh 2020-02-18 10:54:42 +03:00
parent 686548ab63
commit 594feae948
3 changed files with 869 additions and 811 deletions

View File

@ -23,6 +23,7 @@ class RetailCrmHistory
public static $CRM_ORDER_NUMBERS = 'order_numbers'; public static $CRM_ORDER_NUMBERS = 'order_numbers';
public static $CRM_CANSEL_ORDER = 'cansel_order'; public static $CRM_CANSEL_ORDER = 'cansel_order';
public static $CRM_CURRENCY = 'currency'; public static $CRM_CURRENCY = 'currency';
public static $CRM_DISCOUNT_ROUND = 'discount_round';
const CANCEL_PROPERTY_CODE = 'INTAROCRM_IS_CANCELED'; const CANCEL_PROPERTY_CODE = 'INTAROCRM_IS_CANCELED';
@ -652,22 +653,34 @@ class RetailCrmHistory
$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'];
$ditems = [];
$duplicateItems = [];
foreach ($orderTemp['items'] as $item) { foreach ($orderTemp['items'] as $item) {
$ditems[$item['offer']['externalId']]['quantity'] += $item['quantity']; $duplicateItems[$item['id']]['externalId'] += $item['offer']['externalId'];
$ditems[$item['offer']['externalId']]['discountTotal'] += $item['quantity'] * $item['discountTotal']; $duplicateItems[$item['id']]['quantity'] += $item['quantity'];
$ditems[$item['offer']['externalId']]['initialPrice'] = (float)$item['initialPrice']; $duplicateItems[$item['id']]['discountTotal'] += $item['quantity'] * $item['discountTotal'];
$ditems[$item['offer']['externalId']]['price_sum'] = $ditems[$item['offer']['externalId']]['initialPrice'] * $ditems[$item['offer']['externalId']]['quantity'] - $ditems[$item['offer']['externalId']]['discountTotal']; $duplicateItems[$item['id']]['initialPrice'] = (float)$item['initialPrice'];
$ditems[$item['offer']['externalId']]['price_item'] = $ditems[$item['offer']['externalId']]['price_sum'] / $ditems[$item['offer']['externalId']]['quantity']; $duplicateItems[$item['id']]['price_sum'] = ($item['quantity'] * $item['initialPrice']) - ($item['quantity'] * $item['discountTotal']);
} }
unset($orderTemp); unset($orderTemp);
} }
$log->write($ditems, 'duplicateItemsOrderHistory'); $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'];
}
$log->write($duplicateItems, 'duplicateItemsOrderHistory');
$log->write($collectItems, 'collectItemsOrderHistory');
$optionDiscRound = COption::GetOptionString(self::$MODULE_ID, self::$CRM_DISCOUNT_ROUND, 0);
foreach ($order['items'] as $product) { foreach ($order['items'] as $product) {
if($ditems[$product['offer']['externalId']]['quantity']){ if ($collectItems[$product['offer']['externalId']]['quantity']) {
$product['quantity'] = $ditems[$product['offer']['externalId']]['quantity']; $product['quantity'] = $collectItems[$product['offer']['externalId']]['quantity'];
} }
$item = self::getExistsItem($basket, 'catalog', $product['offer']['externalId']); $item = self::getExistsItem($basket, 'catalog', $product['offer']['externalId']);
@ -685,7 +698,7 @@ class RetailCrmHistory
$item->setFields(array( $item->setFields(array(
'CURRENCY' => $newOrder->getCurrency(), 'CURRENCY' => $newOrder->getCurrency(),
'LID' => $site, 'LID' => $site,
'BASE_PRICE' => $product['initialPrice'], 'BASE_PRICE' => $collectItems[$product['offer']['externalId']]['initialPrice_sum'] / $collectItems[$product['offer']['externalId']]['quantity'],
'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',
@ -703,7 +716,7 @@ class RetailCrmHistory
} }
if ($product['delete']) { if ($product['delete']) {
if ($ditems[$product['offer']['externalId']]['quantity'] <= 0) { if ($collectItems[$product['offer']['externalId']]['quantity'] <= 0) {
$item->delete(); $item->delete();
continue; continue;
@ -712,36 +725,22 @@ 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('discountTotal', $product)) { if (array_key_exists('discountTotal_sum', $collectItems[$product['offer']['externalId']])) {
$itemCost = $item->getField('PRICE');
if (empty($itemCost)) {
$itemCost = $item->getField('BASE_PRICE');
}
$discount = (double) $item->getField('DISCOUNT_PRICE');
if (isset($itemCost) && $itemCost >= 0) {
if ($discount < 0) {
$resultDiscount = $product['discountTotal'] + $discount;
} else {
$resultDiscount = $product['discountTotal'];
}
$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', $resultDiscount); $item->setField('DISCOUNT_PRICE', $product['discountTotal_sum']);
$item->setField('PRICE', $itemCost - $resultDiscount);
//set price dublicate item $price = $collectItems[$product['offer']['externalId']]['price_sum'] / $collectItems[$product['offer']['externalId']]['quantity'];
if ($ditems[$product['offer']['externalId']]['price_item']) {
$item->setField('PRICE', $ditems[$product['offer']['externalId']]['price_item']); if ('Y' == $optionDiscRound) {
$item->setField('DISCOUNT_PRICE', ''); $price = self::truncate($price, 2);
}
} }
$item->setField('PRICE', $price);
} }
} }
} }
@ -1378,6 +1377,25 @@ class RetailCrmHistory
return false; return false;
} }
} }
/**
* Truncate a float number
*
* @param float $val
* @param int f Number of precision
*
* @return float
*/
public static function truncate($val, $precision = "0")
{
if(($p = strpos($val, '.')) !== false
|| ($p = strpos($val, ',')) !== false
) {
$val = floatval(substr($val, 0, $p + 1 + $precision));
}
return $val;
}
} }
class RetailUser extends CUser class RetailUser extends CUser

View File

@ -201,23 +201,23 @@ class RetailCrmOrder
$discount = (double) $product['DISCOUNT_PRICE']; $discount = (double) $product['DISCOUNT_PRICE'];
if ($discount < 0) { //discount
$dpItem = $product['BASE_PRICE'] - $product['PRICE'];
if ( $dpItem > 0 && $discount <= 0) {
$discount = $dpItem;
}
if ($discount <= 0) {
$item['discountManualAmount'] = 0; $item['discountManualAmount'] = 0;
$initialPrice = (double) $product['PRICE']; $initialPrice = (double) $product['PRICE'];
} else { } else {
$item['discountManualAmount'] = (double) $product['DISCOUNT_PRICE']; $item['discountManualAmount'] = $discount;
$initialPrice = (double) $product['PRICE'] + (double) $product['DISCOUNT_PRICE']; $initialPrice = (double) $product['PRICE'] + $discount;
} }
$item['discountManualPercent'] = 0; $item['discountManualPercent'] = 0;
$item['initialPrice'] = $initialPrice; $item['initialPrice'] = $initialPrice;
$dpItem = $product['BASE_PRICE'] - $product['PRICE'];
if ( $dpItem > 0) {
$item['discountManualAmount'] = $dpItem;
$item['initialPrice'] = (double)$product['BASE_PRICE'];
}
$order['items'][] = $item; $order['items'][] = $item;
if ($send && $dimensionsSetting == 'Y') { if ($send && $dimensionsSetting == 'Y') {

View File

@ -45,6 +45,8 @@ $CRM_ADDRESS_OPTIONS = 'address_options';
$CRM_DIMENSIONS = 'order_dimensions'; $CRM_DIMENSIONS = 'order_dimensions';
$PROTOCOL = 'protocol'; $PROTOCOL = 'protocol';
$CRM_DISCOUNT_ROUND = 'discount_round';
if(!CModule::IncludeModule('intaro.retailcrm') || !CModule::IncludeModule('sale') || !CModule::IncludeModule('iblock') || !CModule::IncludeModule('catalog')) if(!CModule::IncludeModule('intaro.retailcrm') || !CModule::IncludeModule('sale') || !CModule::IncludeModule('iblock') || !CModule::IncludeModule('catalog'))
return; return;
@ -440,6 +442,15 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
UnRegisterModuleDependences("main", "OnBeforeProlog", $mid, "RetailCrmUa", "add"); UnRegisterModuleDependences("main", "OnBeforeProlog", $mid, "RetailCrmUa", "add");
} }
//discount_round
if (htmlspecialchars(trim($_POST['discount_round'])) == 'Y') {
$discount_round = 'Y';
RegisterModuleDependences("main", "OnBeforeProlog", $mid, "RetailCrmDc", "add");
} else {
$discount_round = 'N';
UnRegisterModuleDependences("main", "OnBeforeProlog", $mid, "RetailCrmDc", "add");
}
//version //version
$version = COption::GetOptionString($mid, $CRM_API_VERSION); $version = COption::GetOptionString($mid, $CRM_API_VERSION);
@ -514,6 +525,8 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
COption::SetOptionString($mid, $CRM_UA_KEYS, serialize(RCrmActions::clearArr($uaKeys))); COption::SetOptionString($mid, $CRM_UA_KEYS, serialize(RCrmActions::clearArr($uaKeys)));
COption::SetOptionString($mid, $CRM_DIMENSIONS, $orderDimensions); COption::SetOptionString($mid, $CRM_DIMENSIONS, $orderDimensions);
COption::SetOptionString($mid, $CRM_DISCOUNT_ROUND, $discount_round);
$request = \Bitrix\Main\Application::getInstance()->getContext()->getRequest(); $request = \Bitrix\Main\Application::getInstance()->getContext()->getRequest();
if ($request->isHttps() === true) { if ($request->isHttps() === true) {
@ -620,6 +633,8 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
$optionUa = COption::GetOptionString($mid, $CRM_UA, 0); $optionUa = COption::GetOptionString($mid, $CRM_UA, 0);
$optionUaKeys = unserialize(COption::GetOptionString($mid, $CRM_UA_KEYS)); $optionUaKeys = unserialize(COption::GetOptionString($mid, $CRM_UA_KEYS));
$optionDiscRound = COption::GetOptionString($mid, $CRM_DISCOUNT_ROUND, 0);
$version = COption::GetOptionString($mid, $CRM_API_VERSION, 0); $version = COption::GetOptionString($mid, $CRM_API_VERSION, 0);
//currency //currency
@ -658,9 +673,9 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
); );
$tabControl = new CAdminTabControl("tabControl", $aTabs); $tabControl = new CAdminTabControl("tabControl", $aTabs);
$tabControl->Begin(); $tabControl->Begin();
?> ?>
<?php $APPLICATION->AddHeadString('<script type="text/javascript" src="/bitrix/js/main/jquery/jquery-1.7.min.js"></script>'); ?> <?php $APPLICATION->AddHeadString('<script type="text/javascript" src="/bitrix/js/main/jquery/jquery-1.7.min.js"></script>'); ?>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
$('input.addr').change(function(){ $('input.addr').change(function(){
splitName = $(this).attr('name').split('-'); splitName = $(this).attr('name').split('-');
@ -727,6 +742,16 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
return true; return true;
}); });
$('.r-dc-button label').change(function(){
if($(this).find('input').is(':checked') === true){
$('tr.r-dc').show('slow');
} else if($(this).find('input').is(':checked') === false){
$('tr.r-dc').hide('slow');
}
return true;
});
}); });
$('input[name="update-delivery-services"]').live('click', function() { $('input[name="update-delivery-services"]').live('click', function() {
@ -760,8 +785,8 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
return false; return false;
}); });
</script> </script>
<style type="text/css"> <style type="text/css">
.option-other-bottom { .option-other-bottom {
border-bottom: 0px !important; border-bottom: 0px !important;
} }
@ -786,12 +811,12 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
color: #4b6267; color: #4b6267;
} }
</style> </style>
<form method="POST" action="<?php echo $uri; ?>" id="FORMACTION"> <form method="POST" action="<?php echo $uri; ?>" id="FORMACTION">
<?php <?php
echo bitrix_sessid_post(); echo bitrix_sessid_post();
$tabControl->BeginNextTab(); $tabControl->BeginNextTab();
?> ?>
<input type="hidden" name="tab" value="catalog"> <input type="hidden" name="tab" value="catalog">
<tr class="heading"> <tr class="heading">
<td colspan="2"><b><?php echo GetMessage('ICRM_CONN_SETTINGS'); ?></b></td> <td colspan="2"><b><?php echo GetMessage('ICRM_CONN_SETTINGS'); ?></b></td>
@ -826,8 +851,8 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>
<?php endif;?> <?php endif;?>
<?php if(!$badKey && !$badJson):?> <?php if(!$badKey && !$badJson):?>
<?php $tabControl->BeginNextTab(); ?> <?php $tabControl->BeginNextTab(); ?>
<input type="hidden" name="tab" value="catalog"> <input type="hidden" name="tab" value="catalog">
<tr class="option-head"> <tr class="option-head">
<td colspan="2"><b><?php echo GetMessage('INFO_1'); ?></b></td> <td colspan="2"><b><?php echo GetMessage('INFO_1'); ?></b></td>
@ -970,7 +995,7 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
</td> </td>
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>
<?php $tabControl->BeginNextTab(); ?> <?php $tabControl->BeginNextTab(); ?>
<input type="hidden" name="tab" value="catalog"> <input type="hidden" name="tab" value="catalog">
<tr class="option-head"> <tr class="option-head">
<td colspan="2"><b><?php echo GetMessage('INFO_2'); ?></b></td> <td colspan="2"><b><?php echo GetMessage('INFO_2'); ?></b></td>
@ -1072,7 +1097,7 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
<?php endforeach; ?> <?php endforeach; ?>
<?php endforeach; ?> <?php endforeach; ?>
<?php $tabControl->BeginNextTab(); ?> <?php $tabControl->BeginNextTab(); ?>
<input type="hidden" name="tab" value="catalog"> <input type="hidden" name="tab" value="catalog">
<tr class="heading"> <tr class="heading">
<td colspan="2" class="option-other-bottom"><b><?php echo GetMessage('ORDERS_OPTIONS'); ?></b></td> <td colspan="2" class="option-other-bottom"><b><?php echo GetMessage('ORDERS_OPTIONS'); ?></b></td>
@ -1263,6 +1288,21 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
</td> </td>
</tr> </tr>
<?php endforeach;?> <?php endforeach;?>
<tr class="heading r-dc-button">
<td colspan="2" class="option-other-heading">
<b>
<label><input class="addr" type="checkbox" name="discount_round" value="Y" <?php if($optionDiscRound === 'Y') echo "checked"; ?>><?php echo "Округление цены товара при сборе одинаковых товарных позиций" ?></label>
</b>
</td>
</tr>
<tr class="r-dc" <?php if($optionDiscRound !== 'Y') echo 'style="display: none;"'; ?>>
<td class="option-head" colspan="2">
<b><?php echo "При включенной опции округление будет происходить в меньшую сторону" ?></b>
</td>
</tr>
<tr class="heading r-ua-button"> <tr class="heading r-ua-button">
<td colspan="2" class="option-other-heading"> <td colspan="2" class="option-other-heading">
<b> <b>
@ -1289,17 +1329,17 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
</td> </td>
</tr> </tr>
<?php endforeach;?> <?php endforeach;?>
<?php endif;?> <?php endif;?>
<?php $tabControl->Buttons(); ?> <?php $tabControl->Buttons(); ?>
<input type="hidden" name="Update" value="Y" /> <input type="hidden" name="Update" value="Y" />
<input type="submit" title="<?php echo GetMessage('ICRM_OPTIONS_SUBMIT_TITLE'); ?>" value="<?php echo GetMessage('ICRM_OPTIONS_SUBMIT_VALUE'); ?>" name="btn-update" class="adm-btn-save" /> <input type="submit" title="<?php echo GetMessage('ICRM_OPTIONS_SUBMIT_TITLE'); ?>" value="<?php echo GetMessage('ICRM_OPTIONS_SUBMIT_VALUE'); ?>" name="btn-update" class="adm-btn-save" />
<?php $tabControl->End(); ?> <?php $tabControl->End(); ?>
</form> </form>
<?php } ?> <?php } ?>
<?php //order upload?> <?php //order upload?>
<?php if($_GET['upl'] == 1){?> <?php if($_GET['upl'] == 1){?>
<style type="text/css"> <style type="text/css">
.instal-load-label { .instal-load-label {
color: #000; color: #000;
margin-bottom: 15px; margin-bottom: 15px;
@ -1370,9 +1410,9 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
position:relative; position:relative;
visible: none; visible: none;
} }
</style> </style>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
$('#percent').width($('.instal-progress-bar-outer').width()); $('#percent').width($('.instal-progress-bar-outer').width());
@ -1426,9 +1466,9 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
return false; return false;
}); });
}); });
</script> </script>
<br> <br>
<form id="upload-orders" action="<?php echo $uri; ?>" method="POST"> <form id="upload-orders" action="<?php echo $uri; ?>" method="POST">
<input type="hidden" name="step" value="0"> <input type="hidden" name="step" value="0">
<div class="adm-detail-content-item-block"> <div class="adm-detail-content-item-block">
<table class="adm-detail-content-table edit-table" id="edit1_edit_table"> <table class="adm-detail-content-table edit-table" id="edit1_edit_table">
@ -1460,5 +1500,5 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
</div> </div>
</div> </div>
</div> </div>
</form> </form>
<?php }?> <?php }?>