* distribution. * * * Neither the name of Dmitry Mamontov nor the names of his * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * @package restnormalizer * @author Dmitry Mamontov <*****@*****.**> * @copyright 2015 Dmitry Mamontov <*****@*****.**> * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License * @since File available since Release 1.0.2 */ // EXAMPLE FOR RETAILCRM require_once 'RestNormalizer.php'; $n = new RestNormalizer(); $n->setValidation('./example/retailcrm.json')->setLogFile('valid.log'); $customer = array("externalId" => 1023, "firstName" => "Test", "email" => "*****@*****.**", "phones" => array(array("number" => "+79998885566"), array(), array("number" => "+78889993332")), "address" => array("floor" => "В", "city" => "Moscow"), "createdAt" => '2015:01:02 03:00:11', "contragentType" => "no-valid", "certificateDate" => new DateTime()); $customer = $n->normalize($customer, 'customers');
/** * * Creates order or returns array of order and customer for mass upload * * @param array $arFields * @param $api * @param $arParams * @param $send * @return boolean * @return array - array('order' = $order, 'customer' => $customer) */ public static function orderCreate($arFields, $api, $arParams, $send = false, $site = null) { if (!$api || empty($arParams)) { // add cond to check $arParams return false; } if (empty($arFields)) { self::eventLog('ICrmOrderActions::orderCreate', 'empty($arFields)', 'incorrect order'); return false; } if (isset($arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['city']) == false) { $rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID'], 'CODE' => 'LOCATION')); $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['city'] = $rsOrderProps->SelectedRowsCount() < 1 ? 'CITY' : 'LOCATION'; } $normalizer = new RestNormalizer(); $normalizer->setValidation($_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/intaro.intarocrm/classes/general/config/retailcrm.json'); $customer = array(); if ($arFields['CANCELED'] == 'Y') { $arFields['STATUS_ID'] = $arFields['CANCELED'] . $arFields['CANCELED']; } $order = array('number' => $arFields['ACCOUNT_NUMBER'], 'externalId' => $arFields['ID'], 'createdAt' => new \DateTime($arFields['DATE_INSERT']), 'customerId' => $arFields['USER_ID'], 'discount' => $arFields['DISCOUNT_VALUE'], 'markDateTime' => $arFields['DATE_MARKED'], 'paymentType' => isset($arParams['optionsPayTypes'][$arFields['PAY_SYSTEM_ID']]) ? $arParams['optionsPayTypes'][$arFields['PAY_SYSTEM_ID']] : '', 'paymentStatus' => isset($arParams['optionsPayment'][$arFields['PAYED']]) ? $arParams['optionsPayment'][$arFields['PAYED']] : '', 'orderType' => isset($arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']]) ? $arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']] : '', 'contragentType' => isset($arParams['optionsContragentType'][$arFields['PERSON_TYPE_ID']]) ? $arParams['optionsContragentType'][$arFields['PERSON_TYPE_ID']] : '', 'status' => isset($arParams['optionsPayStatuses'][$arFields['STATUS_ID']]) ? $arParams['optionsPayStatuses'][$arFields['STATUS_ID']] : '', 'statusComment' => $arFields['REASON_CANCELED'], 'customerComment' => $arFields['USER_DESCRIPTION'], 'managerComment' => $arFields['COMMENTS'], 'delivery' => array('cost' => $arFields['PRICE_DELIVERY'])); $rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID'])); while ($ar = $rsOrderProps->Fetch()) { if ($search = array_search($ar['CODE'], $arParams['optionsLegalDetails'][$arFields['PERSON_TYPE_ID']])) { $order[$search] = $ar['VALUE']; $customer[$search] = $ar['VALUE']; } elseif ($search = array_search($ar['CODE'], $arParams['optionsCustomFields'][$arFields['PERSON_TYPE_ID']])) { $order['customFields'][$search] = $ar['VALUE']; } elseif ($search = array_search($ar['CODE'], $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']])) { if (in_array($search, array('fio', 'phone', 'email'))) { if ($search == 'fio') { $order = array_merge($order, self::explodeFIO($ar['VALUE'])); } else { $order[$search] = $ar['VALUE']; } } else { $prop = CSaleOrderProps::GetByID($ar['ORDER_PROPS_ID']); if ($prop['TYPE'] == 'LOCATION') { $ar['VALUE'] = CSaleLocation::GetByID(method_exists('CSaleLocation', 'getLocationIDbyCODE') ? CSaleLocation::getLocationIDbyCODE($ar['VALUE']) : $ar['VALUE']); $ar['VALUE'] = $ar['VALUE']['CITY_NAME_LANG']; } $order['delivery']['address'][$search] = $ar['VALUE']; } } } if (strpos($arFields['DELIVERY_ID'], ":") !== false) { $arFields["DELIVERY_ID"] = explode(":", $arFields["DELIVERY_ID"], 2); if ($arDeliveryType = CSaleDeliveryHandler::GetBySID(reset($arFields["DELIVERY_ID"]))->GetNext()) { if (array_key_exists(end($arFields["DELIVERY_ID"]), $arDeliveryType['PROFILES'])) { $arFields["DELIVERY_SERVICE"] = array('code' => implode('-', $arFields["DELIVERY_ID"]), 'name' => $arDeliveryType['PROFILES'][end($arFields["DELIVERY_ID"])]['TITLE']); } } $arFields["DELIVERY_ID"] = reset($arFields["DELIVERY_ID"]); } if (array_key_exists($arFields['DELIVERY_ID'], $arParams['optionsDelivTypes'])) { $order['delivery']['code'] = $arParams['optionsDelivTypes'][$arFields["DELIVERY_ID"]]; if (isset($arFields["DELIVERY_SERVICE"])) { $order['delivery']['service'] = $arFields["DELIVERY_SERVICE"]; } } $rsOrderBasket = CSaleBasket::GetList(array('ID' => 'ASC'), array('ORDER_ID' => $arFields['ID'])); while ($p = $rsOrderBasket->Fetch()) { $item = array('quantity' => $p['QUANTITY'], 'productId' => $p['PRODUCT_ID'], 'xmlId' => $p['PRODUCT_XML_ID'], 'productName' => $p['NAME'], 'comment' => $p['NOTES'], 'createdAt' => new \DateTime($p['DATE_INSERT'])); $pp = CCatalogProduct::GetByID($p['PRODUCT_ID']); if (is_null($pp['PURCHASING_PRICE']) == false) { $item['purchasePrice'] = $pp['PURCHASING_PRICE']; } $propCancel = CSaleBasket::GetPropsList(array(), array('BASKET_ID' => $p['ID'], 'CODE' => self::CANCEL_PROPERTY_CODE))->Fetch(); if (!$propCancel || $propCancel && !(int) $propCancel['VALUE']) { $item['discount'] = (double) $p['DISCOUNT_PRICE']; $item['initialPrice'] = (double) $p['PRICE'] + (double) $p['DISCOUNT_PRICE']; } $order['items'][] = $item; } $arUser = CUser::GetByID($arFields['USER_ID'])->Fetch(); $customer = array('externalId' => $arFields['USER_ID'], 'lastName' => $arUser['LAST_NAME'], 'firstName' => $arUser['NAME'], 'patronymic' => $arUser['SECOND_NAME'], 'phones' => array(array('number' => $arUser['PERSONAL_PHONE']), array('number' => $arUser['WORK_PHONE'])), 'createdAt' => new \DateTime($arUser['DATE_REGISTER']), 'contragentType' => $arParams['optionsContragentType'][$arFields['PERSON_TYPE_ID']]); if (function_exists('intarocrm_get_order_type')) { $orderType = intarocrm_get_order_type($arFields); if ($orderType) { $order['orderType'] = $orderType; } } if (function_exists('intarocrm_before_order_send')) { $newResOrder = intarocrm_before_order_send($order); if (is_array($newResOrder) && !empty($newResOrder)) { $order = $newResOrder; } } $customer = $normalizer->normalize($customer, 'customers'); $order = $normalizer->normalize($order, 'orders'); if (isset($arParams['optionsSitesList']) && is_array($arParams['optionsSitesList']) && array_key_exists($arFields['LID'], $arParams['optionsSitesList'])) { $site = $arParams['optionsSitesList'][$arFields['LID']]; } $log = new Logger(); $log->write($customer, 'customer'); $log->write($order, 'order'); if ($send) { if (!self::apiMethod($api, 'customerEdit', __METHOD__, $customer, $site)) { return false; } if ($orderEdit = self::apiMethod($api, 'orderEdit', __METHOD__, $order, $site)) { return $orderEdit; } else { return false; } } return array('order' => $order, 'customer' => $customer); }