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:
@@ -436,14 +436,10 @@ class TahesabSyncService
|
||||
return $this->retry($row);
|
||||
}
|
||||
if ($type === 'withdraw') {
|
||||
$ok = $this->backfillWithdraw($controller, $id);
|
||||
|
||||
return ['ok' => $ok, 'message' => $ok ? 'سند شارژ/برداشت ثبت شد' : 'ارسال شارژ/برداشت ناموفق بود'];
|
||||
return $this->backfillWithdraw($controller, $id);
|
||||
}
|
||||
if ($type === 'order') {
|
||||
$ok = $this->backfillOrder($controller, $id);
|
||||
|
||||
return ['ok' => $ok, 'message' => $ok ? 'سند سفارش ثبت شد' : 'ارسال سفارش ناموفق بود'];
|
||||
return $this->backfillOrder($controller, $id);
|
||||
}
|
||||
|
||||
return ['ok' => false, 'message' => 'نوع نامعتبر است'];
|
||||
@@ -506,12 +502,12 @@ class TahesabSyncService
|
||||
foreach ($this->missingWithdrawIds(max(0, $limit - $used), $customerId) as $withdrawId) {
|
||||
$used++;
|
||||
try {
|
||||
$ok = $this->backfillWithdraw($controller, (int) $withdrawId);
|
||||
if ($ok) {
|
||||
$sent = $this->backfillWithdraw($controller, (int) $withdrawId);
|
||||
if ($sent['ok'] ?? false) {
|
||||
$backfilledWithdraws++;
|
||||
} else {
|
||||
$failed++;
|
||||
$errors[] = 'شارژ/برداشت #'.$withdrawId.': ارسال به تهحساب ناموفق';
|
||||
$errors[] = 'شارژ/برداشت #'.$withdrawId.': '.($sent['message'] ?? 'ارسال به تهحساب ناموفق');
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$failed++;
|
||||
@@ -523,12 +519,12 @@ class TahesabSyncService
|
||||
foreach ($this->missingOrderIds(max(0, $limit - $used), $customerId) as $orderId) {
|
||||
$used++;
|
||||
try {
|
||||
$ok = $this->backfillOrder($controller, (int) $orderId);
|
||||
if ($ok) {
|
||||
$sent = $this->backfillOrder($controller, (int) $orderId);
|
||||
if ($sent['ok'] ?? false) {
|
||||
$backfilledOrders++;
|
||||
} else {
|
||||
$failed++;
|
||||
$errors[] = 'سفارش #'.$orderId.': ارسال به تهحساب ناموفق';
|
||||
$errors[] = 'سفارش #'.$orderId.': '.($sent['message'] ?? 'ارسال به تهحساب ناموفق');
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$failed++;
|
||||
@@ -643,48 +639,64 @@ class TahesabSyncService
|
||||
return $query;
|
||||
}
|
||||
|
||||
private function backfillWithdraw(TahesabController $controller, int $withdrawId): bool
|
||||
private function outcomeFromController($result, string $okMessage, string $failMessage): array
|
||||
{
|
||||
if (is_array($result)) {
|
||||
return ['ok' => true, 'message' => $okMessage];
|
||||
}
|
||||
if (is_string($result) && $result !== '') {
|
||||
return ['ok' => false, 'message' => $result];
|
||||
}
|
||||
|
||||
return ['ok' => false, 'message' => $failMessage];
|
||||
}
|
||||
|
||||
private function backfillWithdraw(TahesabController $controller, int $withdrawId): array
|
||||
{
|
||||
$withdraw = \App\Models\Withdraw::query()->find($withdrawId);
|
||||
if (! $withdraw || ! $withdraw->customer_id) {
|
||||
return false;
|
||||
return ['ok' => false, 'message' => 'شارژ بدون مشتری است'];
|
||||
}
|
||||
|
||||
$cash = (float) ($withdraw->cash ?? 0);
|
||||
$gold = (float) ($withdraw->gold ?? 0);
|
||||
|
||||
if (abs($cash) > 1e-9) {
|
||||
$result = $controller->wallet([
|
||||
'customer_id' => (int) $withdraw->customer_id,
|
||||
'value' => abs($cash),
|
||||
'direction' => $cash >= 0 ? 0 : 1,
|
||||
'withdraw_id' => $withdrawId,
|
||||
'note' => $withdraw->note,
|
||||
]);
|
||||
|
||||
return $result !== false;
|
||||
return $this->outcomeFromController(
|
||||
$controller->wallet([
|
||||
'customer_id' => (int) $withdraw->customer_id,
|
||||
'value' => abs($cash),
|
||||
'direction' => $cash >= 0 ? 0 : 1,
|
||||
'withdraw_id' => $withdrawId,
|
||||
'note' => $withdraw->note,
|
||||
]),
|
||||
'سند شارژ/برداشت ثبت شد',
|
||||
'ارسال شارژ/برداشت ناموفق بود'
|
||||
);
|
||||
}
|
||||
|
||||
if (abs($gold) > 1e-9) {
|
||||
$result = $controller->walletGold([
|
||||
'customer_id' => (int) $withdraw->customer_id,
|
||||
'value' => abs($gold),
|
||||
'direction' => $gold >= 0 ? 1 : 0,
|
||||
'withdraw_id' => $withdrawId,
|
||||
'note' => $withdraw->note,
|
||||
]);
|
||||
|
||||
return $result !== false;
|
||||
return $this->outcomeFromController(
|
||||
$controller->walletGold([
|
||||
'customer_id' => (int) $withdraw->customer_id,
|
||||
'value' => abs($gold),
|
||||
'direction' => $gold >= 0 ? 1 : 0,
|
||||
'withdraw_id' => $withdrawId,
|
||||
'note' => $withdraw->note,
|
||||
]),
|
||||
'سند شارژ/برداشت ثبت شد',
|
||||
'ارسال شارژ/برداشت ناموفق بود'
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
return ['ok' => false, 'message' => 'مبلغ شارژ صفر است'];
|
||||
}
|
||||
|
||||
private function backfillOrder(TahesabController $controller, int $orderId): bool
|
||||
private function backfillOrder(TahesabController $controller, int $orderId): array
|
||||
{
|
||||
$order = \App\Models\Order::query()->with('product')->find($orderId);
|
||||
if (! $order || ! $order->customer_id) {
|
||||
return false;
|
||||
return ['ok' => false, 'message' => 'سفارش بدون مشتری است'];
|
||||
}
|
||||
|
||||
$product = $order->product;
|
||||
@@ -708,9 +720,11 @@ class TahesabSyncService
|
||||
'order' => ['id' => $orderId, 'product_id' => $order->product_id],
|
||||
];
|
||||
|
||||
$result = $controller->storeTrade($payload);
|
||||
|
||||
return $result !== false;
|
||||
return $this->outcomeFromController(
|
||||
$controller->storeTrade($payload),
|
||||
'سند سفارش ثبت شد',
|
||||
'ارسال سفارش ناموفق بود'
|
||||
);
|
||||
}
|
||||
|
||||
public function ensureLocalTable(): bool
|
||||
|
||||
Reference in New Issue
Block a user