diff --git a/.gitignore b/.gitignore index 6a4befe..5b40963 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /nbproject/ /vendor/ +.idea/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 1564cee..6e9c272 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## 2019-04-16 3.5.1 +* Исправлен баг при активации плагина + +## 2019-03-06 3.5.0 +* Добавлена настройка деактивации выгрузки изменений заказа в retailCRM +* Добавлена настройка активации выгрузки SKU в xmlId и связь товаров по полю xmlId +* Добавлена настройка передачи номера заказа в retailCRM + ## 2019-03-06 3.4.5 * Исправлен баг с добавлением скидки при уменьшении количества товара * Перенесена инициализация формы настроек после инициализации всех плагинов diff --git a/Makefile b/Makefile index 60c1309..59a6dd0 100644 --- a/Makefile +++ b/Makefile @@ -1,22 +1,22 @@ FILE = $(TRAVIS_BUILD_DIR)/VERSION VERSION = `cat $(FILE)` -all: svn_clone prepare svn_commit remove_dir +all: svn_clone svn_push remove_dir svn_clone: mkdir /tmp/svn_plugin_dir svn co $(SVNREPOURL) /tmp/svn_plugin_dir --username $(USERNAME) --password $(PASSWORD) --no-auth-cache -prepare: /tmp/svn_plugin_dir - svn delete /tmp/svn_plugin_dir/trunk/* - rm -rf /tmp/svn_plugin_dir/trunk/* - cp -R $(TRAVIS_BUILD_DIR)/src/* /tmp/svn_plugin_dir/trunk - svn copy /tmp/svn_plugin_dir/trunk /tmp/svn_plugin_dir/tags/$(VERSION) --username $(USERNAME) --password $(PASSWORD) --no-auth-cache - -svn_commit: /tmp/svn_plugin_dir/tags - svn add /tmp/svn_plugin_dir/trunk/* --force - svn add /tmp/svn_plugin_dir/tags/$(VERSION)/* --force - svn ci /tmp/svn_plugin_dir -m $(VERSION) --username $(USERNAME) --password $(PASSWORD) --no-auth-cache +svn_push: /tmp/svn_plugin_dir + if [ ! -d "/tmp/svn_plugin_dir/tags/$(VERSION)" ]; then \ + svn delete /tmp/svn_plugin_dir/trunk/*; \ + rm -rf /tmp/svn_plugin_dir/trunk/*; \ + cp -R $(TRAVIS_BUILD_DIR)/src/* /tmp/svn_plugin_dir/trunk; \ + svn copy /tmp/svn_plugin_dir/trunk /tmp/svn_plugin_dir/tags/$(VERSION) --username $(USERNAME) --password $(PASSWORD) --no-auth-cache; \ + svn add /tmp/svn_plugin_dir/trunk/* --force; \ + svn add /tmp/svn_plugin_dir/tags/$(VERSION)/* --force; \ + svn ci /tmp/svn_plugin_dir -m $(VERSION) --username $(USERNAME) --password $(PASSWORD) --no-auth-cache; \ + fi remove_dir: rm -rf /tmp/svn_plugin_dir diff --git a/VERSION b/VERSION index 4f5e697..d5c0c99 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.4.5 +3.5.1 diff --git a/resources/pot/retailcrm-es_ES.pot b/resources/pot/retailcrm-es_ES.pot index c99cb16..d8b64b9 100644 --- a/resources/pot/retailcrm-es_ES.pot +++ b/resources/pot/retailcrm-es_ES.pot @@ -200,4 +200,22 @@ msgid "Enable this setting for activate Daemon Collector on site" msgstr "Active esta configuración para activar Daemon Collector en la página web" msgid "Site key" -msgstr "Clave de la página web" \ No newline at end of file +msgstr "Clave de la página web" + +msgid "Enable data editing in retailCRM" +msgstr "Activar edición de datos en retailCRM" + +msgid "Data updating in retailCRM" +msgstr "Actualización de datos en retailCRM" + +msgid "Activate the binding via sku (xml)" +msgstr "Activar conexión por sku (xmlId)" + +msgid "Stock synchronization and link between products" +msgstr "Sincronización de stock y conexión de productos" + +msgid "Enable transferring the number to retailCRM" +msgstr "Activar la transferencia de números en retailCRM" + +msgid "Transferring the order number" +msgstr "Transferencia de un número de pedido" \ No newline at end of file diff --git a/resources/pot/retailcrm-ru_RU.pot b/resources/pot/retailcrm-ru_RU.pot index 3690109..5da055c 100644 --- a/resources/pot/retailcrm-ru_RU.pot +++ b/resources/pot/retailcrm-ru_RU.pot @@ -209,4 +209,22 @@ msgid "Enable this setting for activate Daemon Collector on site" msgstr "Активируйте эту настройку для активации Daemon Collector на сайте" msgid "Site key" -msgstr "Ключ сайта" \ No newline at end of file +msgstr "Ключ сайта" + +msgid "Enable data editing in retailCRM" +msgstr "Активировать редактирование данных в retailCRM" + +msgid "Data updating in retailCRM" +msgstr "Обновление данных в retailCRM" + +msgid "Activate the binding via sku (xml)" +msgstr "Активировать связь по sku(xmlId)" + +msgid "Stock synchronization and link between products" +msgstr "Синхронизация остатков и связь товаров" + +msgid "Enable transferring the number to retailCRM" +msgstr "Активировать передачу номера в retailCRM" + +msgid "Transferring the order number" +msgstr "Передача номера заказа" \ No newline at end of file diff --git a/src/include/abstracts/class-wc-retailcrm-abstracts-address.php b/src/include/abstracts/class-wc-retailcrm-abstracts-address.php new file mode 100644 index 0000000..b27cdc8 --- /dev/null +++ b/src/include/abstracts/class-wc-retailcrm-abstracts-address.php @@ -0,0 +1,30 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Data +{ + protected $data = array( + 'index' => '', + 'city' => '', + 'region' => '', + 'text' => '', + ); + + public function reset_data() + { + $this->data = array( + 'index' => '', + 'city' => '', + 'region' => '', + 'text' => '', + ); + } +} diff --git a/src/include/abstracts/class-wc-retailcrm-abstracts-data.php b/src/include/abstracts/class-wc-retailcrm-abstracts-data.php new file mode 100644 index 0000000..20ad0b3 --- /dev/null +++ b/src/include/abstracts/class-wc-retailcrm-abstracts-data.php @@ -0,0 +1,63 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +/** + * Class WC_Retailcrm_Abstracts_Data + */ +abstract class WC_Retailcrm_Abstracts_Data +{ + /** @var string */ + protected $filter_name; + + /** @var array */ + protected $data = array(); + + /** + * @return void + */ + abstract public function reset_data(); + + /** + * @param $data + * + * @return self + */ + abstract public function build($data); + + protected function set_data_field($field, $value) + { + if (isset($this->data[$field]) && \gettype($value) !== \gettype($this->data[$field])) { + return false; + } + + $this->data[$field] = $value; + + return true; + } + + /** + * @param $fields + */ + protected function set_data_fields($fields) + { + foreach ($fields as $field => $value) { + $this->set_data_field($field, $value); + } + } + + /** + * @return array + */ + public function get_data() + { + return apply_filters('retailcrm_before_send_' . $this->filter_name, $this->data); + } +} diff --git a/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php b/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php new file mode 100644 index 0000000..9737e0a --- /dev/null +++ b/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php @@ -0,0 +1,705 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration +{ + /** @var string */ + const YES = 'yes'; + + /** @var string */ + const NO = 'no'; + + /** @var string */ + public static $option_key; + + /** + * WC_Retailcrm_Abstracts_Settings constructor. + */ + public function __construct() { + $this->id = 'integration-retailcrm'; + $this->method_title = __('retailCRM', 'retailcrm'); + $this->method_description = __('Integration with retailCRM management system.', 'retailcrm'); + + static::$option_key = $this->get_option_key(); + + if (isset($_GET['page']) && $_GET['page'] == 'wc-settings' + && isset($_GET['tab']) && $_GET['tab'] == 'integration' + ) { + add_action('init', array($this, 'init_settings_fields'), 99); + } + } + + public function ajax_upload() + { + $ajax_url = admin_url('admin-ajax.php'); + ?> + + + + plugin_id . $this->id . '_single_order'; + ?> + + form_fields = array( + array( 'title' => __( 'Main settings', 'retailcrm' ), 'type' => 'title', 'desc' => '', 'id' => 'general_options' ), + + 'api_url' => array( + 'title' => __( 'API of URL', 'retailcrm' ), + 'type' => 'text', + 'description' => __( 'Enter API of URL (https://yourdomain.retailcrm.pro).', 'retailcrm' ), + 'desc_tip' => true, + 'default' => '' + ), + 'api_key' => array( + 'title' => __( 'API key', 'retailcrm' ), + 'type' => 'text', + 'description' => __( 'Enter your API key. You can find it in the administration section of retailCRM', 'retailcrm' ), + 'desc_tip' => true, + 'default' => '' + ) + ); + + $api_version_list = array( + 'v4' => 'v4', + 'v5' => 'v5' + ); + + $this->form_fields[] = array( + 'title' => __( 'API settings', 'retailcrm' ), + 'type' => 'title', + 'description' => '', + 'id' => 'api_options' + ); + + $this->form_fields['api_version'] = array( + 'title' => __( 'API version', 'retailcrm' ), + 'description' => __( 'Select API version', 'retailcrm' ), + 'css' => 'min-width:50px;', + 'class' => 'select', + 'type' => 'select', + 'options' => $api_version_list, + 'desc_tip' => true, + ); + + $this->form_fields[] = array( + 'title' => __( 'Catalog settings', 'retailcrm' ), + 'type' => 'title', + 'description' => '', + 'id' => 'catalog_options' + ); + + foreach (get_post_statuses() as $status_key => $status_value) { + $this->form_fields['p_' . $status_key] = array( + 'title' => $status_value, + 'label' => ' ', + 'description' => '', + 'class' => 'checkbox', + 'type' => 'checkbox', + 'desc_tip' => true, + ); + } + + if ($this->apiClient) { + if (isset($_GET['page']) && $_GET['page'] == 'wc-settings' + && isset($_GET['tab']) && $_GET['tab'] == 'integration' + ) { + add_action('admin_print_footer_scripts', array($this, 'show_blocks'), 99); + + /** + * Order methods options + */ + $order_methods_option = array(); + $order_methods_list = $this->apiClient->orderMethodsList(); + + if ($order_methods_list->isSuccessful()) { + foreach ($order_methods_list['orderMethods'] as $order_method) { + if ($order_method['active'] == false) { + continue; + } + + $order_methods_option[$order_method['code']] = $order_method['name']; + } + + $this->form_fields[] = array( + 'title' => __('Order methods', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'order_methods_options' + ); + + $this->form_fields['order_methods'] = array( + 'label' => ' ', + 'title' => __('Order methods available for uploading from retailCRM', 'retailcrm'), + 'class' => '', + 'type' => 'multiselect', + 'description' => __('Select order methods which will be uploaded from retailCRM to the website', 'retailcrm'), + 'options' => $order_methods_option, + 'css' => 'min-height:100px;', + 'select_buttons' => true + ); + } + + /** + * Shipping options + */ + $shipping_option_list = array(); + $retailcrm_shipping_list = $this->apiClient->deliveryTypesList(); + + if ($retailcrm_shipping_list->isSuccessful()) { + foreach ($retailcrm_shipping_list['deliveryTypes'] as $retailcrm_shipping_type) { + $shipping_option_list[$retailcrm_shipping_type['code']] = $retailcrm_shipping_type['name']; + } + + $wc_shipping_list = get_wc_shipping_methods(); + + $this->form_fields[] = array( + 'title' => __('Delivery types', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'shipping_options' + ); + + foreach ($wc_shipping_list as $shipping_code => $shipping) { + if (isset($shipping['enabled']) && $shipping['enabled'] == static::YES) { + $this->form_fields[$shipping_code] = array( + 'title' => __($shipping['title'], 'woocommerce'), + 'description' => __($shipping['description'], 'woocommerce'), + 'css' => 'min-width:350px;', + 'class' => 'select', + 'type' => 'select', + 'options' => $shipping_option_list, + 'desc_tip' => true, + ); + } + } + } + + /** + * Payment options + */ + $payment_option_list = array(); + $retailcrm_payment_list = $this->apiClient->paymentTypesList(); + + if ($retailcrm_payment_list->isSuccessful()) { + foreach ($retailcrm_payment_list['paymentTypes'] as $retailcrm_payment_type) { + $payment_option_list[$retailcrm_payment_type['code']] = $retailcrm_payment_type['name']; + } + + $wc_payment = WC_Payment_Gateways::instance(); + + $this->form_fields[] = array( + 'title' => __('Payment types', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'payment_options' + ); + + foreach ($wc_payment->payment_gateways() as $payment) { + $this->form_fields[$payment->id] = array( + 'title' => __($payment->method_title, 'woocommerce'), + 'description' => __($payment->method_description, 'woocommerce'), + 'css' => 'min-width:350px;', + 'class' => 'select', + 'type' => 'select', + 'options' => $payment_option_list, + 'desc_tip' => true, + ); + } + } + + /** + * Statuses options + */ + $statuses_option_list = array(); + $retailcrm_statuses_list = $this->apiClient->statusesList(); + + if ($retailcrm_statuses_list->isSuccessful()) { + foreach ($retailcrm_statuses_list['statuses'] as $retailcrm_status) { + $statuses_option_list[$retailcrm_status['code']] = $retailcrm_status['name']; + } + + $wc_statuses = wc_get_order_statuses(); + + $this->form_fields[] = array( + 'title' => __('Statuses', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'statuses_options' + ); + + foreach ($wc_statuses as $idx => $name) { + $uid = str_replace('wc-', '', $idx); + $this->form_fields[$uid] = array( + 'title' => __($name, 'woocommerce'), + 'css' => 'min-width:350px;', + 'class' => 'select', + 'type' => 'select', + 'options' => $statuses_option_list, + 'desc_tip' => true, + ); + } + } + + /** + * Inventories options + */ + $this->form_fields[] = array( + 'title' => __('Setting of the stock balance', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'invent_options' + ); + + $this->form_fields['sync'] = array( + 'label' => __('Synchronization of the stock balance', 'retailcrm'), + 'title' => __('Stock balance', 'retailcrm'), + 'class' => 'checkbox', + 'type' => 'checkbox', + 'description' => __('Enable this setting if you would like to get information on leftover stocks from retailCRM to the website.', 'retailcrm') + ); + + /** + * UA options + */ + $this->form_fields[] = array( + 'title' => __('UA settings', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'ua_options' + ); + + $this->form_fields['ua'] = array( + 'label' => __('Activate UA', 'retailcrm'), + 'title' => __('UA', 'retailcrm'), + 'class' => 'checkbox', + 'type' => 'checkbox', + 'description' => __('Enable this setting for uploading data to UA', 'retailcrm') + ); + + $this->form_fields['ua_code'] = array( + 'title' => __('UA tracking code', 'retailcrm'), + 'class' => 'input', + 'type' => 'input' + ); + + $this->form_fields['ua_custom'] = array( + 'title' => __('User parameter', 'retailcrm'), + 'class' => 'input', + 'type' => 'input' + ); + + /** + * Daemon collector settings + */ + $this->form_fields[] = array( + 'title' => __('Daemon Collector settings', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'invent_options' + ); + + $this->form_fields['daemon_collector'] = array( + 'label' => __('Activate Daemon Collector', 'retailcrm'), + 'title' => __('Daemon Collector', 'retailcrm'), + 'class' => 'checkbox', + 'type' => 'checkbox', + 'description' => __('Enable this setting for activate Daemon Collector on site', 'retailcrm') + ); + + $this->form_fields['daemon_collector_key'] = array( + 'title' => __('Site key', 'retailcrm'), + 'class' => 'input', + 'type' => 'input' + ); + + /** + * Uploads options + */ + $options = array_filter(get_option(static::$option_key)); + + if (!isset($options['uploads'])) { + $this->form_fields[] = array( + 'title' => __('Settings of uploading', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'upload_options' + ); + + $this->form_fields['upload-button'] = array( + 'label' => __('Upload', 'retailcrm'), + 'title' => __('Uploading all customers and orders', 'retailcrm' ), + 'type' => 'button', + 'description' => __('Uploading the existing customers and orders to retailCRM', 'retailcrm' ), + 'desc_tip' => true, + 'id' => 'uploads-retailcrm' + ); + } + + /* + * Generate icml file + */ + $this->form_fields[] = array( + 'title' => __('Generating ICML catalog', 'retailcrm'), + 'type' => 'title', + 'description' => '', + 'id' => 'icml_options' + ); + + $this->form_fields[] = array( + 'label' => __('Generate now', 'retailcrm'), + 'title' => __('Generating ICML', 'retailcrm'), + 'type' => 'button', + 'description' => __('This functionality allows to generate ICML products catalog for uploading to retailCRM.', 'retailcrm'), + 'desc_tip' => true, + 'id' => 'icml-retailcrm' + ); + + $this->form_fields['icml'] = array( + 'label' => __('Generating ICML', 'retailcrm'), + 'title' => __('Generating ICML catalog by wp-cron', 'retailcrm'), + 'class' => 'checkbox', + 'type' => 'checkbox' + ); + + /* + * Upload single order + */ + $this->form_field[] = array( + 'title' => __('Upload the order by ID', 'retailcrm'), + 'type' => 'title', + 'description' => '', + 'id' => 'order_options' + ); + + $this->form_fields['single_order'] = array( + 'label' => __('Order identifier', 'retailcrm'), + 'title' => __('Orders identifiers', 'retailcrm'), + 'type' => 'input', + 'description' => __('Enter orders identifiers separated by a comma.', 'retailcrm'), + 'desc_tip' => true + ); + + $this->form_fields[] = array( + 'label' => __('Upload', 'retailcrm'), + 'title' => __('Uploading orders by identifiers.', 'retailcrm'), + 'type' => 'button', + 'description' => __('This functionality allows to upload orders to CRM differentially.', 'retailcrm'), + 'desc_tip' => true, + 'id' => 'single_order_btn' + ); + + $this->form_fields['history'] = array( + 'label' => __('Activate history uploads', 'retailcrm'), + 'title' => __('Upload data from retailCRM', 'retailcrm'), + 'class' => 'checkbox', + 'type' => 'checkbox' + ); + + $this->form_fields['deactivate_update_order'] = array( + 'label' => __('Enable data editing in retailCRM', 'retailcrm'), + 'title' => __('Data updating in retailCRM', 'retailcrm'), + 'class' => 'checkbox', + 'type' => 'checkbox' + ); + + $this->form_fields['bind_by_sku'] = array( + 'label' => __('Activate the binding via sku (xml)', 'retailcrm'), + 'title' => __('Stock synchronization and link between products', 'retailcrm'), + 'class' => 'checkbox', + 'type' => 'checkbox' + ); + + $this->form_fields['update_number'] = array( + 'label' => __('Enable transferring the number to retailCRM', 'retailcrm'), + 'title' => __('Transferring the order number', 'retailcrm'), + 'class' => 'checkbox', + 'type' => 'checkbox' + ); + } + } + } + + /** + * Generate html button + * + * @param string $key + * @param array $data + * + * @return string + */ + public function generate_button_html($key, $data) + { + $field = $this->plugin_id . $this->id . '_' . $key; + $defaults = array( + 'class' => 'button-secondary', + 'css' => '', + 'custom_attributes' => array(), + 'desc_tip' => false, + 'description' => '', + 'title' => '', + ); + + $data = wp_parse_args( $data, $defaults ); + + ob_start(); + ?> + + + + get_tooltip_html( $data ); ?> + + +
+ + + get_description_html( $data ); ?> +
+ + + get_field_key( $key ); + $defaults = array( + 'title' => '', + 'class' => '', + ); + + $data = wp_parse_args( $data, $defaults ); + + ob_start(); + ?> + +

