192 lines
6.4 KiB
PHP
192 lines
6.4 KiB
PHP
<?php
|
||
|
||
namespace App\Services\Emisi;
|
||
|
||
use App\Models\ActivityCrf;
|
||
use App\Models\Ippu1A;
|
||
use App\Models\ReferenceEf;
|
||
use App\Services\SigdCrudService;
|
||
|
||
class Ippu1AService extends SigdCrudService
|
||
{
|
||
private $sectorCode = 'ippu';
|
||
public function __construct(Ippu1A $model)
|
||
{
|
||
$this->model = $model;
|
||
}
|
||
|
||
public function save($code, $inventoryYear)
|
||
{
|
||
try {
|
||
$dataBatch = [];
|
||
$ws = $this->getWs($code);
|
||
$years = activityYearRange($inventoryYear);
|
||
|
||
if ($ws && class_basename($ws->model) == 'Ippu1A') {
|
||
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;
|
||
|
||
// dd($sector, $code, $wsCode);
|
||
|
||
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;
|
||
|
||
//cf
|
||
// IPPU / Produksi:
|
||
// - Klinker EF: 0.52 ton CO2/ton
|
||
// - Kapur EF: 0.75 ton CO2/ton
|
||
// - Amonia EF: 3.273 ton CO2/ton
|
||
// - Asam Nitrat EF: 0.009 ton N2O/ton
|
||
// - Carbon Black EF: 2.62 ton CO2/ton + 0.0006 ton CH4/ton
|
||
// - Etilen EF: 1.74 ton CO2/ton + 0.003 ton CH4/ton
|
||
// - Etilen Diklorida EF: 0.196 ton CO2/ton + 0.00179 ton CH4/ton
|
||
// - Vinil Klorida EF: 0.294 ton CO2/ton + 0.000226 ton CH4/ton
|
||
$co2Ef = [
|
||
"klinker" => 0.52,
|
||
"kapur" => 0.75,
|
||
"amonia" => 3.273,
|
||
"urea" => 0,
|
||
"asam_nitrat" => 0.009,
|
||
"asam_nitrat_tambahan" => 0,
|
||
"silikon_karbida" => 0,
|
||
"kalsium_karbida" => 0,
|
||
"metanol" => 0,
|
||
"etilen" => 1.74,
|
||
"etilen_diklorida" => 0.196,
|
||
"vinil_klorida" => 0.294,
|
||
"carbon_black" => 2.62,
|
||
"basic_oxygen_furnace" => 0,
|
||
"pig_iron" => 0,
|
||
"direct_reducted_iron" => 0,
|
||
"electric_arc_furnace" => 0,
|
||
"aluminium" => 0,
|
||
"timbal" => 0,
|
||
"zinc" => 0,
|
||
];
|
||
|
||
$ch4Ef = [
|
||
"klinker" => 0,
|
||
"kapur" => 0,
|
||
"amonia" => 0,
|
||
"urea" => 0,
|
||
"asam_nitrat" => 0,
|
||
"asam_nitrat_tambahan" => 0,
|
||
"silikon_karbida" => 0,
|
||
"kalsium_karbida" => 0,
|
||
"metanol" => 0,
|
||
"etilen" => 0.003,
|
||
"etilen_diklorida" => 0.00179,
|
||
"vinil_klorida" => 0.000226,
|
||
"carbon_black" => 0.0006,
|
||
"basic_oxygen_furnace" => 0,
|
||
"pig_iron" => 0,
|
||
"direct_reducted_iron" => 0,
|
||
"electric_arc_furnace" => 0,
|
||
"aluminium" => 0,
|
||
"timbal" => 0,
|
||
"zinc" => 0,
|
||
];
|
||
|
||
$no = 1;
|
||
foreach ($activities as $activity => $value) {
|
||
$ef_cf = 0;
|
||
$ef_co2 = $co2Ef[$activity];
|
||
$ef_ch4 = $ch4Ef[$activity];
|
||
$ef_n2o = 0;
|
||
|
||
$consumption_tj = 0;
|
||
$emission_co2 = $ef_co2 * $value;
|
||
$emission_ch4 = $ef_ch4 * $value;
|
||
$emission_n2o = $ef_n2o * $value;
|
||
|
||
$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' => $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;
|
||
}
|
||
}
|
||
}
|