269 lines
11 KiB
PHP
269 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Emisi;
|
|
|
|
use App\Enums\FruitVegetableCategory;
|
|
use App\Models\ActivityCrf;
|
|
use App\Models\ActivityFormDetail;
|
|
use App\Models\Agriculture3C4;
|
|
use App\Models\ReferenceEf;
|
|
use App\Services\SigdCrudService;
|
|
|
|
class Agriculture3C4Service extends SigdCrudService
|
|
{
|
|
private $sectorCode = 'agriculture';
|
|
|
|
public function __construct(Agriculture3C4 $model)
|
|
{
|
|
$this->model = $model;
|
|
}
|
|
|
|
public function save($code, $inventoryYear)
|
|
{
|
|
try {
|
|
$dataBatch = [];
|
|
$ws = $this->getWs($code);
|
|
$years = activityYearRange($inventoryYear);
|
|
|
|
if ($ws && class_basename($ws->model) == 'Agriculture3C4') {
|
|
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;
|
|
$wsCode = $ws->ws_code;
|
|
|
|
$ricefields = [
|
|
'luas_panen_padi_sawah' => 0,
|
|
'luas_panen_padi_ladang' => 0,
|
|
];
|
|
|
|
$crops = [];
|
|
$horticultures = [];
|
|
$fertilizers = [];
|
|
|
|
$total_fruit = 0;
|
|
$total_vegetable = 0;
|
|
$cropland_production = 0;
|
|
|
|
$total_crop = 0;
|
|
$total_horticulture = 0;
|
|
$total_area = 0;
|
|
|
|
$ef = [];
|
|
|
|
try {
|
|
$no = 1;
|
|
|
|
# inisialisasi get data ActivityFormDetail
|
|
$formTanamanPangan = $this->getActivityFormDetailsByYear($inventoryYear, $activityYear, $sector, 'tanaman_pangan');
|
|
$formHortikultura = $this->getActivityFormDetailsByYear($inventoryYear, $activityYear, $sector, 'hortikultura');
|
|
$formPupuk = $this->getActivityFormDetailsByYear($inventoryYear, $activityYear, $sector, 'pupuk');
|
|
|
|
# ambil data tanaman pangan
|
|
foreach ($formTanamanPangan as $row) $crops[$row->activity_code] = $row->activity_value ?? 0;
|
|
|
|
# ambil data tanaman hortikultura
|
|
foreach ($formHortikultura as $row) {
|
|
$horticultures[$row->activity_code] = $row->activity_value ?? 0;
|
|
$type = FruitVegetableCategory::getType($row->activity_code);
|
|
if ($type == 'fruit') $total_fruit += $row->activity_value;
|
|
else $total_vegetable += $row->activity_value;
|
|
}
|
|
|
|
# ambil data pupuk
|
|
foreach ($formPupuk as $row) $fertilizers[$row->activity_code] = $row->activity_value ?? 0;
|
|
|
|
# ambil data padi ladang dan padi sawah Note: ambil luas panen padi sawah & ladang
|
|
$formLuasPanen = $this->getActivityFormDetailsByYear($inventoryYear, $activityYear, $sector, 'sawah', ['luas_panen_padi_sawah', 'luas_panen_padi_ladang']);
|
|
foreach ($formLuasPanen as $row) $ricefields[$row->activity_code] = $row->activity_value ?? 0;
|
|
|
|
# ambil data produksi sawah
|
|
$formTanamanPangan = $this->getActivityFormDetailsByYear($inventoryYear, $activityYear, $sector, 'sawah', 'produksi_padi_sawah');
|
|
foreach ($formTanamanPangan as $row) $cropland_production = $row->activity_value;
|
|
|
|
# ambil data faktor emisi
|
|
$ef['managed_soils'] = ReferenceEf::getValue('luas_panen_padi_ladang', 'managed_soils');
|
|
$ef['flooded_rice'] = ReferenceEf::getValue('luas_panen_padi_sawah', 'flooded_rice');
|
|
|
|
# hitung semuanya
|
|
foreach ($ricefields as $value) $total_crop += $value;
|
|
foreach ($crops as $value) $total_crop += $value;
|
|
foreach ($horticultures as $value) $total_horticulture += $value;
|
|
|
|
$total_area = $total_crop + $total_horticulture;
|
|
$harvest_dry_area = $total_crop + $total_horticulture - $ricefields['luas_panen_padi_sawah'];
|
|
$harvest_ricefield_area = $ricefields['luas_panen_padi_sawah'];
|
|
|
|
$percentage_dry_area = $total_area == 0 ? 0 : (float) ($harvest_dry_area / $total_area);
|
|
$percentage_ricefield_area = $total_area == 0 ? 0 : (float) ($harvest_ricefield_area / $total_area);
|
|
|
|
#-----
|
|
# DATA PUPUK
|
|
# Semua variabel $plantations tidak dipake, Jakarta tidak ada kebun sawit
|
|
#-----
|
|
$n_urea = 0.46 * ($fertilizers['urea'] ?? 0);
|
|
$n_npk = 0.15 * ($fertilizers['npk'] ?? 0);
|
|
$n_za = 0.21 * ($fertilizers['za'] ?? 0);
|
|
|
|
$n_sinthetic = $n_urea + $n_npk + $n_za;
|
|
|
|
$n_dry_tonne = $percentage_dry_area * $n_sinthetic;
|
|
$n_ricefield_tonne = $percentage_ricefield_area * $n_sinthetic;
|
|
|
|
$n_dry_kg = $n_dry_tonne * 1000;
|
|
$n_ricefield_kg = $n_ricefield_tonne * 1000;
|
|
// $n_oil_palm_kg = 250 * $plantations['private_palm'] * 0.46;
|
|
|
|
$f_sn_dry = $n_dry_kg; // + $n_oil_palm_kg;
|
|
$f_sn_ricefield = $n_ricefield_kg;
|
|
|
|
$f_on_ricefield_tonne = $ricefields['luas_panen_padi_sawah'] * 2 * 0.16;
|
|
$f_on_ricefield_kg = $f_on_ricefield_tonne * 1000;
|
|
|
|
$ricefield_production_adj = $cropland_production * 1.4;
|
|
$f_cr_ricefield_tonne = $ricefield_production_adj * 0.3 * 0.005;
|
|
$f_cr_ricefield_kg = $f_cr_ricefield_tonne * 1000;
|
|
|
|
#-----
|
|
# TOTAL FRUIT = ALPUKAT s.d. SUKUN
|
|
# TOTAL VEGETABLES = SISANYA
|
|
#-----
|
|
$dry_land_non_veg = $total_crop + $total_fruit - $ricefields['luas_panen_padi_sawah'];
|
|
$f_on_dry_land_non_veg_tonne = $dry_land_non_veg * 2 * 0.16;
|
|
$f_on_dry_land_non_veg_kg = $f_on_dry_land_non_veg_tonne * 1000;
|
|
|
|
$dry_land_veg = $total_vegetable;
|
|
$f_on_dry_land_veg_tonne = $dry_land_veg * 10 * 0.005;
|
|
$f_on_dry_land_veg_kg = $f_on_dry_land_veg_tonne * 1000;
|
|
|
|
$f_on_dry_land_tonne = $f_on_dry_land_non_veg_tonne + $f_on_dry_land_veg_tonne;
|
|
$f_on_dry_land_kg = $f_on_dry_land_tonne * 1000;
|
|
|
|
$n2o_n_f_sn_managed_soils = $f_sn_dry * $ef['managed_soils']; # Masukkan tabel EF -- aktivitas = luas panen padi ladang
|
|
$n2o_n_f_on_managed_soils = $f_on_dry_land_kg * $ef['managed_soils'];
|
|
|
|
$n2o_n_f_sn_flooded_rice = $f_sn_ricefield * $ef['flooded_rice']; # Masukkan tabel EF -- aktivitas = luas panen padi sawah
|
|
$n2o_n_f_on_flooded_rice = $f_on_ricefield_kg * $ef['flooded_rice'];
|
|
$n2o_n_f_cr_flooded_rice = $f_cr_ricefield_kg * $ef['flooded_rice'];
|
|
|
|
$n2o_n_total =
|
|
$n2o_n_f_sn_managed_soils +
|
|
$n2o_n_f_on_managed_soils +
|
|
$n2o_n_f_sn_flooded_rice +
|
|
$n2o_n_f_on_flooded_rice +
|
|
$n2o_n_f_cr_flooded_rice;
|
|
|
|
# hitung emisi
|
|
$total_n2o = $n2o_n_total * 44 / 28;
|
|
|
|
$dataForm = [
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'category' => $wsCode,
|
|
'crop_area' => $total_crop,
|
|
'horticulture_area' => $total_horticulture,
|
|
'total_area' => $total_area,
|
|
'harvest_dry_area' => $harvest_dry_area,
|
|
'harvest_ricefield_area' => $harvest_ricefield_area,
|
|
'percentage_dry_area' => $percentage_dry_area,
|
|
'percentage_ricefield_area' => $percentage_ricefield_area,
|
|
'urea' => $fertilizers['urea'] ?? 0,
|
|
'npk' => $fertilizers['npk'] ?? 0,
|
|
'za' => $fertilizers['za'] ?? 0,
|
|
'n_urea' => $n_urea,
|
|
'n_npk' => $n_npk,
|
|
'n_za' => $n_za,
|
|
'n_sinthetic' => $n_sinthetic,
|
|
'n_dry_tonne' => $n_dry_tonne,
|
|
'n_ricefield_tonne' => $n_ricefield_tonne,
|
|
'n_dry_kg' => $n_dry_kg,
|
|
'n_ricefield_kg' => $n_ricefield_kg,
|
|
'f_sn_dry' => $f_sn_dry,
|
|
'f_sn_ricefield' => $f_sn_ricefield,
|
|
'f_on_ricefield_tonne' => $f_on_ricefield_tonne,
|
|
'f_on_ricefield_kg' => $f_on_ricefield_kg,
|
|
'ricefield_production' => $cropland_production,
|
|
'ricefield_production_adj' => $ricefield_production_adj,
|
|
'f_cr_ricefield_tonne' => $f_cr_ricefield_tonne,
|
|
'f_cr_ricefield_kg' => $f_cr_ricefield_kg,
|
|
'dry_land_non_veg' => $dry_land_non_veg,
|
|
'f_on_dry_land_non_veg_tonne' => $f_on_dry_land_non_veg_tonne,
|
|
'f_on_dry_land_non_veg_kg' => $f_on_dry_land_non_veg_kg,
|
|
'dry_land_veg' => $dry_land_veg,
|
|
'f_on_dry_land_veg_tonne' => $f_on_dry_land_veg_tonne,
|
|
'f_on_dry_land_veg_kg' => $f_on_dry_land_veg_kg,
|
|
'f_on_dry_land_tonne' => $f_on_dry_land_tonne,
|
|
'f_on_dry_land_kg' => $f_on_dry_land_kg,
|
|
'ef_managed_soils' => $ef['managed_soils'],
|
|
'ef_flooded_rice' => $ef['flooded_rice'],
|
|
'n2o_n_f_sn_managed_soils' => $n2o_n_f_sn_managed_soils,
|
|
'n2o_n_f_on_managed_soils' => $n2o_n_f_on_managed_soils,
|
|
'n2o_n_f_sn_flooded_rice' => $n2o_n_f_sn_flooded_rice,
|
|
'n2o_n_f_on_flooded_rice' => $n2o_n_f_on_flooded_rice,
|
|
'n2o_n_f_cr_flooded_rice' => $n2o_n_f_cr_flooded_rice,
|
|
'n2o_n_total' => $total_n2o,
|
|
];
|
|
|
|
$this->createOrUpdate([
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'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 = null;
|
|
$n2o = array_sum(array_column($dataForms, 'n2o_n_total'));
|
|
$n2o = $n2o / 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;
|
|
}
|
|
}
|
|
}
|