From 5bc88cc1180e103f55de7725e80a2b1b6fc86ae7 Mon Sep 17 00:00:00 2001 From: iyzoer Date: Mon, 22 May 2017 12:01:31 +0300 Subject: [PATCH] Upload updates customers in CRM (#29) --- .../controller/extension/module/retailcrm.php | 60 ++++++++++++++++++- admin/model/extension/retailcrm/customer.php | 32 ++++++++++ .../controller/extension/module/retailcrm.php | 47 ++++++++++++++- .../model/extension/retailcrm/customer.php | 22 +++++++ system/cron/.htaccess | 4 ++ 5 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 system/cron/.htaccess diff --git a/admin/controller/extension/module/retailcrm.php b/admin/controller/extension/module/retailcrm.php index 8a47e69..f36fc49 100644 --- a/admin/controller/extension/module/retailcrm.php +++ b/admin/controller/extension/module/retailcrm.php @@ -54,6 +54,27 @@ class ControllerExtensionModuleRetailcrm extends Controller 'catalog/model/account/customer/addCustomer/after', 'extension/module/retailcrm/customer_create' ); + + $this->model_extension_event + ->addEvent( + 'retailcrm', + 'catalog/model/account/customer/editCustomer/after', + 'extension/module/retailcrm/customer_edit' + ); + + $this->model_extension_event + ->addEvent( + 'retailcrm', + 'catalog/model/account/address/editAddress/after', + 'extension/module/retailcrm/customer_edit' + ); + + $this->model_extension_event + ->addEvent( + 'retailcrm', + 'admin/model/customer/customer/editCustomer/after', + 'extension/module/retailcrm/customer_edit' + ); } /** @@ -78,7 +99,6 @@ class ControllerExtensionModuleRetailcrm extends Controller */ public function index() { - $this->load->model('localisation/country'); $this->load->model('setting/setting'); $this->load->model('extension/module'); @@ -311,6 +331,44 @@ class ControllerExtensionModuleRetailcrm extends Controller } } + /** + * Update customer on event + * + * @param int $customer_id customer identificator + * + * @return void + */ + public function customer_edit($route, $customer) + { + $this->load->model('localisation/country'); + $this->load->model('localisation/zone'); + $this->load->model('customer/customer'); + + $customerId = $customer[0]; + $customer = $customer[1]; + $addresses = $customer['address']; + unset($customer); + + $customer = $this->model_customer_customer->getCustomer($customerId); + + foreach ($addresses as $address) { + $country = $this->model_localisation_country->getCountry($address['country_id']); + $zone = $this->model_localisation_zone->getZone($address['zone_id']); + + $customer['address'] = array( + 'address_1' => $address['address_1'], + 'address_2' => $address['address_2'], + 'city' => $address['city'], + 'postcode' => $address['postcode'], + 'iso_code_2' => $country['iso_code_2'], + 'zone' => $zone['name'] + ); + } + + $this->load->model('extension/retailcrm/customer'); + $this->model_extension_retailcrm_customer->changeInCrm($customer); + } + /** * Export single order * diff --git a/admin/model/extension/retailcrm/customer.php b/admin/model/extension/retailcrm/customer.php index ab17ee6..b05975f 100644 --- a/admin/model/extension/retailcrm/customer.php +++ b/admin/model/extension/retailcrm/customer.php @@ -32,6 +32,28 @@ class ModelExtensionRetailcrmCustomer extends Model { } } + public function changeInCrm($customer) { + $this->load->model('setting/setting'); + $settings = $this->model_setting_setting->getSetting('retailcrm'); + + if(empty($customer)) + return false; + if(empty($settings['retailcrm_url']) || empty($settings['retailcrm_apikey'])) + return false; + + require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; + + $this->retailcrmApi = new RetailcrmProxy( + $settings['retailcrm_url'], + $settings['retailcrm_apikey'], + DIR_SYSTEM . 'storage/logs/retailcrm.log' + ); + + $customerToCrm = $this->process($customer); + + $this->retailcrmApi->customersEdit($customerToCrm); + } + private function process($customer) { $customerToCrm = array( 'externalId' => $customer['customer_id'], @@ -46,6 +68,16 @@ class ModelExtensionRetailcrmCustomer extends Model { 'createdAt' => $customer['date_added'] ); + if (isset($customer['address'])) { + $customerToCrm['address'] = array( + 'index' => $customer['address']['postcode'], + 'countryIso' => $customer['address']['iso_code_2'], + 'region' => $customer['address']['zone'], + 'city' => $customer['address']['city'], + 'text' => $customer['address']['address_1'] . ' ' . $customer['address']['address_2'] + ); + } + return $customerToCrm; } } diff --git a/catalog/controller/extension/module/retailcrm.php b/catalog/controller/extension/module/retailcrm.php index 47777c3..7222576 100644 --- a/catalog/controller/extension/module/retailcrm.php +++ b/catalog/controller/extension/module/retailcrm.php @@ -53,6 +53,13 @@ class ControllerExtensionModuleRetailcrm extends Controller } } + /** + * Update order on event + * + * @param int $order_id order identificator + * + * @return void + */ public function order_edit($parameter1, $parameter2 = null) { $order_id = $parameter2[0]; @@ -94,12 +101,48 @@ class ControllerExtensionModuleRetailcrm extends Controller * @return void */ public function customer_create($parameter1, $parameter2 = null, $parameter3 = null) { - $customerId = $parameter3; - $this->load->model('account/customer'); + $this->load->model('localisation/country'); + $this->load->model('localisation/zone'); + + $customerId = $parameter3; $customer = $this->model_account_customer->getCustomer($customerId); + if ($this->request->post) { + $country = $this->model_localisation_country->getCountry($this->request->post['country_id']); + $zone = $this->model_localisation_zone->getZone($this->request->post['zone_id']); + + $customer['address'] = array( + 'address_1' => $this->request->post['address_1'], + 'address_2' => $this->request->post['address_2'], + 'city' => $this->request->post['city'], + 'postcode' => $this->request->post['postcode'], + 'iso_code_2' => $country['iso_code_2'], + 'zone' => $zone['name'] + ); + } + $this->load->model('extension/retailcrm/customer'); $this->model_extension_retailcrm_customer->sendToCrm($customer); } + + /** + * Update customer on event + * + * @param int $customerId customer identificator + * + * @return void + */ + public function customer_edit($parameter1, $parameter2, $parameter3) { + $customerId = $this->customer->getId(); + + $this->load->model('account/customer'); + $customer = $this->model_account_customer->getCustomer($customerId); + + $this->load->model('account/address'); + $customer['address'] = $this->model_account_address->getAddress($customer['address_id']); + + $this->load->model('extension/retailcrm/customer'); + $this->model_extension_retailcrm_customer->changeInCrm($customer); + } } diff --git a/catalog/model/extension/retailcrm/customer.php b/catalog/model/extension/retailcrm/customer.php index 1b1f768..251683b 100644 --- a/catalog/model/extension/retailcrm/customer.php +++ b/catalog/model/extension/retailcrm/customer.php @@ -23,6 +23,28 @@ class ModelExtensionRetailcrmCustomer extends Model { $this->retailcrmApi->customersCreate($customerToCrm); } + public function changeInCrm($customer) { + $this->load->model('setting/setting'); + $settings = $this->model_setting_setting->getSetting('retailcrm'); + + if(empty($customer)) + return false; + if(empty($settings['retailcrm_url']) || empty($settings['retailcrm_apikey'])) + return false; + + require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php'; + + $this->retailcrmApi = new RetailcrmProxy( + $settings['retailcrm_url'], + $settings['retailcrm_apikey'], + DIR_SYSTEM . 'storage/logs/retailcrm.log' + ); + + $customerToCrm = $this->process($customer); + + $this->retailcrmApi->customersEdit($customerToCrm); + } + private function process($customer) { $customerToCrm = array( 'externalId' => $customer['customer_id'], diff --git a/system/cron/.htaccess b/system/cron/.htaccess new file mode 100644 index 0000000..23df3ca --- /dev/null +++ b/system/cron/.htaccess @@ -0,0 +1,4 @@ + +Order Allow, Deny +Allow from all + \ No newline at end of file