model = $model; } public function save($code, $inventoryYear) { try { $dataBatch = []; $ws = $this->getWs($code); $years = activityYearRange($inventoryYear); if ($ws && class_basename($ws->model) == 'Agriculture3A') { 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 getEfData($activityCode) { $order = ['weaning', 'yearling', 'young', 'mature', 'imported']; // Prepare a CASE statement for custom sorting $orderStat = 'CASE '; foreach ($order as $index => $value) { $orderStat .= "WHEN tag_1 = '$value' THEN " . ($index + 1) . " "; } // If there are other values, place them at the end $orderStat .= "ELSE " . (count($order) + 1) . " END AS order_tag1"; $data = ReferenceEf::selectRaw("DISTINCT tag_1, $orderStat") ->where('activity_code', $activityCode) ->where(function ($query) { $query->where(function ($subQuery) { $subQuery->whereNotNull('tag_1') ->whereNull('tag_2') ->whereNull('tag_3'); })->orWhere(function ($subQuery) { $subQuery->whereNull('tag_1') ->whereNull('tag_2') ->whereNull('tag_3'); }); }) ->isActive()->rowActive()->orderBy('order_tag1') ->pluck('tag_1'); return $data; } public function calcAndSave($inventoryYear, $activityYear, $ws) { // Initialize variables $dataForms = []; $activities = []; $sector = $this->sectorCode; $code = $this->formCode; $wsCode = $ws->ws_code; try { // Initialize activities $formSetting = $this->getDataSettingFormDetail($sector, $code); foreach ($formSetting as $row) $activities[$row->activity_code] = 0; // Get data activities $formActivity = $this->getActivityFormDetailsByYear($inventoryYear, $activityYear, $sector, $code); foreach ($formActivity as $row) $activities[$row->activity_code] = $row->activity_value ?? 0; $no = 1; foreach ($activities as $activity => $value) { $efs = $this->getEfData($activity); foreach ($efs as $ef) { $ef_proportion = ReferenceEf::getValue($activity, 'proportion', $ef); $num_animal = $value == 0 ? 0 : round($value * $ef_proportion / 100, 0); $ef_ef = ReferenceEf::getValue($activity, $ws->code . '_ef', $ef); $ch4 = $value == 0 ? 0 : $num_animal * $ef_ef / 1000000; $dataForm = [ 'inventory_year' => $inventoryYear, 'activity_year' => $activityYear, 'activity_code' => $activity, 'category' => $wsCode, 'tag_1' => $ef ?? null, 'value' => $value, 'ef_proportion' => $ef_proportion, 'num_animal' => $num_animal, 'ef' => $ef_ef, 'ch4' => $ch4, 'row_num' => $no++, ]; $this->createOrUpdate([ 'inventory_year' => $inventoryYear, 'activity_year' => $activityYear, 'activity_code' => $activity, 'category' => $wsCode, 'tag_1' => $ef ?? null, '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, 'ch4')); $n2o = null; $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; } } }