Confinguration sync, user & order creation

This commit is contained in:
Alex Lushpai 2014-05-08 20:11:28 +04:00
parent 741d079a72
commit 7fd1bceb34
3 changed files with 537 additions and 111 deletions

View File

@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>intarocrm</name>
<displayName><![CDATA[IntaroCRM]]></displayName>
<version><![CDATA[0.1]]></version>
<description><![CDATA[Integration module for IntaroCRM]]></description>
<author><![CDATA[Intaro Ltd.]]></author>
<tab><![CDATA[market_place]]></tab>
<confirmUninstall>Are you sure you want to uninstall?</confirmUninstall>
<is_configurable>1</is_configurable>
<need_instance>0</need_instance>
<limited_countries></limited_countries>
</module>
<module>
<name>intarocrm</name>
<displayName><![CDATA[IntaroCRM]]></displayName>
<version><![CDATA[0.1]]></version>
<description><![CDATA[Модуль интеграции с IntaroCRM]]></description>
<author><![CDATA[Intaro Ltd.]]></author>
<tab><![CDATA[market_place]]></tab>
<confirmUninstall>Вы уверены, что хотите удалить модуль?</confirmUninstall>
<is_configurable>1</is_configurable>
<need_instance>1</need_instance>
<limited_countries></limited_countries>
</module>

View File

