-Put newest first. -From 2021 PLD History. Current year - 1. -Implementation of division by 1000. -Divide the raw data only once by 1000.
This commit is contained in:
parent
9b7987a11e
commit
c79f82f8eb
@ -5,6 +5,7 @@ namespace App\Helpers;
|
|||||||
|
|
||||||
use DateInterval;
|
use DateInterval;
|
||||||
use DatePeriod;
|
use DatePeriod;
|
||||||
|
use DateTime;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
@ -20,9 +21,9 @@ class Helpers
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function orderByDate($result): array
|
public static function orderByDate($result, $field = 'mes'): array
|
||||||
{
|
{
|
||||||
return collect($result)->transform(fn($value) => Arr::set($value, 'mes', date_format(date_create($value['mes']), "M/Y")))->all();
|
return collect($result)->transform(fn($value) => Arr::set($value, $field, DateTime::createFromFormat('ym',$value['mes'])->format('m/Y')))->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function checkDate($value): array
|
public static function checkDate($value): array
|
||||||
|
|||||||
@ -4,9 +4,11 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Helpers\Helpers;
|
||||||
use App\Http\Resources\OperationSummaryResource;
|
use App\Http\Resources\OperationSummaryResource;
|
||||||
use App\Repositories\DadosTe\DadosTeContractInterface;
|
use App\Repositories\DadosTe\DadosTeContractInterface;
|
||||||
use App\Traits\ApiResponse;
|
use App\Traits\ApiResponse;
|
||||||
|
use DateTime;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Response;
|
use Illuminate\Http\Response;
|
||||||
|
|
||||||
@ -24,6 +26,7 @@ class OperationSummaryController extends Controller
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$response = $this->dadosTeContract->search($request->all(), true);
|
$response = $this->dadosTeContract->search($request->all(), true);
|
||||||
|
$response = Helpers::orderByDate($response);
|
||||||
return (new OperationSummaryResource($response))
|
return (new OperationSummaryResource($response))
|
||||||
->response()
|
->response()
|
||||||
->setStatusCode(Response::HTTP_OK);
|
->setStatusCode(Response::HTTP_OK);
|
||||||
|
|||||||
@ -43,14 +43,15 @@ class EconomyRepository extends AbstractRepository implements EconomyContractInt
|
|||||||
"economia.dad_estimado"
|
"economia.dad_estimado"
|
||||||
];
|
];
|
||||||
|
|
||||||
return $this->execute($params, $field)
|
DB::enableQueryLog();
|
||||||
|
$this->execute($params, $field)
|
||||||
->where(DB::raw("TO_DATE(economia.mes, 'YYMM')"),
|
->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"))
|
DB::raw("TO_DATE(TO_CHAR(current_date , 'YYYY-01-01'), 'YYYY-MM-DD') - interval '1' year"))
|
||||||
->groupBy(['ano', 'dad_estimado'])
|
->groupBy(['ano', 'dad_estimado'])
|
||||||
->orderBy('ano')
|
->orderBy(DB::raw("ano, dad_estimado"))
|
||||||
->orderBy('dad_estimado')
|
|
||||||
->get();
|
->get();
|
||||||
|
dd(DB::getQueryLog());
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Economia bruta mensal */
|
/* Economia bruta mensal */
|
||||||
@ -58,7 +59,7 @@ class EconomyRepository extends AbstractRepository implements EconomyContractInt
|
|||||||
{
|
{
|
||||||
$field = [
|
$field = [
|
||||||
DB::raw("TO_DATE(economia.mes, 'YYMM') as mes"),
|
DB::raw("TO_DATE(economia.mes, 'YYMM') as mes"),
|
||||||
DB::raw("SUM(economia.economia_acumulada) as economia_acumulada"),
|
DB::raw("SUM(economia.economia_acumulada)/1000 as economia_acumulada"),
|
||||||
DB::raw("(SUM(economia.economia_mensal)/SUM(economia.custo_livre)) as econ_percentual"),
|
DB::raw("(SUM(economia.economia_mensal)/SUM(economia.custo_livre)) as econ_percentual"),
|
||||||
"economia.dad_estimado"
|
"economia.dad_estimado"
|
||||||
];
|
];
|
||||||
@ -80,9 +81,9 @@ class EconomyRepository extends AbstractRepository implements EconomyContractInt
|
|||||||
{
|
{
|
||||||
$field = [
|
$field = [
|
||||||
DB::raw("TO_CHAR(TO_DATE(economia.mes, 'YYMM'), 'MM/YYYY') as mes"),
|
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_cativo)/1000 as custo_cativo"),
|
||||||
DB::raw("SUM(economia.custo_livre) as custo_livre"),
|
DB::raw("SUM(economia.custo_livre)/1000 as custo_livre"),
|
||||||
DB::raw("SUM(economia.economia_mensal) as economia_mensal"),
|
DB::raw("SUM(economia.economia_mensal)/1000 as economia_mensal"),
|
||||||
DB::raw("(SUM(economia_mensal)/SUM(custo_livre)) as econ_percentual"),
|
DB::raw("(SUM(economia_mensal)/SUM(custo_livre)) as econ_percentual"),
|
||||||
"economia.dad_estimado"
|
"economia.dad_estimado"
|
||||||
];
|
];
|
||||||
|
|||||||
@ -6,8 +6,6 @@ namespace App\Repositories;
|
|||||||
|
|
||||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||||
use Illuminate\Database\Eloquent\Collection;
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
use Illuminate\Support\Arr;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
|
|
||||||
trait MethodsTrait
|
trait MethodsTrait
|
||||||
{
|
{
|
||||||
@ -50,7 +48,7 @@ trait MethodsTrait
|
|||||||
{
|
{
|
||||||
$this->filterBuilder($params);
|
$this->filterBuilder($params);
|
||||||
|
|
||||||
$fields = $this->item->applyField();
|
$fields = $this->item->getFields();
|
||||||
|
|
||||||
$query = $this->model->select($fields);
|
$query = $this->model->select($fields);
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Repositories\Pld;
|
namespace App\Repositories\Pld;
|
||||||
|
|
||||||
|
use App\Helpers\Helpers;
|
||||||
use App\Models\Pld;
|
use App\Models\Pld;
|
||||||
use App\Repositories\AbstractRepository;
|
use App\Repositories\AbstractRepository;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
@ -71,7 +72,7 @@ class PldRepository extends AbstractRepository implements PldContractInterface
|
|||||||
->select([
|
->select([
|
||||||
'submercado as submarket',
|
'submercado as submarket',
|
||||||
'mes_ref as year_month',
|
'mes_ref as year_month',
|
||||||
DB::raw('SUM(valor) as value'),
|
DB::raw('AVG(valor) as value'),
|
||||||
])
|
])
|
||||||
->where('pld.submercado', '=', 'NORTE')
|
->where('pld.submercado', '=', 'NORTE')
|
||||||
->groupBy('submarket', 'year_month');
|
->groupBy('submarket', 'year_month');
|
||||||
@ -85,7 +86,7 @@ class PldRepository extends AbstractRepository implements PldContractInterface
|
|||||||
->select([
|
->select([
|
||||||
'submercado as submarket',
|
'submercado as submarket',
|
||||||
'mes_ref as year_month',
|
'mes_ref as year_month',
|
||||||
DB::raw('SUM(valor) as value')
|
DB::raw('AVG(valor) as value')
|
||||||
])
|
])
|
||||||
->where('pld.submercado', '=', 'SUL')
|
->where('pld.submercado', '=', 'SUL')
|
||||||
->groupBy('submarket', 'year_month');
|
->groupBy('submarket', 'year_month');
|
||||||
@ -99,7 +100,7 @@ class PldRepository extends AbstractRepository implements PldContractInterface
|
|||||||
->select([
|
->select([
|
||||||
'submercado as submarket',
|
'submercado as submarket',
|
||||||
'mes_ref as year_month',
|
'mes_ref as year_month',
|
||||||
DB::raw('SUM(valor) as value')
|
DB::raw('AVG(valor) as value')
|
||||||
])
|
])
|
||||||
->where('pld.submercado', '=', 'NORDESTE')
|
->where('pld.submercado', '=', 'NORDESTE')
|
||||||
->groupBy('submarket', 'year_month');
|
->groupBy('submarket', 'year_month');
|
||||||
@ -113,7 +114,7 @@ class PldRepository extends AbstractRepository implements PldContractInterface
|
|||||||
->select([
|
->select([
|
||||||
'submercado as submarket',
|
'submercado as submarket',
|
||||||
'mes_ref as year_month',
|
'mes_ref as year_month',
|
||||||
DB::raw('SUM(valor) as value')
|
DB::raw('AVG(valor) as value')
|
||||||
])
|
])
|
||||||
->where('pld.submercado', '=', 'SUDESTE')
|
->where('pld.submercado', '=', 'SUDESTE')
|
||||||
->groupBy('submarket', 'year_month');
|
->groupBy('submarket', 'year_month');
|
||||||
@ -131,7 +132,11 @@ class PldRepository extends AbstractRepository implements PldContractInterface
|
|||||||
$join->on('pld.mes_ref', '=', 'pld_nordeste.year_month');
|
$join->on('pld.mes_ref', '=', 'pld_nordeste.year_month');
|
||||||
})->joinSub($sql4, 'pld_sudeste', function ($join) {
|
})->joinSub($sql4, 'pld_sudeste', function ($join) {
|
||||||
$join->on('pld.mes_ref', '=', 'pld_sudeste.year_month');
|
$join->on('pld.mes_ref', '=', 'pld_sudeste.year_month');
|
||||||
})->distinct()->get();
|
})
|
||||||
|
->whereRaw("TO_DATE(pld.mes_ref, 'YYMM') >= TO_DATE(TO_CHAR(current_date , 'YYYY-01-01'), 'YYYY-MM-DD') - INTERVAL '1' year")
|
||||||
|
->orderBy('year_month', 'DESC')
|
||||||
|
->distinct()
|
||||||
|
->get();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'data' => $data,
|
'data' => $data,
|
||||||
@ -150,8 +155,8 @@ class PldRepository extends AbstractRepository implements PldContractInterface
|
|||||||
public function getConsumptionByDaily($params, $field = "mes_ref"): Collection|array
|
public function getConsumptionByDaily($params, $field = "mes_ref"): Collection|array
|
||||||
{
|
{
|
||||||
$fields = [
|
$fields = [
|
||||||
DB::raw("TO_CHAR((date('1899-12-31') + interval '1' day * pld.dia_num), 'DD') as day_formatted"),
|
DB::raw("TO_CHAR((date('1899-12-30') + interval '1' day * pld.dia_num), 'DD') as day_formatted"),
|
||||||
DB::raw("(date('1899-12-31') + interval '1' day * pld.dia_num) as day_calc"),
|
DB::raw("(date('1899-12-30') + interval '1' day * pld.dia_num) as day_calc"),
|
||||||
'pld.submercado as submarket',
|
'pld.submercado as submarket',
|
||||||
DB::raw("AVG(pld.valor) as value"),
|
DB::raw("AVG(pld.valor) as value"),
|
||||||
DB::raw("TO_CHAR(TO_DATE(pld.mes_ref, 'YYMM'), 'MM/YYYY') as year_month"),
|
DB::raw("TO_CHAR(TO_DATE(pld.mes_ref, 'YYMM'), 'MM/YYYY') as year_month"),
|
||||||
|
|||||||
@ -1,77 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace App\Support\FilterBuilder\Entity;
|
|
||||||
|
|
||||||
use App\Support\FilterBuilder\EntityJson;
|
|
||||||
|
|
||||||
class FieldItem extends EntityJson
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string|null
|
|
||||||
*/
|
|
||||||
protected string|null $type = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string|null
|
|
||||||
*/
|
|
||||||
protected string|null $format = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected string $field;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getType(): string|null
|
|
||||||
{
|
|
||||||
return $this->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $type
|
|
||||||
*/
|
|
||||||
public function setType(string $type): void
|
|
||||||
{
|
|
||||||
$this->type = $type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getFormat(): ?string
|
|
||||||
{
|
|
||||||
return $this->format;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string|null $format
|
|
||||||
*/
|
|
||||||
public function setFormat(?string $format): void
|
|
||||||
{
|
|
||||||
$this->format = $format;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getField(): string
|
|
||||||
{
|
|
||||||
return $this->field;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $field
|
|
||||||
*/
|
|
||||||
public function setField(string $field): void
|
|
||||||
{
|
|
||||||
$this->field = $field;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,30 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Support\FilterBuilder;
|
|
||||||
|
|
||||||
use App\Support\FilterBuilder\Entity\FieldItem;
|
|
||||||
use Illuminate\Database\Query\Expression;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
|
|
||||||
class FieldType
|
|
||||||
{
|
|
||||||
const FUNCTION_FIELD = [
|
|
||||||
"TO_CHAR"
|
|
||||||
];
|
|
||||||
|
|
||||||
public static function field(FieldItem $field)
|
|
||||||
{
|
|
||||||
if (in_array($field->getType(), self::FUNCTION_FIELD)) {
|
|
||||||
return static::makeFormatFieldDate($field);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $field->getField();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function makeFormatFieldDate($field): Expression
|
|
||||||
{
|
|
||||||
return DB::raw("TO_CHAR(TO_DATE({$field->field}, 'YYMM'), '{$field->format}') as {$field->field}");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Support\FilterBuilder;
|
namespace App\Support\FilterBuilder;
|
||||||
|
|
||||||
use App\Support\FilterBuilder\Entity\FieldItem;
|
|
||||||
use App\Support\FilterBuilder\Entity\FilterItem;
|
use App\Support\FilterBuilder\Entity\FilterItem;
|
||||||
use App\Support\FilterBuilder\Entity\OrderItem;
|
use App\Support\FilterBuilder\Entity\OrderItem;
|
||||||
use App\Support\FilterBuilder\Interfaces\IFilterBuilder;
|
use App\Support\FilterBuilder\Interfaces\IFilterBuilder;
|
||||||
@ -50,30 +49,6 @@ class FilterQueryBuilder extends EntityJson implements IFilterBuilder
|
|||||||
return $builder;
|
return $builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function applyField($fields = [])
|
|
||||||
{
|
|
||||||
if (!empty($this->getFields())) {
|
|
||||||
foreach ($this->getFields() as $field)
|
|
||||||
{
|
|
||||||
$fields[] = FieldType::field($field);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $fields;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*/
|
|
||||||
public function format_date_sql(): static
|
|
||||||
{
|
|
||||||
foreach ($this->getFields() as $param) {
|
|
||||||
$this->setFields([DB::raw("TO_CHAR(TO_DATE({$param}, 'YYMM'), 'MM/YYYY') as {$param}")]);
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@ -87,7 +62,7 @@ class FilterQueryBuilder extends EntityJson implements IFilterBuilder
|
|||||||
*/
|
*/
|
||||||
public function setFields(array $fields): void
|
public function setFields(array $fields): void
|
||||||
{
|
{
|
||||||
$this->fields = $this->arrayObjectCast($fields, FieldItem::class);
|
$this->fields = $fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user