sigd/app/Services/Emisi/Agriculture3C6Service.php

200 lines
6.8 KiB
PHP

<?php
namespace App\Services\Emisi;
use App\Models\ActivityCrf;
use App\Models\Agriculture3A2b;
use App\Models\Agriculture3C6;
use App\Models\ReferenceEf;
use App\Services\SigdCrudService;
class Agriculture3C6Service extends SigdCrudService
{
private $sectorCode = 'agriculture';
private $formCode = 'ternak';
public function __construct(Agriculture3C6 $model)
{
$this->model = $model;
}
public function save($code, $inventoryYear)
{
try {
$dataBatch = [];
$ws = $this->getWs($code);
$years = activityYearRange($inventoryYear);
if ($ws && class_basename($ws->model) == 'Agriculture3C6') {
foreach ($years as $year) {
$dataBatch = array_merge($dataBatch, $this->calcAndSave($inventoryYear, $year, $ws));
}
} else {
throw new \Exception("Forbidden");
}
return $dataBatch;
} catch (\Exception $e) {
throw $e;
}
}
private function getEfData($activityCode)
{
$filterTag1 = ['weaning', 'yearling', 'young', 'mature', 'imported'];
$filterTag2 = ['pastura', 'dry_lot'];
// Prepare CASE statements for custom sorting
$orderTag1 = 'CASE tag_1 ';
foreach ($filterTag1 as $index => $value) {
$orderTag1 .= "WHEN '$value' THEN " . ($index + 1) . " ";
}
$orderTag1 .= "ELSE " . (count($filterTag1) + 1) . " END";
$orderTag2 = 'CASE tag_2 ';
foreach ($filterTag2 as $index => $value) {
$orderTag2 .= "WHEN '$value' THEN " . ($index + 1) . " ";
}
$orderTag2 .= "ELSE " . (count($filterTag2) + 1) . " END";
$data = ReferenceEf::select('tag_1', 'tag_2')
->selectRaw("$orderTag1 AS order_tag1")
->selectRaw("$orderTag2 AS order_tag2")
->distinct()->where('activity_code', $activityCode)
->where(function ($query) {
$query->where(function ($subQuery) {
$subQuery->whereNotNull('tag_1')
->whereNotNull('tag_2')
->whereNull('tag_3');
})->orWhere(function ($subQuery) {
$subQuery->whereNull('tag_1')
->whereNull('tag_2')
->whereNull('tag_3');
});
})
->isActive()->rowActive()
->orderByRaw('order_tag1')->orderByRaw('order_tag2')
->get();
return $data;
}
private function getAgriculture3A2bData($array)
{
$query = Agriculture3A2b::query();
foreach ($array as $key => $value) {
$query->where($key, $value);
}
$data = $query->rowActive()->first();
return $data;
}
public function calcAndSave($inventoryYear, $activityYear, $ws)
{
// Initialize variables
$dataForms = [];
$sector = $this->sectorCode;
$code = $this->formCode;
$wsCode = $ws->ws_code;
try {
// Initialize activities
$formSetting = $this->getDataSettingFormDetail($sector, $code);
foreach ($formSetting as $row) $activities[$row->activity_code] = 0;
// Get data activities
$formActivity = $this->getActivityFormDetailsByYear($inventoryYear, $activityYear, $sector, $code);
foreach ($formActivity as $row) $activities[$row->activity_code] = $row->activity_value ?? 0;
$no = 1;
foreach ($activities as $activity => $value) {
$efs = $this->getEfData($activity);
foreach ($efs as $ef) {
$param3A2B = [
'inventory_year' => $inventoryYear,
'activity_year' => $activityYear,
'activity_code' => $activity,
'category' => '3A2b',
'tag_1' => $ef->tag_1,
'tag_2' => $ef->tag_2,
];
$ne_mms = $this->getAgriculture3A2bData($param3A2B)->ne_mms ?? 0;
$ef_frac = ReferenceEf::getValue($activity, 'volatilised_fraction', $ef->tag_1, $ef->tag_2);
$n_volatilization_mms = $ne_mms * $ef_frac;
$ef_n2o = ReferenceEf::getValue($activity, 'n2o_ef', $ef->tag_1, $ef->tag_2);
$n2o_g = $n_volatilization_mms * $ef_n2o * 44 / 28;
$dataForm = [
'inventory_year' => $inventoryYear,
'activity_year' => $activityYear,
'activity_code' => $activity,
'category' => $wsCode,
'tag_1' => $ef->tag_1 ?? null,
'tag_2' => $ef->tag_2 ?? null,
'value' => $value,
'ne_mms' => $ne_mms,
'ef_frac' => $ef_frac,
'n_volatilization_mms' => $n_volatilization_mms,
'ef_n2o' => $ef_n2o,
'n2o_g' => $n2o_g,
'row_num' => $no++,
];
$this->createOrUpdate([
'inventory_year' => $inventoryYear,
'activity_year' => $activityYear,
'activity_code' => $activity,
'category' => $wsCode,
'tag_1' => $ef->tag_1 ?? null,
'tag_2' => $ef->tag_2 ?? null,
'row_status' => 1,
], $dataForm);
$dataForms[] = $dataForm;
}
}
$this->saveCrfEmission($inventoryYear, $activityYear, $ws, $dataForms);
return $dataForms;
} catch (\Exception $e) {
throw $e;
}
}
private function saveCrfEmission($inventoryYear, $activityYear, $ws, $dataForms)
{
try {
$co2 = null;
$ch4 = null;
$n2o = array_sum(array_column($dataForms, 'n2o_g')) / 1000000;
$dataForm = [
'inventory_year' => $inventoryYear,
'activity_year' => $activityYear,
'sector' => $this->sectorCode,
'ws_code' => $ws->ws_code,
'co2' => $co2,
'ch4' => $ch4,
'n2o' => $n2o,
];
$this->createOrUpdate([
'inventory_year' => $inventoryYear,
'activity_year' => $activityYear,
'sector' => $this->sectorCode,
'ws_code' => $ws->ws_code,
'row_status' => 1,
], $dataForm, ActivityCrf::class);
} catch (\Exception $e) {
throw $e;
}
}
}