file_put_contents(self::OUT_DIR . '/request_error.txt', PHP_EOL . date("d.m.Y H:i:s", time()) . $e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine(), FILE_APPEND);
LogService::apiDataLogs(1, $e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine(), time());
} finally {
- $jsonArray = [];
+ $jsonId = null;
+ $json = null;
+
try {
+ $apiCron = ApiCron::find()->where(['status' => 0])->orderBy(['date' => SORT_ASC])->one();
- $apiCron = ApiCron::find()->where(['status' => 0])->orderBy(['date' => SORT_ASC])->one();
-
- $mess['debug'] = [
- 'is_object' => is_object($apiCron),
- 'class' => get_class($apiCron),
- 'attributes' => $apiCron ? $apiCron->attributes : null,
- 'errors' => $apiCron ? $apiCron->errors : null
- ];
- if (!empty($apiCron) && empty($mess['create_orders'])) {
- $mess['line'][] = __LINE__;
- $jsonId = $apiCron->request_id;
-// $apiCron2 = ApiCron::find()->where(['id' => $apiCron->id])->andWhere(['request_id' => ''])->one();
-// if ($apiCron2) {
-// $apiCron2->status = 1;
-// $apiCron2->date_up = date('Y-m-d H:i:s');
-// $apiCron2->request_id = $mess['request_id'];
-// $apiCron2->save();
-// if ($apiCron2->getErrors()) {
-// LogService::apiErrorLog(json_encode(["error_id" => 1, "error" => $apiCron2->getErrors()], JSON_UNESCAPED_UNICODE));
-// }
-// $mess['request_id'] = $apiCron2->request_id;
-// }
-// if (!empty($mess['request_id'])) {
-// $apiCron->status = 1;
-// $apiCron->date_up = date('Y-m-d H:i:s');
-// $apiCron->save();
-// if ($apiCron->getErrors()) {
-// LogService::apiErrorLog(json_encode(["error_id" => 3, "error" => $apiCron->getErrors()], JSON_UNESCAPED_UNICODE));
-// }
-// }
- $mess['line'][] = __LINE__;
- $jsonDecoded = json_decode($apiCron->json_post, true);
- $mess['line'][] = __LINE__;
- $jsonDecoded['mess'] = $mess;
+ $payload = $mess;
+
+ if ($apiCron) {
+ $claimed = ApiCron::updateAll(
+ [
+ 'status' => 1,
+ 'date_up' => date('Y-m-d H:i:s'),
+ 'request_id' => $mess['request_id'] ?? (string) time(),
+ ],
+ [
+ 'and',
+ ['id' => $apiCron->id],
+ ['status' => 0],
+ ]
+ );
+
+ if ($claimed) {
+ $cronDecoded = json_decode($apiCron->json_post, true) ?: [];
+
+ if (!empty($cronDecoded['request_id'])) {
+ $jsonId = $cronDecoded['request_id'];
+ } else {
+ $jsonId = $apiCron->request_id ?: ($mess['request_id'] ?? (string) time());
+ }
+
+ $payload = $this->mergePayloads($payload, $cronDecoded);
- $createEmployeeArray = $this->getCreateEmployee();
- if (!empty($createEmployeeArray)) {
- $jsonDecoded['create_employee'] = $createEmployeeArray;
+ $createEmployeeArray = $this->getCreateEmployee();
+ if (!empty($createEmployeeArray)) {
+ $payload['create_employee'] = $createEmployeeArray;
+ }
+ }
}
- $json = json_encode($jsonDecoded, JSON_UNESCAPED_UNICODE);
- } else {
- $mess['line'][] = __LINE__;
if (!empty($writeOffIds) && !empty($writeOff)) {
$this->getWriteOffsSetStatusSend($writeOffIds);
}
if (!empty($replacementInvoiceIds) && !empty($replacementInvoice)) {
$this->getReplacementInvoiceSetStatusSend($replacementInvoiceIds);
}
- $json = json_encode($mess, JSON_UNESCAPED_UNICODE);
- }
- } catch (Exception $e) {
- $mess['line'][] = ' error ' . __LINE__ . ' ' . $e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine();
- }
-//
-// if (!empty($mess)) {
-// $jsonArray = array_merge($jsonArray, $mess);
-// $json = json_encode($jsonArray, JSON_UNESCAPED_UNICODE);
-// }
+ if (empty($jsonId)) {
+ $jsonId = $mess['request_id'] ?? (string) time();
+ }
- if (empty($jsonId)) {
- $mess['line'][] = __LINE__;
- $jsonId = time();
+ $json = json_encode($payload, JSON_UNESCAPED_UNICODE);
+
+ } catch (\Throwable $e) {
+ $mess['line'][] = ' error ' . __LINE__ . ' ' . $e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine();
+ $jsonId = $jsonId ?? (string) time();
+ $json = $json ?? json_encode($mess, JSON_UNESCAPED_UNICODE);
}
file_put_contents(self::OUT_DIR . '/request_' . $jsonId . '.json', PHP_EOL . $json);
file_put_contents(self::getPathJson() . '/request_' . $jsonId . '.json', PHP_EOL . $json);
+
$mess['line'][] = __LINE__;
LogService::apiDataLogs(1, json_encode($mess, JSON_UNESCAPED_UNICODE), $jsonId);
Yii::error('Логи запроса ' . $jsonId . ' ' . json_encode($mess, JSON_UNESCAPED_UNICODE));
return $this->asJson(json_decode($json, true));
}
+ private function mergePayloads(array $base, array $override): array
+ {
+ foreach ($override as $key => $val) {
+ if (!array_key_exists($key, $base)) {
+ $base[$key] = $val;
+ continue;
+ }
+
+ if (is_array($base[$key]) && is_array($val) && array_is_list($base[$key]) && array_is_list($val)) {
+ $base[$key] = array_values(array_merge($base[$key], $val));
+ continue;
+ }
+
+ if (is_array($base[$key]) && is_array($val)) {
+ $base[$key] = mergePayloads($base[$key], $val);
+ continue;
+ }
+
+ $base[$key] = $val;
+ }
+ return $base;
+ }
+
+
private function getWriteOffsSetStatusSend($writeOffIds)
{
try {