/** * Sets quote number * Use OnBeforeQuoteNumberSet event to generate custom quote number. * Quote number value must be unique! By default quote ID is used if generated value is incorrect * * @param int $ID - quote ID * @return bool - true if quote number is set successfully */ private static function SetQuoteNumber($ID) { global $DB; $ID = intval($ID); if ($ID <= 0) { return false; } $type = COption::GetOptionString("crm", "quote_number_template", ""); $param = COption::GetOptionString("crm", "quote_number_data", ""); $bCustomAlgorithm = false; $value = $ID; foreach (GetModuleEvents("crm", "OnBeforeCrmQuoteNumberSet", true) as $arEvent) { $tmpRes = ExecuteModuleEventEx($arEvent, array($ID, $type)); if ($tmpRes !== false) { $bCustomAlgorithm = true; $value = $tmpRes; } } if ($bCustomAlgorithm) { $arFields = array("QUOTE_NUMBER" => $value); $sUpdate = $DB->PrepareUpdate('b_crm_quote', $arFields); $sql = "UPDATE b_crm_quote SET {$sUpdate} WHERE ID = {$ID}"; $res = $DB->Query($sql, true); } else { $res = false; if ($type != "") { for ($i = 0; $i < 10; $i++) { $value = CCrmQuote::GetNextQuoteNumber($ID, $type, $param); if ($value !== false) { $arFields = array("QUOTE_NUMBER" => $value); $sUpdate = $DB->PrepareUpdate('b_crm_quote', $arFields); $sql = "UPDATE b_crm_quote SET {$sUpdate} WHERE ID = {$ID}"; $res = $DB->Query($sql, true); if ($res) { break; } } } } } if ($type == "" || !$res) { $arFields = array("QUOTE_NUMBER" => $ID); $sUpdate = $DB->PrepareUpdate('b_crm_quote', $arFields); $sql = "UPDATE b_crm_quote SET {$sUpdate} WHERE ID = {$ID}"; $res = $DB->Query($sql, true); } return $res; }