From 37aa349d11f2f672ee735b48e6924112dad9f447 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Sun, 30 Jul 2017 15:42:49 +0200 Subject: [PATCH] plugin for API v4, v5 (#4) * plugin for API v4, v5 * fix icml generation --- retailcrm/include-custom/.gitkeep | 0 .../api/class-wc-retailcrm-client-v3.php | 828 ++++++ ...t.php => class-wc-retailcrm-client-v4.php} | 9 +- .../api/class-wc-retailcrm-client-v5.php | 2373 +++++++++++++++++ .../api/class-wc-retailcrm-exception-curl.php | 4 +- .../api/class-wc-retailcrm-exception-json.php | 5 +- .../include/api/class-wc-retailcrm-proxy.php | 32 +- .../api/class-wc-retailcrm-request.php | 3 +- .../api/class-wc-retailcrm-response.php | 3 +- retailcrm/include/class-wc-retailcrm-base.php | 52 +- .../include/class-wc-retailcrm-customers.php | 7 +- .../include/class-wc-retailcrm-history.php | 158 +- retailcrm/include/class-wc-retailcrm-icml.php | 137 +- .../class-wc-retailcrm-inventories.php | 7 +- .../include/class-wc-retailcrm-orders.php | 190 +- retailcrm/retailcrm.php | 32 +- retailcrm/uninstall.php | 2 +- 17 files changed, 3650 insertions(+), 192 deletions(-) create mode 100644 retailcrm/include-custom/.gitkeep create mode 100644 retailcrm/include/api/class-wc-retailcrm-client-v3.php rename retailcrm/include/api/{class-wc-retailcrm-client.php => class-wc-retailcrm-client-v4.php} (99%) create mode 100644 retailcrm/include/api/class-wc-retailcrm-client-v5.php diff --git a/retailcrm/include-custom/.gitkeep b/retailcrm/include-custom/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/retailcrm/include/api/class-wc-retailcrm-client-v3.php b/retailcrm/include/api/class-wc-retailcrm-client-v3.php new file mode 100644 index 0000000..ee3d24a --- /dev/null +++ b/retailcrm/include/api/class-wc-retailcrm-client-v3.php @@ -0,0 +1,828 @@ + + * @license https://opensource.org/licenses/MIT MIT License + * @link http://retailcrm.ru/docs/Developers/ApiVersion3 + */ + + if ( ! class_exists( 'WC_Retailcrm_Request' ) ) { + include_once( __DIR__ . '/class-wc-retailcrm-request.php' ); + } + + if ( ! class_exists( 'WC_Retailcrm_Response' ) ) { + include_once( __DIR__ . '/class-wc-retailcrm-response.php' ); + } + + class WC_Retailcrm_Client_V3 + { + const VERSION = 'v3'; + + protected $client; + + /** + * Site code + */ + protected $siteCode; + + /** + * Client creating + * + * @param string $url + * @param string $apiKey + * @param string $site + */ + public function __construct($url, $apiKey, $site = null) + { + if ('/' != substr($url, strlen($url) - 1, 1)) { + $url .= '/'; + } + + $url = $url . 'api/' . self::VERSION; + + $this->client = new WC_Retailcrm_Request($url, array('apiKey' => $apiKey)); + $this->siteCode = $site; + } + + /** + * Create a order + * + * @param array $order + * @param string $site (default: null) + * @return WC_Retailcrm_Response + */ + public function ordersCreate(array $order, $site = null) + { + if (!sizeof($order)) { + throw new InvalidArgumentException('Parameter `order` must contains a data'); + } + + return $this->client->makeRequest("/orders/create", WC_Retailcrm_Request::METHOD_POST, $this->fillSite($site, array( + 'order' => json_encode($order) + ))); + } + + /** + * Edit a order + * + * @param array $order + * @param string $by + * @param string $site (default: null) + * @return WC_Retailcrm_Response + */ + public function ordersEdit(array $order, $by = 'externalId', $site = null) + { + if (!sizeof($order)) { + throw new InvalidArgumentException('Parameter `order` must contains a data'); + } + + $this->checkIdParameter($by); + + if (!isset($order[$by])) { + throw new InvalidArgumentException(sprintf('Order array must contain the "%s" parameter.', $by)); + } + + return $this->client->makeRequest( + "/orders/" . $order[$by] . "/edit", + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array( + 'order' => json_encode($order), + 'by' => $by, + )) + ); + } + + /** + * Upload array of the orders + * + * @param array $orders + * @param string $site (default: null) + * @return WC_Retailcrm_Response + */ + public function ordersUpload(array $orders, $site = null) + { + if (!sizeof($orders)) { + throw new InvalidArgumentException('Parameter `orders` must contains array of the orders'); + } + + return $this->client->makeRequest("/orders/upload", WC_Retailcrm_Request::METHOD_POST, $this->fillSite($site, array( + 'orders' => json_encode($orders), + ))); + } + + /** + * Get order by id or externalId + * + * @param string $id + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * @return WC_Retailcrm_Response + */ + public function ordersGet($id, $by = 'externalId', $site = null) + { + $this->checkIdParameter($by); + + return $this->client->makeRequest("/orders/$id", WC_Retailcrm_Request::METHOD_GET, $this->fillSite($site, array( + 'by' => $by + ))); + } + + /** + * Returns a orders history + * + * @param DateTime $startDate (default: null) + * @param DateTime $endDate (default: null) + * @param int $limit (default: 100) + * @param int $offset (default: 0) + * @param bool $skipMyChanges (default: true) + * @return WC_Retailcrm_Response + */ + public function ordersHistory( + DateTime $startDate = null, + DateTime $endDate = null, + $limit = 100, + $offset = 0, + $skipMyChanges = true + ) { + $parameters = array(); + + if ($startDate) { + $parameters['startDate'] = $startDate->format('Y-m-d H:i:s'); + } + if ($endDate) { + $parameters['endDate'] = $endDate->format('Y-m-d H:i:s'); + } + if ($limit) { + $parameters['limit'] = (int) $limit; + } + if ($offset) { + $parameters['offset'] = (int) $offset; + } + if ($skipMyChanges) { + $parameters['skipMyChanges'] = (bool) $skipMyChanges; + } + + return $this->client->makeRequest('/orders/history', WC_Retailcrm_Request::METHOD_GET, $parameters); + } + + /** + * Returns filtered orders list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * @return WC_Retailcrm_Response + */ + public function ordersList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (sizeof($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest('/orders', WC_Retailcrm_Request::METHOD_GET, $parameters); + } + + /** + * Returns statuses of the orders + * + * @param array $ids (default: array()) + * @param array $externalIds (default: array()) + * @return WC_Retailcrm_Response + */ + public function ordersStatuses(array $ids = array(), array $externalIds = array()) + { + $parameters = array(); + + if (sizeof($ids)) { + $parameters['ids'] = $ids; + } + if (sizeof($externalIds)) { + $parameters['externalIds'] = $externalIds; + } + + return $this->client->makeRequest('/orders/statuses', WC_Retailcrm_Request::METHOD_GET, $parameters); + } + + /** + * Save order IDs' (id and externalId) association in the CRM + * + * @param array $ids + * @return WC_Retailcrm_Response + */ + public function ordersFixExternalIds(array $ids) + { + if (!sizeof($ids)) { + throw new InvalidArgumentException('Method parameter must contains at least one IDs pair'); + } + + return $this->client->makeRequest("/orders/fix-external-ids", WC_Retailcrm_Request::METHOD_POST, array( + 'orders' => json_encode($ids), + )); + } + + /** + * Get orders assembly history + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * @return WC_Retailcrm_Response + */ + public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (sizeof($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest('/orders/packs/history', WC_Retailcrm_Request::METHOD_GET, $parameters); + } + + /** + * Create a customer + * + * @param array $customer + * @param string $site (default: null) + * @return WC_Retailcrm_Response + */ + public function customersCreate(array $customer, $site = null) + { + if (!sizeof($customer)) { + throw new InvalidArgumentException('Parameter `customer` must contains a data'); + } + + return $this->client->makeRequest("/customers/create", WC_Retailcrm_Request::METHOD_POST, $this->fillSite($site, array( + 'customer' => json_encode($customer) + ))); + } + + /** + * Edit a customer + * + * @param array $customer + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * @return WC_Retailcrm_Response + */ + public function customersEdit(array $customer, $by = 'externalId', $site = null) + { + if (!sizeof($customer)) { + throw new InvalidArgumentException('Parameter `customer` must contains a data'); + } + + $this->checkIdParameter($by); + + if (!isset($customer[$by])) { + throw new InvalidArgumentException(sprintf('Customer array must contain the "%s" parameter.', $by)); + } + + return $this->client->makeRequest( + "/customers/" . $customer[$by] . "/edit", + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite( + $site, + array( + 'customer' => json_encode($customer), + 'by' => $by + ) + ) + ); + } + + /** + * Upload array of the customers + * + * @param array $customers + * @param string $site (default: null) + * @return WC_Retailcrm_Response + */ + public function customersUpload(array $customers, $site = null) + { + if (!sizeof($customers)) { + throw new InvalidArgumentException('Parameter `customers` must contains array of the customers'); + } + + return $this->client->makeRequest("/customers/upload", WC_Retailcrm_Request::METHOD_POST, $this->fillSite($site, array( + 'customers' => json_encode($customers), + ))); + } + + /** + * Get customer by id or externalId + * + * @param string $id + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * @return WC_Retailcrm_Response + */ + public function customersGet($id, $by = 'externalId', $site = null) + { + $this->checkIdParameter($by); + + return $this->client->makeRequest("/customers/$id", WC_Retailcrm_Request::METHOD_GET, $this->fillSite($site, array( + 'by' => $by + ))); + } + + /** + * Returns filtered customers list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * @return WC_Retailcrm_Response + */ + public function customersList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (sizeof($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest('/customers', WC_Retailcrm_Request::METHOD_GET, $parameters); + } + + /** + * Save customer IDs' (id and externalId) association in the CRM + * + * @param array $ids + * @return WC_Retailcrm_Response + */ + public function customersFixExternalIds(array $ids) + { + if (!sizeof($ids)) { + throw new InvalidArgumentException('Method parameter must contains at least one IDs pair'); + } + + return $this->client->makeRequest("/customers/fix-external-ids", WC_Retailcrm_Request::METHOD_POST, array( + 'customers' => json_encode($ids), + )); + } + + /** + * Get purchace prices & stock balance + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * @param string $site (default: null) + * @return WC_Retailcrm_Response + */ + public function storeInventories(array $filter = array(), $page = null, $limit = null, $site = null) + { + $parameters = array(); + + if (sizeof($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest('/store/inventories', WC_Retailcrm_Request::METHOD_GET, $this->fillSite($site, $parameters)); + } + + /** + * Upload store inventories + * + * @param array $offers + * @param string $site (default: null) + * @return WC_Retailcrm_Response + */ + public function storeInventoriesUpload(array $offers, $site = null) + { + if (!sizeof($offers)) { + throw new InvalidArgumentException('Parameter `offers` must contains array of the customers'); + } + + return $this->client->makeRequest( + "/store/inventories/upload", + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('offers' => json_encode($offers))) + ); + } + + /** + * Returns deliveryServices list + * + * @return WC_Retailcrm_Response + */ + public function deliveryServicesList() + { + return $this->client->makeRequest('/reference/delivery-services', WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Returns deliveryTypes list + * + * @return WC_Retailcrm_Response + */ + public function deliveryTypesList() + { + return $this->client->makeRequest('/reference/delivery-types', WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Returns orderMethods list + * + * @return WC_Retailcrm_Response + */ + public function orderMethodsList() + { + return $this->client->makeRequest('/reference/order-methods', WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Returns orderTypes list + * + * @return WC_Retailcrm_Response + */ + public function orderTypesList() + { + return $this->client->makeRequest('/reference/order-types', WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Returns paymentStatuses list + * + * @return WC_Retailcrm_Response + */ + public function paymentStatusesList() + { + return $this->client->makeRequest('/reference/payment-statuses', WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Returns paymentTypes list + * + * @return WC_Retailcrm_Response + */ + public function paymentTypesList() + { + return $this->client->makeRequest('/reference/payment-types', WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Returns productStatuses list + * + * @return WC_Retailcrm_Response + */ + public function productStatusesList() + { + return $this->client->makeRequest('/reference/product-statuses', WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Returns statusGroups list + * + * @return WC_Retailcrm_Response + */ + public function statusGroupsList() + { + return $this->client->makeRequest('/reference/status-groups', WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Returns statuses list + * + * @return WC_Retailcrm_Response + */ + public function statusesList() + { + return $this->client->makeRequest('/reference/statuses', WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Returns sites list + * + * @return WC_Retailcrm_Response + */ + public function sitesList() + { + return $this->client->makeRequest('/reference/sites', WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Returns stores list + * + * @return WC_Retailcrm_Response + */ + public function storesList() + { + return $this->client->makeRequest('/reference/stores', WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Edit deliveryService + * + * @param array $data delivery service data + * @return WC_Retailcrm_Response + */ + public function deliveryServicesEdit(array $data) + { + if (!isset($data['code'])) { + throw new InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest( + '/reference/delivery-services/' . $data['code'] . '/edit', + WC_Retailcrm_Request::METHOD_POST, + array( + 'deliveryService' => json_encode($data) + ) + ); + } + + /** + * Edit deliveryType + * + * @param array $data delivery type data + * @return WC_Retailcrm_Response + */ + public function deliveryTypesEdit(array $data) + { + if (!isset($data['code'])) { + throw new InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest( + '/reference/delivery-types/' . $data['code'] . '/edit', + WC_Retailcrm_Request::METHOD_POST, + array( + 'deliveryType' => json_encode($data) + ) + ); + } + + /** + * Edit orderMethod + * + * @param array $data order method data + * @return WC_Retailcrm_Response + */ + public function orderMethodsEdit(array $data) + { + if (!isset($data['code'])) { + throw new InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest( + '/reference/order-methods/' . $data['code'] . '/edit', + WC_Retailcrm_Request::METHOD_POST, + array( + 'orderMethod' => json_encode($data) + ) + ); + } + + /** + * Edit orderType + * + * @param array $data order type data + * @return WC_Retailcrm_Response + */ + public function orderTypesEdit(array $data) + { + if (!isset($data['code'])) { + throw new InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest( + '/reference/order-types/' . $data['code'] . '/edit', + WC_Retailcrm_Request::METHOD_POST, + array( + 'orderType' => json_encode($data) + ) + ); + } + + /** + * Edit paymentStatus + * + * @param array $data payment status data + * @return WC_Retailcrm_Response + */ + public function paymentStatusesEdit(array $data) + { + if (!isset($data['code'])) { + throw new InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest( + '/reference/payment-statuses/' . $data['code'] . '/edit', + WC_Retailcrm_Request::METHOD_POST, + array( + 'paymentStatus' => json_encode($data) + ) + ); + } + + /** + * Edit paymentType + * + * @param array $data payment type data + * @return WC_Retailcrm_Response + */ + public function paymentTypesEdit(array $data) + { + if (!isset($data['code'])) { + throw new InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest( + '/reference/payment-types/' . $data['code'] . '/edit', + WC_Retailcrm_Request::METHOD_POST, + array( + 'paymentType' => json_encode($data) + ) + ); + } + + /** + * Edit productStatus + * + * @param array $data product status data + * @return WC_Retailcrm_Response + */ + public function productStatusesEdit(array $data) + { + if (!isset($data['code'])) { + throw new InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest( + '/reference/product-statuses/' . $data['code'] . '/edit', + WC_Retailcrm_Request::METHOD_POST, + array( + 'productStatus' => json_encode($data) + ) + ); + } + + /** + * Edit order status + * + * @param array $data status data + * @return WC_Retailcrm_Response + */ + public function statusesEdit(array $data) + { + if (!isset($data['code'])) { + throw new InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest( + '/reference/statuses/' . $data['code'] . '/edit', + WC_Retailcrm_Request::METHOD_POST, + array( + 'status' => json_encode($data) + ) + ); + } + + /** + * Edit site + * + * @param array $data site data + * @return WC_Retailcrm_Response + */ + public function sitesEdit(array $data) + { + if (!isset($data['code'])) { + throw new InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest( + '/reference/sites/' . $data['code'] . '/edit', + WC_Retailcrm_Request::METHOD_POST, + array( + 'site' => json_encode($data) + ) + ); + } + + /** + * Edit store + * + * @param array $data site data + * @return WC_Retailcrm_Response + */ + public function storesEdit(array $data) + { + if (!isset($data['code'])) { + throw new InvalidArgumentException('Data must contain "code" parameter.'); + } + + if (!isset($data['name'])) { + throw new InvalidArgumentException('Data must contain "name" parameter.'); + } + + return $this->client->makeRequest( + '/reference/stores/' . $data['code'] . '/edit', + WC_Retailcrm_Request::METHOD_POST, + array( + 'store' => json_encode($data) + ) + ); + } + + /** + * Update CRM basic statistic + * + * @return WC_Retailcrm_Response + */ + public function statisticUpdate() + { + return $this->client->makeRequest('/statistic/update', WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Return current site + * + * @return string + */ + public function getSite() + { + return $this->siteCode; + } + + /** + * Set site + * + * @param string $site + * @return void + */ + public function setSite($site) + { + $this->siteCode = $site; + } + + /** + * Check ID parameter + * + * @param string $by + * @return bool + */ + protected function checkIdParameter($by) + { + $allowedForBy = array('externalId', 'id'); + if (!in_array($by, $allowedForBy)) { + throw new InvalidArgumentException(sprintf( + 'Value "%s" for parameter "by" is not valid. Allowed values are %s.', + $by, + implode(', ', $allowedForBy) + )); + } + + return true; + } + + /** + * Fill params by site value + * + * @param string $site + * @param array $params + * @return array + */ + protected function fillSite($site, array $params) + { + if ($site) { + $params['site'] = $site; + } elseif ($this->siteCode) { + $params['site'] = $this->siteCode; + } + + return $params; + } + } diff --git a/retailcrm/include/api/class-wc-retailcrm-client.php b/retailcrm/include/api/class-wc-retailcrm-client-v4.php similarity index 99% rename from retailcrm/include/api/class-wc-retailcrm-client.php rename to retailcrm/include/api/class-wc-retailcrm-client-v4.php index 22fd582..c435909 100644 --- a/retailcrm/include/api/class-wc-retailcrm-client.php +++ b/retailcrm/include/api/class-wc-retailcrm-client-v4.php @@ -6,8 +6,8 @@ * Request class * * @category Integration - * @package WC_RetailCrm_Client - * @author Retailcrm + * @package WC_Retailcrm_Client + * @author RetailCRM * @license https://opensource.org/licenses/MIT MIT License * @link http://retailcrm.ru/docs/Developers/ApiVersion4 */ @@ -20,9 +20,8 @@ include_once( __DIR__ . '/class-wc-retailcrm-response.php' ); } - class WC_Retailcrm_Client + class WC_Retailcrm_Client_V4 { - const VERSION = 'v4'; protected $client; @@ -33,7 +32,7 @@ protected $siteCode; /** - * WC_Retailcrm_Request creating + * Client creating * * @param string $url api url * @param string $apiKey api key diff --git a/retailcrm/include/api/class-wc-retailcrm-client-v5.php b/retailcrm/include/api/class-wc-retailcrm-client-v5.php new file mode 100644 index 0000000..cbc6de9 --- /dev/null +++ b/retailcrm/include/api/class-wc-retailcrm-client-v5.php @@ -0,0 +1,2373 @@ + + * @license https://opensource.org/licenses/MIT MIT License + * @link http://retailcrm.ru/docs/Developers/ApiVersion5 + */ + + if ( ! class_exists( 'WC_Retailcrm_Request' ) ) { + include_once( __DIR__ . '/class-wc-retailcrm-request.php' ); + } + + if ( ! class_exists( 'WC_Retailcrm_Response' ) ) { + include_once( __DIR__ . '/class-wc-retailcrm-response.php' ); + } + + class WC_Retailcrm_Client_V5 + { + const VERSION = 'v5'; + + protected $client; + + /** + * Site code + */ + protected $siteCode; + + /** + * Client creating + * + * @param string $url api url + * @param string $apiKey api key + * @param string $site site code + * + * @throws \InvalidArgumentException + * + */ + public function __construct($url, $apiKey, $site = null) + { + if ('/' !== $url[strlen($url) - 1]) { + $url .= '/'; + } + + $url = $url . 'api/' . self::VERSION; + + $this->client = new WC_Retailcrm_Request($url, array('apiKey' => $apiKey)); + $this->siteCode = $site; + } + + /** + * Returns users list + * + * @param array $filter + * @param null $page + * @param null $limit + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function usersList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/users', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Returns user data + * + * @param integer $id user ID + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function usersGet($id) + { + return $this->client->makeRequest("/users/$id", WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Change user status + * + * @param integer $id user ID + * @param string $status user status + * + * @return WC_Retailcrm_Response + */ + public function usersStatus($id, $status) + { + $statuses = array("free", "busy", "dinner", "break"); + + if (empty($status) || !in_array($status, $statuses)) { + throw new \InvalidArgumentException( + 'Parameter `status` must be not empty & must be equal one of these values: free|busy|dinner|break' + ); + } + + return $this->client->makeRequest( + "/users/$id/status", + WC_Retailcrm_Request::METHOD_POST, + array('status' => $status) + ); + } + + /** + * Get segments list + * + * @param array $filter + * @param null $limit + * @param null $page + * + * @return WC_Retailcrm_Response + */ + public function segmentsList(array $filter = array(), $limit = null, $page = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/segments', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get custom fields list + * + * @param array $filter + * @param null $limit + * @param null $page + * + * @return WC_Retailcrm_Response + */ + public function customFieldsList(array $filter = array(), $limit = null, $page = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/custom-fields', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create custom field + * + * @param $entity + * @param $customField + * + * @return WC_Retailcrm_Response + */ + public function customFieldsCreate($entity, $customField) + { + if (!count($customField) || + empty($customField['code']) || + empty($customField['name']) || + empty($customField['type']) + ) { + throw new \InvalidArgumentException( + 'Parameter `customField` must contain a data & fields `code`, `name` & `type` must be set' + ); + } + + if (empty($entity) || $entity != 'customer' || $entity != 'order') { + throw new \InvalidArgumentException( + 'Parameter `entity` must contain a data & value must be `order` or `customer`' + ); + } + + return $this->client->makeRequest( + "/custom-fields/$entity/create", + WC_Retailcrm_Request::METHOD_POST, + array('customField' => json_encode($customField)) + ); + } + + /** + * Edit custom field + * + * @param $entity + * @param $customField + * + * @return WC_Retailcrm_Response + */ + public function customFieldsEdit($entity, $customField) + { + if (!count($customField) || empty($customField['code'])) { + throw new \InvalidArgumentException( + 'Parameter `customField` must contain a data & fields `code` must be set' + ); + } + + if (empty($entity) || $entity != 'customer' || $entity != 'order') { + throw new \InvalidArgumentException( + 'Parameter `entity` must contain a data & value must be `order` or `customer`' + ); + } + + return $this->client->makeRequest( + "/custom-fields/$entity/edit/{$customField['code']}", + $this->client::METHOD_POST, + array('customField' => json_encode($customField)) + ); + } + + /** + * Get custom field + * + * @param $entity + * @param $code + * + * @return WC_Retailcrm_Response + */ + public function customFieldsGet($entity, $code) + { + if (empty($code)) { + throw new \InvalidArgumentException( + 'Parameter `code` must be not empty' + ); + } + + if (empty($entity) || $entity != 'customer' || $entity != 'order') { + throw new \InvalidArgumentException( + 'Parameter `entity` must contain a data & value must be `order` or `customer`' + ); + } + + return $this->client->makeRequest( + "/custom-fields/$entity/$code", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Get custom dictionaries list + * + * @param array $filter + * @param null $limit + * @param null $page + * + * @return WC_Retailcrm_Response + */ + public function customDictionariesList(array $filter = [], $limit = null, $page = null) + { + $parameters = []; + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/custom-fields/dictionaries', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create custom dictionary + * + * @param $customDictionary + * + * @return WC_Retailcrm_Response + */ + public function customDictionariesCreate($customDictionary) + { + if (!count($customDictionary) || + empty($customDictionary['code']) || + empty($customDictionary['elements']) + ) { + throw new \InvalidArgumentException( + 'Parameter `dictionary` must contain a data & fields `code` & `elemets` must be set' + ); + } + + return $this->client->makeRequest( + "/custom-fields/dictionaries/{$customDictionary['code']}/create", + WC_Retailcrm_Request::METHOD_POST, + array('customDictionary' => json_encode($customDictionary)) + ); + } + + /** + * Edit custom dictionary + * + * @param $customDictionary + * + * @return WC_Retailcrm_Response + */ + public function customDictionariesEdit($customDictionary) + { + if (!count($customDictionary) || + empty($customDictionary['code']) || + empty($customDictionary['elements']) + ) { + throw new \InvalidArgumentException( + 'Parameter `dictionary` must contain a data & fields `code` & `elemets` must be set' + ); + } + + return $this->client->makeRequest( + "/custom-fields/dictionaries/{$customDictionary['code']}/edit", + WC_Retailcrm_Request::METHOD_POST, + array('customDictionary' => json_encode($customDictionary)) + ); + } + + /** + * Get custom dictionary + * + * @param $code + * + * @return WC_Retailcrm_Response + */ + public function customDictionariesGet($code) + { + if (empty($code)) { + throw new \InvalidArgumentException( + 'Parameter `code` must be not empty' + ); + } + + return $this->client->makeRequest( + "/custom-fields/dictionaries/$code", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Returns filtered orders list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/orders', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create a order + * + * @param array $order order data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersCreate(array $order, $site = null) + { + if (!count($order)) { + throw new \InvalidArgumentException( + 'Parameter `order` must contains a data' + ); + } + + return $this->client->makeRequest( + '/orders/create', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('order' => json_encode($order))) + ); + } + + /** + * Save order IDs' (id and externalId) association in the CRM + * + * @param array $ids order identificators + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersFixExternalIds(array $ids) + { + if (! count($ids)) { + throw new \InvalidArgumentException( + 'Method parameter must contains at least one IDs pair' + ); + } + + return $this->client->makeRequest( + '/orders/fix-external-ids', + WC_Retailcrm_Request::METHOD_POST, + array('orders' => json_encode($ids) + ) + ); + } + + /** + * Returns statuses of the orders + * + * @param array $ids (default: array()) + * @param array $externalIds (default: array()) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersStatuses(array $ids = array(), array $externalIds = array()) + { + $parameters = array(); + + if (count($ids)) { + $parameters['ids'] = $ids; + } + if (count($externalIds)) { + $parameters['externalIds'] = $externalIds; + } + + return $this->client->makeRequest( + '/orders/statuses', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Upload array of the orders + * + * @param array $orders array of orders + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersUpload(array $orders, $site = null) + { + if (!count($orders)) { + throw new \InvalidArgumentException( + 'Parameter `orders` must contains array of the orders' + ); + } + + return $this->client->makeRequest( + '/orders/upload', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('orders' => json_encode($orders))) + ); + } + + /** + * Get order by id or externalId + * + * @param string $id order identificator + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersGet($id, $by = 'externalId', $site = null) + { + $this->checkIdParameter($by); + + return $this->client->makeRequest( + "/orders/$id", + WC_Retailcrm_Request::METHOD_GET, + $this->fillSite($site, array('by' => $by)) + ); + } + + /** + * Edit a order + * + * @param array $order order data + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersEdit(array $order, $by = 'externalId', $site = null) + { + if (!count($order)) { + throw new \InvalidArgumentException( + 'Parameter `order` must contains a data' + ); + } + + $this->checkIdParameter($by); + + if (!array_key_exists($by, $order)) { + throw new \InvalidArgumentException( + sprintf('Order array must contain the "%s" parameter.', $by) + ); + } + + return $this->client->makeRequest( + sprintf('/orders/%s/edit', $order[$by]), + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite( + $site, + array('order' => json_encode($order), 'by' => $by) + ) + ); + } + + /** + * Get orders history + * @param array $filter + * @param null $page + * @param null $limit + * + * @return WC_Retailcrm_Response + */ + public function ordersHistory(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/orders/history', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Combine orders + * + * @param string $technique + * @param array $order + * @param array $resultOrder + * + * @return WC_Retailcrm_Response + */ + public function ordersCombine($order, $resultOrder, $technique = 'ours') + { + $techniques = array('ours', 'summ', 'theirs'); + + if (!count($order) || !count($resultOrder)) { + throw new \InvalidArgumentException( + 'Parameters `order` & `resultOrder` must contains a data' + ); + } + + if (!in_array($technique, $techniques)) { + throw new \InvalidArgumentException( + 'Parameter `technique` must be on of ours|summ|theirs' + ); + } + + return $this->client->makeRequest( + '/orders/combine', + WC_Retailcrm_Request::METHOD_POST, + array( + 'technique' => $technique, + 'order' => json_encode($order), + 'resultOrder' => json_encode($resultOrder) + ) + ); + } + + /** + * Create an order payment + * + * @param array $payment order data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPaymentCreate(array $payment) + { + if (!count($payment)) { + throw new \InvalidArgumentException( + 'Parameter `payment` must contains a data' + ); + } + + return $this->client->makeRequest( + '/orders/payments/create', + WC_Retailcrm_Request::METHOD_POST, + array('payment' => json_encode($payment)) + ); + } + + /** + * Edit an order payment + * + * @param array $payment order data + * @param string $by by key + * @param null $site site code + * + * @return WC_Retailcrm_Response + */ + public function ordersPaymentEdit(array $payment, $by = 'externalId', $site = null) + { + if (!count($payment)) { + throw new \InvalidArgumentException( + 'Parameter `payment` must contains a data' + ); + } + + $this->checkIdParameter($by); + + if (!array_key_exists($by, $payment)) { + throw new \InvalidArgumentException( + sprintf('Order array must contain the "%s" parameter.', $by) + ); + } + + return $this->client->makeRequest( + sprintf('/orders/payments/%s/edit', $payment[$by]), + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite( + $site, + array('payment' => json_encode($payment), 'by' => $by) + ) + ); + } + + /** + * Edit an order payment + * + * @param string $id payment id + * + * @return WC_Retailcrm_Response + */ + public function ordersPaymentDelete($id) + { + if (!$id) { + throw new \InvalidArgumentException( + 'Parameter `id` must be set' + ); + } + + return $this->client->makeRequest( + sprintf('/orders/payments/%s/delete', $id), + WC_Retailcrm_Request::METHOD_POST + ); + } + + /** + * Returns filtered customers list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/customers', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create a customer + * + * @param array $customer customer data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersCreate(array $customer, $site = null) + { + if (! count($customer)) { + throw new \InvalidArgumentException( + 'Parameter `customer` must contains a data' + ); + } + + return $this->client->makeRequest( + '/customers/create', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('customer' => json_encode($customer))) + ); + } + + /** + * Save customer IDs' (id and externalId) association in the CRM + * + * @param array $ids ids mapping + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersFixExternalIds(array $ids) + { + if (! count($ids)) { + throw new \InvalidArgumentException( + 'Method parameter must contains at least one IDs pair' + ); + } + + return $this->client->makeRequest( + '/customers/fix-external-ids', + WC_Retailcrm_Request::METHOD_POST, + array('customers' => json_encode($ids)) + ); + } + + /** + * Upload array of the customers + * + * @param array $customers array of customers + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersUpload(array $customers, $site = null) + { + if (! count($customers)) { + throw new \InvalidArgumentException( + 'Parameter `customers` must contains array of the customers' + ); + } + + return $this->client->makeRequest( + '/customers/upload', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('customers' => json_encode($customers))) + ); + } + + /** + * Get customer by id or externalId + * + * @param string $id customer identificator + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersGet($id, $by = 'externalId', $site = null) + { + $this->checkIdParameter($by); + + return $this->client->makeRequest( + "/customers/$id", + WC_Retailcrm_Request::METHOD_GET, + $this->fillSite($site, array('by' => $by)) + ); + } + + /** + * Edit a customer + * + * @param array $customer customer data + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersEdit(array $customer, $by = 'externalId', $site = null) + { + if (!count($customer)) { + throw new \InvalidArgumentException( + 'Parameter `customer` must contains a data' + ); + } + + $this->checkIdParameter($by); + + if (!array_key_exists($by, $customer)) { + throw new \InvalidArgumentException( + sprintf('Customer array must contain the "%s" parameter.', $by) + ); + } + + return $this->client->makeRequest( + sprintf('/customers/%s/edit', $customer[$by]), + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite( + $site, + array('customer' => json_encode($customer), 'by' => $by) + ) + ); + } + + /** + * Get customers history + * @param array $filter + * @param null $page + * @param null $limit + * + * @return WC_Retailcrm_Response + */ + public function customersHistory(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/customers/history', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Combine customers + * + * @param array $customers + * @param array $resultCustomer + * + * @return WC_Retailcrm_Response + */ + public function customersCombine(array $customers, $resultCustomer) + { + + if (!count($customers) || !count($resultCustomer)) { + throw new \InvalidArgumentException( + 'Parameters `customers` & `resultCustomer` must contains a data' + ); + } + + return $this->client->makeRequest( + '/customers/combine', + WC_Retailcrm_Request::METHOD_POST, + array( + 'customers' => json_encode($customers), + 'resultCustomer' => json_encode($resultCustomer) + ) + ); + } + + /** + * Returns filtered customers notes list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersNotesList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + return $this->client->makeRequest( + '/customers/notes', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create customer note + * + * @param array $note (default: array()) + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersNotesCreate($note, $site = null) + { + if (empty($note['customer']['id']) && empty($note['customer']['externalId'])) { + throw new \InvalidArgumentException( + 'Customer identifier must be set' + ); + } + return $this->client->makeRequest( + '/customers/notes/create', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('note' => json_encode($note))) + ); + } + + /** + * Delete customer note + * + * @param integer $id + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersNotesDelete($id) + { + if (empty($id)) { + throw new \InvalidArgumentException( + 'Note id must be set' + ); + } + return $this->client->makeRequest( + "/customers/notes/$id/delete", + WC_Retailcrm_Request::METHOD_POST + ); + } + + /** + * Get orders assembly list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/orders/packs', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create orders assembly + * + * @param array $pack pack data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksCreate(array $pack, $site = null) + { + if (!count($pack)) { + throw new \InvalidArgumentException( + 'Parameter `pack` must contains a data' + ); + } + + return $this->client->makeRequest( + '/orders/packs/create', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('pack' => json_encode($pack))) + ); + } + + /** + * Get orders assembly history + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/orders/packs/history', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get orders assembly by id + * + * @param string $id pack identificator + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksGet($id) + { + if (empty($id)) { + throw new \InvalidArgumentException('Parameter `id` must be set'); + } + + return $this->client->makeRequest( + "/orders/packs/$id", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Delete orders assembly by id + * + * @param string $id pack identificator + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksDelete($id) + { + if (empty($id)) { + throw new \InvalidArgumentException('Parameter `id` must be set'); + } + + return $this->client->makeRequest( + sprintf('/orders/packs/%s/delete', $id), + WC_Retailcrm_Request::METHOD_POST + ); + } + + /** + * Edit orders assembly + * + * @param array $pack pack data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksEdit(array $pack, $site = null) + { + if (!count($pack) || empty($pack['id'])) { + throw new \InvalidArgumentException( + 'Parameter `pack` must contains a data & pack `id` must be set' + ); + } + + return $this->client->makeRequest( + sprintf('/orders/packs/%s/edit', $pack['id']), + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('pack' => json_encode($pack))) + ); + } + + /** + * Get tasks list + * + * @param array $filter + * @param null $limit + * @param null $page + * + * @return WC_Retailcrm_Response + */ + public function tasksList(array $filter = array(), $limit = null, $page = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/tasks', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create task + * + * @param array $task + * @param null $site + * + * @return WC_Retailcrm_Response + * + */ + public function tasksCreate($task, $site = null) + { + if (!count($task)) { + throw new \InvalidArgumentException( + 'Parameter `task` must contain a data' + ); + } + + return $this->client->makeRequest( + "/tasks/create", + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite( + $site, + array('task' => json_encode($task)) + ) + ); + } + + /** + * Edit task + * + * @param array $task + * @param null $site + * + * @return WC_Retailcrm_Response + * + */ + public function tasksEdit($task, $site = null) + { + if (!count($task)) { + throw new \InvalidArgumentException( + 'Parameter `task` must contain a data' + ); + } + + return $this->client->makeRequest( + "/tasks/{$task['id']}/edit", + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite( + $site, + array('task' => json_encode($task)) + ) + ); + } + + /** + * Get custom dictionary + * + * @param $id + * + * @return WC_Retailcrm_Response + */ + public function tasksGet($id) + { + if (empty($id)) { + throw new \InvalidArgumentException( + 'Parameter `id` must be not empty' + ); + } + + return $this->client->makeRequest( + "/tasks/$id", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Get products groups + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storeProductsGroups(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/store/product-groups', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get purchace prices & stock balance + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storeInventories(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/store/inventories', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get store settings + * + * @param string $code get settings code + * + * @return WC_Retailcrm_Response + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function storeSettingsGet($code) + { + if (empty($code)) { + throw new \InvalidArgumentException('Parameter `code` must be set'); + } + + return $this->client->makeRequest( + "/store/setting/$code", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit store configuration + * + * @param array $configuration + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function storeSettingsEdit(array $configuration) + { + if (!count($configuration) || empty($configuration['code'])) { + throw new \InvalidArgumentException( + 'Parameter `configuration` must contains a data & configuration `code` must be set' + ); + } + + return $this->client->makeRequest( + sprintf('/store/setting/%s/edit', $configuration['code']), + WC_Retailcrm_Request::METHOD_POST, + $configuration + ); + } + + /** + * Upload store inventories + * + * @param array $offers offers data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storeInventoriesUpload(array $offers, $site = null) + { + if (!count($offers)) { + throw new \InvalidArgumentException( + 'Parameter `offers` must contains array of the offers' + ); + } + + return $this->client->makeRequest( + '/store/inventories/upload', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('offers' => json_encode($offers))) + ); + } + + /** + * Get products + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storeProducts(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/store/products', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get delivery settings + * + * @param string $code + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliverySettingsGet($code) + { + if (empty($code)) { + throw new \InvalidArgumentException('Parameter `code` must be set'); + } + + return $this->client->makeRequest( + "/delivery/generic/setting/$code", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit delivery configuration + * + * @param array $configuration + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function deliverySettingsEdit(array $configuration) + { + if (!count($configuration) || empty($configuration['code'])) { + throw new \InvalidArgumentException( + 'Parameter `configuration` must contains a data & configuration `code` must be set' + ); + } + + return $this->client->makeRequest( + sprintf('/delivery/generic/setting/%s/edit', $configuration['code']), + WC_Retailcrm_Request::METHOD_POST, + array('configuration' => json_encode($configuration)) + ); + } + + /** + * Delivery tracking update + * + * @param string $code + * @param array $statusUpdate + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function deliveryTracking($code, array $statusUpdate) + { + if (empty($code)) { + throw new \InvalidArgumentException('Parameter `code` must be set'); + } + + if (!count($statusUpdate)) { + throw new \InvalidArgumentException( + 'Parameter `statusUpdate` must contains a data' + ); + } + + return $this->client->makeRequest( + sprintf('/delivery/generic/%s/tracking', $code), + WC_Retailcrm_Request::METHOD_POST, + $statusUpdate + ); + } + + /** + * Returns available county list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function countriesList() + { + return $this->client->makeRequest( + '/reference/countries', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Returns deliveryServices list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliveryServicesList() + { + return $this->client->makeRequest( + '/reference/delivery-services', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit deliveryService + * + * @param array $data delivery service data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliveryServicesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/delivery-services/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('deliveryService' => json_encode($data)) + ); + } + + /** + * Returns deliveryTypes list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliveryTypesList() + { + return $this->client->makeRequest( + '/reference/delivery-types', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit deliveryType + * + * @param array $data delivery type data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliveryTypesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/delivery-types/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('deliveryType' => json_encode($data)) + ); + } + + /** + * Returns orderMethods list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function orderMethodsList() + { + return $this->client->makeRequest( + '/reference/order-methods', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit orderMethod + * + * @param array $data order method data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function orderMethodsEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/order-methods/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('orderMethod' => json_encode($data)) + ); + } + + /** + * Returns orderTypes list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function orderTypesList() + { + return $this->client->makeRequest( + '/reference/order-types', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit orderType + * + * @param array $data order type data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function orderTypesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/order-types/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('orderType' => json_encode($data)) + ); + } + + /** + * Returns paymentStatuses list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function paymentStatusesList() + { + return $this->client->makeRequest( + '/reference/payment-statuses', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit paymentStatus + * + * @param array $data payment status data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function paymentStatusesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/payment-statuses/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('paymentStatus' => json_encode($data)) + ); + } + + /** + * Returns paymentTypes list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function paymentTypesList() + { + return $this->client->makeRequest( + '/reference/payment-types', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit paymentType + * + * @param array $data payment type data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function paymentTypesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/payment-types/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('paymentType' => json_encode($data)) + ); + } + + /** + * Returns productStatuses list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function productStatusesList() + { + return $this->client->makeRequest( + '/reference/product-statuses', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit productStatus + * + * @param array $data product status data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function productStatusesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/product-statuses/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('productStatus' => json_encode($data)) + ); + } + + /** + * Returns sites list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function sitesList() + { + return $this->client->makeRequest( + '/reference/sites', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit site + * + * @param array $data site data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function sitesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/sites/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('site' => json_encode($data)) + ); + } + + /** + * Returns statusGroups list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function statusGroupsList() + { + return $this->client->makeRequest( + '/reference/status-groups', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Returns statuses list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function statusesList() + { + return $this->client->makeRequest( + '/reference/statuses', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit order status + * + * @param array $data status data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function statusesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/statuses/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('status' => json_encode($data)) + ); + } + + /** + * Returns stores list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storesList() + { + return $this->client->makeRequest( + '/reference/stores', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit store + * + * @param array $data site data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + if (!array_key_exists('name', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "name" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/stores/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('store' => json_encode($data)) + ); + } + + /** + * Get telephony settings + * + * @param string $code + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function telephonySettingsGet($code) + { + if (empty($code)) { + throw new \InvalidArgumentException('Parameter `code` must be set'); + } + + return $this->client->makeRequest( + "/telephony/setting/$code", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit telephony settings + * + * @param string $code symbolic code + * @param string $clientId client id + * @param boolean $active telephony activity + * @param mixed $name service name + * @param mixed $makeCallUrl service init url + * @param mixed $image service logo url(svg file) + * + * @param array $additionalCodes + * @param array $externalPhones + * @param bool $allowEdit + * @param bool $inputEventSupported + * @param bool $outputEventSupported + * @param bool $hangupEventSupported + * @param bool $changeUserStatusUrl + * + * @return WC_Retailcrm_Response + */ + public function telephonySettingsEdit( + $code, + $clientId, + $active = false, + $name = false, + $makeCallUrl = false, + $image = false, + $additionalCodes = array(), + $externalPhones = array(), + $allowEdit = false, + $inputEventSupported = false, + $outputEventSupported = false, + $hangupEventSupported = false, + $changeUserStatusUrl = false + ) + { + if (!isset($code)) { + throw new \InvalidArgumentException('Code must be set'); + } + + $parameters['code'] = $code; + + if (!isset($clientId)) { + throw new \InvalidArgumentException('client id must be set'); + } + + $parameters['clientId'] = $clientId; + + if (!isset($active)) { + $parameters['active'] = false; + } else { + $parameters['active'] = $active; + } + + if (!isset($name)) { + throw new \InvalidArgumentException('name must be set'); + } + + if (isset($name)) { + $parameters['name'] = $name; + } + + if (isset($makeCallUrl)) { + $parameters['makeCallUrl'] = $makeCallUrl; + } + + if (isset($image)) { + $parameters['image'] = $image; + } + + if (isset($additionalCodes)) { + $parameters['additionalCodes'] = $additionalCodes; + } + + if (isset($externalPhones)) { + $parameters['externalPhones'] = $externalPhones; + } + + if (isset($allowEdit)) { + $parameters['allowEdit'] = $allowEdit; + } + + if (isset($inputEventSupported)) { + $parameters['inputEventSupported'] = $inputEventSupported; + } + + if (isset($outputEventSupported)) { + $parameters['outputEventSupported'] = $outputEventSupported; + } + + if (isset($hangupEventSupported)) { + $parameters['hangupEventSupported'] = $hangupEventSupported; + } + + if (isset($changeUserStatusUrl)) { + $parameters['changeUserStatusUrl'] = $changeUserStatusUrl; + } + + return $this->client->makeRequest( + "/telephony/setting/$code/edit", + WC_Retailcrm_Request::METHOD_POST, + array('configuration' => json_encode($parameters)) + ); + } + + /** + * Call event + * + * @param string $phone phone number + * @param string $type call type + * @param array $codes + * @param string $hangupStatus + * @param string $externalPhone + * @param array $webAnalyticsData + * + * @return WC_Retailcrm_Response + * @internal param string $code additional phone code + * @internal param string $status call status + * + */ + public function telephonyCallEvent( + $phone, + $type, + $codes, + $hangupStatus, + $externalPhone = null, + $webAnalyticsData = array() + ) + { + if (!isset($phone)) { + throw new \InvalidArgumentException('Phone number must be set'); + } + + if (!isset($type)) { + throw new \InvalidArgumentException('Type must be set (in|out|hangup)'); + } + + if (empty($codes)) { + throw new \InvalidArgumentException('Codes array must be set'); + } + + $parameters['phone'] = $phone; + $parameters['type'] = $type; + $parameters['codes'] = $codes; + $parameters['hangupStatus'] = $hangupStatus; + $parameters['callExternalId'] = $externalPhone; + $parameters['webAnalyticsData'] = $webAnalyticsData; + + + return $this->client->makeRequest( + '/telephony/call/event', + WC_Retailcrm_Request::METHOD_POST, + array('event' => json_encode($parameters)) + ); + } + + /** + * Upload calls + * + * @param array $calls calls data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function telephonyCallsUpload(array $calls) + { + if (!count($calls)) { + throw new \InvalidArgumentException( + 'Parameter `calls` must contains array of the calls' + ); + } + + return $this->client->makeRequest( + '/telephony/calls/upload', + WC_Retailcrm_Request::METHOD_POST, + array('calls' => json_encode($calls)) + ); + } + + /** + * Get call manager + * + * @param string $phone phone number + * @param bool $details detailed information + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function telephonyCallManager($phone, $details) + { + if (!isset($phone)) { + throw new \InvalidArgumentException('Phone number must be set'); + } + + $parameters['phone'] = $phone; + $parameters['details'] = isset($details) ? $details : 0; + + return $this->client->makeRequest( + '/telephony/manager', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Update CRM basic statistic + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function statisticUpdate() + { + return $this->client->makeRequest( + '/statistic/update', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Return current site + * + * @return string + */ + public function getSite() + { + return $this->siteCode; + } + + /** + * Set site + * + * @param string $site site code + * + * @return void + */ + public function setSite($site) + { + $this->siteCode = $site; + } + + /** + * Check ID parameter + * + * @param string $by identify by + * + * @throws \InvalidArgumentException + * + * @return bool + */ + protected function checkIdParameter($by) + { + $allowedForBy = array( + 'externalId', + 'id' + ); + + if (!in_array($by, $allowedForBy, false)) { + throw new \InvalidArgumentException( + sprintf( + 'Value "%s" for "by" param is not valid. Allowed values are %s.', + $by, + implode(', ', $allowedForBy) + ) + ); + } + + return true; + } + + /** + * Fill params by site value + * + * @param string $site site code + * @param array $params input parameters + * + * @return array + */ + protected function fillSite($site, array $params) + { + if ($site) { + $params['site'] = $site; + } elseif ($this->siteCode) { + $params['site'] = $this->siteCode; + } + + return $params; + } + } diff --git a/retailcrm/include/api/class-wc-retailcrm-exception-curl.php b/retailcrm/include/api/class-wc-retailcrm-exception-curl.php index f22c34e..9d741e1 100644 --- a/retailcrm/include/api/class-wc-retailcrm-exception-curl.php +++ b/retailcrm/include/api/class-wc-retailcrm-exception-curl.php @@ -5,9 +5,9 @@ * * WC_Retailcrm_Exception_Curl class * - * @category Retailcrm + * @category RetailCRM * @package WC_Retailcrm_Exception_Curl - * @author Retailcrm + * @author RetailCRM * @license https://opensource.org/licenses/MIT MIT License * @link http://retailcrm.ru/docs/Developers/ApiVersion4 */ diff --git a/retailcrm/include/api/class-wc-retailcrm-exception-json.php b/retailcrm/include/api/class-wc-retailcrm-exception-json.php index acb8cfa..9f8f16c 100644 --- a/retailcrm/include/api/class-wc-retailcrm-exception-json.php +++ b/retailcrm/include/api/class-wc-retailcrm-exception-json.php @@ -5,11 +5,10 @@ * * WC_Retailcrm_Exception_Json class * - * @category Retailcrm + * @category RetailCRM * @package WC_Retailcrm_Exception_Json - * @author Retailcrm + * @author RetailCRM * @license https://opensource.org/licenses/MIT MIT License - * @link http://retailcrm.ru/docs/Developers/ApiVersion4 */ class WC_Retailcrm_Exception_Json extends \DomainException diff --git a/retailcrm/include/api/class-wc-retailcrm-proxy.php b/retailcrm/include/api/class-wc-retailcrm-proxy.php index ed8d763..d871666 100644 --- a/retailcrm/include/api/class-wc-retailcrm-proxy.php +++ b/retailcrm/include/api/class-wc-retailcrm-proxy.php @@ -1,10 +1,10 @@ logger = new WC_Logger(); - if ( ! class_exists( 'WC_Retailcrm_Client' ) ) { - include_once( __DIR__ . '/class-wc-retailcrm-client.php' ); + if ( ! class_exists( 'WC_Retailcrm_Client_V3' ) ) { + include_once( __DIR__ . '/class-wc-retailcrm-client-v3.php' ); + } + + if ( ! class_exists( 'WC_Retailcrm_Client_V4' ) ) { + include_once( __DIR__ . '/class-wc-retailcrm-client-v4.php' ); + } + + if ( ! class_exists( 'WC_Retailcrm_Client_V5' ) ) { + include_once( __DIR__ . '/class-wc-retailcrm-client-v5.php' ); } if ($api_url && $api_key) { - $this->retailcrm = new WC_Retailcrm_Client($api_url, $api_key); + switch ($api_vers) { + case 'v3': + $this->retailcrm = new WC_Retailcrm_Client_V3($api_url, $api_key); + break; + + case 'v4': + $this->retailcrm = new WC_Retailcrm_Client_V4($api_url, $api_key); + break; + + case 'v5': + $this->retailcrm = new WC_Retailcrm_Client_V5($api_url, $api_key); + break; + } } } diff --git a/retailcrm/include/api/class-wc-retailcrm-request.php b/retailcrm/include/api/class-wc-retailcrm-request.php index 00cf9b3..53ce65c 100644 --- a/retailcrm/include/api/class-wc-retailcrm-request.php +++ b/retailcrm/include/api/class-wc-retailcrm-request.php @@ -7,9 +7,8 @@ * * @category Integration * @package WC_Retailcrm_Request - * @author Retailcrm + * @author RetailCRM * @license https://opensource.org/licenses/MIT MIT License - * @link http://retailcrm.ru/docs/Developers/ApiVersion4 */ if ( ! class_exists( 'WC_Retailcrm_Exception_Curl' ) ) { diff --git a/retailcrm/include/api/class-wc-retailcrm-response.php b/retailcrm/include/api/class-wc-retailcrm-response.php index c3d753c..3372e48 100644 --- a/retailcrm/include/api/class-wc-retailcrm-response.php +++ b/retailcrm/include/api/class-wc-retailcrm-response.php @@ -7,9 +7,8 @@ * * @category Integration * @package WC_Retailcrm_Response - * @author Retailcrm + * @author RetailCRM * @license https://opensource.org/licenses/MIT MIT License - * @link http://retailcrm.ru/docs/Developers/ApiVersion4 */ if ( ! class_exists( 'WC_Retailcrm_Exception_Json' ) ) { diff --git a/retailcrm/include/class-wc-retailcrm-base.php b/retailcrm/include/class-wc-retailcrm-base.php index 244da41..e8adae0 100644 --- a/retailcrm/include/class-wc-retailcrm-base.php +++ b/retailcrm/include/class-wc-retailcrm-base.php @@ -1,10 +1,10 @@ id = 'integration-retailcrm'; - $this->method_title = __( 'Retailcrm', 'woocommerce-integration-retailcrm' ); + $this->method_title = __( 'RetailCRM', 'woocommerce-integration-retailcrm' ); $this->method_description = __( 'Интеграция с системой управления Retailcrm.', 'woocommerce-integration-retailcrm' ); // Load the settings. @@ -42,7 +42,7 @@ if ( ! class_exists( 'WC_Retailcrm_Base' ) ) : public function init_form_fields() { $this->form_fields = array( - array( 'title' => __( 'Общие настройки', 'woocommerce' ), 'type' => 'title', 'desc' => '', 'id' => 'general_options' ), + array( 'title' => __( 'General Options', 'woocommerce' ), 'type' => 'title', 'desc' => '', 'id' => 'general_options' ), 'api_url' => array( 'title' => __( 'API URL', 'woocommerce-integration-retailcrm' ), @@ -52,7 +52,7 @@ if ( ! class_exists( 'WC_Retailcrm_Base' ) ) : 'default' => '' ), 'api_key' => array( - 'title' => __( 'API Ключ', 'woocommerce-integration-retailcrm' ), + 'title' => __( 'API Key', 'woocommerce-integration-retailcrm' ), 'type' => 'text', 'description' => __( 'Введите ключ API. Вы можете найти его в интерфейсе администратора Retailcrm.', 'woocommerce-integration-retailcrm' ), 'desc_tip' => true, @@ -60,6 +60,25 @@ if ( ! class_exists( 'WC_Retailcrm_Base' ) ) : ) ); + $api_version_list = array('v4' => 'v4','v5' => 'v5'); + + $this->form_fields[] = array( + 'title' => __( 'Настройки API', 'woocommerce' ), + 'type' => 'title', + 'description' => '', + 'id' => 'api_options' + ); + + $this->form_fields['api_version'] = array( + 'title' => __( 'API версия', 'textdomain' ), + 'description' => __( 'Выберите версию API, которую Вы хотите использовать', 'textdomain' ), + 'css' => 'min-width:50px;', + 'class' => 'select', + 'type' => 'select', + 'options' => $api_version_list, + 'desc_tip' => true, + ); + if ($this->get_option( 'api_url' ) != '' && $this->get_option( 'api_key' ) != '') { if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) { @@ -68,7 +87,8 @@ if ( ! class_exists( 'WC_Retailcrm_Base' ) ) : $retailcrm = new WC_Retailcrm_Proxy( $this->get_option( 'api_url' ), - $this->get_option( 'api_key' ) + $this->get_option( 'api_key' ), + $this->get_option( 'api_version') ); /** @@ -185,9 +205,9 @@ if ( ! class_exists( 'WC_Retailcrm_Base' ) ) : $this->form_fields['sync'] = array( 'label' => __( 'Выгружать остатки из CRM', 'textdomain' ), - 'title' => 'Остатки', + 'title' => 'Inventories', 'class' => 'checkbox', - 'type' => 'checkbox', + 'type' => 'checkbox'б 'description' => 'Отметьте данный пункт, если хотите выгружать остатки товаров из CRM в магазин.' ); @@ -244,6 +264,22 @@ if ( ! class_exists( 'WC_Retailcrm_Base' ) ) : statisticUpdate(); + + if (!$response->isSuccessful()) { + WC_Admin_Settings::add_error( esc_html__( '"Выбранная версия API недоступна"', 'woocommerce-integration-demo' ) ); + } + + return $value; + } } endif; diff --git a/retailcrm/include/class-wc-retailcrm-customers.php b/retailcrm/include/class-wc-retailcrm-customers.php index ca21839..98ad01c 100644 --- a/retailcrm/include/class-wc-retailcrm-customers.php +++ b/retailcrm/include/class-wc-retailcrm-customers.php @@ -1,10 +1,10 @@ retailcrm = new WC_Retailcrm_Proxy( $this->retailcrm_settings['api_url'], - $this->retailcrm_settings['api_key'] + $this->retailcrm_settings['api_key'], + $this->retailcrm_settings['api_version'] ); } diff --git a/retailcrm/include/class-wc-retailcrm-history.php b/retailcrm/include/class-wc-retailcrm-history.php index 558c4f2..06d7392 100644 --- a/retailcrm/include/class-wc-retailcrm-history.php +++ b/retailcrm/include/class-wc-retailcrm-history.php @@ -1,10 +1,10 @@ retailcrm = new WC_Retailcrm_Proxy( $this->retailcrm_settings['api_url'], - $this->retailcrm_settings['api_key'] + $this->retailcrm_settings['api_key'], + $this->retailcrm_settings['api_version'] ); $this->startDate = new DateTime(date('Y-m-d H:i:s', strtotime('-1 days', strtotime(date('Y-m-d H:i:s'))))); @@ -155,11 +156,11 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : } elseif($record['field'] == 'order_product.quantity' && $record['newValue']) { + $order = new WC_Order($record['order']['externalId']); $product = wc_get_product($record['item']['offer']['externalId']); $items = $order->get_items(); - $args = array('qty' => $record['newValue']); - + foreach ($items as $order_item_id => $item) { if ($item['variation_id'] != 0 ) { $offer_id = $item['variation_id']; @@ -167,8 +168,8 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $offer_id = $item['product_id']; } if ($offer_id == $record['item']['offer']['externalId']) { - $order->update_product($order_item_id, $product, $args); - + wc_delete_order_item($order_item_id); + $order->add_product($product, $record['newValue']); $this->update_total($order); } } @@ -185,8 +186,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $offer_id = $item['product_id']; } if ($offer_id == $record['item']['offer']['externalId']) { - wc_delete_order_item($order_item_id); - + wc_delete_order_item($order_item_id); $this->update_total($order); } } @@ -219,34 +219,22 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : elseif ($record['field'] == 'delivery_address.region') { $order = new WC_Order($record['order']['externalId']); - $address = array( - 'state' => $record['newValue'] - ); - $order->set_address($address, 'shipping'); + $order->set_shipping_state($record['newValue']); } elseif ($record['field'] == 'delivery_address.city') { $order = new WC_Order($record['order']['externalId']); - $address = array( - 'city' => $record['newValue'] - ); - $order->set_address($address, 'shipping'); + $order->set_shipping_city($record['newValue']); } elseif ($record['field'] == 'delivery_address.street') { $order = new WC_Order($record['order']['externalId']); - $address = array( - 'address_1' => $record['newValue'] - ); - $order->set_address($address, 'shipping'); + $order->set_shipping_address1($record['newValue']); } elseif ($record['field'] == 'delivery_address.building') { $order = new WC_Order($record['order']['externalId']); - $address = array( - 'address_2' => $record['newValue'] - ); - $order->set_address($address, 'shipping'); + $order->set_shipping_address2($record['newValue']); } elseif ($record['field'] == 'payment_type') { @@ -256,7 +244,40 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $payment = new WC_Payment_Gateways(); $payment_types = $payment->get_available_payment_gateways(); if (isset($payment_types[$options[$newValue]])) { - $order->set_payment_method($payment_types[$options[$newValue]]); + update_post_meta($order->get_id(), '_payment_method', $payment->id); + } + } + } + + elseif ($record['field'] == 'payments') { + $response = $this->retailcrm->ordersGet($record['order']['externalId']); + + if ($response->isSuccessful()) { + $order_data = $response['order']; + $order = new WC_Order($record['order']['externalId']); + $payment = new WC_Payment_Gateways(); + $payment_types = $payment->get_available_payment_gateways(); + + if (count($order_data['payments']) == 1) { + $paymentType = end($order_data['payments']); + if (isset($payment_types[$options[$paymentType['type']]])) { + $payment = $payment_types[$options[$paymentType['type']]]; + update_post_meta($order->get_id(), '_payment_method', $payment->id); + } + } else { + foreach ($order_data['payments'] as $payment_data) { + if (isset($payment_data['externalId'])) { + $paymentType = $payment_data; + } + } + + if (!isset($paymentType)) { + $paymentType = $order_data['payments'][0]; + } + + if (isset($payment_types[$options[$paymentType['type']]])) { + update_post_meta($order->get_id(), '_payment_method', $payment->id); + } } } } @@ -303,11 +324,26 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : 'country' => $order_record['customer']['address']['countryIso'] ); - if ($order_record['paymentType']) { - $payment = new WC_Payment_Gateways(); - $payment_types = $payment->get_available_payment_gateways(); - if (isset($payment_types[$options[$order_record['paymentType']]])) { - $order->set_payment_method($payment_types[$options[$order_record['paymentType']]]); + if ($this->retailcrm_settings['api_version'] == 'v5') { + if ($order_record['payments']) { + $payment = new WC_Payment_Gateways(); + + if (count($order_record['payments']) == 1) { + $payment_types = $payment->get_available_payment_gateways(); + $paymentType = end($order_record['payments']); + + if (isset($payment_types[$options[$paymentType['type']]])) { + $order->set_payment_method($payment_types[$options[$paymentType['type']]]); + } + } + } + } else { + if ($order_record['paymentType']) { + $payment = new WC_Payment_Gateways(); + $payment_types = $payment->get_available_payment_gateways(); + if (isset($payment_types[$options[$order_record['paymentType']]])) { + $order->set_payment_method($payment_types[$options[$order_record['paymentType']]]); + } } } @@ -358,30 +394,47 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : } protected function removeFuncsHook() - { - remove_action('woocommerce_order_status_changed', 'retailcrm_update_order_status', 11, 1); - remove_action('woocommerce_saved_order_items', 'retailcrm_update_order_items', 10, 2); - remove_action('update_post_meta', 'retailcrm_update_order', 11, 4); - remove_action('woocommerce_payment_complete', 'retailcrm_update_order_payment', 11, 1); - remove_action('woocommerce_checkout_update_user_meta', 'update_customer', 10, 2); + { + if (version_compare(get_option('woocommerce_db_version'), '3.0', '<' )) { + remove_action('woocommerce_order_status_changed', 'retailcrm_update_order_status', 11, 1); + remove_action('woocommerce_saved_order_items', 'retailcrm_update_order_items', 10, 2); + remove_action('update_post_meta', 'retailcrm_update_order', 11, 4); + remove_action('woocommerce_payment_complete', 'retailcrm_update_order_payment', 11, 1); + remove_action('woocommerce_checkout_update_user_meta', 'update_customer', 10, 2); + } else { + remove_action('woocommerce_update_order', 'update_order', 11, 1); + remove_action('woocommerce_order_status_changed', 'retailcrm_update_order_status', 11, 1); + } } protected function addFuncsHook() - { - if (!has_action('woocommerce_checkout_update_user_meta', 'update_customer')) { - add_action('woocommerce_checkout_update_user_meta', 'update_customer', 10, 2); - } - if (!has_action('woocommerce_order_status_changed', 'retailcrm_update_order_status')) { - add_action('woocommerce_order_status_changed', 'retailcrm_update_order_status', 11, 1); - } - if (!has_action('woocommerce_saved_order_items', 'retailcrm_update_order_items')) { - add_action('woocommerce_saved_order_items', 'retailcrm_update_order_items', 10, 2); - } - if (!has_action('update_post_meta', 'retailcrm_update_order')) { - add_action('update_post_meta', 'retailcrm_update_order', 11, 4); - } - if (!has_action('woocommerce_payment_complete', 'retailcrm_update_order_payment')) { - add_action('woocommerce_payment_complete', 'retailcrm_update_order_payment', 11, 1); + { + if (version_compare(get_option('woocommerce_db_version'), '3.0', '<' )) { + if (!has_action('woocommerce_checkout_update_user_meta', 'update_customer')) { + add_action('woocommerce_checkout_update_user_meta', 'update_customer', 10, 2); + } + if (!has_action('woocommerce_order_status_changed', 'retailcrm_update_order_status')) { + add_action('woocommerce_order_status_changed', 'retailcrm_update_order_status', 11, 1); + } + if (!has_action('woocommerce_saved_order_items', 'retailcrm_update_order_items')) { + add_action('woocommerce_saved_order_items', 'retailcrm_update_order_items', 10, 2); + } + if (!has_action('update_post_meta', 'retailcrm_update_order')) { + add_action('update_post_meta', 'retailcrm_update_order', 11, 4); + } + if (!has_action('woocommerce_payment_complete', 'retailcrm_update_order_payment')) { + add_action('woocommerce_payment_complete', 'retailcrm_update_order_payment', 11, 1); + } + } else { + if (!has_action('woocommerce_update_order', 'update_order')) { + add_action('woocommerce_update_order', 'update_order', 11, 1); + } + if (!has_action('woocommerce_checkout_update_user_meta', 'update_customer')) { + add_action('woocommerce_checkout_update_user_meta', 'update_customer', 10, 2); + } + if (!has_action('woocommerce_order_status_changed', 'retailcrm_update_order_status')) { + add_action('woocommerce_order_status_changed', 'retailcrm_update_order_status', 11, 1); + } } } @@ -400,7 +453,6 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : protected function update_total($order) { - $order->update_taxes(); $order->calculate_totals(); } } diff --git a/retailcrm/include/class-wc-retailcrm-icml.php b/retailcrm/include/class-wc-retailcrm-icml.php index e852a3f..67b22dc 100644 --- a/retailcrm/include/class-wc-retailcrm-icml.php +++ b/retailcrm/include/class-wc-retailcrm-icml.php @@ -1,10 +1,10 @@ post_parent)) { - $product = new WC_Product_Variation($theid); - $parent = new WC_Product_Simple($product->get_parent_id()); - } - } - } - - if ($this->get_parent_product($product) > 0) { - $image = wp_get_attachment_image_src( get_post_thumbnail_id( $parent->get_id() ), 'single-post-thumbnail' ); - $term_list = wp_get_post_terms($parent->get_id(), 'product_cat', array('fields' => 'ids')); - $attributes = get_post_meta( $parent->get_id() , '_product_attributes' ); - } else { - $image = wp_get_attachment_image_src( get_post_thumbnail_id( $theid ), 'single-post-thumbnail' ); - $term_list = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'ids')); - $attributes = get_post_meta( $product->get_id() , '_product_attributes' ); - } - - $attributes = (isset($attributes[0])) ? $attributes[0] : $attributes; - - $params = array(); - - $weight = $product->get_weight(); - - if (!empty($weight)) { - $params[] = array('code' => 'weight', 'name' => 'Weight', 'value' => $weight); - } - - if (!empty($attributes)) { - foreach ($attributes as $attribute_name => $attribute) { - $attributeValue = get_post_meta($product->get_id(), 'attribute_'.$attribute_name); - $attributeValue = end($attributeValue); - if ($attribute['is_visible'] == 1 && !empty($attribute['value'])) { - $params[] = array( - 'code' => $attribute_name, - 'name' => $attribute['name'], - 'value' => $attributeValue - ); + $product = wc_get_product($theid); + $parent = wc_get_product($product->get_parent_id()); + } else { + continue; } } } - if ($product->get_sku() != '') { - $params[] = array('code' => 'sku', 'name' => 'SKU', 'value' => $product->get_sku()); + if ($product->get_type() == 'variable') continue; + + if ((isset($parent) && $parent->get_type() == 'variable') || $product->get_type() == 'simple') { + if ($this->get_parent_product($product) > 0) { + $image = wp_get_attachment_image_src( get_post_thumbnail_id( $theid ), 'single-post-thumbnail' ); + $term_list = wp_get_post_terms($parent->get_id(), 'product_cat', array('fields' => 'ids')); + $attributes = get_post_meta( $parent->get_id() , '_product_attributes' ); + } else { + $image = wp_get_attachment_image_src( get_post_thumbnail_id( $theid ), 'single-post-thumbnail' ); + $term_list = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'ids')); + $attributes = get_post_meta( $product->get_id() , '_product_attributes' ); + } + + $attributes = (isset($attributes[0])) ? $attributes[0] : $attributes; + + $params = array(); + + $weight = $product->get_weight(); + + if (!empty($weight)) { + $params[] = array('code' => 'weight', 'name' => 'Weight', 'value' => $weight); + } + + $attrName = ''; + + if (!empty($attributes)) { + foreach ($attributes as $attribute_name => $attribute) { + $attributeValue = get_post_meta($product->get_id(), 'attribute_'.$attribute_name); + $attributeValue = end($attributeValue); + if ($attribute['is_visible'] == 1 && !empty($attribute['value'])) { + $params[] = array( + 'code' => $attribute_name, + 'name' => $attribute['name'], + 'value' => $attributeValue + ); + + $attrName .= (!empty($attributeValue)) ? ' - ' . $attributeValue : ''; + } + } + } + + if ($product->get_sku() != '') { + $params[] = array('code' => 'sku', 'name' => 'SKU', 'value' => $product->get_sku()); + } + + $name = ($post->post_title == $product->get_title()) ? + $post->post_title . $attrName : + $post->post_title; + + $product_data = array( + 'id' => $product->get_id(), + 'productId' => ($this->get_parent_product($product) > 0) ? $parent->get_id() : $product->get_id(), + 'name' => $name, + 'productName' => ($this->get_parent_product($product) > 0) ? $parent->get_title() : $product->get_title(), + 'price' => $this->get_price_with_tax($product), + 'purchasePrice' => $product->get_regular_price(), + 'picture' => $image[0], + 'url' => ($this->get_parent_product($product) > 0) ? $parent->get_permalink() : $product->get_permalink(), + 'quantity' => is_null($product->get_stock_quantity()) ? 0 : $product->get_stock_quantity(), + 'categoryId' => $term_list + ); + + if (!empty($params)) { + $product_data['params'] = $params; + } + + $full_product_list[$theid] = $product_data; + unset($product_data); } - - $product_data = array( - 'id' => $product->get_id(), - 'productId' => ($this->get_parent_product($product) > 0) ? $parent->get_id() : $product->get_id(), - 'name' => $product->get_title(), - 'productName' => ($this->get_parent_product($product) > 0) ? $parent->get_title() : $product->get_title(), - 'price' => $this->get_price_with_tax($product), - 'purchasePrice' => $product->get_regular_price(), - 'picture' => $image[0], - 'url' => ($this->get_parent_product($product) > 0) ? $parent->get_permalink() : $product->get_permalink(), - 'quantity' => is_null($product->get_stock_quantity()) ? 0 : $product->get_stock_quantity(), - 'categoryId' => $term_list - ); - - if (!empty($params)) { - $product_data['params'] = $params; - } - - $full_product_list[] = $product_data; - unset($product_data); endwhile; return $full_product_list; diff --git a/retailcrm/include/class-wc-retailcrm-inventories.php b/retailcrm/include/class-wc-retailcrm-inventories.php index f4a5efc..ab89a63 100644 --- a/retailcrm/include/class-wc-retailcrm-inventories.php +++ b/retailcrm/include/class-wc-retailcrm-inventories.php @@ -1,10 +1,10 @@ retailcrm = new WC_Retailcrm_Proxy( $this->retailcrm_settings['api_url'], - $this->retailcrm_settings['api_key'] + $this->retailcrm_settings['api_key'], + $this->retailcrm_settings['api_version'] ); } diff --git a/retailcrm/include/class-wc-retailcrm-orders.php b/retailcrm/include/class-wc-retailcrm-orders.php index 887e170..c66e813 100644 --- a/retailcrm/include/class-wc-retailcrm-orders.php +++ b/retailcrm/include/class-wc-retailcrm-orders.php @@ -1,10 +1,10 @@ retailcrm = new WC_Retailcrm_Proxy( $this->retailcrm_settings['api_url'], - $this->retailcrm_settings['api_key'] + $this->retailcrm_settings['api_key'], + $this->retailcrm_settings['api_version'] ); } @@ -90,8 +91,8 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $order_data['customer']['externalId'] = $search['customer']['externalId']; } } - - $this->retailcrm->ordersCreate($order_data); + + $res = $this->retailcrm->ordersCreate($order_data); } /** @@ -128,12 +129,34 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : */ public function orderUpdatePaymentType($order_id, $payment_method) { - $order_data = array( - 'externalId' => $order_id, - 'paymentType' => $this->retailcrm_settings[$payment_method] - ); + if ($this->retailcrm_settings['api_version'] != 'v5') { + $order_data = array( + 'externalId' => $order_id, + 'paymentType' => $this->retailcrm_settings[$payment_method] + ); - $response = $this->retailcrm->ordersEdit($order_data); + $this->retailcrm->ordersEdit($order_data); + } else { + $response = $this->retailcrm->ordersGet($order_id); + + if ($response->isSuccessful()) $order = $response['order']; + + foreach ($order['payments'] as $payment_data) { + if ($payment_data['externalId'] == $order_id) { + $payment = $payment_data; + } + } + + $order = new WC_Order($order_id); + + if (isset($payment) && $payment['type'] != $this->retailcrm_settings[$order->payment_method]) { + $response = $this->retailcrm->ordersPaymentDelete($payment['id']); + + if ($response->isSuccessful()) { + $this->createPayment($order, $order_id); + } + } + } } /** @@ -142,14 +165,23 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : * @param $order_id */ public function orderUpdatePayment($order_id) { - $order = new WC_Order( $order_id ); - $order_data = array( - 'externalId' => $order_id, - 'paymentStatus' => 'paid' - ); + if ($this->retailcrm_settings['api_version'] != 'v5') { + $order_data = array( + 'externalId' => $order_id, + 'paymentStatus' => 'paid' + ); - $response = $this->retailcrm->ordersEdit($order_data); + $this->retailcrm->ordersEdit($order_data); + } else { + $payment = array( + 'externalId' => $order_id, + 'status' => 'paid' + ); + + $this->retailcrm->ordersPaymentsEdit($payment); + } + } /** @@ -174,12 +206,22 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : } $_product = wc_get_product($offer_id); - $items[] = array( - 'offer' => array('externalId' => $offer_id), - 'productName' => $product['name'], - 'initialPrice' => (float)$_product->get_price(), - 'quantity' => $product['qty'] - ); + + if ($this->retailcrm_settings['api_version'] != 'v3') { + $items[] = array( + 'offer' => array('externalId' => $offer_id), + 'productName' => $product['name'], + 'initialPrice' => (float)$_product->get_price(), + 'quantity' => $product['qty'] + ); + } else { + $items[] = array( + 'productId' => $offer_id, + 'productName' => $product['name'], + 'initialPrice' => (float)$_product->get_price(), + 'quantity' => $product['qty'] + ); + } } $order_data['items'] = $items; @@ -209,7 +251,15 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $order_data['number'] = $order->get_order_number(); $order_data['createdAt'] = $order->order_date; - if (!empty($order->payment_method) && !empty($this->retailcrm_settings[$order->payment_method])) { + if ($this->retailcrm_settings['api_version'] == 'v5') { + $discount = $order->data['discount_total'] + $order->data['discount_tax']; + if ($discount > 0) $order_data['discountManualAmount'] = $discount; + } else { + $discount = $order->data['discount_total'] + $order->data['discount_tax']; + if ($discount > 0) $order_data['discount'] = $discount; + } + + if (!empty($order->payment_method) && !empty($this->retailcrm_settings[$order->payment_method]) && $this->retailcrm_settings['api_version'] != 'v5') { $order_data['paymentType'] = $this->retailcrm_settings[$order->payment_method]; } @@ -228,7 +278,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : } } - if ($order->is_paid()) { + if ($this->retailcrm_settings['api_version'] != 'v5' && $order->is_paid()) { $order_data['paymentStatus'] = 'paid'; } @@ -243,15 +293,14 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : if (!empty($user_data['last_name'])) $order_data['lastName'] = $user_data['last_name']; if (!empty($user_data['phone'])) $order_data['phone'] = $user_data['phone']; if (!empty($user_data['email'])) $order_data['email'] = $user_data['email']; + if (!empty($user_data['postcode'])) $order_data['delivery']['address']['index'] = $user_data['postcode']; - if (!empty($user_data['state'])) $order_data['delivery']['address']['region'] = $user_data['state']; if (!empty($user_data['city'])) $order_data['delivery']['address']['city'] = $user_data['city']; if (!empty($user_data['country'])) $order_data['delivery']['address']['countryIso'] = $user_data['country']; $order_data['delivery']['address']['text'] = sprintf( - "%s %s %s %s %s", + "%s %s %s %s", $user_data['postcode'], - $user_data['state'], $user_data['city'], $user_data['address_1'], $user_data['address_2'] @@ -265,12 +314,21 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $_product = wc_get_product($uid); if ($_product) { - $order_item = array( - 'offer' => array('externalId' => $uid), - 'productName' => $item['name'], - 'initialPrice' => (float)$_product->get_price(), - 'quantity' => $item['qty'], - ); + if ($this->retailcrm_settings['api_version'] != 'v3') { + $order_item = array( + 'offer' => array('externalId' => $uid), + 'productName' => $item['name'], + 'initialPrice' => (float)$_product->get_price(), + 'quantity' => $item['qty'], + ); + } else { + $order_item = array( + 'productId' => $uid, + 'productName' => $item['name'], + 'initialPrice' => (float)$_product->get_price(), + 'quantity' => $item['qty'], + ); + } } $order_items[] = $order_item; @@ -278,7 +336,73 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $order_data['items'] = $order_items; + if ($this->retailcrm_settings['api_version'] == 'v5') { + $payment = array( + 'amount' => $order->get_total(), + 'externalId' => $order_id + ); + + $payment['order'] = array( + 'externalId' => $order_id + ); + + if (!empty($order->payment_method) && !empty($this->retailcrm_settings[$order->payment_method])) { + $payment['type'] = $this->retailcrm_settings[$order->payment_method]; + } + + if ($order->is_paid()) { + $payment['status'] = 'paid'; + } + + if ($order->get_date_paid()) { + $pay_date = $order->get_date_paid(); + $payment['paidAt'] = $pay_date->date('Y-m-d H:i:s'); + } + + $order_data['payments'][] = $payment; + } + return $order_data; } + + protected function createPayment($order, $order_id) + { + $payment = array( + 'amount' => $order->get_total(), + 'externalId' => $order_id + ); + + $payment['order'] = array( + 'externalId' => $order_id + ); + + if (!empty($order->payment_method) && !empty($this->retailcrm_settings[$order->payment_method])) { + $payment['type'] = $this->retailcrm_settings[$order->payment_method]; + } + + if ($order->is_paid()) { + $payment['status'] = 'paid'; + } + + if ($order->get_date_paid()) { + $pay_date = $order->get_date_paid(); + $payment['paidAt'] = $pay_date->date('Y-m-d H:i:s'); + } + + $this->retailcrm->ordersPaymentCreate($payment); + } + + public function updateOrder($order_id) + { + $order = $this->processOrder($order_id); + + $response = $this->retailcrm->ordersEdit($order); + + $order = new WC_Order($order_id); + + if ($response->isSuccessful()) { + $this->orderUpdatePaymentType($order_id, $order->payment_method); + } + } } endif; diff --git a/retailcrm/retailcrm.php b/retailcrm/retailcrm.php index a1a8b82..564cff6 100644 --- a/retailcrm/retailcrm.php +++ b/retailcrm/retailcrm.php @@ -1,9 +1,9 @@ array( 'interval' => 900, // seconds - 'display' => __('Every 1 hour') + 'display' => __('Every 15 minutes') ) ); } @@ -293,7 +293,7 @@ function upload_to_crm() { } $options = array_filter(get_option( 'woocommerce_integration-retailcrm_settings' )); - + $orders = new WC_Retailcrm_Orders(); $customers = new WC_Retailcrm_Customers(); $customers->customersUpload(); @@ -320,6 +320,15 @@ function ajax_upload() { updateOrder($order_id); +} + register_activation_hook( __FILE__, 'retailcrm_install' ); register_deactivation_hook( __FILE__, 'retailcrm_deactivation' ); @@ -327,10 +336,6 @@ if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_ load_plugin_textdomain('wc_retailcrm', false, dirname(plugin_basename( __FILE__ )) . '/'); add_filter('cron_schedules', 'filter_cron_schedules', 10, 1); add_action('woocommerce_thankyou', 'retailcrm_process_order', 10, 1); - add_action('woocommerce_order_status_changed', 'retailcrm_update_order_status', 11, 1); - add_action('woocommerce_saved_order_items', 'retailcrm_update_order_items', 10, 2); - add_action('update_post_meta', 'retailcrm_update_order', 11, 4); - add_action('woocommerce_payment_complete', 'retailcrm_update_order_payment', 11, 1); add_action('retailcrm_history', 'retailcrm_history_get'); add_action('retailcrm_icml', 'generate_icml'); add_action('retailcrm_inventories', 'load_stocks'); @@ -341,4 +346,13 @@ if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_ add_action('admin_print_footer_scripts', 'ajax_upload', 99); add_action( 'woocommerce_created_customer', 'create_customer', 10, 1 ); add_action( 'woocommerce_checkout_update_user_meta', 10, 2 ); + + if (version_compare(get_option('woocommerce_db_version'), '3.0', '<' )) { + add_action('woocommerce_order_status_changed', 'retailcrm_update_order_status', 11, 1); + add_action('woocommerce_saved_order_items', 'retailcrm_update_order_items', 10, 2); + add_action('update_post_meta', 'retailcrm_update_order', 11, 4); + add_action('woocommerce_payment_complete', 'retailcrm_update_order_payment', 11, 1); + } else { + add_action('woocommerce_update_order', 'update_order', 11, 1); + } } diff --git a/retailcrm/uninstall.php b/retailcrm/uninstall.php index a2bce27..84d9813 100644 --- a/retailcrm/uninstall.php +++ b/retailcrm/uninstall.php @@ -15,7 +15,7 @@ * * * @link https://wordpress.org/plugins/retailcrm/ - * @since 1.0 + * @since 1.1 * * @package RetailCRM */