diff --git a/app/Http/Controllers/Admin/TahesabController.php b/app/Http/Controllers/Admin/TahesabController.php index dc62322..58db7a4 100644 --- a/app/Http/Controllers/Admin/TahesabController.php +++ b/app/Http/Controllers/Admin/TahesabController.php @@ -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(); diff --git a/app/Services/TahesabDocSyncService.php b/app/Services/TahesabDocSyncService.php index 5943bed..de64273 100644 --- a/app/Services/TahesabDocSyncService.php +++ b/app/Services/TahesabDocSyncService.php @@ -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} */ diff --git a/app/Services/TahesabSyncService.php b/app/Services/TahesabSyncService.php index eac1f7c..c1d2c87 100644 --- a/app/Services/TahesabSyncService.php +++ b/app/Services/TahesabSyncService.php @@ -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,withdraw?:list,order?:list} $skip * @return array{type:string,id:int,label:string}|null diff --git a/routes/admin.php b/routes/admin.php index 74cf2fd..eeb0931 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -249,6 +249,7 @@ Route::middleware('auth:user')->group(function (){ Route::post('tahesab/sync-all-pending', [TahesabController::class, 'syncAllPending']); Route::post('tahesab/doc-sync/start', [TahesabController::class, 'startDocSync']); Route::post('tahesab/doc-sync/stop', [TahesabController::class, 'stopDocSync']); + Route::post('tahesab/doc-sync/reset-local', [TahesabController::class, 'resetLocalDocSync']); Route::post('tahesab/customer-sync/outbound/start', [TahesabController::class, 'startCustomerOutbound']); Route::post('tahesab/customer-sync/inbound/start', [TahesabController::class, 'startCustomerInbound']); Route::get('tahesab/customer-sync/status', [TahesabController::class, 'customerSyncStatus']);