|
@@ -166,22 +166,24 @@ class AccountService
|
166
|
166
|
->addSelect([
|
167
|
167
|
DB::raw("
|
168
|
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
|
177
|
) AS starting_balance
|
176
|
178
|
"),
|
177
|
179
|
DB::raw("
|
178
|
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
|
182
|
), 0) AS total_debit
|
181
|
183
|
"),
|
182
|
184
|
DB::raw("
|
183
|
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
|
187
|
), 0) AS total_credit
|
186
|
188
|
"),
|
187
|
189
|
])
|
|
@@ -227,22 +229,24 @@ class AccountService
|
227
|
229
|
->addSelect([
|
228
|
230
|
DB::raw("
|
229
|
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
|
240
|
) AS starting_balance
|
237
|
241
|
"),
|
238
|
242
|
DB::raw("
|
239
|
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
|
245
|
), 0) AS total_debit
|
242
|
246
|
"),
|
243
|
247
|
DB::raw("
|
244
|
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
|
250
|
), 0) AS total_credit
|
247
|
251
|
"),
|
248
|
252
|
])
|
|
@@ -374,6 +378,11 @@ class AccountService
|
374
|
378
|
public function getUnpaidClientInvoices(?string $asOfDate = null): Builder
|
375
|
379
|
{
|
376
|
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
|
387
|
return Invoice::query()
|
379
|
388
|
->select([
|
|
@@ -382,7 +391,7 @@ class AccountService
|
382
|
391
|
'invoices.due_date',
|
383
|
392
|
'invoices.amount_due',
|
384
|
393
|
'invoices.currency_code',
|
385
|
|
- DB::raw('DATEDIFF(?, invoices.due_date) as days_overdue'),
|
|
394
|
+ DB::raw("{$datediff} as days_overdue"),
|
386
|
395
|
])
|
387
|
396
|
->addBinding([$asOfDate], 'select')
|
388
|
397
|
->unpaid()
|
|
@@ -392,6 +401,11 @@ class AccountService
|
392
|
401
|
public function getUnpaidVendorBills(?string $asOfDate = null): Builder
|
393
|
402
|
{
|
394
|
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
|
410
|
return Bill::query()
|
397
|
411
|
->select([
|
|
@@ -400,7 +414,7 @@ class AccountService
|
400
|
414
|
'bills.due_date',
|
401
|
415
|
'bills.amount_due',
|
402
|
416
|
'bills.currency_code',
|
403
|
|
- DB::raw('DATEDIFF(?, bills.due_date) as days_overdue'),
|
|
417
|
+ DB::raw("{$datediff} as days_overdue"),
|
404
|
418
|
])
|
405
|
419
|
->addBinding([$asOfDate], 'select')
|
406
|
420
|
->unpaid()
|