127 lines
4.2 KiB
PHP
127 lines
4.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repositories\Economy;
|
|
|
|
use App\Models\Economy;
|
|
use App\Repositories\AbstractRepository;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class EconomyRepository extends AbstractRepository implements EconomyContractInterface
|
|
{
|
|
|
|
public function __construct(Economy $economy)
|
|
{
|
|
parent::__construct($economy);
|
|
}
|
|
|
|
public function execute($params, $field): Builder
|
|
{
|
|
|
|
$query = $this->model->select($field);
|
|
|
|
if (!empty($params)) {
|
|
$query = static::getFilterBuilder($params)->applyFilter($query);
|
|
}
|
|
return $query;
|
|
}
|
|
|
|
public function getGrossAnnualEconomy($params): Collection|array
|
|
{
|
|
$field = [
|
|
DB::raw("TO_CHAR(TO_DATE(economia.mes, 'YYMM'), 'YYYY') as ano"),
|
|
DB::raw("SUM(economia.economia_acumulada) as economia_acumulada"),
|
|
DB::raw("(SUM(economia.economia_mensal)/SUM(economia.custo_livre)) as econ_percentual"),
|
|
"economia.dad_estimado"
|
|
];
|
|
|
|
return $this->execute($params, $field)
|
|
->where(DB::raw("TO_DATE(economia.mes, 'YYMM')"),
|
|
">=",
|
|
DB::raw("TO_DATE(TO_CHAR(current_date , 'YYYY-01-01'), 'YYYY-MM-DD') - interval '1' year"))
|
|
->groupBy(['mes', 'dad_estimado'])
|
|
->get();
|
|
}
|
|
|
|
public function getGrossMonthlyEconomy($params)
|
|
{
|
|
$field = [
|
|
DB::raw("TO_CHAR(TO_DATE(economia.mes, 'YYMM'), 'MM/YYYY') as mes"),
|
|
DB::raw("SUM(economia.economia_acumulada) as economia_acumulada"),
|
|
DB::raw("(SUM(economia.economia_mensal)/SUM(economia.custo_livre)) as econ_percentual"),
|
|
"economia.dad_estimado"
|
|
];
|
|
|
|
return $this->execute($params, $field)
|
|
->where(DB::raw("TO_DATE(economia.mes, 'YYMM')"),
|
|
">=",
|
|
DB::raw("TO_DATE(TO_CHAR(current_date , 'YYYY-01-01'), 'YYYY-MM-DD') - interval '1' year"))
|
|
->groupBy(['mes', 'dad_estimado'])
|
|
->get();
|
|
}
|
|
|
|
public function getCaptiveMonthlyEconomy($params)
|
|
{
|
|
$field = [
|
|
DB::raw("TO_CHAR(TO_DATE(economia.mes, 'YYMM'), 'MM/YYYY') as mes"),
|
|
DB::raw("SUM(economia.custo_cativo) as custo_cativo"),
|
|
DB::raw("SUM(economia.custo_livre) as custo_livre"),
|
|
DB::raw("SUM(economia.economia_mensal) as economia_mensal"),
|
|
DB::raw("(SUM(economia_mensal)/SUM(custo_livre)) as econ_percentual"),
|
|
"economia.dad_estimado"
|
|
];
|
|
|
|
return $this->execute($params, $field)
|
|
->whereBetween(
|
|
DB::raw("TO_DATE(economia.mes, 'YYMM')"),
|
|
[
|
|
DB::raw("TO_DATE(TO_CHAR(current_date , 'YYYY-01-01'), 'YYYY-MM-DD') - interval '1' year"),
|
|
DB::raw("TO_DATE(TO_CHAR(current_date, 'YYYY-12-31'), 'YYYY-MM-DD') ")
|
|
])
|
|
->groupBy(['mes', 'dad_estimado'])
|
|
->get();
|
|
}
|
|
|
|
public function getCostMWhEconomy($params)
|
|
{
|
|
$field = [
|
|
DB::raw("TO_CHAR(TO_DATE(economia.mes, 'YYMM'), 'MM/YYYY') as mes"),
|
|
DB::raw("SUM(economia.custo_unit) as custo_unit"),
|
|
"economia.dad_estimado"
|
|
];
|
|
|
|
return $this->execute($params, $field)
|
|
->whereBetween(
|
|
DB::raw("TO_DATE(economia.mes, 'YYMM')"),
|
|
[
|
|
DB::raw("TO_DATE(TO_CHAR(current_date , 'YYYY-01-01'), 'YYYY-MM-DD') - interval '1' year"),
|
|
DB::raw("TO_DATE(TO_CHAR(current_date, 'YYYY-12-31'), 'YYYY-MM-DD') ")
|
|
])
|
|
->groupBy(['mes', 'dad_estimado'])
|
|
->get();
|
|
|
|
}
|
|
|
|
|
|
protected function where($query)
|
|
{
|
|
return $query->where(
|
|
DB::raw()
|
|
);
|
|
}
|
|
|
|
|
|
protected function getRowField(): array
|
|
{
|
|
return [
|
|
DB::raw("TO_CHAR(TO_DATE(economia.mes, 'YYMM'), 'MM/YYYY') as mes"),
|
|
DB::raw("TRIM(TO_CHAR(economia.custo_cativo, '99999999.99')) as custo_cativo"),
|
|
DB::raw("TRIM(TO_CHAR(economia.custo_livre, '99999999.99')) as custo_livre"),
|
|
DB::raw("COALESCE(economia.economia_mensal / NULLIF(economia.custo_livre, 0), 0) as custo")
|
|
];
|
|
}
|
|
}
|