188 lines
6.2 KiB
PHP
188 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Emisi;
|
|
|
|
use App\Models\ActivityCrf;
|
|
use App\Models\Agriculture3A2b;
|
|
use App\Models\Agriculture3C4;
|
|
use App\Models\Agriculture3C5;
|
|
use App\Models\ReferenceEf;
|
|
use App\Services\SigdCrudService;
|
|
|
|
class Agriculture3C5Service extends SigdCrudService
|
|
{
|
|
private $sectorCode = 'agriculture';
|
|
|
|
public function __construct(Agriculture3C5 $model)
|
|
{
|
|
$this->model = $model;
|
|
}
|
|
|
|
public function save($code, $inventoryYear)
|
|
{
|
|
try {
|
|
$dataBatch = [];
|
|
$ws = $this->getWs($code);
|
|
$years = activityYearRange($inventoryYear);
|
|
|
|
if ($ws && class_basename($ws->model) == 'Agriculture3C5') {
|
|
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 getAgriculture3A2b($inventoryYear, $activityYear)
|
|
{
|
|
$tags = ['weaning', 'yearling', 'young', 'mature', 'imported'];
|
|
$query = Agriculture3A2b::where('activity_code', 'sapi_potong')
|
|
->where(function ($query) use ($tags) {
|
|
foreach ($tags as $tag) {
|
|
$query->orWhere(function ($query) use ($tag) {
|
|
$query->where('tag_1', $tag)
|
|
->where('tag_2', 'pastura');
|
|
});
|
|
}
|
|
})
|
|
->where('inventory_year', $inventoryYear)
|
|
->where('activity_year', $activityYear)
|
|
->get();
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function calcAndSave($inventoryYear, $activityYear, $ws)
|
|
{
|
|
// initialize variables
|
|
$dataForms = [];
|
|
$wsCode = $ws->ws_code;
|
|
|
|
$data = [
|
|
'grassland' => [
|
|
'f_sn' => 0,
|
|
'f_on' => 0,
|
|
'f_prp' => 0,
|
|
],
|
|
'cropland' => [
|
|
'f_sn' => 0,
|
|
'f_on' => 0,
|
|
'f_prp' => 0,
|
|
],
|
|
];
|
|
|
|
$ef = []; // Empty array for additional data
|
|
|
|
try {
|
|
$ws3c4 = Agriculture3C4::where('inventory_year', $inventoryYear)
|
|
->where('activity_year', $activityYear)->rowActive()->first();
|
|
|
|
if ($ws3c4) {
|
|
# -----
|
|
# Dari worksheet 3A2, ambil NE MMS dari:
|
|
# 'beef_cattle_weaning_pastura', 'beef_cattle_yearling_pastura', 'beef_cattle_young_pastura', 'beef_cattle_mature_pastura', 'beef_cattle_imported_pastura'
|
|
# -----
|
|
$ws3a2b = $this->getAgriculture3A2b($inventoryYear, $activityYear);
|
|
$ne_mms = 0;
|
|
foreach ($ws3a2b as $p) {
|
|
$ne_mms += $p->ne_mms;
|
|
}
|
|
|
|
# -----
|
|
# Ambil data 3C4
|
|
# grassland = ladang
|
|
# cropland = sawah
|
|
# -----
|
|
$data['grassland']['f_sn'] = $ws3c4->f_sn_dry;
|
|
$data['grassland']['f_on'] = $ws3c4->f_on_dry_land_kg;
|
|
$data['grassland']['f_prp'] = $ne_mms;
|
|
|
|
$data['cropland']['f_sn'] = $ws3c4->f_sn_ricefield;
|
|
$data['cropland']['f_on'] = $ws3c4->f_on_ricefield_kg;
|
|
$data['cropland']['f_prp'] = 0;
|
|
}
|
|
|
|
# ambil data emisi
|
|
$ef['Frac_GASF'] = ReferenceEf::getValue('luas_panen_padi_sawah', 'frac_gasf');
|
|
$ef['Frac_GASM'] = ReferenceEf::getValue('luas_panen_padi_sawah', 'frac_gasm');
|
|
$ef['EF4'] = ReferenceEf::getValue('luas_panen_padi_sawah', 'ef4');
|
|
|
|
# hitung emisi
|
|
$no = 1;
|
|
foreach ($data as $land => $item) {
|
|
if ((float)$item['f_sn'] === 0.0 && (float)$item['f_on'] === 0.0 && (float)$item['f_prp'] === 0.0) {
|
|
$n2o_n = 0;
|
|
} else {
|
|
$n2o_n = ($item['f_sn'] * $ef['Frac_GASF'] + ($item['f_on'] + $item['f_prp']) * $ef['Frac_GASM']) * $ef['EF4'] * 44 / 28;
|
|
}
|
|
|
|
$dataForm = [
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'category' => $wsCode,
|
|
'activity' => $land,
|
|
'f_sn' => $item['f_sn'],
|
|
'frac_gasf' => $ef['Frac_GASF'],
|
|
'f_on' => $item['f_on'],
|
|
'f_prp' => $item['f_prp'],
|
|
'frac_gasm' => $ef['Frac_GASM'],
|
|
'ef4' => $ef['EF4'],
|
|
'n2o_n' => $n2o_n,
|
|
'row_num' => $no++,
|
|
];
|
|
|
|
$this->createOrUpdate([
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'category' => $wsCode,
|
|
'activity' => $land,
|
|
'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_n')) / 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;
|
|
}
|
|
}
|
|
}
|
|
// EDIT MONITOR
|