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

class SsProses_m extends CI_Model
{

    // mencetak invoice otomatis
    public function invoice_no()
    {
        $sql = "SELECT MAX(MID(invoice_no,9,4)) AS invoice_no
                FROM tb_sell 
                WHERE MID(invoice_no,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_no) + 1;
            $no = sprintf("%'.02d", $n);
        } else {
            $no = "01";
        }
        $invoice = "SS" . date('ymd') . $no;
        return $invoice;
    }

    public function get_part()
    {
        $this->db->select('tb_katbrgstok.* , tb_katbrg.nama_katbrg as nama_katbrg');
        $this->db->from('tb_katbrgstok');
        $this->db->join('tb_katbrg', 'tb_katbrg.id_katalogbrg = tb_katbrgstok.id_katalogbrg');
        $this->db->order_by('id_katbrgstok', 'asc');
        $query = $this->db->get();
        return $query;
    }

    public function get($id = null)
    {

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

    public function getpel($id = null)
    {

        if ($id != null) {
            $this->db->from('tb_pel');
            $this->db->order_by('id_pel', 'asc');
            $this->db->where('id_pel', $id);
        } elseif (isset($_GET['bm'])) {
            $cab = $_GET['bm'];
            $this->db->from('tb_pel');
            $this->db->where('car_plat', $cab);
            $this->db->order_by('id_pel', 'asc');
        } else {
            $this->db->from('tb_pel');
            $this->db->where('id_pel', 00);
        }
        $query = $this->db->get();
        return $query;
    }

    public function get_mekanik($id = null)
    {

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

    public function get_rusak($id = null)
    {

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

    public function get_rusakprint($id = null)
    {

        if ($id != null) {
            $this->db->from('tb_sell_rusak');
            $this->db->order_by('id_sell_rusak', 'desc');
            $this->db->where('id_sell', $id);
            $this->db->where('status_rusak', 1);
        } else {
            $this->db->from('tb_sell_rusak');
            $this->db->order_by('id_sell', 'xxx');
        }
        $query = $this->db->get();
        return $query;
    }

    public function get_sparepart($id = null)
    {

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

    public function addpart($post)
    {
        $params['id_sell'] = $post['id_sell'];
        $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['hargau_brg'] = $post['hargau_brg'];
        $params['stok_brg'] = $post['stok_brg'];
        $params['sell_part_status'] = 0;
        $this->db->insert('tb_sell_part', $params);
    }

    public function addsell($post)
    {
        $params['invoice_no'] = $post['invoice_no'];
        $params['sell_tgl'] = $post['sell_tgl'];
        $params['sell_bln'] = $post['sell_bln'];
        $params['sell_th'] = $post['sell_th'];
        $params['pel_nama'] = $post['pel_nama'];
        $params['pel_hp'] = $post['pel_hp'];
        $params['pel_alamat'] = $post['pel_alamat'];
        $params['car_plat'] = $post['car_plat'];
        $params['car_merek'] = $post['car_merek'];
        $params['car_model'] = $post['car_model'];
        $params['car_th'] = $post['car_th'];
        $params['car_modelcode'] = $post['car_modelcode'];
        $params['car_engine'] = $post['car_engine'];
        $params['car_volume'] = $post['car_volume'];
        $params['car_frameno'] = $post['car_frameno'];
        $params['car_color'] = $post['car_color'];
        $params['car_axle'] = $post['car_axle'];
        $params['car_odometer'] = $post['car_odometer'];

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

    public function addpel($post)
    {
        $params['pel_nama'] = $post['pel_nama'];
        $params['pel_hp'] = $post['pel_hp'];
        $params['pel_alamat'] = $post['pel_alamat'];
        $params['car_plat'] = $post['car_plat'];
        $params['car_merek'] = $post['car_merek'];
        $params['car_model'] = $post['car_model'];
        $params['car_th'] = $post['car_th'];
        $params['car_modelcode'] = $post['car_modelcode'];
        $params['car_engine'] = $post['car_engine'];
        $params['car_volume'] = $post['car_volume'];
        $params['car_frameno'] = $post['car_frameno'];
        $params['car_color'] = $post['car_color'];
        $params['car_axle'] = $post['car_axle'];
        $params['car_odometer'] = $post['car_odometer'];
        $params['pass'] = 'b613723546df634f403b429aba5f5b7feff6fa75'; // kimopass

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

    public function upkunjungan($post)
    {
        $params['kunjung'] = $post['kunjung'] + 1;

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

    public function editanalisa($post)
    {
        $params['nama_rusak'] = $post['nama_rusak'];
        $params['jam_rusak'] = 0;
        $params['harga_rusak'] = $post['harga_rusak'];
        $params['status_rusak'] = $post['status_rusak'];
        $params['total_rusak'] = $post['harga_rusak'];

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

    public function editanalisaK($post)
    {
        $params['nama_rusak'] = $post['nama_rusak'];
        $params['jam_rusak'] = 0;
        $params['harga_rusak'] = $post['harga_rusak'];
        if ($post['harga_rusak'] == $post['hargam_rusak']) {
            $params['hun_rusak'] = $post['hargam_rusak'];
        } elseif ($post['harga_rusak'] != $post['hargam_rusak']) {
            $params['hun_rusak'] = $post['harga_rusak'] - $post['hargam_rusak'];
        }

        $params['hargam_rusak'] = $post['hargam_rusak'];
        $params['total_rusak'] = $post['harga_rusak'];

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

    public function addanalisa($post)
    {
        $params['id_sell'] = $post['id_sell'];
        $params['nama_rusak'] = $post['nama_rusak'];
        $params['jam_rusak'] = 0;
        //   $params['harga_rusak'] = $post['harga_rusak'];
        //   $params['total_rusak'] = $post['harga_rusak'] * $post['jam_rusak'];
        $params['status_rusak'] = 0;
        $this->db->insert('tb_sell_rusak', $params);
    }



    public function delrusk($id)
    {
        $this->db->where('id_sell_rusak', $id);
        $this->db->delete('tb_sell_rusak');
    }

    public function delpart($id)
    {
        $this->db->where('id_sell_part', $id);
        $this->db->delete('tb_sell_part');
    }

    public function getbmproses($id = null)
    {
        $this->db->from('tb_sell');
        $this->db->order_by('id_sell', 'desc');
        $this->db->where('proses', 0);
        if ($id != null) {
            $this->db->where('id_sell',  $id);
        }
        $query = $this->db->get();
        return $query;
    }

    public function getbmproseskasir($id = null)
    {
        if (isset($_GET['startDate'])) {
            $tgl = $_GET['startDate'];
            $this->db->from('tb_sell');
            $this->db->order_by('id_sell', 'desc');
            $this->db->like('proses', 1);
            $this->db->where('sell_bln', $tgl);
        } else {
            $tgl = date('d / m / Y');
            $this->db->from('tb_sell');
            $this->db->order_by('id_sell', 'desc');
            $this->db->where('proses', 1);
            $this->db->like('sell_tgl_byr', $tgl);
        }
        $query = $this->db->get();
        return $query;
    }





    public function editpart($post)
    {
        $params['pasang_brg'] = $post['pasang_brg'];
        $params['sell_part_status'] = 1;

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

    public function editpartK($post)
    {
        $params['ambil_brg'] = $post['ambil_brg'];
        $params['id_katbrgstok'] = $post['id_katbrgstok'];
        $params['barcode_brg'] = $post['barcode_brg'];
        $params['name_brg'] = $post['name_brg'];
        $params['hargam_brg'] = $post['hargam_brg'];
        $params['hargau_brg'] = $post['hargau_brg'];
        $params['stok_brg'] = $post['stok_brg'];
        if ($post['ambil_brg'] != null) {
            $params['sell_part_ambil'] = 1;
        } elseif ($post['ambil_brg'] == null) {
            $params['sell_part_ambil'] = 0;
        }
        $this->db->where('id_sell_part', $post['id_sell_part']);
        $this->db->update('tb_sell_part', $params);
    }

    public function editbyr($post)
    {
        $params['sell_tgl_byr'] = $post['sell_tgl_byr'];
        $params['totjasa'] = $post['totjasa'];
        $params['totmjasa'] = $post['totmjasa'];
        $params['totpart'] = $post['totpart'];
        $params['totmpart'] = $post['totmpart'];
        $params['grandtotal'] = $post['grandtotal'];
        $params['cash'] = $post['cash'];
        $params['kembalian'] = $post['kembalian'];
        $params['dpbyr'] = $post['dpbyr'];
        $params['totbyr'] = $post['grandtotal'] - $post['dpbyr'];
        if ($post['dpbyr'] == 0) {
            $params['proses'] = 1;
        } elseif ($post['dpbyr'] > 0) {
            $params['proses'] = 0;
        }
        if ($post['cash'] != null) {
            $params['proses'] = 1;
        }
        $params['byr'] = $post['byr'];


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

    public function totjasa($id)
    {
        $sql = "SELECT sum(total_rusak) as totjasa FROM tb_sell_rusak WHERE status_rusak='1' AND id_sell='$id'";
        $result = $this->db->query($sql);
        return $result->row()->totjasa;
    }

    public function totmjasa($id)
    {
        $sql = "SELECT sum(jam_rusak * hun_rusak) as totmjasa FROM tb_sell_rusak WHERE status_rusak='1' AND id_sell='$id'";
        $result = $this->db->query($sql);
        return $result->row()->totmjasa;
    }

    public function totpart($id)
    {
        $sql = "SELECT sum(stok_brg * hargau_brg) as totpart FROM tb_sell_part WHERE sell_part_status='1' AND id_sell='$id'";
        $result = $this->db->query($sql);
        return $result->row()->totpart;
    }

    public function totmpart($id)
    {
        $sql = "SELECT sum(stok_brg * ( hargau_brg - hargam_brg)) as totmpart FROM tb_sell_part WHERE sell_part_status='1' AND id_sell='$id'";
        $result = $this->db->query($sql);
        return $result->row()->totmpart;
    }

    public function totbyr($id)
    {
        $sql = "SELECT (totbyr) as totbyr FROM tb_sell WHERE id_sell='$id'";
        $result = $this->db->query($sql);
        return $result->row()->totbyr;
    }

    public function dpbyr($id)
    {
        $sql = "SELECT (dpbyr) as dpbyr FROM tb_sell WHERE id_sell='$id'";
        $result = $this->db->query($sql);
        return $result->row()->dpbyr;
    }
}
