реквизиты список полей https://dev.1c-bitrix.ru/rest_help/crm/requisite/requisite_fields.php счета список полей https://dev.1c-bitrix.ru/rest_help/crm/invoice/crm_invoice_fields.php |
if (\Bitrix\Main\Loader::includeModule('socialservices')) { $client = new \Bitrix\socialservices\properties\Client; $arRequisite = $client->getByInn($inn); } |
создать счет
$arInvoiceProps = array(); //получим реквизиты компании $entityRequisite = new \Bitrix\Crm\EntityRequisite; $rsRequisite = $entityRequisite->getList([ "select"=>array("*"), "filter"=>array("ENTITY_ID"=>$companyId,"ENTITY_TYPE_ID"=>CCrmOwnerType::Company), "order"=>array("SORT"=>"desc","ID"=>"desc") ]); $arRequisite = $rsRequisite->fetchAll(); if(count($arRequisite)>0){ //если ревизиты заполнены, берем первые (может быть несколько) $arRequisite = $arRequisite[0]; $arInvoiceProps = array( 9 => $arRequisite["RQ_INN"], //ИНН 10 => $arRequisite["RQ_KPP"], //КПП 11 => $arRequisite["RQ_COMPANY_NAME"], //Название из ревизитов 14 => $arComp["CLIENT_EMAIL"], //email ); $address = new \Bitrix\Crm\EntityAddress(); $dbRes = $address->getList(array( 'filter' => array( 'ENTITY_ID' => $arRequisite['ID'], 'ANCHOR_ID' => $companyId ) )); $arAddresses = array(); while ($arAddr = $dbRes->Fetch()) { if($arAddr["TYPE_ID"]==6){ $arInvoiceProps[12] = $addr["ADDRESS_1"]." ".$addr["ADDRESS_2"];//юр адрес $arInvoiceProps[17] = $addr["POSTAL_CODE"];//индекс $arInvoiceProps[18] = $addr["CITY"];//город } } } else{ //если не заполнены реквизиты $arInvoiceProps = array( 11 => $arComp["TITLE"], //Название из карточки 14 => $arComp["CLIENT_EMAIL"] //email ); } $arInvoiceProps[15] = ""; //телефон $arInvoiceProps[13] = ""; //контактное лицо $arFieldsInvoice = Array( "ORDER_TOPIC" => $arFields["TITLE"], "STATUS_ID" => "N", "DATE_INSERT" => date("d.m.Y H:i:s", strtotime("+0 hours")), "DATE_BILL" => date("d.m.Y", strtotime("+0 hours")), "PAY_VOUCHER_DATE" => "", "DATE_PAY_BEFORE" => "", "RESPONSIBLE_ID" => 1, "COMMENTS" => "", "USER_DESCRIPTION" => "", "UF_QUOTE_ID" => 0, "UF_DEAL_ID" => $arFields["ID"], "UF_COMPANY_ID" => $companyId, "UF_CONTACT_ID" => 0, "UF_MYCOMPANY_ID" => 9, "PRODUCT_ROWS" => Array( Array( "ID" => 0, "PRODUCT_ID" => 0, "PRODUCT_NAME" => "Оказание услуг бла-бла", "QUANTITY" =>$quantity, "PRICE" => $price, "VAT_RATE" => 0, "DISCOUNT_PRICE" => 0, "MEASURE_CODE" => 356, "MEASURE_NAME" => "ч", "CUSTOMIZED" => "Y" ) ), "PERSON_TYPE_ID" => 1, "PAY_SYSTEM_ID" => 1, "INVOICE_PROPERTIES" => $arInvoiceProps, UF_CRM_1567860613 => 1 //доп свойства счета ); $oCrmInvoice = new CCrmInvoice(false); $result = $oCrmInvoice->Add($arFieldsInvoice); |
EntityLink::register(5,$invoiceID,0,0,6,2); |
$oCrmInvoice = new CCrmInvoice(false); $arFilter = array( "CHECK_PERMISSIONS"=>"N", "UF_DEAL_ID" => $arDealIds, "CANCELED"=>"N" ); $arSelect = array("ID","UF_DEAL_ID"); $res = CCrmInvoice::GetList($arOrder = Array("ID"=>"DESC"), $arFilter ,false, false, $arSelect); while($row = $res->Fetch()){ $oCrmInvoice->Update($row["ID"],array("CANCELED"=>"Y","STATUS_ID"=>"D")); //деактивируем счета } |
$link = CCrmInvoice::getPublicLink($invoiceId); |
$entityRequisite = new \Bitrix\Crm\EntityRequisite; $rsRequisite = $entityRequisite->getList([ "select"=>array("*"), "filter"=>array("ENTITY_ID"=>$companyId,"ENTITY_TYPE_ID"=>CCrmOwnerType::Company), "order"=>array("SORT"=>"desc","ID"=>"desc") ]); $arRequisite = $rsRequisite->fetchAll(); $arResult["REQUISITE"] = $arRequisite[0]; if(isset($arResult["REQUISITE"]['ID']) && $arResult["REQUISITE"]['ID']>0){ $bank = new \Bitrix\Crm\EntityBankDetail(); $dbRes = $bank->getList(array( 'filter' => array('ENTITY_ID' => $arResult["REQUISITE"]['ID']) )); $arResult["BANK"] = $dbRes->Fetch(); $address = new \Bitrix\Crm\EntityAddress(); $dbRes = $address->getList(array( 'filter' => array( 'ENTITY_ID' => $arResult["REQUISITE"]['ID'], //'ANCHOR_ID' => $companyId ) )); $arResult["ADDRESS"] = array(); while ($arAddr = $dbRes->Fetch()) { if($arAddr["TYPE_ID"]==6){ $arResult["ADDRESS"]["OFFICIAL"] = $arAddr; } elseif($arAddr["TYPE_ID"]==2){ $arResult["ADDRESS"]["FACT"] = $arAddr; } } } |
по ID пользователя получим компанию, к которой он относится, получим реквизиты компании и их проапдейтим
global $USER; if(is_object($USER) && $USER->IsAuthorized()){ $rsUser = CUser::GetList($by, $order, array("ID" => $USER->GetID()), array( "SELECT" => array("ID","UF_CONTACT"), ) ); if($arUser = $rsUser->Fetch()){ $companyId = intval($arUser["UF_CONTACT"]); if($companyId>0 && CModule::IncludeModule('crm')){ $arFilter = array("ID"=>$companyId,"CHECK_PERMISSIONS"=>"N"); $arSelect = array("ID","TITLE"); $rsCompany = CCrmCompany::GetList(Array(),$arFilter,$arSelect); if($arCompany = $rsCompany->Fetch()){ $entityRequisite = new \Bitrix\Crm\EntityRequisite; $rsRequisite = $entityRequisite->getList([ "select"=>array("*"), "filter"=>array( "ENTITY_ID"=>$companyId, "ID"=> intval($_REQUEST["req_id"]), "ENTITY_TYPE_ID"=>CCrmOwnerType::Company ), "order"=>array("SORT"=>"desc","ID"=>"desc") ]); $arRequisiteOld = $rsRequisite->fetchAll(); $arRequisiteTemp = $arRequisiteOld[0]; if(isset($arRequisiteTemp['ID']) && (intval($arRequisiteTemp['ID'])>0)){ $arRequisite = $_REQUEST['fields']; if($_REQUEST["type_company"] == "ip"){ //ИП $arFields = array( 'NAME' => $arRequisite['RQ_COMPANY_NAME'], 'ACTIVE' => 'Y', 'RQ_LAST_NAME' => $arRequisite['RQ_LAST_NAME'], 'RQ_FIRST_NAME' => $arRequisite["RQ_FIRST_NAME"], 'RQ_SECOND_NAME' => $arRequisite["RQ_SECOND_NAME"], 'RQ_COMPANY_FULL_NAME' => $arRequisite['RQ_COMPANY_FULL_NAME'], 'RQ_INN' => $arRequisite["RQ_INN"], //инн 'RQ_OGRNIP' => $arRequisite["RQ_OGRNIP"], //огрн 'RQ_OKVED' => "", ); } else{ //юр лицо $arFields = array( 'NAME' => $arRequisite['RQ_COMPANY_NAME'], 'SORT' => 500, 'ACTIVE' => 'Y', 'RQ_COMPANY_NAME' => $arRequisite['RQ_COMPANY_NAME'], 'RQ_COMPANY_FULL_NAME' => $arRequisite['RQ_COMPANY_FULL_NAME'], 'RQ_INN' => $arRequisite["RQ_INN"], //инн 'RQ_KPP' => $arRequisite["RQ_KPP"], //кпп 'RQ_OGRN' => $arRequisite["RQ_OGRN"], //огрн 'RQ_COMPANY_REG_DATE' => "", 'RQ_OKVED' => "", 'RQ_DIRECTOR' => $arRequisite["RQ_DIRECTOR"], 'RQ_ACCOUNTANT' => $arRequisite["RQ_ACCOUNTANT"] ); } $entityRequisite->Update(intval($arRequisiteTemp['ID']),$arFields); $bank = new \Bitrix\Crm\EntityBankDetail(); $dbRes = $bank->getList(array( 'filter' => array('ENTITY_ID' => $arRequisiteTemp['ID']) )); $arBankOld = $dbRes->Fetch(); $arFields = array( "RQ_BANK_NAME" => $arRequisite["RQ_BANK_NAME"], "RQ_BIK" => $arRequisite["RQ_BIK"], "RQ_ACC_NUM" => $arRequisite["RQ_ACC_NUM"], "RQ_COR_ACC_NUM" => $arRequisite["RQ_COR_ACC_NUM"], "RQ_BANK_ADDR" => $arRequisite["RQ_BANK_ADDR"], ); if($arBankOld["ID"] >0){ $bank->Update(intval($arBankOld["ID"]),$arFields); } $address = new \Bitrix\Crm\EntityAddress(); $address->deleteByEntity(2, $arRequisiteTemp['ID']); $address->deleteByEntity(6, $arRequisiteTemp['ID']); //юр адрес $arFields = array( "ADDRESS_1" => $arRequisite["company_addr_address_1"], "ADDRESS_2" => $arRequisite["company_addr_address_2"], "CITY" => $arRequisite["company_addr_city"], "POSTAL_CODE" => $arRequisite["company_addr_postal_code"], "COUNTRY" => $arRequisite["company_addr_country"], "REGION" => $arRequisite["company_addr_region"], "PROVINCE" => $arRequisite["company_addr_province"], ); $res = $address->register(8, $arRequisiteTemp['ID'], 6, $arFields); //факт адрес $arFields = array( "ADDRESS_1" => $arRequisite["company_realaddr_address_1"], "ADDRESS_2" => $arRequisite["company_realaddr_address_2"], "CITY" => $arRequisite["company_realaddr_city"], "POSTAL_CODE" => $arRequisite["company_realaddr_postal_code"], "COUNTRY" => $arRequisite["company_realaddr_country"], "REGION" => $arRequisite["company_realaddr_region"], "PROVINCE" => $arRequisite["company_realaddr_province"], ); $res = $address->register(8, $arRequisiteTemp['ID'], 2, $arFields); } } } } } |
$arFieldsComp = array( 'TITLE' => (strlen($arNewUser["INN"])==10)?$arRequisite["NAME_SHORT"]:"ИП ".$arRequisite["LAST_NAME"]." ".substr($arRequisite["NAME"], 0,1).". ".substr($arRequisite["SECOND_NAME"],0,1).".", "ASSIGNED_BY_ID" => $manager, //'CONTACT_ID' => array($iContactID),//здесь привязываем к компании контакт (обязательно с array()) UF_CRM_TARIFF => 154 //почасовое обслуживание ); $companyId = $cCompany->Add($arFieldsComp); if($companyId >0){ //обновим пользователя, добавим компанию $user = new CUser; if(!$user->Update($arNewUser["ID"], array("UF_CONTACT"=>$companyId))){ //не смогли обновить пользователя } //добавим реквизиты if(strlen($arNewUser["INN"])==10){ //юр лицо $arFields = array( 'ENTITY_ID' => $companyId, 'ENTITY_TYPE_ID' => CCrmOwnerType::Company, 'PRESET_ID' => 1,//организация 'NAME' => $arRequisite['NAME_SHORT'], 'SORT' => 500, 'ACTIVE' => 'Y', 'RQ_COMPANY_NAME' => $arRequisite['NAME_SHORT'], 'RQ_COMPANY_FULL_NAME' => $arRequisite['NAME'], 'RQ_INN' => $arRequisite["INN"], //инн 'RQ_KPP' => $arRequisite["KPP"], //кпп 'RQ_OGRN' => $arRequisite["OGRN"], //огрн 'RQ_COMPANY_REG_DATE' => $arRequisite["CREATION_REGISTRATION_DATE"], 'RQ_OKVED' => $arRequisite["OKVED_CODE"], ); if(isset($arRequisite["OFFICIALS"][0]["LAST_NAME"])){ $arFields["RQ_DIRECTOR"] = $arRequisite["OFFICIALS"][0]["LAST_NAME"]." ".$arRequisite["OFFICIALS"][0]["NAME"]." ".$arRequisite["OFFICIALS"][0]["SECOND_NAME"]; } } else{//ИП $arFields = array( 'ENTITY_ID' => $companyId, 'ENTITY_TYPE_ID' => CCrmOwnerType::Company, 'PRESET_ID' => 2,//ИП 'NAME' => "ИП ".$arRequisite["LAST_NAME"]." ".$arRequisite["NAME"]." ".$arRequisite["SECOND_NAME"], 'SORT' => 500, 'ACTIVE' => 'Y', 'RQ_LAST_NAME' => $arRequisite['LAST_NAME'], 'RQ_FIRST_NAME' => $arRequisite["NAME"], 'RQ_SECOND_NAME' => $arRequisite["SECOND_NAME"], 'RQ_COMPANY_FULL_NAME' => $arRequisite['NAME'], 'RQ_INN' => $arRequisite["INN"], //инн 'RQ_OGRNIP' => $arRequisite["OGRNIP"], //огрн 'RQ_OKVED' => $arRequisite["OKVED_CODE"], ); } $res = $entityRequisite->add($arFields); if($res->getId() > 0){ //адрес добавляем $address = new \Bitrix\Crm\EntityAddress(); $address->register(8, $res->getId(), 6, array( "ADDRESS_1" =>$arRequisite["ADDRESS_STREET_NAME"]." ".$arRequisite["ADDRESS_STREET_TYPE"]." ".$arRequisite["ADDRESS_HOUSE"]." ".$arRequisite["ADDRESS_BUILDING"], "ADDRESS_2" => $arRequisite["ADDRESS_FLAT"], "CITY" => $arRequisite["ADDRESS_REGION_NAME"], "POSTAL_CODE" => $arRequisite["ADDRESS_INDEX"], "COUNTRY" => "РФ" )); } //создаем контакт $arFields = array( "NAME" => $arNewUser["NAME"], "LAST_NAME" => $arNewUser["LAST_NAME"], "POST" => "не указано",//должность "OPENED" => "N", //открыто для других пользователей "EXPORT" => "Y",//участвует в экспорте 'FM' => array(//почта, телефон 'EMAIL' => array( 'n0' => array('VALUE' => $arNewUser["EMAIL"], 'VALUE_TYPE' => 'WORK') ), 'PHONE' => array( 'n0' => array('VALUE' => $arNewUser["PERSONAL_MOBILE"], 'VALUE_TYPE' => 'WORK') ) ), "COMPANY_ID" => $companyId, "ASSIGNED_BY_ID" => $manager,//id ответственного менеджера ); //создаем контакт $oContact = new \CCrmContact(false); $oContact->add($arFields); if($oContact->LAST_ERROR != ""){ //не создался контакт } } |
AddEventHandler("crm", "OnBeforeCrmInvoiceAdd", "printInvoice"); AddEventHandler("crm", "OnBeforeCrmInvoiceUpdate", "printInvoice"); AddEventHandler("crm", "OnAfterCrmInvoiceAdd", "printInvoice"); function printInvoice (&$arFields){ } |