diff --git a/app/Models/Tracks.php b/app/Models/Tracks.php index 8eb7b15..49507e9 100755 --- a/app/Models/Tracks.php +++ b/app/Models/Tracks.php @@ -305,6 +305,163 @@ class Tracks extends Model // // em // return $list; // } + public static function listCurrentTracks($filter = []) + { + $now = time(); + $params = []; + + $query = "SELECT + v.id as vid, v.device_id, v.name as vhc_name, c.name as vhc_cat_name, t.name as vhc_type_name, + v.nopol1, v.nopol2, v.nopol3, vd.fvhc_img, + v.is_track_holiday, v.track_sch_d, v.track_sch_h, vd.speed_limit, v.crt as vhc_crt, + client.id as client_group_id, client.c_name as client_group_name, + tr.ignition, tr.stts_engine, tr.stts_gps, tr.stts_gsm, + tr.pre_milleage, tr.sum_milleage, tr.vhc_milleage, v.sum_milleage AS vhc_sum_milleage_1, + tr.id AS lst_master_id, tr.latitude AS lst_lat, tr.longitude AS lst_lng, + tr.speed AS lst_speed, tr.orientation AS lst_orientation, + tr.crt AS lst_loc_crt, tr.crt_d AS lst_loc_crt_d, tr.crt_s AS lst_loc_crt_s, + tr_addr.master_id AS lst_addr_master_id, tr_addr.country_text AS lst_country_text, + tr_addr.state_text AS lst_state_text, tr_addr.city_text AS lst_city_text, + tr_addr.district_text AS lst_district_text, tr_addr.village_text AS lst_village_text, + tr_addr.postcode AS lst_postcode, tr_addr.streets AS lst_streets, + tr_addr.fulladdress AS lst_fulladdress"; + + if (isset($filter["active_rates"])) { + $query .= ", + rate.vdr_id as rate_vdr_id, rate.vhc_type as rate_vhc_type, + rate.origin_prov as rate_origin_prov, rate.dest_city as rate_dest_city, + rate.dest_district as rate_dest_district, rate.fast_time as rate_fast_time, + rate.long_time as rate_long_time, rate.sell_kg as rate_sell_kg, + rate.sell_cbm as rate_sell_cbm, rate.sell_ftl as rate_sell_ftl"; + } + + if (isset($filter["get_order_data"])) { + $query .= ", + ord.id as ord_id, ord.code as ord_code, ord.status as ord_stts, ord.crt as ord_crt, + ord_pck.pck_name as ord_pck_name, ord_pck.pck_addr as ord_pck_addr, + ord_drop.drop_name as ord_drop_name, ord_drop.drop_addr as ord_drop_addr, + (SELECT nmKotamadyaKel FROM t_region WHERE kodeKab = ord_pck.pck_ktid LIMIT 1) AS ord_pck_ktname, + (SELECT nmKotamadyaKel FROM t_region WHERE kodeKab = ord_drop.drop_ktid LIMIT 1) AS ord_drop_ktname, + ord_drv.drv_name as ord_drv_name, ord_drv.drv_phone_val as ord_drv_phone_val, + ord_drv.drv_phone2_val as ord_drv_phone2_val, ord_drv.drv_addr as ord_drv_addr, + ord_c.c_name as ord_c_name, ord_c.c_pt_name as ord_c_pt_name"; + } + + $query .= " FROM t_vehicles AS v + INNER JOIN t_vehicles_detail AS vd ON v.id = vd.vid + INNER JOIN t_vehicles_types AS t ON v.type_id = t.id + INNER JOIN t_vehicles_cats AS c ON v.cat_id = c.id + LEFT JOIN t_users AS vendor ON v.vendor_id = vendor.id + LEFT JOIN t_clients AS client ON vendor.client_group_id = client.id + LEFT JOIN t_gps_tracks_rltm AS tr ON tr.vhc_id = v.id + LEFT JOIN " . self::T_TRACKS_ADDR . " AS tr_addr ON tr.latitude = tr_addr.lat AND tr.longitude = tr_addr.lng"; + + if (isset($filter["active_rates"])) { + $query .= " INNER JOIN t_conf_rates as rate ON v.vendor_id = rate.vdr_id"; + } + + if (isset($filter["get_order_data"])) { + $query .= " + LEFT JOIN t_orders as ord ON v.ord_id = ord.id + LEFT JOIN t_orders_pickups as ord_pck ON v.ord_id = ord_pck.ord_id + LEFT JOIN t_orders_drops as ord_drop ON v.ord_id = ord_drop.ord_id + LEFT JOIN t_orders_drivers as ord_drv ON v.ord_id = ord_drv.ord_id + LEFT JOIN t_orders_clients as ord_c ON v.ord_id = ord_c.ord_id"; + } + + $query .= " WHERE v.dlt IS NULL AND tr.latitude != 0 AND tr.latitude IS NOT NULL"; + + if (isset($filter["status"])) { + $query .= " AND v.status = ?"; + $params[] = $filter["status"]; + } + if (isset($filter["is_in_ord"])) { + $query .= " AND v.is_in_ord = ?"; + $params[] = $filter["is_in_ord"]; + } + if (isset($filter["nopol1"], $filter["nopol2"], $filter["nopol3"])) { + $query .= " AND v.nopol1 = ? AND v.nopol2 = ? AND v.nopol3 = ?"; + array_push($params, $filter["nopol1"], $filter["nopol2"], $filter["nopol3"]); + } + if (isset($filter["vid"])) { + $query .= " AND v.id = ?"; + $params[] = $filter["vid"]; + } + if (isset($filter["vids"])) { + if ($filter["vids"] && count($filter["vids"]) > 0) { + $placeholders = rtrim(str_repeat("?,", count($filter["vids"])), ","); + $query .= " AND v.id IN ($placeholders)"; + $params = array_merge($params, $filter["vids"]); + } else { + $query .= " AND v.id = ?"; + $params[] = 0; + } + } + if (isset($filter["own_by_vdr_id"])) { + $query .= " AND v.vendor_id = ?"; + $params[] = $filter["own_by_vdr_id"]; + } + if (isset($filter["active_rates"])) { + $query .= " AND rate.vdr_id != 0 AND rate.dest_city != 0 AND rate.is_active = " . ConfRates::IS_ACTIVE; + $query .= " AND rate.origin_prov = ? AND (rate.dest_city = ? OR rate.dest_district = ?) AND rate.sell_ftl = ? AND rate.long_time = ?"; + array_push( + $params, + $filter["active_rates"]->origin_prov, + $filter["active_rates"]->dest_city, + $filter["active_rates"]->dest_district, + $filter["active_rates"]->sell_ftl, + $filter["active_rates"]->long_time + ); + } + if (isset($filter["prefer_truck_type"])) { + $query .= " AND v.type_id = ?"; + $params[] = $filter["prefer_truck_type"]; + } + if (isset($filter["get_order_data"])) { + if (isset($filter["client_id"])) { + $query .= " AND ord_c.c_id = ?"; + $params[] = $filter["client_id"]; + } + } + if (isset($filter["company"])) { + $query .= " AND client.id = ?"; + $params[] = $filter["company"]; + } + + $query .= " GROUP BY v.id ORDER BY tr.crt_d DESC LIMIT 500"; + + $list = DB::select($query, $params); + + // Post-processing per row + foreach ($list as $_list) { + $_list->vhc_sum_milleage = optional(DB::select( + "SELECT SUM(pre_milleage) as vhc_sum_milleage FROM " . self::T_TRACKS . " WHERE vhc_id = ?", + [$_list->vid] + ))[0]->vhc_sum_milleage ?? ""; + + $_list->lst_idle_at = optional(DB::select( + "SELECT crt_s FROM " . self::T_TRACKS . " WHERE stts_engine = ? AND vhc_id = ? AND crt_s >= ( + SELECT crt_s FROM " . self::T_TRACKS . " WHERE stts_engine IN (?, ?) AND vhc_id = ? AND crt_s <= ? ORDER BY id DESC LIMIT 1 + ) ORDER BY id ASC LIMIT 1", + [self::STTS_EN_IDLING, $_list->vid, self::STTS_EN_STOPING, self::STTS_EN_MOVING, $_list->vid, $_list->lst_loc_crt_s] + ))[0]->crt_s ?? ""; + + $_list->lst_stop_at = optional(DB::select( + "SELECT crt_s FROM " . self::T_TRACKS . " WHERE stts_engine = ? AND vhc_id = ? AND crt_s >= ( + SELECT crt_s FROM " . self::T_TRACKS . " WHERE stts_engine IN (?, ?) AND vhc_id = ? AND crt_s <= ? ORDER BY id DESC LIMIT 1 + ) ORDER BY id ASC LIMIT 1", + [self::STTS_EN_STOPING, $_list->vid, self::STTS_EN_IDLING, self::STTS_EN_MOVING, $_list->vid, $_list->lst_loc_crt_s] + ))[0]->crt_s ?? ""; + + $_list->lst_heartbeat = optional(DB::select( + "SELECT COUNT(id) as lst_heartbeat FROM " . self::T_TRACKS . " WHERE vhc_id = ? AND action = 'heartbeat' AND crt BETWEEN ? AND ? LIMIT 1", + [$_list->vid, $now - 600, $now] + ))[0]->lst_heartbeat ?? ""; + } + + return $list; + } + // public static function listCurrentTracks($filter = []) // { @@ -396,56 +553,56 @@ class Tracks extends Model // return $list; // } - public static function listCurrentTracks($filter = []) - { - $now = time(); - $params = []; + // public static function listCurrentTracks($filter = []) + // { + // $now = time(); + // $params = []; - $list = DB::select( - " - SELECT - v.id as vid, v.device_id, v.name as vhc_name, - c.name as vhc_cat_name, t.name as vhc_type_name, - v.nopol1, v.nopol2, v.nopol3, vd.fvhc_img, - v.is_track_holiday, v.track_sch_d, v.track_sch_h, - vd.speed_limit, v.crt as vhc_crt, - client.id as client_group_id, client.c_name as client_group_name, - tr.ignition, tr.stts_engine, tr.stts_gps, tr.stts_gsm, - tr.pre_milleage, tr.sum_milleage, tr.vhc_milleage, v.sum_milleage AS vhc_sum_milleage_1, - tr.id AS lst_master_id, tr.latitude AS lst_lat, tr.longitude AS lst_lng, - tr.speed AS lst_speed, tr.orientation AS lst_orientation, - tr.crt AS lst_loc_crt, tr.crt_d AS lst_loc_crt_d, tr.crt_s AS lst_loc_crt_s, - tr_addr.master_id AS lst_addr_master_id, - tr_addr.country_text AS lst_country_text, tr_addr.state_text AS lst_state_text, - tr_addr.city_text AS lst_city_text, tr_addr.district_text AS lst_district_text, - tr_addr.village_text AS lst_village_text, tr_addr.postcode AS lst_postcode, - tr_addr.streets AS lst_streets, tr_addr.fulladdress AS lst_fulladdress - FROM t_vehicles AS v - INNER JOIN t_vehicles_detail AS vd ON v.id = vd.vid - INNER JOIN t_vehicles_types AS t ON v.type_id = t.id - INNER JOIN t_vehicles_cats AS c ON v.cat_id = c.id - LEFT JOIN t_users AS vendor ON v.vendor_id = vendor.id - LEFT JOIN t_clients AS client ON vendor.client_group_id = client.id - LEFT JOIN t_gps_tracks_rltm AS tr ON tr.vhc_id = v.id - LEFT JOIN t_gps_tracks_address AS tr_addr ON tr.latitude = tr_addr.lat AND tr.longitude = tr_addr.lng - WHERE v.dlt IS NULL - AND tr.latitude != 0 AND tr.latitude IS NOT NULL - GROUP BY v.id - ORDER BY tr.crt_d DESC - LIMIT 20 - ", - $params - ); + // $list = DB::select( + // " + // SELECT + // v.id as vid, v.device_id, v.name as vhc_name, + // c.name as vhc_cat_name, t.name as vhc_type_name, + // v.nopol1, v.nopol2, v.nopol3, vd.fvhc_img, + // v.is_track_holiday, v.track_sch_d, v.track_sch_h, + // vd.speed_limit, v.crt as vhc_crt, + // client.id as client_group_id, client.c_name as client_group_name, + // tr.ignition, tr.stts_engine, tr.stts_gps, tr.stts_gsm, + // tr.pre_milleage, tr.sum_milleage, tr.vhc_milleage, v.sum_milleage AS vhc_sum_milleage_1, + // tr.id AS lst_master_id, tr.latitude AS lst_lat, tr.longitude AS lst_lng, + // tr.speed AS lst_speed, tr.orientation AS lst_orientation, + // tr.crt AS lst_loc_crt, tr.crt_d AS lst_loc_crt_d, tr.crt_s AS lst_loc_crt_s, + // tr_addr.master_id AS lst_addr_master_id, + // tr_addr.country_text AS lst_country_text, tr_addr.state_text AS lst_state_text, + // tr_addr.city_text AS lst_city_text, tr_addr.district_text AS lst_district_text, + // tr_addr.village_text AS lst_village_text, tr_addr.postcode AS lst_postcode, + // tr_addr.streets AS lst_streets, tr_addr.fulladdress AS lst_fulladdress + // FROM t_vehicles AS v + // INNER JOIN t_vehicles_detail AS vd ON v.id = vd.vid + // INNER JOIN t_vehicles_types AS t ON v.type_id = t.id + // INNER JOIN t_vehicles_cats AS c ON v.cat_id = c.id + // LEFT JOIN t_users AS vendor ON v.vendor_id = vendor.id + // LEFT JOIN t_clients AS client ON vendor.client_group_id = client.id + // LEFT JOIN t_gps_tracks_rltm AS tr ON tr.vhc_id = v.id + // LEFT JOIN t_gps_tracks_address AS tr_addr ON tr.latitude = tr_addr.lat AND tr.longitude = tr_addr.lng + // WHERE v.dlt IS NULL + // AND tr.latitude != 0 AND tr.latitude IS NOT NULL + // GROUP BY v.id + // ORDER BY tr.crt_d DESC + // LIMIT 20 + // ", + // $params + // ); - foreach ($list as $_list) { - $_list->vhc_sum_milleage = self::getVehicleMileage($_list->vid); - $_list->lst_idle_at = self::getIdleAt($_list->vid, $_list->lst_loc_crt_s); - $_list->lst_stop_at = self::getStopAt($_list->vid, $_list->lst_loc_crt_s); - $_list->lst_heartbeat = self::getHeartbeat($_list->vid, $now); - } + // foreach ($list as $_list) { + // $_list->vhc_sum_milleage = self::getVehicleMileage($_list->vid); + // $_list->lst_idle_at = self::getIdleAt($_list->vid, $_list->lst_loc_crt_s); + // $_list->lst_stop_at = self::getStopAt($_list->vid, $_list->lst_loc_crt_s); + // $_list->lst_heartbeat = self::getHeartbeat($_list->vid, $now); + // } - return $list; - } + // return $list; + // } private static function getVehicleMileage($vhc_id) {