Refactor getGrossAnnualEconomy method to simplify date filtering logic and enhance query performance
This commit is contained in:
parent
21ad4e4b0b
commit
ee5cb014bb
@ -48,28 +48,8 @@ class EconomyRepository extends AbstractRepository implements EconomyContractInt
|
|||||||
->where(
|
->where(
|
||||||
DB::raw("TO_DATE(economia.mes, 'YYMM')"),
|
DB::raw("TO_DATE(economia.mes, 'YYMM')"),
|
||||||
">=",
|
">=",
|
||||||
DB::raw("TO_DATE(TO_CHAR(current_date , 'YYYY-12-01'), 'YYYY-MM-DD') - interval '2' year"))
|
DB::raw("TO_DATE(TO_CHAR(current_date , 'YYYY-12-01'), 'YYYY-MM-DD') - interval '24' month"))
|
||||||
->where(function ($query) {
|
->where(DB::raw("extract(month from TO_DATE(economia.mes, 'YYMM'))"), '=', 12)
|
||||||
$query->where(DB::raw("extract(month from TO_DATE(economia.mes, 'YYMM'))"), '=', 12)
|
|
||||||
->orWhere(function ($query) {
|
|
||||||
$query->where(DB::raw("extract(year from TO_DATE(economia.mes, 'YYMM'))"), '=', DB::raw('extract(year from NOW())'))
|
|
||||||
->whereIn(DB::raw("extract(month from TO_DATE(economia.mes, 'YYMM'))"),
|
|
||||||
DB::table('economia')
|
|
||||||
->selectRaw(
|
|
||||||
"max(extract(month from TO_DATE(mes, 'YYMM')))"
|
|
||||||
)
|
|
||||||
->where('dad_estimado', '=', false)
|
|
||||||
->where(DB::raw("extract(year from TO_DATE(mes, 'YYMM'))"), '=', DB::raw('extract(year from NOW())'))
|
|
||||||
->whereIn(
|
|
||||||
'economia.cod_smart_unidade',
|
|
||||||
DB::table('dados_cadastrais')
|
|
||||||
->select('cod_smart_unidade')
|
|
||||||
->where('dados_cadastrais.codigo_scde', '!=', '0P')
|
|
||||||
->where('dados_cadastrais.cod_smart_cliente', '=', Auth::user()->client_id)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
})
|
|
||||||
->groupBy(['mes', 'ano', 'dad_estimado'])
|
->groupBy(['mes', 'ano', 'dad_estimado'])
|
||||||
->havingRaw("sum(custo_livre) > 0")
|
->havingRaw("sum(custo_livre) > 0")
|
||||||
->orderBy(DB::raw("mes, ano, dad_estimado"))
|
->orderBy(DB::raw("mes, ano, dad_estimado"))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user