gpt4 book ai didi

mysql - Laravel - 使用 Laravel 查询老化报告

转载 作者:行者123 更新时间:2023-11-30 21:30:16 25 4
gpt4 key购买 nike

我想将 MySQL 查询转换为 Laravel。

SELECT client_id,
DATEDIFF(CURDATE(), due_date) AS days_past_due,
SUM(IF(days_past_due = 0, amount_due, 0)),
SUM(IF(days_past_due BETWEEN 1 AND 30, amount_due, 0)),
SUM(IF(days_past_due BETWEEN 31 AND 60, amount_due, 0)),
SUM(IF(days_past_due BETWEEN 61 AND 90, amount_due, 0)),
SUM(IF(days_past_due > 90, amount_due, 0))
FROM invoices
GROUP BY client_id

我想使用 Laravel 查询以矩阵格式创建当前 30、60、90 天的时效报告。

例如,

ClientName 当前 1-30 31-60 >90 总计

AAA 3000 1500 4500

BBB 2000 200 2200

总计 3000 3500 200 6700

我想得到这样的报告。当用户输入日期时,它应该检查截止日期。当输入的日期 > due_date 时,获取帐龄天数。

如果aging agin days是今天,则获取netAnmount并显示在当前列,

如果差异是 1-30 天,那么下一列......等等......

如何查询这个?

最佳答案

我在 Laravel 中使用 subQuery 也许可以帮到你。

$invoice = Invoice::select(\DB::raw('ledgers.id,invoices.invoice_no,invoices.invoice_date,invoices.invoice_due,invoices.invoice_balance,DATEDIFF("'.$request->input('to_date').'", invoices.invoice_date) AS days_past_due'))
->join('quotations','quotations.id','=','invoices.quotation_id')
->join('ledgers','ledgers.id','=','quotations.ledger_id')
->where('invoices.invoice_date','<=',$request->input('to_date'))
->whereNotIn('invoices.status',[1,2]);
$agingReport = Ledger::select(\DB::raw('a.id,ledgers.name,a.invoice_no,a.invoice_date,a.invoice_due,a.invoice_balance,a.days_past_due,CASE WHEN a.days_past_due =0 then a.invoice_balance else 0 end as month,
CASE WHEN a.days_past_due >0 and a.days_past_due <= 30 then a.invoice_balance else 0 end as aging30days,
CASE WHEN a.days_past_due >30 and a.days_past_due <= 60 then a.invoice_balance else 0 end as aging60days,
CASE WHEN a.days_past_due >60 and a.days_past_due <= 90 then a.invoice_balance else 0 end as aging90days,
CASE WHEN a.days_past_due >90 then a.invoice_balance else 0 end as more30days'))
->from(\DB::raw('('.$invoice->toSql().') as a '))
->mergeBindings($invoice->getQuery())
->join('ledgers','ledgers.id','=','a.id')
->get();
return response()->json($agingReport);

关于mysql - Laravel - 使用 Laravel 查询老化报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56610370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com