From 554a3f420fd3ff6d158456857bb87e6922e351b3 Mon Sep 17 00:00:00 2001 From: Dima Uryvskiy Date: Tue, 12 Apr 2022 15:55:09 +0300 Subject: [PATCH] Fix bug with changing order status by history --- src/include/class-wc-retailcrm-history.php | 2 +- .../class-wc-retailcrm-history-assembler.php | 51 +++++++++------- tests/datasets/data-history-retailcrm.php | 60 ++++++++----------- tests/test-wc-retailcrm-history.php | 12 ++-- 4 files changed, 64 insertions(+), 61 deletions(-) diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index aaaac4a..59b7c89 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -368,7 +368,7 @@ if (!class_exists('WC_Retailcrm_History')) : return false; } - if (isset($options[$order['status']])) { + if (isset($order['status']) && isset($options[$order['status']])) { $wcOrder->update_status($options[$order['status']]); } diff --git a/src/include/components/class-wc-retailcrm-history-assembler.php b/src/include/components/class-wc-retailcrm-history-assembler.php index 6bff4b5..486a80d 100644 --- a/src/include/components/class-wc-retailcrm-history-assembler.php +++ b/src/include/components/class-wc-retailcrm-history-assembler.php @@ -44,14 +44,27 @@ class WC_Retailcrm_History_Assembler if (isset($change['order']['contragent']['contragentType']) && $change['order']['contragent']['contragentType']) { $change['order']['contragentType'] = $change['order']['contragent']['contragentType']; - + unset($change['order']['contragent']); } + $orderMainInfo = WC_Retailcrm_Plugin::clearArray( + [ + 'id' => $change['order']['id'] ?? '', + 'externalId' => $change['order']['externalId'] ?? '', + 'managerId' => $change['order']['managerId'] ?? '', + 'site' => $change['order']['site'] ?? '', + ] + ); + if (!empty($orders) && isset($orders[$change['order']['id']])) { - $orders[$change['order']['id']] = array_merge($orders[$change['order']['id']], $change['order']); + $orders[$change['order']['id']] = array_merge($orders[$change['order']['id']], $orderMainInfo); } else { - $orders[$change['order']['id']] = $change['order']; + $orders[$change['order']['id']] = !empty($change['created']) ? $change['order'] : $orderMainInfo; + } + + if ($change['field'] === 'status') { + $orders[$change['order']['id']]['status'] = $change['order']['status']; } if (isset($change['item']) && $change['item']) { @@ -74,16 +87,14 @@ class WC_Retailcrm_History_Assembler } } - if ($change['field'] == 'payments' && isset($change['payment'])) { + if (isset($change['payment']) && $change['field'] == 'payments') { if ($change['newValue'] !== null) { $orders[$change['order']['id']]['payments'][] = self::newValue($change['payment']); } } - if ($change['field'] == 'payments.status') { - if ($change['newValue'] !== null) { - $orders[$change['order']['id']]['payments']['id']['status'] = self::newValue($change['newValue']); - } + if ($change['field'] == 'payments.status' && $change['newValue'] !== null) { + $orders[$change['order']['id']]['payments']['id']['status'] = self::newValue($change['newValue']); } else { if (isset($fields['delivery'][$change['field']]) && $fields['delivery'][$change['field']] == 'service') { $orders[$change['order']['id']]['delivery']['service']['code'] = self::newValue($change['newValue']); @@ -155,9 +166,9 @@ class WC_Retailcrm_History_Assembler && $fields['customer'][$change['field']] ) { $customers[ - $change['customer']['id'] + $change['customer']['id'] ][ - $fields['customer'][$change['field']] + $fields['customer'][$change['field']] ] = self::newValue($change['newValue']); } @@ -170,11 +181,11 @@ class WC_Retailcrm_History_Assembler } $customers[ - $change['customer']['id'] + $change['customer']['id'] ][ - 'address' + 'address' ][ - $fieldsAddress['customerAddress'][$change['field']] + $fieldsAddress['customerAddress'][$change['field']] ] = self::newValue($change['newValue']); } @@ -241,9 +252,9 @@ class WC_Retailcrm_History_Assembler && $fields['customerCorporate'][$change['field']] ) { $customersCorporate[ - $change['customer']['id'] + $change['customer']['id'] ][ - $fields['customerCorporate'][$change['field']] + $fields['customerCorporate'][$change['field']] ] = self::newValue($change['newValue']); } @@ -253,17 +264,17 @@ class WC_Retailcrm_History_Assembler } $customersCorporate[ - $change['customer']['id'] + $change['customer']['id'] ][ - 'address' + 'address' ][ - $fields['customerAddress'][$change['field']] + $fields['customerAddress'][$change['field']] ] = self::newValue($change['newValue']); } if ($change['field'] == 'address') { $customersCorporate[ - $change['customer']['id'] + $change['customer']['id'] ]['address'] = array_merge($change['address'], self::newValue($change['newValue'])); } } @@ -415,4 +426,4 @@ class WC_Retailcrm_History_Assembler return $history; } -} +} \ No newline at end of file diff --git a/tests/datasets/data-history-retailcrm.php b/tests/datasets/data-history-retailcrm.php index 0977ab1..dfa9e29 100644 --- a/tests/datasets/data-history-retailcrm.php +++ b/tests/datasets/data-history-retailcrm.php @@ -460,14 +460,14 @@ class DataHistoryRetailCrm 'offer' => [ 'id' => 2, 'externalId' => $product_add_id, - 'xmlId' => 'xmlId' ] ], 'order' => [ 'id' => 2, 'externalId' => $order_id, + 'managerId' => 6, 'site' => 'test-com', - 'status' => 'status1' + 'status' => 'status4', ], 'item' => [ 'id' => 2, @@ -484,7 +484,6 @@ class DataHistoryRetailCrm 'offer' => [ 'id' => 2, 'externalId' => $product_add_id, - 'xmlId' => 'xmlId', 'name' => 'Test name 2' ], 'properties' => [], @@ -535,20 +534,17 @@ class DataHistoryRetailCrm 'user' => [ 'id' => 1 ], - 'field' => 'managerComment', - 'oldValue' => [ - 'code' => '' - ], - 'newValue' => [ - 'code' => 'managerComment' - ], + 'field' => 'manager_comment', + 'oldValue' => '', + 'newValue' => 'managerCommentTest' + , 'order' => [ 'id' => 2, 'externalId' => $order_id, 'managerId' => 6, 'site' => 'test-com', 'status' => 'status4', - 'managerComment' => 'managerComment' + 'managerComment' => 'managerCommentTest' ] ], [ @@ -558,21 +554,17 @@ class DataHistoryRetailCrm 'user' => [ 'id' => 1 ], - 'field' => 'customerComment', - 'oldValue' => [ - 'code' => '' - ], - 'newValue' => [ - 'code' => 'customerComment' - ], + 'field' => 'customer_comment', + 'oldValue' => '', + 'newValue' => 'customerCommentTest', 'order' => [ 'id' => 2, 'externalId' => $order_id, 'managerId' => 6, 'site' => 'test-com', 'status' => 'status4', - 'managerComment' => 'managerComment', - 'customerComment' => 'customerComment' + 'managerComment' => 'managerCommentTest', + 'customerComment' => 'customerCommentTest' ] ], [ @@ -595,8 +587,8 @@ class DataHistoryRetailCrm 'managerId' => 6, 'site' => 'test-com', 'status' => 'status4', - 'managerComment' => 'managerComment', - 'customerComment' => 'customerComment', + 'managerComment' => 'managerCommentTest', + 'customerComment' => 'customerCommentTest', 'phone' => '12345678' ] ], @@ -620,8 +612,8 @@ class DataHistoryRetailCrm 'managerId' => 6, 'site' => 'test-com', 'status' => 'status4', - 'managerComment' => 'managerComment', - 'customerComment' => 'customerComment', + 'managerComment' => 'managerCommentTest', + 'customerComment' => 'customerCommentTest', 'phone' => '12345678', 'email' => 'tester001@example.com' ] @@ -635,31 +627,31 @@ class DataHistoryRetailCrm ] ]; - $payment_v5 = array( + $payment_v5 = [ 'id' => 4, 'createdAt' => '2018-01-01 00:03:00', 'source' => 'user', - 'user' => array( + 'user' => [ 'id' => 1 - ), + ], 'field' => 'payments', 'oldValue' => null, - 'newValue' => array( + 'newValue' => [ 'code' => 'payment2' - ), - 'order' => array( + ], + 'order' => [ 'id' => 2, 'externalId' => $order_id, 'managerId' => 6, 'site' => 'test-com', 'status' => 'status4' - ), - 'payment' => array( + ], + 'payment' => [ 'id' => 1, 'type' => 'payment2', 'amount' => 100 - ) - ); + ] + ]; array_push($history['history'], $payment_v5); diff --git a/tests/test-wc-retailcrm-history.php b/tests/test-wc-retailcrm-history.php index d9b46fd..1d1c2d2 100644 --- a/tests/test-wc-retailcrm-history.php +++ b/tests/test-wc-retailcrm-history.php @@ -103,7 +103,9 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $wcOrder = end($orders); $options = get_option(\WC_Retailcrm_Base::$option_key); - $this->assertEquals('status1', $options[$wcOrder->get_status()]); + if ($wcOrder instanceof WC_Order) { + $this->assertEquals('status1', $options[$wcOrder->get_status()]); + } } public function test_history_order_create_deleted_items() @@ -216,7 +218,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals('status4', $options[$order_updated->get_status()]); $this->assertEquals('payment2', $options[$order_updated->get_payment_method()]); - $this->assertEquals('customerComment', $order_updated->get_customer_note()); + $this->assertEquals('customerCommentTest', $order_updated->get_customer_note()); $this->assertEquals(12345678, $order_updated->get_billing_phone()); $this->assertEquals('tester001@example.com', $order_updated->get_billing_email()); @@ -225,11 +227,9 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper foreach ($notes as $note) { if ($note->content === 'managerComment') { - $managerComment = $note->content; + $this->assertEquals('managerCommentTest', $note->content); } } - - $this->assertEquals('managerComment', $managerComment); } public function test_history_order_update_empty_order() @@ -328,7 +328,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); $retailcrm_history->getHistory(); - $orders = wc_get_orders(array('numberposts' => -1)); + $orders = wc_get_orders(['numberposts' => -1]); $order_added = end($orders); if (!$order_added) {