sigd/app/Services/Emisi/Waste4D1aService.php

84 lines
2.3 KiB
PHP

<?php
namespace App\Services\Emisi;
use App\Models\ActivityFormDetail;
use App\Models\ReferenceEf;
use App\Models\Waste4D1a;
use App\Services\SigdCrudService;
class Waste4D1aService extends SigdCrudService
{
private $sectorCode = 'waste';
private $formCode = 'kependudukan';
public function __construct(Waste4D1a $model)
{
$this->model = $model;
}
public function save($code, $inventoryYear)
{
try {
$dataBatch = [];
$ws = $this->getWs($code);
$years = activityYearRange($inventoryYear);
if ($ws && class_basename($ws->model) == 'Waste4D1a') {
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 = [];
$sector = $this->sectorCode;
$code = $this->formCode;
$wsCode = $ws->ws_code;
try {
// ambil data aktivitas
$population = ActivityFormDetail::getValue($inventoryYear, $activityYear, $sector, $code, 'jumlah_penduduk');
$ef_bod = ReferenceEf::getValue('jumlah_penduduk', 'BOD');
$ef_cf = ReferenceEf::getValue('jumlah_penduduk', 'CF');
$tow = $population * $ef_bod * $ef_cf;
$no = 1;
$dataForm = [
'inventory_year' => $inventoryYear,
'activity_year' => $activityYear,
'category' => $wsCode,
'population' => $population,
'ef_bod' => $ef_bod,
'ef_correction_factor' => $ef_cf,
'tow' => $tow,
'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;
}
}
}