137 lines
4.9 KiB
PHP
137 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Emisi;
|
|
|
|
use App\Models\ActivityCrf;
|
|
use App\Models\ActivityFormDetail;
|
|
use App\Models\Agriculture3C1;
|
|
use App\Models\ReferenceEf;
|
|
use App\Services\SigdCrudService;
|
|
|
|
class Agriculture3C1Service extends SigdCrudService
|
|
{
|
|
private $sectorCode = 'agriculture';
|
|
private $formCode = 'sawah';
|
|
|
|
public function __construct(Agriculture3C1 $model)
|
|
{
|
|
$this->model = $model;
|
|
}
|
|
|
|
public function save($code, $inventoryYear)
|
|
{
|
|
try {
|
|
$dataBatch = [];
|
|
$ws = $this->getWs($code);
|
|
$years = activityYearRange($inventoryYear);
|
|
|
|
if ($ws && class_basename($ws->model) == 'Agriculture3C1') {
|
|
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 = [];
|
|
|
|
$sector = $this->sectorCode;
|
|
$code = $this->formCode;
|
|
$wsCode = $ws->ws_code;
|
|
|
|
try {
|
|
// Initialize and get data activities
|
|
$activity = 'luas_panen_' . $ws->code;
|
|
$luas_panen = ActivityFormDetail::getValue($inventoryYear, $activityYear, $sector, $code, 'luas_panen_' . $ws->code);
|
|
$produktivitas = ActivityFormDetail::getValue($inventoryYear, $activityYear, $sector, $code, 'produktivitas_' . $ws->code);
|
|
$ef_burnt_fraction = ReferenceEf::getValue($activity, 'burnt_fraction');
|
|
$m_b = $luas_panen == 0 ? 0 : $luas_panen * $produktivitas / 10 * 1.4 / $luas_panen;
|
|
|
|
$ef_combustion_factor = ReferenceEf::getValue($activity, 'combustion_factor');
|
|
$ef_ch4 = ReferenceEf::getValue($activity, 'ch4_ef');
|
|
$ef_co = ReferenceEf::getValue($activity, 'co_ef');
|
|
$ef_n2o = ReferenceEf::getValue($activity, 'n2o_ef');
|
|
$ef_nox = ReferenceEf::getValue($activity, 'nox_ef');
|
|
|
|
$l_fire_ch4 = $luas_panen == 0 ? 0 : $luas_panen * $ef_burnt_fraction * $m_b * $ef_combustion_factor * $ef_ch4 / 1000;
|
|
$l_fire_co = $luas_panen == 0 ? 0 : $luas_panen * $ef_burnt_fraction * $m_b * $ef_combustion_factor * $ef_co / 1000;
|
|
$l_fire_n2o = $luas_panen == 0 ? 0 : $luas_panen * $ef_burnt_fraction * $m_b * $ef_combustion_factor * $ef_n2o / 1000;
|
|
$l_fire_nox = $luas_panen == 0 ? 0 : $luas_panen * $ef_burnt_fraction * $m_b * $ef_combustion_factor * $ef_nox / 1000;
|
|
|
|
$dataForm = [
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'activity_code' => $activity,
|
|
'category' => $wsCode,
|
|
'luas_panen' => $luas_panen,
|
|
'produktivitas' => $produktivitas,
|
|
'ef_burnt_fraction' => $ef_burnt_fraction,
|
|
'm_b' => $m_b,
|
|
'ef_combustion_factor' => $ef_combustion_factor,
|
|
'ef_ch4' => $ef_ch4,
|
|
'ef_co' => $ef_co,
|
|
'ef_n2o' => $ef_n2o,
|
|
'ef_nox' => $ef_nox,
|
|
'l_fire_ch4' => $l_fire_ch4,
|
|
'l_fire_co' => $l_fire_co,
|
|
'l_fire_n2o' => $l_fire_n2o,
|
|
'l_fire_nox' => $l_fire_nox,
|
|
];
|
|
|
|
$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 = null;
|
|
$ch4 = array_sum(array_column($dataForms, 'l_fire_ch4')) / 1000;
|
|
$n2o = array_sum(array_column($dataForms, 'l_fire_n2o')) / 1000;
|
|
|
|
$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;
|
|
}
|
|
}
|
|
}
|