diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index b7e18a4c..1182c98f 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -113,7 +113,8 @@ class ICrmOrderActions return true; } - COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastUpOrderIdNew); + if($lastUpOrderIdNew) + COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastUpOrderIdNew); return true; //all ok! } diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php~ b/intaro.intarocrm/classes/general/ICrmOrderActions.php~ new file mode 100755 index 00000000..b7e18a4c --- /dev/null +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php~ @@ -0,0 +1,321 @@ +ConvertCharset($str, SITE_CHARSET, 'utf-8'); + } + + /** + * + * @global type $APPLICATION + * @param type $str in utf-8 + * @return type $str in SITE_CHARSET + */ + public static function fromJSON($str) { + global $APPLICATION; + + return $APPLICATION->ConvertCharset($str, 'utf-8', SITE_CHARSET); + } + + /** + * Mass order uploading, without repeating; always returns true, but writes error log + * @return boolean + */ + public static function uploadOrders() { + + //COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0); // -- for test + + if (!CModule::IncludeModule('iblock')) { + //handle err + self::eventLog('ICrmOrderActions::uploadOrders', 'iblock', 'module not found'); + return true; + } + + if (!CModule::IncludeModule("sale")) { + //handle err + self::eventLog('ICrmOrderActions::uploadOrders', 'sale', 'module not found'); + return true; + } + + if (!CModule::IncludeModule("catalog")) { + //handle err + self::eventLog('ICrmOrderActions::uploadOrders', 'catalog', 'module not found'); + return true; + } + + $resOrders = array(); + + $dbOrder = CSaleOrder::GetList(); + + $lastUpOrderId = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0); + $lastUpOrderIdNew = 0; + + $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); + + $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 + break; + + if(!$lastUpOrderIdNew) + $lastUpOrderIdNew = $arOrder['ID']; + + $order = self::orderCreate($arOrder['ID'], $api, $arParams); + + if(!$order) + continue; + + $resOrders[] = $order; + } + + $orders = $api->orderUpload($resOrders); + + // error pushing orders + if(!$orders) { + //handle err + 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! + } + + /** + * + * w+ event in bitrix log + */ + private static function eventLog($auditType, $itemId, $description) { + CEventLog::Add(array( + "SEVERITY" => "SECURITY", + "AUDIT_TYPE_ID" => $auditType, + "MODULE_ID" => self::$MODULE_ID, + "ITEM_ID" => $itemId, + "DESCRIPTION" => $description, + )); + + //self::sendEmail($itemId, $description); + } + + /** + * + * send email to admin + */ + private static function sendEmail($itemId, $description) { + $title = 'Error: Intaro CRM.'; + $text = 'Error: ' . $itemId . ' - ' . $description; + $to = COption::GetOptionString("main", "email_from"); + $from = COption::GetOptionString("main", "email_from"); + mail($to, $title, $text, 'From:'.$from); + } + + /** + * + * Agent function + * + * @return self name + */ + + public static function uploadOrdersAgent() { + + if(self::uploadOrders()) + return 'ICrmOrderActions::uploadOrdersAgent();'; + + 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.intarocrm/include.php b/intaro.intarocrm/include.php index 8a8475eb..79e60b6a 100755 --- a/intaro.intarocrm/include.php +++ b/intaro.intarocrm/include.php @@ -3,7 +3,8 @@ CModule::AddAutoloadClasses( 'intaro.intarocrm', // module name array ( 'IntaroCrm\RestApi' => 'classes/general/RestApi.php', - 'ICrmOrderActions' => 'classes/general/ICrmOrderActions.php' + 'ICrmOrderActions' => 'classes/general/ICrmOrderActions.php', + 'ICrmOrderEvent' => 'classes/general/events/ICrmOrderEvent.php' ) ); ?> diff --git a/intaro.intarocrm/install/index.php b/intaro.intarocrm/install/index.php index c8ddef70..5240d329 100755 --- a/intaro.intarocrm/install/index.php +++ b/intaro.intarocrm/install/index.php @@ -190,11 +190,18 @@ class intaro_intarocrm extends CModule $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step2.php' ); - } else if ($step == 3) { + } else if ($step == 3) { if(!CModule::IncludeModule("sale")) { //handler } + if (isset($_POST['back']) && $_POST['back']) { + $APPLICATION->IncludeAdminFile( + GetMessage('MODULE_INSTALL_TITLE'), + $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php' + ); + } + //bitrix orderTypesList -- personTypes $dbOrderTypesList = CSalePersonType::GetList( array( @@ -350,7 +357,6 @@ class intaro_intarocrm extends CModule function DeleteFiles() { unlink($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/crm_run.php'); unlink($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/crm_setup.php'); - } } -?> +?> \ No newline at end of file diff --git a/intaro.intarocrm/install/step1.php b/intaro.intarocrm/install/step1.php index 78bffc91..3d953994 100755 --- a/intaro.intarocrm/install/step1.php +++ b/intaro.intarocrm/install/step1.php @@ -12,20 +12,35 @@ - - - - - - - - - - - - -
+ + + + + + + + + + + + +   + + + + + + + + + + +
- " class="adm-btn-save"> +
+
+ " class="adm-btn-save"> +
+
diff --git a/intaro.intarocrm/install/step2.php b/intaro.intarocrm/install/step2.php index caf957df..d400dbdf 100755 --- a/intaro.intarocrm/install/step2.php +++ b/intaro.intarocrm/install/step2.php @@ -6,6 +6,34 @@ $arResult['bitrixPaymentList'][0]['NAME'] = GetMessage('PAYMENT_Y'); $arResult['bitrixPaymentList'][0]['ID'] = 'Y'; $arResult['bitrixPaymentList'][1]['NAME'] = GetMessage('PAYMENT_N'); $arResult['bitrixPaymentList'][1]['ID'] = 'N'; + +$defaultOrderTypes = array ( + 1 => 'eshop-individual', + 2 => 'eshop-legal' +); + +$defaultDelivTypes = array ( + 1 => 'courier', + 2 => 'self-delivery' +); + +$defaultPayTypes = array ( + 1 => 'cash', + 5 => 'bank-card', + 6 => 'bank-transfer' +); + +$defaultPayStatuses = array ( + 'N' => 'in-processing', + 'P' => 'in-processing', + 'F' => 'complete' +); + +$defaultPayment = array( + 'Y' => 'paid', + 'N' => 'not-paid' +); + ?>
@@ -21,6 +49,9 @@ $arResult['bitrixPaymentList'][1]['ID'] = 'N'; + + + @@ -31,9 +62,10 @@ $arResult['bitrixPaymentList'][1]['ID'] = 'N'; - + - @@ -73,7 +106,8 @@ $arResult['bitrixPaymentList'][1]['ID'] = 'N'; - + - @@ -111,9 +146,10 @@ $arResult['bitrixPaymentList'][1]['ID'] = 'N'; " class="adm-btn-save"> +
+
+ " class="adm-btn-save"> +
+
+ " class="adm-btn-save"> +
+
diff --git a/intaro.intarocrm/install/step3.php b/intaro.intarocrm/install/step3.php index c5463f3e..931fdfcc 100755 --- a/intaro.intarocrm/install/step3.php +++ b/intaro.intarocrm/install/step3.php @@ -6,5 +6,5 @@ echo CAdminMessage::ShowNote(GetMessage("MOD_INST_OK")); ?> - " class="adm-btn-save"> + ">
diff --git a/intaro.intarocrm/install/unstep1.php b/intaro.intarocrm/install/unstep1.php index 93a5dcc6..0c643a80 100755 --- a/intaro.intarocrm/install/unstep1.php +++ b/intaro.intarocrm/install/unstep1.php @@ -1,10 +1,8 @@ - - - - - - - - - " class="adm-btn-save"> + + + + + ">
diff --git a/intaro.intarocrm/install/unstep2.php b/intaro.intarocrm/install/unstep2.php deleted file mode 100755 index 51c331fa..00000000 --- a/intaro.intarocrm/install/unstep2.php +++ /dev/null @@ -1,17 +0,0 @@ - -"; - echo CAdminMessage::ShowMessage(Array("TYPE"=>"ERROR", "MESSAGE" =>GetMessage("MOD_UNINST_ERR"), "DETAILS"=>$alErrors, "HTML"=>true)); -endif; -?> -
- - "> -
diff --git a/intaro.intarocrm/install/version.php b/intaro.intarocrm/install/version.php index d52d60cc..363065c5 100755 --- a/intaro.intarocrm/install/version.php +++ b/intaro.intarocrm/install/version.php @@ -1,6 +1,6 @@ '0.3.0', - 'VERSION_DATE' => '2013-07-16 14:55:00', + 'VERSION' => '0.4.4', + 'VERSION_DATE' => '2013-07-16 14:41:00', ); ?> diff --git a/intaro.intarocrm/lang/ru/install/step1.php b/intaro.intarocrm/lang/ru/install/step1.php index cad89a78..d2b2a632 100755 --- a/intaro.intarocrm/lang/ru/install/step1.php +++ b/intaro.intarocrm/lang/ru/install/step1.php @@ -1,10 +1,12 @@ Магазины).'; ?> diff --git a/intaro.intarocrm/lang/ru/install/step2.php b/intaro.intarocrm/lang/ru/install/step2.php index 574d2360..b1267ddd 100755 --- a/intaro.intarocrm/lang/ru/install/step2.php +++ b/intaro.intarocrm/lang/ru/install/step2.php @@ -1,6 +1,7 @@ diff --git a/intaro.intarocrm/lang/ru/install/unstep1.php b/intaro.intarocrm/lang/ru/install/unstep1.php index bcf94ca7..2005f2ef 100755 --- a/intaro.intarocrm/lang/ru/install/unstep1.php +++ b/intaro.intarocrm/lang/ru/install/unstep1.php @@ -1,4 +1,4 @@ diff --git a/intaro.intarocrm/options.php b/intaro.intarocrm/options.php index 9325bbe3..b5da9cd8 100755 --- a/intaro.intarocrm/options.php +++ b/intaro.intarocrm/options.php @@ -1,6 +1,6 @@ GetCurPage() . '?mid=' . htmlspecialchars($mid) . '&lang=' . LANGUAGE_ID; $CRM_API_HOST_OPTION = 'api_host'; @@ -12,7 +12,7 @@ $CRM_PAYMENT_STATUSES = 'pay_statuses_arr'; $CRM_PAYMENT = 'payment_arr'; //order payment Y/N $CRM_ORDER_LAST_ID = 'order_last_id'; -CModule::IncludeModule('intaro.crm'); +CModule::IncludeModule('intaro.intarocrm'); CModule::IncludeModule('sale'); $_GET['errc'] = htmlspecialchars(trim($_GET['errc']));