where("id", Auth::user()->id)
            ->get();
        $vehicles = DB::table("t_vehicles as tv")
            ->leftJoin("t_users as tu", "tu.id", "tv.vendor_id")
            ->where("tu.id", Auth::user()->id)
            ->where("tv.status", 1)
            ->whereNull("tv.dlt")
            ->select("tv.*")
            ->groupBy("tv.vendor_id")
            ->get();
        $drivers = DB::table("t_drivers as td")
            ->leftJoin("t_users as tu", "tu.id", "td.vendor_id")
            ->where("tu.id", Auth::user()->id)
            ->where("td.status", 1)
            ->where("td.is_in_ord", 2)
            ->whereNull("td.dlt")
            ->select("td.*")
            ->get();
        // select * from t_zones tz where tz.client_group_id = 21 and tz.workflow_type = 1 and tz.status = 1
        $pickupzone = DB::table("t_zones as tz") //
            ->where("tz.client_group_id", Auth::user()->client_id)
            ->where("tz.workflow_type", 1)
            ->where("tz.status", 1)
            ->whereNull("tz.dlt")
            ->get();
        // dd($pickupzone);
        $dropzone = DB::table("t_zones as tz") //
            ->where("tz.client_group_id", Auth::user()->client_id)
            ->where("tz.workflow_type", 2)
            ->where("tz.status", 1)
            ->whereNull("tz.dlt")
            ->get();
        $checklist = DB::table("t_conf_checklists")
            ->where("status", 1)
            ->whereNull("dlt")
            ->get();
        $data = [
            "uclients" => Users::listUsers([
                "role" => Users::ROLE_VENDOR,
                "status" => Users::STATUS_ACTIVE,
                "id" => Auth::user()->id,
            ]),
            "truck_types" => ConfTruckTypes::listTruckTypes(ConfTruckTypes::IS_ACTIVE, [
                "is_publish" => ConfTruckTypes::IS_PUBLISH,
            ]),
            "devices" => Devices::listDevices([
                "is_active" => Devices::IS_ACTIVE,
                "type" => Devices::TYPE_PORTABLE,
                "is_idle_yes" => 1,
                "is_available" => Devices::IS_AVAIL,
            ]),
            "vendors" => $vendors,
            "vehicles" => $vehicles,
            "drivers" => $drivers,
            "pickup_zone" => $pickupzone,
            "drop_zone" => $dropzone,
            "checklist" => $checklist,
            "banks" => Banks::listBanks(["is_active" => Banks::IS_ACTIVE]),
        ];
        // dd($data);
        return view("menu_v1._addTransactionsSpcMultiple", $data);
        // return view('menu_v1._addTransactionsSpcSingle', $data);
    }
    /**
     * CASE SEMI AUTOMATIC
     * Vendor
     *      phone belum digunakan, email digunakan ? AMAN
     *      phone digunakan, email belum digunakan ? AMAN
     *      phone digunakan, emaill digunakan ? AMAN
     * Driver
     *      driver lock on phone1 ? AMAN
     *      driver in order ? AMAN
     *      driver phone1 used but from current input its different name driver ? AMAN
     *      driver ditemukan tapi beda vendor ?
     * Vehicle
     *      device_id used on vhc and nopol same ?
     *      device_id used on vhc but nopol not same ? AMAN
     *      device_id not used on any vhc and nopol not used ? AMAN
     *      device_id not used on any vhc but nopol used ? AMAN
     *      vehilce ditemukan tapi beda vendor ?
     * Rates
     *      rates yang sama ? AMAN
     * Other
     *      kir_exp / tax_exp not input on DB when new vhc AMAN
     *      show suggestion beluman
     */
    //	multiple
    public function api_trx_spc_checkout_v1(Request $req)
    {
        $vhs = DB::table("t_vehicles as tv")
            ->leftJoin("t_vehicles_types as tvt", "tvt.id", "tv.type_id")
            ->leftJoin("t_vehicles_detail as tvd", "tvd.vid", "tv.id")
            ->where("tv.id", $req->id_kendaraan)
            ->select(
                "tv.*",
                "tvt.id as truck_type_id",
                "tvt.name as truck_type_name",
                "tvd.tax_exp as vhc_stnk_taxexp",
                "tvd.kir_exp as vhc_kir_exp"
            )
            ->first();
        $drv = DB::table("t_drivers as td")
            ->leftJoin("t_drivers_detail as tdd", "tdd.did", "td.id")
            ->where("td.id", $req->id_pengemudi)
            ->first();
        // $points = $req->add_pickup_date;
        $vdr = DB::table("t_users as tu")
            ->leftJoin("t_clients as tc", "tc.id", "tu.client_id")
            ->where("tu.id", Auth::user()->id)
            ->first();
        // dd($req->all(), $vdr, $vhs, $drv, $points);
        // dd($req->all());
        try {
            $now = time();
            $input = [
                "uid" => $req->add_client,
                // "points" => $req->points,
                // "points" => [1],
                "truck_type_id" => $vhs->truck_type_id,
                "truck_type_name" => $vhs->truck_type_name,
                "is_insurance" => "NO",
                "is_client_pkp" => 0,
                "amt_beneficiary" => 0,
                "vdr_name" => $vdr->c_name,
                "vdr_phone" => "+" . $vdr->c_phone_code . $vdr->c_phone,
                "vdr_email" => $vdr->c_mail,
                "vdr_addr" => $vdr->c_addr_office,
                "vdr_bank_id" => 0,
                "vdr_bank_code" => 0,
                "vdr_bank_short" => "vdr_bank_short",
                "vdr_bank_name" => "vdr_bank_name",
                "vdr_kcp" => "vdr_kcp",
                "vdr_bank_acc_number" => 0,
                "vdr_bank_acc_name" => "vdr_bank_acc_name",
                "is_vdr_bcng" => 0,
                "device_id" => $vhs->device_id,
                "vhc_nopol" => $vhs->nopol1 . " " . $vhs->nopol2 . " " . $vhs->nopol3,
                "vhc_stnk_taxexp" => $vhs->vhc_stnk_taxexp,
                "vhc_kir_exp" => $vhs->vhc_kir_exp,
                "drv_name" => $drv->fullname,
                "drv_phone" => "+" . $drv->phone_code . $drv->phone,
                "drv_phone2" => "+" . $drv->phone2_code . $drv->phone2,
                "drv_bank_id" => $drv->bank_id,
                "drv_bank_code" => $drv->bank_code,
                "drv_bank_name" => $drv->bank_name,
                "drv_kcp" => $drv->bank_branch_name,
                "drv_bank_acc_number" => $drv->bank_acc_number,
                "drv_bank_acc_name" => $drv->bank_acc_name,
                "sell_price" => 0,
                "buy_price" => 0,
                "buy_price_tax_type" => 0,
                "buy_price_tax_ppn_percent" => 0,
                "buy_price_tax_ppn_flat" => 0,
                "buy_price_tax_pph_percent" => 0,
                "buy_price_tax_pph_flat" => 0,
                "buy_price_total_tax" => 0,
                "margin_price" => 0,
                "percent_price" => 0,
                "lead_time" => 1,
                "termin1_amt_type" => 0,
                "termin1_amt" => 0,
                "termin1_calc_percent" => 0,
                "termin1_date" => "$now",
                "termin1_time" => "$now",
                "termin1_at" => $now,
                "termin2_amt_type" => $now,
                "termin2_amt" => $now,
                "termin2_calc_percent" => $now,
                "sell_termin1_amt_type" => $now,
                "sell_termin1_amt" => $now,
                "sell_termin1_calc_percent" => $now,
                "sell_termin1_date" => "$now",
                "sell_termin1_time" => "$now",
                "sell_termin1_at" => $now,
                "sell_termin2_amt_type" => 0,
                "sell_termin2_amt" => 0,
                "sell_termin2_calc_percent" => 0,
                // "pocket_id" => $req->pocket_id,
                // "pocket_type" => $req->pocket_type,
                // "pocket_checkpoints" => $req->pocket_checkpoints,
            ];
            $rulesInput = [
                "uid" => "required|integer|not_in:0",
                // "points" => "required|array|min:1",
                // "points.*.pickup_at" => "required|integer",
                // "points.*.pickup_zone_id" => "required|integer",
                // "points.*.pickup_zone_pic_name" => "required|string",
                // "points.*.pickup_zone_pic_phone" => "required|numeric",
                // "points.*.drop_zone_id" => "required|integer",
                // "points.*.drop_zone_pic_name" => "required|string",
                // "points.*.drop_zone_pic_phone" => "required|numeric",
                "truck_type_id" => "required|integer|not_in:0",
                "truck_type_name" => "required|string",
                "is_insurance" => "nullable|string",
                "is_client_pkp" => "nullable|numeric",
                "amt_beneficiary" => "nullable|numeric",
                "vdr_name" => "required|string",
                "vdr_phone" => "required|numeric",
                "vdr_email" => "required|email",
                "vdr_addr" => "required|string|min:35",
                "vdr_bank_id" => "required|integer",
                "vdr_bank_code" => "required|numeric",
                "vdr_bank_short" => "required|string",
                "vdr_bank_name" => "required|string",
                "vdr_kcp" => "required|string",
                "vdr_bank_acc_number" => "required|numeric",
                "vdr_bank_acc_name" => "required|string|max:255",
                "is_vdr_bcng" => "nullable|numeric",
                "device_id" => "nullable|numeric",
                "vhc_nopol" => "required|string",
                "vhc_stnk_taxexp" => "required|date_format:Y-m-d",
                "vhc_kir_exp" => "required|date_format:Y-m-d",
                "drv_name" => "required|string",
                "drv_phone" => "required|numeric",
                "drv_phone2" => "nullable|numeric",
                "drv_bank_id" => "nullable|numeric",
                "drv_bank_code" => "nullable|string",
                "drv_bank_name" => "nullable|string",
                "drv_kcp" => "nullable|string",
                "drv_bank_short_name" => "nullable|string",
                "drv_bank_acc_number" => "nullable|string",
                "drv_bank_acc_name" => "nullable|string",
                "sell_price" => "required|numeric",
                "buy_price" => "required|numeric",
                "buy_price_tax_type" => "required|numeric",
                "buy_price_tax_ppn_percent" => "required|numeric",
                "buy_price_tax_ppn_flat" => "required|numeric",
                "buy_price_tax_pph_percent" => "required|numeric",
                "buy_price_tax_pph_flat" => "required|numeric",
                "buy_price_total_tax" => "required|numeric",
                "margin_price" => "required|numeric",
                "lead_time" => "required|integer|not_in:0",
                "termin1_amt_type" => "nullable|numeric",
                "termin1_amt" => "nullable|numeric",
                "termin1_calc_percent" => "nullable|numeric",
                "termin1_date" => "nullable|string",
                "termin1_time" => "nullable|string",
                "termin1_at" => "nullable|numeric",
                "termin2_amt_type" => "nullable|numeric",
                "termin2_amt" => "nullable|numeric",
                "termin2_calc_percent" => "nullable|numeric",
                "sell_termin1_amt_type" => "nullable|numeric",
                "sell_termin1_amt" => "nullable|numeric",
                "sell_termin1_calc_percent" => "nullable|numeric",
                "sell_termin1_date" => "nullable|string",
                "sell_termin1_time" => "nullable|string",
                "sell_termin1_at" => "nullable|numeric",
                "sell_termin2_amt_type" => "nullable|numeric",
                "sell_termin2_amt" => "nullable|numeric",
                "sell_termin2_calc_percent" => "nullable|numeric",
                // "pocket_id" => "nullable|integer",
                // "pocket_type" => "nullable|numeric",
                // "pocket_checkpoints" => "nullable|array",
            ];
            // $req->margin_price = 0;
            // validasi input
            $isValidInput = Validator::make($input, $rulesInput);
            if (!$isValidInput->passes()) {
                $apiResp = Responses::bad_input($isValidInput->messages()->first());
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }
            $pckZids = [];
            foreach ($req->add_pickup_zone as $i => $point) {
                $pckZids[] = $point;
            }
            $dropZids = [];
            foreach ($req->add_drop_zone as $i => $point) {
                $dropZids[] = $point;
            }
            // dd($pckZids);
            $pcks = Zone::getZoneByIds($pckZids);
            // dd($pcks);
            if (count($pcks) < 1) {
                $apiResp = Responses::bad_request("Lokasi penjemputan tidak ditemukan");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }
            foreach ($pcks as $i => $pck) {
                if ($pck->prid == 0 || $pck->ktid == 0 || $pck->kcid == 0 || $pck->klid == 0) {
                    $apiResp = Responses::bad_request(
                        "Zonasi wilayah(provinsi, kecamatan, ...) lokasi penjemputan " .
                            $pck->name .
                            " belum lengkap"
                    );
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
            }
            $drops = Zone::getZoneByIds($dropZids);
            if (count($drops) < 1) {
                $apiResp = Responses::bad_request("Lokasi pengantaran");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }
            foreach ($drops as $i => $drop) {
                if ($drop->prid == 0 || $drop->ktid == 0 || $drop->kcid == 0 || $drop->klid == 0) {
                    $apiResp = Responses::bad_request(
                        "Zonasi wilayah(provinsi, kecamatan, ...) lokasi pengantaran " .
                            $drop->name .
                            " belum lengkap"
                    );
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
            }
            $client = Clients::getClientById($req->auth->client_group_id);
            $insurance = StaticInsurances::listStaticInsurances([
                "is_active" => StaticInsurances::IS_ACTIVE,
            ]);
            $opts = [];
            $opt = [
                "is_best" => 1,
            ];
            $opt["lead_time_id"] = 0;
            // $center_time = $v->long_time - $v->fast_time;
            $opt["lead_time"] = $req->lead_time; // long_time
            $price = $req->sell_price ?? 0;
            $opt["price"] = $price;
            $opt["real_price"] = $price;
            $opt["disc_price"] = 0;
            $opt["disc_type"] = $client[0]->disc_type;
            $opt["disc_percent"] = 0;
            if ($client[0]->disc_type === Clients::DISC_TYPE_FIX) {
                $opt["disc_price"] = $client[0]->disc_amount;
                $opt["price"] = $opt["price"] - $opt["disc_price"];
            } elseif ($client[0]->disc_type === Clients::DISC_TYPE_PERCENT) {
                $opt["disc_percent"] = $client[0]->disc_amount;
                $opt["disc_price"] = ($opt["price"] * $client[0]->disc_amount) / 100;
                $opt["price"] = $opt["price"] - $opt["disc_price"];
            }
            array_push($opts, $opt);
            $apiResp = Responses::success("checkout success");
            $apiResp["data"] = $opts;
            if ($req->is_insurance === "true") {
                // $insurances = Insurances::getInsurancesByRangeBeneficiaryAndActive($req->amt_beneficiary);
                // if (count($insurances) < 1) {
                // 	$apiResp = Responses::bad_request('Total pertangguhan tidak tersedia');
                // 	return new Response($apiResp, $apiResp['meta']['code']);
                // }
                // $apiResp['insurance'] = $insurances[0];
                $beneficiary = $req->amt_beneficiary ?? 0; // nilai pertangguhan
                $calcPercent = ($beneficiary * $insurance[0]->amt_percent) / 100;
                $flatPercent = $beneficiary - $calcPercent;
                $apiResp["insurance"] = [
                    "premi_price" => $calcPercent,
                ];
            }
            // $apiResp["exist_data"] = [
            //     "vdr_id" => $existVdrId,
            //     "drv_id" => $existDrvId,
            //     "vhc_id" => $existVhcId,
            // ];
            // dd($apiResp);
            return new Response($apiResp, $apiResp["meta"]["code"]);
        } catch (\Exception $e) {
            $apiResp = Responses::error($e->getMessage());
            return new Response($apiResp, $apiResp["meta"]["code"]);
        }
    }
    //	multiple
    /**
     * DONE jangan lupa insert t_pocket, t_checkpoint
     * DONE tambahan field t_vendors => is_vdr_bcng
     * DONE tambahin field t_zones => boundary_diameter, boundary_area, boundary_ha
     * tambahin field t_drivers => bank driver
     */
    public function api_trx_spc_create_order_v1(Request $req)
    {
        $vhs = DB::table("t_vehicles as tv")
            ->leftJoin("t_vehicles_types as tvt", "tvt.id", "tv.type_id")
            ->leftJoin("t_vehicles_detail as tvd", "tvd.vid", "tv.id")
            ->where("tv.id", $req->id_kendaraan)
            ->select(
                "tv.*",
                "tvt.id as truck_type_id",
                "tvt.name as truck_type_name",
                "tvd.tax_exp as vhc_stnk_taxexp",
                "tvd.kir_exp as vhc_kir_exp"
            )
            ->first();
        $drv = DB::table("t_drivers as td")
            ->leftJoin("t_drivers_detail as tdd", "tdd.did", "td.id")
            ->where("td.id", $req->id_pengemudi)
            ->select("td.*", "tdd.*", "td.id as id")
            ->first();
        $vdr = DB::table("t_clients as tc")
            ->leftJoin("t_users as tu", "tc.id", "tu.client_id")
            ->where("tu.id", Auth::user()->id)
            ->first();
        try {
            $now = time();
            $input = [
                "uid" => $req->add_client,
                // "points" => $req->points,
                // "points" => [1],
                "truck_type_id" => $vhs->truck_type_id,
                "truck_type_name" => $vhs->truck_type_name,
                "is_insurance" => "NO",
                "is_client_pkp" => 0,
                "amt_beneficiary" => 0,
                "vdr_id" => $vdr->id,
                "vdr_name" => $vdr->c_name,
                "vdr_phone" => "+" . $vdr->c_phone_code . $vdr->c_phone,
                "vdr_email" => $vdr->c_mail,
                "vdr_addr" => $vdr->c_addr_office,
                "vdr_bank_id" => 0,
                "vdr_bank_code" => 0,
                "vdr_bank_short" => "vdr_bank_short",
                "vdr_bank_name" => "vdr_bank_name",
                "vdr_kcp" => "vdr_kcp",
                "vdr_bank_acc_number" => 0,
                "vdr_bank_acc_name" => "vdr_bank_acc_name",
                "is_vdr_bcng" => 0,
                "device_id" => $vhs->device_id,
                "vhc_id" => $vhs->id,
                "vhc_nopol" => $vhs->nopol1 . " " . $vhs->nopol2 . " " . $vhs->nopol3,
                "vhc_stnk_taxexp" => $vhs->vhc_stnk_taxexp,
                "vhc_kir_exp" => $vhs->vhc_kir_exp,
                "drv_id" => $drv->id,
                "drv_name" => $drv->fullname,
                "drv_phone" => "+" . $drv->phone_code . $drv->phone,
                "drv_phone2" => "+" . $drv->phone2_code . $drv->phone2,
                "drv_bank_id" => $drv->bank_id,
                "drv_bank_code" => $drv->bank_code,
                "drv_bank_name" => $drv->bank_name,
                "drv_kcp" => $drv->bank_branch_name,
                "drv_bank_acc_number" => $drv->bank_acc_number,
                "drv_bank_acc_name" => $drv->bank_acc_name,
                "sell_price" => 0,
                "buy_price" => 0,
                "buy_price_tax_type" => 0,
                "buy_price_tax_ppn_percent" => 0,
                "buy_price_tax_ppn_flat" => 0,
                "buy_price_tax_pph_percent" => 0,
                "buy_price_tax_pph_flat" => 0,
                "buy_price_total_tax" => 0,
                "margin_price" => 0,
                "percent_price" => 0,
                "lead_time" => 1,
                "termin1_amt_type" => 0,
                "termin1_amt" => 0,
                "termin1_calc_percent" => 0,
                "termin1_date" => "$now",
                "termin1_time" => "$now",
                "termin1_at" => $now,
                "termin2_amt_type" => $now,
                "termin2_amt" => $now,
                "termin2_calc_percent" => $now,
                "sell_termin1_amt_type" => $now,
                "sell_termin1_amt" => $now,
                "sell_termin1_calc_percent" => $now,
                "sell_termin1_date" => "$now",
                "sell_termin1_time" => "$now",
                "sell_termin1_at" => $now,
                "sell_termin2_amt_type" => 0,
                "sell_termin2_amt" => 0,
                "sell_termin2_calc_percent" => 0,
                // "pocket_id" => $req->pocket_id,
                // "pocket_type" => $req->pocket_type,
                // "pocket_checkpoints" => $req->pocket_checkpoints,
            ];
            $rulesInput = [
                "uid" => "required|integer|not_in:0",
                // "points" => "required|array|min:1",
                // "points.*.pickup_at" => "required|integer",
                // "points.*.pickup_zone_id" => "required|integer",
                // "points.*.pickup_zone_pic_name" => "required|string",
                // "points.*.pickup_zone_pic_phone" => "required|numeric",
                // "points.*.drop_zone_id" => "required|integer",
                // "points.*.drop_zone_pic_name" => "required|string",
                // "points.*.drop_zone_pic_phone" => "required|numeric",
                "truck_type_id" => "required|integer|not_in:0",
                "truck_type_name" => "required|string",
                "is_insurance" => "nullable|string",
                "is_client_pkp" => "nullable|numeric",
                "amt_beneficiary" => "nullable|numeric",
                "vdr_id" => "required|numeric",
                "vdr_name" => "required|string",
                "vdr_phone" => "required|numeric",
                "vdr_email" => "required|email",
                "vdr_addr" => "required|string|min:35",
                // "vdr_bank_id" => "required|integer|not_in:0",
                // "vdr_bank_code" => "required|numeric",
                // "vdr_bank_short" => "required|string",
                // "vdr_bank_name" => "required|string",
                // "vdr_kcp" => "required|string",
                // "vdr_bank_acc_number" => "required|numeric",
                // "vdr_bank_acc_name" => "required|string|max:255",
                "is_vdr_bcng" => "nullable|numeric",
                "device_id" => "nullable|numeric",
                "vhc_id" => "required|numeric",
                "vhc_nopol" => "required|string",
                "vhc_stnk_taxexp" => "required|date_format:Y-m-d",
                "vhc_kir_exp" => "required|date_format:Y-m-d",
                "drv_id" => "required|numeric",
                "drv_name" => "required|string",
                "drv_phone" => "required|numeric",
                "drv_phone2" => "nullable|numeric",
                "drv_bank_id" => "nullable|numeric",
                "drv_bank_code" => "nullable|string",
                "drv_bank_name" => "nullable|string",
                "drv_kcp" => "nullable|string",
                "drv_bank_short_name" => "nullable|string",
                "drv_bank_acc_number" => "nullable|string",
                "drv_bank_acc_name" => "nullable|string",
                "sell_price" => "required|numeric",
                "buy_price" => "required|numeric",
                "buy_price_tax_type" => "required|numeric",
                "buy_price_tax_ppn_percent" => "required|numeric",
                "buy_price_tax_ppn_flat" => "required|numeric",
                "buy_price_tax_pph_percent" => "required|numeric",
                "buy_price_tax_pph_flat" => "required|numeric",
                "buy_price_total_tax" => "required|numeric",
                "margin_price" => "required|numeric",
                // 'margin_price' => 'required',
                // 'percent_price' => 'required|numeric',
                "lead_time" => "required|integer|not_in:0",
                "termin1_amt_type" => "nullable|numeric",
                "termin1_amt" => "nullable|numeric",
                "termin1_calc_percent" => "nullable|numeric",
                "termin1_date" => "nullable|string",
                "termin1_time" => "nullable|string",
                "termin1_at" => "nullable|numeric",
                "termin2_amt_type" => "nullable|numeric",
                "termin2_amt" => "nullable|numeric",
                "termin2_calc_percent" => "nullable|numeric",
                "sell_termin1_amt_type" => "nullable|numeric",
                "sell_termin1_amt" => "nullable|numeric",
                "sell_termin1_calc_percent" => "nullable|numeric",
                "sell_termin1_date" => "nullable|string",
                "sell_termin1_time" => "nullable|string",
                "sell_termin1_at" => "nullable|numeric",
                "sell_termin2_amt_type" => "nullable|numeric",
                "sell_termin2_amt" => "nullable|numeric",
                "sell_termin2_calc_percent" => "nullable|numeric",
                // "pocket_id" => "nullable|integer",
                // "pocket_type" => "nullable|numeric",
                // "pocket_checkpoints" => "nullable|array",
            ];
            // $req->margin_price = 0;
            // validasi input
            $isValidInput = Validator::make($input, $rulesInput);
            // dd($isValidInput);
            if (!$isValidInput->passes()) {
                $apiResp = Responses::bad_input($isValidInput->messages()->first());
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }
            $clientAuth = Users::getUserById($req->add_client)[0];
            $clientAuth->uid = $clientAuth->id;
            // dd($clientAuth);
            $adm_id = $req->auth->uid;
            $crt_type_order = Orders::CRT_TYPE_ORDER_ADMIN_SPECIAL;
            $type_truck_name = $req->truck_type_name;
            if ($req->truck_type_id && $req->truck_type_id != 0) {
                $type_truck_name = $req->truck_type_name;
            } else {
                $type_truck_name = "";
                $req->truck_type_id = 0;
            }
            $vdr_id = (int) $vdr->id;
            $vhc_id = (int) $vhs->id;
            $drv_id = (int) $drv->id;
            $device_id = str_pad($vhs->device_id, Vehicles::MAX_DEVICE_ID, "0", STR_PAD_LEFT);
            $req->vhc_nopol = $vhs->nopol1 . " " . $vhs->nopol2 . " " . $vhs->nopol3;
            $nopol = explode(" ", strtoupper($req->vhc_nopol));
            // dd($nopol);
            if (count($nopol) < 3) {
                $apiResp = Responses::bad_input("Nomor polisi kendaraan tidak valid");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }
            // dd($req->add_pickup_date . " " . $req->add_pickup_time_hour . ":" . $req->add_pickup_time_minute);
            $pckZids = [];
            $dropZids = [];
            $pickupAt = strtotime(
                $req->add_pickup_date .
                    " " .
                    $req->add_pickup_time_hour .
                    ":" .
                    $req->add_pickup_time_minute .
                    ":00"
            );
            // foreach ($req->points as $i => $point) {
            //     $pckZids[] = $point["pickup_zone_id"];
            //     $dropZids[] = $point["drop_zone_id"];
            //     $pickupAt[] = $point["pickup_at"];
            // }
            foreach ($req->add_pickup_zone as $i => $point) {
                $pckZids[] = $point;
            }
            // foreach ($req->add_pickup_zone as $i => $point) {
            //     // $pickupAt[] = $point;
            //     // dd($i);
            //     $time =
            //         $req->add_pickup_date[$i] .
            //         " " .
            //         $req->add_pickup_time_hour[$i] .
            //         ":" .
            //         $req->add_pickup_time_minute[$i] .
            //         ":00";
            //     $pickupAt[] = strtotime($time);
            // }
            foreach ($req->add_drop_zone as $i => $point) {
                $dropZids[] = $point;
            }
            $pcks = Zone::getZoneByIds($pckZids);
            if (count($pcks) < 1) {
                $apiResp = Responses::not_found("Lokasi penjemputan tidak ditemukan");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }
            if (empty($pcks[0]->boundary_points)) {
                $apiResp = Responses::bad_request("Lokasi penjemputan tidak valid");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }
            $drops = Zone::getZoneByIds($dropZids);
            if (count($drops) < 1) {
                $apiResp = Responses::not_found("Lokasi pengantaran tidak ditemukan");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }
            if (empty($drops[0]->boundary_points)) {
                $apiResp = Responses::bad_request("Lokasi pengantaran tidak valid");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }
            $req->points = [];
            foreach ($req->add_pickup_zone as $i => $point) {
                $pointData = [];
                $pointData["pickup_at"] = $pickupAt;
                $pointData["pickup_zone_id"] = $pckZids[$i];
                $pointData["pickup_zone_pic_name"] = $req->add_pickup_pic_name[$i];
                $pointData["pickup_zone_pic_phone"] = $req->add_pickup_pic_phone[$i];
                $pointData["drop_zone_id"] = $dropZids[$i];
                $pointData["drop_zone_pic_name"] = $req->add_drop_pic_name[$i];
                $pointData["drop_zone_pic_phone"] = $req->add_drop_pic_phone[$i];
                $req->points[] = $pointData;
            }
            $pckPoints = [];
            $dropPoints = [];
            $checklist = [];
            foreach ($req->points as $iPoint => $point) {
                foreach ($pcks as $iPck => $pck) {
                    if ($point["pickup_zone_id"] == $pck->id) {
                        if ($pck->prid == 0 || $pck->ktid == 0 || $pck->kcid == 0 || $pck->klid == 0) {
                            $apiResp = Responses::bad_request(
                                "Zonasi wilayah(provinsi, kecamatan, ...) lokasi penjemputan " .
                                    $pck->name .
                                    " belum lengkap"
                            );
                            return new Response($apiResp, $apiResp["meta"]["code"]);
                        }
                        $pcks[$iPck]->inpt_set_pck_at = $req->points[$iPoint]["pickup_at"];
                        $pcks[$iPck]->inpt_pic_name = $req->points[$iPoint]["pickup_zone_pic_name"];
                        $pcks[$iPck]->inpt_pic_phone_code = 62;
                        $pcks[$iPck]->inpt_pic_phone_val =
                            (int) $req->points[$iPoint]["pickup_zone_pic_phone"];
                        $pcks[$iPck]->inpt_pic_mail = null;
                        $pckPoints[$iPoint] = clone $pcks[$iPck];
                    }
                }
                foreach ($drops as $iDrop => $drop) {
                    if ($point["drop_zone_id"] == $drop->id) {
                        if ($drop->prid == 0 || $drop->ktid == 0 || $drop->kcid == 0 || $drop->klid == 0) {
                            $apiResp = Responses::bad_request(
                                "Zonasi wilayah(provinsi, kecamatan, ...) lokasi pengantaran " .
                                    $drop->name .
                                    " belum lengkap"
                            );
                            return new Response($apiResp, $apiResp["meta"]["code"]);
                        }
                        $drops[$iDrop]->inpt_pic_name = $req->points[$iPoint]["drop_zone_pic_name"];
                        $drops[$iDrop]->inpt_pic_phone_code = 62;
                        $drops[$iDrop]->inpt_pic_phone_val =
                            (int) $req->points[$iPoint]["drop_zone_pic_phone"];
                        $drops[$iDrop]->inpt_pic_mail = null;
                        $dropPoints[$iPoint] = clone $drops[$iDrop];
                    }
                }
            }
            // dd($req->checklist, $pckPoints, $dropPoints);
            // $lowestPickupAt = min($pickupAt);
            // $longestPickupAt = max($pickupAt);
            $nanoid = new Nanoid();
            // $ord_code = $nanoid->formattedId('0123456789', 12);
            // $doWhile = true;
            // do {
            // 	$uniqOrdCode = Orders::getOrderByCode($ord_code);
            // 	if (count($uniqOrdCode) > 0) {
            // 		$ord_code = $nanoid->formattedId('0123456789', 12);
            // 	} else {
            // 		$doWhile = false;
            // 	}
            // } while ($doWhile);
            if (
                ($req->sell_termin2_amt_type == OrdersTermins::AMT_TYPE_FLAT ||
                    $req->sell_termin2_amt_type == OrdersTermins::AMT_TYPE_PERCENT) &&
                $req->sell_termin2_amt > 0
            ) {
                $ord_code_type = 6;
            } else {
                if ($req->sell_termin1_at < $req->points[0]["pickup_at"]) {
                    $ord_code_type = 5;
                } else {
                    $ord_code_type = 7;
                }
            }
            $ord_code = Helper::gnrtOrdCode($ord_code_type);
            $rate_code = $nanoid->formattedId("123456789", 6);
            $doWhile = true;
            do {
                $uniqRateCode = ConfRates::getRateByCode($rate_code);
                if (count($uniqRateCode) > 0) {
                    $rate_code = $nanoid->formattedId("123456789", 6);
                } else {
                    $doWhile = false;
                }
            } while ($doWhile);
            $client = Clients::getClientById($clientAuth->client_group_id);
            // $rate = ConfRates::getRateById($req->lead_time_id);
            // $pck_pic = Clients::getClientById($pckPoints[0]->client_group_id);
            // $drop_pic = Clients::getClientById($dropPoints[0]->client_group_id);
            $admins = Users::getUsersActiveByRole(Users::ROLE_ADMIN);
            $device = Devices::getDeviceByDeviceId($device_id);
            if (count($device) < 1 || $device === false) {
                $device = [];
                $device[0] = new \StdClass();
                $device[0]->id = 0;
                $device[0]->name = "-";
                $device[0]->type = Devices::TYPE_BUILT_IN;
                $device[0]->simcard_code = 62;
                $device[0]->simcard = 0;
            }
            $admins_data = [];
            foreach ($admins as $k => $v) {
                array_push($admins_data, [
                    "admin_id" => $v->id,
                    "admin_name" => $v->first_name,
                    "admin_phone" => $v->phone_code . " " . implode(" ", str_split($v->phone, 4)),
                    "admin_mail" => $v->email,
                    "admin_addr" => "",
                ]);
            }
            $rate_id = 0;
            DB::beginTransaction();
            $vendor = Users::getUserById($vdr->id);
            $buy_price = $req->buy_price_total_tax;
            if ($req->buy_price_tax_type == OrdersAItems::AMT_TAX_TYPE_INCLUDE) {
                $buy_price = $req->buy_price_base;
            } elseif ($req->buy_price_tax_type == OrdersAItems::AMT_TAX_TYPE_EXCLUDE) {
                $buy_price = $req->buy_price_total_tax;
            }
            $insOrd = [
                "code" => $ord_code,
                "type" => Orders::TYPE_FCL,
                "pck_total" => count($pckPoints),
                "drop_total" => count($dropPoints),
                "est_lead_time" => 0,
                "lead_time" => 0,
                "est_price" => 0,
                "price" => 0,
                "est_real_price" => 0,
                "real_price" => 0,
                "est_disc_price" => 0,
                "disc_price" => 0,
                "est_buy_price" => 0,
                "buy_price" => 0,
                "est_real_buy_price" => 0,
                "real_buy_price" => 0,
                "est_disc_buy_price" => $req->disc_buy_price ?? 0,
                "disc_buy_price" => $req->disc_buy_price ?? 0,
                "est_rate_id" => $rate_id,
                "rate_id" => $rate_id,
                "status" => Orders::STTS_HAVE_GET_VHC,
                "crt_type_order" => $crt_type_order,
                "crt" => $now,
                "crt_by" => Auth::user()->id,
                "crt_adm_by" => Auth::user()->id,
                "updt" => $now,
                "updt_by" => Auth::user()->id,
                "confirm_at" => $now,
                "confirm_by" => Auth::user()->id,
                // 'find_vhc_at' => $now,
                // 'find_vhc_by' => $req->auth->id,
            ];
            $ord_id = Orders::addOrder($insOrd);
            $driver = Drivers::showDriverById($drv_id);
            $vehicle = Vehicles::showVehicleById($vhc_id);
            if ($device[0]->id > 0) {
                Devices::updateDevice($device[0]->id, [
                    "is_assigned" => Devices::IS_ASSIGNED,
                    "is_available" => Devices::IS_UNAVAIL,
                ]);
            }
            $pck_ids = [];
            foreach ($pckPoints as $i => $pck) {
                $insPck = [
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "set_pck_at" => $pck->inpt_set_pck_at,
                    "stts_pck" => OrdersPickups::STTS_WAIT,
                    "pck_id" => $pck->id,
                    "pck_name" => $pck->name,
                    "pck_desc" => $pck->desc,
                    "pck_type" => $pck->type,
                    "pck_workflow" => $pck->workflow_type,
                    "pck_shiptocode" => $pck->shiptocode,
                    "pck_prid" => $pck->prid,
                    "pck_ktid" => $pck->ktid,
                    "pck_kcid" => $pck->kcid,
                    "pck_klid" => $pck->klid,
                    "pck_addr" => $pck->fulladdress,
                    "pck_cgroup_id" => $pck->client_group_id,
                    "pck_cdiv_id" => $pck->client_div_id,
                    "pck_cgp_id" => $pck->client_gp_id,
                    "pck_hex_color" => $pck->boundary_hex_color,
                    "pck_shape" => $pck->boundary_type,
                    "pck_radius" => $pck->boundary_radius,
                    "pck_diameter" => $pck->boundary_diameter,
                    "pck_area" => $pck->boundary_area,
                    "pck_ha" => $pck->boundary_ha,
                    "pck_bounds" => $pck->boundary_bounds,
                    "pck_latlngs" => $pck->boundary_latlngs,
                    "pck_points" => $pck->boundary_points,
                    "pic_name" => $pck->inpt_pic_name,
                    "pic_phone_code" => $pck->inpt_pic_phone_code,
                    "pic_phone_val" => $pck->inpt_pic_phone_val,
                    "pic_mail" => $pck->inpt_pic_mail,
                    // 'pic_name' => $clientAuth->first_name,
                    // 'pic_phone_code' => $clientAuth->phone_code,
                    // 'pic_phone_val' => $clientAuth->phone,
                    // 'pic_mail' => $clientAuth->email,
                ];
                $pck_id = OrdersPickups::add($insPck);
                $pck_ids[] = $pck_id;
            }
            $drop_ids = [];
            $pck_drop_ids = [];
            foreach ($dropPoints as $i => $drop) {
                $insDrop = [
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "stts_drop" => OrdersDrops::STTS_WAIT,
                    "drop_id" => $drop->id,
                    "drop_name" => $drop->name,
                    "drop_desc" => $drop->desc,
                    "drop_type" => $drop->type,
                    "drop_workflow" => $drop->workflow_type,
                    "drop_shiptocode" => $drop->shiptocode,
                    "drop_prid" => $drop->prid,
                    "drop_ktid" => $drop->ktid,
                    "drop_kcid" => $drop->kcid,
                    "drop_klid" => $drop->klid,
                    "drop_addr" => $drop->fulladdress,
                    "drop_cgroup_id" => $drop->client_group_id,
                    "drop_cdiv_id" => $drop->client_div_id,
                    "drop_cgp_id" => $drop->client_gp_id,
                    "drop_hex_color" => $drop->boundary_hex_color,
                    "drop_shape" => $drop->boundary_type,
                    "drop_radius" => $drop->boundary_radius,
                    "drop_diameter" => $drop->boundary_diameter,
                    "drop_area" => $drop->boundary_area,
                    "drop_ha" => $drop->boundary_ha,
                    "drop_bounds" => $drop->boundary_bounds,
                    "drop_latlngs" => $drop->boundary_latlngs,
                    "drop_points" => $drop->boundary_points,
                    "pic_name" => $drop->inpt_pic_name,
                    "pic_phone_code" => $drop->inpt_pic_phone_code,
                    "pic_phone_val" => $drop->inpt_pic_phone_val,
                    "pic_mail" => $drop->inpt_pic_mail,
                    // 'pic_name' => $clientAuth->first_name,
                    // 'pic_phone_code' => $clientAuth->phone_code,
                    // 'pic_phone_val' => $clientAuth->phone,
                    // 'pic_mail' => $clientAuth->email,
                ];
                $drop_id = OrdersDrops::add($insDrop);
                $drop_ids[] = $drop_id;
                $pck_drop_id = OrdersPckDrop::add([
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "pck_id" => $pck_ids[$i],
                    "drop_id" => $drop_ids[$i],
                    "is_active" => 1,
                    "stts" => OrdersPckDrop::STTS_DELIVERY_OTW_PICKUP,
                    "crt" => $now,
                    "crt_by" => Auth::user()->id,
                    "updt" => $now,
                    "updt_by" => Auth::user()->id,
                ]);
                foreach ($req->checklist[$i] as $_checklist) {
                    $checklist = DB::table("t_conf_checklists")
                        ->where("id", $_checklist)
                        ->first();
                    $dt_insert = [
                        "did" => $drv_id,
                        "ord_id" => $ord_id,
                        "ord_code" => $ord_code,
                        "pck_id" => $pck_ids[$i],
                        "drop_id" => $drop_ids[$i],
                        "ord_pck_drop_id" => $pck_drop_id,
                        "checklist_id" => $checklist->id,
                        "checklist_name" => $checklist->name,
                        "checklist_desc" => $checklist->desc,
                        "crt" => $now,
                        "crt_by" => Auth::user()->id,
                        "updt" => $now,
                        "updt_by" => Auth::user()->id,
                    ];
                    $insertChecklist = DB::table("t_orders_drivers_uploads")->insert($dt_insert);
                }
            }
            $insClient = [
                "ord_id" => $ord_id,
                "ord_code" => $ord_code,
                "c_id" => $clientAuth->uid,
                "c_name" => $clientAuth->first_name,
                "c_phone_code" => $clientAuth->phone_code,
                "c_phone_val" => $clientAuth->phone,
                "c_mail" => $clientAuth->email,
                "c_addr" => $clientAuth->fulladdress,
                "c_disc_type" => $client[0]->disc_type,
                "c_disc_amount" => $client[0]->disc_amount,
                "c_pt_id" => $client[0]->id,
                "c_pt_div_id" => $clientAuth->client_div_id,
                "c_pt_gp_id" => $clientAuth->client_gp_id,
                "c_pt_name" => $client[0]->c_name,
                "c_pt_phone_code" => $client[0]->c_phone_code,
                "c_pt_phone_val" => $client[0]->c_phone,
                "c_pt_mail" => $client[0]->c_mail,
                "c_pt_addr" => $client[0]->c_addr_office,
                "c_pt_pic_name" => $client[0]->pic_name,
                "c_pt_pic_phone_code" => $client[0]->pic_phone_code,
                "c_pt_pic_phone_val" => $client[0]->pic_phone,
                "c_pt_pic_mail" => $client[0]->pic_mail,
                "c_pt_pic_addr" => null,
                "prefer_truck_type" => $req->truck_type_id,
                "is_pkp" => 0,
            ];
            $ord_client_id = OrdersClients::add($insClient);
            // if ($req->is_vdr_bcng != Users::IS_VDR_BCNG_YES) {
            //     $insRate = [
            //         "ord_id" => $ord_id,
            //         "ord_code" => $ord_code,
            //         "rate_id" => $rate_id,
            //         "rate_code" => $rate_code,
            //         "origin_prov" => $insRate["origin_prov"],
            //         "origin_city" => $insRate["origin_city"],
            //         "origin_district" => $insRate["origin_district"],
            //         "origin_village" => $insRate["origin_village"],
            //         "lane" => $insRate["lane"],
            //         "dest_prov" => $insRate["dest_prov"],
            //         "dest_city" => $insRate["dest_city"],
            //         "dest_district" => $insRate["dest_district"],
            //         "dest_village" => $insRate["dest_village"],
            //         "fast_time" => $insRate["fast_time"],
            //         "long_time" => $insRate["long_time"],
            //         "unit_time" => $insRate["unit_time"],
            //         "sell_kg" => $insRate["sell_kg"],
            //         "buy_kg" => $insRate["buy_kg"],
            //         "margin_kg" => $insRate["margin_kg"],
            //         "percent_kg" => $insRate["percent_kg"],
            //         "sell_cbm" => $insRate["sell_cbm"],
            //         "buy_cbm" => $insRate["buy_cbm"],
            //         "margin_cbm" => $insRate["margin_cbm"],
            //         "percent_cbm" => $insRate["percent_cbm"],
            //         "sell_ftl" => $insRate["sell_ftl"],
            //         "buy_ftl" => $insRate["buy_ftl"],
            //         "margin_ftl" => $insRate["margin_ftl"],
            //         "percent_ftl" => $insRate["percent_ftl"],
            //         "vdr_id" => $insRate["vdr_id"],
            //         "vhc_type" => $insRate["vhc_type"],
            //         "crt_type" => $insRate["crt_type"],
            //     ];
            //     OrdersRates::add($insRate);
            // }
            if ($req->is_insurance == "true") {
                // if (count($insurance) > 0) {
                $insurance = StaticInsurances::listStaticInsurances([
                    "is_active" => StaticInsurances::IS_ACTIVE,
                ]);
                $beneficiary = $req->amt_beneficiary ?? 0; // nilai pertangguhan
                $calcPercent = ($beneficiary * $insurance[0]->amt_percent) / 100;
                $flatPercent = $beneficiary - $calcPercent;
                $insInsurance = [
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "insurance_id" => 0,
                    "premi_name" => $insurance[0]->desc,
                    "premi_price" => $calcPercent,
                    "amt_beneficiary" => $beneficiary,
                    "amt_percent" => $insurance[0]->amt_percent,
                    "amt_type" => OrdersInsurances::AMT_TYPE_PERCENT,
                    "premi_min_price" => $beneficiary,
                    "premi_max_price" => $beneficiary,
                    "desc" => $insurance[0]->desc,
                ];
                OrdersInsurances::add($insInsurance);
            }
            $vdrCompany = Clients::getClientById($vendor[0]->client_group_id);
            $insVendors = [
                "ord_id" => $ord_id,
                "ord_code" => $ord_code,
                "vdr_id" => $vendor[0]->id,
                "vdr_name" => $vendor[0]->first_name,
                "vdr_mail" => $vendor[0]->email,
                "vdr_phone_code" => $vendor[0]->phone_code,
                "vdr_phone_val" => $vendor[0]->phone,
                "vdr_addr" => $vendor[0]->fulladdress,
                "vdr_cgroup_id" => $vendor[0]->client_group_id,
                "vdr_pt_name" => $vdrCompany[0]->c_name,
                "vdr_pt_phone_code" => $vdrCompany[0]->c_phone_code,
                "vdr_pt_phone_val" => $vdrCompany[0]->c_phone,
                "vdr_pt_mail" => $vdrCompany[0]->c_mail,
                "vdr_pt_addr" => $vdrCompany[0]->c_addr_office,
                "vdr_pt_pic_name" => $vdrCompany[0]->pic_name,
                "vdr_pt_pic_phone_code" => $vdrCompany[0]->pic_phone_code,
                "vdr_pt_pic_phone_val" => $vdrCompany[0]->pic_phone,
                "vdr_pt_pic_mail" => $vdrCompany[0]->pic_mail,
                "vdr_bank_id" => $vendor[0]->bank_id,
                "vdr_bank_code" => $vendor[0]->bank_code,
                "vdr_bank_short_name" => $vendor[0]->bank_short_name,
                "vdr_bank_name" => $vendor[0]->bank_name,
                "vdr_bank_branch_name" => $vendor[0]->bank_branch_name,
                "vdr_bank_acc_number" => $vendor[0]->bank_acc_number,
                "vdr_bank_acc_name" => $vendor[0]->bank_acc_name,
                "is_vdr_bcng" => 0,
                "find_vhcs" => $vhc_id,
                "find_radius" => 0,
                "find_shape" => Zone::ZONE_BOUNDARY_CIRCLE,
                "find_center_lat" => 0,
                "find_center_lng" => 0,
                "distance" => 0,
                "is_exp" => OrdersVendors::LINK_NOT_EXP,
                "exp_at" => 0, // strtotime('+10 minutes', $now), // strtotime('+1 day', $now)
                "bid_token" => (new Nanoid())->generateId(36),
                "is_mailing_bid" => OrdersVendors::IS_MAILING_BID_NOT,
                "is_mailing_bid_at" => 0,
                "is_want" => OrdersVendors::IS_WANT_YES,
                "is_want_at" => $now,
                "status" => OrdersVendors::STTS_ACC,
                "respond_at" => $now,
                "is_pkp" => 0,
                "crt" => $now,
            ];
            OrdersVendors::add($insVendors);
            OrdersDrivers::add([
                "ord_id" => $ord_id,
                "ord_code" => $ord_code,
                "vdr_id" => $driver[0]->vendor_id,
                "drv_id" => $driver[0]->id,
                "drv_name" => $driver[0]->fullname,
                "drv_mail" => $driver[0]->email,
                "drv_name2" => $driver[0]->fullname2,
                "drv_phone_code" => $driver[0]->phone_code,
                "drv_phone_val" => $driver[0]->phone,
                "drv_phone2_code" => $driver[0]->phone2_code,
                "drv_phone2_val" => $driver[0]->phone2,
                "drv_addr" => $driver[0]->fulladdress,
                "drv_cgroup_id" => $driver[0]->client_group_id,
                "drv_nik" => $driver[0]->nik,
                "drv_dob" => $driver[0]->dob,
                "drv_age" => Helper::countAge($driver[0]->dob),
                "drv_gender" => $driver[0]->gender,
                "drv_blood" => $driver[0]->blood,
                "em_fullname" => $driver[0]->em_fullname,
                "em_phone_code" => $driver[0]->em_phone_code,
                "em_phone_val" => $driver[0]->em_phone,
                "em_relationship" => $driver[0]->em_relationship,
                "drv_ktp_img" => $driver[0]->ktp_img,
                "drv_npwp_img" => $driver[0]->npwp_img,
                "drv_npwp_number" => $driver[0]->npwp_number,
                "drv_npwp_string" => $driver[0]->npwp_string,
                "drv_license_img" => $driver[0]->license_img,
                "drv_license_number" => $driver[0]->license_number,
                "drv_license_exp" => $driver[0]->license_exp,
                "drv_bank_id" => $req->drv_bank_id ?? 0,
                "drv_bank_code" => $req->drv_bank_code ?? null,
                "drv_bank_short_name" => $req->drv_bank_short ?? null,
                "drv_bank_name" => $req->drv_bank_name ?? null,
                "drv_bank_branch_name" => $req->drv_bank_branch_name ?? null,
                "drv_bank_acc_number" => $req->drv_bank_acc_number ?? null,
                "drv_bank_acc_name" => $req->drv_bank_acc_name ?? null,
            ]);
            OrdersVehicles::add([
                "ord_id" => $ord_id,
                "ord_code" => $ord_code,
                "vhc_id" => $vehicle[0]->vid,
                "vhc_name" => $vehicle[0]->name,
                "vhc_dvc_id" => $device[0]->id,
                "vhc_dvc_name" => $device[0]->name,
                "vhc_dvc_type" => $device[0]->type,
                "vhc_device_id" => $vehicle[0]->device_id,
                "vhc_simcard_code" => $device[0]->simcard_code,
                "vhc_simcard" => $device[0]->simcard,
                "vhc_cat_id" => $vehicle[0]->cat_id,
                "vhc_brand_id" => $vehicle[0]->brand_id,
                "vhc_type_id" => $vehicle[0]->type_id,
                "vhc_model_id" => $vehicle[0]->model_id,
                "vhc_c_did" => $vehicle[0]->c_did,
                "vhc_a_did" => $vehicle[0]->a_did,
                "vhc_nopol1" => $vehicle[0]->nopol1,
                "vhc_nopol2" => $vehicle[0]->nopol2,
                "vhc_nopol3" => $vehicle[0]->nopol3,
                "vhc_is_track_holiday" => $vehicle[0]->is_track_holiday,
                "vhc_track_sch_d" => $vehicle[0]->track_sch_d,
                "vhc_track_sch_h" => $vehicle[0]->track_sch_h,
                "vhc_cgroup_id" => $vehicle[0]->client_group_id,
                "vhc_vdr_id" => $vehicle[0]->vendor_id,
                "vhc_speed_limit" => $vehicle[0]->speed_limit,
                "vhc_fuel_capacity" => $vehicle[0]->fuel_capacity,
                "vhc_fuel_drop_treshold" => $vehicle[0]->fuel_drop_treshold,
                "vhc_max_pressure" => $vehicle[0]->max_pressure,
                "vhc_fvhc_img" => $vehicle[0]->fvhc_img,
                "vhc_stnk_img" => $vehicle[0]->stnk_img,
                "vhc_stnk_exp" => $vehicle[0]->stnk_exp,
                "vhc_vyear" => $vehicle[0]->vyear,
                "vhc_cc" => $vehicle[0]->cc,
                "vhc_vin" => $vehicle[0]->vin,
                "vhc_en" => $vehicle[0]->en,
                "vhc_vcolor" => $vehicle[0]->vcolor,
                "vhc_fuel_type" => $vehicle[0]->fuel_type,
                "vhc_tnkb_color" => $vehicle[0]->tnkb_color,
                "vhc_regis_year" => $vehicle[0]->regis_year,
                "vhc_tax_exp" => $vehicle[0]->tax_exp,
            ]);
            if ($req->is_vdr_bcng == Users::IS_VDR_BCNG_YES) {
                // $req->pocket_id
                // $req->pocket_type
                // $req->pocket_checkpoints
                $pocket = PocketMoney::showPocketById($req->pocket_id);
                if (count($pocket) < 1) {
                    $apiResp = Responses::not_found("uang saku belum dipilih");
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
                $ord_pocket_id = OrdersPockets::addPocket([
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "pocket_id" => $pocket[0]->id,
                    "pocket_code" => $pocket[0]->code,
                    "pocket_name" => $pocket[0]->name,
                    "pck_id" => $pocket[0]->pck_id,
                    "drop_id" => $pocket[0]->drop_id,
                    "pocket_type" => $pocket[0]->type,
                    "pocket_flow" => $pocket[0]->flow,
                    "pocket_is_active" => $pocket[0]->is_active,
                    "pocket_total" => $pocket[0]->total,
                    "crt" => $now,
                    "crt_by" => $req->auth->uid,
                    "updt" => $now,
                    "updt_by" => $req->auth->uid,
                ]);
                // order_pocket_id
                $pocket[0]->checkpoints = Checkpoints::listCheckpoints([
                    "pocket_id" => $pocket[0]->id,
                ]);
                foreach ($pocket[0]->checkpoints as $checkpoint) {
                    OrdersCheckpoints::addCheckpoint([
                        "ord_id" => $ord_id,
                        "ord_code" => $ord_code,
                        "ord_pocket_id" => $ord_pocket_id,
                        "pocket_id" => $checkpoint->pocket_id,
                        "pck_id" => $checkpoint->pck_id,
                        "drop_id" => $checkpoint->drop_id,
                        "pocket_sort" => $checkpoint->sort,
                        "pocket_fee" => $checkpoint->pocket_fee,
                        "pocket_tol_fee" => $checkpoint->tol_fee,
                        "pocket_fuel_fee" => $checkpoint->fuel_fee,
                        "pocket_total" => $checkpoint->total,
                        "crt" => $now,
                        "crt_by" => $req->auth->uid,
                        "updt" => $now,
                        "updt_by" => $req->auth->uid,
                    ]);
                }
            }
            $main_item_dec = "";
            // $main_item_dec =
            //     "Pengantaran Logistik
" .
            //     ucwords(strtolower($pckPoints[0]->prid_name)) .
            //     " - " .
            //     ucwords(strtolower($dropPoints[0]->prid_name)) .
            //     "
" .
            //     date("d F Y", $insPck["set_pck_at"]);
            // $main_item_dec =
            //     "Pengantaran Logistik
" .
            //     ucwords(strtolower($pckPoints[0]->prid_name)) .
            //     " - " .
            //     ucwords(strtolower($dropPoints[0]->prid_name)) .
            //     "
" .
            //     strftime("%d %B %Y", $insPck["set_pck_at"]);
            // $main_item_dec =
            //     "Pengantaran Logistik
" .
            //     ucwords(strtolower($pckPoints[0]->ktid_name)) .
            //     " - " .
            //     ucwords(strtolower($dropPoints[0]->ktid_name)) .
            //     "
" .
            //     ucwords(strtolower($dropPoints[0]->name)) .
            //     ", " .
            //     ucwords(strtolower($dropPoints[0]->prid_name)) .
            //     " - " .
            //     $dropPoints[0]->shiptocode .
            //     "
" .
            //     strftime("%d %m %Y", $insPck["set_pck_at"]) .
            //     "
" .
            //     $req->note_tonase;
            if ($req->is_vdr_bcng == Users::IS_VDR_BCNG_YES) {
                $req->termin1_amt_type = OrdersTermins::AMT_TYPE_PERCENT;
                $req->buy_price = 0;
                $req->buy_price_tax_type = 0;
                $req->buy_price_tax_ppn_percent = 0;
                $req->buy_price_tax_ppn_flat = 0;
                $req->buy_price_tax_pph_percent = 0;
                $req->buy_price_tax_pph_flat = 0;
                $req->buy_price_total_tax = 0;
                $req->termin1_amt = 0;
                $req->termin1_calc_percent = 0;
                $req->termin1_at = 0;
                $req->termin1_calc_percent_base = 0;
            }
            $v_termin_sequence = 0;
            $v_termin1_id = 0;
            $v_termin2_id = 0;
            if (
                $req->termin1_amt_type == OrdersTermins::AMT_TYPE_FLAT ||
                $req->termin1_amt_type == OrdersTermins::AMT_TYPE_PERCENT
            ) {
                ++$v_termin_sequence;
                $insTerminsVdr1 = [
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "flow_type" => OrdersTermins::FLOW_TYPE_PAYMENT,
                    "cash_type" => OrdersTermins::TYPE_CASH_OUT,
                    "sequence" => $v_termin_sequence,
                    "termin_base_flat" => $req->buy_price,
                    "termin_tax_type" => $req->buy_price_tax_type,
                    "termin_tax_ppn_percent" => $req->buy_price_tax_ppn_percent ?? 0,
                    "termin_tax_ppn_flat" => $req->buy_price_tax_ppn_flat ?? 0,
                    "termin_tax_pph_percent" => $req->buy_price_tax_pph_percent ?? 0,
                    "termin_tax_pph_flat" => $req->buy_price_tax_pph_flat ?? 0,
                    "termin_total_tax_flat" => $req->buy_price_total_tax ?? 0,
                    "termin_amt_type" => $req->termin1_amt_type ?? 0,
                    "termin_amt" => $req->termin1_amt ?? 0,
                    "termin_calc_percent" => $req->termin1_calc_percent ?? 0,
                    "termin_result_flat" =>
                        $req->termin1_amt_type == OrdersTermins::AMT_TYPE_FLAT
                            ? $req->termin1_amt
                            : $req->termin1_calc_percent,
                    "termin_ddln_at" => $req->termin1_at ?? 0,
                    "termin_ddln_type" => OrdersTermins::DDLN_TERMIN_TYPE_TIME,
                    "termin_for" => OrdersTermins::TERMIN_FOR_VENDOR,
                    "crt" => $now,
                    "crt_by" => $req->auth->uid,
                    "crt_type" => OrdersTermins::CRT_TYPE_ADMIN,
                    "updt" => $now,
                    "updt_by" => $req->auth->uid,
                ];
                // dd($insTerminsVdr1);
                $v_termin1_id = OrdersTermins::add($insTerminsVdr1);
                $qty = 1;
                $amt_base_flat =
                    $req->termin1_amt_type == OrdersTermins::AMT_TYPE_FLAT
                        ? $req->termin1_amt
                        : $req->termin1_calc_percent_base;
                $yang_dijadiin_summary = $insTerminsVdr1["termin_result_flat"];
                if ($req->buy_price_tax_type == OrdersAItems::AMT_TAX_TYPE_INCLUDE) {
                    // $yang_dijadiin_summary = $amt_base_flat;
                    $yang_dijadiin_summary = $insTerminsVdr1["termin_result_flat"];
                } elseif ($req->buy_price_tax_type == OrdersAItems::AMT_TAX_TYPE_EXCLUDE) {
                    $yang_dijadiin_summary = $insTerminsVdr1["termin_result_flat"];
                }
                OrdersAItems::add([
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "flow_type" => OrdersAItems::FLOW_TYPE_PAYMENT,
                    "cash_type" => OrdersAItems::TYPE_CASH_OUT,
                    "v_termin_id" => $v_termin1_id,
                    "a_item_type" => OrdersAItems::A_TYPE_PRIMARY,
                    // 'desc' => $main_item_dec . "
Pembayaran termin 1",
                    "desc" => $main_item_dec,
                    "unit_id" => 0,
                    "unit_type" => UnitTypes::UNIT_TYPE_PAKET,
                    "unit_qty" => $qty,
                    "amt_base_flat" => $amt_base_flat,
                    "amt_tax_type" => $req->buy_price_tax_type,
                    "amt_tax_ppn_percent" => $req->buy_price_tax_ppn_percent ?? 0,
                    "amt_tax_ppn_flat" => $req->buy_price_tax_ppn_flat ?? 0,
                    "amt_tax_pph_percent" => $req->buy_price_tax_pph_percent ?? 0,
                    "amt_tax_pph_flat" => $req->buy_price_tax_pph_flat ?? 0,
                    "amt_total_tax_flat" => $insTerminsVdr1["termin_result_flat"],
                    "amt_result_flat" => $insTerminsVdr1["termin_result_flat"],
                    "amt_total_flat" => $yang_dijadiin_summary * $qty,
                    "ddln_pay_at" => $insTerminsVdr1["termin_ddln_at"],
                    "ddln_pay_type" => OrdersAItems::DDLN_PAY_TYPE_TIME,
                    "img_proof_submission" => null,
                    "img_proof_payment" => null,
                    "bank_id" => $insVendors["vdr_bank_id"],
                    "bank_code" => $insVendors["vdr_bank_code"],
                    "bank_name" => $insVendors["vdr_bank_name"],
                    "bank_short_name" => $insVendors["vdr_bank_short_name"],
                    "bank_acc_number" => $insVendors["vdr_bank_acc_number"],
                    "bank_acc_name" => $insVendors["vdr_bank_acc_name"],
                    "invc_to_client" => OrdersAItems::INVC_TO_CLIENT_NO,
                    "calc_to_vdr" => OrdersAItems::CALC_TO_VDR_YES,
                    "only_vdr" => OrdersAItems::ONLY_VDR_YES,
                    "ref_ord_id" => $ord_id,
                    "ref_ord_code" => $ord_code,
                    "ref_v_termin_id" => $v_termin1_id,
                    "crt" => $now,
                    "crt_by" => $req->auth->uid,
                    "updt" => $now,
                    "updt_by" => $req->auth->uid,
                ]);
            }
            if (
                ($req->termin2_amt_type == OrdersTermins::AMT_TYPE_FLAT ||
                    $req->termin2_amt_type == OrdersTermins::AMT_TYPE_PERCENT) &&
                $req->termin2_amt > 0
            ) {
                ++$v_termin_sequence;
                $insTerminsVdr2 = [
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "flow_type" => OrdersTermins::FLOW_TYPE_PAYMENT,
                    "cash_type" => OrdersTermins::TYPE_CASH_OUT,
                    "sequence" => $v_termin_sequence,
                    "termin_base_flat" => $req->buy_price,
                    "termin_tax_type" => $req->buy_price_tax_type,
                    "termin_tax_ppn_percent" => $req->buy_price_tax_ppn_percent ?? 0,
                    "termin_tax_ppn_flat" => $req->buy_price_tax_ppn_flat ?? 0,
                    "termin_tax_pph_percent" => $req->buy_price_tax_pph_percent ?? 0,
                    "termin_tax_pph_flat" => $req->buy_price_tax_pph_flat ?? 0,
                    "termin_total_tax_flat" => $req->buy_price_total_tax ?? 0,
                    "termin_amt_type" => $req->termin2_amt_type ?? 0,
                    "termin_amt" => $req->termin2_amt ?? 0,
                    "termin_calc_percent" => $req->termin2_calc_percent ?? 0,
                    "termin_result_flat" =>
                        $req->termin2_amt_type == OrdersTermins::AMT_TYPE_FLAT
                            ? $req->termin2_amt
                            : $req->termin2_calc_percent,
                    "termin_ddln_at" => $req->termin2_at ?? 0,
                    "termin_ddln_type" => OrdersTermins::DDLN_TERMIN_TYPE_ORD_FINISH,
                    "termin_for" => OrdersTermins::TERMIN_FOR_VENDOR,
                    "crt" => $now,
                    "crt_by" => $req->auth->uid,
                    "crt_type" => OrdersTermins::CRT_TYPE_ADMIN,
                    "updt" => $now,
                    "updt_by" => $req->auth->uid,
                ];
                $v_termin2_id = OrdersTermins::add($insTerminsVdr2);
                $qty = 1;
                $amt_base_flat =
                    $req->termin2_amt_type == OrdersTermins::AMT_TYPE_FLAT
                        ? $req->termin2_amt
                        : $req->termin2_calc_percent_base;
                $yang_dijadiin_summary = $insTerminsVdr2["termin_result_flat"];
                if ($req->buy_price_tax_type == OrdersAItems::AMT_TAX_TYPE_INCLUDE) {
                    // $yang_dijadiin_summary = $amt_base_flat;
                    $yang_dijadiin_summary = $insTerminsVdr2["termin_result_flat"];
                } elseif ($req->buy_price_tax_type == OrdersAItems::AMT_TAX_TYPE_EXCLUDE) {
                    $yang_dijadiin_summary = $insTerminsVdr2["termin_result_flat"];
                }
                OrdersAItems::add([
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "flow_type" => OrdersAItems::FLOW_TYPE_PAYMENT,
                    "cash_type" => OrdersAItems::TYPE_CASH_OUT,
                    "v_termin_id" => $v_termin2_id,
                    "a_item_type" => OrdersAItems::A_TYPE_PRIMARY,
                    // 'desc' => $main_item_dec . "
Pembayaran termin 2",
                    "desc" => $main_item_dec,
                    "unit_id" => 0,
                    "unit_type" => UnitTypes::UNIT_TYPE_PAKET,
                    "unit_qty" => $qty,
                    "amt_base_flat" => $amt_base_flat,
                    "amt_tax_type" => $req->buy_price_tax_type,
                    "amt_tax_ppn_percent" => $req->buy_price_tax_ppn_percent ?? 0,
                    "amt_tax_ppn_flat" => $req->buy_price_tax_ppn_flat ?? 0,
                    "amt_tax_pph_percent" => $req->buy_price_tax_pph_percent ?? 0,
                    "amt_tax_pph_flat" => $req->buy_price_tax_pph_flat ?? 0,
                    "amt_total_tax_flat" => $insTerminsVdr2["termin_result_flat"],
                    "amt_result_flat" => $insTerminsVdr2["termin_result_flat"],
                    "amt_total_flat" => $yang_dijadiin_summary * $qty,
                    "ddln_pay_at" => $insTerminsVdr2["termin_ddln_at"],
                    "ddln_pay_type" => OrdersAItems::DDLN_PAY_TYPE_ORD_FINISH,
                    "img_proof_submission" => null,
                    "img_proof_payment" => null,
                    "bank_id" => $insVendors["vdr_bank_id"],
                    "bank_code" => $insVendors["vdr_bank_code"],
                    "bank_name" => $insVendors["vdr_bank_name"],
                    "bank_short_name" => $insVendors["vdr_bank_short_name"],
                    "bank_acc_number" => $insVendors["vdr_bank_acc_number"],
                    "bank_acc_name" => $insVendors["vdr_bank_acc_name"],
                    "invc_to_client" => OrdersAItems::INVC_TO_CLIENT_NO,
                    "calc_to_vdr" => OrdersAItems::CALC_TO_VDR_YES,
                    "only_vdr" => OrdersAItems::ONLY_VDR_YES,
                    "ref_ord_id" => $ord_id,
                    "ref_ord_code" => $ord_code,
                    "ref_v_termin_id" => $v_termin2_id,
                    "crt" => $now,
                    "crt_by" => $req->auth->uid,
                    "updt" => $now,
                    "updt_by" => $req->auth->uid,
                ]);
            }
            if ($v_termin_sequence === 0) {
                $req->termin1_amt_type = OrdersTermins::AMT_TYPE_FLAT;
                $req->termin1_amt = $req->buy_price;
                $req->termin1_amt_at = strtotime("+2 days", $now);
                $req->termin1_calc_percent = 0;
                ++$v_termin_sequence;
                $insTerminsVdr1 = [
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "flow_type" => OrdersTermins::FLOW_TYPE_PAYMENT,
                    "cash_type" => OrdersTermins::TYPE_CASH_OUT,
                    "sequence" => $v_termin_sequence,
                    "termin_base_flat" => $req->buy_price,
                    "termin_tax_type" => $req->buy_price_tax_type,
                    "termin_tax_ppn_percent" => $req->buy_price_tax_ppn_percent ?? 0,
                    "termin_tax_ppn_flat" => $req->buy_price_tax_ppn_flat ?? 0,
                    "termin_tax_pph_percent" => $req->buy_price_tax_pph_percent ?? 0,
                    "termin_tax_pph_flat" => $req->buy_price_tax_pph_flat ?? 0,
                    "termin_total_tax_flat" => $req->buy_price_total_tax ?? 0,
                    "termin_amt_type" => $req->termin1_amt_type ?? 0,
                    "termin_amt" => $req->termin1_amt ?? 0,
                    "termin_calc_percent" => $req->termin1_calc_percent ?? 0,
                    "termin_result_flat" =>
                        $req->termin1_amt_type == OrdersTermins::AMT_TYPE_FLAT
                            ? $req->termin1_amt
                            : $req->termin1_calc_percent,
                    "termin_ddln_at" => $req->termin1_at ?? 0,
                    "termin_ddln_type" => OrdersTermins::DDLN_TERMIN_TYPE_ORD_FINISH,
                    "termin_for" => OrdersTermins::TERMIN_FOR_VENDOR,
                    "crt" => $now,
                    "crt_by" => $req->auth->uid,
                    "crt_type" => OrdersTermins::CRT_TYPE_ADMIN,
                    "updt" => $now,
                    "updt_by" => $req->auth->uid,
                ];
                $v_termin1_id = OrdersTermins::add($insTerminsVdr1);
                $qty = 1;
                $amt_base_flat =
                    $req->termin1_amt_type == OrdersTermins::AMT_TYPE_FLAT
                        ? $req->termin1_amt
                        : $req->termin1_calc_percent_base;
                $yang_dijadiin_summary = $insTerminsVdr1["termin_result_flat"];
                if ($req->buy_price_tax_type == OrdersAItems::AMT_TAX_TYPE_INCLUDE) {
                    // $yang_dijadiin_summary = $amt_base_flat;
                    $yang_dijadiin_summary = $insTerminsVdr1["termin_result_flat"];
                } elseif ($req->buy_price_tax_type == OrdersAItems::AMT_TAX_TYPE_EXCLUDE) {
                    $yang_dijadiin_summary = $insTerminsVdr1["termin_result_flat"];
                }
                OrdersAItems::add([
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "flow_type" => OrdersAItems::FLOW_TYPE_PAYMENT,
                    "cash_type" => OrdersAItems::TYPE_CASH_OUT,
                    "v_termin_id" => $v_termin1_id,
                    "a_item_type" => OrdersAItems::A_TYPE_PRIMARY,
                    // 'desc' => $main_item_dec . "
Pembayaran termin 1",
                    "desc" => $main_item_dec,
                    "unit_id" => 0,
                    "unit_type" => UnitTypes::UNIT_TYPE_PAKET,
                    "unit_qty" => $qty,
                    "amt_base_flat" => $amt_base_flat,
                    "amt_tax_type" => $req->buy_price_tax_type,
                    "amt_tax_ppn_percent" => $req->buy_price_tax_ppn_percent ?? 0,
                    "amt_tax_ppn_flat" => $req->buy_price_tax_ppn_flat ?? 0,
                    "amt_tax_pph_percent" => $req->buy_price_tax_pph_percent ?? 0,
                    "amt_tax_pph_flat" => $req->buy_price_tax_pph_flat ?? 0,
                    "amt_total_tax_flat" => $insTerminsVdr1["termin_result_flat"],
                    "amt_result_flat" => $insTerminsVdr1["termin_result_flat"],
                    "amt_total_flat" => $yang_dijadiin_summary * $qty,
                    "ddln_pay_at" => $insTerminsVdr1["termin_ddln_at"],
                    "ddln_pay_type" => OrdersAItems::DDLN_PAY_TYPE_ORD_FINISH,
                    "img_proof_submission" => null,
                    "img_proof_payment" => null,
                    "bank_id" => $insVendors["vdr_bank_id"],
                    "bank_code" => $insVendors["vdr_bank_code"],
                    "bank_name" => $insVendors["vdr_bank_name"],
                    "bank_short_name" => $insVendors["vdr_bank_short_name"],
                    "bank_acc_number" => $insVendors["vdr_bank_acc_number"],
                    "bank_acc_name" => $insVendors["vdr_bank_acc_name"],
                    "invc_to_client" => OrdersAItems::INVC_TO_CLIENT_NO,
                    "calc_to_vdr" => OrdersAItems::CALC_TO_VDR_YES,
                    "only_vdr" => OrdersAItems::ONLY_VDR_YES,
                    "ref_ord_id" => $ord_id,
                    "ref_ord_code" => $ord_code,
                    "ref_v_termin_id" => $v_termin1_id,
                    "crt" => $now,
                    "crt_by" => $req->auth->uid,
                    "updt" => $now,
                    "updt_by" => $req->auth->uid,
                ]);
            }
            $c_termin_sequence = 0;
            $c_termin1_id = 0;
            $c_termin2_id = 0;
            if (
                $req->sell_termin1_amt_type == OrdersTermins::AMT_TYPE_FLAT ||
                $req->sell_termin1_amt_type == OrdersTermins::AMT_TYPE_PERCENT
            ) {
                ++$c_termin_sequence;
                $insTerminsC1 = [
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "flow_type" => OrdersTermins::FLOW_TYPE_BILLING,
                    "cash_type" => OrdersTermins::TYPE_CASH_IN,
                    "sequence" => $c_termin_sequence,
                    "termin_base_flat" => $req->sell_price,
                    "termin_tax_type" => OrdersAItems::AMT_TAX_TYPE_EXCLUDE,
                    "termin_tax_ppn_percent" => OrdersAItems::PPN_PERCENT,
                    "termin_tax_pph_percent" => OrdersAItems::PPH_PERCENT,
                    "termin_amt_type" => $req->sell_termin1_amt_type ?? 0,
                    "termin_amt" => $req->sell_termin1_amt ?? 0,
                    "termin_calc_percent" => $req->sell_termin1_calc_percent ?? 0,
                    "termin_result_flat" =>
                        $req->sell_termin1_amt_type == OrdersTermins::AMT_TYPE_FLAT
                            ? $req->sell_termin1_amt
                            : $req->sell_termin1_calc_percent,
                    "termin_ddln_at" => $req->sell_termin1_at ?? 0,
                    "termin_ddln_type" => OrdersTermins::DDLN_TERMIN_TYPE_TIME,
                    "termin_for" => OrdersTermins::TERMIN_FOR_CLIENT,
                    "crt" => $now,
                    "crt_by" => $req->auth->uid,
                    "crt_type" => OrdersTermins::CRT_TYPE_ADMIN,
                    "updt" => $now,
                    "updt_by" => $req->auth->uid,
                ];
                $c_termin1_id = OrdersTermins::add($insTerminsC1);
                $qty = 1;
                OrdersAItems::add([
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "flow_type" => OrdersAItems::FLOW_TYPE_BILLING,
                    "cash_type" => OrdersAItems::TYPE_CASH_IN,
                    "c_termin_id" => $c_termin1_id,
                    "a_item_type" => OrdersAItems::A_TYPE_PRIMARY,
                    // 'desc' => $main_item_dec . "
Pembayaran termin 1",
                    "desc" => $main_item_dec,
                    "unit_id" => 0,
                    "unit_type" => UnitTypes::UNIT_TYPE_PAKET,
                    "unit_qty" => $qty,
                    "amt_bill_base_flat" => $insTerminsC1["termin_result_flat"],
                    "amt_bill_result_flat" => $insTerminsC1["termin_result_flat"],
                    "amt_bill_total_flat" => $insTerminsC1["termin_result_flat"] * $qty,
                    "ddln_pay_at" => $insTerminsC1["termin_ddln_at"],
                    "ddln_pay_type" => OrdersAItems::DDLN_PAY_TYPE_TIME,
                    "img_proof_submission" => null,
                    "img_proof_payment" => null,
                    "bank_id" => Banks::DFT_BANK_ID,
                    "bank_code" => Banks::DFT_BANK_CODE,
                    "bank_name" => Banks::DFT_BANK_NAME,
                    "bank_short_name" => Banks::DFT_BANK_SHORT_NAME,
                    "bank_acc_number" => Banks::DFT_BANK_ACC_NUMBER,
                    "bank_acc_name" => Banks::DFT_BANK_ACC_NAME,
                    "invc_to_client" => OrdersAItems::INVC_TO_CLIENT_YES,
                    "calc_to_vdr" => OrdersAItems::CALC_TO_VDR_NO,
                    "only_client" => OrdersAItems::ONLY_CLIENT_YES,
                    "ref_ord_id" => $ord_id,
                    "ref_ord_code" => $ord_code,
                    "ref_c_termin_id" => $c_termin1_id,
                    "crt" => $now,
                    "crt_by" => $req->auth->uid,
                    "updt" => $now,
                    "updt_by" => $req->auth->uid,
                ]);
            }
            if (
                ($req->sell_termin2_amt_type == OrdersTermins::AMT_TYPE_FLAT ||
                    $req->sell_termin2_amt_type == OrdersTermins::AMT_TYPE_PERCENT) &&
                $req->sell_termin2_amt > 0
            ) {
                ++$c_termin_sequence;
                $insTerminsC2 = [
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "flow_type" => OrdersTermins::FLOW_TYPE_BILLING,
                    "cash_type" => OrdersTermins::TYPE_CASH_IN,
                    "sequence" => $c_termin_sequence,
                    "termin_base_flat" => $req->sell_price,
                    "termin_tax_type" => OrdersAItems::AMT_TAX_TYPE_EXCLUDE,
                    "termin_tax_ppn_percent" => OrdersAItems::PPN_PERCENT,
                    "termin_tax_pph_percent" => OrdersAItems::PPH_PERCENT,
                    "termin_amt_type" => $req->sell_termin2_amt_type ?? 0,
                    "termin_amt" => $req->sell_termin2_amt ?? 0,
                    "termin_calc_percent" => $req->sell_termin2_calc_percent ?? 0,
                    "termin_result_flat" =>
                        $req->sell_termin2_amt_type == OrdersTermins::AMT_TYPE_FLAT
                            ? $req->sell_termin2_amt
                            : $req->sell_termin2_calc_percent,
                    "termin_ddln_at" => $req->sell_termin2_at ?? 0,
                    "termin_ddln_type" => OrdersTermins::DDLN_TERMIN_TYPE_ORD_FINISH,
                    "termin_for" => OrdersTermins::TERMIN_FOR_CLIENT,
                    "crt" => $now,
                    "crt_by" => $req->auth->uid,
                    "crt_type" => OrdersTermins::CRT_TYPE_ADMIN,
                    "updt" => $now,
                    "updt_by" => $req->auth->uid,
                ];
                $c_termin2_id = OrdersTermins::add($insTerminsC2);
                $qty = 1;
                OrdersAItems::add([
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "flow_type" => OrdersAItems::FLOW_TYPE_BILLING,
                    "cash_type" => OrdersAItems::TYPE_CASH_IN,
                    "c_termin_id" => $c_termin2_id,
                    "a_item_type" => OrdersAItems::A_TYPE_PRIMARY,
                    // 'desc' => $main_item_dec . "
Pembayaran termin 2",
                    "desc" => $main_item_dec,
                    "unit_id" => 0,
                    "unit_type" => UnitTypes::UNIT_TYPE_PAKET,
                    "unit_qty" => $qty,
                    "amt_bill_base_flat" => $insTerminsC2["termin_result_flat"],
                    "amt_bill_result_flat" => $insTerminsC2["termin_result_flat"],
                    "amt_bill_total_flat" => $insTerminsC2["termin_result_flat"] * $qty,
                    "ddln_pay_at" => $insTerminsC2["termin_ddln_at"],
                    "ddln_pay_type" => OrdersAItems::DDLN_PAY_TYPE_ORD_FINISH,
                    "img_proof_submission" => null,
                    "img_proof_payment" => null,
                    "bank_id" => Banks::DFT_BANK_ID,
                    "bank_code" => Banks::DFT_BANK_CODE,
                    "bank_name" => Banks::DFT_BANK_NAME,
                    "bank_short_name" => Banks::DFT_BANK_SHORT_NAME,
                    "bank_acc_number" => Banks::DFT_BANK_ACC_NUMBER,
                    "bank_acc_name" => Banks::DFT_BANK_ACC_NAME,
                    "invc_to_client" => OrdersAItems::INVC_TO_CLIENT_YES,
                    "calc_to_vdr" => OrdersAItems::CALC_TO_VDR_NO,
                    "only_client" => OrdersAItems::ONLY_CLIENT_YES,
                    "ref_ord_id" => $ord_id,
                    "ref_ord_code" => $ord_code,
                    "ref_c_termin_id" => $c_termin2_id,
                    "crt" => $now,
                    "crt_by" => $req->auth->uid,
                    "updt" => $now,
                    "updt_by" => $req->auth->uid,
                ]);
            }
            if ($c_termin_sequence === 0) {
                $req->sell_termin1_amt_type = OrdersTermins::AMT_TYPE_FLAT;
                $req->sell_termin1_amt = $req->sell_price;
                $req->sell_termin1_amt_at = strtotime("+2 days", $now);
                $req->sell_termin1_calc_percent = 0;
                ++$c_termin_sequence;
                $insTerminsC1 = [
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "flow_type" => OrdersTermins::FLOW_TYPE_BILLING,
                    "cash_type" => OrdersTermins::TYPE_CASH_IN,
                    "sequence" => $c_termin_sequence,
                    "termin_base_flat" => $req->sell_price,
                    "termin_tax_type" => OrdersAItems::AMT_TAX_TYPE_EXCLUDE,
                    "termin_tax_ppn_percent" => OrdersAItems::PPN_PERCENT,
                    "termin_tax_pph_percent" => OrdersAItems::PPH_PERCENT,
                    "termin_amt_type" => $req->sell_termin1_amt_type ?? 0,
                    "termin_amt" => $req->sell_termin1_amt ?? 0,
                    "termin_calc_percent" => $req->sell_termin1_calc_percent ?? 0,
                    "termin_result_flat" =>
                        $req->sell_termin1_amt_type == OrdersTermins::AMT_TYPE_FLAT
                            ? $req->sell_termin1_amt
                            : $req->sell_termin1_calc_percent,
                    "termin_ddln_at" => $req->sell_termin1_at ?? 0,
                    "termin_ddln_type" => OrdersTermins::DDLN_TERMIN_TYPE_ORD_FINISH,
                    "termin_for" => OrdersTermins::TERMIN_FOR_CLIENT,
                    "crt" => $now,
                    "crt_by" => $req->auth->uid,
                    "crt_type" => OrdersTermins::CRT_TYPE_ADMIN,
                    "updt" => $now,
                    "updt_by" => $req->auth->uid,
                ];
                $c_termin1_id = OrdersTermins::add($insTerminsC1);
                $qty = 1;
                OrdersAItems::add([
                    "ord_id" => $ord_id,
                    "ord_code" => $ord_code,
                    "flow_type" => OrdersAItems::FLOW_TYPE_BILLING,
                    "cash_type" => OrdersAItems::TYPE_CASH_IN,
                    "c_termin_id" => $c_termin1_id,
                    "a_item_type" => OrdersAItems::A_TYPE_PRIMARY,
                    // 'desc' => $main_item_dec . "
Pembayaran termin 1",
                    "desc" => $main_item_dec,
                    "unit_id" => 0,
                    "unit_type" => UnitTypes::UNIT_TYPE_PAKET,
                    "unit_qty" => $qty,
                    "amt_bill_base_flat" => $insTerminsC1["termin_result_flat"],
                    "amt_bill_result_flat" => $insTerminsC1["termin_result_flat"],
                    "amt_bill_total_flat" => $insTerminsC1["termin_result_flat"] * $qty,
                    "ddln_pay_at" => $insTerminsC1["termin_ddln_at"],
                    "ddln_pay_type" => OrdersAItems::DDLN_PAY_TYPE_ORD_FINISH,
                    "img_proof_submission" => null,
                    "img_proof_payment" => null,
                    "bank_id" => Banks::DFT_BANK_ID,
                    "bank_code" => Banks::DFT_BANK_CODE,
                    "bank_name" => Banks::DFT_BANK_NAME,
                    "bank_short_name" => Banks::DFT_BANK_SHORT_NAME,
                    "bank_acc_number" => Banks::DFT_BANK_ACC_NUMBER,
                    "bank_acc_name" => Banks::DFT_BANK_ACC_NAME,
                    "invc_to_client" => OrdersAItems::INVC_TO_CLIENT_YES,
                    "calc_to_vdr" => OrdersAItems::CALC_TO_VDR_NO,
                    "only_client" => OrdersAItems::ONLY_CLIENT_YES,
                    "ref_ord_id" => $ord_id,
                    "ref_ord_code" => $ord_code,
                    "ref_c_termin_id" => $c_termin1_id,
                    "crt" => $now,
                    "crt_by" => $req->auth->uid,
                    "updt" => $now,
                    "updt_by" => $req->auth->uid,
                ]);
            }
            // $url = env("API_URL_NODE") . "/order/create";
            // $guzReq = new GuzzleClient();
            // $respNode = $guzReq->request("POST", $url, [
            //     "headers" => [
            //         "Host" => $_SERVER["SERVER_ADDR"] ?? "127.0.0.1",
            //         "User-Agent" => "curl/7.65.3",
            //         "Accept" => "*/*",
            //         "Accept-Encoding" => "gzip, deflate, br",
            //         // 'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
            //         // 'Connection' => 'keep-alive',
            //     ],
            //     "json" => [
            //         "trx_code" => $ord_code,
            //         "trx_at" => $now,
            //         "client_id" => $clientAuth->uid,
            //         "client_name" => $clientAuth->first_name,
            //         "client_phone" =>
            //             $clientAuth->phone_code . " " . implode(" ", str_split($clientAuth->phone, 4)),
            //         "client_mail" => $clientAuth->email,
            //         "client_addr" => "",
            //         "client_prefer_type_truck" => $type_truck_name,
            //         "pickup_zone_title" => $pckPoints[0]->name,
            //         "pickup_zone_addr" => $pckPoints[0]->fulladdress,
            //         "pickup_at" => $req->points[0]["pickup_at"],
            //         "drop_zone_title" => $dropPoints[0]->name,
            //         "drop_zone_addr" => $dropPoints[0]->fulladdress,
            //         "admins" => $admins_data,
            //     ],
            // ]);
            // if ($respNode->getStatusCode() != 200) {
            //     DB::rollBack();
            //     $apiResp = Responses::bad_request("fail create order 0");
            //     return new Response($apiResp, $apiResp["meta"]["code"]);
            // }
            // $body = json_decode($respNode->getBody()->getContents());
            $apiResp = Responses::created("success create order");
            DB::commit();
            // return new Response($apiResp, $apiResp["meta"]["code"]);
            return redirect("user/vendor/transactions");
        } catch (\Exception $e) {
            DB::rollBack();
            // dd($e);
            $apiResp = Responses::error($e->getMessage());
            return new Response($apiResp, $apiResp["meta"]["code"]);
        }
    }
}