<?php

namespace App\Models;

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

class OrdersTermins extends Model
{
    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 AMT_TYPE_FLAT = 1;
	const AMT_TYPE_PERCENT = 2;

    // 1=>refer termin_ddln_at, 2=>refer to order finish(pengantaran selesai)
	const DDLN_TERMIN_TYPE_TIME = 1;
	const DDLN_TERMIN_TYPE_ORD_FINISH = 2;

    const IS_PAID_NO = 0;
    const IS_PAID_YES = 1;

    // 1=>transfer
	const PAID_TYPE_TF = 1;

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

    const IS_ACTIVE_NO = 0;
    const IS_ACTIVE_YES = 1;

    // 1=>client, 2=>vendor
    const TERMIN_FOR_CLIENT = 1;
    const TERMIN_FOR_VENDOR = 2;

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

    /**
     * termin_at itu deadline pembayaran
     * column merge disini untuk merge per trx
     */

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

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

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

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

        if (isset($filter['in_stts_merge'])) {
            if (is_array($filter['in_stts_merge'])) {
                $where .= ' AND stts_merge IN (';
                foreach ($filter['in_stts_merge'] as $in) {
                    $where .= '?,';
                    $params[] = $in;
                }
                if (strpos(substr($where, -1), ',') !== false) {
                    $where = substr($where, 0, -1) . ')';
                }
            }
        }

        return DB::select("SELECT
        ord_termin.*
        $select
        FROM t_orders_termins as ord_termin
        WHERE dlt is null
        $where
        ;", $params);
    }

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

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

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

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

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

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

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

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

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