334 lines
12 KiB
PHP
334 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Emisi;
|
|
|
|
use App\Models\ActivityCrf;
|
|
use App\Models\ActivityGpc;
|
|
use App\Models\Energy1A;
|
|
use App\Models\EnergyGpcI13;
|
|
use App\Models\EnergyGpcI41;
|
|
use App\Models\EnergyGpcI42;
|
|
use App\Models\EnergyGpcI51;
|
|
use App\Models\ReferenceEf;
|
|
use App\Services\SigdCrudService;
|
|
|
|
class EnergyGPCService extends SigdCrudService
|
|
{
|
|
private $sectorCode = 'energy_gpc';
|
|
public function __construct(ActivityGpc $model)
|
|
{
|
|
$this->model = $model;
|
|
}
|
|
|
|
public function save($inventoryYear)
|
|
{
|
|
try {
|
|
$dataBatch = [];
|
|
$years = activityYearRange($inventoryYear);
|
|
|
|
foreach ($years as $year) {
|
|
$dataBatch[$year] = $this->calcAndSave($inventoryYear, $year);
|
|
}
|
|
|
|
return $dataBatch;
|
|
} catch (\Exception $e) {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
|
|
public function calcAndSave($inventoryYear, $activityYear)
|
|
{
|
|
// Initialize variables
|
|
$dataForms = [];
|
|
|
|
try {
|
|
// Initialize activities
|
|
$calc_i_1_3 = $this->calc_i_1_3($inventoryYear, $activityYear);
|
|
$calc_i_4_1 = $this->calc_i_4_1($inventoryYear, $activityYear);
|
|
$calc_i_4_2 = $this->calc_i_4_2($inventoryYear, $activityYear);
|
|
$calc_i_5_1 = $this->calc_i_5_1($inventoryYear, $activityYear);
|
|
|
|
$dataForms['calc_i_1_3'] = $calc_i_1_3;
|
|
$dataForms['calc_i_4_1'] = $calc_i_4_1;
|
|
$dataForms['calc_i_4_2'] = $calc_i_4_2;
|
|
$dataForms['calc_i_5_1'] = $calc_i_5_1;
|
|
|
|
return $dataForms;
|
|
} catch (\Exception $e) {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function calc_i_1_3($inventoryYear, $activityYear)
|
|
{
|
|
$dataForms = [];
|
|
$activities = [];
|
|
|
|
$sectorCode = $this->sectorCode;
|
|
$formCode = 'transmisi_distribusi';
|
|
|
|
try {
|
|
// Initialize activities
|
|
$formSetting = $this->getDataSettingFormDetail($sectorCode, $formCode);
|
|
foreach ($formSetting as $row) $activities[$row->activity_code] = 0;
|
|
|
|
// Get data activities
|
|
$formActivity = $this->getActivityFormDetailsByYear($inventoryYear, $activityYear, $sectorCode, $formCode);
|
|
foreach ($formActivity as $row) $activities[$row->activity_code] = $row->activity_value ?? 0;
|
|
|
|
$faktor_emisi = $activities['faktor_emisi'] ?? 0;
|
|
$no = 1;
|
|
foreach ($activities as $activity => $value) {
|
|
if ($activity != 'faktor_emisi') {
|
|
$co2_ton = $value * $faktor_emisi;
|
|
$co2_gg = $co2_ton / 1000;
|
|
|
|
$dataForm = [
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'activity_code' => $activity,
|
|
'value' => $value,
|
|
'faktor_emisi' => $faktor_emisi,
|
|
'co2_ton' => $co2_ton,
|
|
'co2_gg' => $co2_gg,
|
|
'row_num' => $no++,
|
|
];
|
|
|
|
$this->createOrUpdate([
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'activity_code' => $activity,
|
|
'row_status' => 1,
|
|
], $dataForm, EnergyGpcI13::class);
|
|
|
|
$dataForms[] = $dataForm;
|
|
}
|
|
}
|
|
|
|
$this->saveGPCEmission($inventoryYear, $activityYear, $dataForms, 'i_1_3', 'co2_gg', null, null);
|
|
|
|
return $dataForms;
|
|
} catch (\Exception $e) {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function calc_i_4_1($inventoryYear, $activityYear)
|
|
{
|
|
$dataForms = [];
|
|
$activities = [];
|
|
|
|
$sectorCode = $this->sectorCode;
|
|
$formCode = 'penyulingan';
|
|
|
|
try {
|
|
// Initialize activities
|
|
$formSetting = $this->getDataSettingFormDetail($sectorCode, $formCode);
|
|
foreach ($formSetting as $row) $activities[$row->activity_code] = 0;
|
|
|
|
// Get data activities
|
|
$formActivity = $this->getActivityFormDetailsByYear($inventoryYear, $activityYear, $sectorCode, $formCode);
|
|
foreach ($formActivity as $row) $activities[$row->activity_code] = $row->activity_value ?? 0;
|
|
|
|
$ef_co2 = ReferenceEf::getValue('energi_minyak_mentah', 'co2_ef');
|
|
$ef_ch4 = ReferenceEf::getValue('energi_minyak_mentah', 'ch4_ef');
|
|
$ef_n2o = ReferenceEf::getValue('energi_minyak_mentah', 'n2o_ef');
|
|
|
|
$tj = $activities['energi_minyak_mentah'] ?? 0;
|
|
|
|
$co2_ton = $tj == 0 ? 0 : $tj * $ef_co2;
|
|
$ch4_ton = $tj == 0 ? 0 : $tj * $ef_ch4;
|
|
$n2o_ton = $tj == 0 ? 0 : $tj * $ef_n2o;
|
|
|
|
$co2_gg = $tj == 0 ? 0 : $co2_ton / 1000;
|
|
$ch4_gg = $tj == 0 ? 0 : $ch4_ton / 1000;
|
|
$n2o_gg = $tj == 0 ? 0 : $n2o_ton / 1000;
|
|
|
|
$dataForm = [
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'activity_code' => 'energi_minyak_mentah',
|
|
'value_tj' => $tj,
|
|
'ef_co2' => $ef_co2,
|
|
'ef_ch4' => $ef_ch4,
|
|
'ef_n2o' => $ef_n2o,
|
|
'co2_ton' => $co2_ton,
|
|
'ch4_ton' => $ch4_ton,
|
|
'n2o_ton' => $n2o_ton,
|
|
'co2_gg' => $co2_gg,
|
|
'ch4_gg' => $ch4_gg,
|
|
'n2o_gg' => $n2o_gg,
|
|
];
|
|
|
|
$this->createOrUpdate([
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'activity_code' => 'energi_minyak_mentah',
|
|
'row_status' => 1,
|
|
], $dataForm, EnergyGpcI41::class);
|
|
|
|
$dataForms[] = $dataForm;
|
|
|
|
$this->saveGPCEmission($inventoryYear, $activityYear, $dataForms, 'i_4_1', 'co2_gg', 'ch4_gg', 'n2o_gg');
|
|
|
|
return $dataForms;
|
|
} catch (\Exception $e) {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function calc_i_4_2($inventoryYear, $activityYear)
|
|
{
|
|
$dataForms = [];
|
|
$activities = [];
|
|
|
|
$sectorCode = $this->sectorCode;
|
|
$formCode = 'penggunaan_tambahan';
|
|
|
|
try {
|
|
// Initialize activities
|
|
$formSetting = $this->getDataSettingFormDetail($sectorCode, $formCode);
|
|
foreach ($formSetting as $row) $activities[$row->activity_code] = 0;
|
|
|
|
// Get data activities
|
|
$formActivity = $this->getActivityFormDetailsByYear($inventoryYear, $activityYear, $sectorCode, $formCode);
|
|
foreach ($formActivity as $row) $activities[$row->activity_code] = $row->activity_value ?? 0;
|
|
|
|
$faktor_emisi = $activities['faktor_emisi'] ?? 0;
|
|
$no = 1;
|
|
foreach ($activities as $activity => $value) {
|
|
if ($activity != 'faktor_emisi') {
|
|
$co2_ton = $value * $faktor_emisi;
|
|
$co2_gg = $co2_ton / 1000;
|
|
|
|
$dataForm = [
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'activity_code' => $activity,
|
|
'value' => $value,
|
|
'faktor_emisi' => $faktor_emisi,
|
|
'co2_ton' => $co2_ton,
|
|
'co2_gg' => $co2_gg,
|
|
'row_num' => $no++,
|
|
];
|
|
|
|
$this->createOrUpdate([
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'activity_code' => $activity,
|
|
'row_status' => 1,
|
|
], $dataForm, EnergyGpcI42::class);
|
|
|
|
$dataForms[] = $dataForm;
|
|
}
|
|
}
|
|
|
|
$this->saveGPCEmission($inventoryYear, $activityYear, $dataForms, 'i_4_2', 'co2_gg', null, null);
|
|
|
|
return $dataForms;
|
|
} catch (\Exception $e) {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function calc_i_5_1($inventoryYear, $activityYear)
|
|
{
|
|
$dataForms = [];
|
|
$activities = [];
|
|
|
|
$sectorCode = $this->sectorCode;
|
|
$formCode = 'bangunan_pertanian';
|
|
|
|
try {
|
|
// Initialize activities
|
|
$formSetting = $this->getDataSettingFormDetail($sectorCode, $formCode);
|
|
foreach ($formSetting as $row) $activities[$row->activity_code] = 0;
|
|
|
|
// Get data activities
|
|
$formActivity = $this->getActivityFormDetailsByYear($inventoryYear, $activityYear, $sectorCode, $formCode);
|
|
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;
|
|
$co2_ton = $consumption_tj * $ef_co2 / 1000;
|
|
$ch4_ton = $consumption_tj * $ef_ch4 / 1000;
|
|
$n2o_ton = $consumption_tj * $ef_n2o / 1000;
|
|
|
|
$co2_gg = $co2_ton / 1000;
|
|
$ch4_gg = $ch4_ton / 1000;
|
|
$n2o_gg = $n2o_ton / 1000;
|
|
|
|
$dataForm = [
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'activity_code' => $activity,
|
|
'value' => $value,
|
|
'ef_cf' => $ef_cf,
|
|
'consumption_tj' => $consumption_tj,
|
|
'ef_co2' => $ef_co2,
|
|
'ef_ch4' => $ef_ch4,
|
|
'ef_n2o' => $ef_n2o,
|
|
'co2_ton' => $co2_ton,
|
|
'ch4_ton' => $ch4_ton,
|
|
'n2o_ton' => $n2o_ton,
|
|
'co2_gg' => $co2_gg,
|
|
'ch4_gg' => $ch4_gg,
|
|
'n2o_gg' => $n2o_gg,
|
|
'row_num' => $no++,
|
|
];
|
|
|
|
$this->createOrUpdate([
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'activity_code' => $activity,
|
|
'row_status' => 1,
|
|
], $dataForm, EnergyGpcI51::class);
|
|
|
|
$this->saveGPCEmission($inventoryYear, $activityYear, $dataForms, 'i_5_1', 'co2_gg', 'ch4_gg', 'n2o_gg');
|
|
|
|
$dataForms[] = $dataForm;
|
|
}
|
|
|
|
return $dataForms;
|
|
} catch (\Exception $e) {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
private function saveGPCEmission($inventoryYear, $activityYear, $dataForms, $gpcCode, $co2Code, $ch4Code, $n2oCode)
|
|
{
|
|
try {
|
|
$co2 = $co2Code ? array_sum(array_column($dataForms, $co2Code)) : null;
|
|
$ch4 = $ch4Code ? array_sum(array_column($dataForms, $ch4Code)) : null;
|
|
$n2o = $n2oCode ? array_sum(array_column($dataForms, $n2oCode)) : null;
|
|
|
|
$dataForm = [
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'sector' => 'energy_gpc',
|
|
'gpc_code' => $gpcCode,
|
|
'co2' => $co2,
|
|
'ch4' => $ch4,
|
|
'n2o' => $n2o,
|
|
];
|
|
|
|
$this->createOrUpdate([
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'sector' => $this->sectorCode,
|
|
'gpc_code' => $gpcCode,
|
|
'row_status' => 1,
|
|
], $dataForm, ActivityGpc::class);
|
|
} catch (\Exception $e) {
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|