1
0
mirror of synced 2024-11-28 08:16:07 +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,11 +96,81 @@ 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(!$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)) { if (empty($arFields)) {
//handle err //handle err
self::eventLog('empty($arFields)', 'incorrect order'); self::eventLog('ICrmOrderActions::orderCreate', 'empty($arFields)', 'incorrect order');
return false; return false;
} }
@ -156,7 +235,7 @@ class ICrmOrderActions
// error pushing customer // error pushing customer
if (!$customer) { if (!$customer) {
//handle err //handle err
self::eventLog('IntaroCrm\RestApi::customerEdit', $api->getLastError()); self::eventLog('ICrmOrderActions::orderCreate', 'IntaroCrm\RestApi::customerEdit', $api->getLastError());
return false; return false;
} }
@ -192,26 +271,12 @@ class ICrmOrderActions
} }
} }
$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(); $items = array();
$rsOrderBasket = CSaleBasket::GetList(array('PRODUCT_ID' => 'ASC'), array('ORDER_ID' => $arFields['ID'])); $rsOrderBasket = CSaleBasket::GetList(array('PRODUCT_ID' => 'ASC'), array('ORDER_ID' => $arFields['ID']));
while ($p = $rsOrderBasket->Fetch()) { while ($p = $rsOrderBasket->Fetch()) {
$pr = CCatalogProduct::GetList(array('ID' => $p['PRODUCT_ID']))->Fetch(); $pr = CCatalogProduct::GetList(array('ID' => $p['PRODUCT_ID']))->Fetch();
if($pr) if ($pr)
$pr = $pr['PURCHASING_PRICE']; $pr = $pr['PURCHASING_PRICE'];
else else
$pr = ''; $pr = '';
@ -226,103 +291,31 @@ class ICrmOrderActions
); );
} }
$resOrder['items'] = $items; $resOrder = array(
$resOrders[] = $resOrder; '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
);
$orders = $api->orderUpload($resOrders); if($send)
return $api->createOrder($resOrder);
// error pushing orders return $resOrder;
if(!$orders) {
//handle err
self::eventLog('IntaroCrm\RestApi::orderUpload', $api->getLastError());
return false;
}
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) {
CEventLog::Add(array(
"SEVERITY" => "SECURITY",
"AUDIT_TYPE_ID" => 'ICrmOrderActions::uploadOrders',
"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;
} }
} }
?> ?>

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',
); );
?> ?>