<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Validator;
use Carbon\Carbon;
use App\Responses;
use App\Helper;
use App\Models\Users;
use App\Models\Drivers;
use App\Models\Vehicles;
use App\Models\Devices;
use App\Models\Clients;
use App\Models\ConfRates;
use App\Models\ConfTruckTypes;
use App\Models\Zone;
use App\Models\Banks;
use App\Models\Orders;
use App\Models\OrdersVendors;
use App\Models\OrdersItems;
use App\Models\OrdersAItems;
use App\Models\UnitTypes;
use App\Models\AItems;
use App\Models\OrdersRates;
use App\Models\Finance;
use App\Models\OrdersCheckpoints;
use App\Models\OrdersInvoices;
use App\Models\OrdersDriversUploads;
use App\Models\UserLogs;
use Auth;	

class MenuController extends Controller
{
    public function view_dashboard(Request $req)
    {
		$log = [
			"module" => "Dashboard",
			"action" => "View",
			"desc" => "Open Dashboard menu",
		];
		UserLogs::insert($req->auth->uid, $log);
    
		$data = [
            "client_group" => Clients::getClientById($req->auth->client_group_id),
        ];

        if (count($data["client_group"]) > 0) {
            $data["client_group"] = $data["client_group"][0];
        } else {
            $data["client_group"] = null;
        }


        return view("menu_v1.dashboard", $data);
    }

    public function view_drivers(Request $req)
    {
		$log = [
			"module" => "Driver",
			"action" => "View",
			"desc" => "Open Driver menu",
		];
		UserLogs::insert($req->auth->uid, $log);

		$data = [
            "bloods" => Helper::listBloods(),
            "relationships" => Drivers::listRelationships(),
            "vendors" => Users::listUsersByRole(Users::ROLE_VENDOR),
            "banks" => Banks::listBanks(["is_active" => Banks::IS_ACTIVE]),
        ];
        return view("menu_v1.drivers", $data);
    }

    public function view_vehicles(Request $req)
    {
		$log = [
			"module" => "Vehicle",
			"action" => "View",
			"desc" => "Open Vehicle menu",
		];
		UserLogs::insert($req->auth->uid, $log);

		$listPool = DB::select("SELECT * FROM t_conf_pool WHERE dlt IS NULL ORDER BY pool_code ASC");
		$listDistribution = DB::select("SELECT * FROM t_conf_distribution_category WHERE dlt IS NULL ORDER BY dc_code ASC");

		$data = [
            // 'cats' => Vehicles::listCats(), // default Truck
            "brands" => Vehicles::listBrands(),
            "types" => Vehicles::listTypes(),
            "drivers" => Drivers::getDrivers($req->auth),
            "vendors" => Users::listUsersByRole(Users::ROLE_VENDOR),
            "devices" => Devices::listDevices([
                "is_active" => Devices::IS_ACTIVE,
                "type" => Devices::TYPE_BUILT_IN,
                "is_idle_yes" => 1,
                "is_available" => Devices::IS_AVAIL,
            ]),
			"listPool" => $listPool,
			"listDistribution" => $listDistribution
        ];
        // dd($data);
        return view("menu_v1.vehicles", $data);
    }

    /**
     * ADMIN TRX
     */

