浏览代码

Merge pull request #141 from andrewdwallo/development-3.x

Development 3.x
3.x
Andrew Wallo 5 个月前
父节点
当前提交
5845800afc
没有帐户链接到提交者的电子邮件

+ 13
- 3
app/Filament/Company/Resources/Purchases/BillResource/Widgets/BillOverview.php 查看文件

8
 use App\Utilities\Currency\CurrencyAccessor;
8
 use App\Utilities\Currency\CurrencyAccessor;
9
 use App\Utilities\Currency\CurrencyConverter;
9
 use App\Utilities\Currency\CurrencyConverter;
10
 use Filament\Widgets\Concerns\InteractsWithPageTable;
10
 use Filament\Widgets\Concerns\InteractsWithPageTable;
11
+use Illuminate\Support\Facades\DB;
11
 use Illuminate\Support\Number;
12
 use Illuminate\Support\Number;
12
 
13
 
13
 class BillOverview extends EnhancedStatsOverviewWidget
14
 class BillOverview extends EnhancedStatsOverviewWidget
29
         $lastMonthTotalSuffix = null;
30
         $lastMonthTotalSuffix = null;
30
 
31
 
31
         if ($activeTab !== 'unpaid') {
32
         if ($activeTab !== 'unpaid') {
32
-            $averagePaymentTime = $this->getPageTableQuery()
33
-                ->whereNotNull('paid_at')
34
-                ->selectRaw('AVG(TIMESTAMPDIFF(DAY, date, paid_at)) as avg_days')
33
+            $driver = DB::getDriverName();
34
+
35
+            $query = $this->getPageTableQuery()
36
+                ->whereNotNull('paid_at');
37
+
38
+            if ($driver === 'pgsql') {
39
+                $query->selectRaw('AVG(EXTRACT(EPOCH FROM (paid_at - date)) / 86400) as avg_days');
40
+            } elseif ($driver === 'mysql') {
41
+                $query->selectRaw('AVG(TIMESTAMPDIFF(DAY, date, paid_at)) as avg_days');
42
+            }
43
+
44
+            $averagePaymentTime = $query
35
                 ->groupBy('company_id')
45
                 ->groupBy('company_id')
36
                 ->reorder()
46
                 ->reorder()
37
                 ->value('avg_days');
47
                 ->value('avg_days');

+ 13
- 3
app/Filament/Company/Resources/Sales/InvoiceResource/Widgets/InvoiceOverview.php 查看文件

8
 use App\Utilities\Currency\CurrencyAccessor;
8
 use App\Utilities\Currency\CurrencyAccessor;
9
 use App\Utilities\Currency\CurrencyConverter;
9
 use App\Utilities\Currency\CurrencyConverter;
10
 use Filament\Widgets\Concerns\InteractsWithPageTable;
10
 use Filament\Widgets\Concerns\InteractsWithPageTable;
11
+use Illuminate\Support\Facades\DB;
11
 use Illuminate\Support\Number;
12
 use Illuminate\Support\Number;
12
 
13
 
13
 class InvoiceOverview extends EnhancedStatsOverviewWidget
14
 class InvoiceOverview extends EnhancedStatsOverviewWidget
75
         $averagePaymentTimeSuffix = null;
76
         $averagePaymentTimeSuffix = null;
76
 
77
 
77
         if ($activeTab !== 'unpaid') {
78
         if ($activeTab !== 'unpaid') {
78
-            $averagePaymentTime = $this->getPageTableQuery()
79
-                ->whereNotNull('paid_at')
80
-                ->selectRaw('AVG(TIMESTAMPDIFF(DAY, approved_at, paid_at)) as avg_days')
79
+            $driver = DB::getDriverName();
80
+
81
+            $query = $this->getPageTableQuery()
82
+                ->whereNotNull('paid_at');
83
+
84
+            if ($driver === 'pgsql') {
85
+                $query->selectRaw('AVG(EXTRACT(EPOCH FROM (paid_at - approved_at)) / 86400) as avg_days');
86
+            } else {
87
+                $query->selectRaw('AVG(TIMESTAMPDIFF(DAY, approved_at, paid_at)) as avg_days');
88
+            }
89
+
90
+            $averagePaymentTime = $query
81
                 ->groupBy('company_id')
91
                 ->groupBy('company_id')
82
                 ->reorder()
92
                 ->reorder()
83
                 ->value('avg_days');
93
                 ->value('avg_days');

+ 32
- 18
app/Services/AccountService.php 查看文件

166
             ->addSelect([
166
             ->addSelect([
167
                 DB::raw("
167
                 DB::raw("
168
                     COALESCE(
168
                     COALESCE(
169
-                        IF(accounts.category IN ('asset', 'expense'),
170
-                            SUM(IF(journal_entries.type = 'debit' AND transactions.posted_at < ?, journal_entries.amount, 0)) -
171
-                            SUM(IF(journal_entries.type = 'credit' AND transactions.posted_at < ?, journal_entries.amount, 0)),
172
-                            SUM(IF(journal_entries.type = 'credit' AND transactions.posted_at < ?, journal_entries.amount, 0)) -
173
-                            SUM(IF(journal_entries.type = 'debit' AND transactions.posted_at < ?, journal_entries.amount, 0))
174
-                        ), 0
169
+                        CASE
170
+                            WHEN accounts.category IN ('asset', 'expense') THEN
171
+                                SUM(CASE WHEN journal_entries.type = 'debit' AND transactions.posted_at < ? THEN journal_entries.amount ELSE 0 END) -
172
+                                SUM(CASE WHEN journal_entries.type = 'credit' AND transactions.posted_at < ? THEN journal_entries.amount ELSE 0 END)
173
+                            ELSE
174
+                                SUM(CASE WHEN journal_entries.type = 'credit' AND transactions.posted_at < ? THEN journal_entries.amount ELSE 0 END) -
175
+                                SUM(CASE WHEN journal_entries.type = 'debit' AND transactions.posted_at < ? THEN journal_entries.amount ELSE 0 END)
176
+                        END, 0
175
                     ) AS starting_balance
177
                     ) AS starting_balance
176
                 "),
178
                 "),
177
                 DB::raw("
179
                 DB::raw("
178
                     COALESCE(SUM(
180
                     COALESCE(SUM(
179
-                        IF(journal_entries.type = 'debit' AND transactions.posted_at BETWEEN ? AND ?, journal_entries.amount, 0)
181
+                        CASE WHEN journal_entries.type = 'debit' AND transactions.posted_at BETWEEN ? AND ? THEN journal_entries.amount ELSE 0 END
180
                     ), 0) AS total_debit
182
                     ), 0) AS total_debit
181
                 "),
183
                 "),
182
                 DB::raw("
184
                 DB::raw("
183
                     COALESCE(SUM(
185
                     COALESCE(SUM(
184
-                        IF(journal_entries.type = 'credit' AND transactions.posted_at BETWEEN ? AND ?, journal_entries.amount, 0)
186
+                        CASE WHEN journal_entries.type = 'credit' AND transactions.posted_at BETWEEN ? AND ? THEN journal_entries.amount ELSE 0 END
185
                     ), 0) AS total_credit
187
                     ), 0) AS total_credit
186
                 "),
188
                 "),
187
             ])
189
             ])
227
             ->addSelect([
229
             ->addSelect([
228
                 DB::raw("
230
                 DB::raw("
229
                     COALESCE(
231
                     COALESCE(
230
-                        IF(accounts.category IN ('asset', 'expense'),
231
-                            SUM(IF(journal_entries.type = 'debit' AND transactions.posted_at < ?, journal_entries.amount, 0)) -
232
-                            SUM(IF(journal_entries.type = 'credit' AND transactions.posted_at < ?, journal_entries.amount, 0)),
233
-                            SUM(IF(journal_entries.type = 'credit' AND transactions.posted_at < ?, journal_entries.amount, 0)) -
234
-                            SUM(IF(journal_entries.type = 'debit' AND transactions.posted_at < ?, journal_entries.amount, 0))
235
-                        ), 0
232
+                        CASE
233
+                            WHEN accounts.category IN ('asset', 'expense') THEN
234
+                                SUM(CASE WHEN journal_entries.type = 'debit' AND transactions.posted_at < ? THEN journal_entries.amount ELSE 0 END) -
235
+                                SUM(CASE WHEN journal_entries.type = 'credit' AND transactions.posted_at < ? THEN journal_entries.amount ELSE 0 END)
236
+                            ELSE
237
+                                SUM(CASE WHEN journal_entries.type = 'credit' AND transactions.posted_at < ? THEN journal_entries.amount ELSE 0 END) -
238
+                                SUM(CASE WHEN journal_entries.type = 'debit' AND transactions.posted_at < ? THEN journal_entries.amount ELSE 0 END)
239
+                        END, 0
236
                     ) AS starting_balance
240
                     ) AS starting_balance
237
                 "),
241
                 "),
238
                 DB::raw("
242
                 DB::raw("
239
                     COALESCE(SUM(
243
                     COALESCE(SUM(
240
-                        IF(journal_entries.type = 'debit' AND transactions.posted_at BETWEEN ? AND ?, journal_entries.amount, 0)
244
+                        CASE WHEN journal_entries.type = 'debit' AND transactions.posted_at BETWEEN ? AND ? THEN journal_entries.amount ELSE 0 END
241
                     ), 0) AS total_debit
245
                     ), 0) AS total_debit
242
                 "),
246
                 "),
243
                 DB::raw("
247
                 DB::raw("
244
                     COALESCE(SUM(
248
                     COALESCE(SUM(
245
-                        IF(journal_entries.type = 'credit' AND transactions.posted_at BETWEEN ? AND ?, journal_entries.amount, 0)
249
+                        CASE WHEN journal_entries.type = 'credit' AND transactions.posted_at BETWEEN ? AND ? THEN journal_entries.amount ELSE 0 END
246
                     ), 0) AS total_credit
250
                     ), 0) AS total_credit
247
                 "),
251
                 "),
248
             ])
252
             ])
374
     public function getUnpaidClientInvoices(?string $asOfDate = null): Builder
378
     public function getUnpaidClientInvoices(?string $asOfDate = null): Builder
375
     {
379
     {
376
         $asOfDate = $asOfDate ?? now()->toDateString();
380
         $asOfDate = $asOfDate ?? now()->toDateString();
381
+        $driver = DB::getDriverName();
382
+
383
+        $datediff = $driver === 'pgsql'
384
+            ? "DATE_PART('day', ?::date - invoices.due_date)"
385
+            : 'DATEDIFF(?, invoices.due_date)';
377
 
386
 
378
         return Invoice::query()
387
         return Invoice::query()
379
             ->select([
388
             ->select([
382
                 'invoices.due_date',
391
                 'invoices.due_date',
383
                 'invoices.amount_due',
392
                 'invoices.amount_due',
384
                 'invoices.currency_code',
393
                 'invoices.currency_code',
385
-                DB::raw('DATEDIFF(?, invoices.due_date) as days_overdue'),
394
+                DB::raw("{$datediff} as days_overdue"),
386
             ])
395
             ])
387
             ->addBinding([$asOfDate], 'select')
396
             ->addBinding([$asOfDate], 'select')
388
             ->unpaid()
397
             ->unpaid()
392
     public function getUnpaidVendorBills(?string $asOfDate = null): Builder
401
     public function getUnpaidVendorBills(?string $asOfDate = null): Builder
393
     {
402
     {
394
         $asOfDate = $asOfDate ?? now()->toDateString();
403
         $asOfDate = $asOfDate ?? now()->toDateString();
404
+        $driver = DB::getDriverName();
405
+
406
+        $datediff = $driver === 'pgsql'
407
+            ? "DATE_PART('day', ?::date - bills.due_date)"
408
+            : 'DATEDIFF(?, bills.due_date)';
395
 
409
 
396
         return Bill::query()
410
         return Bill::query()
397
             ->select([
411
             ->select([
400
                 'bills.due_date',
414
                 'bills.due_date',
401
                 'bills.amount_due',
415
                 'bills.amount_due',
402
                 'bills.currency_code',
416
                 'bills.currency_code',
403
-                DB::raw('DATEDIFF(?, bills.due_date) as days_overdue'),
417
+                DB::raw("{$datediff} as days_overdue"),
404
             ])
418
             ])
405
             ->addBinding([$asOfDate], 'select')
419
             ->addBinding([$asOfDate], 'select')
406
             ->unpaid()
420
             ->unpaid()

正在加载...
取消
保存