CAllCrmQuote::GetNextQuoteNumber

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CAllCrmQuote
  4. GetNextQuoteNumber
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_quote.php
  • Класс: \CAllCrmQuote
  • Вызов: CAllCrmQuote::GetNextQuoteNumber
static function GetNextQuoteNumber($ID, $templateType, $param)
{
	global $DB;
	$value = false;

	switch ($templateType)
	{
		case 'NUMBER':

			$param = intval($param);
			$maxLastID = 0;
			$strSql = '';

			switch($DB->type)
			{
				case "MYSQL":
					$strSql = "SELECT ID, QUOTE_NUMBER FROM b_crm_quote WHERE QUOTE_NUMBER IS NOT NULL ORDER BY ID DESC LIMIT 1";
					break;
				case "ORACLE":
					$strSql = "SELECT ID, QUOTE_NUMBER FROM b_crm_quote WHERE QUOTE_NUMBER IS NOT NULL AND ROWNUM <= 1 ORDER BY ID DESC";
					break;
				case "MSSQL":
					$strSql = "SELECT TOP 1 ID, QUOTE_NUMBER FROM b_crm_quote WHERE QUOTE_NUMBER IS NOT NULL ORDER BY ID DESC";
					break;
			}

			$dbres = $DB->Query($strSql, true);
			if ($arRes = $dbres->GetNext())
			{
				if (mb_strlen($arRes["QUOTE_NUMBER"]) === mb_strlen(intval($arRes["QUOTE_NUMBER"])))
					$maxLastID = intval($arRes["QUOTE_NUMBER"]);
			}

			$value = ($maxLastID >= $param) ? $maxLastID + 1 : $param;
			break;

		case 'PREFIX':

			$value = $param.$ID;
			break;

		case 'RANDOM':

			$rand = randString(intval($param), array("ABCDEFGHIJKLNMOPQRSTUVWXYZ", "0123456789"));
			$dbres = $DB->Query("SELECT ID, QUOTE_NUMBER FROM b_crm_quote WHERE QUOTE_NUMBER = '".$rand."'", true);
			$value = ($arRes = $dbres->GetNext()) ? false : $rand;
			break;

		case 'USER':

			$dbres = $DB->Query("SELECT ASSIGNED_BY_ID FROM b_crm_quote WHERE ID = '".$ID."'", true);

			if ($arRes = $dbres->GetNext())
			{
				$userID = intval($arRes["ASSIGNED_BY_ID"]);
				$strSql = '';

				switch($DB->type)
				{
					case "MYSQL":
						$strSql = "SELECT MAX(CAST(SUBSTRING(QUOTE_NUMBER, LENGTH('".$userID."_') + 1) as UNSIGNED)) as NUM_ID FROM b_crm_quote WHERE QUOTE_NUMBER LIKE '".$userID."\_%'";
						break;
					case "ORACLE":
						$strSql = "SELECT MAX(CAST(SUBSTR(QUOTE_NUMBER, LENGTH('".$userID."_') + 1) as NUMBER)) as NUM_ID FROM b_crm_quote WHERE QUOTE_NUMBER LIKE '".$userID."_%'";
						break;
					case "MSSQL":
						$strSql = "SELECT MAX(CAST(SUBSTRING(QUOTE_NUMBER, LEN('".$userID."_') + 1, LEN(QUOTE_NUMBER)) as INT)) as NUM_ID FROM b_crm_quote WHERE QUOTE_NUMBER LIKE '".$userID."_%'";
						break;
				}

				$dbres = $DB->Query($strSql, true);
				if ($arRes = $dbres->GetNext())
				{
					$numID = (intval($arRes["NUM_ID"]) > 0) ? $arRes["NUM_ID"] + 1 : 1;
					$value = $userID."_".$numID;
				}
				else
					$value = $userID."_1";
			}
			else
				$value = false;

			break;

		case 'DATE':
			$date = '';
			switch ($param)
			{
				// date in the site format but without delimeters
				case 'day':
					$date = date($DB->DateFormatToPHP(CSite::GetDateFormat("SHORT")), mktime(0, 0, 0, date("m"), date("d"), date("Y")));
					$date = preg_replace("/[^0-9]/", "", $date);
					break;
				case 'month':
					$date = date($DB->DateFormatToPHP(str_replace("DD", "", CSite::GetDateFormat("SHORT"))), mktime(0, 0, 0, date("m"), date("d"), date("Y")));
					$date = preg_replace("/[^0-9]/", "", $date);
					break;
				case 'year':
					$date = date('Y');
					break;
			}

			$strSql = '';
			switch($DB->type)
			{
				case "MYSQL":
					$strSql = "SELECT MAX(CAST(SUBSTRING(QUOTE_NUMBER, LENGTH('".$date." / ') + 1) as UNSIGNED)) as NUM_ID FROM b_crm_quote WHERE QUOTE_NUMBER LIKE '".$date." / %'";
					break;
				case "ORACLE":
					$strSql = "SELECT MAX(CAST(SUBSTR(QUOTE_NUMBER, LENGTH('".$date." / ') + 1) as NUMBER)) as NUM_ID FROM b_crm_quote WHERE QUOTE_NUMBER LIKE '".$date." / %'";
					break;
				case "MSSQL":
					$strSql = "SELECT MAX(CAST(SUBSTRING(QUOTE_NUMBER, LEN('".$date." / ') + 1, LEN(QUOTE_NUMBER)) as INT)) as NUM_ID FROM b_crm_quote WHERE QUOTE_NUMBER LIKE '".$date." / %'";
					break;
			}

			$dbres = $DB->Query($strSql, true);
			if ($arRes = $dbres->GetNext())
			{
				$numID = (intval($arRes["NUM_ID"]) > 0) ? $arRes["NUM_ID"] + 1 : 1;
				$value = $date." / ".$numID;
			}
			else
				$value = $date." / 1";

			break;

		default: // if unknown template

			$value = false;
			break;
	}

	return $value;
}

Добавить комментарий