<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Models\OrdersVendors;
use App\Models\Users;

class Orders extends Model
{
    const ONE_KG_ONE_CBM = 1328;

    const TYPE_FCL = 1;
    const TYPE_LCL = 2;

    const STTS_WAIT = 1;
    const STTS_CONFIRM = 21;
    const STTS_HAVE_GET_VHC = 22;
    const STTS_PCK = 2;
    const STTS_GO = 3;
    const STTS_ARV = 4;
    const STTS_DROP = 5;
    const STTS_CLIENT_PAY = 6;
    const STTS_VENDOR_PAYED = 8;
    const STTS_CLOSE = 10;
    const STTS_CANCEL = 11;

    const IS_FIX_PRICE_NO = 0;
    const IS_FIX_PRICE_YES = 1;

    const CANCEL_TYPE_IGNORE = 1;
    const CANCEL_TYPE_CLIENT = 2;
    const CANCEL_TYPE_VDR_NOT_READY = 3;

    const IS_ACCIDENT_NO = 0;
    const IS_ACCIDENT_YES = 1;

    const CHK_STTS_WAITING = 0;
    const CHK_STTS_HAS_PICKUP = 1;
    const CHK_STTS_HAS_DROP = 3;

    const IS_PAID_NO = 0;
    const IS_PAID_YES = 1;

    const IS_ACTIVE_NO = 0;
    const IS_ACTIVE_YES = 1;

    const CRT_TYPE_ORDER_CLIENT = 1; // normal_via_client;
    const CRT_TYPE_ORDER_ADMIN = 2; // normal_via_admin;
    const CRT_TYPE_ORDER_ADMIN_SPECIAL = 3; // special_via_admin;

    // merge per trx
    const STTS_MERGE_NO = 0;
    const STTS_MERGE_TO = 1;
    const STTS_MERGE_RESULT = 2;

    /**
     * column merge disini untuk merge per trx
     */

    const defaultSelectedOrders = "
    ord.*
    ,ord.id as ord_id,ord.code as ord_code
    ,ord_pck.pck_name,ord_pck.stts_pck,ord_pck.pck_addr
    ,ord_drop.drop_name,ord_drop.stts_drop,ord_drop.drop_addr
    ,ord_c.c_name
    ,ord_c.c_pt_name
    ,ord_vdr.vdr_name
    ,ord_drv.drv_name
    ,ord_vhc.vhc_id,ord_vhc.vhc_name,ord_vhc.vhc_nopol1,ord_vhc.vhc_nopol2,ord_vhc.vhc_nopol3
    ,ord_rate.id as ord_rate_id
    ";
    const defaultWhereOrders = "
    WHERE ord.dlt is null
    ";

    const defaultSelectedOrdersDetail = "
    ord.*
    ,ord.id as ord_id,ord.code as ord_code
    ,ord_pck.id as ord_pck_id,ord_pck.pck_id as ord_pck_zone_id,ord_pck.pck_name,ord_pck.pck_addr,ord_pck.set_pck_at,ord_pck.pck_at,ord_pck.pck_enter_at,ord_pck.pck_leave_at
    ,ord_drop.id as ord_drop_id,ord_drop.drop_id as ord_drop_zone_id,ord_drop.drop_name,ord_drop.drop_addr,ord_drop.drop_at,ord_drop.drop_enter_at,ord_drop.drop_leave_at
    ,ord_c.id as ord_client_id,ord_c.c_id,ord_c.c_name,ord_c.c_phone_code,ord_c.c_phone_val,ord_c.c_mail,ord_c.prefer_truck_type
    ,ord_vdr.id as ord_vdr_id,ord_vdr.vdr_id,ord_vdr.vdr_name,ord_vdr.vdr_phone_code,ord_vdr.vdr_phone_val,ord_vdr.vdr_mail,ord_vdr.vdr_addr,ord_vdr.status as vdr_status,ord_vdr.respond_at as vdr_respond_at,ord_vdr.crt as vdr_crt,ord_vdr.is_pkp as is_vdr_pkp
    ,ord_drv.drv_id,ord_drv.drv_name,ord_drv.drv_phone_code,ord_drv.drv_phone_val,ord_drv.drv_name2,ord_drv.drv_phone2_val,ord_drv.drv_mail,ord_drv.drv_addr
    ,ord_vhc.vhc_id,ord_vhc.vhc_name,ord_vhc.vhc_nopol1,ord_vhc.vhc_nopol2,ord_vhc.vhc_nopol3
    ,ord_rate.id as ord_rate_id,ord_rate.buy_ftl
    ";
    const defaultWhereOrdersDetail = "
    WHERE ord.dlt is null
    ";

