1
0
mirror of synced 2024-11-24 14:26:08 +03:00

v 0.3f; +updated Agent;

This commit is contained in:
Grisha Pomadchin 2013-07-16 14:57:22 +04:00
parent 1bf0bdb4e3
commit 55f262d3f5
3 changed files with 191 additions and 198 deletions

View File

@ -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() { public static function uploadOrders() {
@ -45,20 +46,20 @@ class ICrmOrderActions
if (!CModule::IncludeModule('iblock')) { if (!CModule::IncludeModule('iblock')) {
//handle err //handle err
self::eventLog('iblock', 'module not found'); self::eventLog('ICrmOrderActions::uploadOrders', 'iblock', 'module not found');
return false; return true;
} }
if (!CModule::IncludeModule("sale")) { if (!CModule::IncludeModule("sale")) {
//handle err //handle err
self::eventLog('sale', 'module not found'); self::eventLog('ICrmOrderActions::uploadOrders', 'sale', 'module not found');
return false; return true;
} }
if (!CModule::IncludeModule("catalog")) { if (!CModule::IncludeModule("catalog")) {
//handle err //handle err
self::eventLog('catalog', 'module not found'); self::eventLog('ICrmOrderActions::uploadOrders', 'catalog', 'module not found');
return false; return true;
} }
$resOrders = array(); $resOrders = array();
@ -80,6 +81,14 @@ class ICrmOrderActions
$api = new IntaroCrm\RestApi($api_host, $api_key); $api = new IntaroCrm\RestApi($api_host, $api_key);
$arParams = array(
'optionsOrderTypes' => $optionsOrderTypes,
'optionsDelivTypes' => $optionsDelivTypes,
'optionsPayTypes' => $optionsPayTypes,
'optionsPayStatuses' => $optionsPayStatuses,
'optionsPayment' => $optionsPayment
);
while ($arOrder = $dbOrder->GetNext()) { while ($arOrder = $dbOrder->GetNext()) {
if ($arOrder['ID'] <= $lastUpOrderId) //old orders not to upload if ($arOrder['ID'] <= $lastUpOrderId) //old orders not to upload
return true; return true;
@ -87,147 +96,12 @@ class ICrmOrderActions
if(!$lastUpOrderIdNew) if(!$lastUpOrderIdNew)
$lastUpOrderIdNew = $arOrder['ID']; $lastUpOrderIdNew = $arOrder['ID'];
$arFields = CSaleOrder::GetById($arOrder['ID']); $order = self::orderCreate($arOrder['ID'], $api, $arParams);
if (empty($arFields)) { if(!$order)
//handle err continue;
self::eventLog('empty($arFields)', 'incorrect order');
return false; $resOrders[] = $order;
}
$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('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;
} }
$orders = $api->orderUpload($resOrders); $orders = $api->orderUpload($resOrders);
@ -235,8 +109,8 @@ class ICrmOrderActions
// error pushing orders // error pushing orders
if(!$orders) { if(!$orders) {
//handle err //handle err
self::eventLog('IntaroCrm\RestApi::orderUpload', $api->getLastError()); self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError());
return false; return true;
} }
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastUpOrderIdNew); COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastUpOrderIdNew);
@ -244,59 +118,22 @@ class ICrmOrderActions
return true; //all ok! 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 * w+ event in bitrix log
*/ */
private static function eventLog($itemId, $description) { private static function eventLog($auditType, $itemId, $description) {
CEventLog::Add(array( CEventLog::Add(array(
"SEVERITY" => "SECURITY", "SEVERITY" => "SECURITY",
"AUDIT_TYPE_ID" => 'ICrmOrderActions::uploadOrders', "AUDIT_TYPE_ID" => $auditType,
"MODULE_ID" => self::$MODULE_ID, "MODULE_ID" => self::$MODULE_ID,
"ITEM_ID" => $itemId, "ITEM_ID" => $itemId,
"DESCRIPTION" => $description, "DESCRIPTION" => $description,
)); ));
self::sendEmail($itemId, $description); //self::sendEmail($itemId, $description);
} }
/** /**
* *
* send email to admin * send email to admin
@ -324,5 +161,161 @@ class ICrmOrderActions
else return; 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;
}
} }
?> ?>

View File

@ -17,11 +17,11 @@
</tr> </tr>
<tr> <tr>
<td width="50%" class="adm-detail-content-cell-l"><?php echo GetMessage('ICRM_API_HOST'); ?></td> <td width="50%" class="adm-detail-content-cell-l"><?php echo GetMessage('ICRM_API_HOST'); ?></td>
<td width="50%" class="adm-detail-content-cell-r"><input type="text" id="api_host" name="api_host" value="http://bitrix.beta.intarocrm.ru"></td> <td width="50%" class="adm-detail-content-cell-r"><input type="text" id="api_host" name="api_host" value=""></td>
</tr> </tr>
<tr> <tr>
<td width="50%" class="adm-detail-content-cell-l"><?php echo GetMessage('ICRM_API_KEY'); ?></td> <td width="50%" class="adm-detail-content-cell-l"><?php echo GetMessage('ICRM_API_KEY'); ?></td>
<td width="50%" class="adm-detail-content-cell-r"><input type="text" id="api_key" name="api_key" value="test2"></td> <td width="50%" class="adm-detail-content-cell-r"><input type="text" id="api_key" name="api_key" value=""></td>
</tr> </tr>
</tbody> </tbody>
</table> </table>

View File

@ -1,6 +1,6 @@
<? <?
$arModuleVersion = array( $arModuleVersion = array(
'VERSION' => '0.3b', 'VERSION' => '0.3f',
'VERSION_DATE' => '2013-07-10 18:19:00', 'VERSION_DATE' => '2013-07-16 14:55:00',
); );
?> ?>