    public function view_transactions()
    {
		$log = [
			"module" => "Transactions",
			"action" => "View",
			"desc" => "Open Transactions menu",
		];
		UserLogs::insert(Auth::user()->id, $log);

        $data = [
            "availOrdToMerge" => Finance::availOrdToMerge(),
        ];
        return view("menu_v1.transactions", $data);
    }
    public function view_transactions_view__bak(Request $req)
    {
        $codes = explode(",", $req->code);
        $limit = count($codes);
        if ($limit > 2) {
            return redirect(route("view_transactions"));
        }
        // dd($codes[0]);
        $orders = Orders::showOrder([
            "codes" => $codes,
            "get_stts_checker" => 1,
            "get_checker_data" => 1,
            "get_checker_user" => 1,
            // 'get_prefer_type_truck' => 1,
            "get_additional_vehicles_info" => 1,
            "get_accidents" => 1,
            "couple_pck_drop" => 1,
            "get_user_aprv_pck" => 1,
            "group_by" => "ord.id",
            "limit" => $limit,
        ]);
        // dd($orders);
        if (count($orders) < 1) {
            return redirect(route("view_transactions"));
        }

        // uang saku / pocket money
        $checkpoints = OrdersCheckpoints::listCheckpoints([
            "ord_id" => $orders[0]->ord_id,
        ]);

        $bladeViewOrders = [];
        foreach ($orders as $iOrd => $order) {
            // get multiple point (pick,drop)
            $adtPoints = Orders::getPoints([
                "id" => $order->ord_id,
                "limit" => $order->drop_total,
            ]);
            // hilangkan index 0, karena yang didapatkan adalah childnya
            array_splice($adtPoints, 0, 1);
            $orders[$iOrd]->adtPoints = $adtPoints;

            // uang saku / pocket money
            $orders[$iOrd]->checkpoints = $checkpoints;

            // formated for blade views
            $tmpBladeView = clone $order;
            if ($order->ord_pck_docs_client_img) {
                $tmpBladeView->ord_pck_docs_client_img = json_decode($order->ord_pck_docs_client_img);
            }
            if ($order->ord_acdnt_imgs) {
                $tmpBladeView->ord_acdnt_imgs = json_decode($order->ord_acdnt_imgs);
            }
            $bladeViewOrders[] = clone $tmpBladeView;
        }
        $drvs_ups = OrdersDriversUploads::list([
            "ord_id" => $orders[0]->ord_id,
            "pck_id" => $orders[0]->ord_pck_id,
            "drop_id" => $orders[0]->ord_drop_id,
            "ord_pck_drop_id" => $orders[0]->ord_pck_drop_id,
        ]);
        $data = [
            "orders" => $orders,
            "bladeViewOrders" => $bladeViewOrders,
            "drvs_ups" => $drvs_ups,
            "items" => AItems::listAItems([
                "is_active" => AItems::IS_ACTIVE,
                "is_adm_price" => AItems::IS_ADM_PRICE_NO,
                "crt_type" => AItems::CRT_TYPE_ADMIN,
            ]),
            "unitTypes" => UnitTypes::listUnitTypes([
                "is_active" => UnitTypes::IS_ACTIVE,
                "is_publish" => UnitTypes::IS_PUBLISH,
            ]),
            "banks" => Banks::listBanks(["is_active" => Banks::IS_ACTIVE]),
        ];
        return view("menu_v1._viewTransactions", $data);
    }
    public function view_transactions_view(Request $req)
    {
		$log = [
			"module" => "Transactions",
			"action" => "View",
			"desc" => "Open Transactions menu",
		];
		UserLogs::insert($req->auth->uid, $log);


        $codes = explode(",", $req->code);
        $limit = count($codes);
        if ($limit > 2) {
            return redirect(route("view_transactions"));
        }
        $orders = Orders::showOrder([
            "codes" => $codes,
            "get_stts_checker" => 1,
            "get_checker_data" => 1,
            "get_checker_user" => 1,
            // 'get_prefer_type_truck' => 1,
            "get_additional_vehicles_info" => 1,
            "get_accidents" => 1,
            "group_by" => "ord.id",
            "limit" => $limit,
        ]);

        if (count($orders) < 1) {
            return redirect(route("view_transactions"));
        }
        $bladeViewOrders = [];
        foreach ($orders as $iOrd => $order) {
            // get multiple point (pick,drop)
            $adtPoints = Orders::getPoints([
                "id" => $order->ord_id,
                "limit" => $order->drop_total,
            ]);
            // hilangkan index 0, karena yang didapatkan adalah childnya
            array_splice($adtPoints, 0, 1);
            $orders[$iOrd]->adtPoints = $adtPoints;
            // dd($adtPoints);
            // formated for blade views
            $tmpBladeView = clone $order;
            if ($order->ord_pck_docs_client_img) {
                $tmpBladeView->ord_pck_docs_client_img = json_decode($order->ord_pck_docs_client_img);
            }
            if ($order->ord_acdnt_imgs) {
                $tmpBladeView->ord_acdnt_imgs = json_decode($order->ord_acdnt_imgs);
            }
            $bladeViewOrders[] = clone $tmpBladeView;
        }

        $rawData = DB::table("t_orders_pck_drop as topd")
            ->leftJoin("t_orders_pickups as top", "top.id", "topd.pck_id")
            ->leftJoin("t_orders_drops as tod", "tod.id", "topd.drop_id")
            ->where("topd.ord_code", $codes)
            ->selectRaw(
                "topd.id,
                FROM_UNIXTIME(top.set_pck_at, '%d %M %Y') as pck_date,
                FROM_UNIXTIME(top.set_pck_at, '%H:%i:%s') as pck_time,
                top.pck_name,
                top.pck_addr,
                tod.drop_name,
                tod.drop_addr"
            )
            ->get();
        foreach ($rawData as $_data) {
            // $ehck = DB::table("t_orders_drivers_uploads")
            //     ->select(
            //         "id", //
            //         "checklist_name",
            //         "checklist_desc",
            //         "img",
            //         "updt"
            //     )
            //     ->where("ord_pck_drop_id", $_data->id)
            //     ->get();
            $data[] = [
                "id" => $_data->id,
                "pck_date" => $_data->pck_date,
                "pck_time" => $_data->pck_time,
                "pck_name" => $_data->pck_name,
                "pck_addr" => $_data->pck_addr,
                "drop_name" => $_data->drop_name,
                "drop_addr" => $_data->drop_addr,
                "checklist_array" => [],
            ];
        }
        $data = [
            "data" => $data,
            "orders" => $orders,
            "bladeViewOrders" => $bladeViewOrders,
        ];
        // dd($data);
        return view("menu_v1._viewTransactions", $data);
    }
    public function view_transactions_view_bak_single(Request $req)
    {
        $codes = explode(",", $req->code);
        $order = Orders::showOrder([
            "code" => $codes[0],
            "get_stts_checker" => 1,
            "get_checker_data" => 1,
            "get_checker_user" => 1,
            // 'get_prefer_type_truck' => 1,
            "get_additional_vehicles_info" => 1,
            "get_accidents" => 1,
        ]);
        if (count($order) < 1) {
            return redirect(route("view_transactions"));
        }
        $cantConfirmPrice = [Orders::STTS_WAIT, Orders::STTS_CONFIRM, Orders::STTS_HAVE_GET_VHC];
        $show_confirm_price = 1;
        if (in_array($order[0]->status, $cantConfirmPrice)) {
            $show_confirm_price = 0;
        }
        $adtPoints = Orders::getPoints([
            "id" => $order[0]->ord_id,
            "limit" => $order[0]->drop_total,
        ]);
        array_splice($adtPoints, 0, 1);
        $data = [
            "order" => $order[0],
            "adtPoints" => $adtPoints,
            "adtPointsJson" => json_encode($adtPoints),
            "items" => AItems::listAItems([
                "is_active" => AItems::IS_ACTIVE,
                "is_adm_price" => AItems::IS_ADM_PRICE_NO,
                "crt_type" => AItems::CRT_TYPE_ADMIN,
            ]),
            "unitTypes" => UnitTypes::listUnitTypes([
                "is_active" => UnitTypes::IS_ACTIVE,
                "is_publish" => UnitTypes::IS_PUBLISH,
            ]),
            "banks" => Banks::listBanks(["is_active" => Banks::IS_ACTIVE]),
        ];
        return view("menu_v1._viewTransactions", $data);
    }
    public function view_transactions_confirm(Request $req)
    {
        $order = Orders::showOrder([
            "code" => $req->code,
            "center_pck" => 1,
            "get_prefer_type_truck" => 1,
        ]);
        if (count($order) < 1) {
            return redirect(route("view_transactions"));
        }
        if (isset($order[0]->pck_center)) {
            $split = explode(" ", $order[0]->pck_center);
            $order[0]->pck_center_lat = substr($split[1], 0, -1);
            $order[0]->pck_center_lng = substr($split[0], 6);
        }
        $rate = OrdersRates::getById($order[0]->ord_rate_id);
        $data = [
            "order" => $order[0],
            // "vendors" => OrdersVendors::searchVendorsByRate([
            //     "active_rates" => $rate[0],
            //     "prefer_truck_type" => $order[0]->prefer_truck_type,
            // ]),
        ];
        // dd($data["order"]);
        return view("menu_v1._confirmTransactions", $data);
    }

