1
0
mirror of synced 2025-01-19 01:11:42 +03:00

Add priceType processing to CRM order by history

This commit is contained in:
Dima Uryvskiy 2022-05-24 13:01:29 +03:00 committed by GitHub
parent 6d94082c22
commit 01e3ad71ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 76 deletions

View File

@ -94,6 +94,7 @@
<field id="order_product.quantity" group="item">quantity</field> <field id="order_product.quantity" group="item">quantity</field>
<field id="order_product.status" group="item">status</field> <field id="order_product.status" group="item">status</field>
<field id="order_product.summ" group="item">summ</field> <field id="order_product.summ" group="item">summ</field>
<field id="order_product.price_type" group="item">priceType</field>
<field id="delivery_type" group="delivery">code</field> <field id="delivery_type" group="delivery">code</field>
<field id="delivery_service" group="delivery">service</field> <field id="delivery_service" group="delivery">service</field>

View File

@ -109,7 +109,7 @@ if (!class_exists('WC_Retailcrm_History')) :
$customers = WC_Retailcrm_History_Assembler::assemblyCustomer($history); $customers = WC_Retailcrm_History_Assembler::assemblyCustomer($history);
WC_Retailcrm_Plugin::$history_run = true; WC_Retailcrm_Plugin::$history_run = true;
WC_Retailcrm_Logger::debug(__METHOD__, array('Assembled customers history:', $customers)); WC_Retailcrm_Logger::debug(__METHOD__, ['Assembled customers history:', $customers]);
foreach ($customers as $crmCustomer) { foreach ($customers as $crmCustomer) {
/* /*
@ -157,7 +157,7 @@ if (!class_exists('WC_Retailcrm_History')) :
$this->updateMetaData($customFields, $crmCustomer, $wcCustomer); $this->updateMetaData($customFields, $crmCustomer, $wcCustomer);
} }
WC_Retailcrm_Logger::debug(__METHOD__, array('Updated WC_Customer:', $wcCustomer)); WC_Retailcrm_Logger::debug(__METHOD__, ['Updated WC_Customer:', $wcCustomer]);
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
} catch (Exception $exception) { } catch (Exception $exception) {
@ -210,7 +210,7 @@ if (!class_exists('WC_Retailcrm_History')) :
$lastChange = end($history); $lastChange = end($history);
$historyAssembly = WC_Retailcrm_History_Assembler::assemblyOrder($history); $historyAssembly = WC_Retailcrm_History_Assembler::assemblyOrder($history);
WC_Retailcrm_Logger::debug(__METHOD__, array('Assembled orders history:', $historyAssembly)); WC_Retailcrm_Logger::debug(__METHOD__, ['Assembled orders history:', $historyAssembly]);
WC_Retailcrm_Plugin::$history_run = true; WC_Retailcrm_Plugin::$history_run = true;
foreach ($historyAssembly as $orderHistory) { foreach ($historyAssembly as $orderHistory) {
@ -673,7 +673,7 @@ if (!class_exists('WC_Retailcrm_History')) :
$wcOrder = wc_create_order(['status' => $orderStatus, 'customer_id' => $customerId]); $wcOrder = wc_create_order(['status' => $orderStatus, 'customer_id' => $customerId]);
$wcOrder->set_date_created($order['createdAt']); $wcOrder->set_date_created($order['createdAt']);
$customer = $order['customer']; $customer = $order['customer'];
$contactOrCustomer = array(); $contactOrCustomer = [];
$billingAddress = ''; $billingAddress = '';
if ($this->retailcrm->getCorporateEnabled() && self::isOrderCorporate($order)) { if ($this->retailcrm->getCorporateEnabled() && self::isOrderCorporate($order)) {
@ -784,7 +784,8 @@ if (!class_exists('WC_Retailcrm_History')) :
$wcOrder->set_address($addressBilling, 'billing'); $wcOrder->set_address($addressBilling, 'billing');
$wcOrder->set_address($addressShipping, 'shipping'); $wcOrder->set_address($addressShipping, 'shipping');
$productData = isset($order['items']) ? $order['items'] : array();
$productData = $order['items'] ?? [];
if ($productData) { if ($productData) {
foreach ($productData as $key => $product) { foreach ($productData as $key => $product) {
@ -792,8 +793,9 @@ if (!class_exists('WC_Retailcrm_History')) :
continue; continue;
} }
$arItemsNew = array(); $arItemsNew = [];
$arItemsOld = array(); $arItemsOld = [];
$item = retailcrm_get_wc_product($product['offer'][$this->bindField], $this->retailcrmSettings); $item = retailcrm_get_wc_product($product['offer'][$this->bindField], $this->retailcrmSettings);
if (!$item) { if (!$item) {
@ -809,20 +811,22 @@ if (!class_exists('WC_Retailcrm_History')) :
$wcOrder->add_product( $wcOrder->add_product(
$item, $item,
$product['quantity'], $product['quantity'],
array( [
'subtotal' => wc_get_price_excluding_tax( 'subtotal' => wc_get_price_excluding_tax(
$item, $item,
array( [
'price' => $product['initialPrice'], 'price' => $product['initialPrice'],
'qty' => $product['quantity'],) 'qty' => $product['quantity'],
]
), ),
'total' => wc_get_price_excluding_tax( 'total' => wc_get_price_excluding_tax(
$item, $item,
array( [
'price' => $product['initialPrice'] - $product['discountTotal'], 'price' => $product['initialPrice'] - $product['discountTotal'],
'qty' => $product['quantity'],) 'qty' => $product['quantity'],
]
), ),
) ]
); );
foreach ($wcOrder->get_items() as $orderItemId => $orderItem) { foreach ($wcOrder->get_items() as $orderItemId => $orderItem) {
@ -873,10 +877,10 @@ if (!class_exists('WC_Retailcrm_History')) :
} }
} }
$ids[] = array( $ids[] = [
'id' => (int) $order['id'], 'id' => (int) $order['id'],
'externalId' => (int) $wcOrder->get_id() 'externalId' => (int) $wcOrder->get_id()
); ];
$wcOrder->save(); $wcOrder->save();
@ -888,7 +892,7 @@ if (!class_exists('WC_Retailcrm_History')) :
} }
/** /**
* @param $order * @param array $order Data CRM order.
* @param string $event * @param string $event
*/ */
protected function editOrder($order, $event = 'create') protected function editOrder($order, $event = 'create')
@ -910,15 +914,16 @@ if (!class_exists('WC_Retailcrm_History')) :
$data = $order; $data = $order;
} }
$iterableItems = isset($data['items']) ? $data['items'] : array(); $iterableItems = $data['items'] ?? [];
foreach ($iterableItems as $id => $item) { foreach ($iterableItems as $id => $item) {
if (isset($item['delete']) && $item['delete'] == true) { if (isset($item['delete']) && $item['delete'] == true) {
continue; continue;
} }
$orderItems[$id]['id'] = $item['id']; $orderItems[$id]['id'] = $item['id'];
$orderItems[$id]['offer'] = array('id' => $item['offer']['id']); $orderItems[$id]['offer'] = ['id' => $item['offer']['id']];
$orderItems[$id]['priceType'] = $item['priceType'] ?? '';
if (!isset($order['items'][$item['id']])) { if (!isset($order['items'][$item['id']])) {
if (empty($crmOrder)) { if (empty($crmOrder)) {
@ -960,12 +965,12 @@ if (!class_exists('WC_Retailcrm_History')) :
continue; continue;
} }
$externalIds = array( $externalIds = [
array( [
'code' => 'woocomerce', 'code' => 'woocomerce',
'value' => $item['offer']['externalId'] . '_' . $woocommerceId, 'value' => $item['offer']['externalId'] . '_' . $woocommerceId,
) ]
); ];
if (!empty($item['externalIds'])) { if (!empty($item['externalIds'])) {
$found = false; $found = false;
@ -988,10 +993,10 @@ if (!class_exists('WC_Retailcrm_History')) :
} }
if (!empty($orderItems)) { if (!empty($orderItems)) {
$orderEdit = array( $orderEdit = [
'id' => $order['id'], 'id' => $order['id'],
'items' => WC_Retailcrm_Plugin::clearArray($orderItems), 'items' => WC_Retailcrm_Plugin::clearArray($orderItems),
); ];
$this->retailcrm->ordersEdit($orderEdit, 'id'); $this->retailcrm->ordersEdit($orderEdit, 'id');
} }
@ -1007,20 +1012,15 @@ if (!class_exists('WC_Retailcrm_History')) :
*/ */
protected function handleCustomerDataChange($wcOrder, $order) protected function handleCustomerDataChange($wcOrder, $order)
{ {
$handled = false; $data = new WC_Retailcrm_Customer_Switcher_State();
$crmOrder = array(); $handled = false;
$switcher = new WC_Retailcrm_Customer_Switcher();
$crmOrder = [];
$newCustomerId = null; $newCustomerId = null;
$switcher = new WC_Retailcrm_Customer_Switcher();
$data = new WC_Retailcrm_Customer_Switcher_State();
$data->setWcOrder($wcOrder); $data->setWcOrder($wcOrder);
WC_Retailcrm_Logger::debug( WC_Retailcrm_Logger::debug(__METHOD__, ['processing order', $order]);
__METHOD__,
[
'processing order',
$order
]
);
if (isset($order['customer'])) { if (isset($order['customer'])) {
$crmOrder = $this->getCRMOrder($order['id'], 'id'); $crmOrder = $this->getCRMOrder($order['id'], 'id');
@ -1034,18 +1034,18 @@ if (!class_exists('WC_Retailcrm_History')) :
return false; return false;
} }
$newCustomerId = $order['customer']['id']; $newCustomerId = $order['customer']['id'];
$isChangedToRegular = self::isCustomerChangedToRegular($order); $isChangedToRegular = self::isCustomerChangedToRegular($order);
$isChangedToCorporate = self::isCustomerChangedToLegal($order); $isChangedToCorporate = self::isCustomerChangedToLegal($order);
if (!$isChangedToRegular && !$isChangedToCorporate) { if (!$isChangedToRegular && !$isChangedToCorporate) {
$isChangedToCorporate = self::isOrderCorporate($crmOrder); $isChangedToCorporate = self::isOrderCorporate($crmOrder);
$isChangedToRegular = !$isChangedToCorporate; $isChangedToRegular = !$isChangedToCorporate;
} }
if ($isChangedToRegular) { if ($isChangedToRegular) {
$this->prepareChangeToIndividual( $this->prepareChangeToIndividual(
self::arrayValue($crmOrder, 'customer', array()), self::arrayValue($crmOrder, 'customer', []),
$data $data
); );
} }
@ -1074,7 +1074,7 @@ if (!class_exists('WC_Retailcrm_History')) :
true true
); );
$data->setNewCustomer(array()); $data->setNewCustomer([]);
} }
} }
@ -1288,7 +1288,7 @@ if (!class_exists('WC_Retailcrm_History')) :
*/ */
private static function noRealDataInEntity($entity) private static function noRealDataInEntity($entity)
{ {
$allowedKeys = array('id', 'externalId', 'site'); $allowedKeys = ['id', 'externalId', 'site'];
if (count($entity) <= 3) { if (count($entity) <= 3) {
foreach (array_keys($entity) as $key) { foreach (array_keys($entity) as $key) {

View File

@ -264,6 +264,7 @@ if (!class_exists('WC_Retailcrm_Orders')) :
} }
$wcOrder = wc_get_order($order_id); $wcOrder = wc_get_order($order_id);
$this->processOrder($wcOrder, true); $this->processOrder($wcOrder, true);
$response = $this->retailcrm->ordersEdit($this->order); $response = $this->retailcrm->ordersEdit($this->order);
@ -387,6 +388,7 @@ if (!class_exists('WC_Retailcrm_Orders')) :
/** @var WC_Order_Item_Product $item */ /** @var WC_Order_Item_Product $item */
foreach ($order->get_items() as $item) { foreach ($order->get_items() as $item) {
$orderItems[] = $this->order_item->build($item)->get_data(); $orderItems[] = $this->order_item->build($item)->get_data();
$this->order_item->reset_data(); $this->order_item->reset_data();
} }

View File

@ -23,14 +23,14 @@ class WC_Retailcrm_History_Assembler
public static function assemblyOrder($orderHistory) public static function assemblyOrder($orderHistory)
{ {
$fields = self::getMappingValues(); $fields = self::getMappingValues();
$orders = array(); $orders = [];
$orderHistory = self::filterHistory($orderHistory, 'order'); $orderHistory = self::filterHistory($orderHistory, 'order');
foreach ($orderHistory as $change) { foreach ($orderHistory as $change) {
$change['order'] = self::removeEmpty($change['order']); $change['order'] = self::removeEmpty($change['order']);
if (isset($change['order']['items']) && $change['order']['items']) { if (isset($change['order']['items']) && $change['order']['items']) {
$items = array(); $items = [];
foreach ($change['order']['items'] as $item) { foreach ($change['order']['items'] as $item) {
if (isset($change['created'])) { if (isset($change['created'])) {

View File

@ -5,29 +5,28 @@ if (! defined('ABSPATH')) {
} }
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
// TODO: There is a task to analyze the work
function get_wc_shipping_methods_by_zones($enhanced = false) function get_wc_shipping_methods_by_zones($enhanced = false)
{ {
$result = array(); $result = [];
$shippingZones = WC_Shipping_Zones::get_zones(); $shippingZones = WC_Shipping_Zones::get_zones();
$defaultZone = WC_Shipping_Zones::get_zone_by(); $defaultZone = WC_Shipping_Zones::get_zone_by();
$shippingZones[$defaultZone->get_id()] = array( $shippingZones[$defaultZone->get_id()] = [
$defaultZone->get_data(), $defaultZone->get_data(),
'zone_id' => $defaultZone->get_id(), 'zone_id' => $defaultZone->get_id(),
'formatted_zone_location' => $defaultZone->get_formatted_location(), 'formatted_zone_location' => $defaultZone->get_formatted_location(),
'shipping_methods' => $defaultZone->get_shipping_methods(false) 'shipping_methods' => $defaultZone->get_shipping_methods(false)
); ];
if ($shippingZones) { if ($shippingZones) {
foreach ($shippingZones as $code => $shippingZone) { foreach ($shippingZones as $code => $shippingZone) {
foreach ($shippingZone['shipping_methods'] as $key => $shipping_method) { foreach ($shippingZone['shipping_methods'] as $key => $shipping_method) {
$shipping_methods = array( $shipping_methods = [
'id' => $shipping_method->id, 'id' => $shipping_method->id,
'instance_id' => $shipping_method->instance_id, 'instance_id' => $shipping_method->instance_id,
'title' => $shipping_method->title 'title' => $shipping_method->title
); ];
if ($enhanced) { if ($enhanced) {
$shipping_code = $shipping_method->id; $shipping_code = $shipping_method->id;
@ -36,12 +35,12 @@ function get_wc_shipping_methods_by_zones($enhanced = false)
} }
if (!isset($result[$shipping_code])) { if (!isset($result[$shipping_code])) {
$result[$shipping_code] = array( $result[$shipping_code] = [
'name' => $shipping_method->method_title, 'name' => $shipping_method->method_title,
'enabled' => $shipping_method->enabled, 'enabled' => $shipping_method->enabled,
'description' => $shipping_method->method_description, 'description' => $shipping_method->method_description,
'title' => $shipping_method->title 'title' => $shipping_method->title
); ];
} }
if ($enhanced) { if ($enhanced) {
@ -61,15 +60,15 @@ function get_wc_shipping_methods()
$wc_shipping = WC_Shipping::instance(); $wc_shipping = WC_Shipping::instance();
$shipping_methods = $wc_shipping->get_shipping_methods(); $shipping_methods = $wc_shipping->get_shipping_methods();
$result = array(); $result = [];
foreach ($shipping_methods as $code => $shipping) { foreach ($shipping_methods as $code => $shipping) {
$result[$code] = array( $result[$code] = [
'name' => $shipping->method_title, 'name' => $shipping->method_title,
'enabled' => $shipping->enabled, 'enabled' => $shipping->enabled,
'description' => $shipping->method_description, 'description' => $shipping->method_description,
'title' => $shipping->title ? $shipping->title : $shipping->method_title 'title' => $shipping->title ? $shipping->title : $shipping->method_title
); ];
} }
return apply_filters('retailcrm_shipping_list', WC_Retailcrm_Plugin::clearArray($result)); return apply_filters('retailcrm_shipping_list', WC_Retailcrm_Plugin::clearArray($result));
@ -80,7 +79,7 @@ function retailcrm_get_delivery_service($method_id, $instance_id)
$shippings_by_zone = get_wc_shipping_methods_by_zones(true); $shippings_by_zone = get_wc_shipping_methods_by_zones(true);
$method = explode(':', $method_id); $method = explode(':', $method_id);
$method_id = $method[0]; $method_id = $method[0];
$shipping = isset($shippings_by_zone[$method_id]) ? $shippings_by_zone[$method_id] : array(); $shipping = $shippings_by_zone[$method_id] ?? [];
if ($shipping && isset($shipping['shipping_methods'][$method_id . ':' . $instance_id])) { if ($shipping && isset($shipping['shipping_methods'][$method_id . ':' . $instance_id])) {
return $shipping['shipping_methods'][$method_id . ':' . $instance_id]; return $shipping['shipping_methods'][$method_id . ':' . $instance_id];
@ -128,7 +127,7 @@ function retailcrm_is_debug()
*/ */
function is_wplogin() function is_wplogin()
{ {
$ABSPATH_MY = str_replace(array('\\','/'), DIRECTORY_SEPARATOR, ABSPATH); $ABSPATH_MY = str_replace(['\\','/'], DIRECTORY_SEPARATOR, ABSPATH);
return ( return (
(in_array($ABSPATH_MY . 'wp-login.php', get_included_files()) (in_array($ABSPATH_MY . 'wp-login.php', get_included_files())

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* PHP version 5.6 * PHP version 5.6
* *
@ -15,17 +16,17 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
/** /**
* @var array order item * @var array order item
*/ */
protected $data = array( protected $data = [
'offer' => array(), 'offer' => [],
'productName' => '', 'productName' => '',
'initialPrice' => 0.00, 'initialPrice' => 0.00,
'quantity' => 0.00 'quantity' => 0.00
); ];
/** /**
* @var array * @var array
*/ */
protected $settings = array(); protected $settings = [];
/** /**
* WC_Retailcrm_Order_Item constructor. * WC_Retailcrm_Order_Item constructor.
@ -47,21 +48,21 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
$price = $this->calculate_price($item); $price = $this->calculate_price($item);
$discount_price = $this->calculate_discount($item, $price); $discount_price = $this->calculate_discount($item, $price);
$data['productName'] = $item['name']; $data['productName'] = $item['name'];
$data['initialPrice'] = (float)$price; $data['initialPrice'] = $price;
$data['quantity'] = (double)$item['qty']; $data['quantity'] = (double)$item['qty'];
$itemId = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id']; $itemId = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id'];
$data['externalIds'] = array( $data['externalIds'] = [
array( [
'code' =>'woocomerce', 'code' => 'woocomerce',
'value' => $itemId . '_' . $item->get_id(), 'value' => $itemId . '_' . $item->get_id(),
) ]
); ];
$this->set_data_fields($data); $this->set_data_fields($data);
$this->set_offer($item); $this->set_offer($item);
$this->set_data_field('discountManualAmount', (float) round($discount_price, 2)); $this->set_data_field('discountManualAmount', round($discount_price, 2));
return $this; return $this;
} }
@ -112,9 +113,9 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
*/ */
private function calculate_discount(WC_Order_Item_Product $item, $price) private function calculate_discount(WC_Order_Item_Product $item, $price)
{ {
$product_price = $item->get_total() ? $item->get_total() / $item->get_quantity() : 0; $product_price = $item->get_total() ? $item->get_total() / $item->get_quantity() : 0;
$product_tax = $item->get_total_tax() ? $item->get_total_tax() / $item->get_quantity() : 0; $product_tax = $item->get_total_tax() ? $item->get_total_tax() / $item->get_quantity() : 0;
$price_item = $product_price + $product_tax; $price_item = $product_price + $product_tax;
$discount_price = $price - $price_item; $discount_price = $price - $price_item;
return round($discount_price, 2); return round($discount_price, 2);
@ -125,11 +126,11 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
*/ */
public function reset_data() public function reset_data()
{ {
$this->data = array( $this->data = [
'offer' => array(), 'offer' => [],
'productName' => '', 'productName' => '',
'initialPrice' => 0.00, 'initialPrice' => 0.00,
'quantity' => 0.00 'quantity' => 0.00
); ];
} }
} }