Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
reportmanager.php
1<?php
2
3namespace Bitrix\Sale\Cashbox;
4
5use Bitrix\Main\Entity\ExpressionField;
11
12Loc::loadLanguageFile(__FILE__);
13
14final class ReportManager
15{
19
24 public static function addZReport($cashboxId)
25 {
26 $addResult = Internals\CashboxZReportTable::add(
27 array(
28 'CASHBOX_ID' => $cashboxId,
29 'DATE_CREATE' => new DateTime(),
30 'STATUS' => 'N',
31 'CURRENCY' => 'RUB'
32 )
33 );
34
35 return $addResult->getId();
36 }
37
42 public static function getLastZReport($cashboxId)
43 {
44 $dbRes = Internals\CashboxZReportTable::getList(
45 array(
46 'select' => array('*'),
47 'filter' => array('CASHBOX_ID' => $cashboxId),
48 'order' => array('ID' => 'DESC'),
49 'limit' => 1,
50 )
51 );
52
53 return $dbRes->fetch();
54 }
55
60 public static function getPrintableZReport($cashboxId)
61 {
62 $lastZReport = static::getLastZReport($cashboxId);
63
64 $now = new DateTime();
65 $nowTs = $now->getTimestamp();
66
67 if ($lastZReport && ($lastZReport['STATUS'] === 'N' || $lastZReport['STATUS'] === 'P'))
68 {
69 if ($lastZReport['STATUS'] === 'N')
70 {
71 Internals\CashboxZReportTable::update($lastZReport['ID'], array('STATUS' => 'P', 'DATE_PRINT_START' => new DateTime()));
72 return $lastZReport['ID'];
73 }
74 else
75 {
77 $datePrintStart = $lastZReport['DATE_PRINT_START'];
78 $datePrintStartTs = $datePrintStart->getTimestamp();
79
80 $p = $nowTs - $datePrintStartTs;
81 if ($p > static::MIN_TIME_RESENDING_REPORT && $p < static::MAX_TIME_RESENDING_REPORT)
82 {
83 return $lastZReport['ID'];
84 }
85 elseif ($p >= static::MAX_TIME_RESENDING_REPORT)
86 {
87 Internals\CashboxZReportTable::update($lastZReport['ID'], array('STATUS' => 'E', 'DATE_PRINT_END' => new DateTime()));
88 }
89 }
90 }
91 else
92 {
93 $cashbox = Manager::getCashboxFromCache($cashboxId);
94 $prevPrintDate = new DateTime();
95 $prevPrintDate->setTime($cashbox['SETTINGS']['Z_REPORT']['TIME']['H'], $cashbox['SETTINGS']['Z_REPORT']['TIME']['M']);
96 if ($prevPrintDate->getTimestamp() > $nowTs)
97 $prevPrintDate->add("-1d");
98
100 if ($lastZReport)
101 $datePrintStart = $lastZReport['DATE_PRINT_START'];
102 else
103 $datePrintStart = new DateTime('2017-01-01 00:00:00', 'Y-m-d H:i:s');
104
105 $datePrintStartTs = $datePrintStart->getTimestamp();
106
107 if ($prevPrintDate->getTimestamp() - $datePrintStartTs > static::MIN_TIME_RESENDING_REPORT)
108 {
109 $dbChecksCount = CheckManager::getList(
110 array(
111 'select' => array('CNT'),
112 'filter' => array(
113 'CASHBOX_ID' => $cashboxId,
114 '>=DATE_PRINT_START' => $datePrintStart,
115 '<DATE_PRINT_START' => $prevPrintDate,
116 ),
117 'runtime' => array(
118 new ExpressionField('CNT', 'COUNT(*)')
119 )
120 )
121 );
122 $checksCount = $dbChecksCount->fetch();
123 if ($checksCount && $checksCount['CNT'] > 0)
124 {
125 $reportId = static::addZReport($cashboxId);
126 Internals\CashboxZReportTable::update($reportId, array('STATUS' => 'P', 'DATE_PRINT_START' => new DateTime()));
127 return $reportId;
128 }
129 }
130 }
131
132 return 0;
133 }
134
140 public static function saveZReportPrintResult($reportId, $data)
141 {
142 $result = new Result();
143
144 if ($reportId <= 0)
145 {
146 $result->addError(new Error(Loc::getMessage('SALE_CASHBOX_ERROR_REPORT_ID')));
147 return $result;
148 }
149
150 $report = Internals\CashboxZReportTable::getRowById($reportId);
151 if (!$report)
152 {
153 $result->addError(new Error(Loc::getMessage('SALE_CASHBOX_ERROR_REPORT_NOT_FOUND', array('#REPORT_ID#' => $reportId))));
154 return $result;
155 }
156
157 if ($report['STATUS'] === 'Y')
158 return $result;
159
160 if (isset($data['ERROR']))
161 {
162 $errorMessage = Loc::getMessage('SALE_CASHBOX_ERROR_REPORT_PRINT', array('#REPORT_ID#' => $reportId));
163 if ($data['ERROR']['MESSAGE'])
164 $errorMessage .= ': '.$data['ERROR']['MESSAGE'];
165
166 if ($data['ERROR']['TYPE'] === Errors\Warning::TYPE)
167 {
168 if ($report['CNT_FAIL_PRINT'] >= 3)
169 {
170 $data['ERROR']['TYPE'] = Errors\Error::TYPE;
171 }
172 else
173 {
174 $result->addError(new Errors\Warning($errorMessage));
175 Internals\CashboxZReportTable::update($reportId, array('CNT_FAIL_PRINT' => $report['CNT_FAIL_PRINT']+1));
176 return $result;
177 }
178 }
179
180 if ($data['ERROR']['TYPE'] === Errors\Error::TYPE)
181 {
182 $updatedFields = array('STATUS' => 'E', 'DATE_PRINT_END' => new DateTime());
183 if ((int)$report['CNT_FAIL_PRINT'] === 0)
184 $updatedFields['CNT_FAIL_PRINT'] = 1;
185
186 Internals\CashboxZReportTable::update($reportId, $updatedFields);
187 $error = new Errors\Error($errorMessage);
188 Logger::addError($error->getMessage(), $report['CASHBOX_ID']);
189 }
190 else
191 {
192 $error = new Errors\Warning($errorMessage);
193 Logger::addWarning($error->getMessage(), $report['CASHBOX_ID']);
194 }
195
196 $result->addError($error);
197 }
198 else
199 {
200 $updateResult = Internals\CashboxZReportTable::update(
201 $reportId,
202 array(
203 'STATUS' => 'Y',
204 'DATE_PRINT_END' => new DateTime(),
205 'CASH_SUM' => $data['CASH_SUM'],
206 'CASHLESS_SUM' => $data['CASHLESS_SUM'],
207 'CUMULATIVE_SUM' => $data['CUMULATIVE_SUM'],
208 'RETURNED_SUM' => $data['RETURNED_SUM'],
209 'LINK_PARAMS' => $data['LINK_PARAMS']
210 )
211 );
212
213 if (!$updateResult->isSuccess())
214 $result->addErrors($updateResult->getErrors());
215 }
216
217 return $result;
218 }
219}
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())
static addWarning(?string $message, $cashboxId=null)
Definition logger.php:34
static addError(?string $message, $cashboxId=null)
Definition logger.php:25
static getCashboxFromCache($cashboxId)
Definition manager.php:268
static saveZReportPrintResult($reportId, $data)