fix(tahesab): do not 500 pending-summary when tahesab table is missing

Gholaktala has no local tahesab table; count queries now return empty pending stats instead of QueryException.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-02 21:00:22 +03:30
co-authored by Cursor
parent c5fd3b45fe
commit ad12de87c7
2 changed files with 38 additions and 3 deletions
@@ -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');
+33 -3
View File
@@ -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');