    /**
     * ADMIN CONFIG
     */

    public function view_logs_gps()
    {
		$log = [
			"module" => "Logs GPS",
			"action" => "View",
			"desc" => "Open Logs GPS menu",
		];
		UserLogs::insert(Auth::user()->id, $log);

        return view("menu_v1.configs.index_logs_gps");
    }

    /**
     * CLIENT TRX
     */

    public function view_transactions_add(Request $req)
    {
        // dd(Users::ROLE_ADMIN);
        $uclients = Users::listUsers([
            "role" => Users::ROLE_ADMIN,
            "status" => Users::STATUS_ACTIVE,
        ]);
        // dd($uclients);
        $data = [
            "uclients" => [collect($uclients)->firstWhere("id", 1)],
            "truck_types" => ConfTruckTypes::listTruckTypesRates(
                ConfTruckTypes::IS_ACTIVE,
                ConfRates::LANE_EARTH
            ),
            "vehicle" => Vehicles::listVehicles([]),
            "driver" => Drivers::getDrivers([]),
        ];
        // dd($data["vehicle"]);
        return view("menu_v2.Clients._addTransactions", $data);
    }

    public function view_user_client_transaction()
    {
        return view("menu_v2.Clients.transactions");
    }
    public function view_user_client_transaction_view(Request $req)
    {
        $order = Orders::showOrder([
            "code" => $req->code,
            "get_stts_checker" => 1,
            // 'get_prefer_type_truck' => 1,
            "get_additional_vehicles_info" => 1,
            "get_accidents" => 1,
        ]);
        if (count($order) < 1) {
            return redirect(route("view_user_client_transaction"));
        }
        $adtPoints = Orders::getPoints([
            "id" => $order[0]->ord_id,
            "limit" => $order[0]->drop_total,
        ]);
        array_splice($adtPoints, 0, 1);
        $data = [
            "order" => $order[0],
            "adtPoints" => $adtPoints,
            "adtPointsJson" => json_encode($adtPoints),
        ];
        return view("menu_v2.Clients._viewTransactions", $data);
    }
    public function view_user_client_transaction_add(Request $req)
    {
        $client_group_id = $req->auth->client_group_id;

        // if ($req->rdl == 1) {
        //     // $inActive = Orders::getOrdersClientActive($req->auth->uid);
        //     $haveOrder = Orders::getOrdersByClient($req->auth->uid, 1);
        //     if (count($haveOrder) > 0) {
        //         return redirect(route('view_user_client_transaction'));
        //     }
        // }

        $data = [
            "pickups" => Zone::getActiveZones($client_group_id, 0, Zone::ZONE_WORKFLOW_PICKUP),
            "drops" => Zone::getActiveZones($client_group_id, 0, Zone::ZONE_WORKFLOW_DEST),
            "truck_types" => ConfTruckTypes::listTruckTypesRates(
                ConfTruckTypes::IS_ACTIVE,
                ConfRates::LANE_EARTH
            ),
        ];
        return view("menu_v2.Clients._addTransactions", $data);
    }
    // public function view_user_client() {
    //     return view('menu_v2.Clients._addTransactions');
    // }

