<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Supl_m extends CI_Model
{

    public function get($id = null)
    {

        if ($id != null) {
            $this->db->from('tb_supplier');
            $this->db->order_by('id_sup', 'desc');
            $this->db->where('id_sup', $id);
        } else {
            $this->db->from('tb_supplier');
            $this->db->order_by('id_sup', 'desc');
        }
        $query = $this->db->get();
        return $query;
    }

    public function get_pesanbrg($id = null)
    {
        $tgl = date('d / m / Y');
        $this->db->from('t_pesanbrg');
        $this->db->order_by('id_pesbrg', 'desc');
        $this->db->where('tanggal_brg', $tgl);

        $query = $this->db->get();
        return $query;
    }

    public function addsupl($post)
    {
        $params['sup_acc'] = $post['sup_acc'];
        $params['sup_reg'] = $post['sup_reg'];
        $params['nama_sup'] = $post['nama_sup'];
        $params['sup_email'] = $post['sup_email'];
        $params['no_hpsup'] = $post['no_hpsup'];
        $params['alamat_sup'] = $post['alamat_sup'];

        $this->db->insert('tb_supplier', $params);
    }

    public function delsupl($id)
    {
        $this->db->where('id_sup', $id);
        $this->db->delete('tb_supplier');
    }

    // pesan barang ////////////////////
    public function invoice_nobrg()
    {
        $sql = "SELECT MAX(MID(invoice_nobrg,9,4)) AS invoice_nobrg
               FROM t_pesanbrg 
               WHERE MID(invoice_nobrg,3,6) = DATE_FORMAT(CURDATE(),'%y%m%d')";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $row = $query->row();
            $n = ((int) $row->invoice_nobrg) + 1;
            $no = sprintf("%'.02d", $n);
        } else {
            $no = "01";
        }
        $invoicea =  $this->fungsi->user_login()->id_user . date('ymd') . $no;
        return $invoicea;
    }

    public function buy_sparepart($id = null)
    {
        $this->db->from('t_pesanbrg_cart');
        $this->db->order_by('id_pesbrg_cart', 'desc');

        $query = $this->db->get();
        return $query;
    }

    public function buy_sparepartcek($id = null)
    {
        $tgl = date('d / m / Y');
        $this->db->select('t_pesanbrg.* , t_pesanbrg_cart_detil.stok_brgup as stok_brgup');
        $this->db->select('t_pesanbrg.* , t_pesanbrg_cart_detil.id_katbrgstok as id_katbrgstok');
        $this->db->select('t_pesanbrg.* , t_pesanbrg_cart_detil.barcode_brg as barcode_brg');
        $this->db->select('t_pesanbrg.* , t_pesanbrg_cart_detil.hargam_brgup as hargam_brgup');
        $this->db->select('t_pesanbrg.* , t_pesanbrg_cart_detil.hargau_brg as hargau_brg');
        $this->db->select('t_pesanbrg.* , t_pesanbrg_cart_detil.hargat_brg as hargat_brg');

        $this->db->select('t_pesanbrg.* , t_pesanbrg_cart_detil.t_pesanbrg_detail  as t_pesanbrg_detail');
        $this->db->select('t_pesanbrg.* , t_pesanbrg_cart_detil.name_brg as name_brg');
        $this->db->from('t_pesanbrg');
        $this->db->join('t_pesanbrg_cart_detil', 't_pesanbrg_cart_detil.id_pesbrg = t_pesanbrg.id_pesbrg');
        $this->db->order_by('tanggal_brg', 'asc');
        $this->db->where('tanggal_brg', $tgl);
        if ($id != null) {
            $this->db->from('t_pesanbrg');
            $this->db->where('id_pesbrg',  $id);
        }
        $query = $this->db->get();
        return $query;
    }

    public function addpartpesan($post)
    {
        $params['barcode_brg'] = $post['barcode_brg'];
        $params['id_katbrgstok'] = $post['id_katbrgstok'];
        $params['name_brg'] = $post['name_brg'];
        $params['hargam_brg'] = $post['hargam_brg'];
        $params['hargam_brgup'] = $post['hargam_brg'];
        $params['hargau_brg'] = $post['hargau_brg'];
        $params['stok_brg'] = $post['stok_brg'];
        $params['stok_brgup'] = $post['stok_brg'];
        $params['hargat_brg'] = $post['hargam_brg'] * $post['stok_brg'];
        $params['datein'] = date('m / Y');

        $this->db->insert('t_pesanbrg_cart', $params);
    }

    public function delpesananbrg($id)
    {
        $this->db->where('id_pesbrg_cart', $id);
        $this->db->delete('t_pesanbrg_cart');
    }

    public function get_cart($params = null)
    {
        $this->db->from('t_pesanbrg_cart');
        if ($params != null) {
            $this->db->where($params);
        }
        $query = $this->db->get();
        return $query;
    }

    public function addpesanbrgdetail($post)
    {
        $params['invoice_nobrg'] = $this->invoice_nobrg();
        $params['suplier'] = $post['suplier'];
        $params['tanggal_brg'] = $post['tanggal_brg'];
        $params['bln_brg'] = date('m / Y');
        $params['th_brg'] = date('Y');
        $params['byr_brg'] = $post['byr_brg'];
        $params['id_cab'] = $this->fungsi->user_login()->status_cabang;
        $this->db->insert('t_pesanbrg', $params);
        $in_id = $this->db->insert_id();
        return $in_id;
    }

    // input pesan barang tabel t_pesanbrg_cart ok ok ok ok
    public function addpesanbrg($post)
    {
        $query = $this->db->query("SELECT MAX(id_pesbrg_cart) AS cart_no FROM t_pesanbrg_cart");
        if ($query->num_rows() > 0) {
            $row = $query->row();
            $car_no = ((int) $row->cart_no) + 1;
        } else {
            $car_no = "1";
        }
        $params['id_pesbrg_cart'] = $car_no;
        $params['barcode_brg'] = $post['barcode_brg'];
        $params['id_katbrgstok'] = $post['id_katbrgstok'];
        $params['name_brg'] = $post['name_brg'];
        $params['hargam_brg'] = $post['hargam_brg'];
        $params['hargam_brgup'] = $post['hargam_brg'];

        $params['hargau_brg'] = $post['hargau_brg'];
        $params['stok_brg'] = $post['stok_brg'];
        $params['stok_brgup'] = $post['stok_brg'];
        $params['datein'] = $post['datein'];


        $params['hargat_brg'] = $post['hargam_brg'] * $post['stok_brg'];
        $this->db->insert('t_pesanbrg_cart', $params);
    }

    function t_pesanbrg_cart_detil($params)
    {

        $this->db->insert_batch('t_pesanbrg_cart_detil', $params);
    }

    public function delall()
    {
        $this->db->empty_table('t_pesanbrg_cart');
    }

    // -----------print pesan brg
    public function get_pesanbrg_detail($sale_id = null)
    {
        $this->db->from('t_pesanbrg_cart_detil');
        if ($sale_id != null) {
            $this->db->where('t_pesanbrg_cart_detil.id_pesbrg  ',  $sale_id);
        }
        $query = $this->db->get();
        return $query;
    }

    public function getpesansuplier($id = null)
    {
        $cab = $this->fungsi->user_login()->status_cabang;
        $tgl = date('d / m / Y');
        //  $this->db->select('t_pesanbrg.* , t_pesanbrg_cart_detil.nama_brg as nama_brg ');
        $this->db->from('t_pesanbrg');
        //   $this->db->join('t_pesanbrg_cart_detil', 't_pesanbrg_cart_detil.id_pesbrg = t_pesanbrg.id_pesbrg');
        $this->db->order_by('id_pesbrg', 'desc');
        $this->db->where('tanggal_brg', $tgl);
        $this->db->like('id_cab', $cab);


        if ($id != null) {
            $this->db->where('id_pesbrg',  $id);
        }
        $query = $this->db->get();
        return $query;
    }

    //--------- lap. pembeliann part

    public function  gettotalpesbrgcabkasiraja()
    {
        //        $cab = $this->fungsi->user_login()->status_cabang;
        $tgl = date('d / m / Y');
        $sql = "SELECT sum(stok_brgup * hargam_brgup) as totpesbrgkasir FROM t_pesanbrg_cart_detil t_pesanbrg_cart_detil JOIN t_pesanbrg
        ON t_pesanbrg_cart_detil.id_pesbrg =t_pesanbrg.id_pesbrg  where tanggal_brg='$tgl' AND byr_brg='0'";
        $result = $this->db->query($sql);
        return $result->row()->totpesbrgkasir;
    }

    public function updateharbrg($post)
    {
        $params['stok_brgup'] = $post['stok_brgup'];
        $params['hargam_brgup'] = $post['hargam_brgup'];
        $params['dateup'] = 1;
        $params['hargat_brg'] = $post['hargam_brgup'] * $post['stok_brgup'];

        $this->db->where('t_pesanbrg_detail', $post['t_pesanbrg_detail']);
        $this->db->update('t_pesanbrg_cart_detil', $params);
    }
}
