diff --git a/app/Http/Controllers/Admin/TahesabController.php b/app/Http/Controllers/Admin/TahesabController.php index ee6e7cb..24c20a8 100644 --- a/app/Http/Controllers/Admin/TahesabController.php +++ b/app/Http/Controllers/Admin/TahesabController.php @@ -8,6 +8,7 @@ use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Schema; use App\Models\Tahesab; use App\Models\Customer; use App\Services\AccountingProductDocMapper; @@ -44,6 +45,10 @@ class TahesabController extends Controller public function table(Request $request) { + if (! Schema::hasTable('tahesab')) { + return response()->json([]); + } + $view = (string) $request->input('view', 'all'); $query = Tahesab::with('customer')->orderByDesc('id'); diff --git a/app/Services/TahesabSyncService.php b/app/Services/TahesabSyncService.php index dde4fae..2bf055d 100644 --- a/app/Services/TahesabSyncService.php +++ b/app/Services/TahesabSyncService.php @@ -218,10 +218,14 @@ class TahesabSyncService /** * خلاصهٔ سندهای جاافتاده / ناموفق برای بنر ادمین. * - * @return array{failed_rows:int,missing_withdraws:int,missing_orders:int,total:int} + * @return array{failed_rows:int,missing_withdraws:int,missing_orders:int,total:int,doc_sync:array} */ public function pendingSummary(?int $customerId = null): array { + if (! Schema::hasTable('tahesab')) { + return $this->emptyPendingPayload(); + } + $failedRows = (int) $this->unsyncedRowsQuery() ->when($customerId, fn ($q) => $q->where('customer_id', $customerId)) ->count(); @@ -244,6 +248,13 @@ class TahesabSyncService */ public function pendingTotals(?int $customerId = null): array { + if (! Schema::hasTable('tahesab')) { + $empty = $this->emptyPendingPayload(); + unset($empty['doc_sync']); + + return $empty; + } + $failedRows = (int) $this->unsyncedRowsQuery() ->when($customerId, fn ($q) => $q->where('customer_id', $customerId)) ->count(); @@ -264,6 +275,10 @@ class TahesabSyncService */ public function peekNextPending(array $skip = []): ?array { + if (! Schema::hasTable('tahesab')) { + return null; + } + $skipRow = array_values(array_filter($skip['row'] ?? [])); $row = $this->unsyncedRowsQuery() ->when($skipRow !== [], fn ($q) => $q->whereNotIn('id', $skipRow)) @@ -309,6 +324,10 @@ class TahesabSyncService */ public function pendingDocs(?int $customerId = null, int $limit = 200): array { + if (! Schema::hasTable('tahesab')) { + return []; + } + $limit = max(1, min(500, $limit)); $rows = []; @@ -556,7 +575,7 @@ class TahesabSyncService private function missingWithdrawQuery(?int $customerId = null, array $exclude = []) { - if (! Schema::hasTable('withdraws')) { + if (! Schema::hasTable('withdraws') || ! Schema::hasTable('tahesab')) { return null; } @@ -601,7 +620,7 @@ class TahesabSyncService private function missingOrderQuery(?int $customerId = null, array $exclude = []) { - if (! Schema::hasTable('orders')) { + if (! Schema::hasTable('orders') || ! Schema::hasTable('tahesab')) { return null; } @@ -686,6 +705,17 @@ class TahesabSyncService return $result !== false; } + private function emptyPendingPayload(): array + { + return [ + 'failed_rows' => 0, + 'missing_withdraws' => 0, + 'missing_orders' => 0, + 'total' => 0, + 'doc_sync' => app(TahesabDocSyncService::class)->snapshot(), + ]; + } + private function hasSyncColumns(): bool { return Schema::hasTable('tahesab') && Schema::hasColumn('tahesab', 'sync_status');