@ -19,24 +19,37 @@ class IntaroCRM extends Module
$this->description = $this->l('Integration module for IntaroCRM');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
$this->intaroCRM = new \IntaroCrm\RestApi(
Configuration::get('INTAROCRM_ADDRESS'),
Configuration::get('INTAROCRM_API_TOKEN')
);
parent::__construct();
}
function install()
{
return (parent::install());
return (
parent::install() &&
$this->registerHook('newOrder') &&
$this->registerHook('actionOrderStatusPostUpdate')
);
}
function uninstall()
{
return parent::uninstall() &&
Configuration::deleteByName('INTAROCRM_ADDRESS') &&
Configuration::deleteByName('INTAROCRM_API_TOKEN')
Configuration::deleteByName('INTAROCRM_API_TOKEN') &&
Configuration::deleteByName('INTAROCRM_API_STATUS') &&
Configuration::deleteByName('INTAROCRM_API_DELIVERY') &&
Configuration::deleteByName('INTAROCRM_API_ADDR')
;
}
public function getContent()
{
$output = null;
$address = Configuration::get('INTAROCRM_ADDRESS');
@ -52,6 +65,10 @@ class IntaroCRM extends Module
{
$address = strval(Tools::getValue('INTAROCRM_ADDRESS'));
$token = strval(Tools::getValue('INTAROCRM_API_TOKEN'));
$delivery = json_encode(Tools::getValue('INTAROCRM_API_DELIVERY'));
$status = json_encode(Tools::getValue('INTAROCRM_API_STATUS'));
$payment = json_encode(Tools::getValue('INTAROCRM_API_PAYMENT'));
$order_address = json_encode(Tools::getValue('INTAROCRM_API_ADDR'));
if (!$address || empty($address) || !Validate::isGenericName($address)) {
$output .= $this->displayError( $this->l('Invalid crm address') );
@ -60,9 +77,14 @@ class IntaroCRM extends Module
} else {
Configuration::updateValue('INTAROCRM_ADDRESS', $address);
Configuration::updateValue('INTAROCRM_API_TOKEN', $token);
Configuration::updateValue('INTAROCRM_API_DELIVERY', $delivery);
Configuration::updateValue('INTAROCRM_API_STATUS', $status);
Configuration::updateValue('INTAROCRM_API_PAYMENT', $payment);
Configuration::updateValue('INTAROCRM_API_ADDR', $order_address);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
$this->display(__FILE__, 'intarocrm.tpl');
return $output.$this->displayForm();
}
@ -73,10 +95,7 @@ class IntaroCRM extends Module
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$intaroCrm = new \IntaroCrm\RestApi(
Configuration::get('INTAROCRM_ADDRESS'),
Configuration::get('INTAROCRM_API_TOKEN')
);
$intaroCrm = $this->intaroCRM;
/*
* Network connection form
@ -108,114 +127,50 @@ class IntaroCRM extends Module
);
/*
* Arrays for dictionaries
* Delivery
*/
$delivery_dict = array();
$crmDeliveryTypes = array();
$status_dict = array();
$crmStatusTypes = array();
/*
* Delivery dictionary form
*/
try {
$deliveryTypes = $intaroCrm->deliveryTypesList();
}
catch (\IntaroCrm\Exception\CurlException $e) {
error_log('deliveryTypesList: connection error');
}
catch (\IntaroCrm\Exception\ApiException $e) {
error_log('deliveryTypesList: ' . $e->getMessage());
}
if (!empty($deliveryTypes)) {
$crmDeliveryTypes[] = array();
foreach ($deliveryTypes as $dType) {
$crmDeliveryTypes[] = array(
'id_option' => $dType['code'],
'name' => $dType['name']
);
}
}
$carriers = Carrier::getCarriers(
$default_lang, true, false, false,
null, PS_CARRIERS_AND_CARRIER_MODULES_NEED_RANGE
);
if (!empty($carriers)) {
foreach ($carriers as $carrier) {
$delivery_dict[] = array(
'type' => 'select',
'label' => $carrier['name'],
'name' => 'carrier_' . $carrier['id_carrier'],
'required' => false,
'options' => array(
'query' => $crmDeliveryTypes,
'id' => 'id_option',
'name' => 'name'
)
);
}
}
$fields_form[1]['form'] = array(
'legend' => array(
'title' => $this->l('Delivery'),
),
'input' => $delivery_dict,
'input' => $this->getDeliveryTypes($default_lang, $intaroCrm),
);
/*
* Status dictionary form
* Order status
*/
try {
$statusTypes = $intaroCrm->orderStatusesList();
}
catch (\IntaroCrm\Exception\CurlException $e) {
error_log('statusTypesList: connection error');
}
catch (\IntaroCrm\Exception\ApiException $e) {
error_log('statusTypesList: ' . $e->getMessage());
}
if (!empty($statusTypes)) {
$crmStatusTypes[] = array();
foreach ($statusTypes as $sType) {
$crmStatusTypes[] = array(
'id_option' => $sType['code'],
'name' => $sType['name']
);
}
}
$states = OrderState::getOrderStates($default_lang, true);
if (!empty($states)) {
foreach ($states as $state) {
if ($state['name'] != ' ') {
$status_dict[] = array(
'type' => 'select',
'label' => $state['name'],
'name' => 'state_' . $state['id_order_state'],
'required' => false,
'options' => array(
'query' => $crmStatusTypes,
'id' => 'id_option',
'name' => 'name'
)
);
}
}
}
$fields_form[2]['form'] = array(
'legend' => array(
'title' => $this->l('Order statuses'),
),
'input' => $status_dict,
'input' => $this->getStatusTypes($default_lang, $intaroCrm),
);
/*
* Payment
*/
$fields_form[3]['form'] = array(
'legend' => array(
'title' => $this->l('Payment types'),
),
'input' => $this->getPaymentTypes($intaroCrm),
);
/*
* Address fields
*/
$fields_form[4]['form'] = array(
'legend' => array(
'title' => $this->l('Address'),
),
'input' => $this->getAddressFields()
);
/*
* Diplay forms
*/
$helper = new HelperForm();
$helper->module = $this;
@ -246,6 +201,459 @@ class IntaroCRM extends Module
$helper->fields_value['INTAROCRM_ADDRESS'] = Configuration::get('INTAROCRM_ADDRESS');
$helper->fields_value['INTAROCRM_API_TOKEN'] = Configuration::get('INTAROCRM_API_TOKEN');
$deliverySettings = Configuration::get('INTAROCRM_API_DELIVERY');
if (isset($deliverySettings) && $deliverySettings != '')
{
$deliveryTypes = json_decode($deliverySettings);
foreach ($deliveryTypes as $idx => $delivery) {
$name = 'INTAROCRM_API_DELIVERY[' . $idx . ']';
$helper->fields_value[$name] = $delivery;
}
}
$statusSettings = Configuration::get('INTAROCRM_API_STATUS');
if (isset($statusSettings) && $statusSettings != '')
{
$statusTypes = json_decode($statusSettings);
foreach ($statusTypes as $idx => $status) {
$name = 'INTAROCRM_API_STATUS[' . $idx . ']';
$helper->fields_value[$name] = $status;
}
}
$paymentSettings = Configuration::get('INTAROCRM_API_PAYMENT');
if (isset($paymentSettings) && $paymentSettings != '')
{
$paymentTypes = json_decode($paymentSettings);
foreach ($paymentTypes as $idx => $payment) {
$name = 'INTAROCRM_API_PAYMENT[' . $idx . ']';
$helper->fields_value[$name] = $payment;
}
}
$addressSettings = Configuration::get('INTAROCRM_API_ADDR');
if (isset($addressSettings) && $addressSettings != '')
{
$addressTypes = json_decode($addressSettings);
foreach ($addressTypes as $idx => $address) {
$name = 'INTAROCRM_API_ADDR[' . $idx . ']';
$helper->fields_value[$name] = $address;
}
}
return $helper->generateForm($fields_form);
}
public function hookNewOrder($params)
{
return $this->hookActionOrderStatusPostUpdate($params);
}
public function hookActionOrderStatusPostUpdate($params)
{
$address_id = Address::getFirstCustomerAddressId($params['cart']->id_customer);
$sql = 'SELECT * FROM '._DB_PREFIX_.'address WHERE id_address='.(int)$address_id;
$address = Db::getInstance()->ExecuteS($sql);
$address = $address[0];
$delivery = json_decode(Configuration::get('INTAROCRM_API_DELIVERY'));
$inCart = $params['cart']->getProducts();
if (isset($params['orderStatus'])) {
try {
$crmCustomerId = $this->intaroCRM->customerCreate(
array(
'externalId' => $params['cart']->id_customer,
'lastName' => $params['customer']->lastname,
'firstName' => $params['customer']->firstname,
'email' => $params['customer']->email,
'phones' => array(
array(
'number' => $address['phone'],
'type' => 'mobile'
),
array(
'number' => $address['phone_mobile'],
'type' => 'mobile'
)
),
'createdAt' => $params['customer']->date_add
)
);
}
catch (\IntaroCrm\Exception\CurlException $e) {
error_log('customerCreate: connection error');
}
catch (\IntaroCrm\Exception\ApiException $e) {
error_log('customerCreate: ' . $e->getMessage());
}
try {
$items = array();
foreach ($inCart as $item) {
$items[] = array(
'initialPrice' => $item['price'],
'quantity' => $item['quantity'],
'productId' => $item['id_product'],
'productName' => $item['name'],
'createdAt' => $item['date_add']
);
}
$dTypeKey = $params['cart']->id_carrier;
$this->intaroCRM->orderCreate(
array(
'externalId' => $params['order']->id,
'orderType' => 'eshop-individual',
'orderMethod' => 'shopping-cart',
'customerId' => $params['cart']->id_customer,
'firstName' => $params['customer']->firstname,
'lastName' => $params['customer']->lastname,
'phone' => $address['phone'],
'email' => $params['customer']->email,
'deliveryType' => $delivery->$dTypeKey,
'deliveryCost' => $params['order']->total_shipping,
'status' => 'new',
//'paymentType' => 'cash',
'deliveryAddress' => array(
'city' => $address['city'],
'index' => $address['postcode'],
'text' => $address['address1'],
),
'discount' => $params['order']->total_discounts,
'items' => $items,
'createdAt' => $params['order']->date_add
)
);
}
catch (\IntaroCrm\Exception\CurlException $e) {
error_log('orderCreate: connection error');
}
catch (\IntaroCrm\Exception\ApiException $e) {
error_log('orderCreate: ' . $e->getMessage());
}
}
if (isset($params['newOrderStatus']) && !empty($params['newOrderStatus'])) {
//var_dump($params['newOrderStatus']);
}
}
protected function getApiDeliveryTypes($intaroCrm)
{
$crmDeliveryTypes = array();
try {
$deliveryTypes = $intaroCrm->deliveryTypesList();
}
catch (\IntaroCrm\Exception\CurlException $e) {
error_log('deliveryTypesList: connection error');
}
catch (\IntaroCrm\Exception\ApiException $e) {
error_log('deliveryTypesList: ' . $e->getMessage());
}
if (!empty($deliveryTypes)) {
$crmDeliveryTypes[] = array();
foreach ($deliveryTypes as $dType) {
$crmDeliveryTypes[] = array(
'id_option' => $dType['code'],
'name' => $dType['name'],
);
}
}
return $crmDeliveryTypes;
}
protected function getDeliveryTypes($default_lang, $intaroCrm)
{
$deliveryTypes = array();
$carriers = Carrier::getCarriers(
$default_lang, true, false, false,
null, PS_CARRIERS_AND_CARRIER_MODULES_NEED_RANGE
);
if (!empty($carriers)) {
foreach ($carriers as $carrier) {
$deliveryTypes[] = array(
'type' => 'select',
'label' => $carrier['name'],
'name' => 'INTAROCRM_API_DELIVERY[' . $carrier['id_carrier'] . ']',
'required' => false,
'options' => array(
'query' => $this->getApiDeliveryTypes($intaroCrm),
'id' => 'id_option',
'name' => 'name'
)
);
}
}
return $deliveryTypes;
}
protected function getApiStatuses($intaroCrm)
{
$crmStatusTypes = array();
try {
$statusTypes = $intaroCrm->orderStatusesList();
}
catch (\IntaroCrm\Exception\CurlException $e) {
error_log('statusTypesList: connection error');
}
catch (\IntaroCrm\Exception\ApiException $e) {
error_log('statusTypesList: ' . $e->getMessage());
}
if (!empty($statusTypes)) {
$crmStatusTypes[] = array();
foreach ($statusTypes as $sType) {
$crmStatusTypes[] = array(
'id_option' => $sType['code'],
'name' => $sType['name']
);
}
}
return $crmStatusTypes;
}
protected function getStatusTypes($default_lang, $intaroCrm)
{
$statusTypes = array();
$states = OrderState::getOrderStates($default_lang, true);
if (!empty($states)) {
foreach ($states as $state) {
if ($state['name'] != ' ') {
$statusTypes[] = array(
'type' => 'select',
'label' => $state['name'],
'name' => 'INTAROCRM_API_STATUS[' . $state['id_order_state'] . ']',
'required' => false,
'options' => array(
'query' => $this->getApiStatuses($intaroCrm),
'id' => 'id_option',
'name' => 'name'
)
);
}
}
}
return $statusTypes;
}
protected function getApiPaymentTypes($intaroCrm)
{
$crmPaymentTypes = array();
try {
$paymentTypes = $intaroCrm->paymentTypesList();
}
catch (\IntaroCrm\Exception\CurlException $e) {
error_log('paymentTypesList: connection error');
}
catch (\IntaroCrm\Exception\ApiException $e) {
error_log('paymentTypesList: ' . $e->getMessage());
}
if (!empty($paymentTypes)) {
$crmPaymentTypes[] = array();
foreach ($paymentTypes as $pType) {
$crmPaymentTypes[] = array(
'id_option' => $pType['code'],
'name' => $pType['name']
);
}
}
return $crmPaymentTypes;
}
protected function getPaymentTypes($intaroCrm)
{
$payments = $this->getSystemPaymentModules();
$paymentTypes = array();
if (!empty($payments)) {
foreach ($payments as $payment) {
$paymentTypes[] = array(
'type' => 'select',
'label' => $payment['name'],
'name' => 'INTAROCRM_API_PAYMENT[' . $payment['id'] . ']',
'required' => false,
'options' => array(
'query' => $this->getApiPaymentTypes($intaroCrm),
'id' => 'id_option',
'name' => 'name'
)
);
}
}
return $paymentTypes;
}
protected function getSystemPaymentModules()
{
$shop_id = Context::getContext()->shop->id;
/* Get all modules then select only payment ones */
$modules = Module::getModulesOnDisk(true);
foreach ($modules as $module) {
if ($module->tab == 'payments_gateways')
{
if ($module->id)
{
if (!get_class($module) == 'SimpleXMLElement')
$module->country = array();
$countries = DB::getInstance()->executeS('
SELECT id_country
FROM '._DB_PREFIX_.'module_country
WHERE id_module = '.(int)$module->id.' AND `id_shop`='.(int)$shop_id
);
foreach ($countries as $country)
$module->country[] = $country['id_country'];
if (!get_class($module) == 'SimpleXMLElement')
$module->currency = array();
$currencies = DB::getInstance()->executeS('
SELECT id_currency
FROM '._DB_PREFIX_.'module_currency
WHERE id_module = '.(int)$module->id.' AND `id_shop`='.(int)$shop_id
);
foreach ($currencies as $currency)
$module->currency[] = $currency['id_currency'];
if (!get_class($module) == 'SimpleXMLElement')
$module->group = array();
$groups = DB::getInstance()->executeS('
SELECT id_group
FROM '._DB_PREFIX_.'module_group
WHERE id_module = '.(int)$module->id.' AND `id_shop`='.(int)$shop_id
);
foreach ($groups as $group)
$module->group[] = $group['id_group'];
}
else
{
$module->country = null;
$module->currency = null;
$module->group = null;
}
if ($module->active != 0) {
$this->payment_modules[] = array(
'id' => $module->id,
'name' => $module->displayName
);
}
}
}
return $this->payment_modules;
}
protected function getAddressFields()
{
$addressFields = array();
$address = explode(' ', str_replace("\n", ' ', AddressFormat::getAddressCountryFormat($this->context->country->id)));
if (!empty($address)) {
foreach ($address as $idx => $a) {
if (!strpos($a, ':')) {
$addressFields[] = array(
'type' => 'select',
'label' => $this->l((string)$a),
'name' => 'INTAROCRM_API_ADDR[' . $idx . ']',
'required' => false,
'options' => array(
'query' => array(
array(
'name' => '',
'id_option' => ''
),
array(
'name' => $this->l('FIRST_NAME'),
'id_option' => 'first_name'
),
array(
'name' => $this->l('LAST_NAME'),
'id_option' => 'last_name'
),
array(
'name' => $this->l('PHONE'),
'id_option' => 'phone'
),
array(
'name' => $this->l('EMAIL'),
'id_option' => 'email'
),
array(
'name' => $this->l('ADDRESS'),
'id_option' => 'address'
),
array(
'name' => $this->l('COUNTRY'),
'id_option' => 'country'
),
array(
'name' => $this->l('REGION'),
'id_option' => 'region'
),
array(
'name' => $this->l('CITY'),
'id_option' => 'city'
),
array(
'name' => $this->l('ZIP'),
'id_option' => 'index'
),
array(
'name' => $this->l('STREET'),
'id_option' => 'street'
),
array(
'name' => $this->l('BUILDING'),
'id_option' => 'building'
),
array(
'name' => $this->l('FLAT'),
'id_option' => 'flat'
),
array(
'name' => $this->l('INTERCOMCODE'),
'id_option' => 'intercomcode'
),
array(
'name' => $this->l('FLOOR'),
'id_option' => 'floor'
),
array(
'name' => $this->l('BLOCK'),
'id_option' => 'block'
),
array(
'name' => $this->l('HOUSE'),
'ID' => 'house'
)
),
'id' => 'id_option',
'name' => 'name'
)
);
}
}
}
return $addressFields;
}
}

