95 lines
2.9 KiB
PHP
95 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Emisi;
|
|
|
|
use App\Models\ReferenceEf;
|
|
use App\Models\Waste4D1d;
|
|
use App\Services\SigdCrudService;
|
|
|
|
class Waste4D1dService extends SigdCrudService
|
|
{
|
|
private $sectorCode = 'waste';
|
|
private $formCode = 'kependudukan';
|
|
|
|
public function __construct(Waste4D1d $model)
|
|
{
|
|
$this->model = $model;
|
|
}
|
|
|
|
public function save($code, $inventoryYear)
|
|
{
|
|
try {
|
|
$dataBatch = [];
|
|
$ws = $this->getWs($code);
|
|
$years = activityYearRange($inventoryYear);
|
|
|
|
if ($ws && class_basename($ws->model) == 'Waste4D1d') {
|
|
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)
|
|
{
|
|
$dataForms = [];
|
|
$activities = [];
|
|
|
|
$sector = $this->sectorCode;
|
|
$code = $this->formCode;
|
|
$wsCode = $ws->ws_code;
|
|
|
|
try {
|
|
// Get data activities
|
|
$formActivity = $this->getActivityFormDetailsByYear($inventoryYear, $activityYear, $sector, $code);
|
|
foreach ($formActivity as $row) $activities[$row->activity_code] = $row->activity_value ?? 0;
|
|
|
|
$population = $activities['jumlah_penduduk'] ?? 0;
|
|
$protein = $activities['protein_per_kapita'] ?? 0;
|
|
|
|
$ef_f_npr = ReferenceEf::getValue('protein_per_kapita', 'F_NPR');
|
|
$ef_f_non_con = ReferenceEf::getValue('protein_per_kapita', 'F_NON_CON');
|
|
$ef_f_ind_com = ReferenceEf::getValue('protein_per_kapita', 'F_IND_COM');
|
|
$ef_n_sludge = ReferenceEf::getValue('protein_per_kapita', 'N_sludge');
|
|
|
|
$n_effluent = ($population * $protein * $ef_f_npr * $ef_f_non_con * $ef_f_ind_com) - $ef_n_sludge;
|
|
|
|
$no = 1;
|
|
|
|
$dataForm = [
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'category' => $wsCode,
|
|
'population' => $population,
|
|
'protein' => $protein,
|
|
'ef_f_npr' => $ef_f_npr,
|
|
'ef_f_non_con' => $ef_f_non_con,
|
|
'ef_f_ind_com' => $ef_f_ind_com,
|
|
'ef_n_sludge' => $ef_n_sludge,
|
|
'n_effluent' => $n_effluent,
|
|
'row_num' => $no++,
|
|
];
|
|
|
|
$this->createOrUpdate([
|
|
'inventory_year' => $inventoryYear,
|
|
'activity_year' => $activityYear,
|
|
'category' => $wsCode,
|
|
'row_status' => 1,
|
|
], $dataForm);
|
|
|
|
$dataForms[] = $dataForm;
|
|
|
|
return $dataForms;
|
|
} catch (\Exception $e) {
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|