From 01e3ad71ee75712985ba2fb9db4a9c94d4e41b00 Mon Sep 17 00:00:00 2001 From: Dima Uryvskiy Date: Tue, 24 May 2022 13:01:29 +0300 Subject: [PATCH] Add priceType processing to CRM order by history --- src/config/objects.xml | 1 + src/include/class-wc-retailcrm-history.php | 84 +++++++++---------- src/include/class-wc-retailcrm-orders.php | 2 + .../class-wc-retailcrm-history-assembler.php | 4 +- src/include/functions.php | 25 +++--- .../order/class-wc-retailcrm-order-item.php | 39 ++++----- 6 files changed, 79 insertions(+), 76 deletions(-) diff --git a/src/config/objects.xml b/src/config/objects.xml index 44e35b1..c9daa4a 100644 --- a/src/config/objects.xml +++ b/src/config/objects.xml @@ -94,6 +94,7 @@ quantity status summ + priceType code service diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index e9d35a0..70a7a48 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -109,7 +109,7 @@ if (!class_exists('WC_Retailcrm_History')) : $customers = WC_Retailcrm_History_Assembler::assemblyCustomer($history); 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) { /* @@ -157,7 +157,7 @@ if (!class_exists('WC_Retailcrm_History')) : $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 } catch (Exception $exception) { @@ -210,7 +210,7 @@ if (!class_exists('WC_Retailcrm_History')) : $lastChange = end($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; foreach ($historyAssembly as $orderHistory) { @@ -673,7 +673,7 @@ if (!class_exists('WC_Retailcrm_History')) : $wcOrder = wc_create_order(['status' => $orderStatus, 'customer_id' => $customerId]); $wcOrder->set_date_created($order['createdAt']); $customer = $order['customer']; - $contactOrCustomer = array(); + $contactOrCustomer = []; $billingAddress = ''; 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($addressShipping, 'shipping'); - $productData = isset($order['items']) ? $order['items'] : array(); + + $productData = $order['items'] ?? []; if ($productData) { foreach ($productData as $key => $product) { @@ -792,8 +793,9 @@ if (!class_exists('WC_Retailcrm_History')) : continue; } - $arItemsNew = array(); - $arItemsOld = array(); + $arItemsNew = []; + $arItemsOld = []; + $item = retailcrm_get_wc_product($product['offer'][$this->bindField], $this->retailcrmSettings); if (!$item) { @@ -809,20 +811,22 @@ if (!class_exists('WC_Retailcrm_History')) : $wcOrder->add_product( $item, $product['quantity'], - array( + [ 'subtotal' => wc_get_price_excluding_tax( $item, - array( + [ 'price' => $product['initialPrice'], - 'qty' => $product['quantity'],) + 'qty' => $product['quantity'], + ] ), 'total' => wc_get_price_excluding_tax( $item, - array( + [ 'price' => $product['initialPrice'] - $product['discountTotal'], - 'qty' => $product['quantity'],) + 'qty' => $product['quantity'], + ] ), - ) + ] ); foreach ($wcOrder->get_items() as $orderItemId => $orderItem) { @@ -873,10 +877,10 @@ if (!class_exists('WC_Retailcrm_History')) : } } - $ids[] = array( + $ids[] = [ 'id' => (int) $order['id'], 'externalId' => (int) $wcOrder->get_id() - ); + ]; $wcOrder->save(); @@ -888,7 +892,7 @@ if (!class_exists('WC_Retailcrm_History')) : } /** - * @param $order + * @param array $order Data CRM order. * @param string $event */ protected function editOrder($order, $event = 'create') @@ -910,15 +914,16 @@ if (!class_exists('WC_Retailcrm_History')) : $data = $order; } - $iterableItems = isset($data['items']) ? $data['items'] : array(); + $iterableItems = $data['items'] ?? []; foreach ($iterableItems as $id => $item) { if (isset($item['delete']) && $item['delete'] == true) { continue; } - $orderItems[$id]['id'] = $item['id']; - $orderItems[$id]['offer'] = array('id' => $item['offer']['id']); + $orderItems[$id]['id'] = $item['id']; + $orderItems[$id]['offer'] = ['id' => $item['offer']['id']]; + $orderItems[$id]['priceType'] = $item['priceType'] ?? ''; if (!isset($order['items'][$item['id']])) { if (empty($crmOrder)) { @@ -960,12 +965,12 @@ if (!class_exists('WC_Retailcrm_History')) : continue; } - $externalIds = array( - array( + $externalIds = [ + [ 'code' => 'woocomerce', 'value' => $item['offer']['externalId'] . '_' . $woocommerceId, - ) - ); + ] + ]; if (!empty($item['externalIds'])) { $found = false; @@ -988,10 +993,10 @@ if (!class_exists('WC_Retailcrm_History')) : } if (!empty($orderItems)) { - $orderEdit = array( + $orderEdit = [ 'id' => $order['id'], 'items' => WC_Retailcrm_Plugin::clearArray($orderItems), - ); + ]; $this->retailcrm->ordersEdit($orderEdit, 'id'); } @@ -1007,20 +1012,15 @@ if (!class_exists('WC_Retailcrm_History')) : */ protected function handleCustomerDataChange($wcOrder, $order) { - $handled = false; - $crmOrder = array(); + $data = new WC_Retailcrm_Customer_Switcher_State(); + $handled = false; + $switcher = new WC_Retailcrm_Customer_Switcher(); + $crmOrder = []; $newCustomerId = null; - $switcher = new WC_Retailcrm_Customer_Switcher(); - $data = new WC_Retailcrm_Customer_Switcher_State(); + $data->setWcOrder($wcOrder); - WC_Retailcrm_Logger::debug( - __METHOD__, - [ - 'processing order', - $order - ] - ); + WC_Retailcrm_Logger::debug(__METHOD__, ['processing order', $order]); if (isset($order['customer'])) { $crmOrder = $this->getCRMOrder($order['id'], 'id'); @@ -1034,18 +1034,18 @@ if (!class_exists('WC_Retailcrm_History')) : return false; } - $newCustomerId = $order['customer']['id']; - $isChangedToRegular = self::isCustomerChangedToRegular($order); + $newCustomerId = $order['customer']['id']; + $isChangedToRegular = self::isCustomerChangedToRegular($order); $isChangedToCorporate = self::isCustomerChangedToLegal($order); if (!$isChangedToRegular && !$isChangedToCorporate) { $isChangedToCorporate = self::isOrderCorporate($crmOrder); - $isChangedToRegular = !$isChangedToCorporate; + $isChangedToRegular = !$isChangedToCorporate; } if ($isChangedToRegular) { $this->prepareChangeToIndividual( - self::arrayValue($crmOrder, 'customer', array()), + self::arrayValue($crmOrder, 'customer', []), $data ); } @@ -1074,7 +1074,7 @@ if (!class_exists('WC_Retailcrm_History')) : true ); - $data->setNewCustomer(array()); + $data->setNewCustomer([]); } } @@ -1288,7 +1288,7 @@ if (!class_exists('WC_Retailcrm_History')) : */ private static function noRealDataInEntity($entity) { - $allowedKeys = array('id', 'externalId', 'site'); + $allowedKeys = ['id', 'externalId', 'site']; if (count($entity) <= 3) { foreach (array_keys($entity) as $key) { diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 2a96e1d..41cda24 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -264,6 +264,7 @@ if (!class_exists('WC_Retailcrm_Orders')) : } $wcOrder = wc_get_order($order_id); + $this->processOrder($wcOrder, true); $response = $this->retailcrm->ordersEdit($this->order); @@ -387,6 +388,7 @@ if (!class_exists('WC_Retailcrm_Orders')) : /** @var WC_Order_Item_Product $item */ foreach ($order->get_items() as $item) { $orderItems[] = $this->order_item->build($item)->get_data(); + $this->order_item->reset_data(); } diff --git a/src/include/components/class-wc-retailcrm-history-assembler.php b/src/include/components/class-wc-retailcrm-history-assembler.php index 486a80d..788f176 100644 --- a/src/include/components/class-wc-retailcrm-history-assembler.php +++ b/src/include/components/class-wc-retailcrm-history-assembler.php @@ -23,14 +23,14 @@ class WC_Retailcrm_History_Assembler public static function assemblyOrder($orderHistory) { $fields = self::getMappingValues(); - $orders = array(); + $orders = []; $orderHistory = self::filterHistory($orderHistory, 'order'); foreach ($orderHistory as $change) { $change['order'] = self::removeEmpty($change['order']); if (isset($change['order']['items']) && $change['order']['items']) { - $items = array(); + $items = []; foreach ($change['order']['items'] as $item) { if (isset($change['created'])) { diff --git a/src/include/functions.php b/src/include/functions.php index a958091..cbf57f8 100644 --- a/src/include/functions.php +++ b/src/include/functions.php @@ -5,29 +5,28 @@ if (! defined('ABSPATH')) { } // @codeCoverageIgnoreStart -// TODO: There is a task to analyze the work function get_wc_shipping_methods_by_zones($enhanced = false) { - $result = array(); + $result = []; $shippingZones = WC_Shipping_Zones::get_zones(); $defaultZone = WC_Shipping_Zones::get_zone_by(); - $shippingZones[$defaultZone->get_id()] = array( + $shippingZones[$defaultZone->get_id()] = [ $defaultZone->get_data(), 'zone_id' => $defaultZone->get_id(), 'formatted_zone_location' => $defaultZone->get_formatted_location(), 'shipping_methods' => $defaultZone->get_shipping_methods(false) - ); + ]; if ($shippingZones) { foreach ($shippingZones as $code => $shippingZone) { foreach ($shippingZone['shipping_methods'] as $key => $shipping_method) { - $shipping_methods = array( + $shipping_methods = [ 'id' => $shipping_method->id, 'instance_id' => $shipping_method->instance_id, 'title' => $shipping_method->title - ); + ]; if ($enhanced) { $shipping_code = $shipping_method->id; @@ -36,12 +35,12 @@ function get_wc_shipping_methods_by_zones($enhanced = false) } if (!isset($result[$shipping_code])) { - $result[$shipping_code] = array( + $result[$shipping_code] = [ 'name' => $shipping_method->method_title, 'enabled' => $shipping_method->enabled, 'description' => $shipping_method->method_description, 'title' => $shipping_method->title - ); + ]; } if ($enhanced) { @@ -61,15 +60,15 @@ function get_wc_shipping_methods() $wc_shipping = WC_Shipping::instance(); $shipping_methods = $wc_shipping->get_shipping_methods(); - $result = array(); + $result = []; foreach ($shipping_methods as $code => $shipping) { - $result[$code] = array( + $result[$code] = [ 'name' => $shipping->method_title, 'enabled' => $shipping->enabled, 'description' => $shipping->method_description, 'title' => $shipping->title ? $shipping->title : $shipping->method_title - ); + ]; } 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); $method = explode(':', $method_id); $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])) { return $shipping['shipping_methods'][$method_id . ':' . $instance_id]; @@ -128,7 +127,7 @@ function retailcrm_is_debug() */ function is_wplogin() { - $ABSPATH_MY = str_replace(array('\\','/'), DIRECTORY_SEPARATOR, ABSPATH); + $ABSPATH_MY = str_replace(['\\','/'], DIRECTORY_SEPARATOR, ABSPATH); return ( (in_array($ABSPATH_MY . 'wp-login.php', get_included_files()) diff --git a/src/include/order/class-wc-retailcrm-order-item.php b/src/include/order/class-wc-retailcrm-order-item.php index 635bac0..3b1236a 100644 --- a/src/include/order/class-wc-retailcrm-order-item.php +++ b/src/include/order/class-wc-retailcrm-order-item.php @@ -1,4 +1,5 @@ array(), + protected $data = [ + 'offer' => [], 'productName' => '', 'initialPrice' => 0.00, 'quantity' => 0.00 - ); + ]; /** * @var array */ - protected $settings = array(); + protected $settings = []; /** * WC_Retailcrm_Order_Item constructor. @@ -47,21 +48,21 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data $price = $this->calculate_price($item); $discount_price = $this->calculate_discount($item, $price); - $data['productName'] = $item['name']; - $data['initialPrice'] = (float)$price; - $data['quantity'] = (double)$item['qty']; + $data['productName'] = $item['name']; + $data['initialPrice'] = $price; + $data['quantity'] = (double)$item['qty']; $itemId = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id']; - $data['externalIds'] = array( - array( - 'code' =>'woocomerce', + $data['externalIds'] = [ + [ + 'code' => 'woocomerce', 'value' => $itemId . '_' . $item->get_id(), - ) - ); + ] + ]; $this->set_data_fields($data); $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; } @@ -112,9 +113,9 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data */ private function calculate_discount(WC_Order_Item_Product $item, $price) { - $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; - $price_item = $product_price + $product_tax; + $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; + $price_item = $product_price + $product_tax; $discount_price = $price - $price_item; return round($discount_price, 2); @@ -125,11 +126,11 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data */ public function reset_data() { - $this->data = array( - 'offer' => array(), + $this->data = [ + 'offer' => [], 'productName' => '', 'initialPrice' => 0.00, 'quantity' => 0.00 - ); + ]; } }