135 lines
4.5 KiB
PHP
135 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Emisi;
|
|
|
|
use App\Models\ActivityCrf;
|
|
use App\Models\Energy1A;
|
|
use App\Models\ReferenceEf;
|
|
use App\Services\SigdCrudService;
|
|
|
|
class Energy1AService extends SigdCrudService
|
|
{
|
|
private $sectorCode = 'energy';
|
|
public function __construct(Energy1A $model)
|
|
{
|
|
$this->model = $model;
|
|
}
|
|
|
|
public function save($code, $inventoryYear)
|
|
{
|
|
try {
|
|
$dataBatch = [];
|
|
$ws = $this->getWs($code);
|
|
$years = activityYearRange($inventoryYear);
|
|
|
|
if ($ws && class_basename($ws->model) == 'Energy1A') {
|
|
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;
|
|
}
|
|
}
|
|
|
|
public function calcAndSave($inventoryYear, $activityYear, $ws)
|
|
{
|
|
// Initialize variables
|
|
$dataForms = [];
|
|
$activities = [];
|
|
|
|
$sector = $this->sectorCode;
|
|
$code = $ws->code;
|
|
$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) {
|
|
$ef_cf = ReferenceEf::getValue($activity, 'cf');
|
|
$ef_co2 = ReferenceEf::getValue($activity, 'co2_ef');
|
|
$ef_ch4 = ReferenceEf::getValue($activity, 'ch4_ef');
|
|
$ef_n2o = ReferenceEf::getValue($activity, 'n2o_ef');
|
|
|
|
$consumption_tj = $value * $ef_cf;
|
|
$emission_co2 = $consumption_tj * $ef_co2 / 1000000;
|
|
$emission_ch4 = $consumption_tj * $ef_ch4 / 1000000;
|
|
$emission_n2o = $consumption_tj * $ef_n2o / 1000000;
|
|
|
|
$dataForm = [
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'activity_code' => $activity,
|
|
'category' => $wsCode,
|
|
'value' => $value,
|
|
'ef_cf' => $ef_cf,
|
|
'consumption_tj' => $consumption_tj,
|
|
'ef_co2' => $ef_co2,
|
|
'emission_co2' => $emission_co2,
|
|
'ef_ch4' => $ef_ch4,
|
|
'emission_ch4' => $emission_ch4,
|
|
'ef_n2o' => $ef_n2o,
|
|
'emission_n2o' => $emission_n2o,
|
|
'row_num' => $no++,
|
|
];
|
|
|
|
$this->createOrUpdate([
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'activity_code' => $activity,
|
|
'category' => $wsCode,
|
|
'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 = array_sum(array_column($dataForms, 'emission_co2'));
|
|
$ch4 = array_sum(array_column($dataForms, 'emission_ch4'));
|
|
$n2o = array_sum(array_column($dataForms, 'emission_n2o'));
|
|
|
|
$dataForm = [
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'sector' => 'energy',
|
|
'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;
|
|
}
|
|
}
|
|
}
|