    /**
     * VENDOR TRX
     */

    public function view_user_vendor_transaction()
    {
        return view("menu_v2.Vendors.transactions");
    }
    public function view_user_vendor_transaction_view(Request $req)
    {
        $codes = explode(",", $req->code);
        $limit = count($codes);
        if ($limit > 2) {
            return redirect(route("view_transactions"));
        }
        $orders = Orders::showOrder([
            "codes" => $codes,
            "get_stts_checker" => 1,
            "get_checker_data" => 1,
            "get_checker_user" => 1,
            // 'get_prefer_type_truck' => 1,
            "get_additional_vehicles_info" => 1,
            "get_accidents" => 1,
            "group_by" => "ord.id",
            "limit" => $limit,
        ]);

        if (count($orders) < 1) {
            return redirect(route("view_transactions"));
        }
        $bladeViewOrders = [];
        foreach ($orders as $iOrd => $order) {
            // get multiple point (pick,drop)
            $adtPoints = Orders::getPoints([
                "id" => $order->ord_id,
                "limit" => $order->drop_total,
            ]);
            // hilangkan index 0, karena yang didapatkan adalah childnya
            array_splice($adtPoints, 0, 1);
            $orders[$iOrd]->adtPoints = $adtPoints;
            // dd($adtPoints);
            // formated for blade views
            $tmpBladeView = clone $order;
            if ($order->ord_pck_docs_client_img) {
                $tmpBladeView->ord_pck_docs_client_img = json_decode($order->ord_pck_docs_client_img);
            }
            if ($order->ord_acdnt_imgs) {
                $tmpBladeView->ord_acdnt_imgs = json_decode($order->ord_acdnt_imgs);
            }
            $bladeViewOrders[] = clone $tmpBladeView;
        }

        $rawData = DB::table("t_orders_pck_drop as topd")
            ->leftJoin("t_orders_pickups as top", "top.id", "topd.pck_id")
            ->leftJoin("t_orders_drops as tod", "tod.id", "topd.drop_id")
            ->where("topd.ord_code", $codes)
            ->selectRaw(
                "topd.id,
                FROM_UNIXTIME(top.set_pck_at, '%d %M %Y') as pck_date,
                FROM_UNIXTIME(top.set_pck_at, '%H:%i:%s') as pck_time,
                top.pck_name,
                top.pck_addr,
                tod.drop_name,
                tod.drop_addr"
            )
            ->get();
        foreach ($rawData as $_data) {
            $ehck = DB::table("t_orders_drivers_uploads")
                ->select(
                    "id", //
                    "checklist_name",
                    "checklist_desc",
                    "img",
                    "updt"
                )
                ->where("ord_pck_drop_id", $_data->id)
                ->get();
            $data[] = [
                "id" => $_data->id,
                "pck_date" => $_data->pck_date,
                "pck_time" => $_data->pck_time,
                "pck_name" => $_data->pck_name,
                "pck_addr" => $_data->pck_addr,
                "drop_name" => $_data->drop_name,
                "drop_addr" => $_data->drop_addr,
                "checklist_array" => $ehck,
            ];
        }
        // dd($data[0]["checklist_array"]);
        // dd($data);
        $data = [
            "data" => $data,
            "orders" => $orders,
            "bladeViewOrders" => $bladeViewOrders,
        ];
        // dd($data);
        return view("menu_v2.Vendors._viewTransactions", $data);
    }
    public function view_user_vendor_transaction_view_bak_single(Request $req)
    {
        $order = Orders::showOrder([
            "code" => $req->code,
        ]);
        if (count($order) < 1) {
            return redirect(route("view_transactions"));
        }
        $adtPoints = Orders::getPoints([
            "id" => $order[0]->ord_id,
            "limit" => $order[0]->drop_total,
        ]);
        array_splice($adtPoints, 0, 1);
        $data = [
            "order" => $order[0],
            "adtPoints" => $adtPoints,
            "adtPointsJson" => json_encode($adtPoints),
        ];
        return view("menu_v2.Vendors._viewTransactions", $data);
    }
    public function view_user_vendor_transaction_new_order(Request $req)
    {
        // dd($req->code);
        $order = Orders::showOrder([
            "code" => $req->code,
            "get_exp_vendor" => 1,
            "vdr_id" => $req->auth->uid,
            "get_bid_info" => 1,
        ]);
        // dd($order);
        // if (count($order) < 1) {
        //     return redirect(route("view_user_vendor_transaction"));
        // }
        // if ($order[0]->vdr_status !== OrdersVendors::STTS_WAIT) {
        //     return redirect(route("view_user_vendor_transaction"));
        // }
        // if (
        //     $order[0]->status === Orders::STTS_WAIT ||
        //     $order[0]->status === Orders::STTS_CONFIRM
        // ) {
        // } else {
        //     return redirect(route("view_user_vendor_transaction"));
        // }
        // // date('Y-m-d H:i:s', $order[0]->vdr_exp_at);
        // if ($order[0]->vdr_is_exp === OrdersVendors::LINK_WILL_EXP) {
        //     if ($order[0]->vdr_exp_at < time()) {
        //         return redirect(route("view_user_vendor_transaction"));
        //     }
        // }
        // if ($order[0]->is_mailing_bid != OrdersVendors::IS_MAILING_BID_SEND) {
        //     return redirect(route("view_user_vendor_transaction"));
        // }
        $haveBeenAcc = OrdersVendors::getByOrdIdByStatus($order[0]->ord_id, OrdersVendors::STTS_ACC);
        if (count($haveBeenAcc) > 0) {
            return redirect(route("view_user_vendor_transaction"));
        }
        $vehicles = Vehicles::getVehiclesInIdsActiveNoInOrder($order[0]->find_vhcs);
        // $drivers = Drivers::getDriversNoInOrder($req->auth, [
        //     'status' => Drivers::STTS_ACTIVE
        // ]);
        $drivers = Drivers::getDriversNoInOrderNew($req->auth, [
            "status" => Drivers::STTS_ACTIVE,
        ]);

        $data = [
            "order" => $order[0],
            "vehicles" => $vehicles,
            "drivers" => $drivers,
        ];
        return view("menu_v2.Vendors._newOrder", $data);
    }

