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


class Penjualan_model extends CI_Model
{
    public function get_new_penjualan() {
        $this->db->select('*');
        $this->db->order_by('id_penjualan', 'DESC');
        $this->db->limit(1);
        return $this->db->get('penjualan');
    }

    
    public function get_all() 
    {
        $this->db->join('pembeli', 'pembeli.id_pembeli=penjualan.id_pembeli');
        $this->db->join('penjualan_produk', 'penjualan_produk.id_penjualan=penjualan.id_penjualan');
        $this->db->join('produk', 'produk.id_produk=penjualan_produk.id_produk');
        return $this->db->get('penjualan');
    }
    
    public function countall() 
    {
        return $this->db->count_all('penjualan');
    }

    public function add_penjualan($params)
    {
        $this->db->insert('penjualan', $params);
        return $this->db->insert_id();
    }

    public function add_penjualan_produk($params)
    {
        return $this->db->insert('penjualan_produk', $params);
    }

    public function get_all_penjualan()
    {
        $this->db->select('penjualan.*,pembeli.*,DATE_FORMAT(tanggal, "%d-%m-%Y") as tanggal');
        $this->db->order_by('id_penjualan', 'desc');
        $this->db->join('pembeli', 'pembeli.id_pembeli=penjualan.id_pembeli', 'left');
        return $this->db->get('penjualan');
    }

    public function get_all_penjualan_by_id($id_penjualan)
    {
        $this->db->order_by('id_penjualan', 'desc');
        $this->db->join('pembeli', 'pembeli.id_pembeli=penjualan.id_pembeli', 'left');
        $this->db->where('id_penjualan', $id_penjualan);
        return $this->db->get('penjualan');
    }

    public function get_penjualan($id_pembeli)
    {
        $this->db->select('penjualan.*,pembeli.*,DATE_FORMAT(tanggal, "%d-%m-%Y") as tanggal,(total+total_ongkir) as total2');
        $this->db->order_by('id_penjualan', 'desc');
        $this->db->join('pembeli', 'pembeli.id_pembeli=penjualan.id_pembeli', 'left');
        $this->db->where('penjualan.id_pembeli', $id_pembeli);
        return $this->db->get('penjualan');
    }

    public function get_penjualan_detail($id_penjualan)
    {
        $this->db->where('id_penjualan', $id_penjualan);
        $this->db->join('produk', 'produk.id_produk=penjualan_produk.id_produk', 'left');
        return $this->db->get('penjualan_produk');
    }

    public function delete_penjualan($id_penjualan)
    {
        $this->db->where('id_penjualan', $id_penjualan);
        return $this->db->delete('penjualan');
    }

    public function get_all_penjualan_by_tgl($tgl1, $tgl2)
    {
        $this->db->order_by('id_penjualan', 'asc');
        $this->db->join('pembeli', 'pembeli.id_pembeli=penjualan.id_pembeli', 'left');
        $this->db->where('tanggal >=', $tgl1);
        $this->db->where('tanggal <=', $tgl2);
        return $this->db->get('penjualan');
    }

    public function update_penjualan($id_penjualan, $params)
    {
        $this->db->where('id_penjualan', $id_penjualan);
        return $this->db->update('penjualan', $params);
    }

    public function get_penjualanid($id_penjualan)
    {
        $this->db->where('', $id_penjualan);
        return $this->db->get('penjualan');
    }

}

/* End of file Penjualan_model.php */
/* Location: ./application/models/Penjualan_model.php */