    public static function listOrders($filter = [])
    {
        $params = [];
        $select_order = "";
        $join_join = "";
        $where_where = "";
        $group_by = "";
        $order_by = "";

        if (isset($filter["client_id"])) {
            $where_where .= " AND ord_c.c_id = ? ";
            array_push($params, $filter["client_id"]);
        }

        if (isset($filter["client_pt_id"])) {
            $where_where .= " AND ord_c.c_pt_id = ? ";
            array_push($params, $filter["client_pt_id"]);
        }

        if (isset($filter["vendor_id"])) {
            $where_where .= " AND ord_vdr.vdr_id = ? ";
            $where_where .= " AND ord_vdr.status = " . OrdersVendors::STTS_ACC;
            array_push($params, $filter["vendor_id"]);
        }

        if (isset($filter["get_stts_checker"])) {
            $select_order .=
                " ,ord_pck.chk_id as pck_chk_id,ord_pck.chk_at as pck_chk_at,ord_pck.chk_stts as pck_chk_stts";
            $select_order .=
                " ,ord_drop.chk_id as drop_chk_id,ord_drop.chk_at as drop_chk_at,ord_drop.chk_stts as drop_chk_stts";
        }
        if (isset($filter["chk_id"])) {
            if ($filter["chk_type"] == Users::CHK_TYPE_PICKUP) {
                $where_where .= " AND (ord_pck.chk_id = ? OR ord_pck.chk_id = 0) ";
                array_push($params, $filter["chk_id"]);
            } elseif ($filter["chk_type"] == Users::CHK_TYPE_DROP) {
                $where_where .= " AND (ord_drop.chk_id = ? OR ord_drop.chk_id = 0) ";
                array_push($params, $filter["chk_id"]);
            }
        }
        if (isset($filter["order_open"])) {
            $where_where .= " AND ord.status IN (?,?,?,?,?,?,?,?) ";
            array_push(
                $params,
                Orders::STTS_WAIT,
                Orders::STTS_WAIT,
                Orders::STTS_CONFIRM,
                Orders::STTS_HAVE_GET_VHC,
                Orders::STTS_GO,
                Orders::STTS_PCK,
                Orders::STTS_ARV,
                Orders::STTS_DROP
            );
        }

        if (isset($filter["ready_checker"])) {
            $where_where .= " AND ord.status IN (?,?,?,?,?,?,?,?) ";
            array_push(
                $params,
                Orders::STTS_HAVE_GET_VHC,
                Orders::STTS_GO,
                Orders::STTS_PCK,
                Orders::STTS_ARV,
                Orders::STTS_DROP,
                Orders::STTS_CLIENT_PAY,
                Orders::STTS_VENDOR_PAYED,
                Orders::STTS_CLOSE
            );
        }

        if (isset($filter["client_id_active_orders"])) {
            $where_where .= " AND ord_c.c_id = ? AND ord.status IN (?,?,?,?,?) ";
            array_push(
                $params,
                $filter["client_id_active_orders"],
                Orders::STTS_HAVE_GET_VHC,
                Orders::STTS_GO,
                Orders::STTS_PCK,
                Orders::STTS_ARV,
                Orders::STTS_DROP
            );
        }
        if (isset($filter["is_accident"])) {
            $where_where .= " AND ord.is_accident = ?  ";
            array_push($params, $filter["is_accident"]);
        }

        if (isset($filter["couple_pck_drop"])) {
            $select_order .=
                " ,ord_pck_drop.id as ord_pck_drop_id,ord_pck_drop.stts as stts_delivery,ord_pck_drop.is_aprv_pck,ord_pck_drop.aprv_pck_at";
            $join_join .= " INNER JOIN t_orders_pck_drop as ord_pck_drop ON ord.id = ord_pck_drop.ord_id";
            $join_join .= " INNER JOIN t_orders_pickups as ord_pck ON ord_pck_drop.pck_id = ord_pck.id";
            $join_join .= " INNER JOIN t_orders_drops as ord_drop ON ord_pck_drop.drop_id = ord_drop.id";

            if (isset($filter["ord_pck_drop_id"])) {
                $where_where .= " AND ord_pck_drop.id = ?";
                array_push($params, $filter["ord_pck_drop_id"]);
            }
        } else {
            $join_join .= " INNER JOIN t_orders_pickups as ord_pck ON ord.id = ord_pck.ord_id";
            $join_join .= " INNER JOIN t_orders_drops as ord_drop ON ord.id = ord_drop.ord_id";
        }

        if (isset($filter["join_pockets"])) {
            $select_order .=
                " ,ord_pocket.pocket_id,ord_pocket.pocket_type,ord_pocket.id as ord_pocket_id,ord_pocket.pocket_name";
            $join_join .= " INNER JOIN t_orders_pockets as ord_pocket ON ord.id = ord_pocket.ord_id";
        }

        // $group_by .= ' GROUP BY ord.group_code';
        if (isset($filter["group_by"])) {
            $group_by .= " GROUP BY " . $filter["group_by"];
        }
        if (isset($filter["order_by"])) {
            $order_by .= " ORDER BY " . $filter["order_by"];
        }

        return DB::select(
            "SELECT
        " .
                Orders::defaultSelectedOrders .
                "
        " .
                $select_order .
                "
        FROM t_orders as ord
        INNER JOIN t_orders_clients as ord_c ON ord.id = ord_c.ord_id
        LEFT JOIN t_orders_rates as ord_rate ON ord.id = ord_rate.ord_id
        LEFT JOIN ( SELECT MIN(respond_at) as respond_at,ord_id,vdr_id FROM t_orders_vendors WHERE status = " .
                OrdersVendors::STTS_ACC .
                " GROUP BY ord_id ) AS ord_vdr1 ON (ord.id = ord_vdr1.ord_id)
        LEFT JOIN t_orders_vendors as ord_vdr ON ( ord_vdr.respond_at = ord_vdr1.respond_at)
        LEFT JOIN t_orders_drivers as ord_drv ON ord.id = ord_drv.ord_id
        LEFT JOIN t_orders_vehicles as ord_vhc ON ord.id = ord_vhc.ord_id
        " .
                $join_join .
                "
        " .
                Orders::defaultWhereOrders .
                "
        " .
                $where_where .
                "
		" .
                $group_by .
                "
		" .
                $order_by .
                "
        ;",
            $params
        );
    }

    public static function showOrder($filter)
    {
        $params = [];
        $select_order = "";
        $where_order = "";
        $join_order = "";
        $group_by = "";

        if (isset($filter["id"])) {
            $where_order .= " AND ord.id = ? ";
            array_push($params, $filter["id"]);
        } elseif (isset($filter["code"])) {
            $where_order .= " AND ord.code = ? ";
            array_push($params, $filter["code"]);
        } elseif (isset($filter["codes"])) {
            $binds_codes = "";
            foreach ($filter["codes"] as $k => $v) {
                $binds_codes .= "?,";
                $params[] = $v;
            }
            if (substr($binds_codes, -1) === ",") {
                $binds_codes = substr($binds_codes, 0, -1);
            }
            $where_order .= " AND ord.code IN ($binds_codes)";
        } elseif (isset($filter["group_merge_code"])) {
            $where_order .= " AND ord.group_merge_code IN (?)";
            $params[] = $filter["group_merge_code"];
        } else {
            return false;
        }

        if (isset($filter["center_pck"])) {
            $select_order .= ",ST_AsText(ST_Centroid(pck_points)) as pck_center";
        }

        if (isset($filter["get_prefer_type_truck"])) {
            $select_order .= ",tt.name as prefer_truck_type_name";
            $join_order .= " LEFT JOIN t_vehicles_types as tt ON ord_c.prefer_truck_type = tt.id";
        }

        if (isset($filter["get_additional_vehicles_info"])) {
            $select_order .=
                ",ord_vhc.vhc_type_id, tt2.name as vhc_type_name, ord_vhc.vhc_brand_id, tb.name as vhc_brand_name";
            $join_order .= " LEFT JOIN t_vehicles_types as tt2 ON ord_vhc.vhc_type_id = tt2.id";
            $join_order .= " LEFT JOIN t_vehicles_brands as tb ON ord_vhc.vhc_brand_id = tb.id";
        }

        if (isset($filter["get_zone_data"])) {
            $select_order .=
                ",ord_pck.pck_hex_color,ord_pck.pck_shape,ord_pck.pck_radius,ord_pck.pck_bounds,ord_pck.pck_latlngs,ST_AsText(ST_Centroid(pck_points)) as pck_center";
            $select_order .=
                ",ord_drop.drop_hex_color,ord_drop.drop_shape,ord_drop.drop_radius,ord_drop.drop_bounds,ord_drop.drop_latlngs,ST_AsText(ST_Centroid(drop_points)) as drop_center";
        }

        if (isset($filter["get_zone_zonasi"])) {
            $select_order .=
                ",(SELECT nmProvinsiKel FROM t_region WHERE kodeProv = ord_pck.pck_prid LIMIT 1) as pck_prid_name ,(SELECT nmKotamadyaKel FROM t_region WHERE kodeKab = ord_pck.pck_ktid LIMIT 1) as pck_ktid_name";
            $select_order .=
                ",(SELECT nmProvinsiKel FROM t_region WHERE kodeProv = ord_drop.drop_prid LIMIT 1) as drop_prid_name ,(SELECT nmKotamadyaKel FROM t_region WHERE kodeKab = ord_drop.drop_ktid LIMIT 1) as drop_ktid_name";
        }

        if (isset($filter["get_pic_zone"])) {
            $select_order .=
                ",ord_pck.pic_name as pck_pic_name,ord_pck.pic_phone_code as pck_pic_phone_code,ord_pck.pic_phone_val as pck_pic_phone_val,ord_pck.pic_mail as pck_pic_mail";
            $select_order .=
                ",ord_drop.pic_name as drop_pic_name,ord_drop.pic_phone_code as drop_pic_phone_code,ord_drop.pic_phone_val as drop_pic_phone_val,ord_drop.pic_mail as drop_pic_mail";
        }
        if (isset($filter["get_client_pt"])) {
            $select_order .=
                ",ord_c.c_pt_id,ord_c.c_pt_name,ord_c.c_pt_phone_code,ord_c.c_pt_phone_val,ord_c.c_pt_mail,ord_c.c_pt_addr";
            $select_order .=
                ",ord_c.c_pt_pic_name,ord_c.c_pt_pic_phone_code,ord_c.c_pt_pic_phone_val,ord_c.c_pt_pic_mail,ord_c.c_pt_pic_addr";
        }

        if (isset($filter["get_exp_vendor"])) {
            $select_order .= ",ord_vdr.is_exp as vdr_is_exp,ord_vdr.exp_at as vdr_exp_at";
        }
        if (isset($filter["vdr_id"])) {
            $select_order .= ",ord_vdr.find_vhcs";
            $where_order .= " AND ord_vdr.vdr_id = ?";
            array_push($params, $filter["vdr_id"]);
            $join_order .= " LEFT JOIN t_orders_vendors as ord_vdr ON ord.id = ord_vdr.ord_id";
        } else {
            $join_order .=
                "
            LEFT JOIN ( SELECT MIN(respond_at) as respond_at,ord_id,vdr_id FROM t_orders_vendors WHERE status = " .
                OrdersVendors::STTS_ACC .
                " GROUP BY ord_id ) AS ord_vdr1 ON (ord.id = ord_vdr1.ord_id)
            LEFT JOIN t_orders_vendors as ord_vdr ON ( ord_vdr.respond_at = ord_vdr1.respond_at)
            ";
        }

        if (isset($filter["get_drv_bank"])) {
            $select_order .=
                ",ord_drv.drv_bank_id,ord_drv.drv_bank_code,ord_drv.drv_bank_name,ord_drv.drv_bank_short_name,ord_drv.drv_bank_branch_name,ord_drv.drv_bank_acc_number,ord_drv.drv_bank_acc_name";
        }
        if (isset($filter["get_vdr_bank"])) {
            $select_order .=
                ",ord_vdr.vdr_bank_id,ord_vdr.vdr_bank_code,ord_vdr.vdr_bank_name,ord_vdr.vdr_bank_short_name,ord_vdr.vdr_bank_acc_number,ord_vdr.vdr_bank_acc_name";
        }

        if (isset($filter["get_stts_checker"])) {
            $select_order .=
                " ,ord_pck.chk_id as pck_chk_id,ord_pck.chk_at as pck_chk_at,ord_pck.chk_stts as pck_chk_stts";
            $select_order .=
                " ,ord_drop.chk_id as drop_chk_id,ord_drop.chk_at as drop_chk_at,ord_drop.chk_stts as drop_chk_stts";
        }
        if (isset($filter["chk_id"])) {
            if ($filter["chk_type"] == Users::CHK_TYPE_PICKUP) {
                $where_order .= " AND (ord_pck.chk_id = ? OR ord_pck.chk_id = 0) ";
                array_push($params, $filter["chk_id"]);
            } elseif ($filter["chk_type"] == Users::CHK_TYPE_DROP) {
                $where_order .= " AND (ord_drop.chk_id = ? OR ord_drop.chk_id = 0) ";
                array_push($params, $filter["chk_id"]);
            }
        }
        if (isset($filter["get_checker_user"])) {
            $select_order .=
                " ,ord_pck.chk_name as pck_chk_name,ord_pck.chk_mail as pck_chk_mail,ord_pck.chk_phone_val as pck_chk_phone_val";
            $select_order .=
                " ,ord_drop.chk_name as drop_chk_name,ord_drop.chk_mail as drop_chk_mail,ord_drop.chk_phone_val as drop_chk_phone_val";
        }

        if (isset($filter["get_bid_info"])) {
            $select_order .=
                " ,ord_vdr.is_mailing_bid,ord_vdr.is_mailing_bid_at,ord_vdr.is_want,ord_vdr.is_want_at";
        }

        // get img pickup and drop
        if (isset($filter["get_checker_data"])) {
            $select_order .=
                " ,ord_pck.chk_seal_number as ord_pck_seal_number,ord_pck.chk_seal_img as ord_pck_seal_img,ord_pck.chk_drv_armd_img as ord_pck_drv_armd_img,ord_pck.chk_nopol_img as ord_pck_nopol_img,ord_pck.chk_docs_client_img as ord_pck_docs_client_img,ord_pck.chk_seal_install_img as ord_pck_seal_install_img,ord_pck.chk_goods_img as ord_pck_goods_img";
            $select_order .=
                " ,ord_drop.chk_arrived_img as ord_drop_arrived_img,ord_drop.chk_handover_img as ord_drop_handover_img,ord_drop.chk_do_ttd_img as ord_drop_do_ttd_img,ord_drop.chk_spk_img as ord_drop_spk_img";
        }

        if (isset($filter["get_accidents"])) {
            $select_order .=
                ",ord_acdnt.id as ord_acdnt_id,ord_acdnt.accident_location as ord_acdnt_location,ord_acdnt.accident_story as ord_acdnt_story,ord_acdnt.accident_imgs as ord_acdnt_imgs";
            $select_order .=
                ",ord_acdnt.new_nopol1 as ord_acdnt_new_nopol1,ord_acdnt.new_nopol2 as ord_acdnt_new_nopol2,ord_acdnt.new_nopol3 as ord_acdnt_new_nopol3,ord_acdnt.new_vhc_type_id as ord_acdnt_new_vhc_type_id";
            $select_order .= ",tt3.name as ord_acdnt_new_vhc_type_name";
            $select_order .=
                ",ord_acdnt.new_drv_name as ord_acdnt_new_drv_name,ord_acdnt.new_drv_phone_code as ord_acdnt_new_drv_phone_code,ord_acdnt.new_drv_phone_val as ord_acdnt_new_drv_phone_val";
            $select_order .= ",ord_acdnt.crt as ord_acdnt_crt_at,ord_acdnt.updt as ord_acdnt_updt_at";
            $join_order .= " LEFT JOIN t_orders_accidents as ord_acdnt ON ord.id = ord_acdnt.ord_id";
            $join_order .= " LEFT JOIN t_vehicles_types as tt3 ON ord_vhc.vhc_type_id = tt3.id";
        }

        if (isset($filter["select_region_pck_drop"])) {
            $select_order .=
                ",(SELECT nmKotamadyaKel FROM t_region WHERE kodeKab = ord_pck.pck_ktid LIMIT 1) pck_ktname, (SELECT nmKotamadyaKel FROM t_region WHERE kodeKab = ord_drop.drop_ktid LIMIT 1) drop_ktname";
        }

        if (isset($filter["couple_pck_drop"])) {
            $select_order .=
                " ,ord_pck_drop.id as ord_pck_drop_id,ord_pck_drop.stts as stts_delivery,ord_pck_drop.is_aprv_pck,ord_pck_drop.aprv_pck_at";
            $join_order .= " INNER JOIN t_orders_pck_drop as ord_pck_drop ON ord.id = ord_pck_drop.ord_id";
            $join_order .= " INNER JOIN t_orders_pickups as ord_pck ON ord_pck_drop.pck_id = ord_pck.id";
            $join_order .= " INNER JOIN t_orders_drops as ord_drop ON ord_pck_drop.drop_id = ord_drop.id";

            if (isset($filter["ord_pck_drop_id"])) {
                $where_order .= " AND ord_pck_drop.id = ?";
                array_push($params, $filter["ord_pck_drop_id"]);
            }
            if (isset($filter["get_user_aprv_pck"])) {
                $select_order .=
                    " ,(SELECT first_name FROM t_users WHERE id = ord_pck_drop.aprv_pck_by LIMIT 1) as aprv_pck_by_name";
            }
        } else {
            $join_order .= " INNER JOIN t_orders_pickups as ord_pck ON ord.id = ord_pck.ord_id";
            $join_order .= " INNER JOIN t_orders_drops as ord_drop ON ord.id = ord_drop.ord_id";
        }

        if (isset($filter["join_pockets"])) {
            $select_order .=
                " ,ord_pocket.pocket_id,ord_pocket.pocket_type,ord_pocket.id as ord_pocket_id,ord_pocket.pocket_name";
            $join_order .= " INNER JOIN t_orders_pockets as ord_pocket ON ord.id = ord_pocket.ord_id";
        }

        if (isset($filter["group_by"])) {
            $group_by .= " GROUP BY " . $filter["group_by"];
        }

        if (!isset($filter["limit"])) {
            $filter["limit"] = 1;
        }

        return DB::select(
            "SELECT
        " .
                Orders::defaultSelectedOrdersDetail .
                "
        " .
                $select_order .
                "
        FROM t_orders as ord
        INNER JOIN t_orders_clients as ord_c ON ord.id = ord_c.ord_id
        LEFT JOIN t_orders_rates as ord_rate ON ord.id = ord_rate.ord_id
        LEFT JOIN t_orders_drivers as ord_drv ON ord.id = ord_drv.ord_id
        LEFT JOIN t_orders_vehicles as ord_vhc ON ord.id = ord_vhc.ord_id
        " .
                $join_order .
                "
        " .
                Orders::defaultWhereOrdersDetail .
                "
        " .
                $where_order .
                "
        " .
                $group_by .
                "
        LIMIT " .
                $filter["limit"] .
                ";",
            $params
        );
    }

    public static function getOrders()
    {
        return DB::select("SELECT * FROM t_orders WHERE dlt is null;");
    }

    public static function getOrdersClientActive($client_id)
    {
        $stts =
            Orders::STTS_WAIT .
            "," .
            Orders::STTS_CONFIRM .
            "," .
            Orders::STTS_HAVE_GET_VHC .
            "," .
            Orders::STTS_PCK .
            "," .
            Orders::STTS_GO .
            "," .
            Orders::STTS_ARV .
            "," .
            Orders::STTS_DROP;
        return DB::select(
            "SELECT
        ord.*,ord_c.*
        ,ord_c.pay_at as client_pay_at,ord_c.is_pay as client_is_pay
        FROM t_orders as ord
        INNER JOIN t_orders_clients as ord_c ON ord.id = ord_c.ord_id
        WHERE ord.dlt is null AND ord.status IN (?) AND ord_c.c_id = ?;",
            [$stts, $client_id]
        );
    }

    public static function getOrdersByClient($client_id, $limit = 1000)
    {
        return DB::select(
            "SELECT
        ord.*,ord_c.*
        ,ord_c.pay_at as client_pay_at,ord_c.is_pay as client_is_pay
        FROM t_orders as ord
        INNER JOIN t_orders_clients as ord_c ON ord.id = ord_c.ord_id
        WHERE ord.dlt is null AND ord_c.c_id = ?
        LIMIT ?;",
            [$client_id, $limit]
        );
    }

    public static function getOrderById($ordid)
    {
        return DB::select("SELECT * FROM t_orders WHERE dlt is null AND id = ? LIMIT 1;", [$ordid]);
    }

    public static function getOrderByCode($ordcode)
    {
        return DB::select("SELECT * FROM t_orders WHERE dlt is null AND code = ? LIMIT 1;", [$ordcode]);
    }

    // special case utk generate order code tanpa where dlt is null
    public static function getOrderLikeCode($ordcode)
    {
        // return DB::select("SELECT COUNT(id) as total FROM t_orders WHERE dlt is null AND code LIKE ? LIMIT 1;", [$ordcode.'%']);
        return DB::select("SELECT COUNT(id) as total FROM t_orders WHERE code LIKE ? LIMIT 1;", [
            $ordcode . "%",
        ]);
    }

    public static function getPoints($filter)
    {
        $params = [];
        $select = "";
        $join = "";
        $where = "";
        $limit = "";

        if (isset($filter["id"])) {
            $where .= " AND ord.id = ? ";
            array_push($params, $filter["id"]);
        } elseif (isset($filter["code"])) {
            $where .= " AND ord.code = ? ";
            array_push($params, $filter["code"]);
        } else {
            return false;
        }

        if (isset($filter["point_id"])) {
            $where .= " AND topd.id = ? ";
            array_push($params, $filter["point_id"]);
        }

        if (isset($filter["center_pck"])) {
            $select .= ",ST_AsText(ST_Centroid(pck_points)) as pck_center";
        }

        if (isset($filter["get_zone_data"])) {
            $select .=
                ",ord_pck.pck_hex_color,ord_pck.pck_shape,ord_pck.pck_radius,ord_pck.pck_bounds,ord_pck.pck_latlngs,ST_AsText(ST_Centroid(pck_points)) as pck_center";
            $select .=
                ",ord_drop.drop_hex_color,ord_drop.drop_shape,ord_drop.drop_radius,ord_drop.drop_bounds,ord_drop.drop_latlngs,ST_AsText(ST_Centroid(drop_points)) as drop_center";
        }

        if (isset($filter["get_pic_zone"])) {
            $select .=
                ",ord_pck.pic_name as pck_pic_name,ord_pck.pic_phone_code as pck_pic_phone_code,ord_pck.pic_phone_val as pck_pic_phone_val,ord_pck.pic_mail as pck_pic_mail";
            $select .=
                ",ord_drop.pic_name as drop_pic_name,ord_drop.pic_phone_code as drop_pic_phone_code,ord_drop.pic_phone_val as drop_pic_phone_val,ord_drop.pic_mail as drop_pic_mail";
        }

        if (isset($filter["select_region_pck_drop"])) {
            $select .=
                ",(SELECT nmKotamadyaKel FROM t_region WHERE kodeKab = ord_pck.pck_ktid LIMIT 1) pck_ktname, (SELECT nmKotamadyaKel FROM t_region WHERE kodeKab = ord_drop.drop_ktid LIMIT 1) drop_ktname";
        }

        if (isset($filter["limit"])) {
            $limit .= " LIMIT " . $filter["limit"];
        }

        // -- total_pck must be equal to total_drop
        return DB::select(
            "SELECT
        ord.*
        ,ord.id as ord_id,ord.code as ord_code
        ,ord_pck.id as ord_pck_id,ord_pck.pck_id as ord_pck_zone_id,ord_pck.pck_name,ord_pck.pck_addr,ord_pck.set_pck_at,ord_pck.pck_at,ord_pck.pck_enter_at,ord_pck.pck_leave_at
        ,ord_drop.id as ord_drop_id,ord_drop.drop_id as ord_drop_zone_id,ord_drop.drop_name,ord_drop.drop_addr,ord_drop.drop_at,ord_drop.drop_enter_at,ord_drop.drop_leave_at
        ,topd.id as point_id
        " .
                $select .
                "
        FROM t_orders as ord
        LEFT JOIN t_orders_pck_drop topd ON
            topd.ord_id = ord.id
        LEFT JOIN t_orders_pickups as ord_pck ON
            topd.pck_id = ord_pck.id
        LEFT JOIN t_orders_drops as ord_drop ON
            topd.drop_id = ord_drop.id
        " .
                $join .
                "
        " .
                Orders::defaultWhereOrdersDetail .
                "
        " .
                $where .
                "
        " .
                $limit .
                "
        ",
            $params
        );
    }

    public static function addOrder($data)
    {
        $ordid = DB::table("t_orders")->insertGetId($data);
        return $ordid;
    }

    public static function updateOrder($ordid, $data)
    {
        return DB::table("t_orders")
            ->where("id", $ordid)
            ->update($data);
    }

    public static function deleteOrder($ordid)
    {
        return DB::table("t_orders")
            ->where("id", $ordid)
            ->delete();
    }
}