+ +

+ + + get_post_data(); + + $versionMap = array( + 'v4' => '4.0', + 'v5' => '5.0' + ); + + $api = new WC_Retailcrm_Proxy( + $post[$this->plugin_id . $this->id . '_api_url'], + $post[$this->plugin_id . $this->id . '_api_key'] + ); + + $response = $api->apiVersions(); + + if ($response && $response->isSuccessful()) { + if (!in_array($versionMap[$value], $response['versions'])) { + WC_Admin_Settings::add_error( esc_html__( 'The selected API version is unavailable', 'retailcrm' ) ); + $value = ''; + } + } + + return $value; + } + + /** + * Validate API url + * + * @param string $key + * @param string $value + * + * @return string + */ + public function validate_api_url_field($key, $value) + { + $post = $this->get_post_data(); + $api = new WC_Retailcrm_Proxy( + $value, + $post[$this->plugin_id . $this->id . '_api_key'] + ); + + $response = $api->apiVersions(); + + if ($response == null) { + WC_Admin_Settings::add_error(esc_html__( 'Enter the correct URL of CRM', 'retailcrm')); + $value = ''; + } + + return $value; + } + + /** + * Validate API key + * + * @param string $key + * @param string $value + * + * @return string + */ + public function validate_api_key_field($key, $value) + { + $post = $this->get_post_data(); + $api = new WC_Retailcrm_Proxy( + $post[$this->plugin_id . $this->id . '_api_url'], + $value + ); + + $response = $api->apiVersions(); + + if (!is_object($response)) { + $value = ''; + } + + if (!$response->isSuccessful()) { + WC_Admin_Settings::add_error( esc_html__( 'Enter the correct API key', 'retailcrm' ) ); + $value = ''; + } + + return $value; + } + + /** + * Scritp show|hide block settings + */ + function show_blocks() + { + ?> + + add_menu( + array( + 'id' => 'retailcrm_top_menu', + 'title' => __('retailCRM', 'retailcrm') + ) + ); + $wp_admin_bar->add_menu( + array( + 'id' => 'retailcrm_ajax_generate_icml', + 'title' => __('Generating ICML catalog', 'retailcrm'), + 'href' => '#', + 'parent' => 'retailcrm_top_menu', + 'class' => 'retailcrm_ajax_generate_icml' + ) + ); + $wp_admin_bar->add_menu( + array( + 'id' => 'retailcrm_ajax_generate_setings', + 'title' => __('Settings', 'retailcrm'), + 'href'=> get_site_url().'/wp-admin/admin.php?page=wc-settings&tab=integration§ion=integration-retailcrm', + 'parent' => 'retailcrm_top_menu', + 'class' => 'retailcrm_ajax_settings' + ) + ); + } +} diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index 1c59f8a..3df57db 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -8,34 +8,34 @@ */ if (!class_exists('WC_Retailcrm_Base')) { + if (!class_exists('WC_Retailcrm_Abstracts_Settings')) { + include_once 'abstracts/class-wc-retailcrm-abstracts-settings.php'; + } /** * Class WC_Retailcrm_Base */ - class WC_Retailcrm_Base extends WC_Integration { - - public static $option_key; - + class WC_Retailcrm_Base extends WC_Retailcrm_Abstracts_Settings + { protected $api_url; protected $api_key; - - private $apiClient; + protected $apiClient; + protected $order_item; + protected $order_address; + protected $customers; + protected $orders; /** * Init and hook in the integration. * @param $retailcrm (default = false) */ public function __construct($retailcrm = false) { - //global $woocommerce; + parent::__construct(); - if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) { - include_once( __DIR__ . '/api/class-wc-retailcrm-proxy.php' ); + if (!class_exists( 'WC_Retailcrm_Proxy')) { + include_once(__DIR__ . '/api/class-wc-retailcrm-proxy.php'); } - $this->id = 'integration-retailcrm'; - $this->method_title = __('retailCRM', 'retailcrm'); - $this->method_description = __('Integration with retailCRM management system.', 'retailcrm'); - if ($retailcrm === false) { $this->apiClient = $this->getApiClient(); } else { @@ -43,7 +43,29 @@ if (!class_exists('WC_Retailcrm_Base')) { $this->init_settings_fields(); } - self::$option_key = $this->get_option_key(); + if (!class_exists('WC_Retailcrm_Orders')) { + include_once(static::checkCustomFile('orders')); + } + + if (!class_exists('WC_Retailcrm_Customers')) { + include_once(static::checkCustomFile('customers')); + } + + $this->customers = new WC_Retailcrm_Customers( + $this->apiClient, + $this->settings, + new WC_Retailcrm_Customer_Address + ); + + $this->orders = new WC_Retailcrm_Orders( + $this->apiClient, + $this->settings, + new WC_Retailcrm_Order_Item($this->settings), + new WC_Retailcrm_Order_Address, + $this->customers, + new WC_Retailcrm_Order($this->settings), + new WC_Retailcrm_Order_Payment($this->settings) + ); // Actions. add_action('woocommerce_update_options_integration_' . $this->id, array($this, 'process_admin_options')); @@ -61,15 +83,14 @@ if (!class_exists('WC_Retailcrm_Base')) { add_action('admin_print_footer_scripts', array($this, 'ajax_selected_order'), 99); add_action('woocommerce_created_customer', array($this, 'create_customer'), 10, 1); add_action('woocommerce_update_customer', array($this, 'update_customer'), 10, 1); - add_action('woocommerce_update_order', array($this, 'update_order'), 11, 1); add_action('wp_print_scripts', array($this, 'initialize_analytics'), 98); add_action('wp_print_scripts', array($this, 'initialize_daemon_collector'), 99); add_action('wp_print_footer_scripts', array($this, 'send_analytics'), 99); - if (isset($_GET['page']) && $_GET['page'] == 'wc-settings' - && isset($_GET['tab']) && $_GET['tab'] == 'integration' + if (!$this->get_option('deactivate_update_order') + || $this->get_option('deactivate_update_order') == static::NO ) { - add_action('init', array($this, 'init_settings_fields'), 99); + add_action('woocommerce_update_order', array($this, 'update_order'), 11, 1); } // Deactivate hook @@ -92,27 +113,27 @@ if (!class_exists('WC_Retailcrm_Base')) { */ public function api_sanitized($settings) { - if (isset($settings['sync']) && $settings['sync'] == 'yes') { + if (isset($settings['sync']) && $settings['sync'] == static::YES) { if (!wp_next_scheduled('retailcrm_inventories')) { wp_schedule_event(time(), 'fiveteen_minutes', 'retailcrm_inventories'); } - } elseif (isset($settings['sync']) && $settings['sync'] == 'no') { + } elseif (isset($settings['sync']) && $settings['sync'] == static::NO) { wp_clear_scheduled_hook('retailcrm_inventories'); } - if (isset($settings['history']) && $settings['history'] == 'yes') { + if (isset($settings['history']) && $settings['history'] == static::YES) { if (!wp_next_scheduled('retailcrm_history')) { wp_schedule_event(time(), 'five_minutes', 'retailcrm_history'); } - } elseif (isset($settings['history']) && $settings['history'] == 'no') { + } elseif (isset($settings['history']) && $settings['history'] == static::NO) { wp_clear_scheduled_hook('retailcrm_history'); } - if (isset($settings['icml']) && $settings['icml'] == 'yes') { + if (isset($settings['icml']) && $settings['icml'] == static::YES) { if (!wp_next_scheduled('retailcrm_icml')) { wp_schedule_event(time(), 'three_hours', 'retailcrm_icml'); } - } elseif (isset($settings['icml']) && $settings['icml'] == 'no') { + } elseif (isset($settings['icml']) && $settings['icml'] == static::NO) { wp_clear_scheduled_hook('retailcrm_icml'); } @@ -140,7 +161,7 @@ if (!class_exists('WC_Retailcrm_Base')) { public function generate_icml() { if (!class_exists('WC_Retailcrm_Icml')) { - require_once (self::checkCustomFile('icml')); + require_once (static::checkCustomFile('icml')); } $retailcrm_icml = new WC_Retailcrm_Icml(); @@ -152,7 +173,7 @@ if (!class_exists('WC_Retailcrm_Base')) { */ public function retailcrm_history_get() { if (!class_exists('WC_Retailcrm_History')) { - include_once(self::checkCustomFile('history')); + include_once(static::checkCustomFile('history')); } $retailcrm_history = new WC_Retailcrm_History($this->apiClient); @@ -163,12 +184,7 @@ if (!class_exists('WC_Retailcrm_Base')) { * @param int $order_id */ public function retailcrm_process_order($order_id) { - if (!class_exists('WC_Retailcrm_Orders')) { - include_once(self::checkCustomFile('orders')); - } - - $retailcm_order = new WC_Retailcrm_Orders($this->apiClient); - $retailcm_order->orderCreate($order_id); + $this->orders->orderCreate($order_id); } /** @@ -176,7 +192,7 @@ if (!class_exists('WC_Retailcrm_Base')) { */ public function load_stocks() { if (!class_exists('WC_Retailcrm_Inventories')) { - include_once(self::checkCustomFile('inventories')); + include_once(static::checkCustomFile('inventories')); } $inventories = new WC_Retailcrm_Inventories($this->apiClient); @@ -187,20 +203,14 @@ if (!class_exists('WC_Retailcrm_Base')) { * Upload selected orders */ public function order_upload() { - if (!class_exists('WC_Retailcrm_Orders')) { - include_once(self::checkCustomFile('orders')); - } - $ids = false; if (isset($_GET['order_ids_retailcrm'])) { $ids = explode(',', $_GET['order_ids_retailcrm']); } - $retailcm_order = new WC_Retailcrm_Orders($this->apiClient); - if ($ids) { - $retailcm_order->ordersUpload($ids, true); + $this->orders->ordersUpload($ids, true); } } @@ -209,85 +219,13 @@ if (!class_exists('WC_Retailcrm_Base')) { */ public function upload_to_crm() { - if (!class_exists('WC_Retailcrm_Orders')) { - include_once(self::checkCustomFile('orders')); - } + $options = array_filter(get_option(static::$option_key)); - if (!class_exists('WC_Retailcrm_Customers')) { - include_once(self::checkCustomFile('customers')); - } + $this->customers->customersUpload(); + $this->orders->ordersUpload(); - $options = array_filter(get_option(self::$option_key)); - - $retailcrm_customers = new WC_Retailcrm_Customers($this->apiClient); - $retailcrm_orders = new WC_Retailcrm_Orders($this->apiClient); - - $retailcrm_customers->customersUpload(); - $retailcrm_orders->ordersUpload(); - - $options['uploads'] = 'yes'; - update_option(self::$option_key, $options); - } - - public function ajax_upload() - { - $ajax_url = admin_url('admin-ajax.php'); - ?> - - - - plugin_id . $this->id . '_single_order'; - ?> - - apiClient); - $retailcrm_customer->createCustomer($customer_id); + $this->customers->createCustomer($customer_id); } /** @@ -318,12 +251,7 @@ if (!class_exists('WC_Retailcrm_Base')) { return; } - if (!class_exists('WC_Retailcrm_Customers')) { - include_once(self::checkCustomFile('customers')); - } - - $retailcrm_customer = new WC_Retailcrm_Customers($this->apiClient); - $retailcrm_customer->updateCustomer($customer_id); + $this->customers->updateCustomer($customer_id); } /** @@ -336,12 +264,7 @@ if (!class_exists('WC_Retailcrm_Base')) { return; } - if (!class_exists('WC_Retailcrm_Orders')) { - include_once(self::checkCustomFile('orders')); - } - - $retailcrm_order = new WC_Retailcrm_Orders($this->apiClient); - $retailcrm_order->updateOrder($order_id); + $this->orders->updateOrder($order_id); } /** @@ -350,7 +273,7 @@ if (!class_exists('WC_Retailcrm_Base')) { public function initialize_analytics() { if (!class_exists('WC_Retailcrm_Google_Analytics')) { - include_once(self::checkCustomFile('ga')); + include_once(static::checkCustomFile('ga')); } if ($this->get_option('ua') && $this->get_option('ua_code')) { @@ -367,10 +290,10 @@ if (!class_exists('WC_Retailcrm_Base')) { public function send_analytics() { if (!class_exists('WC_Retailcrm_Google_Analytics')) { - include_once(self::checkCustomFile('ga')); + include_once(static::checkCustomFile('ga')); } - if ($this->get_option('ua') == 'yes' && $this->get_option('ua_code') && is_checkout()) { + if ($this->get_option('ua') == static::YES && $this->get_option('ua_code') && is_checkout()) { $retailcrm_analytics = WC_Retailcrm_Google_Analytics::getInstance($this->settings); echo $retailcrm_analytics->send_analytics(); } else { @@ -384,10 +307,10 @@ if (!class_exists('WC_Retailcrm_Base')) { public function initialize_daemon_collector() { if (!class_exists('WC_Retailcrm_Daemon_Collector')) { - include_once(self::checkCustomFile('daemon-collector')); + include_once(static::checkCustomFile('daemon-collector')); } - if ($this->get_option('daemon_collector') == 'yes' && $this->get_option('daemon_collector_key')) { + if ($this->get_option('daemon_collector') == static::YES && $this->get_option('daemon_collector_key')) { $retailcrm_daemon_collector = WC_Retailcrm_Daemon_Collector::getInstance($this->settings); echo $retailcrm_daemon_collector->initialize_daemon_collector(); } else { @@ -395,556 +318,6 @@ if (!class_exists('WC_Retailcrm_Base')) { } } - /** - * Initialize integration settings form fields. - */ - public function init_form_fields() - { - $this->form_fields = array( - array( 'title' => __( 'Main settings', 'retailcrm' ), 'type' => 'title', 'desc' => '', 'id' => 'general_options' ), - - 'api_url' => array( - 'title' => __( 'API of URL', 'retailcrm' ), - 'type' => 'text', - 'description' => __( 'Enter API of URL (https://yourdomain.retailcrm.pro).', 'retailcrm' ), - 'desc_tip' => true, - 'default' => '' - ), - 'api_key' => array( - 'title' => __( 'API key', 'retailcrm' ), - 'type' => 'text', - 'description' => __( 'Enter your API key. You can find it in the administration section of retailCRM', 'retailcrm' ), - 'desc_tip' => true, - 'default' => '' - ) - ); - - $api_version_list = array( - 'v4' => 'v4', - 'v5' => 'v5' - ); - - $this->form_fields[] = array( - 'title' => __( 'API settings', 'retailcrm' ), - 'type' => 'title', - 'description' => '', - 'id' => 'api_options' - ); - - $this->form_fields['api_version'] = array( - 'title' => __( 'API version', 'retailcrm' ), - 'description' => __( 'Select API version', 'retailcrm' ), - 'css' => 'min-width:50px;', - 'class' => 'select', - 'type' => 'select', - 'options' => $api_version_list, - 'desc_tip' => true, - ); - - $this->form_fields[] = array( - 'title' => __( 'Catalog settings', 'retailcrm' ), - 'type' => 'title', - 'description' => '', - 'id' => 'catalog_options' - ); - - foreach (get_post_statuses() as $status_key => $status_value) { - $this->form_fields['p_' . $status_key] = array( - 'title' => $status_value, - 'label' => ' ', - 'description' => '', - 'class' => 'checkbox', - 'type' => 'checkbox', - 'desc_tip' => true, - ); - } - - if ($this->apiClient) { - if (isset($_GET['page']) && $_GET['page'] == 'wc-settings' - && isset($_GET['tab']) && $_GET['tab'] == 'integration' - ) { - add_action('admin_print_footer_scripts', array($this, 'show_blocks'), 99); - - /** - * Order methods options - */ - $order_methods_option = array(); - $order_methods_list = $this->apiClient->orderMethodsList(); - - if ($order_methods_list->isSuccessful()) { - foreach ($order_methods_list['orderMethods'] as $order_method) { - if ($order_method['active'] == false) { - continue; - } - - $order_methods_option[$order_method['code']] = $order_method['name']; - } - - $this->form_fields[] = array( - 'title' => __('Order methods', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'order_methods_options' - ); - - $this->form_fields['order_methods'] = array( - 'label' => ' ', - 'title' => __('Order methods available for uploading from retailCRM', 'retailcrm'), - 'class' => '', - 'type' => 'multiselect', - 'description' => __('Select order methods which will be uploaded from retailCRM to the website', 'retailcrm'), - 'options' => $order_methods_option, - 'css' => 'min-height:100px;', - 'select_buttons' => true - ); - } - - /** - * Shipping options - */ - $shipping_option_list = array(); - $retailcrm_shipping_list = $this->apiClient->deliveryTypesList(); - - if ($retailcrm_shipping_list->isSuccessful()) { - foreach ($retailcrm_shipping_list['deliveryTypes'] as $retailcrm_shipping_type) { - $shipping_option_list[$retailcrm_shipping_type['code']] = $retailcrm_shipping_type['name']; - } - - $wc_shipping_list = get_wc_shipping_methods(); - - $this->form_fields[] = array( - 'title' => __('Delivery types', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'shipping_options' - ); - - foreach ($wc_shipping_list as $shipping_code => $shipping) { - if (isset($shipping['enabled']) && $shipping['enabled'] == 'yes') { - $this->form_fields[$shipping_code] = array( - 'title' => __($shipping['title'], 'woocommerce'), - 'description' => __($shipping['description'], 'woocommerce'), - 'css' => 'min-width:350px;', - 'class' => 'select', - 'type' => 'select', - 'options' => $shipping_option_list, - 'desc_tip' => true, - ); - } - } - } - - /** - * Payment options - */ - $payment_option_list = array(); - $retailcrm_payment_list = $this->apiClient->paymentTypesList(); - - if ($retailcrm_payment_list->isSuccessful()) { - foreach ($retailcrm_payment_list['paymentTypes'] as $retailcrm_payment_type) { - $payment_option_list[$retailcrm_payment_type['code']] = $retailcrm_payment_type['name']; - } - - $wc_payment = WC_Payment_Gateways::instance(); - - $this->form_fields[] = array( - 'title' => __('Payment types', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'payment_options' - ); - - foreach ($wc_payment->payment_gateways() as $payment) { - $this->form_fields[$payment->id] = array( - 'title' => __($payment->method_title, 'woocommerce'), - 'description' => __($payment->method_description, 'woocommerce'), - 'css' => 'min-width:350px;', - 'class' => 'select', - 'type' => 'select', - 'options' => $payment_option_list, - 'desc_tip' => true, - ); - } - } - - /** - * Statuses options - */ - $statuses_option_list = array(); - $retailcrm_statuses_list = $this->apiClient->statusesList(); - - if ($retailcrm_statuses_list->isSuccessful()) { - foreach ($retailcrm_statuses_list['statuses'] as $retailcrm_status) { - $statuses_option_list[$retailcrm_status['code']] = $retailcrm_status['name']; - } - - $wc_statuses = wc_get_order_statuses(); - - $this->form_fields[] = array( - 'title' => __('Statuses', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'statuses_options' - ); - - foreach ($wc_statuses as $idx => $name) { - $uid = str_replace('wc-', '', $idx); - $this->form_fields[$uid] = array( - 'title' => __($name, 'woocommerce'), - 'css' => 'min-width:350px;', - 'class' => 'select', - 'type' => 'select', - 'options' => $statuses_option_list, - 'desc_tip' => true, - ); - } - } - - /** - * Inventories options - */ - $this->form_fields[] = array( - 'title' => __('Setting of the stock balance', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'invent_options' - ); - - $this->form_fields['sync'] = array( - 'label' => __('Synchronization of the stock balance', 'retailcrm'), - 'title' => __('Stock balance', 'retailcrm'), - 'class' => 'checkbox', - 'type' => 'checkbox', - 'description' => __('Enable this setting if you would like to get information on leftover stocks from retailCRM to the website.', 'retailcrm') - ); - - /** - * UA options - */ - $this->form_fields[] = array( - 'title' => __('UA settings', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'ua_options' - ); - - $this->form_fields['ua'] = array( - 'label' => __('Activate UA', 'retailcrm'), - 'title' => __('UA', 'retailcrm'), - 'class' => 'checkbox', - 'type' => 'checkbox', - 'description' => __('Enable this setting for uploading data to UA', 'retailcrm') - ); - - $this->form_fields['ua_code'] = array( - 'title' => __('UA tracking code', 'retailcrm'), - 'class' => 'input', - 'type' => 'input' - ); - - $this->form_fields['ua_custom'] = array( - 'title' => __('User parameter', 'retailcrm'), - 'class' => 'input', - 'type' => 'input' - ); - - /** - * Daemon collector settings - */ - $this->form_fields[] = array( - 'title' => __('Daemon Collector settings', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'invent_options' - ); - - $this->form_fields['daemon_collector'] = array( - 'label' => __('Activate Daemon Collector', 'retailcrm'), - 'title' => __('Daemon Collector', 'retailcrm'), - 'class' => 'checkbox', - 'type' => 'checkbox', - 'description' => __('Enable this setting for activate Daemon Collector on site', 'retailcrm') - ); - - $this->form_fields['daemon_collector_key'] = array( - 'title' => __('Site key', 'retailcrm'), - 'class' => 'input', - 'type' => 'input' - ); - - /** - * Uploads options - */ - $options = array_filter(get_option(self::$option_key)); - - if (!isset($options['uploads'])) { - $this->form_fields[] = array( - 'title' => __('Settings of uploading', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'upload_options' - ); - - $this->form_fields['upload-button'] = array( - 'label' => __('Upload', 'retailcrm'), - 'title' => __('Uploading all customers and orders', 'retailcrm' ), - 'type' => 'button', - 'description' => __('Uploading the existing customers and orders to retailCRM', 'retailcrm' ), - 'desc_tip' => true, - 'id' => 'uploads-retailcrm' - ); - } - - /* - * Generate icml file - */ - $this->form_fields[] = array( - 'title' => __('Generating ICML catalog', 'retailcrm'), - 'type' => 'title', - 'description' => '', - 'id' => 'icml_options' - ); - - $this->form_fields[] = array( - 'label' => __('Generate now', 'retailcrm'), - 'title' => __('Generating ICML', 'retailcrm'), - 'type' => 'button', - 'description' => __('This functionality allows to generate ICML products catalog for uploading to retailCRM.', 'retailcrm'), - 'desc_tip' => true, - 'id' => 'icml-retailcrm' - ); - - $this->form_fields['icml'] = array( - 'label' => __('Generating ICML', 'retailcrm'), - 'title' => __('Generating ICML catalog by wp-cron', 'retailcrm'), - 'class' => 'checkbox', - 'type' => 'checkbox' - ); - - /* - * Upload single order - */ - $this->form_field[] = array( - 'title' => __('Upload the order by ID', 'retailcrm'), - 'type' => 'title', - 'description' => '', - 'id' => 'order_options' - ); - - $this->form_fields['single_order'] = array( - 'label' => __('Order identifier', 'retailcrm'), - 'title' => __('Orders identifiers', 'retailcrm'), - 'type' => 'input', - 'description' => __('Enter orders identifiers separated by a comma.', 'retailcrm'), - 'desc_tip' => true - ); - - $this->form_fields[] = array( - 'label' => __('Upload', 'retailcrm'), - 'title' => __('Uploading orders by identifiers.', 'retailcrm'), - 'type' => 'button', - 'description' => __('This functionality allows to upload orders to CRM differentially.', 'retailcrm'), - 'desc_tip' => true, - 'id' => 'single_order_btn' - ); - - $this->form_fields['history'] = array( - 'label' => __('Activate history uploads', 'retailcrm'), - 'title' => __('Upload data from retailCRM', 'retailcrm'), - 'class' => 'checkbox', - 'type' => 'checkbox' - ); - } - } - } - - /** - * Generate html button - * - * @param string $key - * @param array $data - * - * @return string - */ - public function generate_button_html($key, $data) - { - $field = $this->plugin_id . $this->id . '_' . $key; - $defaults = array( - 'class' => 'button-secondary', - 'css' => '', - 'custom_attributes' => array(), - 'desc_tip' => false, - 'description' => '', - 'title' => '', - ); - - $data = wp_parse_args( $data, $defaults ); - - ob_start(); - ?> - - - - - get_field_key( $key ); - $defaults = array( - 'title' => '', - 'class' => '', - ); - - $data = wp_parse_args( $data, $defaults ); - - ob_start(); - ?> - -

- -

- - - get_post_data(); - - $versionMap = array( - 'v4' => '4.0', - 'v5' => '5.0' - ); - - $api = new WC_Retailcrm_Proxy( - $post[$this->plugin_id . $this->id . '_api_url'], - $post[$this->plugin_id . $this->id . '_api_key'] - ); - - $response = $api->apiVersions(); - - if ($response && $response->isSuccessful()) { - if (!in_array($versionMap[$value], $response['versions'])) { - WC_Admin_Settings::add_error( esc_html__( 'The selected API version is unavailable', 'retailcrm' ) ); - $value = ''; - } - } - - return $value; - } - - /** - * Validate API url - * - * @param string $key - * @param string $value - * - * @return string - */ - public function validate_api_url_field($key, $value) - { - $post = $this->get_post_data(); - $api = new WC_Retailcrm_Proxy( - $value, - $post[$this->plugin_id . $this->id . '_api_key'] - ); - - $response = $api->apiVersions(); - - if ($response == null) { - WC_Admin_Settings::add_error(esc_html__( 'Enter the correct URL of CRM', 'retailcrm')); - $value = ''; - } - - return $value; - } - - /** - * Validate API key - * - * @param string $key - * @param string $value - * - * @return string - */ - public function validate_api_key_field($key, $value) - { - $post = $this->get_post_data(); - $api = new WC_Retailcrm_Proxy( - $post[$this->plugin_id . $this->id . '_api_url'], - $value - ); - - $response = $api->apiVersions(); - - if (!is_object($response)) { - $value = ''; - } - - if (!$response->isSuccessful()) { - WC_Admin_Settings::add_error( esc_html__( 'Enter the correct API key', 'retailcrm' ) ); - $value = ''; - } - - return $value; - } - - /** - * Scritp show|hide block settings - */ - function show_blocks() - { - ?> - - add_menu( - array( - 'id' => 'retailcrm_top_menu', - 'title' => __('retailCRM', 'retailcrm') - ) - ); - $wp_admin_bar->add_menu( - array( - 'id' => 'retailcrm_ajax_generate_icml', - 'title' => __('Generating ICML catalog', 'retailcrm'), - 'href' => '#', - 'parent' => 'retailcrm_top_menu', - 'class' => 'retailcrm_ajax_generate_icml' - ) - ); - - $wp_admin_bar->add_menu( - array( - 'id' => 'retailcrm_ajax_generate_setings', - 'title' => __('Settings', 'retailcrm'), - 'href'=> get_site_url().'/wp-admin/admin.php?page=wc-settings&tab=integration§ion=integration-retailcrm', - 'parent' => 'retailcrm_top_menu', - 'class' => 'retailcrm_ajax_settings' - ) - ); - } - /** * Deactivate module in marketplace retailCRM * diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index 0cbbc9b..f1adb1c 100644 --- a/src/include/class-wc-retailcrm-customers.php +++ b/src/include/class-wc-retailcrm-customers.php @@ -16,20 +16,30 @@ if (!class_exists('WC_Retailcrm_Customers')) : const CUSTOMER_ROLE = 'customer'; + /** @var bool | WC_Retailcrm_Proxy */ protected $retailcrm; - protected $retailcrm_settings; + /** @var array */ + protected $retailcrm_settings = array(); + + /** @var WC_Retailcrm_Customer_Address */ + protected $customer_address; + + /** @var array */ private $customer = array(); /** * WC_Retailcrm_Customers constructor. * - * @param $retailcrm + * @param bool | WC_Retailcrm_Proxy $retailcrm + * @param array $retailcrm_settings + * @param WC_Retailcrm_Customer_Address $customer_address */ - public function __construct($retailcrm = false) + public function __construct($retailcrm = false, $retailcrm_settings, $customer_address) { - $this->retailcrm_settings = get_option(WC_Retailcrm_Base::$option_key); $this->retailcrm = $retailcrm; + $this->retailcrm_settings = $retailcrm_settings; + $this->customer_address = $customer_address; } /** @@ -139,13 +149,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : 'firstName' => $firstName ? $firstName : $customer->get_username(), 'lastName' => $customer->get_last_name(), 'email' => $customer->get_email(), - 'address' => array( - 'index' => $customer->get_billing_postcode(), - 'countryIso' => $customer->get_billing_country(), - 'region' => $customer->get_billing_state(), - 'city' => $customer->get_billing_city(), - 'text' => $customer->get_billing_address_1() . ',' . $customer->get_billing_address_2() - ) + 'address' => $this->customer_address->build($customer)->get_data() ); if ($customer->get_id() > 0) { diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 21649be..6699d82 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -20,6 +20,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : protected $retailcrm_settings; protected $retailcrm; protected $order_methods = array(); + protected $bind_field = 'externalId'; /** * WC_Retailcrm_History constructor. @@ -29,6 +30,12 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : { $this->retailcrm_settings = get_option(WC_Retailcrm_Base::$option_key); + if (isset($this->retailcrm_settings['bind_by_sku']) + && $this->retailcrm_settings['bind_by_sku'] == WC_Retailcrm_Base::YES + ) { + $this->bind_field = 'xmlId'; + } + if (isset($this->retailcrm_settings['order_methods'])) { $this->order_methods = $this->retailcrm_settings['order_methods']; unset($this->retailcrm_settings['order_methods']); @@ -322,12 +329,16 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : if (array_key_exists('items', $order)) { foreach ($order['items'] as $item) { - if (!isset($item['offer']['externalId'])) { + if (!isset($item['offer'][$this->bind_field])) { continue; } if (isset($item['create']) && $item['create'] == true) { - $product = wc_get_product($item['offer']['externalId']); + $product = retailcrm_get_wc_product( + $item['offer'][$this->bind_field], + $this->retailcrm_settings + ); + $wc_order->add_product($product, $item['quantity']); } else { foreach ($wc_order->get_items() as $order_item_id => $order_item) { @@ -337,7 +348,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $offer_id = $order_item['product_id']; } - if ($offer_id == $item['offer']['externalId']) { + if ($offer_id == $item['offer'][$this->bind_field]) { $this->deleteOrUpdateOrderItem($item, $order_item, $order_item_id); } } @@ -428,7 +439,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : } else { if (isset($item['quantity']) && $item['quantity']) { $order_item->set_quantity($item['quantity']); - $product = wc_get_product($item['offer']['externalId']); + $product = retailcrm_get_wc_product($item['offer'][$this->bind_field], $this->retailcrm_settings); $order_item->set_total($product->get_price() * $item['quantity']); $order_item->set_subtotal($product->get_price()); $data_store = $order_item->get_data_store(); @@ -526,7 +537,10 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : if ($product_data) { foreach ($product_data as $product) { - $wc_order->add_product(wc_get_product($product['offer']['externalId']), $product['quantity']); + $wc_order->add_product( + retailcrm_get_wc_product($product['offer'][$this->bind_field], $this->retailcrm_settings), + $product['quantity'] + ); } } diff --git a/src/include/class-wc-retailcrm-icml.php b/src/include/class-wc-retailcrm-icml.php index e30fa9f..f06e6b0 100644 --- a/src/include/class-wc-retailcrm-icml.php +++ b/src/include/class-wc-retailcrm-icml.php @@ -41,12 +41,16 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) : protected $chunk = 500; protected $fileLifeTime = 3600; + /** @var array */ + protected $settings; + /** * WC_Retailcrm_Icml constructor. * */ public function __construct() { + $this->settings = get_option(WC_Retailcrm_Base::$option_key); $this->shop = get_bloginfo( 'name' ); $this->file = ABSPATH . 'retailcrm.xml'; $this->tmpFile = sprintf('%s.tmp', $this->file); @@ -355,7 +359,6 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) : ); foreach ($products as $offer) { - if ($offer->get_type() == 'simple') { $this->setOffer($full_product_list, $product_attributes, $offer); } elseif ($offer->get_type() == 'variable') { @@ -424,8 +427,8 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) : * * @param array $full_product_list * @param array $product_attributes - * @param object WC_Product_Simple | WC_Product_Variation $product - * @param mixed WC_Product_Variable | bool $parent default false + * @param WC_Product $product + * @param bool | WC_Product_Variable $parent * * @return void */ @@ -462,10 +465,6 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) : } } - if ($product->get_sku() != '') { - $params[] = array('code' => 'article', 'name' => 'Артикул', 'value' => $product->get_sku()); - } - $dimension = ''; if ($product->get_length() != '') { @@ -506,6 +505,14 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) : 'tax' => isset($tax) ? $tax['rate'] : 'none' ); + if ($product->get_sku() != '') { + $params[] = array('code' => 'article', 'name' => 'Артикул', 'value' => $product->get_sku()); + + if (isset($this->settings['bind_by_sku']) && $this->settings['bind_by_sku'] == WC_Retailcrm_Base::YES) { + $product_data['xmlId'] = $product->get_sku(); + } + } + if (!empty($params)) { $product_data['params'] = $params; } @@ -523,11 +530,10 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) : * @return array */ private function checkPostStatuses() { - $options = get_option(WC_Retailcrm_Base::$option_key); $status_args = array(); foreach (get_post_statuses() as $key => $value) { - if (isset($options['p_' . $key]) && $options['p_' . $key] == 'yes') { + if (isset($this->settings['p_' . $key]) && $this->settings['p_' . $key] == WC_Retailcrm_Base::YES) { $status_args[] = $key; } } diff --git a/src/include/class-wc-retailcrm-inventories.php b/src/include/class-wc-retailcrm-inventories.php index b69a3a3..a2e5406 100644 --- a/src/include/class-wc-retailcrm-inventories.php +++ b/src/include/class-wc-retailcrm-inventories.php @@ -14,9 +14,15 @@ if (!class_exists('WC_Retailcrm_Inventories')) : */ class WC_Retailcrm_Inventories { + /** @var WC_Retailcrm_Client_V5 */ protected $retailcrm; + + /** @var array */ protected $retailcrm_settings; + /** @var string */ + protected $bind_field = 'externalId'; + /** * WC_Retailcrm_Inventories constructor. * @param bool $retailcrm @@ -25,6 +31,12 @@ if (!class_exists('WC_Retailcrm_Inventories')) : { $this->retailcrm_settings = get_option(WC_Retailcrm_Base::$option_key); $this->retailcrm = $retailcrm; + + if (isset($this->retailcrm_settings['bind_by_sky']) + && $this->retailcrm_settings['bind_by_sky'] == WC_Retailcrm_Base::YES + ) { + $this->bind_field = 'xmlId'; + } } /** @@ -43,6 +55,7 @@ if (!class_exists('WC_Retailcrm_Inventories')) : $page = 1; do { + /** @var WC_Retailcrm_Response $result */ $result = $this->retailcrm->storeInventories(array(), $page, 250); if (!$result->isSuccessful()) { @@ -53,8 +66,8 @@ if (!class_exists('WC_Retailcrm_Inventories')) : $page++; foreach ($result['offers'] as $offer) { - if (isset($offer['externalId'])) { - $product = wc_get_product($offer['externalId']); + if (isset($offer[$this->bind_field])) { + $product = retailcrm_get_wc_product($offer[$this->bind_field], $this->retailcrm_settings); if ($product instanceof WC_Product) { if ($product->get_type() == 'variable') { @@ -79,7 +92,7 @@ if (!class_exists('WC_Retailcrm_Inventories')) : */ public function updateQuantity() { - if ($this->retailcrm_settings['sync'] == 'yes') { + if ($this->retailcrm_settings['sync'] == WC_Retailcrm_Base::YES) { return $this->load_stocks(); } diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 3dbea98..44f5daf 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -14,16 +14,49 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : */ class WC_Retailcrm_Orders { - protected $retailcrm_settings; + /** @var bool|WC_Retailcrm_Proxy */ protected $retailcrm; + /** @var array */ + protected $retailcrm_settings; + + /** @var WC_Retailcrm_Order_Item */ + protected $order_item; + + /** @var WC_Retailcrm_Order_Address */ + protected $order_address; + + /** @var WC_Retailcrm_Order_Payment */ + protected $order_payment; + + /** @var WC_Retailcrm_Customers */ + protected $customers; + + /** @var WC_Retailcrm_Order */ + protected $orders; + + /** @var array */ private $order = array(); + + /** @var array */ private $payment = array(); - public function __construct($retailcrm = false) - { - $this->retailcrm_settings = get_option(WC_Retailcrm_Base::$option_key); + public function __construct( + $retailcrm = false, + $retailcrm_settings, + $order_item, + $order_address, + $customers, + $orders, + $order_payment + ) { $this->retailcrm = $retailcrm; + $this->retailcrm_settings = $retailcrm_settings; + $this->order_item = $order_item; + $this->order_address = $order_address; + $this->customers = $customers; + $this->orders = $orders; + $this->order_payment = $order_payment; } /** @@ -66,12 +99,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : } if ($withCustomers === true && !empty($customers)) { - if (!class_exists('WC_Retailcrm_Customers')) { - include_once(WC_Retailcrm_Base::checkCustomFile('customers')); - } - - $retailcrmCustomer = new WC_Retailcrm_Customers($this->retailcrm); - $retailcrmCustomer->customersUpload($customers); + $this->customers->customersUpload($customers); } $uploadOrders = array_chunk($orders_data, 50); @@ -101,26 +129,20 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $this->processOrder($order); $customer = $order->get_user(); - if (!class_exists('WC_Retailcrm_Customers')) { - include_once(WC_Retailcrm_Base::checkCustomFile('customers')); - } - - $retailcrm_customers = new WC_Retailcrm_Customers($this->retailcrm); - if ($customer != false) { - $search = $retailcrm_customers->searchCustomer(array('id' => $customer->get('ID'))); + $search = $this->customers->searchCustomer(array('id' => $customer->get('ID'))); if (!$search) { - $retailcrm_customers->createCustomer($customer); + $this->customers->createCustomer($customer); } else { $this->order['customer']['externalId'] = $search['externalId']; } } else { - $search = $retailcrm_customers->searchCustomer(array('email' => $order->get_billing_email())); + $search = $this->customers->searchCustomer(array('email' => $order->get_billing_email())); if (!$search) { - $new_customer = $retailcrm_customers->buildCustomerFromOrderData($order); - $id = $retailcrm_customers->createCustomer($new_customer); + $new_customer = $this->customers->buildCustomerFromOrderData($order); + $id = $this->customers->createCustomer($new_customer); if ($id !== null) { $this->order['customer']['id'] = $id; @@ -212,28 +234,6 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : return null; } - /** - * Get order data - * - * @param WC_Order $order - * - * @return array $order_data_arr - */ - protected function getOrderData($order) - { - $order_data_arr = array(); - $order_info = $order->get_data(); - - $order_data_arr['id'] = $order_info['id']; - $order_data_arr['payment_method'] = $order->get_payment_method(); - $order_data_arr['date'] = $order_info['date_created']->date('Y-m-d H:i:s'); - $order_data_arr['discount_total'] = $order_info['discount_total']; - $order_data_arr['discount_tax'] = $order_info['discount_tax']; - $order_data_arr['customer_comment'] = $order->get_customer_note(); - - return $order_data_arr; - } - /** * process to combine order data * @@ -248,21 +248,12 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : return; } - $order_data_info = $this->getOrderData($order); - $order_data = array(); - - $order_data['externalId'] = $order_data_info['id']; - $order_data['number'] = $order->get_order_number(); - $order_data['createdAt'] = trim($order_data_info['date']); - $order_data['customerComment'] = $order_data_info['customer_comment']; - - if (!empty($order_data_info['payment_method']) - && !empty($this->retailcrm_settings[$order_data_info['payment_method']]) - && $this->retailcrm_settings['api_version'] != 'v5' - ) { - $order_data['paymentType'] = $this->retailcrm_settings[$order_data_info['payment_method']]; + if ($update === true) { + $this->orders->is_new = false; } + $order_data = $this->orders->build($order)->get_data(); + if ($order->get_items('shipping')) { $shippings = $order->get_items( 'shipping' ); $shipping = reset($shippings); @@ -300,95 +291,20 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : } } - if ($this->retailcrm_settings['api_version'] != 'v5' && $order->is_paid()) { - $order_data['paymentStatus'] = 'paid'; - } - - $status = $order->get_status(); - $order_data['status'] = $this->retailcrm_settings[$status]; - - $user_data_billing = $order->get_address('billing'); - - if (!empty($user_data_billing)) { - $order_data['phone'] = $user_data_billing['phone']; - $order_data['email'] = $user_data_billing['email']; - } - - $user_data_shipping = $order->get_address('shipping'); - - if (!empty($user_data_shipping)) { - $order_data['firstName'] = $user_data_shipping['first_name']; - $order_data['lastName'] = $user_data_shipping['last_name']; - $order_data['delivery']['address']['index'] = $user_data_shipping['postcode']; - $order_data['delivery']['address']['city'] = $user_data_shipping['city']; - $order_data['delivery']['address']['region'] = $user_data_shipping['state']; - $order_data['countryIso'] = $user_data_shipping['country']; - } - - $order_data['delivery']['address']['text'] = sprintf( - "%s %s %s %s %s", - $user_data_shipping['postcode'], - $user_data_shipping['state'], - $user_data_shipping['city'], - $user_data_shipping['address_1'], - $user_data_shipping['address_2'] - ); - + $order_data['delivery']['address'] = $this->order_address->build($order)->get_data(); $order_items = array(); + /** @var WC_Order_Item_Product $item */ foreach ($order->get_items() as $item) { - $uid = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id'] ; - $price = round(($item['line_subtotal'] / $item->get_quantity()) + ($item['line_subtotal_tax'] / $item->get_quantity()), 2); - - $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; - - $order_item = array( - 'offer' => array('externalId' => $uid), - 'productName' => $item['name'], - 'initialPrice' => (float)$price, - 'quantity' => $item['qty'], - ); - - if ($this->retailcrm_settings['api_version'] == 'v5' && round($discount_price, 2)) { - $order_item['discountManualAmount'] = round($discount_price, 2); - } elseif ($this->retailcrm_settings['api_version'] == 'v4' && round($discount_price, 2)) { - $order_item['discount'] = round($discount_price, 2); - } - - $order_items[] = $order_item; + $order_items[] = $this->order_item->build($item)->get_data(); + $this->order_item->reset_data(); } $order_data['items'] = $order_items; - if ($this->retailcrm_settings['api_version'] == 'v5') { - $payment = array( - 'amount' => $order->get_total(), - 'externalId' => $order->get_id() . uniqid('-') - ); - - $payment['order'] = array( - 'externalId' => $order->get_id() - ); - - if (!empty($order_data_info['payment_method']) && !empty($this->retailcrm_settings[$order_data_info['payment_method']])) { - $payment['type'] = $this->retailcrm_settings[$order_data_info['payment_method']]; - } - - if ($order->is_paid()) { - $payment['status'] = 'paid'; - } - - if ($order->get_date_paid()) { - $pay_date = $order->get_date_paid(); - $payment['paidAt'] = trim($pay_date->date('Y-m-d H:i:s')); - } - - if (!$update) { - $order_data['payments'][] = $payment; - } + if ($this->retailcrm_settings['api_version'] == 'v5' && !$update) { + $this->order_payment->is_new = true; + $order_data['payments'][] = $this->order_payment->build($order)->get_data(); } $this->order = apply_filters('retailcrm_process_order', $order_data, $order); @@ -399,39 +315,16 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : * * @param WC_Order $order * @param boolean $update + * @param mixed $externalId * * @return array $payment */ protected function sendPayment($order, $update = false, $externalId = false) { - $payment = array( - 'amount' => $order->get_total() - ); - - if ($update) { - $payment['externalId'] = $externalId; - } else { - $payment['externalId'] = $order->get_id() . uniqid('-'); - } - - $payment['order'] = array( - 'externalId' => $order->get_id() - ); - - if ($order->is_paid()) { - $payment['status'] = 'paid'; - } - - if ($order->get_date_paid()) { - $pay_date = $order->get_date_paid(); - $payment['paidAt'] = trim($pay_date->date('Y-m-d H:i:s')); - } + $this->order_payment->is_new = !$update; + $payment = $this->order_payment->build($order, $externalId)->get_data(); if ($update === false) { - if (isset($this->retailcrm_settings[$order->get_payment_method()])) { - $payment['type'] = $this->retailcrm_settings[$order->get_payment_method()]; - } - $this->retailcrm->ordersPaymentCreate($payment); } else { $this->retailcrm->ordersPaymentEdit($payment); diff --git a/src/include/customer/class-wc-retailcrm-customer-address.php b/src/include/customer/class-wc-retailcrm-customer-address.php new file mode 100644 index 0000000..621fc72 --- /dev/null +++ b/src/include/customer/class-wc-retailcrm-customer-address.php @@ -0,0 +1,38 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +/** + * Class WC_Retailcrm_Customer_Address + */ +class WC_Retailcrm_Customer_Address extends WC_Retailcrm_Abstracts_Address +{ + protected $filter_name = 'customer_address'; + + /** + * @param WC_Customer $customer + * + * @return self + */ + public function build($customer) + { + $data = array( + 'index' => $customer->get_billing_postcode(), + 'countryIso' => $customer->get_billing_country(), + 'region' => $customer->get_billing_state(), + 'city' => $customer->get_billing_city(), + 'text' => $customer->get_billing_address_1() . ', ' . $customer->get_billing_address_2() + ); + + $this->set_data_fields($data); + + return $this; + } +} diff --git a/src/include/functions.php b/src/include/functions.php index 310bcf9..570b093 100644 --- a/src/include/functions.php +++ b/src/include/functions.php @@ -82,3 +82,19 @@ function retailcrm_get_delivery_service($method_id, $instance_id) { return false; } + +/** + * @param $id + * @param $settings + * + * @return false|WC_Product|null + */ +function retailcrm_get_wc_product($id, $settings) { + if (isset($settings['bind_by_sky']) + && $settings['bind_by_sky'] == WC_Retailcrm_Base::YES + ) { + $id = wc_get_product_id_by_sku($id); + } + + return wc_get_product($id); +} diff --git a/src/include/order/class-wc-retailcrm-order-address.php b/src/include/order/class-wc-retailcrm-order-address.php new file mode 100644 index 0000000..308d70f --- /dev/null +++ b/src/include/order/class-wc-retailcrm-order-address.php @@ -0,0 +1,46 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +class WC_Retailcrm_Order_Address extends WC_Retailcrm_Abstracts_Address +{ + protected $filter_name = 'order_address'; + + /** + * @param WC_Order $order + * + * @return self + */ + public function build($order) + { + $address = $order->get_address('shipping'); + + if (!empty($address)) { + $data = array( + 'index' => $address['postcode'], + 'city' => $address['city'], + 'region' => $address['state'] + ); + + $this->set_data_fields($data); + } + + $this->set_data_field('text', sprintf( + "%s %s %s %s %s", + $address['postcode'], + $address['state'], + $address['city'], + $address['address_1'], + $address['address_2'] + )); + + return $this; + } +} diff --git a/src/include/order/class-wc-retailcrm-order-item.php b/src/include/order/class-wc-retailcrm-order-item.php new file mode 100644 index 0000000..42e71aa --- /dev/null +++ b/src/include/order/class-wc-retailcrm-order-item.php @@ -0,0 +1,127 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +/** + * Class WC_Retailcrm_Order_Item + */ +class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data +{ + protected $filter_name = 'order_item'; + + /** + * @var array order item + */ + protected $data = array( + 'offer' => array(), + 'productName' => '', + 'initialPrice' => 0.00, + 'quantity' => 0 + ); + + /** + * @var array + */ + protected $settings = array(); + + /** + * WC_Retailcrm_Order_Item constructor. + * + * @param array $settings + */ + public function __construct($settings) + { + $this->settings = $settings; + } + + /** + * @param WC_Order_Item_Product $item + * + * @return self + */ + public function build($item) + { + $price = $this->calculate_price($item); + $discount_price = $this->calculate_discount($item, $price); + + $data['productName'] = $item['name']; + $data['initialPrice'] = (float)$price; + $data['quantity'] = $item['qty']; + + $this->set_data_fields($data); + $this->set_offer($item); + + if ($this->settings['api_version'] == 'v5' && round($discount_price, 2)) { + $this->set_data_field('discountManualAmount',round($discount_price, 2)); + } elseif ($this->settings['api_version'] == 'v4' && round($discount_price, 2)) { + $this->set_data_field('discount', round($discount_price, 2)); + } + + return $this; + } + + /** + * @param WC_Order_Item_Product $item + * + * @return void + */ + private function set_offer(WC_Order_Item_Product $item) + { + $uid = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id'] ; + $offer = array('externalId' => $uid); + + if (isset($this->settings['bind_by_sku']) && $this->settings['bind_by_sku'] == WC_Retailcrm_Base::YES) { + $offer['xmlId'] = $item->get_product()->get_sku(); + } + + $this->set_data_field('offer', $offer); + } + + /** + * @param WC_Order_Item_Product $item + * + * @return float + */ + private function calculate_price(WC_Order_Item_Product $item) + { + $price = ($item['line_subtotal'] / $item->get_quantity()) + ($item['line_subtotal_tax'] / $item->get_quantity()); + + return round($price, 2); + } + + /** + * @param WC_Order_Item_Product $item + * @param $price + * + * @return float|int + */ + 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; + $discount_price = $price - $price_item; + + return $discount_price; + } + + /** + * Reset data for object + */ + public function reset_data() + { + $this->data = array( + 'offer' => array(), + 'productName' => '', + 'initialPrice' => 0.00, + 'quantity' => 0 + ); + } +} diff --git a/src/include/order/class-wc-retailcrm-order-payment.php b/src/include/order/class-wc-retailcrm-order-payment.php new file mode 100644 index 0000000..da836ee --- /dev/null +++ b/src/include/order/class-wc-retailcrm-order-payment.php @@ -0,0 +1,98 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +/** + * Class WC_Retailcrm_Order_Payment + */ +class WC_Retailcrm_Order_Payment extends WC_Retailcrm_Abstracts_Data +{ + /** @var string */ + protected $filter_name = 'order_payment'; + + /** @var array */ + protected $data = array( + 'externalId' => '', + 'amount' => 0.00, + 'type' => '', + 'order' => array() + ); + + /** @var bool */ + public $is_new = true; + + /** + * @var array + */ + protected $settings = array(); + + /** + * WC_Retailcrm_Order_Item constructor. + * + * @param array $settings + */ + public function __construct($settings) + { + $this->settings = $settings; + } + + /** + * @param WC_Order $order + * @param mixed $externalId + * + * @return self + */ + public function build($order, $externalId = false) + { + $data = array( + 'amount' => $order->get_total() + ); + + if (!$this->is_new) { + $data['externalId'] = $externalId; + } else { + $data['externalId'] = uniqid($order->get_id()); + } + + $data['order'] = array( + 'externalId' => $order->get_id() + ); + + if ($order->is_paid()) { + $data['status'] = 'paid'; + } + + if ($order->get_date_paid()) { + $data['paidAt'] = $order->get_date_paid()->date('Y-m-d H:i:s'); + } + + if ($this->is_new) { + if (isset($this->settings[$order->get_payment_method()])) { + $data['type'] = $this->settings[$order->get_payment_method()]; + } + } + + $this->set_data_fields($data); + + return $this; + } + + public function reset_data() + { + $this->data = array( + 'externalId' => '', + 'amount' => 0.00, + 'type' => '', + 'status' => '', + 'paidAt' => '', + 'order' => array() + ); + } +} diff --git a/src/include/order/class-wc-retailcrm-order.php b/src/include/order/class-wc-retailcrm-order.php new file mode 100644 index 0000000..760f880 --- /dev/null +++ b/src/include/order/class-wc-retailcrm-order.php @@ -0,0 +1,132 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +class WC_Retailcrm_Order extends WC_Retailcrm_Abstracts_Data +{ + /** @var bool */ + public $is_new = true; + + protected $filter_name = 'order'; + + protected $data = array( + 'externalId' => 0, + 'status' => '', + 'number' => '', + 'createdAt' => '', + 'firstName' => '', + 'lastName' => '', + 'email' => '', + 'paymentType' => '', + 'customerComment' => '', + 'paymentStatus' => '', + 'phone' => '', + 'countryIso' => '' + ); + + /** + * @var array + */ + protected $settings = array(); + + /** + * WC_Retailcrm_Order constructor. + * + * @param array $settings + */ + public function __construct($settings) + { + $this->settings = $settings; + } + + /** + * @param WC_Order $order + * + * @return self + */ + public function build($order) + { + $data = array( + 'externalId' => $order->get_id(), + 'createdAt' => $order->get_date_created()->date('Y-m-d H:i:s'), + 'firstName' => $order->get_shipping_first_name(), + 'lastName' => $order->get_shipping_last_name(), + 'email' => $order->get_billing_email(), + 'customerComment' => $order->get_customer_note(), + 'phone' => $order->get_billing_phone(), + 'countryIso' => $order->get_shipping_country() + ); + + $this->set_data_fields($data); + $this->set_number($order); + + if ($this->settings['api_version'] != 'v5') { + $this->set_payment_data($order); + } + + if (isset($this->settings[$order->get_status()])) { + $this->set_data_field('status', $this->settings[$order->get_status()]); + } + + return $this; + } + + /** + * @param WC_Order $order + */ + protected function set_payment_data($order) + { + if ($order->get_payment_method() && isset($this->settings[$order->get_payment_method()])) { + $this->set_data_field('paymentType', $this->settings[$order->get_payment_method()]); + } + + if ($order->is_paid()) { + $this->set_data_field('paymentStatus', 'paid'); + } + } + + /** + * @param WC_Order $order + */ + protected function set_number($order) + { + if ($this->is_new) { + $this->set_data_field('number', $order->get_order_number()); + } + + if (isset($this->settings['update_number']) + && $this->settings['update_number'] == WC_Retailcrm_Base::YES + ) { + if (!$this->is_new) { + $this->set_data_field('number', $order->get_order_number()); + } + } elseif (!$this->is_new) { + unset($this->data['number']); + } + } + + public function reset_data() + { + $this->data = array( + 'externalId' => '', + 'status' => '', + 'number' => '', + 'createdAt' => '', + 'firstName' => '', + 'lastName' => '', + 'email' => '', + 'paymentType' => '', + 'customerComment' => '', + 'paymentStatus' => '', + 'phone' => '', + 'countryIso' => '' + ); + } +} diff --git a/src/languages/retailcrm-es_ES.mo b/src/languages/retailcrm-es_ES.mo index fa24d27..6eb92d7 100644 Binary files a/src/languages/retailcrm-es_ES.mo and b/src/languages/retailcrm-es_ES.mo differ diff --git a/src/languages/retailcrm-ru_RU.mo b/src/languages/retailcrm-ru_RU.mo index f844390..3bf2649 100644 Binary files a/src/languages/retailcrm-ru_RU.mo and b/src/languages/retailcrm-ru_RU.mo differ diff --git a/src/readme.txt b/src/readme.txt index d0d88e2..f120096 100644 --- a/src/readme.txt +++ b/src/readme.txt @@ -46,6 +46,14 @@ API-ключ должен быть для отдельного магазина == Changelog == += 3.5.1 = +* Исправлен баг при активации плагина + += 3.5.0 = +* Добавлена настройка деактивации выгрузки изменений заказа в retailCRM +* Добавлена настройка активации выгрузки SKU в xmlId и связь товаров по полю xmlId +* Добавлена настройка передачи номера заказа в retailCRM + = 3.4.5 = * Исправлен баг с добавлением скидки при уменьшении количества товара в retailCRM diff --git a/src/retailcrm.php b/src/retailcrm.php index 8d69c3e..8557c5f 100644 --- a/src/retailcrm.php +++ b/src/retailcrm.php @@ -1,6 +1,6 @@ load_plugin_textdomain(); if (class_exists( 'WC_Integration' ) ) { + require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-settings.php'); + require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-data.php'); + require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-address.php'); + require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order.php'); + require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-payment.php'); + require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-item.php'); + require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-address.php'); + require_once(dirname(__FILE__ ) . '/include/customer/class-wc-retailcrm-customer-address.php'); require_once(dirname(__FILE__ ) . '/include/class-wc-retailcrm-base.php'); require_once(dirname(__FILE__ ) . '/include/functions.php'); add_filter('woocommerce_integrations', array( $this, 'add_integration')); diff --git a/src/uninstall.php b/src/uninstall.php index aad2219..44e1bf8 100644 --- a/src/uninstall.php +++ b/src/uninstall.php @@ -15,7 +15,7 @@ * * * @link https://wordpress.org/plugins/woo-retailcrm/ - * @version 3.4.5 + * @version 3.5.1 * * @package RetailCRM */ diff --git a/tests/helpers/class-wc-retailcrm-test-case-helper.php b/tests/helpers/class-wc-retailcrm-test-case-helper.php index 26d1f7e..a7865a3 100644 --- a/tests/helpers/class-wc-retailcrm-test-case-helper.php +++ b/tests/helpers/class-wc-retailcrm-test-case-helper.php @@ -1,7 +1,15 @@ + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +class WC_Retailcrm_Customer_Address_Test extends WC_Retailcrm_Test_Case_Helper +{ + protected $customer; + + public function setUp() + { + parent::setUp(); + + $this->customer = WC_Helper_Customer::create_customer(); + } + + public function test_build() + { + $customer_address = new WC_Retailcrm_Customer_Address; + $data = $customer_address->build($this->customer)->get_data(); + + $this->assertArrayHasKey('index', $data); + $this->assertArrayHasKey('city', $data); + $this->assertArrayHasKey('region', $data); + $this->assertArrayHasKey('text', $data); + $this->assertArrayHasKey('countryIso', $data); + $this->assertNotEmpty($data['index']); + $this->assertNotEmpty($data['city']); + $this->assertNotEmpty($data['region']); + $this->assertNotEmpty($data['text']); + $this->assertNotEmpty($data['countryIso']); + } +} diff --git a/tests/phpunit/order/test-wc-retailcrm-order-address.php b/tests/phpunit/order/test-wc-retailcrm-order-address.php new file mode 100644 index 0000000..b7c0a52 --- /dev/null +++ b/tests/phpunit/order/test-wc-retailcrm-order-address.php @@ -0,0 +1,37 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +/** + * Class WC_Retailcrm_Order_Address_Test + */ +class WC_Retailcrm_Order_Address_Test extends WC_Retailcrm_Test_Case_Helper +{ + /** @var WC_Order */ + protected $order; + + public function setUp() + { + parent::setUp(); + + $this->order = WC_Helper_Order::create_order(); + } + + public function test_build() + { + $order_address = new WC_Retailcrm_Order_Address; + $data = $order_address->build($this->order)->get_data(); + + $this->assertArrayHasKey('index', $data); + $this->assertArrayHasKey('city', $data); + $this->assertArrayHasKey('region', $data); + $this->assertArrayHasKey('text', $data); + } +} diff --git a/tests/phpunit/order/test-wc-retailcrm-order-item.php b/tests/phpunit/order/test-wc-retailcrm-order-item.php new file mode 100644 index 0000000..5c6a523 --- /dev/null +++ b/tests/phpunit/order/test-wc-retailcrm-order-item.php @@ -0,0 +1,38 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +class WC_Retailcrm_Order_Item_Test extends WC_Retailcrm_Test_Case_Helper +{ + /** @var WC_Order */ + protected $order; + + public function setUp() + { + parent::setUp(); + + $this->order = WC_Helper_Order::create_order(); + } + + public function test_build() + { + $order_item = new WC_Retailcrm_Order_Item($this->getOptions()); + + /** @var WC_Order_Item_Product $item */ + foreach ($this->order->get_items() as $item) { + $data = $order_item->build($item)->get_data(); + + $this->assertArrayHasKey('productName', $data); + $this->assertArrayHasKey('initialPrice', $data); + $this->assertArrayHasKey('quantity', $data); + $this->assertArrayHasKey('offer', $data); + } + } +} diff --git a/tests/phpunit/order/test-wc-retailcrm-order-payment.php b/tests/phpunit/order/test-wc-retailcrm-order-payment.php new file mode 100644 index 0000000..31bffde --- /dev/null +++ b/tests/phpunit/order/test-wc-retailcrm-order-payment.php @@ -0,0 +1,55 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +class WC_Retailcrm_Order_Payment_Test extends WC_Retailcrm_Test_Case_Helper +{ + /** @var WC_Order */ + protected $order; + + public function setUp() + { + parent::setUp(); + + $this->order = WC_Helper_Order::create_order(); + } + + /** + * @param mixed $externalId + * + * @dataProvider dataProvider + */ + public function test_build($externalId) + { + $order_payment = new WC_Retailcrm_Order_Payment($this->getOptions()); + + $data = $order_payment->build($this->order, $externalId)->get_data(); + + $this->assertArrayHasKey('externalId', $data); + $this->assertArrayHasKey('type', $data); + $this->assertArrayHasKey('amount', $data); + $this->assertArrayHasKey('order', $data); + } + + /** + * @return array + */ + public function dataProvider() + { + return array( + array( + 'externalId' => false + ), + array( + 'externalId' => uniqid() + ) + ); + } +} diff --git a/tests/phpunit/test-wc-retailcrm-customers.php b/tests/phpunit/test-wc-retailcrm-customers.php index 284260a..35c82c3 100644 --- a/tests/phpunit/test-wc-retailcrm-customers.php +++ b/tests/phpunit/test-wc-retailcrm-customers.php @@ -51,7 +51,7 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper public function test_wc_customer_get($retailcrm) { $wc_customer = new WC_Customer($this->customer->get_id()); - $retailcrm_customer = new WC_Retailcrm_Customers($retailcrm); + $retailcrm_customer = $this->getRetailcrmCustomer($retailcrm); $this->assertEquals($wc_customer, $retailcrm_customer->wcCustomerGet($this->customer->get_id())); } @@ -61,7 +61,7 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper */ public function test_customers_upload($retailcrm) { - $retailcrm_customer = new WC_Retailcrm_Customers($retailcrm); + $retailcrm_customer = $this->getRetailcrmCustomer($retailcrm); $data = $retailcrm_customer->customersUpload(); if ($retailcrm) { @@ -79,7 +79,7 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper */ public function test_create_customer($retailcrm) { - $retailcrm_customer = new WC_Retailcrm_Customers($retailcrm); + $retailcrm_customer = $this->getRetailcrmCustomer($retailcrm); $id = $retailcrm_customer->createCustomer($this->customer->get_id()); $customer_send = $retailcrm_customer->getCustomer(); @@ -102,7 +102,7 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper */ public function test_update_customer($retailcrm) { - $retailcrm_customer = new WC_Retailcrm_Customers($retailcrm); + $retailcrm_customer = $this->getRetailcrmCustomer($retailcrm); $customer = $retailcrm_customer->updateCustomer($this->customer->get_id()); $customer_send = $retailcrm_customer->getCustomer(); @@ -134,4 +134,18 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper ) ); } -} \ No newline at end of file + + /** + * @param $retailcrm + * + * @return WC_Retailcrm_Customers + */ + private function getRetailcrmCustomer($retailcrm) + { + return new WC_Retailcrm_Customers( + $retailcrm, + $this->getOptions(), + new WC_Retailcrm_Customer_Address() + ); + } +} diff --git a/tests/phpunit/test-wc-retailcrm-orders.php b/tests/phpunit/test-wc-retailcrm-orders.php index 7f5d3cc..b7df534 100644 --- a/tests/phpunit/test-wc-retailcrm-orders.php +++ b/tests/phpunit/test-wc-retailcrm-orders.php @@ -34,7 +34,7 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper public function test_order_upload($retailcrm, $apiVersion) { $this->options = $this->setOptions($apiVersion); - $retailcrm_orders = new WC_Retailcrm_Orders($retailcrm); + $retailcrm_orders = $this->getRetailcrmOrders($retailcrm); $upload_orders = $retailcrm_orders->ordersUpload(); if ($retailcrm) { @@ -83,7 +83,7 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->createTestOrder(); $this->options = $this->setOptions($apiVersion); - $retailcrm_orders = new WC_Retailcrm_Orders($retailcrm); + $retailcrm_orders = $this->getRetailcrmOrders($retailcrm); $order = $retailcrm_orders->orderCreate($this->order->get_id()); $order_send = $retailcrm_orders->getOrder(); @@ -164,7 +164,7 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper ->willReturn($responseMock); } - $retailcrm_orders = new WC_Retailcrm_Orders($retailcrm); + $retailcrm_orders = $this->getRetailcrmOrders($retailcrm); $order = $retailcrm_orders->updateOrder($this->order->get_id()); $order_send = $retailcrm_orders->getOrder(); @@ -312,4 +312,24 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper ) ); } + + /** + * @param $retailcrm + * + * @return WC_Retailcrm_Orders + */ + private function getRetailcrmOrders($retailcrm) + { + return new WC_Retailcrm_Orders( + $retailcrm, + $this->getOptions(), + new WC_Retailcrm_Order_Item($this->getOptions()), + new WC_Retailcrm_Order_Address, + new WC_Retailcrm_Customers( + $retailcrm, $this->getOptions(), new WC_Retailcrm_Customer_Address + ), + new WC_Retailcrm_Order($this->getOptions()), + new WC_Retailcrm_Order_Payment($this->getOptions()) + ); + } }