我有要转换为Laravel的MySQL查询。
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查询以Matrix格式创建30、60、90天的当前老化报告。
例如,
ClientName当前1-30 31-60> 90总计
AAA 3000 1500 4500
BBB 2000 200 2200
总计3000 3500 200 6700
我想要这样的报告。当用户输入日期时,应使用Duedate进行检查。输入的日期为> Due_date时,获取老化天数。
如果今天是老化的起始天,则获取netAnmount并显示在当前列中,
如果相差1-30天,则下一行...等等...
如何获取此查询?
答案 0 :(得分:0)
我在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);