<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class OrdersAItems extends Model
{
	// t_orders_a_items fungsinya untuk nalangin pembayaran

    const FLOW_TYPE_PAYMENT = 1;
    const FLOW_TYPE_BILLING = 2;
    const FLOW_TYPE_HYBRID = 3;

    const TYPE_CASH_OUT = 1;
    const TYPE_CASH_IN = 2;
    const TYPE_CASH_HYBRID = 3;

	const A_TYPE_PRIMARY = 1;
	const A_TYPE_SECONDARY = 2;
    
	const AMT_TYPE_FLAT = 1;
	const AMT_TYPE_PERCENT = 2;

	// 1=>refer ddln_pay_at, 2=>refer to order finish(pengantaran selesai)
	const DDLN_PAY_TYPE_TIME = 1;
	const DDLN_PAY_TYPE_ORD_FINISH = 2;

    const IS_PAID_NO = 0;
    const IS_PAID_YES = 1;

    const IS_HIDDEN_NO = 0;
    const IS_HIDDEN_YES = 1;

    // tax yang berlaku utk 1 row saja
    const IS_TAX_NO = 0;
    const IS_TAX_YES = 1;

    const IS_DISC_NO = 0;
    const IS_DISC_YES = 1;

    const IS_SUBTRACT_NO = 0;
    const IS_SUBTRACT_YES = 1;

	// 1=>transfer
	const PAID_TYPE_TF = 1;

	const INVC_TO_CLIENT_YES = 1;
	const INVC_TO_CLIENT_NO = 2;

	const CALC_TO_VDR_YES = 1;
	const CALC_TO_VDR_NO = 2;

	const ONLY_CLIENT_YES = 1;
	const ONLY_CLIENT_NO = 2;

	const ONLY_VDR_YES = 1;
	const ONLY_VDR_NO = 2;

    const IS_ADM_PRICE_NO = 0;
    const IS_ADM_PRICE_YES = 1;

    const IS_APRV_NO = 0;
    const IS_APRV_YES = 1;

    const IS_ACTIVE_NO = 0;
    const IS_ACTIVE_YES = 1;

    // tax yang include dengan itemnya
    const AMT_TAX_TYPE_WITHOUT = 0; // tanpa pajak
    const AMT_TAX_TYPE_INCLUDE = 1; // termasuk pajak || pajak dibayarkan sendiri oleh (client/vendor)
    const AMT_TAX_TYPE_EXCLUDE = 2; // belum termasuk pajak || pajak dibayarkan oleh bonceng
    const PPN_PERCENT = 1.1;
    const PPN_PERCENT_INCLUDE = 1.011;
    const PPH_PERCENT = 2;

    // merge satuan
    const IS_MERGE_NO = 0;
    const IS_MERGE_YES = 1;

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

    // 0=>system, 1=>admin, 2=>finance
    const CRT_TYPE_SYSTEM = 0;
    const CRT_TYPE_ADMIN = 1;
    const CRT_TYPE_FINANCE = 2;

    /**
     * termin_at itu deadline pembayaran
     * column merge disini untuk merge satuan:
     *      is_merge_to, merge_to_a_id, merge_to_ord_id, merge_to_ord_code, merge_to_at, merge_to_by
     *      is_merge_from, merge_from_a_id, merge_from_ord_id, merge_from_ord_code, merge_from_at, merge_from_by
     * column merge disini untuk merge per trx:
     *      stts_merge, merge_to_code, group_merge_code, merge_at
     */

    public static function listAItems($filter = [])
    {
        $params = [];
        $select = '';
        $where = '';

        if (isset($filter['c_termin_id'])) {
            $where .= ' AND ord_a_item.c_termin_id = ?';
            $params[] = $filter['c_termin_id'];
        }

        if (isset($filter['is_hidden'])) {
            $where .= ' AND ord_a_item.is_hidden = ?';
            $params[] = $filter['is_hidden'];
        }

        if (isset($filter['is_tax'])) {
            $where .= ' AND ord_a_item.is_tax = ?';
            $params[] = $filter['is_tax'];
        }

        if (isset($filter['is_ppn'])) {
            $where .= ' AND ord_a_item.is_ppn = ?';
            $params[] = $filter['is_ppn'];
        }

        if (isset($filter['is_pph'])) {
            $where .= ' AND ord_a_item.is_pph = ?';
            $params[] = $filter['is_pph'];
        }

        if (isset($filter['is_disc'])) {
            $where .= ' AND ord_a_item.is_disc = ?';
            $params[] = $filter['is_disc'];
        }

        return DB::select("SELECT
        ord_a_item.id as ord_a_item_id
        ,ord_a_item.*
        $select
        FROM
        t_orders_a_items as ord_a_item
        WHERE dlt is null
        $where
        ;",
        $params);
    }

    public static function get()
    {
        return DB::select("SELECT * FROM t_orders_a_items;");
    }

    public static function showAItem($filter = [])
    {
        $params = [];
        $join = '';
        $select = '';
        $where = '';
        $group_by = '';
        $limit = '';

        if (isset($filter['ord_a_item_id'])) {
            $where .= ' AND ord_a_item.id = ?';
            $params[] = $filter['ord_a_item_id'];
        } else if (isset($filter['group_merge_code'])) {
            $where .= ' AND ord_a_item.group_merge_code IN (?)';
            $params[] = $filter['group_merge_code'];
        }

        if (isset($filter['ord_id'])) {
            $where .= ' AND ord_a_item.ord_id = ?';
            $params[] = $filter['ord_id'];
        }

        if (isset($filter['exclude_ord_a_item_id'])) {
            $where .= ' AND ord_a_item.id != ?';
            $params[] = $filter['exclude_ord_a_item_id'];
        }

        if (isset($filter['is_adm_price'])) {
            $where .= ' AND ord_a_item.is_adm_price = ?';
            $params[] = $filter['is_adm_price'];
        }
        if (isset($filter['is_active'])) {
            $where .= ' AND ord_a_item.is_active = ?';
            $params[] = $filter['is_active'];
        }
        if (isset($filter['a_item_type'])) {
            $where .= ' AND ord_a_item.a_item_type = ?';
            $params[] = $filter['a_item_type'];
        }
        if (isset($filter['v_termin_id_not_zero'])) {
            $where .= ' AND ord_a_item.v_termin_id != 0';
        }
        if (isset($filter['c_termin_id_not_zero'])) {
            $where .= ' AND ord_a_item.c_termin_id != 0';
        }

        if (isset($filter['get_user_crt'])) {
			$select .= ',ucrt.first_name as ucrt_name';
			$join .= ' LEFT JOIN t_users as ucrt ON ord_a_item.crt_by = ucrt.id';
		}

		if (isset($filter['get_user_rjct'])) {
			$select .= ',urjct.first_name as urjct_name,urjct_bill.first_name as urjct_bill_name';
			$join .= ' LEFT JOIN t_users as urjct ON ord_a_item.rjct_by = urjct.id';
			$join .= ' LEFT JOIN t_users as urjct_bill ON ord_a_item.rjct_bill_by = urjct_bill.id';
		}

        if (isset($filter['prev_main_item_id'])) {
            $where .= ' AND ord_a_item.id < ?';
            $params[] = $filter['prev_main_item_id'];
        }

        if (isset($filter['limit'])) {
            $limit .= ' LIMIT ?';
            $params[] = $filter['limit'];
        }

        return DB::select("SELECT
        ord_a_item.id as ord_a_item_id
        ,ord_a_item.*
        $select
        FROM
        t_orders_a_items as ord_a_item
        $join
        WHERE ord_a_item.dlt is null
        $where
        $group_by
        $limit
        ;",
        $params);
    }

    public static function showAItemById($ord_a_item_id, $filter = [])
    {
        $params = [];
        $join = '';
        $select = '';
        $where = '';

        if (isset($filter['is_adm_price'])) {
            $where .= ' AND ord_a_item.is_adm_price = ?';
            $params[] = $filter['is_adm_price'];
        }

        if (isset($filter['get_user_crt'])) {
			$select .= ',ucrt.first_name as ucrt_name';
			$join .= ' LEFT JOIN t_users as ucrt ON ord_a_item.crt_by = ucrt.id';
		}

		if (isset($filter['get_user_rjct'])) {
			$select .= ',urjct.first_name as urjct_name,urjct_bill.first_name as urjct_bill_name';
			$join .= ' LEFT JOIN t_users as urjct ON ord_a_item.rjct_by = urjct.id';
			$join .= ' LEFT JOIN t_users as urjct_bill ON ord_a_item.rjct_bill_by = urjct_bill.id';
		}

        $params[] = $ord_a_item_id;

        return DB::select("SELECT
        ord_a_item.id as ord_a_item_id
        ,ord_a_item.*
        $select
        FROM
        t_orders_a_items as ord_a_item
        $join
        WHERE ord_a_item.dlt is null
        $where
        AND ord_a_item.id = ?
        ;",
        $params);
    }

    public static function showAItemByIds($ids)
    {
        $params = [];
        $select = '';
        $where = '';

        if ($ids && count($ids) > 0) {
            $binds_ids = "";
            foreach ($ids as $k => $v) {
                $binds_ids .= "?,";
                $params[] = $v;
            }
            if (substr($binds_ids, -1) === ',') {
                $binds_ids = substr($binds_ids, 0, -1);
            }
            $where .= " AND ord_a_item.id IN ($binds_ids)";
        } else {
            $where .= " AND ord_a_item.id = ?";
            $params[] = 0;
        }

        return DB::select("SELECT
        ord_a_item.id as ord_a_item_id
        ,ord_a_item.*
        ,ut.name as unit_type_name
        $select
        FROM t_orders_a_items as ord_a_item
        LEFT JOIN t_unit_types as ut ON ord_a_item.unit_type = ut.id
        WHERE ord_a_item.dlt is null
        $where
        ;",
        $params);
    }

    // termin ids
    public static function showAItemByVids($tids, $filter = [])
    {
        $params = [];
        $select = '';
        $where = '';

        if ($tids && count($tids) > 0) {
            $binds_ids = "";
            foreach ($tids as $k => $v) {
                $binds_ids .= "?,";
                $params[] = $v;
            }
            if (substr($binds_ids, -1) === ',') {
                $binds_ids = substr($binds_ids, 0, -1);
            }
            if (isset($filter['c_termin_id'])) {
                $where .= " AND ord_a_item.c_termin_id IN ($binds_ids)";
            } else {
                $where .= " AND ord_a_item.v_termin_id IN ($binds_ids)";
            }
        } else {
            if (isset($filter['c_termin_id'])) {
                $where .= " AND ord_a_item.c_termin_id = ?";
            } else {
                $where .= " AND ord_a_item.v_termin_id = ?";
            }
            $params[] = 0;
        }

        if (isset($filter['a_item_type'])) {
            $where .= " AND ord_a_item.a_item_type = ?";
            $params[] = $filter['a_item_type'];
        }

        return DB::select("SELECT
        ord_a_item.id as ord_a_item_id
        ,ord_a_item.*
        ,ut.name as unit_type_name
        $select
        FROM t_orders_a_items as ord_a_item
        LEFT JOIN t_unit_types as ut ON ord_a_item.unit_type = ut.id
        WHERE ord_a_item.dlt is null
        $where
        ;",
        $params);
    }

    public static function getById($id)
    {
        return DB::select("SELECT * FROM t_orders_a_items WHERE id = ? LIMIT 1;", [$id]);
    }

    public static function getByOrdCode($code)
    {
        return DB::select("SELECT * FROM t_orders_a_items WHERE ord_code = ? LIMIT 1;", [$code]);
    }

	public static function getByOrdId($ordid)
    {
        return DB::select("SELECT * FROM t_orders_a_items WHERE ord_id = ? LIMIT 1;", [$ordid]);
    }

    public static function add($data)
    {
        $id = DB::table("t_orders_a_items")->insertGetId($data);
        return $id;
    }

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

    public static function updtByOrdId($ord_id, $data)
    {
        return DB::table("t_orders_a_items")->where("ord_id", $ord_id)->update($data);
    }

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

    public static function dltByOrdId($ord_id)
    {
        return DB::table("t_orders_a_items")->where("ord_id", $ord_id)->delete();
    }
}