    /**
     * CHECKER TRX
     */

    // public function view_user_checker()
    // {
    //     return view('menu_v2.Checker.index');
    // }
    // public function view_user_checker_view(Request $req)
    // {
    //     $order = Orders::showOrder([
    //         'code' => $req->code,
    //         'get_stts_checker' => 1,
    //     ]);
    //     if (count($order) < 1) {
    //         return redirect(route('view_user_checker'));
    //     }
    //     $filter = [
    //         'get_not_deleted' => 1,
    //     ];
    //     $items = OrdersItems::getsByOrdId($order[0]->ord_id, $filter);
    //     $data = [
    //         'order' => $order[0],
    //         'items' => $items,
    //     ];
    //     return view('menu_v2.Checker._view', $data);
    // }

    public function view_user_checker()
    {
        return view("menu_v2.Checker.index");
    }
    public function view_user_checker_view(Request $req)
    {
        $data = [];
        $orders = Orders::showOrder([
            "code" => $req->code,
            "get_stts_checker" => 1,
            "get_prefer_type_truck" => 1,
            "get_checker_data" => 1,
            "get_client_pt" => 1,
            "get_accidents" => 1,
            "couple_pck_drop" => 1,
            "get_user_aprv_pck" => 1,
            "get_user_aprv_pck" => 1,
            "join_pockets" => 1,
            "group_by" => "ord.id",
        ]);
        if (count($orders) < 1) {
            return redirect(route("view_user_checker"));
        }
        $orderOne = clone $orders[0];
        if ($orders[0]->group_merge_code) {
            $orders = Orders::showOrder([
                "group_merge_code" => $orders[0]->group_merge_code,
                "get_stts_checker" => 1,
                "get_prefer_type_truck" => 1,
                "get_checker_data" => 1,
                "get_client_pt" => 1,
                "get_accidents" => 1,
                "couple_pck_drop" => 1,
                "get_user_aprv_pck" => 1,
                "join_pockets" => 1,
                "group_by" => "ord.id",
                "limit" => 2,
            ]);
        }

        // uang saku / pocket money
        $checkpoints = OrdersCheckpoints::listCheckpoints([
            "ord_id" => $orders[0]->ord_id,
        ]);

        $truck_types = ConfTruckTypes::listTruckTypes(ConfTruckTypes::IS_ACTIVE, [
            "is_publish" => ConfTruckTypes::IS_PUBLISH,
        ]);
        $bladeViewOrders = [];
        foreach ($orders as $iOrd => $order) {
            // get multiple point (pick,drop)
            $adtPoints = Orders::getPoints([
                "id" => $order->ord_id,
                "limit" => $order->drop_total,
            ]);
            // hilangkan index 0, karena yang didapatkan adalah childnya
            array_splice($adtPoints, 0, 1);
            $orders[$iOrd]->adtPoints = $adtPoints;

            // uang saku / pocket money
            $orders[$iOrd]->checkpoints = $checkpoints;

            // formated for blade views
            $tmpBladeView = clone $order;
            if ($order->ord_pck_docs_client_img) {
                $tmpBladeView->ord_pck_docs_client_img = json_decode($order->ord_pck_docs_client_img);
            }
            if ($order->ord_acdnt_imgs) {
                $tmpBladeView->ord_acdnt_imgs = json_decode($order->ord_acdnt_imgs);
            }
            $bladeViewOrders[] = clone $tmpBladeView;

            // special case for checker, karena meskipun merge tapi tampilnya tetap 1-1 per trx
            if ($req->code === $order->ord_code) {
                $orderOne = clone $order;
            }
        }

        $drvs_ups = OrdersDriversUploads::list([
            "ord_id" => $orders[0]->ord_id,
            "pck_id" => $orders[0]->ord_pck_id,
            "drop_id" => $orders[0]->ord_drop_id,
            "ord_pck_drop_id" => $orders[0]->ord_pck_drop_id,
        ]);

        $data = [
            "orders" => $orders,
            "drvs_ups" => $drvs_ups,
            "order" => $orderOne,
            "truck_types" => $truck_types,
            "adtPoints" => $orders[0]->adtPoints,
            "adtPointsJson" => json_encode($orders[0]->adtPoints),
        ];
        return view("menu_v2.Checker._view", $data);
    }

