From 55f262d3f57ebb4fde01ee3c890b13666c8c4504 Mon Sep 17 00:00:00 2001 From: Grisha Pomadchin Date: Tue, 16 Jul 2013 14:57:22 +0400 Subject: [PATCH] v 0.3f; +updated Agent; --- .../classes/general/ICrmOrderActions.php | 381 +++++++++--------- intaro.crm/install/step1.php | 4 +- intaro.crm/install/version.php | 4 +- 3 files changed, 191 insertions(+), 198 deletions(-) diff --git a/intaro.crm/classes/general/ICrmOrderActions.php b/intaro.crm/classes/general/ICrmOrderActions.php index 41fc6298..b75a0d1b 100755 --- a/intaro.crm/classes/general/ICrmOrderActions.php +++ b/intaro.crm/classes/general/ICrmOrderActions.php @@ -37,7 +37,8 @@ class ICrmOrderActions } /** - * Mass order uploading func + * Mass order uploading, without repeating; always returns true, but writes error log + * @return boolean */ public static function uploadOrders() { @@ -45,20 +46,20 @@ class ICrmOrderActions if (!CModule::IncludeModule('iblock')) { //handle err - self::eventLog('iblock', 'module not found'); - return false; + self::eventLog('ICrmOrderActions::uploadOrders', 'iblock', 'module not found'); + return true; } if (!CModule::IncludeModule("sale")) { //handle err - self::eventLog('sale', 'module not found'); - return false; + self::eventLog('ICrmOrderActions::uploadOrders', 'sale', 'module not found'); + return true; } if (!CModule::IncludeModule("catalog")) { //handle err - self::eventLog('catalog', 'module not found'); - return false; + self::eventLog('ICrmOrderActions::uploadOrders', 'catalog', 'module not found'); + return true; } $resOrders = array(); @@ -79,7 +80,15 @@ class ICrmOrderActions $optionsPayment = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT, 0)); $api = new IntaroCrm\RestApi($api_host, $api_key); - + + $arParams = array( + 'optionsOrderTypes' => $optionsOrderTypes, + 'optionsDelivTypes' => $optionsDelivTypes, + 'optionsPayTypes' => $optionsPayTypes, + 'optionsPayStatuses' => $optionsPayStatuses, + 'optionsPayment' => $optionsPayment + ); + while ($arOrder = $dbOrder->GetNext()) { if ($arOrder['ID'] <= $lastUpOrderId) //old orders not to upload return true; @@ -87,147 +96,12 @@ class ICrmOrderActions if(!$lastUpOrderIdNew) $lastUpOrderIdNew = $arOrder['ID']; - $arFields = CSaleOrder::GetById($arOrder['ID']); - - if (empty($arFields)) { - //handle err - self::eventLog('empty($arFields)', 'incorrect order'); - - return false; - } - - $rsUser = CUser::GetByID($arFields['USER_ID']); - $arUser = $rsUser->Fetch(); - - // push customer (for crm) - $firstName = self::toJSON($arUser['NAME']); - $lastName = self::toJSON($arUser['LAST_NAME']); - $patronymic = self::toJSON($arUser['SECOND_NAME']); - - $phonePersonal = array( - 'number' => self::toJSON($arUser['PERSONAL_PHONE']), - 'type' => 'mobile' - ); - $phones[] = $phonePersonal; + $order = self::orderCreate($arOrder['ID'], $api, $arParams); - $phoneWork = array( - 'number' => self::toJSON($arUser['WORK_PHONE']), - 'type' => 'work' - ); - $phones[] = $phoneWork; - - $addressPersonal = array( - 'index' => self::toJSON($arUser['PERSONAL_ZIP']), - 'country' => self::toJSON(GetCountryByID($arUser['PERSONAL_COUNTRY'])), - 'city' => self::toJSON($arUser['PERSONAL_CITY']), - 'street' => self::toJSON($arUser['PERSONAL_STREET']), - 'building' => self::toJSON($arUser['UF_PERSONAL_BUILDING']), - 'flat' => self::toJSON($arUser['UF_PERSONAL_FLAT']), - 'notes' => self::toJSON($arUser['PERSONAL_NOTES']), - 'text' => self::toJSON($arUser['UF_PERSONAL_TEXT']), - 'type' => 'home' - ); - $addresses[] = $addressPersonal; - - $addressWork = array( - 'index' => self::toJSON($arUser['WORK_ZIP']), - 'country' => self::toJSON(GetCountryByID($arUser['WORK_COUNTRY'])), - 'city' => self::toJSON($arUser['WORK_CITY']), - 'street' => self::toJSON($arUser['WORK_STREET']), - 'building' => self::toJSON($arUser['UF_WORK_BUILDING']), // -- - 'flat' => self::toJSON($arUser['UF_WORK_FLAT']), - 'notes' => self::toJSON($arUser['PERSONAL_NOTES']), - 'text' => self::toJSON($arUser['UF_WORK_TEXT']), - 'type' => 'work' - ); - $addresses[] = $addressWork; - - $result = array( - 'externalId' => $arFields['USER_ID'], - 'lastName' => $lastName, - 'firstName' => $firstName, - 'patronymic' => $patronymic, - 'phones' => $phones, - 'addresses' => $addresses - ); - - $customer = $api->customerEdit($result); + if(!$order) + continue; - // error pushing customer - if (!$customer) { - //handle err - self::eventLog('IntaroCrm\RestApi::customerEdit', $api->getLastError()); - return false; - } - - // delivery types - $arId = array(); - if (strpos($arFields['DELIVERY_ID'], ":") !== false) - $arId = explode(":", $arFields["DELIVERY_ID"]); - - if ($arId) - $resultDeliveryTypeId = $arId[0]; - else - $resultDeliveryTypeId = $arFields['DELIVERY_ID']; - - - $resOrder = array(); - $resOrderDeliveryAddress = array(); - - $rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID'])); - while ($ar = $rsOrderProps->Fetch()) { - switch ($ar['CODE']) { - case 'ZIP': $resOrderDeliveryAddress['index'] = self::toJSON($ar['VALUE']); - break; - case 'CITY': $resOrderDeliveryAddress['city'] = self::toJSON($ar['VALUE']); - break; - case 'ADDRESS': $resOrderDeliveryAddress['text'] = self::toJSON($ar['VALUE']); - break; - case 'FIO': $resOrder['contactName'] = self::toJSON($ar['VALUE']); - break; - case 'PHONE': $resOrder['phone'] = $ar['VALUE']; - break; - case 'EMAIL': $resOrder['email'] = $ar['VALUE']; - break; - } - } - - $resOrder['deliveryCost'] = $arFields['PRICE_DELIVERY']; - $resOrder['summ'] = $arFields['PRICE']; - $resOrder['markDateTime'] = $arFields['DATE_MARKED']; - $resOrder['externalId'] = $arFields['ID']; - $resOrder['customerId'] = $arFields['USER_ID']; - - $resOrder['paymentType'] = $optionsPayTypes[$arFields['PAY_SYSTEM_ID']]; - $resOrder['paymentStatus'] = $optionsPayment[$arFields['PAYED']]; - $resOrder['orderType'] = $optionsOrderTypes[$arFields['PERSON_TYPE_ID']]; - $resOrder['deliveryType'] = $optionsDelivTypes[$resultDeliveryTypeId]; - $resOrder['status'] = $optionsPayStatuses[$arFields['STATUS_ID']]; - - $resOrder['deliveryAddress'] = $resOrderDeliveryAddress; - - $items = array(); - - $rsOrderBasket = CSaleBasket::GetList(array('PRODUCT_ID' => 'ASC'), array('ORDER_ID' => $arFields['ID'])); - while ($p = $rsOrderBasket->Fetch()) { - $pr = CCatalogProduct::GetList(array('ID' => $p['PRODUCT_ID']))->Fetch(); - if($pr) - $pr = $pr['PURCHASING_PRICE']; - else - $pr = ''; - - $items[] = array( - 'price' => $p['PRICE'], - 'purchasePrice' => $pr, - 'discount' => $p['DISCOUNT_VALUE'], - 'quantity' => $p['QUANTITY'], - 'productId' => $p['PRODUCT_ID'], - 'productName' => self::toJSON($p['NAME']) - ); - } - - $resOrder['items'] = $items; - $resOrders[] = $resOrder; + $resOrders[] = $order; } $orders = $api->orderUpload($resOrders); @@ -235,68 +109,31 @@ class ICrmOrderActions // error pushing orders if(!$orders) { //handle err - self::eventLog('IntaroCrm\RestApi::orderUpload', $api->getLastError()); - return false; + self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError()); + return true; } COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastUpOrderIdNew); return true; //all ok! } - - public static function orderHistory() { - - if (!CModule::IncludeModule('iblock')) { - //handle err - self::eventLog('iblock', 'module not found'); - return false; - } - - if (!CModule::IncludeModule("sale")) { - //handle err - self::eventLog('sale', 'module not found'); - return false; - } - - if (!CModule::IncludeModule("catalog")) { - //handle err - self::eventLog('catalog', 'module not found'); - return false; - } - - $api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0); - $api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0); - - //saved cat params - $optionsOrderTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_TYPES_ARR, 0)); - $optionsDelivTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_DELIVERY_TYPES_ARR, 0)); - $optionsPayTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_TYPES, 0)); - $optionsPayStatuses = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_STATUSES, 0)); // --statuses - $optionsPayment = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT, 0)); - - $api = new IntaroCrm\RestApi($api_host, $api_key); - - var_dump($api->orderHistory()); - } - + /** * * w+ event in bitrix log */ - private static function eventLog($itemId, $description) { + private static function eventLog($auditType, $itemId, $description) { CEventLog::Add(array( - "SEVERITY" => "SECURITY", - "AUDIT_TYPE_ID" => 'ICrmOrderActions::uploadOrders', - "MODULE_ID" => self::$MODULE_ID, - "ITEM_ID" => $itemId, - "DESCRIPTION" => $description, + "SEVERITY" => "SECURITY", + "AUDIT_TYPE_ID" => $auditType, + "MODULE_ID" => self::$MODULE_ID, + "ITEM_ID" => $itemId, + "DESCRIPTION" => $description, )); - self::sendEmail($itemId, $description); + //self::sendEmail($itemId, $description); } - - /** * * send email to admin @@ -324,5 +161,161 @@ class ICrmOrderActions else return; } + public static function orderCreate($orderId, $api, $arParams, $send = false) { + if(!$api || empty($arParams) || !$orderId) { // add cond to check $arParams + return false; + } + + $arFields = CSaleOrder::GetById($orderId); + + if (empty($arFields)) { + //handle err + self::eventLog('ICrmOrderActions::orderCreate', 'empty($arFields)', 'incorrect order'); + + return false; + } + + $rsUser = CUser::GetByID($arFields['USER_ID']); + $arUser = $rsUser->Fetch(); + + // push customer (for crm) + $firstName = self::toJSON($arUser['NAME']); + $lastName = self::toJSON($arUser['LAST_NAME']); + $patronymic = self::toJSON($arUser['SECOND_NAME']); + + $phonePersonal = array( + 'number' => self::toJSON($arUser['PERSONAL_PHONE']), + 'type' => 'mobile' + ); + $phones[] = $phonePersonal; + + $phoneWork = array( + 'number' => self::toJSON($arUser['WORK_PHONE']), + 'type' => 'work' + ); + $phones[] = $phoneWork; + + $addressPersonal = array( + 'index' => self::toJSON($arUser['PERSONAL_ZIP']), + 'country' => self::toJSON(GetCountryByID($arUser['PERSONAL_COUNTRY'])), + 'city' => self::toJSON($arUser['PERSONAL_CITY']), + 'street' => self::toJSON($arUser['PERSONAL_STREET']), + 'building' => self::toJSON($arUser['UF_PERSONAL_BUILDING']), + 'flat' => self::toJSON($arUser['UF_PERSONAL_FLAT']), + 'notes' => self::toJSON($arUser['PERSONAL_NOTES']), + 'text' => self::toJSON($arUser['UF_PERSONAL_TEXT']), + 'type' => 'home' + ); + $addresses[] = $addressPersonal; + + $addressWork = array( + 'index' => self::toJSON($arUser['WORK_ZIP']), + 'country' => self::toJSON(GetCountryByID($arUser['WORK_COUNTRY'])), + 'city' => self::toJSON($arUser['WORK_CITY']), + 'street' => self::toJSON($arUser['WORK_STREET']), + 'building' => self::toJSON($arUser['UF_WORK_BUILDING']), // -- + 'flat' => self::toJSON($arUser['UF_WORK_FLAT']), + 'notes' => self::toJSON($arUser['PERSONAL_NOTES']), + 'text' => self::toJSON($arUser['UF_WORK_TEXT']), + 'type' => 'work' + ); + $addresses[] = $addressWork; + + $result = array( + 'externalId' => $arFields['USER_ID'], + 'lastName' => $lastName, + 'firstName' => $firstName, + 'patronymic' => $patronymic, + 'phones' => $phones, + 'addresses' => $addresses + ); + + $customer = $api->customerEdit($result); + + // error pushing customer + if (!$customer) { + //handle err + self::eventLog('ICrmOrderActions::orderCreate', 'IntaroCrm\RestApi::customerEdit', $api->getLastError()); + return false; + } + + // delivery types + $arId = array(); + if (strpos($arFields['DELIVERY_ID'], ":") !== false) + $arId = explode(":", $arFields["DELIVERY_ID"]); + + if ($arId) + $resultDeliveryTypeId = $arId[0]; + else + $resultDeliveryTypeId = $arFields['DELIVERY_ID']; + + + $resOrder = array(); + $resOrderDeliveryAddress = array(); + + $rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID'])); + while ($ar = $rsOrderProps->Fetch()) { + switch ($ar['CODE']) { + case 'ZIP': $resOrderDeliveryAddress['index'] = self::toJSON($ar['VALUE']); + break; + case 'CITY': $resOrderDeliveryAddress['city'] = self::toJSON($ar['VALUE']); + break; + case 'ADDRESS': $resOrderDeliveryAddress['text'] = self::toJSON($ar['VALUE']); + break; + case 'FIO': $resOrder['contactName'] = self::toJSON($ar['VALUE']); + break; + case 'PHONE': $resOrder['phone'] = $ar['VALUE']; + break; + case 'EMAIL': $resOrder['email'] = $ar['VALUE']; + break; + } + } + + $items = array(); + + $rsOrderBasket = CSaleBasket::GetList(array('PRODUCT_ID' => 'ASC'), array('ORDER_ID' => $arFields['ID'])); + while ($p = $rsOrderBasket->Fetch()) { + $pr = CCatalogProduct::GetList(array('ID' => $p['PRODUCT_ID']))->Fetch(); + if ($pr) + $pr = $pr['PURCHASING_PRICE']; + else + $pr = ''; + + $items[] = array( + 'price' => $p['PRICE'], + 'purchasePrice' => $pr, + 'discount' => $p['DISCOUNT_VALUE'], + 'quantity' => $p['QUANTITY'], + 'productId' => $p['PRODUCT_ID'], + 'productName' => self::toJSON($p['NAME']) + ); + } + + $resOrder = array( + 'contactName' => $resOrder['contactName'], + 'phone' => $resOrder['phone'], + 'email' => $resOrder['email'], + 'deliveryCost' => $arFields['PRICE_DELIVERY'], + 'summ' => $arFields['PRICE'], + 'markDateTime' => $arFields['DATE_MARKED'], + 'externalId' => $arFields['ID'], + 'customerId' => $arFields['USER_ID'], + 'paymentType' => $arParams['optionsPayTypes'][$arFields['PAY_SYSTEM_ID']], + 'paymentStatus' => $arParams['optionsPayment'][$arFields['PAYED']], + 'orderType' => $arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']], + 'deliveryType' => $arParams['optionsDelivTypes'][$resultDeliveryTypeId], + 'status' => $arParams['optionsPayStatuses'][$arFields['STATUS_ID']], + 'deliveryAddress' => $resOrderDeliveryAddress, + 'items' => $items + ); + + if($send) + return $api->createOrder($resOrder); + + return $resOrder; + + } + } ?> + diff --git a/intaro.crm/install/step1.php b/intaro.crm/install/step1.php index 0282d2f8..6f62828e 100755 --- a/intaro.crm/install/step1.php +++ b/intaro.crm/install/step1.php @@ -17,11 +17,11 @@ - + - + diff --git a/intaro.crm/install/version.php b/intaro.crm/install/version.php index dafd6ca5..dde38370 100755 --- a/intaro.crm/install/version.php +++ b/intaro.crm/install/version.php @@ -1,6 +1,6 @@ '0.3b', - 'VERSION_DATE' => '2013-07-10 18:19:00', + 'VERSION' => '0.3f', + 'VERSION_DATE' => '2013-07-16 14:55:00', ); ?>