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:
@@ -743,6 +743,13 @@ class TahesabController extends Controller
|
|||||||
return response()->json($result);
|
return response()->json($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function resetLocalDocSync()
|
||||||
|
{
|
||||||
|
$result = $this->sync()->resetLocalSyncedDocs();
|
||||||
|
|
||||||
|
return response()->json($result, ($result['ok'] ?? false) ? 200 : 422);
|
||||||
|
}
|
||||||
|
|
||||||
public function startCustomerOutbound()
|
public function startCustomerOutbound()
|
||||||
{
|
{
|
||||||
$result = app(TahesabDocSyncService::class)->startCustomerOutbound();
|
$result = app(TahesabDocSyncService::class)->startCustomerOutbound();
|
||||||
|
|||||||
@@ -35,6 +35,19 @@ class TahesabDocSyncService
|
|||||||
return $this->publicSnapshot($job);
|
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}
|
* @return array{ok:bool,message:string,doc_sync:array}
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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
|
* @param array{row?:list<int>,withdraw?:list<int>,order?:list<int>} $skip
|
||||||
* @return array{type:string,id:int,label:string}|null
|
* @return array{type:string,id:int,label:string}|null
|
||||||
|
|||||||
@@ -249,6 +249,7 @@ Route::middleware('auth:user')->group(function (){
|
|||||||
Route::post('tahesab/sync-all-pending', [TahesabController::class, 'syncAllPending']);
|
Route::post('tahesab/sync-all-pending', [TahesabController::class, 'syncAllPending']);
|
||||||
Route::post('tahesab/doc-sync/start', [TahesabController::class, 'startDocSync']);
|
Route::post('tahesab/doc-sync/start', [TahesabController::class, 'startDocSync']);
|
||||||
Route::post('tahesab/doc-sync/stop', [TahesabController::class, 'stopDocSync']);
|
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/outbound/start', [TahesabController::class, 'startCustomerOutbound']);
|
||||||
Route::post('tahesab/customer-sync/inbound/start', [TahesabController::class, 'startCustomerInbound']);
|
Route::post('tahesab/customer-sync/inbound/start', [TahesabController::class, 'startCustomerInbound']);
|
||||||
Route::get('tahesab/customer-sync/status', [TahesabController::class, 'customerSyncStatus']);
|
Route::get('tahesab/customer-sync/status', [TahesabController::class, 'customerSyncStatus']);
|
||||||
|
|||||||
Reference in New Issue
Block a user