Minor fixes

* take into account the gift wrapping price
* ignore gift products in history
* change order gift status if gift item was deleted
* improvements for controllers
* improvements for history logging
* only use shutdown handler when critical error appears
This commit is contained in:
Pavel 2020-05-07 13:04:44 +03:00 committed by GitHub
parent 9b2a8e1585
commit 06fcce2dec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 315 additions and 6 deletions

View File

@ -42,6 +42,10 @@ if (!isset($_SERVER['REQUEST_METHOD'])) {
$_SERVER['REQUEST_METHOD'] = 'POST'; $_SERVER['REQUEST_METHOD'] = 'POST';
} }
if (!isset($_SERVER['REMOTE_ADDR'])) {
$_SERVER['REMOTE_ADDR'] = '0.0.0.0';
}
require_once dirname(__FILE__) . '/lib/RetailcrmCli.php'; require_once dirname(__FILE__) . '/lib/RetailcrmCli.php';
function retailcrmCliInterruptHandler($signo) function retailcrmCliInterruptHandler($signo)

View File

@ -49,7 +49,9 @@ class RetailcrmConsultantModuleFrontController extends ModuleFrontController
$this->ajax = true; $this->ajax = true;
} }
header('Content-Type: application/json'); if (!headers_sent()) {
header('Content-Type: application/json');
}
if (Tools::substr(_PS_VERSION_, 0, 3) == '1.6') { if (Tools::substr(_PS_VERSION_, 0, 3) == '1.6') {
echo $response; echo $response;

View File

@ -49,7 +49,9 @@ class RetailcrmDaemonCollectorModuleFrontController extends ModuleFrontControlle
$this->ajax = true; $this->ajax = true;
} }
header('Content-Type: application/json'); if (!headers_sent()) {
header('Content-Type: application/json');
}
if (Tools::substr(_PS_VERSION_, 0, 3) == '1.6') { if (Tools::substr(_PS_VERSION_, 0, 3) == '1.6') {
echo $response; echo $response;

View File

@ -49,7 +49,9 @@ class RetailcrmJobsModuleFrontController extends ModuleFrontController
$this->ajax = true; $this->ajax = true;
} }
header('Content-Type: application/json'); if (!headers_sent()) {
header('Content-Type: application/json');
}
if (Tools::substr(_PS_VERSION_, 0, 3) == '1.6') { if (Tools::substr(_PS_VERSION_, 0, 3) == '1.6') {
echo $response; echo $response;

View File

@ -489,8 +489,23 @@ class RetailcrmHistory
static::assignAddressIdByFields($customer, $address); static::assignAddressIdByFields($customer, $address);
if (empty($address->id)) { if (empty($address->id)) {
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<Customer ID: %d> %s::%s',
$address->id_customer,
get_class($address),
'add'
)
);
$address->add(); $address->add();
} else { } else {
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf('<%d> %s::%s', $address->id, get_class($address), 'save')
);
$address->save(); $address->save();
} }
@ -502,12 +517,26 @@ class RetailcrmHistory
$cart->id_address_invoice = (int) $address->id; $cart->id_address_invoice = (int) $address->id;
$cart->id_carrier = (int) $deliveryType; $cart->id_carrier = (int) $deliveryType;
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<Customer ID: %d> %s::%s',
$cart->id_customer,
get_class($cart),
'add'
)
);
$cart->add(); $cart->add();
$products = array(); $products = array();
if (!empty($order['items'])) { if (!empty($order['items'])) {
foreach ($order['items'] as $item) { foreach ($order['items'] as $item) {
if (RetailcrmOrderBuilder::isGiftItem($item)) {
continue;
}
$productId = explode('#', $item['offer']['externalId']); $productId = explode('#', $item['offer']['externalId']);
$product = array(); $product = array();
@ -520,6 +549,17 @@ class RetailcrmHistory
} }
$cart->setWsCartRows($products); $cart->setWsCartRows($products);
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<%d> %s::%s',
$cart->id,
get_class($cart),
'update'
)
);
$cart->update(); $cart->update();
/* /*
@ -581,6 +621,10 @@ class RetailcrmHistory
$product_id = $item['offer']['externalId']; $product_id = $item['offer']['externalId'];
$product_attribute_id = 0; $product_attribute_id = 0;
if (RetailcrmReferences::GIFT_WRAPPING_ITEM_EXTERNAL_ID == $product_id) {
continue;
}
if (strpos($item['offer']['externalId'], '#') !== false) { if (strpos($item['offer']['externalId'], '#') !== false) {
$product_id = explode('#', $item['offer']['externalId']); $product_id = explode('#', $item['offer']['externalId']);
$product_attribute_id = $product_id[1]; $product_attribute_id = $product_id[1];
@ -625,6 +669,16 @@ class RetailcrmHistory
} }
try { try {
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<Customer ID: %d> %s::%s',
$newOrder->id_customer,
get_class($newOrder),
'add'
)
);
$newOrder->add(false, false); $newOrder->add(false, false);
if (isset($newOrderHistoryRecord)) { if (isset($newOrderHistoryRecord)) {
@ -633,6 +687,17 @@ class RetailcrmHistory
$newOrderHistoryRecord->id_employee = static::getFirstEmployeeId(); $newOrderHistoryRecord->id_employee = static::getFirstEmployeeId();
$newOrderHistoryRecord->date_add = date('Y-m-d H:i:s'); $newOrderHistoryRecord->date_add = date('Y-m-d H:i:s');
$newOrderHistoryRecord->date_upd = $newOrderHistoryRecord->date_add; $newOrderHistoryRecord->date_upd = $newOrderHistoryRecord->date_add;
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<Order ID: %d> %s::%s',
$newOrderHistoryRecord->id_order,
get_class($newOrderHistoryRecord),
'add'
)
);
$newOrderHistoryRecord->add(); $newOrderHistoryRecord->add();
} }
} catch (\Exception $e) { } catch (\Exception $e) {
@ -664,6 +729,17 @@ class RetailcrmHistory
$orderPayment->id_currency = $default_currency; $orderPayment->id_currency = $default_currency;
$orderPayment->amount = $payment['amount']; $orderPayment->amount = $payment['amount'];
$orderPayment->date_add = $payment['paidAt']; $orderPayment->date_add = $payment['paidAt'];
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<Order Reference: %s> %s::%s',
$newOrder->reference,
get_class($orderPayment),
'save'
)
);
$orderPayment->save(); $orderPayment->save();
} }
} }
@ -675,6 +751,17 @@ class RetailcrmHistory
$carrier->id_carrier = $deliveryType; $carrier->id_carrier = $deliveryType;
$carrier->shipping_cost_tax_excl = $order['delivery']['cost']; $carrier->shipping_cost_tax_excl = $order['delivery']['cost'];
$carrier->shipping_cost_tax_incl = $order['delivery']['cost']; $carrier->shipping_cost_tax_incl = $order['delivery']['cost'];
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<Order ID: %d> %s::%s',
$carrier->id_order,
get_class($carrier),
'add'
)
);
$carrier->add(false, false); $carrier->add(false, false);
/* /*
@ -738,6 +825,16 @@ class RetailcrmHistory
$orderCarrier->id_order = $orderToUpdate->id; $orderCarrier->id_order = $orderToUpdate->id;
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<%d> %s::%s',
$orderCarrier->id,
get_class($orderCarrier),
'update'
)
);
$orderCarrier->update(); $orderCarrier->update();
} }
} }
@ -777,6 +874,17 @@ class RetailcrmHistory
$orderPayment->id_currency = $default_currency; $orderPayment->id_currency = $default_currency;
$orderPayment->date_add = $orderPayment->date_add =
isset($payment['paidAt']) ? $payment['paidAt'] : date('Y-m-d H:i:s'); isset($payment['paidAt']) ? $payment['paidAt'] : date('Y-m-d H:i:s');
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<Order Reference: %s> %s::%s',
$orderToUpdate->reference,
get_class($orderPayment),
'save'
)
);
$orderPayment->save(); $orderPayment->save();
} }
} }
@ -800,9 +908,31 @@ class RetailcrmHistory
$orderHistory->id_order = $orderToUpdate->id; $orderHistory->id_order = $orderToUpdate->id;
$orderHistory->id_order_state = $statuses[$stype]; $orderHistory->id_order_state = $statuses[$stype];
$orderHistory->date_add = date('Y-m-d H:i:s'); $orderHistory->date_add = date('Y-m-d H:i:s');
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<Order ID: %d> %s::%s',
$orderToUpdate->id,
get_class($orderHistory),
'save'
)
);
$orderHistory->save(); $orderHistory->save();
$orderToUpdate->current_state = $statuses[$stype]; $orderToUpdate->current_state = $statuses[$stype];
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<Order ID: %d> %s::%s',
$orderToUpdate->id,
get_class($orderToUpdate),
'update'
)
);
$orderToUpdate->update(); $orderToUpdate->update();
} }
} }
@ -834,6 +964,10 @@ class RetailcrmHistory
foreach ($order['items'] as $key => $item) { foreach ($order['items'] as $key => $item) {
if (isset($item['delete']) && $item['delete'] == true) { if (isset($item['delete']) && $item['delete'] == true) {
if (RetailcrmOrderBuilder::isGiftItem($item)) {
$orderToUpdate->gift = false;
}
$parsedExtId = static::parseItemExternalId($item); $parsedExtId = static::parseItemExternalId($item);
$product_id = $parsedExtId['product_id']; $product_id = $parsedExtId['product_id'];
$product_attribute_id = $parsedExtId['product_attribute_id']; $product_attribute_id = $parsedExtId['product_attribute_id'];
@ -852,6 +986,10 @@ class RetailcrmHistory
*/ */
foreach ($orderToUpdate->getProductsDetail() as $orderItem) { foreach ($orderToUpdate->getProductsDetail() as $orderItem) {
foreach ($order['items'] as $key => $item) { foreach ($order['items'] as $key => $item) {
if (RetailcrmOrderBuilder::isGiftItem($item)) {
continue;
}
$parsedExtId = static::parseItemExternalId($item); $parsedExtId = static::parseItemExternalId($item);
$product_id = $parsedExtId['product_id']; $product_id = $parsedExtId['product_id'];
$product_attribute_id = $parsedExtId['product_attribute_id']; $product_attribute_id = $parsedExtId['product_attribute_id'];
@ -909,6 +1047,16 @@ class RetailcrmHistory
$orderDetail->product_attribute_id = (int) $product_attribute_id; $orderDetail->product_attribute_id = (int) $product_attribute_id;
$orderDetail->product_quantity = (int) $item['quantity']; $orderDetail->product_quantity = (int) $item['quantity'];
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<Order ID: %d> %s::%s',
$orderDetail->id_order,
get_class($orderDetail),
'save'
)
);
if ($orderDetail->save()) { if ($orderDetail->save()) {
$upOrderItems = array( $upOrderItems = array(
'externalId' => $orderDetail->id_order, 'externalId' => $orderDetail->id_order,
@ -943,6 +1091,16 @@ class RetailcrmHistory
} }
} }
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<%d> %s::%s',
$orderDetail->id,
get_class($orderDetail),
'update'
)
);
$orderDetail->update(); $orderDetail->update();
$ItemDiscount = true; $ItemDiscount = true;
unset($order['items'][$key]); unset($order['items'][$key]);
@ -955,6 +1113,10 @@ class RetailcrmHistory
*/ */
if (!empty($order['items'])) { if (!empty($order['items'])) {
foreach ($order['items'] as $key => $newItem) { foreach ($order['items'] as $key => $newItem) {
if (RetailcrmOrderBuilder::isGiftItem($newItem)) {
continue;
}
$isNewItem = isset($newItem['create']) ? $newItem['create'] : false; $isNewItem = isset($newItem['create']) ? $newItem['create'] : false;
if (!$isNewItem) { if (!$isNewItem) {
@ -1013,6 +1175,16 @@ class RetailcrmHistory
$orderDetail->id_order_detail = $orderDetail->id_order_detail =
!empty($parsedExtId['id_order_detail']) ? $parsedExtId['id_order_detail'] : null; !empty($parsedExtId['id_order_detail']) ? $parsedExtId['id_order_detail'] : null;
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<Order ID: %d> %s::%s',
$orderDetail->id_order,
get_class($orderDetail),
'save'
)
);
if ($orderDetail->save()) { if ($orderDetail->save()) {
$upOrderItems = array( $upOrderItems = array(
'externalId' => $orderDetail->id_order, 'externalId' => $orderDetail->id_order,
@ -1053,6 +1225,17 @@ class RetailcrmHistory
$infoOrder = $infoOrd->order; $infoOrder = $infoOrd->order;
$totalPaid = $infoOrder['totalSumm']; $totalPaid = $infoOrder['totalSumm'];
$orderToUpdate->total_paid = $totalPaid; $orderToUpdate->total_paid = $totalPaid;
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<%d> %s::%s',
$orderToUpdate->id,
get_class($orderToUpdate),
'update'
)
);
$orderToUpdate->update(); $orderToUpdate->update();
/* /*
@ -1085,6 +1268,17 @@ class RetailcrmHistory
$orderToUpdate->total_paid_tax_incl = $totalPaid; $orderToUpdate->total_paid_tax_incl = $totalPaid;
$orderToUpdate->total_paid_tax_excl = $totalPaid; $orderToUpdate->total_paid_tax_excl = $totalPaid;
$orderToUpdate->total_products_wt = $orderTotalProducts; $orderToUpdate->total_products_wt = $orderTotalProducts;
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<%d> %s::%s',
$orderToUpdate->id,
get_class($orderToUpdate),
'update'
)
);
$orderToUpdate->update(); $orderToUpdate->update();
unset($ItemDiscount); unset($ItemDiscount);
} }
@ -1127,6 +1321,28 @@ class RetailcrmHistory
*/ */
private static function loadInCMS($object, $action) private static function loadInCMS($object, $action)
{ {
$prefix = $object->id;
if (empty($object->id)) {
if (property_exists(get_class($object), 'id_customer')) {
$prefix = sprintf('Customer ID: %d', $object->id_customer);
}
if (property_exists(get_class($object), 'id_order')) {
$prefix = sprintf('Order ID: %d', $object->id_order);
}
}
RetailcrmLogger::writeDebug(
__METHOD__,
sprintf(
'<%s> %s::%s',
$prefix,
get_class($object),
$action
)
);
try { try {
$object->$action(); $object->$action();
} catch (PrestaShopException $e) { } catch (PrestaShopException $e) {

View File

@ -392,7 +392,7 @@ class RetailcrmJobManager
register_shutdown_function(function() { register_shutdown_function(function() {
$error = error_get_last(); $error = error_get_last();
if(null !== $error) { if(null !== $error && $error['type'] === E_ERROR) {
self::defaultShutdownHandler($error); self::defaultShutdownHandler($error);
} }
}); });

View File

@ -1016,6 +1016,10 @@ class RetailcrmOrderBuilder
$crmOrder['items'][] = $item; $crmOrder['items'][] = $item;
} }
if ($order->gift && $order->total_wrapping > 0) {
self::setOrderGiftItem($order, $crmOrder);
}
if ($order->id_customer) { if ($order->id_customer) {
if (empty($corporateCustomerId)) { if (empty($corporateCustomerId)) {
$crmOrder['customer']['externalId'] = $order->id_customer; $crmOrder['customer']['externalId'] = $order->id_customer;
@ -1214,4 +1218,80 @@ class RetailcrmOrderBuilder
return RetailcrmTools::clearArray($customer); return RetailcrmTools::clearArray($customer);
} }
/**
* Returns true if provided item array contains placeholder item added for equal price with payment.
*
* @param array $item
*
* @return bool
*/
public static function isGiftItem($item)
{
if (isset($item['offer'])
&& isset($item['offer']['externalId'])
&& $item['offer']['externalId'] == RetailcrmReferences::GIFT_WRAPPING_ITEM_EXTERNAL_ID
) {
return true;
}
if (isset($item['externalIds'])) {
foreach ($item['externalIds'] as $externalId) {
if ($externalId['code'] == 'prestashop'
&& $externalId['value'] == RetailcrmReferences::GIFT_WRAPPING_ITEM_EXTERNAL_ID
) {
return true;
}
}
}
return false;
}
/**
* Returns gift item
*
* @param float $giftItemPrice
*
* @return array
*/
public static function getGiftItem($giftItemPrice)
{
return array(
'externalIds' => array(array(
'code' => 'prestashop',
'value' => RetailcrmReferences::GIFT_WRAPPING_ITEM_EXTERNAL_ID
)),
'offer' => array('externalId' => RetailcrmReferences::GIFT_WRAPPING_ITEM_EXTERNAL_ID),
'productName' => 'Gift Wrapping Cost',
'quantity' => 1,
'initialPrice' => $giftItemPrice,
'purchasePrice' => $giftItemPrice
);
}
/**
* Sets gift item to order (should be called if order is marked as gift)
*
* @param Order|\OrderCore $orderCms
* @param array $orderCrm
*/
private static function setOrderGiftItem($orderCms, &$orderCrm)
{
$isFound = false;
$giftItemPrice = round($orderCms->total_wrapping, 2);
foreach ($orderCrm['items'] as $key => $item) {
if (self::isGiftItem($item)) {
$orderCrm['items'][$key] = self::getGiftItem($giftItemPrice);
$isFound = true;
break;
}
}
if (!$isFound) {
$orderCrm['items'][] = self::getGiftItem($giftItemPrice);
}
}
} }

View File

@ -37,6 +37,8 @@
*/ */
class RetailcrmReferences class RetailcrmReferences
{ {
const GIFT_WRAPPING_ITEM_EXTERNAL_ID = 'giftWrappingCost';
public $default_lang; public $default_lang;
public $carriers; public $carriers;
public $payment_modules = array(); public $payment_modules = array();

View File

@ -216,7 +216,8 @@ class RetailcrmHistoryTest extends RetailcrmTestCase
), ),
'properties' => array(), 'properties' => array(),
'purchasePrice' => 50 'purchasePrice' => 50
) ),
array_merge(RetailcrmOrderBuilder::getGiftItem(10), array('id' => 25919))
), ),
'fromApi' => false, 'fromApi' => false,
'length' => 0, 'length' => 0,
@ -231,7 +232,7 @@ class RetailcrmHistoryTest extends RetailcrmTestCase
$order['payments'][] = array( $order['payments'][] = array(
'id' => 97, 'id' => 97,
'type' => 'cheque', 'type' => 'cheque',
'amount' => 200 'amount' => 210
); );
return $order; return $order;