feat: shayegan inbound charges workflow and copper kilogram unit enforcement

This commit is contained in:
2026-11-05 17:15:38 +03:30
parent e65b8053fd
commit 7dd5304638
13 changed files with 787 additions and 18 deletions
+67 -11
View File
@@ -94,11 +94,16 @@ class ShayeganSyncService
/**
* @return array{ok:bool,message:string,moshtari_code?:string}
*/
public function registerCustomer(Customer $customer): array
public function registerCustomer(Customer $customer, ?string $customJobNumber = null): array
{
$customer->refresh();
if (! empty($customer->moshtari_code)) {
$customJobNumber = $customJobNumber !== null ? trim($customJobNumber) : null;
if ($customJobNumber === '') {
$customJobNumber = null;
}
if ($customJobNumber === null && ! empty($customer->moshtari_code)) {
return [
'ok' => true,
'message' => 'مشتری قبلاً در شایگان ثبت شده است',
@@ -119,8 +124,8 @@ class ShayeganSyncService
$name = 'مشتری ' . $customer->id;
}
// تفصیلی شناور: کل ثابت + کد کاربری ۷رقمی. مشتری ۱ → 1210000001
$requestedAccountNumber = $this->customerTafsilNumber((int) $customer->id);
// تفصیلی شناور: اگر کد دستی ارسال شده باشد همان کد استفاده می‌شود، در غیر این صورت فرمول پیش‌فرض
$requestedAccountNumber = $customJobNumber ?? $this->customerTafsilNumber((int) $customer->id);
$guid = (string) Str::uuid();
$put = [[
@@ -138,9 +143,22 @@ class ShayeganSyncService
$result = $this->client->put('Job/Put', $put);
$accountNumber = trim((string) ($result['number'] ?? ''));
if ($accountNumber === '' && $customJobNumber !== null && ! empty($result['ok'])) {
$accountNumber = $customJobNumber;
}
$exchange = $this->client->debugExchange($result);
if (! empty($result['timeout'])) {
$this->logCustomerRegistrationDoc(
$customer,
$requestedAccountNumber,
$guid,
$put,
$result,
self::STATUS_TIMEOUT,
(string) ($result['message'] ?? 'timeout')
);
return array_merge([
'ok' => false,
'message' => (string) ($result['message'] ?? 'timeout'),
@@ -148,15 +166,56 @@ class ShayeganSyncService
}
if (empty($result['ok']) || $accountNumber === '') {
$errorMsg = (string) ($result['message'] ?? 'ثبت حساب مشتری در شایگان ناموفق بود');
$this->logCustomerRegistrationDoc(
$customer,
$requestedAccountNumber,
$guid,
$put,
$result,
self::STATUS_FAILED,
$errorMsg
);
return array_merge([
'ok' => false,
'message' => (string) ($result['message'] ?? 'ثبت حساب مشتری در شایگان ناموفق بود'),
'message' => $errorMsg,
], $exchange);
}
$customer->moshtari_code = $accountNumber;
$customer->save();
$this->logCustomerRegistrationDoc(
$customer,
$accountNumber,
$guid,
$put,
$result,
self::STATUS_SUCCESS,
null
);
return array_merge([
'ok' => true,
'message' => 'تفصیلی شناور مشتری در شایگان ایجاد شد',
'moshtari_code' => $accountNumber,
], $exchange);
}
/**
* @param array<int, mixed> $put
* @param array<string, mixed> $result
*/
private function logCustomerRegistrationDoc(
Customer $customer,
string $accountNumber,
string $guid,
array $put,
array $result,
string $status,
?string $logMessage = null
): Shayegan {
$row = new Shayegan();
$row->customer_id = (int) $customer->id;
$row->order_id = null;
@@ -170,7 +229,8 @@ class ShayeganSyncService
$row->request_guid = $guid;
$row->shayegan_number = $accountNumber;
$row->shayegan_guid = $result['guid'] ?? null;
$row->sync_status = self::STATUS_SUCCESS;
$row->sync_status = $status;
$row->sync_log = $logMessage;
$row->data = json_encode($this->client->redactPayload([
'Config' => $this->client->buildConfig(),
'PutObject' => $put,
@@ -178,11 +238,7 @@ class ShayeganSyncService
$row->res = json_encode($result['body'] ?? [], JSON_UNESCAPED_UNICODE);
$row->save();
return array_merge([
'ok' => true,
'message' => 'تفصیلی شناور مشتری در شایگان ایجاد شد',
'moshtari_code' => $accountNumber,
], $exchange);
return $row;
}
/**