fix(tahesab): register unlinked customers before sending documents
File cache cannot use Cache::lock, so docs for customers without a code always failed. Register first, put the customer name on the sanad, and surface the real error. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1004,21 +1004,22 @@ class TahesabController extends Controller
|
||||
$isColleagueLeg = ! empty($data['colleague_leg']) && $accountOverride !== '';
|
||||
|
||||
if (! $isColleagueLeg) {
|
||||
if ($customer && ($customer->moshtari_code === null)) {
|
||||
$this->createCustomerInTahesab($customer->id);
|
||||
$customer = Customer::where('id', $data['customer_id'])->first();
|
||||
$ensured = $this->ensureCustomerHasTahesabCode($customer);
|
||||
if (! ($ensured['ok'] ?? false)) {
|
||||
return $ensured['message'] ?? 'مشتری کد تهحساب ندارد';
|
||||
}
|
||||
$customer = $ensured['customer'];
|
||||
}
|
||||
|
||||
if (! $customer) {
|
||||
return false;
|
||||
return 'مشتری سند پیدا نشد';
|
||||
}
|
||||
|
||||
$hesabCode = $isColleagueLeg
|
||||
? (int) $accountOverride
|
||||
: (int) ($customer->moshtari_code ?? 0);
|
||||
if ($hesabCode <= 0) {
|
||||
return false;
|
||||
return 'مشتری کد تهحساب ندارد';
|
||||
}
|
||||
|
||||
$kind = $mapper->resolveKind($product);
|
||||
@@ -1056,6 +1057,7 @@ class TahesabController extends Controller
|
||||
if ($sharh === '') {
|
||||
$sharh = trim($title !== '' ? "{$action} {$title}" : "{$action} {$kindFa}");
|
||||
}
|
||||
$sharh = $this->sharhWithCustomer($sharh, $customer);
|
||||
|
||||
$orderId = is_object($data['order'] ?? null)
|
||||
? ($data['order']->id ?? null)
|
||||
@@ -1138,14 +1140,17 @@ class TahesabController extends Controller
|
||||
// throw new UnprocessableEntityHttpException("Auth needed");
|
||||
// }
|
||||
$customer = Customer::where('id', $data['customer_id'])->first();
|
||||
|
||||
if ($customer && ($customer->moshtari_code === null)) {
|
||||
$this->createCustomerInTahesab($customer->id);
|
||||
$customer = Customer::where('id', $data['customer_id'])->first();
|
||||
$ensured = $this->ensureCustomerHasTahesabCode($customer);
|
||||
if (! ($ensured['ok'] ?? false)) {
|
||||
return $ensured['message'] ?? 'مشتری کد تهحساب ندارد';
|
||||
}
|
||||
$customer = $ensured['customer'];
|
||||
$data['handler'] = 'wallet';
|
||||
$isDeposit = (int) ($data['direction'] ?? 0) === 0;
|
||||
$sharh = self::accountingWalletDesc($isDeposit, $data['note'] ?? null);
|
||||
$sharh = $this->sharhWithCustomer(
|
||||
self::accountingWalletDesc($isDeposit, $data['note'] ?? null),
|
||||
$customer
|
||||
);
|
||||
$tahesab = $existing ?? Tahesab::create([
|
||||
"customer_id" => $customer->id,
|
||||
"desc" => $sharh,
|
||||
@@ -1327,17 +1332,17 @@ class TahesabController extends Controller
|
||||
}
|
||||
|
||||
$customer = Customer::where('id', $data['customer_id'])->first();
|
||||
if (!$customer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($customer->moshtari_code === null) {
|
||||
$this->createCustomerInTahesab($customer->id);
|
||||
$customer = Customer::where('id', $data['customer_id'])->first();
|
||||
$ensured = $this->ensureCustomerHasTahesabCode($customer);
|
||||
if (! ($ensured['ok'] ?? false)) {
|
||||
return $ensured['message'] ?? 'مشتری کد تهحساب ندارد';
|
||||
}
|
||||
$customer = $ensured['customer'];
|
||||
|
||||
$amount = abs((float) $data['value']);
|
||||
$desc = !empty($data['desc']) ? (string) $data['desc'] : 'پاداش';
|
||||
$desc = $this->sharhWithCustomer(
|
||||
! empty($data['desc']) ? (string) $data['desc'] : 'پاداش',
|
||||
$customer
|
||||
);
|
||||
|
||||
$data['handler'] = 'goldReward';
|
||||
$tahesab = $existing ?? Tahesab::create([
|
||||
@@ -1432,11 +1437,11 @@ class TahesabController extends Controller
|
||||
}
|
||||
|
||||
$customer = Customer::where('id', $data['customer_id'])->first();
|
||||
|
||||
if ($customer && ($customer->moshtari_code === null)) {
|
||||
$this->createCustomerInTahesab($customer->id);
|
||||
$customer = Customer::where('id', $data['customer_id'])->first();
|
||||
$ensured = $this->ensureCustomerHasTahesabCode($customer);
|
||||
if (! ($ensured['ok'] ?? false)) {
|
||||
return $ensured['message'] ?? 'مشتری کد تهحساب ندارد';
|
||||
}
|
||||
$customer = $ensured['customer'];
|
||||
|
||||
$kind = $mapper->resolveKind($product);
|
||||
$rawValue = (float) $data['value'];
|
||||
@@ -1447,9 +1452,12 @@ class TahesabController extends Controller
|
||||
$isDeposit = (int) ($data['direction'] ?? 1) === 1;
|
||||
$isVorood = $isDeposit ? 1 : 0;
|
||||
|
||||
$sharh = self::accountingWalletDesc(
|
||||
$isDeposit,
|
||||
!empty($data['note']) ? (string) $data['note'] : (!empty($data['desc']) ? (string) $data['desc'] : null)
|
||||
$sharh = $this->sharhWithCustomer(
|
||||
self::accountingWalletDesc(
|
||||
$isDeposit,
|
||||
! empty($data['note']) ? (string) $data['note'] : (! empty($data['desc']) ? (string) $data['desc'] : null)
|
||||
),
|
||||
$customer
|
||||
);
|
||||
$data['note'] = $sharh;
|
||||
$data['handler'] = 'walletGold';
|
||||
@@ -1643,18 +1651,49 @@ class TahesabController extends Controller
|
||||
];
|
||||
}
|
||||
|
||||
private function ensureCustomerHasTahesabCode(?Customer $customer): array
|
||||
{
|
||||
if (! $customer) {
|
||||
return ['ok' => false, 'message' => 'مشتری سند پیدا نشد'];
|
||||
}
|
||||
if ((int) ($customer->moshtari_code ?? 0) > 0) {
|
||||
return ['ok' => true, 'customer' => $customer];
|
||||
}
|
||||
|
||||
$reg = $this->createCustomerInTahesab($customer->id);
|
||||
$customer->refresh();
|
||||
if ((int) ($customer->moshtari_code ?? 0) > 0) {
|
||||
return ['ok' => true, 'customer' => $customer];
|
||||
}
|
||||
|
||||
$message = is_string($reg) && $reg !== ''
|
||||
? $reg
|
||||
: 'ثبت مشتری در تهحساب ناموفق بود';
|
||||
|
||||
return ['ok' => false, 'message' => $message];
|
||||
}
|
||||
|
||||
private function sharhWithCustomer(string $sharh, Customer $customer): string
|
||||
{
|
||||
$name = trim((string) ($customer->name ?? ''));
|
||||
if ($name === '' || str_contains($sharh, $name)) {
|
||||
return $sharh;
|
||||
}
|
||||
|
||||
return trim($sharh.' — '.$name);
|
||||
}
|
||||
|
||||
public function createCustomerInTahesab($customer_id)
|
||||
{
|
||||
$customer = Customer::findOrFail($customer_id);
|
||||
$response = null;
|
||||
|
||||
if ($customer->moshtari_code == null) {
|
||||
$lock = Cache::lock('tahesab:register-customer:'.$customer->id, 30);
|
||||
|
||||
if (! $lock->get()) {
|
||||
if ((int) ($customer->moshtari_code ?? 0) <= 0) {
|
||||
$lockKey = 'tahesab:register-customer:'.$customer->id;
|
||||
if (! Cache::add($lockKey, 1, 30)) {
|
||||
$customer->refresh();
|
||||
|
||||
if ($customer->moshtari_code != null) {
|
||||
if ((int) ($customer->moshtari_code ?? 0) > 0) {
|
||||
return $customer->moshtari_code;
|
||||
}
|
||||
|
||||
@@ -1664,7 +1703,7 @@ class TahesabController extends Controller
|
||||
try {
|
||||
$customer->refresh();
|
||||
|
||||
if ($customer->moshtari_code != null) {
|
||||
if ((int) ($customer->moshtari_code ?? 0) > 0) {
|
||||
return $customer->moshtari_code;
|
||||
}
|
||||
|
||||
@@ -1697,9 +1736,11 @@ class TahesabController extends Controller
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
return 'tahesab api error!!!';
|
||||
return is_array($response)
|
||||
? ($this->sync()->buildLog($response, 'ثبت مشتری در تهحساب ناموفق بود') ?: 'ثبت مشتری در تهحساب ناموفق بود')
|
||||
: 'ثبت مشتری در تهحساب ناموفق بود';
|
||||
} finally {
|
||||
$lock->release();
|
||||
Cache::forget($lockKey);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user