View File

@ -14,4 +14,22 @@ $_MODULE['<{intarocrm}prestashop>intarocrm_7f775042e08eddee6bbfd8fbe0add4a3'] =
$_MODULE['<{intarocrm}prestashop>intarocrm_c9cc8cce247e49bae79f15173ce97354'] = 'Сохранить';
$_MODULE['<{intarocrm}prestashop>intarocrm_065ab3a28ca4f16f55f103adc7d0226f'] = 'Способы доставки';
$_MODULE['<{intarocrm}prestashop>intarocrm_33af8066d3c83110d4bd897f687cedd2'] = 'Статусы заказов';
$_MODULE['<{intarocrm}prestashop>intarocrm_bab959acc06bb03897b294fbb892be6b'] = 'Способы оплаты';
$_MODULE['<{intarocrm}prestashop>intarocrm_dd7bf230fde8d4836917806aff6a6b27'] = 'Адрес';
$_MODULE['<{intarocrm}prestashop>intarocrm_630f6dc397fe74e52d5189e2c80f282b'] = 'Вернуться к списку';
$_MODULE['<{intarocrm}prestashop>intarocrm_5c1cf6cfec2dad86c8ca5286a0294516'] = 'Имя';
$_MODULE['<{intarocrm}prestashop>intarocrm_c695cfe527a6fcd680114851b86b7555'] = 'Фамилия';
$_MODULE['<{intarocrm}prestashop>intarocrm_f9dd946cc89c1f3b41a0edbe0f36931d'] = 'Телефон';
$_MODULE['<{intarocrm}prestashop>intarocrm_61a649a33f2869e5e35fbb7aff3a80d9'] = 'Email';
$_MODULE['<{intarocrm}prestashop>intarocrm_2664f03ac6b8bb9eee4287720e407db3'] = 'Адрес';
$_MODULE['<{intarocrm}prestashop>intarocrm_6ddc09dc456001d9854e9fe670374eb2'] = 'Страна';
$_MODULE['<{intarocrm}prestashop>intarocrm_69aede266809f89b89fe70681f6a129f'] = 'Область/Край/Республика';
$_MODULE['<{intarocrm}prestashop>intarocrm_859214628431995197c0558f7b5f8ffc'] = 'Город';
$_MODULE['<{intarocrm}prestashop>intarocrm_4348f938bbddd8475e967ccb47ecb234'] = 'Почтовый индекс';
$_MODULE['<{intarocrm}prestashop>intarocrm_78fce82336bbbdca7f6da7564b8f9325'] = 'Улица';
$_MODULE['<{intarocrm}prestashop>intarocrm_71a6834884666147c0334f0c40bc7295'] = 'Дом/Строение';
$_MODULE['<{intarocrm}prestashop>intarocrm_f88a77e3d68d251c3dc4008c327b5a0c'] = 'Квартира';
$_MODULE['<{intarocrm}prestashop>intarocrm_d977f846d110fcb7f71c6f97330c9d10'] = 'Код домофона';
$_MODULE['<{intarocrm}prestashop>intarocrm_56c1e354d36beb85b0d881c5b2e24cbe'] = 'Этаж';
$_MODULE['<{intarocrm}prestashop>intarocrm_4d34f53389ed7f28ca91fc31ea360a66'] = 'Корпус';
$_MODULE['<{intarocrm}prestashop>intarocrm_49354b452ec305136a56fe7731834156'] = 'Дом/Строение';