public function saveEditRunner(Request $request) { $data = $request->all(); $runner_id = $data['runner_id']; $runner_name = $data['runner_name']; if (!$runner_name) { $runner_name = "R{$runner_id}"; } $obj_froms = explode(',', $data['obj_from']); $obj_tos = explode(',', $data['obj_to']); $order = $data['order']; $alloc_type = $data['alloc_type']; $theor_value_type = $data['theor_value_type']; $theor_phase = $data['theor_phase']; //Update order AllocRunner::where(['ID' => $runner_id])->update(['NAME' => $runner_name, 'ORDER' => $order, 'ALLOC_TYPE' => $alloc_type, 'THEOR_VALUE_TYPE' => $theor_value_type, 'THEOR_PHASE' => $theor_phase]); //Delete all Object in runner AllocRunnerObjects::where(['RUNNER_ID' => $runner_id])->delete(); //Add again foreach ($obj_froms as $obj_from) { $xs_f = explode(':', $obj_from); if ($xs_f[1] == "") { continue; } AllocRunnerObjects::insert(['RUNNER_ID' => $runner_id, 'OBJECT_TYPE' => $xs_f[1], 'OBJECT_ID' => $xs_f[0], 'DIRECTION' => 1, 'MINUS' => $xs_f[2]]); } foreach ($obj_tos as $obj_to) { $xs_t = explode(':', $obj_to); if ($xs_t[1] == "") { continue; } AllocRunnerObjects::insert(['RUNNER_ID' => $runner_id, 'OBJECT_TYPE' => $xs_t[1], 'OBJECT_ID' => $xs_t[0], 'DIRECTION' => 0, 'MINUS' => $xs_t[2]]); } return response()->json('ok'); }
private function run_runner($runner_id, $from_date, $to_date, $alloc_attr, $alloc_phase, $theor_phase, $alloc_comp, $alloc_type, $theor_attr) { global $error_count; $runner_name = AllocRunner::where(['ID' => $runner_id])->select('NAME')->first(); $this->_log("Begin runner {$runner_name->NAME} (ID: {$runner_id}), from date: {$from_date}, to date: {$to_date}, alloc_attr: {$alloc_attr}, alloc_phase: {$alloc_phase}", 2); $xdate = date_create("2016-01-01"); $from_date = date('Y-m-d', strtotime($from_date)); $to_date = date('Y-m-d', strtotime($to_date)); if (date_create($from_date) < $xdate || date_create($to_date) < $xdate) { $ret = $this->_log("Can not run allocation for the date earlier than 01/01/2016.", 1); return false; } $success = true; $event_type = 0; if ($alloc_type == 1 || $alloc_type == 2) { $event_type = $alloc_type; } if ($alloc_phase != 2) { $alloc_comp = false; } if (!$theor_phase) { $theor_phase = $alloc_phase; } else { if ($theor_phase != $alloc_phase) { $this->_log("Theor. phase changed to: {$theor_phase}", 2); } } $F = "VOL"; if (strpos($alloc_attr, 'MASS') !== false) { $F = "MASS"; } $alloc_attr_eu = $alloc_attr; if ($alloc_attr == "NET_VOL") { $alloc_attr_eu = "GRS_VOL"; } if (!$theor_attr) { $theor_attr = $alloc_attr; } else { if ($theor_attr != $alloc_attr) { $this->_log("Theor. value type changed to: {$theor_attr}", 2); } } $theor_attr_eu = $theor_attr; if ($theor_attr == "NET_VOL") { $theor_attr_eu = "GRS_VOL"; } $total_from = 0; $total_to = 0; $total_fixed = 0; $ids_from = ""; $ids_to = ""; $ids_fixed = ""; $ids_minus = "-999"; $obj_type_from = ""; $obj_type_to = ""; $OBJ_TYPE_FLOW = 1; $OBJ_TYPE_EU = 2; $OBJ_TYPE_TANK = 3; $OBJ_TYPE_STORAGE = 4; $rows = AllocRunnerObjects::where(['RUNNER_ID' => $runner_id])->get(); foreach ($rows as $row) { if ($row->DIRECTION == 1) { if (!$obj_type_from) { $obj_type_from = $row->OBJECT_TYPE; } if ($row->OBJECT_TYPE == $obj_type_from) { $ids_from .= ($ids_from ? "," : "") . $row->OBJECT_ID; } if ($row->MINUS == 1) { $ids_minus .= ($ids_minus ? "," : "") . $row->OBJECT_ID; } } else { if (!$obj_type_to) { $obj_type_to = $row->OBJECT_TYPE; } if ($row->OBJECT_TYPE == $obj_type_to) { if ($row->FIXED == 1) { $ids_fixed .= ($ids_fixed ? "," : "") . $row->OBJECT_ID; } else { $ids_to .= ($ids_to ? "," : "") . $row->OBJECT_ID; } } } } if ($ids_from) { $total_from = 0; $sum = []; $sSQL_alloc_from = []; $arrfrom = explode(',', $ids_from); if ($obj_type_from == $OBJ_TYPE_FLOW) { // //\DB::enableQueryLog (); $sum = DB::table('FLOW_DATA_ALLOC AS a')->leftjoin('FLOW_DATA_VALUE AS v', function ($join) { $join->on('v.FLOW_ID', '=', 'a.FLOW_ID'); $join->on('v.OCCUR_DATE', '=', 'a.OCCUR_DATE'); })->leftjoin('FLOW_DATA_THEOR AS t', function ($join) { $join->on('v.FLOW_ID', '=', 'a.FLOW_ID'); $join->on('v.OCCUR_DATE', '=', 'a.OCCUR_DATE'); })->join('FLOW AS b', 'a.FLOW_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.FLOW_ID', $arrfrom)->SELECT(DB::raw('sum((case when a.FLOW_ID in (' . $ids_minus . ') then -1 else 1 end))*IF(IFNULL(a.FL_DATA_' . $alloc_attr . ',0)>0,a.FL_DATA_' . $alloc_attr . ',IF(IFNULL(v.FL_DATA_' . $alloc_attr . ',0)>0,v.FL_DATA_' . $alloc_attr . ',t.FL_DATA_' . $alloc_attr . ')) AS total_from'))->get(); // //\Log::info ( \DB::getQueryLog () ); //\DB::enableQueryLog (); $sSQL_alloc_from = DB::table('FLOW_DATA_ALLOC AS a')->leftjoin('FLOW_DATA_VALUE AS v', function ($join) { $join->on('v.FLOW_ID', '=', 'a.FLOW_ID'); $join->on('v.OCCUR_DATE', '=', 'a.OCCUR_DATE'); })->leftjoin('FLOW_DATA_THEOR AS t', function ($join) { $join->on('v.FLOW_ID', '=', 'a.FLOW_ID'); $join->on('v.OCCUR_DATE', '=', 'a.OCCUR_DATE'); })->join('FLOW AS b', 'a.FLOW_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.FLOW_ID', $arrfrom)->SELECT(DB::raw('IF(IFNULL(a.FL_DATA_' . $alloc_attr . ',0)>0,a.FL_DATA_' . $alloc_attr . ',IF(IFNULL(v.FL_DATA_' . $alloc_attr . ',0)>0,v.FL_DATA_' . $alloc_attr . ',t.FL_DATA_' . $alloc_attr . ')) AS ALLOC_VALUE', 'a.FLOW_ID AS OBJECT_ID', 'b.NAME AS OBJECT_NAME', 'a.ACTIVE_HRS', 'b.NAME AS ALLOC_VALUE', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR'))->get(); //\Log::info ( \DB::getQueryLog () ); } else { if ($obj_type_from == $OBJ_TYPE_EU) { // //\DB::enableQueryLog (); $sum = DB::table('ENERGY_UNIT_DATA_ALLOC AS a')->leftjoin('ENERGY_UNIT_DATA_VALUE AS v', function ($join) { $join->on('v.EU_ID', '=', 'a.EU_ID'); $join->on('v.OCCUR_DATE', '=', 'a.OCCUR_DATE'); $join->on('v.FLOW_PHASE', '=', 'a.FLOW_PHASE'); $join->on('v.EVENT_TYPE', '=', 'a.EVENT_TYPE'); })->leftjoin('ENERGY_UNIT_DATA_THEOR AS t', function ($join) { $join->on('t.EU_ID', '=', 'a.EU_ID'); $join->on('t.OCCUR_DATE', '=', 'a.OCCUR_DATE'); $join->on('t.FLOW_PHASE', '=', 'a.FLOW_PHASE'); $join->on('t.EVENT_TYPE', '=', 'a.EVENT_TYPE'); })->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.EU_ID', $arrfrom)->where(['a.FLOW_PHASE' => $alloc_phase])->where(['a.EVENT_TYPE' => $event_type])->SELECT(DB::raw('sum((case when a.EU_ID in (' . $ids_minus . ') then -1 else 1 end))*IF(IFNULL(a.EU_DATA_' . $alloc_attr_eu . ',0)>0,a.EU_DATA_' . $alloc_attr_eu . ',IF(IFNULL(v.EU_DATA_' . $alloc_attr_eu . ',0)>0,v.EU_DATA_' . $alloc_attr_eu . ',t.EU_DATA_' . $alloc_attr_eu . ')) AS total_from'))->get(); // //\Log::info ( \DB::getQueryLog () ); //\DB::enableQueryLog (); $sSQL_alloc_from = DB::table('ENERGY_UNIT_DATA_ALLOC AS a')->leftjoin('ENERGY_UNIT_DATA_VALUE AS v', function ($join) { $join->on('v.EU_ID', '=', 'a.EU_ID'); $join->on('v.OCCUR_DATE', '=', 'a.OCCUR_DATE'); $join->on('v.FLOW_PHASE', '=', 'a.FLOW_PHASE'); $join->on('v.EVENT_TYPE', '=', 'a.EVENT_TYPE'); })->leftjoin('ENERGY_UNIT_DATA_THEOR AS t', function ($join) { $join->on('t.EU_ID', '=', 'a.EU_ID'); $join->on('t.OCCUR_DATE', '=', 'a.OCCUR_DATE'); $join->on('t.FLOW_PHASE', '=', 'a.FLOW_PHASE'); $join->on('t.EVENT_TYPE', '=', 'a.EVENT_TYPE'); })->join('ENERGY_UNIT AS b', 'a.EU_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.EU_ID', $arrfrom)->where(['a.FLOW_PHASE' => $alloc_phase])->where(['a.EVENT_TYPE' => $event_type])->SELECT(DB::raw('IF(IFNULL(a.EU_DATA_' . $alloc_attr_eu . ',0)>0,a.EU_DATA_' . $alloc_attr_eu . ',IF(IFNULL(v.EU_DATA_' . $alloc_attr_eu . ',0)>0,v.EU_DATA_' . $alloc_attr_eu . ',t.EU_DATA_' . $alloc_attr_eu . ')) AS ALLOC_VALUE', 'a.EU_ID AS OBJECT_ID', 'b.NAME AS OBJECT_NAME', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR', 'a.FLOW_PHASE'))->get(); //\Log::info ( \DB::getQueryLog () ); } else { if ($obj_type_from == $OBJ_TYPE_TANK) { // //\DB::enableQueryLog (); $sum = DB::table('TANK_DATA_ALLOC AS a')->leftjoin('TANK_DATA_VALUE AS v', function ($join) { $join->on('v.TANK_ID', '=', 'a.TANK_ID'); $join->on('v.OCCUR_DATE', '=', 'a.OCCUR_DATE'); })->join('TANK AS b', 'a.TANK_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.TANK_ID', $arrfrom)->select(DB::raw('sum((case when a.TANK_ID in (' . $ids_minus . ') then -1 else 1 end)*IF(IFNULL(a.TANK_' . $alloc_attr . ',0)>0,a.TANK_' . $alloc_attr . ',v.TANK_' . $alloc_attr . ')) AS total_from'))->get(); // //\Log::info ( \DB::getQueryLog () ); // //\DB::enableQueryLog (); $sSQL_alloc_from = DB::table('TANK_DATA_ALLOC AS a')->leftjoin('TANK_DATA_VALUE AS v', function ($join) { $join->on('v.TANK_ID', '=', 'a.TANK_ID'); $join->on('v.OCCUR_DATE', '=', 'a.OCCUR_DATE'); })->join('TANK AS b', 'a.TANK_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.TANK_ID', $arrfrom)->select(DB::raw('IF(IFNULL(a.TANK_' . $alloc_attr . ',0)>0,a.TANK_' . $alloc_attr . ',v.TANK_' . $alloc_attr . ') AS total_from', 'a.TANK_ID AS OBJECT_ID', 'b.NAME AS OBJECT_NAME', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR'))->get(); // //\Log::info ( \DB::getQueryLog () ); } else { if ($obj_type_from == $OBJ_TYPE_STORAGE) { // //\DB::enableQueryLog (); $sum = DB::table('STORAGE_DATA_ALLOC AS a')->leftjoin('STORAGE_DATA_VALUE AS v', function ($join) { $join->on('v.STORAGE_ID', '=', 'a.STORAGE_ID'); $join->on('v.OCCUR_DATE', '=', 'a.OCCUR_DATE'); })->join('STORAGE AS b', 'a.STORAGE_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.STORAGE_ID', $arrfrom)->select(DB::raw('sum((case when a.STORAGE_ID in (' . $ids_minus . ') then -1 else 1 end)*IF(IFNULL(a.STORAGE_' . $alloc_attr . ',0)>0,a.STORAGE_' . $alloc_attr . ',a.STORAGE_' . $alloc_attr . ')) AS total_from'))->get(); // //\Log::info ( \DB::getQueryLog () ); //\DB::enableQueryLog (); $sSQL_alloc_from = DB::table('STORAGE_DATA_ALLOC AS a')->leftjoin('STORAGE_DATA_VALUE AS v', function ($join) { $join->on('v.STORAGE_ID', '=', 'a.STORAGE_ID'); $join->on('v.OCCUR_DATE', '=', 'a.OCCUR_DATE'); })->join('STORAGE AS b', 'a.STORAGE_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.STORAGE_ID', $arrfrom)->select(DB::raw('IF(IFNULL(a.STORAGE_' . $alloc_attr . ',0)>0,a.STORAGE_' . $alloc_attr . ',a.STORAGE_' . $alloc_attr . ') AS total_from', 'a.STORAGE_ID AS OBJECT_ID', 'b.NAME as OBJECT_NAME', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR'))->get(); //\Log::info ( \DB::getQueryLog () ); } } } } $total_from = $sum[0]->total_from; // _log("command: $sSQL"); // _log("sSQL_alloc_from: $sSQL_alloc_from"); $this->_log("total_from (allocated ~ std value ~ theor): {$total_from}", 2); if ($total_from <= 0) { // does not have value at "ALLOCATION", use data from STD VALUE $subSum = []; $sum = []; switch ($obj_type_from) { case $OBJ_TYPE_FLOW: $sum = DB::table('FLOW_DATA_VALUE AS a')->join('FLOW AS b', 'a.FLOW_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.FLOW_ID', $arrfrom)->select(DB::raw('sum(FL_DATA_' . $alloc_attr . ') AS total_from'))->get(); break; case $OBJ_TYPE_EU: $sum = DB::table('ENERGY_UNIT_DATA_VALUE AS a')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.EU_ID', $arrfrom)->where(['a.FLOW_PHASE' => $alloc_phase])->select(DB::raw('sum(FL_DATA_' . $alloc_attr . ') AS total_from'))->get(); break; case $OBJ_TYPE_TANK: $sum = DB::table('TANK_DATA_VALUE AS a')->join('TANK AS b', 'a.TANK_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.TANK_ID', $arrfrom)->select(DB::raw('sum(TANK_' . $alloc_attr . ') AS total_from'))->get(); break; case $OBJ_TYPE_STORAGE: $sum = DB::table('STORAGE_DATA_VALUE AS a')->join('STORAGE AS b', 'a.STORAGE_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.STORAGE_ID', $arrfrom)->select(DB::raw('sum(STORAGE_' . $alloc_attr . ') AS total_from'))->get(); break; } $total_from = $sum[0]->total_from; $this->_log("total_from (std value): {$total_from}", 2); } } else { $ret = $this->_log("From objects not found", 1); if ($ret === false) { return false; } } if ($ids_to) { $arrto = explode(',', $ids_to); $sum = []; $sSQL_alloc = []; if ($obj_type_to == $OBJ_TYPE_FLOW) { $sum = DB::table('FLOW_DATA_THEOR AS a')->join('FLOW AS b', 'a.FLOW_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.FLOW_ID', $arrto)->where(['b.PHASE_ID' => $theor_phase])->select(DB::raw('sum(FL_DATA_' . $theor_attr . ') AS total_to'))->get(); $sSQL_alloc = DB::table('FLOW_DATA_THEOR AS a')->join('FLOW AS b', 'a.FLOW_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.FLOW_ID', $arrto)->where(['b.PHASE_ID' => $theor_phase])->get(['a.FLOW_ID AS OBJECT_ID', 'b.NAME AS OBJECT_NAME', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR', 'a.FL_DATA_' . $theor_attr . ' AS ALLOC_THEOR']); $sSQL_alloc_to = DB::table('FLOW_DATA_ALLOC AS a')->join('FLOW AS b', 'a.FLOW_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.FLOW_ID', $arrto)->where(['b.PHASE_ID' => $theor_phase])->get(['a.FLOW_ID AS OBJECT_ID', 'b.NAME AS OBJECT_NAME', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR', 'a.FL_DATA_' . $theor_attr . ' AS ALLOC_VALUE']); } else { if ($obj_type_to == $OBJ_TYPE_EU) { $sum = DB::table('ENERGY_UNIT_DATA_THEOR AS a')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.EU_ID', $arrto)->where(['a.FLOW_PHASE' => $theor_phase, 'a.FLOW_PHASE' => $theor_phase, 'a.EVENT_TYPE' => $event_type])->select(DB::raw('sum(EU_DATA_' . $theor_attr_eu . ') AS total_to'))->get(); $sSQL_alloc = DB::table('ENERGY_UNIT_DATA_THEOR AS a')->join('ENERGY_UNIT AS b', 'a.EU_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.EU_ID', $arrto)->where(['a.FLOW_PHASE' => $theor_phase, 'a.EVENT_TYPE' => $event_type])->get(['a.EU_ID AS OBJECT_ID', 'b.NAME AS OBJECT_NAME', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR', 'a.FLOW_PHASE', 'EU_DATA_' . $theor_attr_eu . ' AS ALLOC_THEOR']); $sSQL_alloc_to = DB::table('ENERGY_UNIT_DATA_ALLOC AS a')->join('ENERGY_UNIT AS b', 'a.EU_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.EU_ID', $arrto)->where(['a.FLOW_PHASE' => $theor_phase, 'a.EVENT_TYPE' => $event_type])->get(['a.EU_ID AS OBJECT_ID', 'b.NAME AS OBJECT_NAME', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR', 'a.FLOW_PHASE', 'EU_DATA_' . $theor_attr_eu . ' AS ALLOC_VALUE']); } else { if ($obj_type_to == $OBJ_TYPE_TANK) { $sum = DB::table('TANK_DATA_VALUE AS a')->join('TANK AS b', 'a.TANK_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.TANK_ID', $arrto)->select(DB::raw('sum(TANK_' . $theor_attr . ') AS total_to'))->get(); $sSQL_alloc = DB::table('TANK_DATA_VALUE AS a')->join('TANK AS b', 'a.TANK_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.TANK_ID', $arrto)->get(['a.TANK_ID AS OBJECT_ID', 'b.NAME AS OBJECT_NAME', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR', 'a.TANK_DATA_' . $theor_attr . ' AS ALLOC_THEOR']); $sSQL_alloc_to = DB::table('TANK_DATA_VALUE AS a')->join('TANK AS b', 'a.TANK_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.TANK_ID', $arrto)->get(['a.TANK_ID AS OBJECT_ID', 'b.NAME AS OBJECT_NAME', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR', 'a.TANK_' . $theor_attr . ' AS ALLOC_VALUE']); } else { if ($obj_type_to == $OBJ_TYPE_STORAGE) { $sum = DB::table('STORAGE_DATA_VALUE AS a')->join('STORAGE AS b', 'a.STORAGE_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.STORAGE_ID', $arrto)->select(DB::raw('sum(STORAGE_' . $theor_attr . ') AS total_to'))->get(); $sSQL_alloc = DB::table('STORAGE_DATA_VALUE AS a')->join('STORAGE AS b', 'a.STORAGE_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.STORAGE_ID', $arrto)->get(['a.STORAGE_ID AS OBJECT_ID', 'b.NAME AS OBJECT_NAME', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR', 'a.STORAGE_' . $theor_attr . ' AS ALLOC_THEOR']); $sSQL_alloc_to = DB::table('STORAGE_DATA_ALLOC AS a')->join('STORAGE AS b', 'a.STORAGE_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.STORAGE_ID', $arrto)->get(['a.STORAGE_ID AS OBJECT_ID', 'b.NAME AS OBJECT_NAME', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR', 'a.STORAGE_' . $theor_attr . ' AS ALLOC_VALUE']); } } } } $total_to = $sum[0]->total_to; // _log("command: $sSQL"); $this->_log("total_to (theor): {$total_to}", 2); $this->_log("Allocation factor: " . $total_from . "/" . $total_to, 2); } else { $ret = $this->_log("TO object not found", 1); if ($ret === false) { return false; } } if ($ids_fixed) { $rows = []; $arrfixed = explode(',', $ids_fixed); if ($obj_type_to == $OBJ_TYPE_FLOW) { //\DB::enableQueryLog (); $rows = DB::table('FLOW_DATA_VALUE AS a')->leftjoin('FLOW_DATA_THEOR AS t', function ($join) { $join->on('t.FLOW_ID', '=', 'a.FLOW_ID'); $join->on('t.OCCUR_DATE', '=', 'a.OCCUR_DATE'); })->join('FLOW AS b', 'a.FLOW_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->where(['b.PHASE_ID' => $alloc_phase])->whereIn('a.FLOW_ID', $arrfixed)->SELECT(DB::raw('IF(IFNULL(a.FL_DATA_' . $alloc_attr . ',0)>0,a.FL_DATA_' . $alloc_attr . ',t.FL_DATA_' . $alloc_attr . ') AS FIXED_VALUE', 'a.FLOW_ID AS OBJECT_ID', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR'))->get(); //\Log::info ( \DB::getQueryLog () ); } else { if ($obj_type_to == $OBJ_TYPE_EU) { //\DB::enableQueryLog (); $rows = DB::table('ENERGY_UNIT_DATA_VALUE AS a')->leftjoin('ENERGY_UNIT_DATA_THEOR AS t', function ($join) { $join->on('t.EU_ID', '=', 'a.EU_ID'); $join->on('t.OCCUR_DATE', '=', 'a.OCCUR_DATE'); $join->on('t.FLOW_PHASE', '=', 'a.FLOW_PHASE'); $join->on('t.EVENT_TYPE', '=', 'a.EVENT_TYPE'); })->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.EU_ID', $arrfixed)->where(['a.FLOW_PHASE' => $alloc_phase, 'a.EVENT_TYPE' => $event_type])->SELECT(DB::raw('IF(IFNULL(a.EU_DATA_' . $alloc_attr . ',0)>0,a.EU_DATA_' . $alloc_attr . ',t.EU_DATA_' . $alloc_attr . ') AS FIXED_VALUE', 'a.EU_ID AS OBJECT_ID', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR'))->get(); //\Log::info ( \DB::getQueryLog () ); } else { if ($obj_type_to == $OBJ_TYPE_TANK) { //\DB::enableQueryLog (); $rows = DB::table('TANK_DATA_VALUE AS a')->join('TANK AS b', 'a.TANK_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.TANK_ID', $arrfixed)->SELECT('a.TANK_ID AS OBJECT_ID', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR', 'TANK_' . $alloc_attr . ' AS FIXED_VALUE')->get(); //\Log::info ( \DB::getQueryLog () ); } else { if ($obj_type_to == $OBJ_TYPE_STORAGE) { //\DB::enableQueryLog (); $rows = DB::table('STORAGE_DATA_VALUE AS a')->join('STORAGE AS b', 'a.STORAGE_ID', '=', 'b.ID')->whereDate('a.OCCUR_DATE', '>=', $from_date)->whereDate('a.OCCUR_DATE', '<=', $to_date)->whereIn('a.STORAGE_ID', $arrfixed)->SELECT('a.STORAGE_ID AS OBJECT_ID', 'a.ACTIVE_HRS', 'a.OCCUR_DATE', 'a.OCCUR_DATE AS OCCUR_DATE_STR', 'STORAGE_' . $alloc_attr . ' AS FIXED_VALUE')->get(); //\Log::info ( \DB::getQueryLog () ); } } } } $this->_log("Create allocation data from fixed objects (id: {$ids_fixed}):", 2); $total_fixed = 0; foreach ($rows as $row) { $v_to = $row->FIXED_VALUE; $total_fixed += $v_to; if ($obj_type_to == $OBJ_TYPE_FLOW) { $ro = FlowDataAlloc::where(['FLOW_ID' => $row->OBJECT_ID])->whereDate('OCCUR_DATE', '=', $row->OCCUR_DATE)->select('ID')->first(); if (count($ro) > 0) { if ($_REQUEST["act"] == "run") { FlowDataAlloc::where(['ID' => $ro->ID])->update(['FL_DATA_' . $alloc_attr => $v_to]); } $sSQL = "update FLOW_DATA_ALLOC set FL_DATA_" . $alloc_attr . "='" . $v_to . "' where ID=" . $ro->ID; } else { if ($_REQUEST["act"] == "run") { FlowDataAlloc::insert(['FLOW_ID' => $row->ID, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FL_DATA_' . $alloc_attr => $v_to]); } $sSQL = "insert into FLOW_DATA_ALLOC(`FLOW_ID`,`OCCUR_DATE`,FL_DATA_" . $alloc_attr . ") values('" . $row->OBJECT_ID . "','" . $row->OCCUR_DATE . "'," . $v_to . ")"; } $this->_log($sSQL, 2); } else { if ($obj_type_to == $OBJ_TYPE_EU) { $ro = EnergyUnitDataAlloc::where(['EU_ID' => $row->ID, 'FLOW_PHASE' => $alloc_phase, 'EVENT_TYPE' => $event_type, 'ALLOC_TYPE' => $alloc_type])->whereDate('OCCUR_DATE', '=', $row->OCCUR_DATE)->select('ID')->first(); if (count($ro) > 0) { if ($_REQUEST["act"] == "run") { EnergyUnitDataAlloc::where(['ID' => $ro->ID])->update(['EU_DATA_' . $alloc_attr_eu => $v_to]); } $sSQL = "update ENERGY_UNIT_DATA_ALLOC set EU_DATA_" . $alloc_attr_eu . "='" . $v_to . "' where ID=" . $ro->ID; } else { if ($_REQUEST["act"] == "run") { $EnergyUnitDataAlloc = EnergyUnitDataAlloc::insert(['EU_ID' => $row->OBJECT_ID, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => $alloc_phase, 'EVENT_TYPE' => $event_type, 'ALLOC_TYPE' => $alloc_type, 'EU_DATA_' . $alloc_attr_eu => $v_to]); } $sSQL = "insert into ENERGY_UNIT_DATA_ALLOC(`EU_ID`,`OCCUR_DATE`,FLOW_PHASE,EVENT_TYPE,ALLOC_TYPE,EU_DATA_" . $alloc_attr_eu . ") values('" . $row->OBJECT_ID . "','" . $row->OCCUR_DATE . "'," . $alloc_phase . "," . $event_type . "," . $alloc_type . "," . $v_to . ")"; } $this->_log($sSQL, 2); } else { if ($obj_type_to == $OBJ_TYPE_TANK) { $ro = TankDataAlloc::where(['TANK_ID' => $row->OBJECT_ID])->whereDate('OCCUR_DATE', '=', $row->OCCUR_DATE)->select('ID')->first(); if (count($ro) > 0) { if ($_REQUEST["act"] == "run") { TankDataAlloc::where(['ID' => $ro->ID])->update(['TANK_' . $alloc_attr => $v_to]); } $sSQL = "update TANK_DATA_ALLOC set TANK_DATA_" . $alloc_attr . "='" . $v_to . "' where ID=" . $ro->ID; } else { if ($_REQUEST["act"] == "run") { TankDataAlloc::insert(['TANK_ID' => $row->OBJECT_ID, 'OCCUR_DATE' => $row->OCCUR_DATE, 'TANK_' . $alloc_attr => $v_to]); } $sSQL = "insert into TANK_DATA_ALLOC(`TANK_ID`,`OCCUR_DATE`,TANK_" . $alloc_attr . ") values('" . $row->OBJECT_ID . "','" . $row->OCCUR_DATE . "'," . $v_to . ")"; } $this->_log($sSQL, 2); } else { if ($obj_type_to == $OBJ_TYPE_STORAGE) { $ro = StorageDataAlloc::where(['STORAGE_ID' => $row->OBJECT_ID])->whereDate('OCCUR_DATE', '=', $row->OCCUR_DATE)->select('ID')->first(); if (count($ro) > 0) { if ($_REQUEST["act"] == "run") { StorageDataAlloc::where(['ID' => $ro->ID])->update(['STORAGE_' . $alloc_attr => $v_to]); } $sSQL = "update STORAGE_DATA_ALLOC set STORAGE_" . $alloc_attr . "='" . $v_to . "' where ID=" . $ro->ID; } else { if ($_REQUEST["act"] == "run") { StorageDataAlloc::insert(['STORAGE_ID' => $row->OBJECT_ID, 'OCCUR_DATE' => $row->OCCUR_DATE, 'STORAGE_' . $alloc_attr => $v_to]); } $sSQL = "insert into STORAGE_DATA_ALLOC(`STORAGE_ID`,`OCCUR_DATE`,STORAGE_" . $alloc_attr . ") values('" . $row->OBJECT_ID . "','" . $row->OCCUR_DATE . "'," . $v_to . ")"; } $this->_log($sSQL, 2); } } } } $this->_log("total_fixed (std value ~ theor): {$total_fixed}", 2); $total_from -= $total_fixed; $this->_log("total_from (minus total_fixed): {$total_from}", 2); } // Alloc if ($total_to == 0) { $ret = $this->_log("total_to is zero, can not calculate", 1); if ($ret === false) { return false; } } foreach ($sSQL_alloc as $row) { if ($row->ALLOC_THEOR === '' || $row->ALLOC_THEOR == null) { $row->ALLOC_THEOR = 0; } $v_to = $total_from * $row->ALLOC_THEOR / $total_to; if ($obj_type_to == $OBJ_TYPE_FLOW) { $ro = FlowDataAlloc::where(['FLOW_ID' => $row->OBJECT_ID])->whereDate('OCCUR_DATE', '=', $row->OCCUR_DATE)->select('ID')->first(); if (count($ro) > 0) { if ($_REQUEST["act"] == "run") { FlowDataAlloc::where(['ID' => $ro->ID])->update(['FL_DATA_' . $alloc_attr => $v_to]); } $sSQL = "update FLOW_DATA_ALLOC set FL_DATA_" . $alloc_attr . "='" . $v_to . "' where ID=" . $ro->ID; } else { if ($_REQUEST["act"] == "run") { FlowDataAlloc::insert(['FLOW_ID' => $row->OBJECT_ID, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FL_DATA_' . $alloc_attr => $v_to]); } $sSQL = "insert into FLOW_DATA_ALLOC(`FLOW_ID`,`OCCUR_DATE`,FL_DATA_" . $alloc_attr . ") values('" . $row->OBJECT_ID . "','" . $row->OCCUR_DATE . "'," . $v_to . ")"; } $this->_log($sSQL, 2); // ////// Flow COST_INT_CTR allocation if ($_REQUEST["act"] == "run") { FlowCoEntDataAlloc::where(['FLOW_ID' => $row->OBJECT_ID])->delete(); $sSQL = "delete from FLOW_CO_ENT_DATA_ALLOC where FLOW_ID=" . $row->OBJECT_ID; $this->_log($sSQL, 2); } $re_co = DB::table('FLOW AS a')->join('COST_INT_CTR_DETAIL AS b', 'a.COST_INT_CTR_ID', '=', 'b.COST_INT_CTR_ID')->where(['a.ID' => $row->OBJECT_ID, 'b.FLOW_PHASE' => $alloc_phase])->get(['a.COST_INT_CTR_ID', 'b.BA_ID', 'b.INTEREST_PCT AS ALLOC_PERCENT']); foreach ($re_co as $ro_co) { $v_co = $v_to * $ro_co->ALLOC_PERCENT / 100; if ($_REQUEST["act"] == "run") { FlowCoEntDataAlloc::insert(['FLOW_ID' => $row->OBJECT_ID, 'OCCUR_DATE' => $row->OCCUR_DATE, 'COST_INT_CTR_ID' => $ro_co->COST_INT_CTR_ID, 'BA_ID' => $ro_co->BA_ID, 'FL_DATA_' . $alloc_attr => $v_co]); } $sSQL = "insert into FLOW_CO_ENT_DATA_ALLOC(`FLOW_ID`,`OCCUR_DATE`,COST_INT_CTR_ID,BA_ID,FL_DATA_" . $alloc_attr . ") values('" . $row->OBJECT_ID . "','" . $row->OCCUR_DATE . "','" . $ro_co->COST_INT_CTR_ID . "','" . $ro_co->BA_ID . "'," . $v_co . ")"; $this->_log($sSQL, 2); } // /////// END of Flow COST_INT_CTR allocation } else { if ($obj_type_to == $OBJ_TYPE_EU) { $ro = EnergyUnitDataAlloc::where(['FLOW_PHASE' => $alloc_phase, 'EVENT_TYPE' => $event_type, 'ALLOC_TYPE' => $alloc_type])->whereDate('OCCUR_DATE', '=', $row->OCCUR_DATE)->select('ID')->first(); if (count($ro) > 0) { if ($_REQUEST["act"] == "run") { EnergyUnitDataAlloc::where(['ID' => $ro->ID])->update(['EU_DATA_' . $alloc_attr_eu => $v_to]); } $sSQL = "update ENERGY_UNIT_DATA_ALLOC set EU_DATA_" . $alloc_attr_eu . "='" . $v_to . "' where ID=" . $ro->ID; } else { if ($_REQUEST["act"] == "run") { EnergyUnitDataAlloc::insert(['EU_ID' => $row->OBJECT_ID, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => $alloc_phase, 'EVENT_TYPE' => $event_type, 'ALLOC_TYPE' => $alloc_type, 'EU_DATA_' . $alloc_attr_eu => $v_to]); } $sSQL = "insert into ENERGY_UNIT_DATA_ALLOC(`EU_ID`,`OCCUR_DATE`,FLOW_PHASE,EVENT_TYPE,ALLOC_TYPE,EU_DATA_" . $alloc_attr_eu . ") values('" . $row->OBJECT_ID . "','" . $row->OCCUR_DATE . "'," . $alloc_phase . "," . $event_type . ",'" . $alloc_type . "'," . $v_to . ")"; } $this->_log($sSQL, 2); if ($_REQUEST["act"] == "run") { EnergyUnitCoEntDataAlloc::where(['EU_ID' => $row->OBJECT_ID])->delete(); $sSQL = "delete from ENERGY_UNIT_CO_ENT_DATA_ALLOC where EU_ID=" . $row->OBJECT_ID; $this->_log($sSQL, 2); } // ////// Well COST_INT_CTR allocation $re_co = DB::table('ENERGY_UNIT AS a')->join('COST_INT_CTR_DETAIL AS b', 'a.COST_INT_CTR_ID', '=', 'b.COST_INT_CTR_ID')->where(['a.ID' => $row->OBJECT_ID, 'b.FLOW_PHASE' => $alloc_phase])->get(['a.COST_INT_CTR_ID', 'b.BA_ID', 'b.INTEREST_PCT AS ALLOC_PERCENT']); foreach ($re_co as $ro_co) { $v_co = $v_to * $ro_co->ALLOC_PERCENT / 100; if ($_REQUEST["act"] == "run") { EnergyUnitCoEntDataAlloc::insert(['EU_ID' => $row->OBJECT_ID, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => $alloc_phase, 'EVENT_TYPE' => $event_type, 'ALLOC_TYPE' => $alloc_type, 'COST_INT_CTR_ID' => $ro_co->COST_INT_CTR_ID, 'BA_ID' => $ro_co->BA_ID, 'EU_DATA_' . $alloc_attr_eu => $v_to]); } $sSQL = "insert into ENERGY_UNIT_CO_ENT_DATA_ALLOC(`EU_ID`,`OCCUR_DATE`,FLOW_PHASE,EVENT_TYPE,ALLOC_TYPE,COST_INT_CTR_ID,BA_ID,EU_DATA_" . $alloc_attr_eu . ") values('" . $row->OBJECT_ID . "','" . $row->OCCUR_DATE . "'," . $alloc_phase . "," . $event_type . ",'" . $alloc_type . "','" . $ro_co->COST_INT_CTR_ID . "','" . $ro_co->BA_ID . "'," . $v_co . ")"; $this->_log($sSQL, 2); } // /////// END of Well COST_INT_CTR allocation // completion, interval if ($alloc_attr == "GRS_VOL" || $alloc_attr == "GRS_MASS") { $this->allocWellCompletion($row->OBJECT_ID, $row->OCCUR_DATE, $alloc_phase, $event_type, $alloc_attr, $v_to); } } else { if ($obj_type_to == $OBJ_TYPE_TANK) { $ro = TankDataAlloc::where(['TANK_ID' => $row->OBJECT_ID])->whereDate('OCCUR_DATE', '=', $row->OCCUR_DATE)->select('ID')->first(); if (count($ro) > 0) { if ($_REQUEST["act"] == "run") { TankDataAlloc::where(['ID' => $ro->ID])->update(['TANK_' . $alloc_attr => $v_to]); } $sSQL = "update TANK_DATA_ALLOC set TANK_" . $alloc_attr . "='" . $v_to . "' where ID=" . $ro->ID; } else { if ($_REQUEST["act"] == "run") { TankDataAlloc::insert(['TANK_ID' => $row->OBJECT_ID, 'OCCUR_DATE' => $row->OCCUR_DATE, 'TANK_' . $alloc_attr => $v_to]); } $sSQL = "insert into TANK_DATA_ALLOC(`TANK_ID`,`OCCUR_DATE`,TANK_" . $alloc_attr . ") values('" . $row->OBJECT_ID . "','" . $row->OCCUR_DATE . "'," . $v_to . ")"; } $this->_log($sSQL, 2); } else { if ($obj_type_to == $OBJ_TYPE_STORAGE) { $ro = StorageDataAlloc::where(['STORAGE_ID' => $row->OBJECT_ID])->whereDate('OCCUR_DATE', '=', $row->OCCUR_DATE)->select('ID')->first(); if (count($ro) > 0) { if ($_REQUEST["act"] == "run") { StorageDataAlloc::where(['ID' => $ro->ID])->update(['STORAGE_' . $alloc_attr => $v_to]); } $sSQL = "update STORAGE_DATA_ALLOC set STORAGE_" . $alloc_attr . "='" . $v_to . "' where ID=" . $ro->ID; } else { if ($_REQUEST["act"] == "run") { StorageDataAlloc::insert(['STORAGE_ID' => $row->OBJECT_ID, 'OCCUR_DATE' => $row->OCCUR_DATE, 'STORAGE_' . $alloc_attr => $v_to]); } $sSQL = "insert into STORAGE_DATA_ALLOC(`STORAGE_ID`,`OCCUR_DATE`,STORAGE_" . $alloc_attr . ") values('" . $row->OBJECT_ID . "','" . $row->OCCUR_DATE . "'," . $v_to . ")"; } $this->_log($sSQL, 2); } } } } } // Composition if ($alloc_comp) { $this->_log("Begin composition allocation", 2); $comp_sqls = array(); $obj_type_code = $obj_type_from == 1 ? "FLOW" : "WELL"; $comp_total_from = array("C1" => 0, "C2" => 0, "C3" => 0, "C4I" => 0, "C4N" => 0, "C5I" => 0, "C5N" => 0, "C6" => 0, "C7" => 0, "H2S" => 0, "CO2" => 0, "N2" => 0); $comp_total_to = array("C1" => 0, "C2" => 0, "C3" => 0, "C4I" => 0, "C4N" => 0, "C5I" => 0, "C5N" => 0, "C6" => 0, "C7" => 0, "H2S" => 0, "CO2" => 0, "N2" => 0); $comp_total_rate = array("C1" => 0, "C2" => 0, "C3" => 0, "C4I" => 0, "C4N" => 0, "C5I" => 0, "C5N" => 0, "C6" => 0, "C7" => 0, "H2S" => 0, "CO2" => 0, "N2" => 0); // step 1: calculate composition for all "from" object foreach ($sSQL_alloc_from as $row) { $this->_log("Calculate composition _FROM, object_name: " . $row->OBJECT_NAME . ",date" . $row->OCCUR_DATE_STR . ",2"); $object_id = $row->OBJECT_ID; $occur_date = $row->OCCUR_DATE_STR; $quality_from = $this->getQualityGas($object_id, $obj_type_code, $occur_date, $F); if ($quality_from) { foreach ($comp_total_from as $x => $x_value) { $comp_total_from[$x] += $row->ALLOC_VALUE * $quality_from[$x]; } if ($obj_type_from == $OBJ_TYPE_FLOW) { if ($success && $_REQUEST["act"] == "run") { FlowCompDataAlloc::where(['FLOW_ID' => $object_id])->whereDate('OCCUR_DATE', '=', $row->OCCUR_DATE)->delete(); $sSQL = "delete from FLOW_COMP_DATA_ALLOC where FLOW_ID={$object_id} and OCCUR_DATE='{$row['OCCUR_DATE']}'"; $this->_log($sSQL, 2); $param = []; array_push($param, ['FLOW_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'COMPOSITION' => $quality_from->ID_C1, 'FL_DATA_' . $alloc_attr => $row->ALLOC_VALUE * $quality_from->C1]); array_push($param, ['FLOW_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'COMPOSITION' => $quality_from->ID_C2, 'FL_DATA_' . $alloc_attr => $row->ALLOC_VALUE * $quality_from->C2]); array_push($param, ['FLOW_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'COMPOSITION' => $quality_from->ID_C3, 'FL_DATA_' . $alloc_attr => $row->ALLOC_VALUE * $quality_from->C3]); array_push($param, ['FLOW_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'COMPOSITION' => $quality_from->ID_C4I, 'FL_DATA_' . $alloc_attr => $row->ALLOC_VALUE * $quality_from->C4I]); array_push($param, ['FLOW_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'COMPOSITION' => $quality_from->ID_C4N, 'FL_DATA_' . $alloc_attr => $row->ALLOC_VALUE * $quality_from->C4N]); array_push($param, ['FLOW_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'COMPOSITION' => $quality_from->ID_C5I, 'FL_DATA_' . $alloc_attr => $row->ALLOC_VALUE * $quality_from->C5I]); array_push($param, ['FLOW_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'COMPOSITION' => $quality_from->ID_C5N, 'FL_DATA_' . $alloc_attr => $row->ALLOC_VALUE * $quality_from->C5N]); array_push($param, ['FLOW_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'COMPOSITION' => $quality_from->ID_C6, 'FL_DATA_' . $alloc_attr => $row->ALLOC_VALUE * $quality_from->C6]); array_push($param, ['FLOW_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'COMPOSITION' => $quality_from->ID_C7, 'FL_DATA_' . $alloc_attr => $row->ALLOC_VALUE * $quality_from->C7]); array_push($param, ['FLOW_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'COMPOSITION' => $quality_from->ID_H2S, 'FL_DATA_' . $alloc_attr => $row->ALLOC_VALUE * $quality_from->H2S]); array_push($param, ['FLOW_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'COMPOSITION' => $quality_from->ID_CO2, 'FL_DATA_' . $alloc_attr => $row->ALLOC_VALUE * $quality_from->CO2]); array_push($param, ['FLOW_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'COMPOSITION' => $quality_from->ID_N2, 'FL_DATA_' . $alloc_attr => $row->ALLOC_VALUE * $quality_from->N2]); $sSQL = "insert into FLOW_COMP_DATA_ALLOC(FLOW_ID,OCCUR_DATE,COMPOSITION,FL_DATA_{$alloc_attr}) VALUES "; foreach ($param as $pa) { FlowCompDataAlloc::insert($pa); $sSQL .= $pa['FLOW_ID'] . "," . $pa['OCCUR_DATE'] . "," . $pa['COMPOSITION'] . "," . $pa['FL_DATA_' . $alloc_attr] . "\n"; } $sSQL .= ")"; $this->_log($sSQL, 2); } } else { if ($success && $_REQUEST["act"] == "run") { EnergyUnitCompDataAlloc::where(['EU_ID' => $object_id, 'FLOW_PHASE' => 2])->whereDate('OCCUR_DATE', '=', $row->OCCUR_DATE)->delete(); $sSQL = "delete from FLOW_COMP_DATA_ALLOC where FLOW_ID={$object_id} and OCCUR_DATE='{$row['OCCUR_DATE']}'"; $this->_log($sSQL, 2); $param = []; array_push($param, ['EU_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => 2, 'ALLOC_TYPE' => $alloc_type, 'COMPOSITION' => $quality_from->ID_C1, 'EU_DATA_' . $alloc_attr_eu => $row->ALLOC_VALUE * $quality_from->C1]); array_push($param, ['EU_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => 2, 'ALLOC_TYPE' => $alloc_type, 'COMPOSITION' => $quality_from->ID_C2, 'EU_DATA_' . $alloc_attr_eu => $row->ALLOC_VALUE * $quality_from->C2]); array_push($param, ['EU_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => 2, 'ALLOC_TYPE' => $alloc_type, 'COMPOSITION' => $quality_from->ID_C3, 'EU_DATA_' . $alloc_attr_eu => $row->ALLOC_VALUE * $quality_from->C3]); array_push($param, ['EU_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => 2, 'ALLOC_TYPE' => $alloc_type, 'COMPOSITION' => $quality_from->ID_C4I, 'EU_DATA_' . $alloc_attr_eu => $row->ALLOC_VALUE * $quality_from->C4I]); array_push($param, ['EU_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => 2, 'ALLOC_TYPE' => $alloc_type, 'COMPOSITION' => $quality_from->ID_C4N, 'EU_DATA_' . $alloc_attr_eu => $row->ALLOC_VALUE * $quality_from->C4N]); array_push($param, ['EU_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => 2, 'ALLOC_TYPE' => $alloc_type, 'COMPOSITION' => $quality_from->ID_C5I, 'EU_DATA_' . $alloc_attr_eu => $row->ALLOC_VALUE * $quality_from->C5I]); array_push($param, ['EU_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => 2, 'ALLOC_TYPE' => $alloc_type, 'COMPOSITION' => $quality_from->ID_C5N, 'EU_DATA_' . $alloc_attr_eu => $row->ALLOC_VALUE * $quality_from->C5N]); array_push($param, ['EU_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => 2, 'ALLOC_TYPE' => $alloc_type, 'COMPOSITION' => $quality_from->ID_C6, 'EU_DATA_' . $alloc_attr_eu => $row->ALLOC_VALUE * $quality_from->C6]); array_push($param, ['EU_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => 2, 'ALLOC_TYPE' => $alloc_type, 'COMPOSITION' => $quality_from->ID_C7, 'EU_DATA_' . $alloc_attr_eu => $row->ALLOC_VALUE * $quality_from->C7]); array_push($param, ['EU_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => 2, 'ALLOC_TYPE' => $alloc_type, 'COMPOSITION' => $quality_from->ID_H2S, 'EU_DATA_' . $alloc_attr_eu => $row->ALLOC_VALUE * $quality_from->H2S]); array_push($param, ['EU_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => 2, 'ALLOC_TYPE' => $alloc_type, 'COMPOSITION' => $quality_from->ID_CO2, 'EU_DATA_' . $alloc_attr_eu => $row->ALLOC_VALUE * $quality_from->CO2]); array_push($param, ['EU_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => 2, 'ALLOC_TYPE' => $alloc_type, 'COMPOSITION' => $quality_from->ID_N2, 'EU_DATA_' . $alloc_attr_eu => $row->ALLOC_VALUE * $quality_from->N2]); $sSQL = "insert into ENERGY_UNIT_COMP_DATA_ALLOC(EU_ID,OCCUR_DATE,FLOW_PHASE,ALLOC_TYPE,COMPOSITION,EU_DATA_{$alloc_attr_eu}) VALUES "; foreach ($param as $pa) { EnergyUnitCompDataAlloc::insert($pa); $sSQL .= $pa['EU_ID'] . "," . $pa['OCCUR_DATE'] . "," . $pa['FLOW_PHASE'] . "," . $pa['ALLOC_TYPE'] . "," . $pa['COMPOSITION'] . "," . $pa['EU_DATA_' . $alloc_attr_eu] . "\n"; } $sSQL .= ")"; $this->_log($sSQL, 2); } } } else { $ret = $this->_log("Quality data not found (_FROM object_id: {$object_id}, object_name: {$row->OBJECT_NAME}, date {$row->OCCUR_DATE_STR})", 1); if ($ret === false) { return false; } } } // step2: $this->_log("Calculate composition allocation rates", 2); $obj_type_code = $obj_type_to == 1 ? "FLOW" : "WELL"; foreach ($sSQL_alloc_to as $row) { $object_id = $row->OBJECT_ID; $occur_date = $row->OCCUR_DATE_STR; $quality_to = getQualityGas($object_id, $obj_type_code, $occur_date, $F); if ($quality_to) { foreach ($comp_total_to as $x => $x_value) { $comp_total_to[$x] += $row->ALLOC_VALUE * $quality_to[$x]; } } else { $ret = $this->_log("Quality data not found (_TO object_id: {$object_id}, object_name: {$row->OBJECT_NAME}, date {$row->OCCUR_DATE_STR})", 1); if ($ret === false) { return false; } } } if ($success) { foreach ($comp_total_to as $x => $x_value) { if ($comp_total_to[$x] == 0) { $comp_total_rate[$x] = -1; } else { $comp_total_rate[$x] = $comp_total_from[$x] / $comp_total_to[$x]; } $this->_log("[{$x}] comp_total_from = {$comp_total_from[$x]}, comp_total_to = {$comp_total_to[$x]}, rate = {$comp_total_rate[$x]}", 2); } $result = AllocRunnerObjects::where(['RUNNER_ID' => $runner_id])->get(); foreach ($result as $row) { $this->_log("Calculate composition allocation _TO, object_name: {$row->OBJECT_NAME}, date {$row->OCCUR_DATE_STR}", 2); $object_id = $row->OBJECT_ID; $occur_date = $row->OCCUR_DATE_STR; $quality_from = $this->getQualityGas($object_id, $obj_type_code, $occur_date, $F); if ($quality_from && $_REQUEST["act"] == "run") { if ($obj_type_to == $OBJ_TYPE_FLOW) { FlowCompDataAlloc::where(['FLOW_ID' => $object_id])->whereDate('OCCUR_DATE', '=', $row->OCCUR_DATE)->delete(); $sSQL = "delete from FLOW_COMP_DATA_ALLOC where FLOW_ID={$object_id} and OCCUR_DATE='{$row['OCCUR_DATE']}'"; $this->_log($sSQL, 2); $sSQL = "insert into FLOW_COMP_DATA_ALLOC(FLOW_ID,OCCUR_DATE,COMPOSITION,FL_DATA_{$alloc_attr}) VALUES ("; foreach ($comp_total_rate as $x => $x_value) { if ($x_value > 0 && $row->ALLOC_VALUE > 0 && $quality_from[$x] > 0) { $_v = $x_value * $row->ALLOC_VALUE * $quality_from[$x]; } else { $_v = 0; } FlowCompDataAlloc::insert(['FLOW_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'COMPOSITION' => $quality_from["ID_{$x}"], 'FL_DATA_' . $alloc_attr => $_v]); $sSQL .= $object_id . "," . $row->OCCUR_DATE . "," . $quality_from["ID_{$x}"] . "," . $_v . "\n"; } $sSQL .= ")"; $this->_log($sSQL, 2); } else { EnergyUnitCompDataAlloc::where(['EU_ID' => $object_id, 'FLOW_PHASE' => 2])->whereDate('OCCUR_DATE', '=', $row->OCCUR_DATE)->delete(); $sSQL = "delete from FLOW_COMP_DATA_ALLOC where FLOW_ID={$object_id} and OCCUR_DATE='{$row['OCCUR_DATE']}'"; $this->_log($sSQL, 2); $sSQL = "insert into ENERGY_UNIT_COMP_DATA_ALLOC(EU_ID,OCCUR_DATE,FLOW_PHASE,ALLOC_TYPE,COMPOSITION,EU_DATA_{$alloc_attr_eu}) VALUES ("; foreach ($comp_total_rate as $x => $x_value) { if ($x_value > 0 && $row->ALLOC_VALUE > 0 && $quality_from[$x] > 0) { $_v = $x_value * $row->ALLOC_VALUE * $quality_from[$x]; } else { $_v = 0; } EnergyUnitCompDataAlloc::insert(['EU_ID' => $object_id, 'OCCUR_DATE' => $row->OCCUR_DATE, 'FLOW_PHASE' => $alloc_phase, 'ALLOC_TYPE' => $alloc_type, 'COMPOSITION' => $quality_from["ID_{$x}"], 'EU_DATA_' . $alloc_attr_eu => $_v]); $sSQL .= $object_id . "," . $row->OCCUR_DATE . "," . $alloc_phase . "," . "," . $alloc_type . "," . $quality_from["ID_{$x}"] . "," . $_v . "\n"; } $sSQL .= ")"; $this->_log($sSQL, 2); } } else { $ret = $this->_log("Quality data not found (_TO object_id: {$object_id}, object_name: {$row['OBJECT_NAME']}, date {$row['OCCUR_DATE_STR']})", 1); if ($ret === false) { return false; } } } if ($success) { $this->_log("Execute SQL composition allocation commands", 2); /* * foreach($comp_sqls as $sSQL) * { * if($_REQUEST["act"]=="run") mysql_query($sSQL) or die("fail: ".$sSQL."-> error:".mysql_error()); * _log($sSQL); * } */ } } } $this->_log("End runner ID: {$runner_id} -------------------------------------------------------", 2); return $success; } }