feat(tahesab): reset local success flags when remote docs were wiped

Clear tahesab_id on locally successful rows so they re-enter the send queue without touching panel customer codes.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-03 13:43:16 +03:30
co-authored by Cursor
parent d65bbce791
commit 3b7db1eb72
4 changed files with 78 additions and 0 deletions
@@ -743,6 +743,13 @@ class TahesabController extends Controller
return response()->json($result);
}
public function resetLocalDocSync()
{
$result = $this->sync()->resetLocalSyncedDocs();
return response()->json($result, ($result['ok'] ?? false) ? 200 : 422);
}
public function startCustomerOutbound()
{
$result = app(TahesabDocSyncService::class)->startCustomerOutbound();
+13
View File
@@ -35,6 +35,19 @@ class TahesabDocSyncService
return $this->publicSnapshot($job);
}
public function isRunning(): bool
{
return ($this->read()['running'] ?? false) === true;
}
public function clearIdleSnapshot(): void
{
if ($this->isRunning()) {
return;
}
Cache::forget(self::CACHE_KEY);
}
/**
* @return array{ok:bool,message:string,doc_sync:array}
*/
+57
View File
@@ -265,6 +265,63 @@ class TahesabSyncService
];
}
/**
* ته‌حساب خالی شده ولی ردیف محلی هنوز موفق است — کد ته‌حساب را پاک کن تا دوباره در صف بیاید.
*
* @return array{ok:bool,message:string,cleared:int,summary:array}
*/
public function resetLocalSyncedDocs(): array
{
$pump = app(TahesabDocSyncService::class);
if ($pump->isRunning()) {
return [
'ok' => false,
'message' => 'اول ارسال جاری را متوقف کنید',
'cleared' => 0,
'summary' => $this->pendingSummary(),
];
}
if (! $this->ensureLocalTable()) {
return [
'ok' => false,
'message' => 'جدول محلی ته‌حساب نیست',
'cleared' => 0,
'summary' => $this->pendingSummary(),
];
}
$query = Tahesab::query()->where(function ($q) {
$q->where(function ($inner) {
$inner->whereNotNull('tahesab_id')->where('tahesab_id', '!=', '');
});
if ($this->hasSyncColumns()) {
$q->orWhere('sync_status', self::STATUS_SUCCESS);
}
});
$cleared = (int) (clone $query)->count();
$update = ['tahesab_id' => null];
if ($this->hasSyncColumns()) {
$update['sync_status'] = self::STATUS_PENDING;
$update['sync_log'] = 'بازنشانی محلی — سند در ته‌حساب پاک شده';
}
if ($cleared > 0) {
(clone $query)->update($update);
}
$pump->clearIdleSnapshot();
return [
'ok' => true,
'message' => $cleared > 0
? ($cleared.' سند محلی دوباره در صف ارسال است')
: 'سند موفق محلی برای بازنشانی نبود',
'cleared' => $cleared,
'summary' => $this->pendingSummary(),
];
}
/**
* @param array{row?:list<int>,withdraw?:list<int>,order?:list<int>} $skip
* @return array{type:string,id:int,label:string}|null