    /**
     * FINANCE
     */
    public function view_keuangan_payment()
    {
        return view("menu_v2.Finance.payment");
    }
    public function view_keuangan_payment_view(Request $req)
    {
        $codes = explode(",", $req->code);
        $limit = count($codes);
        if ($limit > 2) {
            return redirect(route("view_transactions"));
        }
        $orders = Orders::showOrder([
            "codes" => $codes,
            // 'get_prefer_type_truck' => 1,
            "get_additional_vehicles_info" => 1,
            "get_pic_zone" => 1,
            // 'get_stts_checker' => 1,
            // 'get_checker_data' => 1,
            // 'get_checker_user' => 1,
            // 'get_accidents' => 1,
            "couple_pck_drop" => 1,
            "get_user_aprv_pck" => 1,
            "group_by" => "ord.id",
            "limit" => $limit,
        ]);
        if (count($orders) < 1) {
            return redirect(route("view_keuangan_payment"));
        }
        $bladeViewOrders = [];
        foreach ($orders as $iOrd => $order) {
            // get multiple point (pick,drop)
            $adtPoints = Orders::getPoints([
                "id" => $order->ord_id,
                "limit" => $order->drop_total,
            ]);
            // hilangkan index 0, karena yang didapatkan adalah childnya
            array_splice($adtPoints, 0, 1);
            $orders[$iOrd]->adtPoints = $adtPoints;

            // formated for blade views
            $tmpBladeView = clone $order;
            // if ($order->ord_pck_docs_client_img) $tmpBladeView->ord_pck_docs_client_img = json_decode($order->ord_pck_docs_client_img);
            // if ($order->ord_acdnt_imgs) $tmpBladeView->ord_acdnt_imgs = json_decode($order->ord_acdnt_imgs);
            $bladeViewOrders[] = clone $tmpBladeView;
        }

        $drvs_ups = OrdersDriversUploads::list([
            "ord_id" => $orders[0]->ord_id,
            "pck_id" => $orders[0]->ord_pck_id,
            "drop_id" => $orders[0]->ord_drop_id,
            "ord_pck_drop_id" => $orders[0]->ord_pck_drop_id,
        ]);

        $data = [
            "orders" => $orders,
            "bladeViewOrders" => $bladeViewOrders,
            "drvs_ups" => $drvs_ups,
            "items" => AItems::listAItems([
                "is_active" => AItems::IS_ACTIVE,
                "crt_type" => AItems::CRT_TYPE_FINANCE,
            ]),
            "unitTypes" => UnitTypes::listUnitTypes([
                "is_active" => UnitTypes::IS_ACTIVE,
                "is_publish" => UnitTypes::IS_PUBLISH,
            ]),
            "banks" => Banks::listBanks(["is_active" => Banks::IS_ACTIVE]),
            "availOrdToMerge" => Finance::availOrdToMerge([
                "except_ord_id" => $orders[0]->ord_id,
            ]),
        ];
        return view("menu_v2.Finance._viewPayment", $data);
    }
    public function view_keuangan_payment_view_single_bak(Request $req)
    {
        $order = Orders::showOrder([
            "code" => $req->code,
            // 'get_prefer_type_truck' => 1,
            "get_additional_vehicles_info" => 1,
            "get_pic_zone" => 1,
            // 'get_stts_checker' => 1,
            // 'get_checker_data' => 1,
            // 'get_checker_user' => 1,
            // 'get_accidents' => 1,
        ]);
        if (count($order) < 1) {
            return redirect(route("view_keuangan_payment"));
        }
        $adtPoints = Orders::getPoints([
            "id" => $order[0]->ord_id,
            "limit" => $order[0]->drop_total,
        ]);
        array_splice($adtPoints, 0, 1);

        $data = [
            "order" => $order[0],
            "adtPoints" => $adtPoints,
            "adtPointsJson" => json_encode($adtPoints),
            "items" => AItems::listAItems([
                "is_active" => AItems::IS_ACTIVE,
                "crt_type" => AItems::CRT_TYPE_FINANCE,
            ]),
            "unitTypes" => UnitTypes::listUnitTypes([
                "is_active" => UnitTypes::IS_ACTIVE,
                "is_publish" => UnitTypes::IS_PUBLISH,
            ]),
            "banks" => Banks::listBanks(["is_active" => Banks::IS_ACTIVE]),
            "availOrdToMerge" => Finance::availOrdToMerge([
                "except_ord_id" => $order[0]->ord_id,
            ]),
        ];
        return view("menu_v2.Finance._viewPayment", $data);
    }
    public function view_keuangan_billing()
    {
        return view("menu_v2.Finance.billing");
    }
    public function view_keuangan_billing_view(Request $req)
    {
        $order = Orders::showOrder([
            "code" => $req->code,
            // 'get_prefer_type_truck' => 1,
            "get_additional_vehicles_info" => 1,
            "get_pic_zone" => 1,
            // 'get_stts_checker' => 1,
            // 'get_checker_data' => 1,
            // 'get_checker_user' => 1,
            // 'get_accidents' => 1,
            "get_client_pt" => 1,
            "couple_pck_drop" => 1,
            "get_user_aprv_pck" => 1,
        ]);
        if (count($order) < 1) {
            return redirect(route("view_keuangan_billing"));
        }

        $a_items = Finance::listAdtItemsBillings([
            "ord_id" => $order[0]->ord_id,
            "group_by" => "ord_a_item.id",
            "is_bill_aprv" => OrdersAItems::IS_APRV_YES,
        ]);
        $group_items_by_termin = [];
        foreach ($a_items as $i => $row) {
            if (!isset($group_items_by_termin[$row->c_group_termin_id])) {
                if (
                    $row->a_item_type === OrdersAItems::A_TYPE_PRIMARY &&
                    $row->stts_merge !== OrdersAItems::STTS_MERGE_RESULT
                ) {
                    $row->previous_termin = OrdersAItems::showAItem([
                        "ord_id" => $row->ord_id,
                        "is_active" => OrdersAItems::IS_ACTIVE_YES,
                        "a_item_type" => OrdersAItems::A_TYPE_PRIMARY,
                        "prev_main_item_id" => $row->ord_a_item_id,
                        "c_termin_id_not_zero" => 1,
                        "limit" => 1,
                    ]);
                }
                $group_items_by_termin[$row->c_group_termin_id] = [];
                $group_items_by_termin[$row->c_group_termin_id][] = $row;
                continue;
            }
            $group_items_by_termin[$row->c_group_termin_id][] = $row;
        }
        $adtPoints = Orders::getPoints([
            "id" => $order[0]->ord_id,
            "limit" => $order[0]->drop_total,
        ]);
        array_splice($adtPoints, 0, 1);

        $drvs_ups = OrdersDriversUploads::list([
            "ord_id" => $order[0]->ord_id,
            "pck_id" => $order[0]->ord_pck_id,
            "drop_id" => $order[0]->ord_drop_id,
            "ord_pck_drop_id" => $order[0]->ord_pck_drop_id,
        ]);

        $data = [
            "order" => $order[0],
            "adtPoints" => $adtPoints,
            "adtPointsJson" => json_encode($adtPoints),
            "drvs_ups" => $drvs_ups,
            "items" => AItems::listAItems([
                "is_active" => AItems::IS_ACTIVE,
                "crt_type" => AItems::CRT_TYPE_FINANCE,
            ]),
            "unitTypes" => UnitTypes::listUnitTypes([
                "is_active" => UnitTypes::IS_ACTIVE,
                "is_publish" => UnitTypes::IS_PUBLISH,
            ]),
            "banks" => Banks::listBanks(["is_active" => Banks::IS_ACTIVE]),
            "group_items_by_termin" => $group_items_by_termin,
        ];
        return view("menu_v2.Finance._viewBilling", $data);
    }
}
