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

class Kasir_model extends CI_Model {

	public function get_toko() {
		$user = $this->session->userdata('kd_toko');
		$query = $this->db->query("SELECT * FROM tabel_toko where kd_toko='$user';");
		return $query->row();
	}
public function get_tokoku($kdtoko) {
		$query = $this->db->query("SELECT * FROM tabel_toko where kd_toko='$kdtoko';");
		return $query->row();
	}
	public function get_detail_produk($idbarang) {
	    $kdtoko = $this->session->userdata('kd_toko');
		$hsl = $this->db->query("SELECT tabel_stok_toko.stok, tabel_barang.nm_barang, tabel_satuan_barang.nm_satuan, tabel_barang.hrg_beli, tabel_barang.hrg_jual, tabel_kategori_barang.nm_kategori FROM tabel_barang 
		LEFT JOIN tabel_stok_toko ON tabel_barang.kd_barang = tabel_stok_toko.kd_barang
		LEFT JOIN tabel_kategori_barang ON tabel_barang.kd_kategori = tabel_kategori_barang.kd_kategori 
		LEFT JOIN tabel_satuan_barang ON tabel_barang.kd_satuan = tabel_satuan_barang.kd_satuan WHERE tabel_barang.kd_barang='$idbarang' and tabel_stok_toko.kd_toko='$kdtoko'");
		if ($hsl->num_rows() > 0) {
			foreach ($hsl->result() as $data) {
				$hasil = array(
					'namaproduk' => $data->nm_barang,
					'stok' => $data->stok,
					'harga' => $data->hrg_jual,
					'kategori' => $data->nm_kategori,
					'harga_beli' => $data->hrg_beli,
					'satuan' => $data->nm_satuan,
				);
			}
		}
		return $hasil;
	}
	public function getDataMenu() {
		$noresi = $this->uri->segment(3);
		$kod = $this->db->query("select jnsmenu,kdharga,kdcabang,jenisbrg from tabel_penjualan where no_faktur_penjualan='$noresi';");
		$kode=$kod->row_array();
		$tgl=date('Y-m-d');
		$kdjns='J01';
		if(isset($kode)) {
		    $kdjns=$kode['jnsmenu'];
		    $jnsbrg=$kode['jenisbrg'];
		    $kdharga=$kode['kdharga'];
		    $kdcabang=$kode['kdcabang'];
		}
		if($jnsbrg =='J08' || $jnsbrg=='J09') {
		$this->db->join('hargajual AS b', 'a.kode_menu = b.kdbrg', 'left');
		$this->db->join('tabel_penjualan AS c', 'b.kdcabang = c.kdcabang and b.kdharga = c.kdharga', 'left');
		$this->db->where('a.jenisbrg', $kdjns);
		$this->db->where('c.no_faktur_penjualan', $noresi);
		$this->db->where('b.tglawal<=', $tgl);
		$this->db->where('b.tglakhir>=', $tgl);
		$this->db->where('a.stat', '1');
		$this->db->order_by('a.jenisbrg');
		$this->db->order_by('a.kode_menu');
		return $this->db->get('tabel_menu AS a');
		}
		if($jnsbrg !='J08' && $jnsbrg !='J09') {
		$this->db->join('hargajual AS b', 'a.kode_menu = b.kdbrg', 'left');
		$this->db->join('tabel_penjualan AS c', 'b.kdcabang = c.kdcabang and b.kdharga = c.kdharga', 'left');
		$this->db->where('a.jenisbrg', $kdjns);
		$this->db->where('a.jenismenu', $jnsbrg);
		$this->db->where('c.no_faktur_penjualan', $noresi);
		$this->db->where('b.tglawal<=', $tgl);
		$this->db->where('b.tglakhir>=', $tgl);
		$this->db->where('a.stat', '1');
		$this->db->order_by('a.jenisbrg');
		$this->db->order_by('a.kode_menu');
		return $this->db->get('tabel_menu AS a');
		}
	}
		public function getDataMenu1() {
		$noresi = $this->uri->segment(3);
		$kod = $this->db->query("select jnsmenu,kdharga,kdcabang,cari from tabel_penjualan where no_faktur_penjualan='$noresi';");
		$kode=$kod->row_array();
		$tgl=date('Y-m-d');
		$kdjns='J01';
		if(isset($kode)) {
		    $kdjns=$kode['jnsmenu'];
		    $cari=$kode['cari'];
		    $kdharga=$kode['kdharga'];
		    $kdcabang=$kode['kdcabang'];
		}
		
		$this->db->join('hargajual AS b', 'a.kode_menu = b.kdbrg', 'left');
		$this->db->join('tabel_penjualan AS c', 'b.kdcabang = c.kdcabang and b.kdharga = c.kdharga', 'left');
		$this->db->like('a.nama_menu', $cari);
		$this->db->where('c.no_faktur_penjualan', $noresi);
		$this->db->where('b.tglawal<=', $tgl);
		$this->db->where('b.tglakhir>=', $tgl);
		$this->db->where('a.stat', '1');
		$this->db->order_by('a.kode_menu');
		return $this->db->get('tabel_menu AS a');
	}
	public function dataTransaksiHariIni_52($tgl,$kdtoko) {
	    $ymd = date('ymd');
	    $persen=100;
	    $a=$this->db->query("select persen from tabel_persen where kdcabang='$kdtoko' and tgl='$tgl';") ;
	    $b=$a->row_array();
	    if(isset($b)) {
	    $persen=$b['persen'];}
	    
	    $pr=$this->db->query("select persen from tabel_toko where kd_toko='$kdtoko';") ;
	    $pr1=$pr->row_array();
	    if($persen==100) {
	        if(isset($pr1)) {
	             $persen=$pr1['persen'];}
	        }
	    $wafile=$kdtoko.date('ymd');
        $this->db->query("delete from tanggal where  kdcabang='$kdtoko';");
		$this->db->query("delete from rekapharian1 where jenis<>'6' and tgl='$tgl' and  kdcabang='$kdtoko';");
		$this->db->query("insert ignore into tabel_persen(tgl,kdcabang,persen) value ('$tgl','$kdtoko','$persen');");
		$this->db->query("insert ignore into tanggal(tgl,kdcabang) value ('$tgl','$kdtoko');");
		$this->db->query("insert ignore into rekapharian1(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn,nilai1,nppn1)
						 SELECT '1','$tgl','$kdtoko',a.kdharga,a.keterangan,
						 IFNULL((SELECT SUM((total_penjualan_sdiskon*($persen/100))) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,
						 IFNULL((SELECT SUM((nppn*($persen/100))) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn,
						 IFNULL((SELECT SUM((total_penjualan_sdiskon)) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg1,
						 IFNULL((SELECT SUM((nppn)) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn1
						 FROM kodeharga a where a.stat='1';");
	    $this->db->query("insert ignore into rekapharian1(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn,nilai1,nppn1)
						 SELECT 'A','$tgl','$kdtoko',a.kdharga,a.keterangan,
						 IFNULL((SELECT SUM((total_penjualan_sdiskon*($persen/100))) FROM tabel_penjualan WHERE selesai='1' AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,
						 IFNULL((SELECT SUM((nppn*($persen/100))) FROM tabel_penjualan WHERE selesai='1'  AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn,
						 IFNULL((SELECT SUM((total_penjualan_sdiskon)) FROM tabel_penjualan WHERE selesai='1' AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg1,
						 IFNULL((SELECT SUM((nppn)) FROM tabel_penjualan WHERE selesai='1'  AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn1
						 
						 FROM kodeharga a where a.stat='1';");							 
		$this->db->query("insert ignore into rekapharian1(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn,nilai1,nppn1)
						 SELECT '2','$tgl','$kdtoko',a.kdharga,a.keterangan,
						 IFNULL((SELECT SUM((ndisc/1.1)) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,
						 IFNULL((SELECT SUM((ndisc/11)) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn,
						 IFNULL((SELECT SUM((ndisc/1.1)) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg1,
						 IFNULL((SELECT SUM((ndisc/11)) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn1
						 FROM kodeharga a where a.stat='1';");				 
				 
        $this->db->query("insert ignore into rekapharian1(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						 SELECT '7','$tgl','$kdtoko',a.kdharga,a.keterangan,
						 IFNULL((SELECT SUM(ongkir) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,0
						 FROM kodeharga a where a.stat='1';");				 						 
						 
		$this->db->query("insert ignore into rekapharian1(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn,nilai1,nppn1)
						 SELECT '3','$tgl','$kdtoko',a.kdbayar,a.keterangan,
						 IFNULL((SELECT SUM((netto*($persen/100))) FROM tabel_penjualan WHERE selesai='1' and jnsbayar=a.kdbayar AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,
						 IFNULL((SELECT SUM((nppn*($persen/100))) FROM tabel_penjualan WHERE selesai='1' and jnsbayar=a.kdbayar AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn,
						 IFNULL((SELECT SUM((netto)) FROM tabel_penjualan WHERE selesai='1' and jnsbayar=a.kdbayar AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg1,
						 IFNULL((SELECT SUM((nppn)) FROM tabel_penjualan WHERE selesai='1' and jnsbayar=a.kdbayar AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn1
						 FROM jenispembayaran a where kdbayar<>'1';");		
		$this->db->query("insert ignore into rekapharian1(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn,nilai1,nppn1)
						 SELECT '4','$tgl','$kdtoko',a.kdharga,a.keterangan,
						 IFNULL((SELECT SUM((total_penjualan_sdiskon*($persen/100))) FROM tabel_penjualan WHERE waktu<'18:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,
						 IFNULL((SELECT SUM((nppn*($persen/100))) FROM tabel_penjualan WHERE waktu<'18:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn,
						 IFNULL((SELECT SUM((total_penjualan_sdiskon)) FROM tabel_penjualan WHERE waktu<'18:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg1,
						 IFNULL((SELECT SUM((nppn)) FROM tabel_penjualan WHERE waktu<'18:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn1
						 FROM kodeharga a where a.stat='1';");				 
		$this->db->query("insert ignore into rekapharian1(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn,nilai1,nppn1)
						 SELECT '5','$tgl','$kdtoko',a.kdharga,a.keterangan,
						 IFNULL((SELECT SUM((total_penjualan_sdiskon*($persen/100))) FROM tabel_penjualan WHERE waktu>='18:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,
						 IFNULL((SELECT SUM((nppn*($persen/100))) FROM tabel_penjualan WHERE waktu>='18:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn,
						 IFNULL((SELECT SUM((total_penjualan_sdiskon)) FROM tabel_penjualan WHERE waktu>='18:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg1,
						 IFNULL((SELECT SUM((nppn)) FROM tabel_penjualan WHERE waktu>='18:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn1
						 FROM kodeharga a where a.stat='1';");
		        $up=$this->db->query("update tabel_penjualan set statuswa='Gagal' where struk='1' and statuswa='';");
		        $wa1=$this->db2->query("select sent.status,sent.wa_file from sent where left(wa_file,11) like '$wafile';");				 
		        if ($wa1->num_rows() > 0) {
			        foreach ($wa1->result() as $data) {
			            $nobukwa=$data->wa_file;
			            if($data->status=='fail') {
			                    $statwa='Gagal';
			            }
			            if($data->status==NULL) {
			                    $statwa='Terkirim';
			            }
				    $hasil = array(
					    'statuswa' => $statwa,
				        );				 
				    	$this->db->where('no_faktur_penjualan', $nobukwa);
			            $this->db->update('tabel_penjualan', $hasil);    
		            }
		        } 
	
		return $this->db->select("waktu,(total_penjualan*($persen/100)) as total_penjualan,(ndisc) as diskon,(netto*($persen/100)) as total_penjualan_sdiskon,ket,id_user,ongkir,no_faktur_penjualan,tgl_penjualan,(nppn*($persen/100)) as nppn,kdcabang,kdharga,keter,if(statuswa='','Print',statuswa) as stwa,
		                        if(jnsbayar='1','Cash',if(jnsbayar='2','Debet',if(jnsbayar='3','Gopay',if(jnsbayar='4','Grabfood',if(jnsbayar='6','Shopeepay','OVO'))))) as jbayar,if(selesai='1','Selesai',if(selesai='4','Batal','Pending')) as selesai,note,notelp,
								IF(kdharga='KH01',IF(layanan='1',(total_penjualan_sdiskon*($persen/100)),0),0) AS reg,
								IF(kdharga='KH01',IF(layanan='2',(total_penjualan_sdiskon*($persen/100)),0),0) AS regta,
								IF(kdharga='KH01',IF(layanan='3',(total_penjualan_sdiskon*($persen/100)),0),0) AS regdo,
								IF(kdharga='KH02',(total_penjualan_sdiskon*($persen/100)),0) AS gofood,
								IF(kdharga='KH03',(total_penjualan_sdiskon*($persen/100)),0) AS grab,
								IF(kdharga='KH04',(total_penjualan_sdiskon*($persen/100)),0) AS shopee,
								IF(kdharga='KH05',(total_penjualan_sdiskon*($persen/100)),0) AS regma
								")
			
			->where('tabel_penjualan.tgl_penjualan', $tgl)
			->where('tabel_penjualan.selesai', '1')
			->where('tabel_penjualan.kdcabang', $kdtoko)
			->order_by('no_faktur_penjualan')
			->get('tabel_penjualan');
	
	}
	 public function getRekapHarian_52($kdtoko,$tgl) {
		$this->db->where('jenis', '1');
		$this->db->where('kdcabang', $kdtoko);
		$this->db->where('tgl', $tgl);
		return $this->db->get('rekapharian1');
	}
		public function datarekap_52($kdtoko,$tgl) {
	$this->db->query("delete from reportpb1 where tgl='$tgl' and kdcabang='$kdtoko';") ;   
	$this->db->query("INSERT IGNORE INTO reportpb1(kdcabang,tgl,dpp,pb1,netto)
                      SELECT kdcabang,GROUP_CONCAT(tgl ORDER BY tgl) AS tgls,GROUP_CONCAT(CAST(sumnilai AS CHAR(20)) ORDER BY tgl) AS sumnilai,GROUP_CONCAT(CAST(sumnppn AS CHAR(20)) ORDER BY tgl) AS sumnppn,
                      GROUP_CONCAT(CAST(sumnetto AS CHAR(20)) ORDER BY tgl) AS sumnetto
                      FROM
                        (
                            SELECT tgl,kdcabang,
                            SUM(nilai) AS sumnilai,
                            SUM(nppn) AS sumnppn,
                            SUM(nilai+nppn) AS sumnetto
                            FROM rekapharian1 WHERE MONTH(tgl)=MONTH(CURDATE()) AND jenis='1'
                            GROUP BY kdcabang,tgl
                        ) t
                    GROUP BY kdcabang,tgl;");
	    
		return  $this->db->select('SUM(IF(jenis="1",(nilai+nppn), 0)) AS omset,
		                           SUM(IF(jenis="1",(nppn), 0)) AS nppn, 
	                               SUM(IF(jenis="2",(nilai+nppn), 0)) AS discount,
	                               SUM(IF(jenis="2",(nppn), 0)) AS nppnd,
								   SUM(IF(jenis="3",(nilai+nppn), 0)) AS setor,
								   SUM(IF(jenis="4",(nilai+nppn), 0)) AS set1,
								   SUM(IF(jenis="5",(nilai+nppn), 0)) AS set2,
								   SUM(IF(jenis="6",(nilai+nppn), 0)) AS biaya,
								   SUM(IF(jenis="7",(nilai+nppn), 0)) AS ongkir,
								   SUM(IF(jenis="4",(nilai), 0)) AS bruto1,
								   SUM(IF(jenis="5",(nilai), 0)) AS bruto2,
								    SUM(IF(jenis="9",(nilai), 0)) AS pjual,
								   SUM(IF(jenis="10",(nilai), 0)) AS pbulat,
								   SUM(IF(jenis="11",(nilai), 0)) AS plain,
								   SUM(IF(jenis="1",(nppn), IF(jenis="2",((nppn)*-1),0))) AS netnppn,
								   SUM(IF(jenis="1",(nilai+nppn), IF(jenis="2",((nilai+nppn)*-1),0))) AS netomset,
								   (SUM(IF(jenis="1",(nilai+nppn), IF(jenis="2",((nilai+nppn)*-1),0))))+SUM(IF(jenis="7",(nilai+nppn), 0))+SUM(IF(jenis="9",(nilai+nppn), 0))+SUM(IF(jenis="10",(nilai+nppn), 0))+SUM(IF(jenis="11",(nilai+nppn), 0)) AS tomset,
								   (SUM(IF(jenis="1",(nilai+nppn), IF(jenis="2",((nilai+nppn)*-1),0)))-SUM(IF(jenis="3",(nilai+nppn), 0))) AS tunai,
								   (SUM(IF(jenis="6",nilai,0))+SUM(IF(jenis="3",(nilai), 0))) AS tbiaya,
							    	((SUM(IF(jenis="1",(nilai+nppn), IF(jenis="2",((nilai+nppn)*-1),0)))-SUM(IF(jenis="3",(nilai), 0)))-SUM(IF(jenis="6",(nilai+nppn), 0)))+SUM(IF(jenis="7",(nilai+nppn), 0))+SUM(IF(jenis="9",(nilai+nppn), 0))+SUM(IF(jenis="10",(nilai+nppn), 0))+SUM(IF(jenis="11",(nilai+nppn), 0)) AS saldo,
								
									SUM(IF(jenis="4",nppn,0)) AS nppn1,
									SUM(IF(jenis="5",nppn,0)) AS nppn2'
								   )
		->where('kdcabang', $kdtoko)
		->where('tgl', $tgl)
		->get('rekapharian1');
	}
	public function datarekapPengeluaranHarian_52($kdtoko,$tgl) {
		$this->db->where('jenis', '2');
		$this->db->where('kdcabang', $kdtoko);
		$this->db->where('tgl', $tgl);
		return $this->db->get('rekapharian1');
	}
	public function cek_stok_mutasi($kode) {
	    $kdtoko = $this->session->userdata('kd_toko');
		return $this->db->query("SELECT * FROM tabel_stok_toko WHERE kd_barang='$kode' and kd_toko='$kdtoko'");
	}

	public function get_list($nofak) {
		return $this->db->select('tabel_rinci_penjualan.*')
			->where('tabel_rinci_penjualan.no_faktur_penjualan', $nofak)
			->get('tabel_rinci_penjualan')
			->result();
	}
	public function get_list_dpr() {
	    $waktu = date('Y-m-d');
	    $this->db->select("sum(a.jumlah) as jumlah,a.kd_barang,a.nm_barang,a.jenis,a.nourut,a.no_faktur_penjualan");
		$this->db->join('tabel_penjualan AS b', 'a.no_faktur_penjualan = b.no_faktur_penjualan', 'left');
		$this->db->where('b.tgl_penjualan', $waktu);
		$this->db->where('b.layanan', '2');
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan AS a');
			
	}
	public function detail_faktur($nofak) {
		return $this->db->select('tabel_penjualan.*')
			->where('tabel_penjualan.no_faktur_penjualan', $nofak)
			->where('tabel_penjualan.selesai', '1')
			->get('tabel_penjualan')
			->row();
	}
public function detail_faktur_dpr() {
        $waktu = date('Y-m-d');
		return $this->db->select('tabel_penjualan.*')
			->where('tabel_penjualan.tgl_penjualan', $waktu)
			->where('tabel_penjualan.selesai', '1')
			->get('tabel_penjualan')
			->row();
	}


	public function getProdukRetur($nofak, $kd_barang) {
		return $this->db->select('tabel_rinci_penjualan.*')
			->where('tabel_rinci_penjualan.no_faktur_penjualan', $nofak)
			->where('tabel_rinci_penjualan.kd_barang', $kd_barang)
			->get('tabel_rinci_penjualan');
	}

	public function getStokRetur($kd_barang) {
	    $kdtoko = $this->session->userdata('kd_toko');
		return $this->db->query("SELECT * FROM tabel_stok_toko WHERE kd_barang='$kd_barang' and kd_toko='$kdtoko'");
	}
	public function getStokdetail($nobukti,$tgl) {
	    $kdtoko = $this->session->userdata('kd_toko');
		return $this->db->query("SELECT a.* FROM stokdetaila a,stokheadera b WHERE a.nobukti=b.nobukti and b.kegd='$kdtoko' and b.tgl='$tgl' and a.nobukti='$nobukti' and a.st<>'2' and b.st<>'6'");
	}
    public function dataTransaksi() {     
        $tgl = date("Y-m-d");
		$this->db->select("SUM(IF(idmenu='4',qty,0)) AS miehr,SUM(IF(idmenu='5',qty,0)) AS tophr,SUM(IF(idmenu='2',qty,0)) AS snackhr,SUM(IF(idmenu='6',qty,0)) AS minumhr,
                           SUM(IF(idmenu='4',(bruto+nppn),0)) AS rpmiehr,SUM(IF(idmenu='5',(bruto+nppn),0)) AS rptophr,SUM(IF(idmenu='2',(bruto+nppn),0)) AS rpsnackhr,SUM(IF(idmenu='6',(bruto+nppn),0)) AS rpminumhr,
                           SUM(bruto+nppn) AS totalhr,SUM(bruto+nppn) AS totals,b.nm_toko,a.kdcabang,b.kd_toko");
        $this->db->join('tabel_toko as b','b.kd_toko=a.kdcabang','left');                           
	    $this->db->where('tgl',$tgl);
	    $this->db->where('kdjenis<>','j06');
	    $this->db->group_by('kdcabang');
		return $this->db->get('analisapenjualan as a');	
	}
	 public function dataTransaksibrutokkk() {     
        $tgl = date("Y-m-d");
		$this->db->select("SUM(IF(idmenu='4',qty,0)) AS miehr,SUM(IF(idmenu='5',qty,0)) AS tophr,SUM(IF(idmenu='2',qty,0)) AS snackhr,SUM(IF(idmenu='6',qty,0)) AS minumhr,
                           SUM(IF(idmenu='4',(bruto+nppn),0)) AS rpmiehr,SUM(IF(idmenu='5',(bruto+nppn),0)) AS rptophr,SUM(IF(idmenu='2',(bruto+nppn),0)) AS rpsnackhr,SUM(IF(idmenu='6',(bruto+nppn),0)) AS rpminumhr,
                           SUM(bruto+nppn) AS totalhr,SUM(bruto+nppn) AS totals,b.nm_toko,a.kdcabang,b.kd_toko");
        $this->db->join('tabel_toko as b','b.kd_toko=a.kdcabang','left');                           
	    $this->db->where('tgl',$tgl);
	    $this->db->where('kdjenis<>','j06');
	    $this->db->group_by('kdcabang');
		return $this->db->get('analisapenjualan as a');	
	}
		public function dataTransaksibruto() {     
	    $tgl = date("Y-m-d");
	    $dari=$tgl;
	    $sampai=$tgl;
	    $tgl = date("Y-m-d");
	    $this->db->select("kd_toko,nm_toko,
	                       ifnull((select sum(if(b.harga1>b.harga,(b.jumlah*b.harga1),(b.jumlah*b.harga))) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko  
	                       and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and b.kdpromo<>'048'),0) as bruto, 
	                       ifnull((select sum(if(b.harga1>b.harga,(b.jumlah*b.harga1),(b.jumlah*b.harga))) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko  
	                       and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and b.kdpromo='048'),0) as brutoky, 
	                       ifnull((select sum(b.jumlah*b.harga) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and b.kdpromo<>'048'),0) as bruto1, 
	                       ifnull((select sum(b.jumlah*b.harga) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and b.kdpromo='048'),0) as karya, 
	                       ifnull((select sum(if(ppn='10',((b.jumlah*b.harga)*(b.ppn/100)),0)) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko  
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai'),0) as pb1,  
                           ifnull((select sum(ndisc) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) as ndisc,
                           ifnull((select sum(ongkir) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) as ongkir,
                          ");
		$this->db->where('a.stat','2');
		return $this->db->get('tabel_toko as a');	
	}
	 public function dataTransaksiomset() {     
        $tgl = date("Y-m-d");
		$this->db->select("SUM(IF(idmenu='4',qty,0)) AS miehr,SUM(IF(idmenu='5',qty,0)) AS tophr,SUM(IF(idmenu='2',qty,0)) AS snackhr,SUM(IF(idmenu='6',qty,0)) AS minumhr,
                           SUM(IF(idmenu='4',(bruto+nppn),0)) AS rpmiehr,SUM(IF(idmenu='5',(bruto+nppn),0)) AS rptophr,SUM(IF(idmenu='2',(bruto+nppn),0)) AS rpsnackhr,SUM(IF(idmenu='6',(bruto+nppn),0)) AS rpminumhr,
                           SUM(bruto+nppn) AS totalhr,SUM(bruto+nppn) AS totals,b.nm_toko,a.kdcabang,b.kd_toko");
        $this->db->join('tabel_toko as b','b.kd_toko=a.kdcabang','left');                           
	    $this->db->where('tgl',$tgl);
	    $this->db->where('kdjenis<>','j06');
	    $this->db->group_by('kdcabang');
		return $this->db->get('analisapenjualan as a');	
	}
	public function dataTransaksitunai() {     
        $tgl = date("Y-m-d");
		$this->db->select("SUM(IF(idmenu='4',qty,0)) AS miehr,SUM(IF(idmenu='5',qty,0)) AS tophr,SUM(IF(idmenu='2',qty,0)) AS snackhr,SUM(IF(idmenu='6',qty,0)) AS minumhr,
                           SUM(IF(idmenu='4',(bruto+nppn),0)) AS rpmiehr,SUM(IF(idmenu='5',(bruto+nppn),0)) AS rptophr,SUM(IF(idmenu='2',(bruto+nppn),0)) AS rpsnackhr,SUM(IF(idmenu='6',(bruto+nppn),0)) AS rpminumhr,
                           SUM(bruto+nppn) AS totalhr,SUM(bruto+nppn) AS totals,b.nm_toko,a.kdcabang,b.kd_toko");
        $this->db->join('tabel_toko as b','b.kd_toko=a.kdcabang','left');                           
	    $this->db->where('tgl',$tgl);
	    $this->db->where('kdjenis<>','j06');
	    $this->db->group_by('kdcabang');
		return $this->db->get('analisapenjualan as a');	
	}
	public function dataTransaksifilter($dari,$sampai) {     
	    $tgl = date("Y-m-d");
	    $this->db->select("kd_toko,nm_toko,
	                        ifnull((select sum(b.jumlah) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='4' and b.jnsbarang<>'j06' and b.jnsbarang<>'j10' and b.jnsbarang<>'j11'  
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and (b.jnsbarang='j01' or b.jnsbarang='j07')),0) as miehr,
	                        
	                        ifnull((select sum(b.jumlah) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='4' and b.jnsbarang<>'j06' and b.jnsbarang<>'j10' and b.jnsbarang<>'j11'  
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and b.jnsbarang<>'j01' and b.jnsbarang<>'j07'),0) as miehrt,
	                        
	                        ifnull((select sum(b.jumlah) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='4' and b.jnsbarang<>'j06' and (b.jnsbarang='j10' or b.jnsbarang='j11')  
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai'),0) as mewahmiehr,
	                       ifnull((select sum(b.jumlah) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.jnsbarang='j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai'),0) as ta, 
	                       ifnull((select sum(b.jumlah) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='5' and b.jnsbarang<>'j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai'),0) as tophr,
	                       ifnull((select sum(b.jumlah) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='2' and b.jnsbarang<>'j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai'),0) as snackhr, 
	                       ifnull((select sum(b.jumlah) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='6' and b.jnsbarang<>'j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai'),0) as minumhr, 
	                        
	                        ifnull((select sum(b.sub_total_jual+b.nppn) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='4' and b.jnsbarang<>'j06' and b.jnsbarang<>'j10' and b.jnsbarang<>'j11'  
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and (b.jnsbarang='j01' or b.jnsbarang='j07')),0) as rpmiehr, 
	                        ifnull((select sum(b.sub_total_jual+b.nppn) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='4' and b.jnsbarang<>'j06' and b.jnsbarang<>'j10' and b.jnsbarang<>'j11'  
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and b.jnsbarang<>'j01' and b.jnsbarang<>'j07'),0) as rpmiehrt, 
	                        
	                        
	                       ifnull((select sum(b.sub_total_jual+b.nppn) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='4' and b.jnsbarang<>'j06' and (b.jnsbarang='J10' or b.jnsbarang='J11')
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai'),0) as mewahrpmiehr,  
	                       ifnull((select sum(b.sub_total_jual+b.nppn) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='5' and b.jnsbarang<>'j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai'),0) as rptophr,  
	                       ifnull((select sum(b.sub_total_jual+b.nppn) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='2' and b.jnsbarang<>'j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai'),0) as rpsnackhr,  
	                       ifnull((select sum(b.sub_total_jual+b.nppn) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='6' and b.jnsbarang<>'j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai'),0) as rpminumhr, 
	                       ifnull((select sum(b.sub_total_jual+b.nppn) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and  b.jnsbarang='j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai'),0) as rpta,  
                           ifnull((select sum(ndisc) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) as ndisc,
                           ifnull((select sum(ongkir) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) as ongkir,
                           ifnull((select sum(netto) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) as totalhr,
                           ifnull((select sum(netto) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) as totals,
                           ifnull((select count(*) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai'  and kdcabang=a.kd_toko),0) as nota");
		$this->db->where('a.stat','2');
		return $this->db->get('tabel_toko as a');	
	}
	public function dataTransaksifilterbruto($dari,$sampai) {     
	    $tgl = date("Y-m-d");
	    $this->db->select("kd_toko,nm_toko,
	                       ifnull((select sum(if(b.harga1>b.harga,(b.jumlah*b.harga1),(b.jumlah*b.harga))) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko  
	                       and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and b.kdpromo<>'048'),0) as bruto, 
	                       ifnull((select sum(if(b.harga1>b.harga,(b.jumlah*b.harga1),(b.jumlah*b.harga))) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko  
	                       and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and b.kdpromo='048'),0) as brutoky, 
	                       ifnull((select sum(b.jumlah*b.harga) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and b.kdpromo<>'048'),0) as bruto1, 
	                       ifnull((select sum(b.jumlah*b.harga) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and b.kdpromo='048'),0) as karya, 
	                       ifnull((select sum(if(ppn='10',((b.jumlah*b.harga)*(b.ppn/100)),0)) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko  
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai'),0) as pb1,  
                           ifnull((select sum(ndisc) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) as ndisc,
                           ifnull((select sum(ongkir) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) as ongkir,
                          ");
		$this->db->where('a.stat','2');
		$this->db->where('a.kd_toko !=','tri01');
		return $this->db->get('tabel_toko as a');	
	}
	public function dataTransaksifilterbrutojjj($dari,$sampai) {     
	    $tgl = date("Y-m-d");
	    $tahun=date('Y');
	    $bulan=date('m');
	    $this->db->select("SELECT a.tgl,a.hari,
                    IFNULL((SELECT SUM(IF(b.harga1>0,(b.jumlah*b.harga1),(b.jumlah*b.harga))) FROM tabel_rinci_penjualan b,tabel_penjualan c WHERE b.no_faktur_penjualan=c.no_faktur_penjualan AND 
                    c.selesai='1' AND c.tgl_penjualan =a.`tgl` AND c.kdharga='KH01' AND c.layanan='1'),0) AS brutodin,
                    IFNULL((SELECT SUM(IF(b.harga1>0,(b.jumlah*b.harga1),(b.jumlah*b.harga))) FROM tabel_rinci_penjualan b,tabel_penjualan c WHERE b.no_faktur_penjualan=c.no_faktur_penjualan AND 
                    c.selesai='1' AND c.tgl_penjualan =a.`tgl` AND c.kdharga='KH01' AND c.layanan='2'),0) AS brutota,
                    IFNULL((SELECT SUM(IF(b.harga1>0,(b.jumlah*b.harga1),(b.jumlah*b.harga))) FROM tabel_rinci_penjualan b,tabel_penjualan c WHERE b.no_faktur_penjualan=c.no_faktur_penjualan AND 
                    c.selesai='1' AND c.tgl_penjualan =a.`tgl` AND c.kdharga='KH01' AND c.layanan='3'),0) AS brutodo,
                    IFNULL((SELECT SUM(IF(b.harga1>0,(b.jumlah*b.harga1),(b.jumlah*b.harga))) FROM tabel_rinci_penjualan b,tabel_penjualan c WHERE b.no_faktur_penjualan=c.no_faktur_penjualan AND 
                    c.selesai='1' AND c.tgl_penjualan =a.`tgl` AND c.kdharga='KH02'),0) AS brutogojek,
                    IFNULL((SELECT SUM(IF(b.harga1>0,(b.jumlah*b.harga1),(b.jumlah*b.harga))) FROM tabel_rinci_penjualan b,tabel_penjualan c WHERE b.no_faktur_penjualan=c.no_faktur_penjualan AND 
                    c.selesai='1' AND c.tgl_penjualan =a.`tgl` AND c.kdharga='KH03' ),0) AS brutograb,
                    IFNULL((SELECT SUM(IF(b.harga1>0,(b.jumlah*b.harga1),(b.jumlah*b.harga))) FROM tabel_rinci_penjualan b,tabel_penjualan c WHERE b.no_faktur_penjualan=c.no_faktur_penjualan AND 
                    c.selesai='1' AND c.tgl_penjualan =a.`tgl` AND c.kdharga='KH04' ),0) AS brutosp,

                    IFNULL((SELECT SUM(b.jumlah*b.harga) FROM tabel_rinci_penjualan b,tabel_penjualan c WHERE b.no_faktur_penjualan=c.no_faktur_penjualan AND 
                    c.selesai='1' AND c.tgl_penjualan =a.`tgl` AND c.kdharga='KH01' AND c.layanan='1'),0) AS 1brutodin,
                    IFNULL((SELECT SUM(b.jumlah*b.harga) FROM tabel_rinci_penjualan b,tabel_penjualan c WHERE b.no_faktur_penjualan=c.no_faktur_penjualan AND 
                    c.selesai='1' AND c.tgl_penjualan =a.`tgl` AND c.kdharga='KH01' AND c.layanan='2'),0) AS 1brutota,
                    IFNULL((SELECT SUM(b.jumlah*b.harga) FROM tabel_rinci_penjualan b,tabel_penjualan c WHERE b.no_faktur_penjualan=c.no_faktur_penjualan AND 
                    c.selesai='1' AND c.tgl_penjualan =a.`tgl` AND c.kdharga='KH01' AND c.layanan='3'),0) AS 1brutodo,
                    IFNULL((SELECT SUM(b.jumlah*b.harga) FROM tabel_rinci_penjualan b,tabel_penjualan c WHERE b.no_faktur_penjualan=c.no_faktur_penjualan AND 
                    c.selesai='1' AND c.tgl_penjualan =a.`tgl` AND c.kdharga='KH02'),0) AS 1brutogojek,
                    IFNULL((SELECT SUM(b.jumlah*b.harga) FROM tabel_rinci_penjualan b,tabel_penjualan c WHERE b.no_faktur_penjualan=c.no_faktur_penjualan AND 
                    c.selesai='1' AND c.tgl_penjualan =a.`tgl` AND c.kdharga='KH03' ),0) AS 1brutograb,
                    IFNULL((SELECT SUM(b.jumlah*b.harga) FROM tabel_rinci_penjualan b,tabel_penjualan c WHERE b.no_faktur_penjualan=c.no_faktur_penjualan AND 
                    c.selesai='1' AND c.tgl_penjualan =a.`tgl` AND c.kdharga='KH04' ),0) AS 1brutosp,
                    IFNULL((SELECT SUM(ndisc) FROM tabel_penjualan WHERE selesai='1' AND tgl_penjualan=a.tgl ),0) AS ndisc,
                    IFNULL((SELECT SUM(ongkir) FROM tabel_penjualan WHERE selesai='1' AND tgl_penjualan=a.tgl),0) AS ongkir,
                    IFNULL((SELECT SUM(nppn) FROM tabel_penjualan WHERE selesai='1' AND tgl_penjualan=a.tgl),0) AS nppn,
                          ");
		$this->db->where('a.tahun',$tahun);
		$this->db->where('a.bulan',$bulan);
		return $this->db->get('kalender as a');	
	}
public function dataTransaksifilterom($dari,$sampai) {     
	    $tgl = date("Y-m-d");
	    $bulan=date('m',strtotime($sampai));
	    $tahun=date('Y',strtotime($sampai));
	    $awal=$tahun.'-'.$bulan.-'01';
	    $hari=cal_days_in_month(CAL_GREGORIAN, $bulan, $tahun);
	    $this->db->select("a.kd_toko,a.nm_toko,
	                       ifnull((select sum(nota*'$hari') from tabel_target where kdcabang=a.kd_toko and bulan='$bulan' and tahun='$tahun'),0) as tnota,
	                       ifnull((select nota from tabel_target where kdcabang=a.kd_toko and bulan='$bulan' and tahun='$tahun'),0) as thari,
	                       ifnull((select nilai from tabel_target where kdcabang=a.kd_toko and bulan='$bulan' and tahun='$tahun'),0) as trp,
	                       ((datediff('$sampai','$dari'))+1) as jhari,
	                       ifnull((select (nota*((datediff('$sampai','$dari'))+1)) from tabel_target where kdcabang=a.kd_toko and bulan='$bulan' and tahun='$tahun'),0) as starget,
	                       ifnull((select sum(b.jumlah) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='4' and b.jnsbarang<>'j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and c.layanan='3'),0) as miehr,
	                       ifnull((select sum(b.jumlah) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.jnsbarang='j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and c.layanan='3'),0) as ta, 
	                       ifnull((select sum(b.jumlah) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='5' and b.jnsbarang<>'j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and c.layanan='3'),0) as tophr,
	                       ifnull((select sum(b.jumlah) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='2' and b.jnsbarang<>'j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and c.layanan='3'),0) as snackhr, 
	                       ifnull((select sum(b.jumlah) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='6' and b.jnsbarang<>'j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and c.layanan='3'),0) as minumhr, 
	                       ifnull((select sum(b.sub_total_jual+b.nppn) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='4' and b.jnsbarang<>'j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and c.layanan='3'),0) as rpmiehr, 
	                       ifnull((select sum(b.sub_total_jual+b.nppn) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='5' and b.jnsbarang<>'j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and c.layanan='3'),0) as rptophr,  
	                       ifnull((select sum(b.sub_total_jual+b.nppn) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='2' and b.jnsbarang<>'j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and c.layanan='3'),0) as rpsnackhr,  
	                       ifnull((select sum(b.sub_total_jual+b.nppn) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and b.idmenu='6' and b.jnsbarang<>'j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and c.layanan='3'),0) as rpminumhr, 
	                       ifnull((select sum(b.sub_total_jual+b.nppn) from tabel_rinci_penjualan b,tabel_penjualan c where b.no_faktur_penjualan=c.no_faktur_penjualan and c.kdcabang=a.kd_toko and  b.jnsbarang='j06' 
	                        and c.selesai='1' and c.tgl_penjualan >='$dari' and c.tgl_penjualan <='$sampai' and c.layanan='3'),0) as rpta,  
                           ifnull((select sum(ndisc) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko and layanan='3'),0) as ndisc,
                           ifnull((select sum(ongkir) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko and layanan='3'),0) as ongkir,
                           ifnull((select sum(netto) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko and layanan='3'),0) as totalhr,
                           ifnull((select sum(netto) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko and layanan='3'),0) as totals,
                           ifnull((select sum(netto) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) as totalall,
                           ifnull((select count(*) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai'  and kdcabang=a.kd_toko and layanan='3'),0) as nota,
                           ifnull((select count(*) from tabel_penjualan where selesai='1' and tgl_penjualan>='$awal' and tgl_penjualan<='$sampai'  and kdcabang=a.kd_toko and layanan='3'),0) as snota");
		$this->db->where('a.stat','2');
		$this->db->where('a.drgd !=','');
		return $this->db->get('tabel_toko as a');	
	}
	

public function dataTransaksifilteromset($dari,$sampai) {     
	    $tgl = date("Y-m-d");
	    $this->db->select("kd_toko,nm_toko,
	                       IFNULL((SELECT SUM(total_penjualan_sdiskon) FROM tabel_penjualan WHERE selesai='1' AND tgl_penjualan>='$dari' AND tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) AS omset1,
	                       IFNULL((SELECT SUM(total_penjualan_sdiskon) FROM tabel_penjualan WHERE waktu>='16:01:00' and selesai='1' AND tgl_penjualan>='$dari' AND tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) AS omset2,
	                       IFNULL((SELECT SUM(nppn) FROM tabel_penjualan WHERE selesai='1' AND tgl_penjualan>='$dari' AND tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) AS pb11,
	                       IFNULL((SELECT SUM(nppn) FROM tabel_penjualan WHERE waktu>='16:01:00' and selesai='1' AND tgl_penjualan>='$dari' AND tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) AS pb12,
	                       ifnull((select sum(netto) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) as totalhr,
	                       ifnull((select sum(ongkir) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) as ongkir,
	                       ifnull((select sum(biaya) from tabel_pendapatan where tgl>='$dari' and tgl<='$sampai' and kdcabang=a.kd_toko),0) as dapat,
                           ifnull((select sum(ndisc) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) as ndisc,
                           IFNULL((SELECT SUM(netto) FROM tabel_penjualan WHERE selesai='1' and kdharga='KH02' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) AS gojek,
                           IFNULL((SELECT SUM(netto) FROM tabel_penjualan WHERE selesai='1' and kdharga='KH03' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) AS grab,
                           IFNULL((SELECT SUM(netto) FROM tabel_penjualan WHERE selesai='1' and kdharga='KH04' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) AS sfood,
                           IFNULL((SELECT SUM(netto) FROM tabel_penjualan WHERE selesai='1' and kdharga='KH06' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) AS maxim,
                           IFNULL((SELECT SUM(netto) FROM tabel_penjualan WHERE selesai='1' and kdharga='KH01' and jnsbayar='3' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) AS gopay,
                           IFNULL((SELECT SUM(netto) FROM tabel_penjualan WHERE selesai='1' and kdharga='KH01' and jnsbayar='7' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) AS ovo,
                           IFNULL((SELECT SUM(netto) FROM tabel_penjualan WHERE selesai='1' and kdharga='KH01' and jnsbayar='5' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) AS spay,
                           IFNULL((SELECT SUM(netto) FROM tabel_penjualan WHERE selesai='1' and kdharga='KH01' and jnsbayar='8' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai' and kdcabang=a.kd_toko),0) AS qris,
                           ifnull((select sum(biaya) from tabel_biaya where jenis<>'1120.103' and tgl>='$dari' and tgl<='$sampai' and kdcabang=a.kd_toko),0) as biaya,
                           ifnull((select sum(5000-ongkir) from tabel_penjualan WHERE selesai='1' AND tgl_penjualan>='$dari' AND tgl_penjualan<='$sampai' and kdcabang=a.kd_toko and layanan='3' and kdharga='KH01'),0) as biaya1,	
                           ifnull((select sum(biaya) from tabel_biaya where jenis='1120.103' and tgl>='$dari' and tgl<='$sampai' and kdcabang=a.kd_toko),0) as transfer,
                           ifnull((select count(*) from tabel_penjualan where selesai='1' and tgl_penjualan>='$dari' and tgl_penjualan<='$sampai'  and kdcabang=a.kd_toko),0) as nota");
		$this->db->where('a.stat','2');
		return $this->db->get('tabel_toko as a');	
	}
	public function dataTransaksifiltertunai($dari,$sampai) {     
	    $tgl = date("Y-m-d");
	    $tglawal=date("Y-m-01");
	    $this->db->select("kd_toko,nm_toko,
	                       ifnull((select sum(biaya) from tabel_setoran where tgl<'$dari' and tgl>='$tglawal' and kdcabang=a.kd_toko),0) as awal,
	                       ifnull((select sum(biaya) from tabel_setoran where tgl>='$dari' and tgl<='$sampai' and kdcabang=a.kd_toko),0) as dapat,
                           ifnull((select sum(biaya) from tabel_biaya1 where tgl>='$dari' and tgl<='$sampai' and kdcabang=a.kd_toko),0) as biaya
                           ");
		$this->db->where('a.divisi !=','9');
		return $this->db->get('tabel_toko as a');	
	}
	
	public function datalistfilterrekap($dari,$sampai) {     
	    $tgl = date("Y-m-d");
	    $this->db->select("b.kd_toko,b.nm_toko,a.tgl,if(a.stat='1','Close','Input') as stat");
	    $this->db->join('tabel_toko as b','b.kd_toko=a.kdcabang','left');
        $this->db->where('tgl BETWEEN "' . date('Y-m-d', strtotime($dari)) . '" and "' . date('Y-m-d', strtotime($sampai)) . '"');
        $this->db->order_by('a.kdcabang,a.tgl');
		return $this->db->get('tabel_close as a');	
	}
	
	public function datalistrekap() {     
	    $tgl = date("Y-m-d");
	    $this->db->select("b.kd_toko,b.nm_toko,a.tgl,if(a.stat='1','Close','Input') as stat");
	    $this->db->join('tabel_toko as b','b.kd_toko=a.kdcabang','left');
        $this->db->where('tgl BETWEEN "' . date('Y-m-d', strtotime($tgl)) . '" and "' . date('Y-m-d', strtotime($tgl)) . '"');
        $this->db->order_by('a.kdcabang,a.tgl');
		return $this->db->get('tabel_close as a');	
	}
	
	public function rekapkedai($kdcabang,$bulan,$npb1,$tahun) {
	    
	    $persen=1;
	    if($npb1>0) {
	        $caritotal=$this->db->query("select sum(nppn) as total from rekapharian where kdcabang='$kdcabang'  and month(tgl)='$bulan' and year(tgl)='$tahun' and (jenis='4' or jenis='5');");
	        $total=$caritotal->row_array();
	        $persen=($npb1/$total['total']);
	    }
	    
	    if($kdcabang<>'ZZ') {
	        if($npb1==0) {
	            return $this->db->select ("a.kdcabang,b.nm_toko,a.tgl,IF(DAYOFWEEK(a.tgl)='1','Minggu',IF(DAYOFWEEK(a.tgl)='2','Senin',IF(DAYOFWEEK(a.tgl)='3','Selasa',
                IF(DAYOFWEEK(a.tgl)='4','Rabu',IF(DAYOFWEEK(a.tgl)='5','Kamis',IF(DAYOFWEEK(a.tgl)='6','Jumat',IF(DAYOFWEEK(a.tgl)='7','Sabtu',''))))))) hari,
                SUM(a.nilai) rtotal,SUM(a.nppn) rnppn,SUM(a.nilai+a.nppn) rnetto,SUM(a.nilai1) ptotal,SUM(a.nppn1) pnppn,SUM(a.nilai1+a.nppn1) pnetto,sum(if(a.kode<>'KH01',a.nilai,0)) as ojol,
                ifnull((select sum(nilai) from rekapharian where jenis='3' and (kode='2' or kode='5' or kode='7' or kode='8') and tgl=a.tgl and kdcabang=a.kdcabang),0) as bank
                ")
                ->join('tabel_toko as b','b.kd_toko=a.kdcabang','left')
                ->where('a.kdcabang',$kdcabang)
                ->where('month(a.tgl)',$bulan)
                ->where('year(a.tgl)',$tahun)
                ->where_in('a.jenis',array('4','5'))
                ->group_by('a.tgl')
                ->get('rekapharian as a');
	        } else {
	            return $this->db->select ("a.kdcabang,b.nm_toko,a.tgl,IF(DAYOFWEEK(a.tgl)='1','Minggu',IF(DAYOFWEEK(a.tgl)='2','Senin',IF(DAYOFWEEK(a.tgl)='3','Selasa',
                IF(DAYOFWEEK(a.tgl)='4','Rabu',IF(DAYOFWEEK(a.tgl)='5','Kamis',IF(DAYOFWEEK(a.tgl)='6','Jumat',IF(DAYOFWEEK(a.tgl)='7','Sabtu',''))))))) hari,
                SUM(a.nilai) rtotal,SUM(a.nppn) rnppn,SUM(a.nilai+a.nppn) rnetto,SUM(a.nilai*'$persen') ptotal,SUM((a.nilai*'$persen')*(10/100)) pnppn,SUM((a.nilai+a.nppn)*'$persen') pnetto")
                ->join('tabel_toko as b','b.kd_toko=a.kdcabang','left')
                ->where('a.kdcabang',$kdcabang)
                ->where('month(a.tgl)',$bulan)
                ->where('year(a.tgl)',$tahun)
                ->where_in('a.jenis',array('4','5'))
                ->group_by('a.tgl')
                ->get('rekapharian as a');
	        }
	    } else {
	     return $this->db->select ("a.kdcabang,b.nm_toko,date_format(a.tgl,'%M') tgl,'Rekap' hari,
          SUM(a.nilai) rtotal,SUM(a.nppn) rnppn,SUM(a.nilai+a.nppn) rnetto,SUM(a.nilai1) ptotal,SUM(a.nppn1) pnppn,SUM(a.nilai1+a.nppn1) pnetto")
        ->join('tabel_toko as b','b.kd_toko=a.kdcabang','left')
        ->where('month(a.tgl)',$bulan)
        ->where('year(a.tgl)',$tahun)
        ->where_in('a.jenis',array('4','5'))
        ->where('b.stat','2')
        ->group_by('a.kdcabang')
        ->get('rekapharian as a');   
	    }
	}
	public function rekapkedaiex($kdcabang,$bulan,$npb1,$tahun) {
	    
	    $persen=1;
	    if($npb1>0) {
	        $caritotal=$this->db6->query("select sum(nppn) as total from rekapharian where kdcabang='$kdcabang'  and month(tgl)='$bulan' and year(tgl)='$tahun' and (jenis='4' or jenis='5');");
	        $total=$caritotal->row_array();
	        $persen=($npb1/$total['total']);
	    }
	    
	    if($kdcabang<>'ZZ') {
	        if($npb1==0) {
	            return $this->db6->select ("a.kdcabang,b.nm_toko,a.tgl,IF(DAYOFWEEK(a.tgl)='1','Minggu',IF(DAYOFWEEK(a.tgl)='2','Senin',IF(DAYOFWEEK(a.tgl)='3','Selasa',
                IF(DAYOFWEEK(a.tgl)='4','Rabu',IF(DAYOFWEEK(a.tgl)='5','Kamis',IF(DAYOFWEEK(a.tgl)='6','Jumat',IF(DAYOFWEEK(a.tgl)='7','Sabtu',''))))))) hari,
                SUM(a.nilai) rtotal,SUM(a.nppn) rnppn,SUM(a.nilai+a.nppn) rnetto,SUM(a.nilai1) ptotal,SUM(a.nppn1) pnppn,SUM(a.nilai1+a.nppn1) pnetto,sum(if(a.kode<>'KH01',a.nilai,0)) as ojol,
                ifnull((select sum(nilai) from rekapharian where jenis='3' and (kode='2' or kode='5' or kode='7' or kode='8') and tgl=a.tgl and kdcabang=a.kdcabang),0) as bank
                ")
                ->join('tabel_toko as b','b.kd_toko=a.kdcabang','left')
                ->where('a.kdcabang',$kdcabang)
                ->where('month(a.tgl)',$bulan)
                ->where('year(a.tgl)',$tahun)
                ->where_in('a.jenis',array('4','5'))
                ->group_by('a.tgl')
                ->get('rekapharian as a');
	        } else {
	            return $this->db6->select ("a.kdcabang,b.nm_toko,a.tgl,IF(DAYOFWEEK(a.tgl)='1','Minggu',IF(DAYOFWEEK(a.tgl)='2','Senin',IF(DAYOFWEEK(a.tgl)='3','Selasa',
                IF(DAYOFWEEK(a.tgl)='4','Rabu',IF(DAYOFWEEK(a.tgl)='5','Kamis',IF(DAYOFWEEK(a.tgl)='6','Jumat',IF(DAYOFWEEK(a.tgl)='7','Sabtu',''))))))) hari,
                SUM(a.nilai) rtotal,SUM(a.nppn) rnppn,SUM(a.nilai+a.nppn) rnetto,SUM(a.nilai*'$persen') ptotal,SUM((a.nilai*'$persen')*(10/100)) pnppn,SUM((a.nilai+a.nppn)*'$persen') pnetto")
                ->join('tabel_toko as b','b.kd_toko=a.kdcabang','left')
                ->where('a.kdcabang',$kdcabang)
                ->where('month(a.tgl)',$bulan)
                ->where('year(a.tgl)',$tahun)
                ->where_in('a.jenis',array('4','5'))
                ->group_by('a.tgl')
                ->get('rekapharian as a');
	        }
	    } else {
	     return $this->db6->select ("a.kdcabang,b.nm_toko,date_format(a.tgl,'%M') tgl,'Rekap' hari,
          SUM(a.nilai) rtotal,SUM(a.nppn) rnppn,SUM(a.nilai+a.nppn) rnetto,SUM(a.nilai1) ptotal,SUM(a.nppn1) pnppn,SUM(a.nilai1+a.nppn1) pnetto")
        ->join('tabel_toko as b','b.kd_toko=a.kdcabang','left')
        ->where('month(a.tgl)',$bulan)
        ->where('year(a.tgl)',$tahun)
        ->where_in('a.jenis',array('4','5'))
        ->where('b.stat','2')
        ->group_by('a.kdcabang')
        ->get('rekapharian as a');   
	    }
	}	
public function rekapkedaiall($kdcabang,$bulan,$npb1) {
	    $tahun=date('Y');
	    $persen=1;
	    if($npb1>0) {
	        $caritotal=$this->db->query("select sum(nppn) as total from rekapharian where kdcabang='$kdcabang'  and month(tgl)='$bulan' and year(tgl)='$tahun' and (jenis='4' or jenis='5');");
	        $total=$caritotal->row_array();
	        $persen=($npb1/$total['total']);
	    }
	    
	    if($kdcabang<>'ZZ') {
	        if($npb1==0) {
	            return $this->db->select ("a.kdcabang,b.nm_toko,a.tgl,IF(DAYOFWEEK(a.tgl)='1','Minggu',IF(DAYOFWEEK(a.tgl)='2','Senin',IF(DAYOFWEEK(a.tgl)='3','Selasa',
                IF(DAYOFWEEK(a.tgl)='4','Rabu',IF(DAYOFWEEK(a.tgl)='5','Kamis',IF(DAYOFWEEK(a.tgl)='6','Jumat',IF(DAYOFWEEK(a.tgl)='7','Sabtu',''))))))) hari,
                SUM(a.nilai) rtotal,SUM(a.nppn) rnppn,SUM(a.nilai+a.nppn) rnetto,SUM(a.nilai1) ptotal,SUM(a.nppn1) pnppn,SUM(a.nilai1+a.nppn1) pnetto,sum(if(a.kode<>'KH01',a.nilai,0)) as ojol,
                ifnull((select sum(nilai) from rekapharian where jenis='3' and (kode='2' or kode='5' or kode='7' or kode='8') and tgl=a.tgl and kdcabang=a.kdcabang),0) as bank
                ")
                ->join('tabel_toko as b','b.kd_toko=a.kdcabang','left')
                ->where('a.kdcabang',$kdcabang)
                ->where('month(a.tgl)',$bulan)
                ->where('year(a.tgl)',$tahun)
                ->where_in('a.jenis',array('4','5'))
                ->group_by('a.tgl')
                ->get('rekapharian as a');
	        } else {
	            return $this->db->select ("a.kdcabang,b.nm_toko,a.tgl,IF(DAYOFWEEK(a.tgl)='1','Minggu',IF(DAYOFWEEK(a.tgl)='2','Senin',IF(DAYOFWEEK(a.tgl)='3','Selasa',
                IF(DAYOFWEEK(a.tgl)='4','Rabu',IF(DAYOFWEEK(a.tgl)='5','Kamis',IF(DAYOFWEEK(a.tgl)='6','Jumat',IF(DAYOFWEEK(a.tgl)='7','Sabtu',''))))))) hari,
                SUM(a.nilai) rtotal,SUM(a.nppn) rnppn,SUM(a.nilai+a.nppn) rnetto,SUM(a.nilai*'$persen') ptotal,SUM((a.nilai*'$persen')*(10/100)) pnppn,SUM((a.nilai+a.nppn)*'$persen') pnetto")
                ->join('tabel_toko as b','b.kd_toko=a.kdcabang','left')
                ->where('a.kdcabang',$kdcabang)
                ->where('month(a.tgl)',$bulan)
                ->where('year(a.tgl)',$tahun)
                ->where_in('a.jenis',array('4','5'))
                ->group_by('a.tgl')
                ->get('rekapharian as a');
	        }
	    } else {
	     return $this->db->select ("
	               a.tgl_penjualan,IF(DAYOFWEEK(a.tgl_penjualan)='1','Minggu',IF(DAYOFWEEK(a.tgl_penjualan)='2','Senin',IF(DAYOFWEEK(a.tgl_penjualan)='3','Selasa',
                   IF(DAYOFWEEK(a.tgl_penjualan)='4','Rabu',IF(DAYOFWEEK(a.tgl_penjualan)='5','Kamis',IF(DAYOFWEEK(a.tgl_penjualan)='6','Jumat',IF(DAYOFWEEK(a.tgl_penjualan)='7','Sabtu',''))))))) hari,
                   sum(a.nppn) as pb1,sum(a.ndisc) as ndisc,sum(a.ongkir) as ongkir,
	               ifnull((select sum(if(b.harga1>0,(b.jumlah*b.harga1),(b.jumlah*b.harga))) from tabel_rinci_penjualan b where b.no_faktur_penjualan=a.no_faktur_penjualan),0) as bruto, 
	               ifnull((select sum(b.jumlah*b.harga) from tabel_rinci_penjualan bwhere b.no_faktur_penjualan=a.no_faktur_penjualan),0) as bruto1, 
	              ")
        ->where('month(a.tgl_penjualan)',$bulan)
        ->where('year(a.tgl_penjualan)',$tahun)
        ->where('a.selesai','1')
        ->group_by('a.tgl_penjualan')
        ->get('tabel_penjualan as a');   
	    }
	}
	public function dataTransaksiHariIni($tgl,$kdtoko) {
	    $ymd = date('ymd');
	    $wafile=$kdtoko.date('ymd');
        $this->db->query("delete from tanggal where  kdcabang='$kdtoko';");
		$this->db->query("delete from rekapharian where jenis<>'6' and tgl='$tgl' and  kdcabang='$kdtoko';");
		$this->db->query("insert ignore into tanggal(tgl,kdcabang) value ('$tgl','$kdtoko');");
		if ($kdtoko !='zzzzz') {
		    $this->db->query("insert ignore into tabel_close(kdcabang,tgl,stat) value ('$kdtoko','$tgl','0');");
		   // $this->db5->query("insert ignore into tabel_close(kdcabang,tgl,stat) value ('$kdtoko','$tgl','0');");
		    }
		
		$this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						 SELECT '1','$tgl','$kdtoko',a.kdharga,a.keterangan,
						 IFNULL((SELECT SUM(total_penjualan_sdiskon) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,
						 IFNULL((SELECT SUM(nppn) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn
						 FROM kodeharga a;");					 				 
		$this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						 SELECT '2','$tgl','$kdtoko',a.kdharga,a.keterangan,IFNULL((SELECT SUM(ndisc) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,0
						 FROM kodeharga a;");				 
		$this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						 select 'A','$tgl','$kdtoko','A','Promo DO',count(*) as jnota,sum(5000-ongkir) ongkir from tabel_penjualan WHERE selesai='1' AND tgl_penjualan='$tgl' and kdcabang='$kdtoko' and layanan='3' and kdharga='KH01';");		 
						 
        $this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						 SELECT '7','$tgl','$kdtoko',a.kdharga,a.keterangan,IFNULL((SELECT SUM(ongkir) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,0
						 FROM kodeharga a;");				 						 
						 
		$this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						 SELECT '3','$tgl','$kdtoko',a.kdbayar,a.keterangan,
						 IFNULL((SELECT SUM(netto) FROM tabel_penjualan WHERE selesai='1' and jnsbayar=a.kdbayar AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,
						 IFNULL((SELECT SUM(nppn) FROM tabel_penjualan WHERE selesai='1' and jnsbayar=a.kdbayar AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn
						 FROM jenispembayaran a where kdbayar<>'1';");	
						 
		$this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn,nilai1,nppn1,nilaiex,nppnex)
						 SELECT '4','$tgl','$kdtoko',a.kdharga,a.keterangan,
						 IFNULL((SELECT SUM(total_penjualan_sdiskon) FROM tabel_penjualan WHERE waktu<'16:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,
						 IFNULL((SELECT SUM(nppn) FROM tabel_penjualan WHERE waktu<'16:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn,
						 IFNULL((SELECT SUM(b.total_penjualan_sdiskon*(c.persen/100)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu<'16:01:00' and b.kdcabang=c.kd_toko and b.selesai='1' and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS reg1,
						 IFNULL((SELECT SUM(b.nppn*(c.persen/100)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu<'16:01:00' and b.selesai='1' and b.kdcabang=c.kd_toko and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS nppn1,
						 IFNULL((SELECT SUM(if(b.kdharga='KH01',b.total_penjualan_sdiskon*(c.exter/100),b.total_penjualan_sdiskon)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu<'16:01:00' and b.kdcabang=c.kd_toko and b.selesai='1' and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS reg1ex,
						 IFNULL((SELECT SUM(if(b.kdharga='KH01',b.nppn*(c.exter/100),b.nppn)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu<'16:01:00' and b.selesai='1' and b.kdcabang=c.kd_toko and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS nppn1ex
						 FROM kodeharga a;");				 
		$this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn,nilai1,nppn1,nilaiex,nppnex)
						 SELECT '5','$tgl','$kdtoko',a.kdharga,a.keterangan,
						 IFNULL((SELECT SUM(total_penjualan_sdiskon) FROM tabel_penjualan WHERE waktu>='16:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,
						 IFNULL((SELECT SUM(nppn) FROM tabel_penjualan WHERE waktu>='16:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn,
						 
						 IFNULL((SELECT SUM(b.total_penjualan_sdiskon*(c.persen/100)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu>='16:01:00' and b.kdcabang=c.kd_toko and b.selesai='1' and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS reg1,
						 IFNULL((SELECT SUM(b.nppn*(c.persen/100)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu>='16:01:00' and b.selesai='1' and b.kdcabang=c.kd_toko and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS nppn1,
						 IFNULL((SELECT SUM(if(b.kdharga='KH01',b.total_penjualan_sdiskon*(c.exter/100),b.total_penjualan_sdiskon)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu>='16:01:00' and b.kdcabang=c.kd_toko and b.selesai='1' and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS reg1ex,
						 IFNULL((SELECT SUM(if(b.kdharga='KH01',b.nppn*(c.exter/100),b.nppn)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu>='16:01:00' and b.selesai='1' and b.kdcabang=c.kd_toko and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS nppn1ex
						 FROM kodeharga a;");
						 
	        
		if ($kdtoko =='NJK01' || $kdtoko =='KDR05' || $kdtoko =='KNR01' || $kdtoko =='JBG01' || $kdtoko =='JBG03' || $kdtoko =='NJK03' || $kdtoko =='MJK01'  ) {
		        $up=$this->db->query("update tabel_penjualan set statuswa='Gagal' where struk='1' and statuswa='';");
		        $wa1=$this->db2->query("select sent.status,sent.wa_file from sent where left(wa_file,11) like '$wafile';");				 
		        if ($wa1->num_rows() > 0) {
			        foreach ($wa1->result() as $data) {
			            $nobukwa=$data->wa_file;
			            if($data->status=='fail') {
			                    $statwa='Gagal';
			            }
			            if($data->status==NULL) {
			                    $statwa='Terkirim';
			            }
				    $hasil = array(
					    'statuswa' => $statwa,
				        );				 
				    	$this->db->where('no_faktur_penjualan', $nobukwa);
			            $this->db->update('tabel_penjualan', $hasil);    
		            }
		        } 
		}      
		if ($kdtoko =='KDR01' || $kdtoko =='KDR02' || $kdtoko =='KDR03' || $kdtoko =='NJK02' || $kdtoko =='BLT01' || $kdtoko =='KDR04') {
		        $up=$this->db->query("update tabel_penjualan set statuswa='Gagal' where struk='1' and statuswa='';");
		        $wa1=$this->db2->query("select sent.status,sent.wa_file from sent where left(wa_file,11) like '$wafile';");				 
		        if ($wa1->num_rows() > 0) {
			        foreach ($wa1->result() as $data) {
			            $nobukwa=$data->wa_file;
			            if($data->status=='fail') {
			                    $statwa='Gagal';
			            }
			            if($data->status==NULL) {
			                    $statwa='Terkirim';
			            }
				    $hasil = array(
					    'statuswa' => $statwa,
				        );				 
				    	$this->db->where('no_faktur_penjualan', $nobukwa);
			            $this->db->update('tabel_penjualan', $hasil);    
		            }
		        } 
		
		}
		return $this->db->select('waktu,total_penjualan,ndisc as diskon,netto as total_penjualan_sdiskon,ket,id_user,ongkir,no_faktur_penjualan,tgl_penjualan,nppn,kdcabang,kdharga,keter,if(statuswa="","Print",statuswa) as stwa,jnsbayar,
		                        if(jnsbayar="1","Cash",if(jnsbayar="2","Debet",if(jnsbayar="3","Gopay",if(jnsbayar="4","Grabfood",if(jnsbayar="6","Shopeepay","OVO"))))) as jbayar,if(selesai="1","Selesai",if(selesai="4","Batal","Pending")) as selesai,note,notelp,
								IF(kdharga="KH01",IF(layanan="1",total_penjualan_sdiskon,0),0) AS reg,
								IF(kdharga="KH01",IF(layanan="2",total_penjualan_sdiskon,0),0) AS regta,
								IF(kdharga="KH01",IF(layanan="3",total_penjualan_sdiskon,0),0) AS regdo,
								IF(kdharga="KH02",total_penjualan_sdiskon,0) AS gofood,
								IF(kdharga="KH03",total_penjualan_sdiskon,0) AS grab,
								IF(kdharga="KH04",total_penjualan_sdiskon,0) AS shopee,
								IF(kdharga="KH05",total_penjualan_sdiskon,0) AS regma
								')
			
			->where('tabel_penjualan.tgl_penjualan', $tgl)
			->where('tabel_penjualan.selesai', '1')
			->where('tabel_penjualan.kdcabang', $kdtoko)
			->order_by('no_faktur_penjualan')
			->get('tabel_penjualan');
	
	}
		public function dataTransaksiHariInis($tgl,$kdtoko,$stgl) {
	    $ymd = date('ymd');
	    if($tgl==null) {
	        $tgl=date('y-m-d');
	    }
	    $wafile=$kdtoko.date('ymd');
        $this->db->query("delete from tanggal where  kdcabang='$kdtoko';");
		$this->db->query("delete from rekapharian where jenis<>'6' and tgl='$tgl' and  kdcabang='$kdtoko';");
		$this->db->query("insert ignore into tanggal(tgl,kdcabang) value ('$tgl','$kdtoko');");
		if ($kdtoko !='zzzzz') {
		    $this->db->query("insert ignore into tabel_close(kdcabang,tgl,stat) value ('$kdtoko','$tgl','0');");
		   // $this->db5->query("insert ignore into tabel_close(kdcabang,tgl,stat) value ('$kdtoko','$tgl','0');");
		    }
		
		$this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						 SELECT '1','$tgl','$kdtoko',a.kdharga,a.keterangan,
						 IFNULL((SELECT SUM(total_penjualan_sdiskon) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,
						 IFNULL((SELECT SUM(nppn) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn
						 FROM kodeharga a;");					 				 
		$this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						 SELECT '2','$tgl','$kdtoko',a.kdharga,a.keterangan,IFNULL((SELECT SUM(ndisc) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,0
						 FROM kodeharga a;");				 
		$this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						 select 'A','$tgl','$kdtoko','A','Promo DO',count(*) as jnota,sum(5000-ongkir) ongkir from tabel_penjualan WHERE selesai='1' AND tgl_penjualan='$tgl' and kdcabang='$kdtoko' and layanan='3' and kdharga='KH01';");		 
						 
        $this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						 SELECT '7','$tgl','$kdtoko',a.kdharga,a.keterangan,IFNULL((SELECT SUM(ongkir) FROM tabel_penjualan WHERE selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,0
						 FROM kodeharga a;");				 						 
						 
		$this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						 SELECT '3','$tgl','$kdtoko',a.kdbayar,a.keterangan,
						 IFNULL((SELECT SUM(netto) FROM tabel_penjualan WHERE selesai='1' and jnsbayar=a.kdbayar AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,
						 IFNULL((SELECT SUM(nppn) FROM tabel_penjualan WHERE selesai='1' and jnsbayar=a.kdbayar AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn
						 FROM jenispembayaran a where kdbayar<>'1';");	
						 
		$this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn,nilai1,nppn1,nilaiex,nppnex)
						 SELECT '4','$tgl','$kdtoko',a.kdharga,a.keterangan,
						 IFNULL((SELECT SUM(total_penjualan_sdiskon) FROM tabel_penjualan WHERE waktu<'16:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,
						 IFNULL((SELECT SUM(nppn) FROM tabel_penjualan WHERE waktu<'16:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn,
						 IFNULL((SELECT SUM(b.total_penjualan_sdiskon*(c.persen/100)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu<'16:01:00' and b.kdcabang=c.kd_toko and b.selesai='1' and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS reg1,
						 IFNULL((SELECT SUM(b.nppn*(c.persen/100)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu<'16:01:00' and b.selesai='1' and b.kdcabang=c.kd_toko and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS nppn1,
						 IFNULL((SELECT SUM(if(b.kdharga='KH01',b.total_penjualan_sdiskon*(c.exter/100),b.total_penjualan_sdiskon)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu<'16:01:00' and b.kdcabang=c.kd_toko and b.selesai='1' and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS reg1ex,
						 IFNULL((SELECT SUM(if(b.kdharga='KH01',b.nppn*(c.exter/100),b.nppn)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu<'16:01:00' and b.selesai='1' and b.kdcabang=c.kd_toko and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS nppn1ex
						 FROM kodeharga a;");				 
		$this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn,nilai1,nppn1,nilaiex,nppnex)
						 SELECT '5','$tgl','$kdtoko',a.kdharga,a.keterangan,
						 IFNULL((SELECT SUM(total_penjualan_sdiskon) FROM tabel_penjualan WHERE waktu>='16:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS reg,
						 IFNULL((SELECT SUM(nppn) FROM tabel_penjualan WHERE waktu>='16:01:00' and selesai='1' and kdharga=a.kdharga AND tgl_penjualan='$tgl' and kdcabang='$kdtoko'),0) AS nppn,
						 
						 IFNULL((SELECT SUM(b.total_penjualan_sdiskon*(c.persen/100)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu>='16:01:00' and b.kdcabang=c.kd_toko and b.selesai='1' and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS reg1,
						 IFNULL((SELECT SUM(b.nppn*(c.persen/100)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu>='16:01:00' and b.selesai='1' and b.kdcabang=c.kd_toko and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS nppn1,
						 IFNULL((SELECT SUM(if(b.kdharga='KH01',b.total_penjualan_sdiskon*(c.exter/100),b.total_penjualan_sdiskon)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu>='16:01:00' and b.kdcabang=c.kd_toko and b.selesai='1' and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS reg1ex,
						 IFNULL((SELECT SUM(if(b.kdharga='KH01',b.nppn*(c.exter/100),b.nppn)) FROM tabel_penjualan b,tabel_toko c WHERE b.waktu>='16:01:00' and b.selesai='1' and b.kdcabang=c.kd_toko and b.kdharga=a.kdharga AND b.tgl_penjualan='$tgl' and b.kdcabang='$kdtoko'),0) AS nppn1ex
						 FROM kodeharga a;");
						 
	        
		if ($kdtoko =='NJK01' || $kdtoko =='KDR05' || $kdtoko =='KNR01' || $kdtoko =='JBG01' || $kdtoko =='JBG03' || $kdtoko =='NJK03' || $kdtoko =='MJK01'  ) {
		        $up=$this->db->query("update tabel_penjualan set statuswa='Gagal' where struk='1' and statuswa='';");
		        $wa1=$this->db2->query("select sent.status,sent.wa_file from sent where left(wa_file,11) like '$wafile';");				 
		        if ($wa1->num_rows() > 0) {
			        foreach ($wa1->result() as $data) {
			            $nobukwa=$data->wa_file;
			            if($data->status=='fail') {
			                    $statwa='Gagal';
			            }
			            if($data->status==NULL) {
			                    $statwa='Terkirim';
			            }
				    $hasil = array(
					    'statuswa' => $statwa,
				        );				 
				    	$this->db->where('no_faktur_penjualan', $nobukwa);
			            $this->db->update('tabel_penjualan', $hasil);    
		            }
		        } 
		}      
		if ($kdtoko =='KDR01' || $kdtoko =='KDR02' || $kdtoko =='KDR03' || $kdtoko =='NJK02' || $kdtoko =='BLT01' || $kdtoko =='KDR04') {
		        $up=$this->db->query("update tabel_penjualan set statuswa='Gagal' where struk='1' and statuswa='';");
		        $wa1=$this->db2->query("select sent.status,sent.wa_file from sent where left(wa_file,11) like '$wafile';");				 
		        if ($wa1->num_rows() > 0) {
			        foreach ($wa1->result() as $data) {
			            $nobukwa=$data->wa_file;
			            if($data->status=='fail') {
			                    $statwa='Gagal';
			            }
			            if($data->status==NULL) {
			                    $statwa='Terkirim';
			            }
				    $hasil = array(
					    'statuswa' => $statwa,
				        );				 
				    	$this->db->where('no_faktur_penjualan', $nobukwa);
			            $this->db->update('tabel_penjualan', $hasil);    
		            }
		        } 
		
		}
		if($kdtoko=='99') {
		return $this->db->select('a.waktu,a.total_penjualan,a.ndisc as diskon,a.netto as total_penjualan_sdiskon,a.ket,a.id_user,a.ongkir,a.no_faktur_penjualan,a.tgl_penjualan,a.nppn,a.kdcabang,a.kdharga,a.keter,if(statuswa="","Print",statuswa) as stwa,a.jnsbayar,b.nm_toko ketcabang,
		                        if(a.jnsbayar="1","Cash",if(a.jnsbayar="2","Debet",if(a.jnsbayar="3","Gopay",if(a.jnsbayar="4","Grabfood",if(a.jnsbayar="6","Shopeepay","OVO"))))) as jbayar,if(a.selesai="1","Selesai",if(selesai="4","Batal","Pending")) as selesai,a.note,a.notelp,
								IF(a.kdharga="KH01",IF(a.layanan="1",a.total_penjualan_sdiskon,0),0) AS reg,
								IF(a.kdharga="KH01",IF(a.layanan="2",a.total_penjualan_sdiskon,0),0) AS regta,
								IF(a.kdharga="KH01",IF(a.layanan="3",a.total_penjualan_sdiskon,0),0) AS regdo,
								IF(a.kdharga="KH02",a.total_penjualan_sdiskon,0) AS gofood,
								IF(a.kdharga="KH03",a.total_penjualan_sdiskon,0) AS grab,
								IF(a.kdharga="KH04",a.total_penjualan_sdiskon,0) AS shopee,
								IF(a.kdharga="KH05",a.total_penjualan_sdiskon,0) AS regma,
								if(a.layanan="1","DineIn",if(a.layanan="2","Takeaway","DO")) as layan
								')
			->join('tabel_toko as b','a.kdcabang=b.kd_toko','left')	
			->where('a.tgl_penjualan >=', $tgl)
			->where('a.tgl_penjualan <=', $stgl)
			->where('a.selesai', '1')
			->order_by('a.kdcabang')
			->order_by('a.no_faktur_penjualan')
			->get('tabel_penjualan as a');
		} else {
		    
		return $this->db->select('a.waktu,a.total_penjualan,a.ndisc as diskon,a.netto as total_penjualan_sdiskon,a.ket,a.id_user,a.ongkir,a.no_faktur_penjualan,a.tgl_penjualan,a.nppn,a.kdcabang,a.kdharga,a.keter,if(statuswa="","Print",statuswa) as stwa,a.jnsbayar,b.nm_toko ketcabang,
		                        if(a.jnsbayar="1","Cash",if(a.jnsbayar="2","Debet",if(a.jnsbayar="3","Gopay",if(a.jnsbayar="4","Grabfood",if(a.jnsbayar="6","Shopeepay","OVO"))))) as jbayar,if(a.selesai="1","Selesai",if(selesai="4","Batal","Pending")) as selesai,a.note,a.notelp,
								IF(a.kdharga="KH01",IF(a.layanan="1",a.total_penjualan_sdiskon,0),0) AS reg,
								IF(a.kdharga="KH01",IF(a.layanan="2",a.total_penjualan_sdiskon,0),0) AS regta,
								IF(a.kdharga="KH01",IF(a.layanan="3",a.total_penjualan_sdiskon,0),0) AS regdo,
								IF(a.kdharga="KH02",a.total_penjualan_sdiskon,0) AS gofood,
								IF(a.kdharga="KH03",a.total_penjualan_sdiskon,0) AS grab,
								IF(a.kdharga="KH04",a.total_penjualan_sdiskon,0) AS shopee,
								IF(a.kdharga="KH05",a.total_penjualan_sdiskon,0) AS regma,
								if(a.layanan="1","DineIn",if(a.layanan="2","Takeaway","DO")) as layan
								')
			->join('tabel_toko as b','a.kdcabang=b.kd_toko','left')	
			->where('a.tgl_penjualan >=', $tgl)
			->where('a.tgl_penjualan <=', $tgl)
			->where('a.selesai', '1')
			->where('a.kdcabang', $kdtoko)
			->order_by('a.no_faktur_penjualan')
			->get('tabel_penjualan as a');
		}
	}
	public function delete_biaya($kode_menu) {
		$this->db->trans_start();
		$this->db->delete('tabel_biaya', array('id' => $kode_menu));
		$this->db->trans_complete();
	}
	public function delete_pendapatan($kode_menu) {
		$this->db->trans_start();
		$this->db->delete('tabel_pendapatan', array('id' => $kode_menu));
		$this->db->trans_complete();
	}	
	public function delete_setoran($kode_menu) {
		$this->db->trans_start();
		$this->db->delete('tabel_setoran', array('id' => $kode_menu));
		$this->db->trans_complete();
	}		
	
  public function getbiaya() {
	    $tgl=date('Y-m-d');
	    $kdtoko = $this->session->userdata('kd_toko');
		$this->db->where('kdcabang',$kdtoko);
		$this->db->where('tgl',$tgl);
		return $this->db->get('tabel_biaya');
	}

public function getdapat($dari,$sampai,$kedai) {
	    $tgl=date('Y-m-d');
	    $tgla=date('Y-m-d', strtotime('-7 days', strtotime($tgl))); 
	     $akses=$this->session->userdata('akses');
	    $kdtoko = $this->session->userdata('kd_toko');
	    if($akses=='kasir') {
	        $this->db->join('tabel_toko AS c', 'a.kdcabang = c.kd_toko', 'left');	
		    $this->db->where('a.kdcabang',$kdtoko);
		    $this->db->where('a.tgl',$tgl);
		    $this->db->where('a.type>=','1');
		    return $this->db->get('tabel_pendapatan as a');
	    } else {
	        if($kedai=='zz') {
	            $this->db->join('tabel_toko AS c', 'a.kdcabang = c.kd_toko', 'left');	
		        $this->db->where('a.tgl<=',$sampai);
		        $this->db->where('a.tgl>=',$dari);
		        $this->db->where('a.type>=','1');
		        return $this->db->get('tabel_pendapatan as a');
	        } else {
	            $this->db->join('tabel_toko AS c', 'a.kdcabang = c.kd_toko', 'left');	
		        $this->db->where('a.tgl<=',$sampai);
		        $this->db->where('a.tgl>=',$dari);
		        $this->db->where('a.type>=','1');
		        $this->db->where('a.kdcabang',$kedai);
		        return $this->db->get('tabel_pendapatan as a');
	        }
		    
	    }
	}
	public function getsetoran($dari,$sampai,$kedai) {
	    $tgl=date('Y-m-d');
	    $tgla=date('Y-m-d', strtotime('-7 days', strtotime($tgl))); 
	     $akses=$this->session->userdata('akses');
	    $kdtoko = $this->session->userdata('kd_toko');
	    if($akses=='kasir') {
	        $this->db->join('tabel_toko AS c', 'a.kdcabang = c.kd_toko', 'left');	
		    $this->db->where('a.kdcabang',$kdtoko);
		    $this->db->where('a.tgl',$tgl);
		    return $this->db->get('tabel_setoran as a');
	    } else {
	        if($kedai=='zz') {
	            $this->db->join('tabel_toko AS c', 'a.kdcabang = c.kd_toko', 'left');	
		        $this->db->where('a.tgl<=',$sampai);
		        $this->db->where('a.tgl>=',$dari);
		        $this->db->where('a.tgl>=',$dari);
		        $this->db->where('a.type>=','2');
		        return $this->db->get('tabel_setoran as a');
	        } else {
	            $this->db->join('tabel_toko AS c', 'a.kdcabang = c.kd_toko', 'left');	
		        $this->db->where('a.tgl<=',$sampai);
		        $this->db->where('a.tgl>=',$dari);
		        $this->db->where('a.type>=','2');
		        $this->db->where('a.kdcabang',$kedai);
		        return $this->db->get('tabel_setoran as a');
	        }
		    
	    }
	}
	public function getdapattotal($dari,$sampai,$kedai) {
	    $tgl=date('Y-m-d');
	    $tgla=date('Y-m-d', strtotime('-7 days', strtotime($tgl))); 
	    $akses=$this->session->userdata('akses');
	    $kdtoko = $this->session->userdata('kd_toko');
	    if($akses=='kasir') {
	        return $this->db->select('sum(a.biaya) as tbiaya')
	        ->where('a.tgl',$tgl)
		    ->get('tabel_pendapatan as a');
		    
	    } else {
	        if($kedai=='zz') {
	        return $this->db->select('sum(a.biaya) as tbiaya')
	        ->where('a.tgl<=',$sampai)
		    ->where('a.tgl>=',$dari)
		    ->where('a.type','1')
		    ->get('tabel_pendapatan as a');
	        } else {
	        return $this->db->select('sum(a.biaya) as tbiaya')
	        ->where('a.tgl<=',$sampai)
		    ->where('a.tgl>=',$dari)
		    ->where('a.type','1')
		    ->where('a.kdcabang',$kedai)
		    ->get('tabel_pendapatan as a');
	        }
	    }
	}
	public function getsetorantotal($dari,$sampai,$kedai) {
	    $tgl=date('Y-m-d');
	    $tgla=date('Y-m-d', strtotime('-7 days', strtotime($tgl))); 
	    $akses=$this->session->userdata('akses');
	    $kdtoko = $this->session->userdata('kd_toko');
	    if($akses=='kasir') {
	        return $this->db->select('sum(a.biaya) as tbiaya')
	        ->where('a.tgl',$tgl)
	        ->where('a.type','2')
		    ->get('tabel_setoran as a');
		    
	    } else {
	        if($kedai=='zz') {
	        return $this->db->select('sum(a.biaya) as tbiaya')
	        ->where('a.tgl<=',$sampai)
		    ->where('a.tgl>=',$dari)
		    ->where('a.type','2')
		    ->get('tabel_setoran as a');
	        } else {
	        return $this->db->select('sum(a.biaya) as tbiaya')
	        ->where('a.tgl<=',$sampai)
		    ->where('a.tgl>=',$dari)
		    ->where('a.type','2')
		    ->where('a.kdcabang',$kedai)
		    ->get('tabel_setoran as a');
	        }
	    }
	}
	public function dataTransaksiHariInid1($jam,$tgl,$kdtoko) {        						 
		return $this->db->select('a.tgl,a.jam,a.kdjenis,a.omset,a.keterangan,a.stokakhir,a.qty1,a.qty2,(a.qty1+a.qty2) as total,a.nmjenis')
			->where('a.tgl', $tgl)
			->where('a.jam<=', $jam)
			->where('a.kdcabang', $kdtoko)
			->where('a.kdjenis', '4')
			->get('rekapsales as a');
	
	}
	public function getpegawai() {
		$kdtoko=$this->session->userdata('kd_toko');
		$this->db->where('jabat','3');
		$this->db->where('aktif','0');
		$this->db->where('kdcab',$kdtoko);
		$this->db->order_by('jabat');
		$this->db->order_by('kdcab');
		return $this->db->get('pegawai');
	}

	public function getcustomer() {
		$kdtoko=$this->session->userdata('kd_toko');
		$this->db->where('kdcabang',$kdtoko);
		$this->db->order_by('kdcabang');
		$this->db->order_by('custno');
		return $this->db->get('customer');
	}
	
	public function getcabang() {
	    $kdtoko = $this->session->userdata('kd_toko');
	    $this->db->where('stat','2');
		$this->db->order_by('kd_toko');
		return $this->db->get('tabel_toko');
	}	
	public function getpromo($tgl,$kdharga) {
		$this->db->where('stat','1');
		$this->db->where('tglawal<=',$tgl);
		$this->db->where('tglakhir>=',$tgl);
		$this->db->where('kdharga',$kdharga);
		return $this->db->get('promoheader');
	}
	
 public function dataPendapatanHariIniAll($tgl,$kdtoko) {
		$this->db->query("delete from rekapharian where jenis='9' and tgl='$tgl' and kdcabang='$kdtoko';");
		$this->db->query("delete from rekapharian where jenis='10' and tgl='$tgl' and kdcabang='$kdtoko';");
		$this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						 select '9','$tgl',kdcabang,jenis,ket,biaya,0 from tabel_pendapatan where tgl='$tgl' and kdcabang='$kdtoko' and jenis='1';");	
        $this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						 select '10','$tgl',kdcabang,jenis,ket,biaya,0 from tabel_pendapatan where tgl='$tgl' and kdcabang='$kdtoko' and jenis='8901.001';");							 
        $this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						  select '11','$tgl',kdcabang,jenis,ket,biaya,0 from tabel_pendapatan where tgl='$tgl' and kdcabang='$kdtoko' and jenis='3';");							 						 
		return $this->db->select('tgl,id,id_user,biaya,ket,kdcabang,if(jenis="1","Penjualan",if(jenis="2","Pembulatan","Lain lain")) as ketjenis')
			->where('tgl', $tgl)
			->where('kdcabang', $kdtoko)
			->order_by('id')
			->get('tabel_pendapatan');
	}


	public function dataPengeluaranHariIni($tgl) {
		return $this->db->select('tabel_biaya.*')
			->where('tabel_biaya.tgl', $tgl)
			->order_by('id')
			->get('tabel_biaya');
	}

    public function dataPengeluaranHariIniAll($tgl,$kdtoko) {
		$this->db->query("delete from rekapharian where jenis='6' and tgl='$tgl' and kdcabang='$kdtoko';");
		$this->db->query("insert ignore into rekapharian(jenis,tgl,kdcabang,kode,keterangan,nilai,nppn)
						 select '6','$tgl',kdcabang,jenis,ket,biaya,0 from tabel_biaya where tgl='$tgl' and kdcabang='$kdtoko';");	
		return $this->db->select('tabel_biaya.*')
			->where('tgl', $tgl)
			->where('kdcabang', $kdtoko)
			->order_by('id')
			->get('tabel_biaya');
	}

	public function reprintStruk($nofaktur) {
		$this->db->where('no_faktur_penjualan', $nofaktur);
		return $this->db->get('tabel_penjualan');
	}

	public function getProdukDijual($nofaktur) {
		$this->db->where('no_faktur_penjualan', $nofaktur);
		$this->db->order_by('nourut');
		return $this->db->get('tabel_rinci_penjualan');
	}

    public function getRekapHarian($kdtoko,$tgl) {
		$this->db->where('jenis', '1');
		$this->db->where('kdcabang', $kdtoko);
		$this->db->where('tgl', $tgl);
		return $this->db->get('rekapharian');
	}
	public function getRekapbiaya($kdtoko,$tgl) {
		$this->db->where('jenis', '6');
		$this->db->where('kdcabang', $kdtoko);
		$this->db->where('tgl', $tgl);
		return $this->db->get('rekapharian');
	}
	
	public function datarekap($kdtoko,$tgl) {
		return  $this->db->select('SUM(IF(jenis="1",(nilai+nppn), 0)) AS omset,
		                           SUM(IF(jenis="1",(nppn), 0)) AS nppn, 
	                               SUM(IF(jenis="2",(nilai+nppn), 0)) AS discount,
	                               SUM(IF(jenis="2",(nppn), 0)) AS nppnd,
								   SUM(IF(jenis="3",(nilai+nppn), 0)) AS setor,
								   SUM(IF(jenis="4",(nilai+nppn), 0)) AS set1,
								   SUM(IF(jenis="5",(nilai+nppn), 0)) AS set2,
								   SUM(IF(jenis="6",(nilai+nppn), 0)) AS biaya,
								   SUM(IF(jenis="7",(nilai+nppn), 0)) AS ongkir,
								   SUM(IF(jenis="4",(nilai), 0)) AS bruto1,
								   SUM(IF(jenis="5",(nilai), 0)) AS bruto2,
								    SUM(IF(jenis="9",(nilai), 0)) AS pjual,
								   SUM(IF(jenis="10",(nilai), 0)) AS pbulat,
								   SUM(IF(jenis="11",(nilai), 0)) AS plain,
								   SUM(IF(jenis="1",(nilai+nppn), IF(jenis="2",((nilai+nppn)*-1),0))) AS netomset,
								   (SUM(IF(jenis="1",(nilai+nppn), IF(jenis="2",((nilai+nppn)*-1),0))))+SUM(IF(jenis="7",(nilai+nppn), 0))+SUM(IF(jenis="9",(nilai+nppn), 0))+SUM(IF(jenis="10",(nilai+nppn), 0))+SUM(IF(jenis="11",(nilai+nppn), 0)) AS tomset,
								   (SUM(IF(jenis="1",(nilai+nppn), IF(jenis="2",((nilai+nppn)*-1),0)))-SUM(IF(jenis="3",(nilai+nppn), 0))) AS tunai,
								   (SUM(IF(jenis="6",nilai,0))+SUM(IF(jenis="3",(nilai), 0))+sum(if(jenis="A",nppn,0))) AS tbiaya,
							    	((SUM(IF(jenis="1",(nilai+nppn), IF(jenis="2",((nilai+nppn)*-1),0)))-SUM(IF(jenis="3",(nilai), 0)))-sum(if(jenis="A",nppn,0))-SUM(IF(jenis="6",(nilai+nppn), 0)))+SUM(IF(jenis="7",(nilai+nppn), 0))+SUM(IF(jenis="9",(nilai+nppn), 0))+SUM(IF(jenis="10",(nilai+nppn), 0))+SUM(IF(jenis="11",(nilai+nppn), 0)) AS saldo,
								
									SUM(IF(jenis="4",nppn,0)) AS nppn1,
									SUM(IF(jenis="5",nppn,0)) AS nppn2'
								   )
		->where('kdcabang', $kdtoko)
		->where('tgl', $tgl)
		->get('rekapharian');
	}
		public function datarekapd($kdtoko,$tgl) {
		return  $this->db->select('SUM(IF(a.takeaway="1",a.sub_total_jual, 0)) AS dirp,
		                           SUM(if(a.takeaway="1",a.jumlah,0)) AS diqty, 
		                           SUM(if(a.kdpromo="032",(a.jumlah-a.qty),0)) AS pmqty, 
		                           SUM(if(a.takeaway<>"1",if(a.qtyta>0,a.qtyta,a.jumlah),0)) AS taqty, 
		                           SUM(if(a.takeaway<>"1",if(a.qtyta>0,(a.jumlah-a.qtyta),0),0)) AS taqty1, 
		                           SUM(IF(a.takeaway<>"1",a.sub_total_jual, 0)) AS tarp,
		                           SUM(a.jumlah)  AS ttqty,
		                           SUM(a.sub_total_jual)  AS ttrp'
								   )
		->join('tabel_penjualan as b','a.no_faktur_penjualan=b.no_faktur_penjualan','left')						   
		->where('b.kdcabang', $kdtoko)
		->where('b.tgl_penjualan', $tgl)
		->where('a.idmenu', '4')
		->where('b.selesai', '1')
		->where('a.jnsbarang <>', 'J06')
		->get('tabel_rinci_penjualan as a');
	}
	
	public function datarekapPengeluaranHarian($kdtoko,$tgl) {
		$this->db->where('jenis', '2');
		$this->db->where('kdcabang', $kdtoko);
		$this->db->where('tgl', $tgl);
		return $this->db->get('rekapharian');
	}
	public function datarekapPengeluaranHarianp($kdtoko,$tgl) {
		$this->db->where('jenis', 'A');
		$this->db->where('kdcabang', $kdtoko);
		$this->db->where('tgl', $tgl);
		return $this->db->get('rekapharian');
	}
	public function datarekapPengeluarantetap($kdtoko,$tgl) {
		$this->db->where('jenis', '3');
		$this->db->where('kdcabang', $kdtoko);
		$this->db->where('tgl', $tgl);
		return $this->db->get('rekapharian');
	}

	public function dataBarangMasuk($nobukti) {
	$kdtoko=$this->session->userdata('kd_toko');
		$this->db->join('tabel_barang AS b', 'a.kdbrg = b.kd_barang', 'left');
		$this->db->join('stokheadera AS c', 'a.nobukti = c.nobukti', 'left');
		$this->db->where('c.st>=', '2');
		$this->db->where('c.jtran', 'TP');
		$this->db->where('a.nobukti', $nobukti);
		$this->db->order_by('a.nourut');
		return $this->db->get('stokdetaila AS a');
	}
	public function isidataBarangMasuk($nobukti,$kdgudang) {
	  $kdtoko=$this->session->userdata('kd_toko');
	  $is=$this->db->query("update stokheader set st='2',kegd='$kdtoko',jtran='TP'  where nobukti='$nobukti'");
	  $is=$this->db->query("update stokdetail set qtyterima=qty  where nobukti='$nobukti'");
	  $is=$this->db->query("update stokdetaila set qtyterima=qty  where nobukti='$nobukti'");
    }
    public function isidataBarangMasukd($nobukti,$kdgudang,$tgl) {
	  $kdtoko=$this->session->userdata('kd_toko');
	  $this->db2->join('stokheader as b','a.nobukti=b.nobukti','left');
	  $this->db2->where('a.nobukti',$nobukti);
	  $this->db2->where('b.tgl',$tgl);
	  $this->db2->where('b.kegd',$kdgudang);
	  $this->db2->where('b.tgl<','6');
	  return $this->db2->get('stokdetail AS a');
	  
    }
	public function getNoFaktur($kodefak) {
		$q = $this->db->query("SELECT MAX(RIGHT(no_faktur_penjualan,3)) AS id_max FROM tabel_penjualan WHERE substr(no_faktur_penjualan,1,11)='$kodefak'");
		$id_user = $this->session->userdata('ses_username');
		$kd = "";
		$kodea = $this->db->query("select kd_toko from tabel_user where id_user='$id_user';");
		$kodeaw=$kodea->row_array();
		$kodeawal=$this->session->userdata('kd_toko');
		if ($q->num_rows() > 0) {
			foreach ($q->result() as $k) {
				$tmp = ((int) $k->id_max) + 1;
				$kd = sprintf("%03s", $tmp);
			}
		} else {
			$kd = "001";
		
		}
		return $kodefak . $kd;
	}

	public function getDataPenjualan($noresi, $username) {
		$this->db->where('no_faktur_penjualan', $noresi);
		$this->db->where('id_user', $username);
		$this->db->where('selesai', '0');
		return $this->db->get('tabel_penjualan');
	}
	
	
	public function getDatadapur1($fak1) {
	    $this->db->select('a.*,sum(jumlah) as jqty,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak1);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur1a($fak1) {
	    $this->db->select('a.*,sum(jumlah) as jqty,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.no_faktur_penjualan', $fak1);
	    $this->db->where('a.idmenu', '4');
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur1b($fak1) {
	    $this->db->select('a.*,sum(jumlah) as jqty,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '2');
		$this->db->where('a.no_faktur_penjualan', $fak1);
		$this->db->group_by('a.kd_barang');
		
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur1c($fak1) {
	    $this->db->select('a.*,sum(jumlah) as jqty,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '6');
		$this->db->where('a.no_faktur_penjualan', $fak1);
		$this->db->group_by('a.kd_barang');
		
		return $this->db->get('tabel_rinci_penjualan as a');
	}

	public function getDatadapur1d($fak1) {
	    $this->db->select('a.*');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '5');
		$this->db->where('a.no_faktur_penjualan', $fak1);
		$this->db->group_by('a.kd_barang');
		
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	
	public function getDatadapur2($fak2) {
	    $this->db->select('a.*,sum(jumlah) as jqty,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak2);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur2a($fak2) {
	    $this->db->select('a.*,sum(jumlah) as jqty,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.no_faktur_penjualan', $fak2);
	    $this->db->where('a.idmenu', '4');
		$this->db->group_by('a.kd_barang');
		$this->db->group_by('a.idmenu');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur2b($fak2) {
	    $this->db->select('a.*,sum(jumlah) as jqty,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '2');
		$this->db->where('a.no_faktur_penjualan', $fak2);
		$this->db->group_by('a.kd_barang');
	
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur2c($fak2) {
	    $this->db->select('a.*,sum(jumlah) as jqty,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '6');
		$this->db->where('a.no_faktur_penjualan', $fak2);
		$this->db->group_by('a.kd_barang');
		
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur2d($fak2) {
	    $this->db->select('a.*,sum(jumlah) as jqty,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '5');
		$this->db->where('a.no_faktur_penjualan', $fak2);
		$this->db->group_by('a.kd_barang');
		
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	
	public function getDatadapur3($fak3) {
		$this->db->select('a.*,sum(jumlah) as jqty,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak3);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
		public function getDatadapur3a($fak3) {
	    $this->db->select('a.*,sum(jumlah) as jqty,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.no_faktur_penjualan', $fak3);
	    $this->db->where('a.idmenu', '4');
		$this->db->group_by('a.kd_barang');
		
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur3b($fak3) {
	    $this->db->select('a.*,sum(jumlah) as jqty,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '2');
		$this->db->where('a.no_faktur_penjualan', $fak3);
		$this->db->group_by('a.kd_barang');
		
		
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur3c($fak3) {
	    $this->db->select('a.*,sum(jumlah) as jqty,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '6');
		$this->db->where('a.no_faktur_penjualan', $fak3);
		$this->db->group_by('a.kd_barang');
		
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur3d($fak3) {
	    $this->db->select('a.*,sum(jumlah) as jqty,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '5');
		$this->db->where('a.no_faktur_penjualan', $fak3);
		$this->db->group_by('a.kd_barang');
		
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur4($fak4) {
		$this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak4);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
		public function getDatadapur4a($fak4) {
	    $this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.no_faktur_penjualan', $fak4);
	    $this->db->where('a.idmenu', '4');
		$this->db->group_by('a.kd_barang');
		$this->db->group_by('a.idmenu');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur4b($fak4) {
	    $this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '2');
		$this->db->where('a.no_faktur_penjualan', $fak4);
		$this->db->group_by('a.kd_barang');
		$this->db->group_by('a.idmenu');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur4c($fak4) {
	    $this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '6');
		$this->db->where('a.no_faktur_penjualan', $fak4);
		$this->db->group_by('a.kd_barang');
		$this->db->group_by('a.idmenu');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur4d($fak4) {
	    $this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '5');
		$this->db->where('a.no_faktur_penjualan', $fak4);
		$this->db->group_by('a.kd_barang');
		$this->db->group_by('a.idmenu');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur5($fak5) {
		$this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak5);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur5a($fak5) {
	    $this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.no_faktur_penjualan', $fak5);
	    $this->db->where('a.idmenu', '4');
		$this->db->group_by('a.kd_barang');
		$this->db->group_by('a.idmenu');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur5b($fak5) {
	    $this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '2');
		$this->db->where('a.no_faktur_penjualan', $fak5);
		$this->db->group_by('a.kd_barang');
		$this->db->group_by('a.idmenu');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur5c($fak5) {
	    $this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '6');
		$this->db->where('a.no_faktur_penjualan', $fak5);
		$this->db->group_by('a.kd_barang');
		$this->db->group_by('a.idmenu');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur5d($fak5) {
	    $this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
	    $this->db->where('a.nourut<', '50');
	    $this->db->where('a.idmenu', '5');
		$this->db->where('a.no_faktur_penjualan', $fak5);
		$this->db->group_by('a.kd_barang');
		$this->db->group_by('a.idmenu');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	
	public function getDatadapur6($fak6) {
		$this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak6);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur7($fak7) {
		$this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak7);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur8($fak8) {
		$this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak8);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur9($fak9) {
		$this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak9);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur10($fak10) {
		$this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak10);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur11($fak11) {
		$this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak11);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur12($fak12) {
		$this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak12);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur13($fak13) {
		$this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak13);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur14($fak14) {
		$this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak14);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapur15($fak15) {
		$this->db->select('a.*,count(*) as jml');
	    $this->db->join('tabel_menu as b','b.kode_menu=a.kd_barang','left');
	    $this->db->where('b.masak', '1');
		$this->db->where('a.no_faktur_penjualan', $fak15);
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapurall($kdtoko,$tgl) {
		$this->db->select('a.nm_barang,sum(a.jumlah) as jumlah');
		$this->db->join('tabel_antri as b','b.nobukti=a.no_faktur_penjualan','left');
		$this->db->where('b.kdcabang', $kdtoko);
		$this->db->where('b.stat', '1');
		$this->db->where('a.nourut<', '50');
		$this->db->where('a.idmenu', '4');
		$this->db->where('b.tgl', $tgl);
		$this->db->order_by('a.kd_barang', 'ASC');
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
		public function getDatadapuralla($kdtoko,$tgl) {
		$this->db->select('a.nm_barang,sum(a.jumlah) as jumlah');
		$this->db->join('tabel_antri as b','b.nobukti=a.no_faktur_penjualan','left');
		$this->db->where('b.kdcabang', $kdtoko);
		$this->db->where('b.stat', '1');
		$this->db->where('a.idmenu', '2');
		$this->db->where('a.nourut<', '50');
		$this->db->where('b.tgl', $tgl);
		$this->db->order_by('a.kd_barang', 'ASC');
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
		public function getDatadapurallb($kdtoko,$tgl) {
		$this->db->select('a.nm_barang,sum(a.jumlah) as jumlah');
		$this->db->join('tabel_antri as b','b.nobukti=a.no_faktur_penjualan','left');
		$this->db->where('b.kdcabang', $kdtoko);
		$this->db->where('b.stat', '1');
		$this->db->where('a.idmenu', '6');
		$this->db->where('a.nourut<', '50');
		$this->db->where('b.tgl', $tgl);
		$this->db->order_by('a.kd_barang', 'ASC');
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
		public function getDatadapurallc($kdtoko,$tgl) {
		$this->db->select('a.nm_barang,sum(a.jumlah) as jumlah');
		$this->db->join('tabel_antri as b','b.nobukti=a.no_faktur_penjualan','left');
		$this->db->where('b.kdcabang', $kdtoko);
		$this->db->where('b.stat', '1');
		$this->db->where('b.tgl', $tgl);
		$this->db->where('a.nourut>=', '50');
		$this->db->order_by('a.kd_barang', 'ASC');
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	public function getDatadapuralld($kdtoko,$tgl) {
		$this->db->select('a.nm_barang,sum(a.jumlah) as jumlah');
		$this->db->join('tabel_antri as b','b.nobukti=a.no_faktur_penjualan','left');
		$this->db->where('b.kdcabang', $kdtoko);
		$this->db->where('b.stat', '1');
		$this->db->where('a.idmenu', '5');
		$this->db->where('b.tgl', $tgl);
		$this->db->order_by('a.kd_barang', 'ASC');
		$this->db->group_by('a.kd_barang');
		return $this->db->get('tabel_rinci_penjualan as a');
	}
	
	public function getDatadapurh1($fak1) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak1);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	public function getDatadapurh2($fak2) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak2);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	public function getDatadapurh3($fak3) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak3);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	public function getDatadapurh4($fak4) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak4);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	public function getDatadapurh5($fak5) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak5);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	public function getDatadapurh6($fak6) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak6);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	public function getDatadapurh7($fak7) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak7);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	public function getDatadapurh8($fak8) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak8);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	public function getDatadapurh9($fak9) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak9);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	public function getDatadapurh10($fak10) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak10);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	public function getDatadapurh11($fak11) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak11);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	public function getDatadapurh12($fak12) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak12);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	public function getDatadapurh13($fak13) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak13);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	public function getDatadapurh14($fak14) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak14);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	public function getDatadapurh15($fak15) {
		$this->db->select("mid(no_faktur_penjualan,12,3) as nofak,no_faktur_penjualan,if(layanan='1','Dine In',if(layanan='2','T.Away','DO')) as makan,if(layanan='1',nomeja,'0') as meja,tgl_penjualan,waktu,layanan,npelanggan");
		$this->db->where('no_faktur_penjualan', $fak15);
		$this->db->where('netto>', 0);
		return $this->db->get('tabel_penjualan');
	}
	
	
	public function getbarang($idbarang,$nofaktur) {
		//$this->db->where('kode_menu', $idbarang);
		//return $this->db->get('tabel_menu');
		$tgl=date('Y-m-d');
		$noresi = $this->uri->segment(3);
		$kod = $this->db->query("select jnsmenu,kdharga,kdcabang from tabel_penjualan where no_faktur_penjualan='$nofaktur';");
		$kode=$kod->row_array();
		$kdjns=$kode['jnsmenu'];
		$kdharga=$kode['kdharga'];
		$kdcabang=$kode['kdcabang'];
		
		$this->db->join('hargajual AS b', 'a.kode_menu = b.kdbrg' , 'left');
		$this->db->join('tabel_penjualan AS c', 'b.kdcabang = c.kdcabang and b.kdharga = c.kdharga', 'left');
		$this->db->join('promodetail AS d', 'd.kdbrg = a.kode_menu and d.idpromo = c.idpromo and  d.kdcabang=c.kdcabang', 'left');
		$this->db->where('a.kode_menu', $idbarang);
		$this->db->where('c.no_faktur_penjualan', $nofaktur);
		$this->db->where('b.tglawal<=', $tgl);
		$this->db->where('b.tglakhir>=', $tgl);
		return $this->db->get('tabel_menu AS a');
	}
		


	public function cek_sudah_ada_promo($idbarang, $nofaktur,$harga_jual) {
		return $this->db->query("SELECT * FROM tabel_rinci_penjualan WHERE kd_barang='$idbarang' AND no_faktur_penjualan='$nofaktur' and harga='$harga_jual';");
	}
	public function cek_sudah_ada($idbarang, $nofaktur) {
		return $this->db->query("SELECT * FROM tabel_rinci_penjualan WHERE kd_barang='$idbarang' AND no_faktur_penjualan='$nofaktur';");
	}
	public function cek_ada($idbarang, $kd_toko) {
		return $this->db->query("SELECT * FROM tabel_stok_toko WHERE kd_barang='$idbarang' AND kd_toko='$kd_toko'");
	}
	public function cek_sudah_ada_order($noorder) {
		return $this->db->query("SELECT * FROM tabel_penjualan WHERE noorder='$noorder' and posting<>'0'");
	}

	public function cek_promo($nofaktur, $layan) {
		$kod = $this->db->query("select a.kdcabang,a.kdharga,a.tgl_penjualan,a.waktu,b.jnsbayar,b.id from tabel_penjualan a
								left join kodeharga b on a.kdharga=b.kdharga		
								where no_faktur_penjualan='$nofaktur';");
		$kode=$kod->row_array();
		$kdharga=$kode['kdharga'];
		$kdcabang=$kode['kdcabang'];
		$tgl=$kode['tgl_penjualan'];
		$waktu=$kode['waktu'];
		$jnsbayar=$kode['jnsbayar'];
		$idbayar=$kode['id'];
		$this->db->join('promoarea as b', 'a.nobukti=b.nobukti','left');
		$this->db->where('a.stat','1');
		$this->db->where('a.tglawal<=',$tgl);
		$this->db->where('a.tglakhir>=',$tgl);
		$this->db->where('a.jamawal<=',$waktu);
		$this->db->where('a.jamakhir>=',$waktu);
		$this->db->where('b.kdwil',$kdcabang);
		$this->db->where_in('a.jnslayanan',("0,'$layan'"));
		$this->db->where_in('a.custno',('"$jnsbayar","$idbayar",0'));
		return $this->db->get('promoheader AS a');
	}
    public function getStokskr($kdtoko,$idbarang,$tgl) {
		return $this->db->query("SELECT * FROM tabel_stok_toko  a 
		                        left join tabel_barang b on b.kd_barang=a.kd_barang
	                        	WHERE a.kd_toko='$kdtoko' and b.report>=1 and b.report<=11 
	                        	order by b.report");
	}
	
    public function datapromo($kdtoko,$tgl) {
		return $this->db->query("SELECT * FROM tabel_stok_toko  WHERE tabel_stok_toko.kd_toko='$kdtoko' and tabel_stok_toko.kd_barang='$idbarang'");
	}
	
	
	public function cek_jumlah_stok($idbarang) {
		$kdtoko = $this->session->userdata('kd_toko');
		return $this->db->query("SELECT MIN(tabel_stok_toko.stok) AS stok FROM tabel_stok_toko,tabel_rinci_menu WHERE tabel_stok_toko.kd_toko='$kdtoko' and tabel_rinci_menu.kode_bahan=tabel_stok_toko.kd_barang AND tabel_rinci_menu.kode_menu='$idbarang'");
	}

	public function getListPenjualan($noresi) {
		return $this->db->query("SELECT * FROM tabel_rinci_penjualan WHERE no_faktur_penjualan='$noresi' ORDER BY nourut");
	}

	public function getTotalBelanja($noresi) {
		return $this->db->query("SELECT SUM(sub_total_jual) AS tot_bel,SUM(sub_total_jual+nppn) AS tot_belppn FROM tabel_rinci_penjualan WHERE no_faktur_penjualan='$noresi'");
	}

	public function cari_nama($nm_barang) {
		$this->db->like('nama_menu', $nm_barang, 'both');
		$this->db->order_by('kode_menu', 'ASC');
		$this->db->limit(6);
		return $this->db->get('tabel_menu')->result();
	}

	public function getPenjualanSelesai($nofaktur) {
		$this->db->where('no_faktur_penjualan', $nofaktur);
		return $this->db->get('tabel_penjualan');
	}

	public function getPenjualanstruk($nofaktur) {
		$this->db->where('no_faktur_penjualan', $nofaktur);
		return $this->db->get('tabel_penjualan');
	}

	public function getStok($kd_barang_item) {
	    $kdtoko = $this->session->userdata('kd_toko');
		return $this->db->query("SELECT MIN(tabel_stok_toko.stok) AS stok FROM tabel_stok_toko,tabel_rinci_menu WHERE tabel_rinci_menu.kode_bahan=tabel_stok_toko.kd_barang AND tabel_rinci_menu.kode_menu='$kd_barang_item' and tabel_stok_toko.kd_toko='$kdtoko'");
	}
	
	

	public function transaksiPending($id_user, $now, $before) {
	    $kdtoko = $this->session->userdata('kd_toko');
		return $this->db->query("SELECT * FROM tabel_penjualan WHERE (selesai='0' or selesai='3')  AND kdcabang='$kdtoko' AND tgl_penjualan='$now' ORDER BY no_faktur_penjualan DESC");
	}

    public function cek_stok($kd_bahan, $kdtoko) {
		return $this->db->query("SELECT * FROM tabel_stok_toko WHERE kd_barang='$kd_bahan' and kd_toko='$kdtoko'");
	}


	public function getStokPorsi($kd_barang_item) {
		return $this->db->query("SELECT * FROM tabel_rinci_menu WHERE kode_menu='$kd_barang_item'");
	}
}

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