model = $model; } public function save($code, $inventoryYear) { try { $dataBatch = []; $ws = $this->getWs($code); $years = activityYearRange($inventoryYear); if ($ws && class_basename($ws->model) == 'Agriculture3A2b') { 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) { $filterTag1 = ['weaning', 'yearling', 'young', 'mature', 'imported']; $filterTag2 = ['pastura', 'dry_lot']; // Prepare CASE statements for custom sorting $orderTag1 = 'CASE tag_1 '; foreach ($filterTag1 as $index => $value) { $orderTag1 .= "WHEN '$value' THEN " . ($index + 1) . " "; } $orderTag1 .= "ELSE " . (count($filterTag1) + 1) . " END"; $orderTag2 = 'CASE tag_2 '; foreach ($filterTag2 as $index => $value) { $orderTag2 .= "WHEN '$value' THEN " . ($index + 1) . " "; } $orderTag2 .= "ELSE " . (count($filterTag2) + 1) . " END"; $data = ReferenceEf::select('tag_1', 'tag_2') ->selectRaw("$orderTag1 AS order_tag1") ->selectRaw("$orderTag2 AS order_tag2") ->distinct()->where('activity_code', $activityCode) ->where(function ($query) { $query->where(function ($subQuery) { $subQuery->whereNotNull('tag_1') ->whereNotNull('tag_2') ->whereNull('tag_3'); })->orWhere(function ($subQuery) { $subQuery->whereNull('tag_1') ->whereNull('tag_2') ->whereNull('tag_3'); }); })->where('active_status', 1)->where('row_status', 1) ->orderByRaw('order_tag1')->orderByRaw('order_tag2') ->get(); 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->tag_1); $num_animal = $value == 0 ? 0 : round($value * $ef_proportion / 100, 0); $ef_excretion_rate = ReferenceEf::getValue($activity, 'excretion_rate', $ef->tag_1, $ef->tag_2); $ef_tam = ReferenceEf::getValue($activity, 'tam', $ef->tag_1, $ef->tag_2); $nex = $value == 0 ? 0 : $ef_excretion_rate * $ef_tam / 1000 * 365; $ef_nitrogen_fraction = ReferenceEf::getValue($activity, 'nitrogen_fraction', $ef->tag_1, $ef->tag_2); $ne_mms = $value == 0 ? 0 : $num_animal * $nex * $ef_nitrogen_fraction / 100; $ef_mms = ReferenceEf::getValue($activity, 'mms_ef', $ef->tag_1, $ef->tag_2); $n2o_d = $value == 0 ? 0 : $ne_mms * $ef_mms * 44 / 28; $dataForm = [ 'inventory_year' => $inventoryYear, 'activity_year' => $activityYear, 'activity_code' => $activity, 'category' => $wsCode, 'tag_1' => $ef->tag_1 ?? null, 'tag_2' => $ef->tag_2 ?? null, 'value' => $value, 'ef_proportion' => $ef_proportion, 'num_animal' => $num_animal, 'ef_excretion_rate' => $ef_excretion_rate, 'ef_tam' => $ef_tam, 'nex' => $nex, 'ef_nitrogen_fraction' => $ef_nitrogen_fraction, 'ne_mms' => $ne_mms, 'ef_mms' => $ef_mms, 'n2o_d' => $n2o_d, 'row_num' => $no++, ]; $this->createOrUpdate([ 'inventory_year' => $inventoryYear, 'activity_year' => $activityYear, 'activity_code' => $activity, 'category' => $wsCode, 'tag_1' => $ef->tag_1 ?? null, 'tag_2' => $ef->tag_2 ?? 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 = null; $n2o = array_sum(array_column($dataForms, 'n2o_d')) / 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