diff --git a/src/config/objects.xml b/src/config/objects.xml index 4480004..466b020 100644 --- a/src/config/objects.xml +++ b/src/config/objects.xml @@ -77,6 +77,7 @@ shipmentDate shipped contact + company payment amount diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 6d4c100..4bbcbc5 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -156,7 +156,11 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $lastChange = end($history); $customers = WC_Retailcrm_History_Assembler::assemblyCustomer($history); WC_Retailcrm_Plugin::$history_run = true; +<<<<<<< HEAD >>>>>>> WIP: Change client in the order (not ready at this point; also tests should fail) +======= + WC_Retailcrm_Logger::debug(__METHOD__, 'Assembled customers history:', $customers); +>>>>>>> WIP: Logic for company replacement via component (which was surprisingly easy to implement) foreach ($customers as $crmCustomer) { if (!isset($crmCustomer['externalId'])) { @@ -240,7 +244,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : if (!empty($history)) { $last_change = end($history); $historyAssembly = WC_Retailcrm_History_Assembler::assemblyOrder($history); - + WC_Retailcrm_Logger::debug(__METHOD__, 'Assembled orders history:', $historyAssembly); WC_Retailcrm_Plugin::$history_run = true; foreach ($historyAssembly as $orderHistory) { @@ -394,6 +398,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $wc_order->set_shipping_last_name($order['lastName']); } +<<<<<<< HEAD <<<<<<< HEAD if (isset($order['phone'])) { $wc_order->set_billing_phone($order['phone']); @@ -405,6 +410,10 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : ======= >>>>>>> fixes & more fields for sync +======= + + +>>>>>>> WIP: Logic for company replacement via component (which was surprisingly easy to implement) if (array_key_exists('items', $order)) { foreach ($order['items'] as $key => $item) { diff --git a/src/include/components/class-wc-retailcrm-customer-switcher.php b/src/include/components/class-wc-retailcrm-customer-switcher.php index 3a1594c..2ee037d 100644 --- a/src/include/components/class-wc-retailcrm-customer-switcher.php +++ b/src/include/components/class-wc-retailcrm-customer-switcher.php @@ -27,7 +27,7 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface /** * In fact, this will execute customer change in provided order. - * This will not build anything. + * This will not produce any new entities. * * @return $this|\WC_Retailcrm_Builder_Interface * @throws \WC_Data_Exception @@ -38,9 +38,8 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface $wcOrder = $this->data->getWcOrder(); $newCustomer = $this->data->getNewCustomer(); - $newCorporateCustomer = $this->data->getNewCorporateCustomer(); $newContact = $this->data->getNewContact(); - $newCompany = $this->data->getNewCompany(); + $newCompany = $this->data->getNewCompanyName(); if (!empty($newCustomer)) { $this->processChangeToRegular($wcOrder, $newCustomer); @@ -52,7 +51,7 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface } if (!empty($newCompany)) { - $this->updateCompany($wcOrder, $newCorporateCustomer, $newCompany); + $this->updateCompany($wcOrder, $newCompany); } return $this; @@ -76,6 +75,21 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface if (!empty($wcCustomer)) { $wcOrder->set_customer_id($wcCustomer->get_id()); } + } else { + //TODO: + // 1. Too risky! Consider using default WooCommerce object. + // 2. Will it work as expected with such property name? Check that. + // 3. It will remove user from order directly, WC_Order logic is completely skipped here. + // It can cause these problems: + // 1) Order is changed and it's state in WC_Order is inconsistent, which can lead to problems + // and data inconsistency while saving. For example, order saving can overwrite `_customer_user` + // meta, which will revert this operation and we'll end up with a broken data (order is still + // attached to an old customer). Whichever, this last statement should be checked. + // 2) The second problem is a lifecycle in general. We're using builder interface, and code inside + // doesn't do anything which is not expected from builder. For example, besides this line, there's no + // CRUD operations. Such operation will not be expected here, so, it's better to remove it from here. + // The best solution would be to use WC_Order, and not modify it's data directly. + delete_post_meta($wcOrder->get_id(), '_customer_user'); } $fields = array( @@ -96,12 +110,13 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface * Update company in the order * * @param WC_Order $wcOrder - * @param array $corporateCustomer - * @param array $company + * @param string $company + * + * @throws \WC_Data_Exception */ - public function updateCompany($wcOrder, $corporateCustomer, $company) + public function updateCompany($wcOrder, $company) { - // TODO: Implement + $wcOrder->set_billing_company($company); } /** diff --git a/src/include/customer/woocommerce/class-wc-retailcrm-wc-customer-corporate-builder.php b/src/include/customer/woocommerce/class-wc-retailcrm-wc-customer-corporate-builder.php deleted file mode 100644 index 181742e..0000000 --- a/src/include/customer/woocommerce/class-wc-retailcrm-wc-customer-corporate-builder.php +++ /dev/null @@ -1,249 +0,0 @@ -reset(); - } - - /** - * @param array $contactPerson - * - * @return \WC_Retailcrm_WC_Customer_Corporate_Builder - */ - public function setContactPerson($contactPerson) - { - $this->contactPerson = $contactPerson; - return $this; - } - - /** - * @return array - */ - public function getContactPerson() - { - return $this->contactPerson; - } - - /** - * @param \WC_Retailcrm_Builder_Interface $customerBuilder - */ - public function setCustomerBuilder($customerBuilder) - { - $this->customerBuilder = $customerBuilder; - } - - /** - * @param string $firstName - * - * @return $this - */ - public function setFirstName($firstName) - { - $this->contactPerson['firstName'] = $firstName; - return $this; - } - - /** - * @param string $lastName - * - * @return $this - */ - public function setLastName($lastName) - { - $this->contactPerson['lastName'] = $lastName; - return $this; - } - - /** - * @param string $email - * - * @return $this - */ - public function setEmail($email) - { - $this->contactPerson['email'] = $email; - return $this; - } - - /** - * @param string $externalId - * - * @return $this - */ - public function setExternalId($externalId) - { - $this->contactPerson['externalId'] = $externalId; - return $this; - } - - /** - * @param array $phones - * - * @return $this - */ - public function setPhones($phones) - { - if (self::isPhonesArrayValid($phones)) { - $this->contactPerson['phones'] = $phones; - } - - return $this; - } - - /** - * @param array $address - * - * @return $this - */ - public function setAddress($address) - { - if (is_array($address)) { - $this->contactPerson['address'] = $address; - } - - return $this; - } - - /** - * @param \WC_Customer $customer - * - * @return $this - */ - public function setWcCustomer($customer) - { - if ($customer instanceof WC_Customer) { - $this->customer = $customer; - } - - return $this; - } - - /** - * Sets provided externalId and loads associated customer from DB (it it exists there). - * Returns true if everything went find; returns false if customer wasn't found. - * - * @param string $externalId - * - * @return bool - * @throws \Exception - */ - public function loadExternalId($externalId) - { - try { - $wcCustomer = new WC_Customer($externalId); - } catch (\Exception $exception) { - return false; - } - - $this->setExternalId($externalId); - $this->setWcCustomer($wcCustomer); - - return true; - } - - public function reset() - { - parent::reset(); - $this->customer = new WC_Customer(); - $this->contactPerson = array(); - $this->customerBuilder = new WC_Retailcrm_WC_Customer_Builder(); - - return $this; - } - - /** - * Fill WC_Customer fields with customer data from retailCRM. - * If field is not present in retailCRM customer - it will remain unchanged. - * - * @return $this|\WC_Retailcrm_Builder_Interface - * @throws \Exception - */ - public function build() - { - $this->checkBuilderValidity(); - WC_Retailcrm_Logger::debug( - __METHOD__, - 'Building WC_Customer from corporate data:', - $this->data, - "\nContact:", - $this->contactPerson - ); - - $wcCustomer = $this->customerBuilder - ->setData($this->contactPerson) - ->build() - ->getResult(); - - return $this; - } - - /** - * @return mixed|\WC_Customer|null - */ - public function getResult() - { - return $this->customer; - } - - /** - * Throws an exception if internal state is not ready for data building. - * - * @throws \RuntimeException - */ - private function checkBuilderValidity() - { - if (empty($this->data)) { - throw new \RuntimeException('Empty data'); - } - - if (!is_array($this->data)) { - throw new \RuntimeException('Data must be an array'); - } - } - - /** - * Returns true if provided variable contains array with customer phones. - * - * @param mixed $phones - * - * @return bool - */ - private static function isPhonesArrayValid($phones) - { - if (!is_array($phones)) { - return false; - } - - foreach ($phones as $phone) { - if (!is_array($phone) || count($phone) != 1 || !array_key_exists('number', $phone)) { - return false; - } - } - - return true; - } -} diff --git a/src/include/models/class-wc-retailcrm-customer-switcher-state.php b/src/include/models/class-wc-retailcrm-customer-switcher-state.php index a4bc045..ee2e67e 100644 --- a/src/include/models/class-wc-retailcrm-customer-switcher-state.php +++ b/src/include/models/class-wc-retailcrm-customer-switcher-state.php @@ -15,11 +15,8 @@ class WC_Retailcrm_Customer_Switcher_State /** @var array */ private $newContact; - /** @var array */ - private $newCorporateCustomer; - - /** @var array $newCompany */ - private $newCompany; + /** @var string $newCompanyName */ + private $newCompanyName; /** * @return \WC_Order @@ -79,32 +76,24 @@ class WC_Retailcrm_Customer_Switcher_State } /** - * @return array + * @return string */ - public function getNewCorporateCustomer() + public function getNewCompanyName() { - return $this->newCorporateCustomer; + return $this->newCompanyName; } /** - * @param array $newCorporateCustomer + * @param string $newCompanyName * * @return WC_Retailcrm_Customer_Switcher_State */ - public function setNewCorporateCustomer($newCorporateCustomer) + public function setNewCompanyName($newCompanyName) { - $this->newCorporateCustomer = $newCorporateCustomer; + $this->newCompanyName = $newCompanyName; return $this; } - /** - * @return array - */ - public function getNewCompany() - { - return $this->newCompany; - } - /** * @param array $newCompany * @@ -112,7 +101,10 @@ class WC_Retailcrm_Customer_Switcher_State */ public function setNewCompany($newCompany) { - $this->newCompany = $newCompany; + if (isset($newCompany['name'])) { + $this->setNewCompany($newCompany['name']); + } + return $this; }