• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/cashbox/reportmanager.php
  • Класс: BitrixSaleCashboxReportManager
  • Вызов: ReportManager::getPrintableZReport
static function getPrintableZReport($cashboxId)
{
	$lastZReport = static::getLastZReport($cashboxId);

	$now = new DateTime();
	$nowTs = $now->getTimestamp();

	if ($lastZReport && ($lastZReport['STATUS'] === 'N' || $lastZReport['STATUS'] === 'P'))
	{
		if ($lastZReport['STATUS'] === 'N')
		{
			InternalsCashboxZReportTable::update($lastZReport['ID'], array('STATUS' => 'P', 'DATE_PRINT_START' => new DateTime()));
			return $lastZReport['ID'];
		}
		else
		{
			/** @var Date $datePrintStart */
			$datePrintStart = $lastZReport['DATE_PRINT_START'];
			$datePrintStartTs = $datePrintStart->getTimestamp();

			$p = $nowTs - $datePrintStartTs;
			if ($p > static::MIN_TIME_RESENDING_REPORT && $p < static::MAX_TIME_RESENDING_REPORT)
			{
				return $lastZReport['ID'];
			}
			elseif ($p >= static::MAX_TIME_RESENDING_REPORT)
			{
				InternalsCashboxZReportTable::update($lastZReport['ID'], array('STATUS' => 'E', 'DATE_PRINT_END' => new DateTime()));
			}
		}
	}
	else
	{
		$cashbox = Manager::getCashboxFromCache($cashboxId);
		$prevPrintDate = new DateTime();
		$prevPrintDate->setTime($cashbox['SETTINGS']['Z_REPORT']['TIME']['H'], $cashbox['SETTINGS']['Z_REPORT']['TIME']['M']);
		if ($prevPrintDate->getTimestamp() > $nowTs)
			$prevPrintDate->add("-1d");

		/** @var Date $datePrintStart */
		if ($lastZReport)
			$datePrintStart = $lastZReport['DATE_PRINT_START'];
		else
			$datePrintStart = new DateTime('2017-01-01 00:00:00', 'Y-m-d H:i:s');

		$datePrintStartTs = $datePrintStart->getTimestamp();

		if ($prevPrintDate->getTimestamp() - $datePrintStartTs > static::MIN_TIME_RESENDING_REPORT)
		{
			$dbChecksCount = CheckManager::getList(
				array(
					'select' => array('CNT'),
					'filter' => array(
						'CASHBOX_ID' => $cashboxId,
						'>=DATE_PRINT_START' => $datePrintStart,
						' $prevPrintDate,
					),
					'runtime' => array(
						new ExpressionField('CNT', 'COUNT(*)')
					)
				)
			);
			$checksCount = $dbChecksCount->fetch();
			if ($checksCount && $checksCount['CNT'] > 0)
			{
				$reportId = static::addZReport($cashboxId);
				InternalsCashboxZReportTable::update($reportId, array('STATUS' => 'P', 'DATE_PRINT_START' => new DateTime()));
				return $reportId;
			}
		}
	}

	return 0;
}