<?php
class Menu extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model('m_menu');
		$this->load->model('m_order');
		$this->load->model('m_kategori');
		$this->load->library('upload');
	}

            
	function index(){
	$kopel=$this->session->userdata('kopel');
	if ($kopel == null) {
		    $x['judul']="Login";
		    $x['data']=$this->m_menu->lokasi();
		    $this->load->view('mobile/v_login',$x);
	    }
	 if ($kopel != null) {   
	        redirect('mobile/menu/hotpromo');
	    }
	}
	function lokasi(){
	    $kopel=$this->session->userdata('kopel');
	    $aa=$this->db->query("select * from tbl_pelanggan  where plg_id='$kopel';");
	    $ab=$aa->row_array();
	    $x['nama']=$ab['plg_nama'];
	    $x['telp']=$ab['plg_notelp'];
	   
	    $x['alamat']=$ab['plg_alamat'];
		$x['judul']="Lokasi Kedai";
		$x['data']=$this->m_menu->lokasi();
		
		$this->load->view('mobile/v_lokasi',$x);
	}
    function hotpromo(){
		$kopel=$this->session->userdata('kopel');
		$kode=$this->uri->segment(4);
		if($kode<>''){
		    $aa=$this->db->query("update tbl_pelanggan set kdtoko='$kode' where plg_id='$kopel'");
		}
		$x['judul']="HOT PROMO";
		$x['data']=$this->m_menu->hot_promo();
		$this->load->view('mobile/v_menu',$x);
	}
	function favorit(){
		$kopel=$this->session->userdata('kopel');
		$kode=$this->uri->segment(4);
		if($kode<>''){
		    $aa=$this->db->query("update tbl_pelanggan set kdtoko='$kode' where plg_id='$kopel'");
		}
		$x['judul']="MENU FAVORIT";
		$x['data']=$this->m_menu->favorit();
		$this->load->view('mobile/v_menu',$x);
	}
	function jorder(){
		$kdtoko=$this->uri->segment(4);
		$aa=$this->db->query("select * from tabel_toko  where kd_toko='$kdtoko';");
	    $ab=$aa->row_array();
	    $data['toko']=$ab['nm_toko'];
	    $data['kdtoko']=$kdtoko;
	    $data['alamat']=$ab['almt_toko'];
	    $this->session->set_userdata('cabang',$kdtoko);
		$this->load->view('mobile/eorder',$data);
	}
	function hotpro(){
		$x['judul']="HOT PROMO";
		$x['data']=$this->m_menu->hot_promo();
		$this->load->view('mobile/v_home',$x);
	}
	function allmenu(){
		$x['judul']="allmenu";
		$x['data']=$this->m_menu->allmenu();
		$x['promo']=$this->m_menu->promonya();
		$this->load->view('mobile/v_menu',$x);
		$this->session->set_userdata('muncul',0);
	}
	function utama(){
		$x['judul']="HOT PROMO";
		$x['data']=$this->m_menu->hot_promo();
		$this->load->view('mobile/v_home',$x);
		$this->load->view('mobile/v_home',$x);
	}
	function makanan(){
		$x['judul']="makanan";
		$x['data']=$this->m_menu->makanan();
		$this->load->view('mobile/v_menu',$x);
	}
	function makanana(){
		$x['judul']="makanana";
		$x['judul1']="makanana";
		$x['data']=$this->m_menu->makanana();
		$this->load->view('mobile/v_menu',$x);
	}
	function makanan1(){
		$x['judul']="makanan1";
		$x['data']=$this->m_menu->makanan1();
		$this->load->view('mobile/v_menu',$x);
	}
	function makanan1a(){
	    $x['judul1']="makanan1a";
		$x['judul']="makanan1a";
		$x['data']=$this->m_menu->makanan1a();
		$this->load->view('mobile/v_menu',$x);
	}
	function makanan2(){
		$x['judul']="makanan2";
		$x['data']=$this->m_menu->makanan2();
		$this->load->view('mobile/v_menu',$x);
	}
	function makanan3(){
		$x['judul']="makanan3";
		$x['data']=$this->m_menu->makanan3();
		$this->load->view('mobile/v_menu',$x);
	}
	function minuman(){
		$x['judul']="minuman";
		$x['data']=$this->m_menu->minuman();
		$this->load->view('mobile/v_menu',$x);
	}
	function mtop1(){
	    $x['judul']="makanana";
		$x['judul1']="mtop1";
		$x['data']=$this->m_menu->mtop1();
		$this->load->view('mobile/v_menu',$x);
	}
	function mtop2(){
	    $x['judul']="makanana";
		$x['judul1']="mtop2";
		$x['data']=$this->m_menu->mtop2();
		$this->load->view('mobile/v_menu',$x);
	}
	function mtop3(){
	    $x['judul']="makanana";
		$x['judul1']="mtop3";
		$x['data']=$this->m_menu->mtop3();
		$this->load->view('mobile/v_menu',$x);
	}
	function mtop4(){
	    $x['judul']="makanana";
		$x['judul1']="mtop4";
		$x['data']=$this->m_menu->mtop4();
		$this->load->view('mobile/v_menu',$x);
	}	
    function gtop1(){
        $x['judul']="makanan1a";
		$x['judul1']="gtop1";
		$x['data']=$this->m_menu->gtop1();
		$this->load->view('mobile/v_menu',$x);
	}
	function gtop2(){
	    $x['judul']="makanan1a";
		$x['judul1']="gtop2";
		$x['data']=$this->m_menu->gtop2();
		$this->load->view('mobile/v_menu',$x);
	}
	function gtop3(){
	    $x['judul']="makanan1a";
		$x['judul1']="gtop3";
		$x['data']=$this->m_menu->gtop3();
		$this->load->view('mobile/v_menu',$x);
	}
	function gtop4(){
	    $x['judul']="makanan1a";
		$x['judul1']="gtop4";
		$x['data']=$this->m_menu->gtop4();
		$this->load->view('mobile/v_menu',$x);
	}		
	
	function favorite(){
		$x['judul']="FAVORITE";
		$x['data']=$this->m_menu->favorite();
		$this->load->view('mobile/v_menu',$x);
	}
	function detail_menu(){
		$kode=$this->uri->segment(4);
		$x['data']=$this->m_menu->detail_menu($kode);
		$this->load->view('mobile/v_detail_menu',$x);
	}

	function like(){
		$kode=$this->uri->segment(4);
		$this->m_menu->add_like($kode);
		$x['data']=$this->m_menu->detail_menu($kode);
		$this->load->view('mobile/v_detail_menu',$x);
	}

	function add_to_cart(){
		$kode=$this->uri->segment(4);
		$produk=$this->m_menu->detail_menu($kode);
		$i=$produk->row_array();
		$data = array(
               'id'      => $kode,
               'qty'     => 1,
               'price'   => $i['menu_harga_baru'],
               'name'    => $i['menu_nama'],
               'image'	=> $i['menu_gambar']
               
            );

		$this->cart->insert($data); 
		redirect('mobile/menu/cart');
	}

	function cart(){
	    $nobukti=$this->session->userdata('nobukti');
		$this->load->view('mobile/v_cart');
	}
    public function belanja() {
		$tgl = date('Y-m-d');
		$noresi = $this->session->userdata('nobukti');
		$custno = $this->session->userdata('kopel');
		$kdharga='kh01';
		$data_faktur = $this->m_order->getDataPenjualan($noresi, $custno)->row();
		$list_barang = $this->m_order->getListPenjualan($noresi);				
		
		if ($data_faktur) {
			$data['tgl'] = date('Y-m-d');
			$data['faktur'] = $data_faktur;
			$data['list'] = $list_barang;
			$data['tot_item'] = 0;
			$data['tot_belanja'] = 0;
			$data['tot_nppn'] = 0;
			$data['tot_netto'] = 0;
			$data['tot_diskon'] = 0;
			$data['belanja'] = $this->m_order->getTotalBelanja($noresi)->row();
			$this->load->view('mobile/v_belanja',$data);
		} else {
			$this->load->view('error404');
		}
	}
	
	public function belanja1() {
	    $kopel = $this->session->userdata('kopel');
    $kdcabang = $this->session->userdata('kdcabang');
    $this->db->query("update tbl_pelanggan set plg_status='1' where plg_id='$kopel'");
    $no_invoice = $this->m_order->get_invoice($kdcabang);
    $this->session->set_userdata('no_invoice', $no_invoice);
    $this->session->set_userdata('nobukti', $no_invoice);
    $order_proses = $this->m_order->order_produk1($no_invoice);
    
		$tgl = date('Y-m-d');
		$noresi = $this->session->userdata('nobukti');
		$custno = $this->session->userdata('kopel');
		$kdharga='kh01';
		$data_faktur = $this->m_order->getDataPenjualan($noresi, $custno)->row();
		$list_barang = $this->m_order->getListPenjualan($noresi);				
		
		if ($data_faktur) {
			$data['tgl'] = date('Y-m-d');
			$data['faktur'] = $data_faktur;
			$data['list'] = $list_barang;
			$data['tot_item'] = 0;
			$data['tot_belanja'] = 0;
			$data['tot_nppn'] = 0;
			$data['tot_netto'] = 0;
			$data['tot_diskon'] = 0;
			$data['belanja'] = $this->m_order->getTotalBelanja($noresi)->row();
			$this->load->view('mobile/v_belanja',$data);
		} else {
			$this->load->view('error404');
		}
	}


	function remove(){
		$row_id=$this->uri->segment(4);
		$this->cart->update(array(
               'rowid'      => $row_id,
               'qty'     => 0
            ));
		redirect('mobile/menu/cart');
	}
	function update(){
		$this->cart->update($_POST);
        redirect('mobile/menu/cart');
	}
	
	
	function check_out(){
		if($this->session->userdata('online') !=TRUE){
	            $url=base_url('mobile/member');
	            redirect($url);
            }else{
            	$no_invoice=$this->m_order->get_invoice();
				$total=$this->cart->total();
				$this->session->set_userdata('no_invoice',$no_invoice);
				$order_proses=$this->m_order->order_produk($no_invoice,$total);
				if($order_proses){
					$this->cart->destroy();
					redirect('mobile/menu/sukses');
				}else{
					redirect('mobile/menu/cart');
				}
            }
		
	}
	function check_out1(){
	    $nobukti=$this->session->userdata('nobukti');
	    if($this->session->userdata('online') !=TRUE){
	            $url=base_url('mobile/member');
	            redirect($url);
            }else{
               $order_proses=$this->m_order->updateorder($nobukti);
				if($order_proses){
				    $this->m_order->kirim_struk($nobukti);
					redirect('mobile/menu/cetak_invoice');
				}else{
					redirect('mobile/menu/belanja');
				} 
            }
	}
	public function add_list_order() {
	    
	    $kdbrg=$this->uri->segment(4);
	    $kode=$this->uri->segment(4);
	    $balik=$this->uri->segment(5);
		$nobukti=$this->session->userdata('nobukti');
		$kdtoko = $this->session->userdata('cabang');
		$tgl = date('Y-m-d');
		$id_user = $this->session->userdata('ses_username');
		$produk = $this->m_menu->detail_menu($kode);
		$x = $produk->row_array();
		$harga_jual = $x['menu_harga_lama'];
		$ppn = $x['ppn'];
		$nourut=1;
		$nppn=0;
		if($ppn=="Y") {
		    $nppn=($harga_jual*(10/100));
		}
		$cek_sudah_ada = $this->m_order->cek_sudah_ada($kdbrg, $nobukti);
			if ($cek_sudah_ada->num_rows() > 0) {
				$s = $cek_sudah_ada->row_array();
				$jum_beli = $s['qty'];
				$jum_beli_sekarang = $jum_beli+1;
				$nppn_sekarang=0;	
				$subtot_sekarang = ($harga_jual * $jum_beli_sekarang);
				if($ppn=='Y') {
				    $nppn_sekarang=($subtot_sekarang*(10/100));
				    }
				$this->db->query("UPDATE tbl_belanja_detail SET qty='$jum_beli_sekarang', bruto='$subtot_sekarang',nppn='$nppn_sekarang' WHERE kdbrg='$kdbrg' AND nobukti='$nobukti'");
				redirect('mobile/menu/'.$balik);
				
			} else  {
		$data = array(
			'kdcabang' => $kdtoko,
			'nobukti' => $nobukti,
			'nourut' => $nourut,
			'kdbrg' => $kdbrg,
			'qty' =>1,
			'harga' =>$harga_jual,
			'bruto' =>$harga_jual,
			'nppn' =>$nppn,
		);
		$this->db->insert('tbl_belanja_detail', $data);
		}
		redirect('mobile/menu/'.$balik);
	}
	public function add_list_order1() {
	    $kdbrg=$this->uri->segment(4);
	    $kode=$this->uri->segment(4);
	    $balik=$this->uri->segment(5);
		$nobukti=$this->session->userdata('nobukti');
		$kdtoko = $this->session->userdata('cabang');
		$tgl = date('Y-m-d');
		$id_user = $this->session->userdata('ses_username');
		$produk = $this->m_menu->detail_menu($kode);
		$x = $produk->row_array();
		$harga_jual = $x['menu_harga_lama'];
		$ppn = $x['ppn'];
		$nourut=1;
		$nppn=0;
		if($ppn=="Y") {
		    $nppn=($harga_jual*(10/100));
		}
		$cek_sudah_ada = $this->m_order->cek_sudah_ada($kdbrg, $nobukti);
			if ($cek_sudah_ada->num_rows() > 0) {
				$s = $cek_sudah_ada->row_array();
				$jum_beli = $s['qty'];
				$jum_beli_sekarang = $jum_beli+1;
					
				$subtot_sekarang = ($harga_jual * $jum_beli_sekarang);
				$nppn_sekarang=0;
				if($ppn=='Y') {
				    $nppn_sekarang=$subtot_sekarang*(10/100);
				}
				$this->db->query("UPDATE tbl_belanja_detail SET qty='$jum_beli_sekarang', bruto='$subtot_sekarang',nppn='$nppn_sekarang' WHERE kdbrg='$kdbrg' AND nobukti='$nobukti'");
				redirect('mobile/menu/belanja');
				
			} else  {
		$data = array(
			'kdcabang' => $kdtoko,
			'nobukti' => $nobukti,
			'nourut' => $nourut,
			'kdbrg' => $kdbrg,
			'qty' =>1,
			'harga' =>$harga_jual,
			'bruto' =>$harga_jual,
			'nppn' =>$nppn,
		);
		$this->db->insert('tbl_belanja_detail', $data);
		}
		redirect('mobile/menu/belanja');
	}
		public function delorder() {
	    $kdbrg=$this->uri->segment(4);
	    $kode=$this->uri->segment(4);
	    $balik=$this->uri->segment(5);
		$nobukti=$this->session->userdata('nobukti');
		$kdtoko = $this->session->userdata('cabang');
		$this->db->query("delete from tbl_belanja_detail where kdbrg='$kdbrg' AND nobukti='$nobukti';");
		redirect('mobile/menu/belanja');
		}
	public function min_list_order() {
	    $kdbrg=$this->uri->segment(4);
	    $kode=$this->uri->segment(4);
		$nobukti=$this->session->userdata('nobukti');
		$kdtoko = $this->session->userdata('cabang');
		$balik=$this->uri->segment(5);
		$tgl = date('Y-m-d');
		$id_user = $this->session->userdata('ses_username');
		$produk = $this->m_menu->detail_menu($kode);
		$x = $produk->row_array();
		$harga_jual = $x['menu_harga_lama'];
		$ppn = $x['ppn'];
		$nourut=1;
		$nppn=0;
		if($ppn=="Y") {
		    $nppn=($harga_jual*(10/100));
		}
		$cek_sudah_ada = $this->m_order->cek_sudah_ada($kdbrg, $nobukti);
			if ($cek_sudah_ada->num_rows() > 0) {
				$s = $cek_sudah_ada->row_array();
				$jum_beli = $s['qty'];
				$jum_beli_sekarang = $jum_beli-1;
					
				$subtot_sekarang = ($harga_jual * $jum_beli_sekarang);
				$nppn_sekarang=0;
				if($ppn=='Y') {
				    $nppn_sekarang=$subtot_sekarang*(10/100);
				}
				$this->db->query("UPDATE tbl_belanja_detail SET qty='$jum_beli_sekarang', bruto='$subtot_sekarang',nppn='$nppn_sekarang' WHERE kdbrg='$kdbrg' AND nobukti='$nobukti'");
				$this->db->query("delete from tbl_belanja_detail where qty='0' and kdbrg='$kdbrg' AND nobukti='$nobukti'");
			} 
		redirect('mobile/menu/'.$balik);
	}
	
		public function min_list_order1() {
	    $kdbrg=$this->uri->segment(4);
	    $kode=$this->uri->segment(4);
		$nobukti=$this->session->userdata('nobukti');
		$kdtoko = $this->session->userdata('cabang');
		$balik=$this->uri->segment(5);
		$tgl = date('Y-m-d');
		$id_user = $this->session->userdata('ses_username');
		$produk = $this->m_menu->detail_menu($kode);
		$x = $produk->row_array();
		$harga_jual = $x['menu_harga_lama'];
			$ppn = $x['ppn'];
		$nourut=1;
		$nppn=0;
		if($ppn=="Y") {
		    $nppn=($harga_jual*(10/100));
		}
		$cek_sudah_ada = $this->m_order->cek_sudah_ada($kdbrg, $nobukti);
			if ($cek_sudah_ada->num_rows() > 0) {
				$s = $cek_sudah_ada->row_array();
				$jum_beli = $s['qty'];
				$jum_beli_sekarang = $jum_beli-1;
					
				$subtot_sekarang = ($harga_jual * $jum_beli_sekarang);
				$nppn_sekarang=0;
				if($ppn=='Y') {
				    $nppn_sekarang=$subtot_sekarang*(10/100);
				}
				$this->db->query("UPDATE tbl_belanja_detail SET qty='$jum_beli_sekarang', bruto='$subtot_sekarang',nppn='$nppn_sekarang' WHERE kdbrg='$kdbrg' AND nobukti='$nobukti'");
				$this->db->query("delete from tbl_belanja_detail where qty='0' and kdbrg='$kdbrg' AND nobukti='$nobukti'");
			} 
		redirect('mobile/menu/belanja');
	}
	function sukses(){
        $this->load->view('mobile/v_pembayaran');
        
	}
	function cod(){
		$no_invoice=$this->session->userdata('no_invoice');
		$this->m_order->set_pembayaran_cod($no_invoice);
		$x['data']=$this->m_order->get_checkout($no_invoice);
		
		$this->load->view('mobile/v_invoice',$x);
		
	}

	function transfer_bank(){
		$x['data']=$this->m_order->get_all_rekening();
		$this->load->view('mobile/v_rekening',$x);
	}

	function set_pembayaran(){
		$no_invoice=$this->session->userdata('no_invoice');
		$pem_id=$this->uri->segment(4);
		$d=$this->m_order->get_rekening($pem_id);
		$q=$d->row_array();
		$rek_id=$q['rek_id'];
		$rek_no=$q['rek_no'];
		$rek_bank=$q['rek_bank'];
		$rek_nama=$q['rek_nama'];
		$rek_cabang=$q['rek_cabang'];
		$jnsbayar=$q['jnsbayar'];
		$this->m_order->set_pembayaran_transfer($no_invoice,$rek_id,$rek_no,$rek_bank,$rek_nama,$rek_cabang,$jnsbayar);
		redirect('mobile/menu/cetak_invoice');
	}
	function cetak_invoice(){
		$no_invoice=$this->session->userdata('nobukti');
		$x['total']=0;
		$x['pb1']=0;
		$x['netto']=0;
		$x['data']=$this->m_order->get_checkout($no_invoice);
		$this->load->view('mobile/v_invoice',$x);
	}
	
	function lihat_invoice($no_invoice)
	{
	    $cek = $this->m_order->get_checkout1($no_invoice);
		$x['total']=0;
		$x['pb1']=0;
		$x['netto']=0;
		$x['data']=$this->m_order->get_checkout1($no_invoice);
		if($cek->num_rows > 0){
		    $this->load->view('mobile/v_invacc',$x);    
		}else{
		    $this->load->view('mobile/v_error');  
		}
		
	}

    function feedback1($noinvoice){
	    $detail = $this->input->post('komentar');
	    $comment = $this->input->post('comment');
	    $no = $this->input->post('no');
	    $feedback = $this->input->post('feedback');
	    $this->db->query("update tabel_penjualan set feedback = '$feedback', comment='$comment', comment_detail = '$detail' where no_faktur_penjualan = '$no' ");
	    redirect('mobile/menu/lihat_invoice/' . $noinvoice );
	}
	
	function feedback(){
	    $detail = $this->input->post('komentar');
	    $comment = $this->input->post('comment');
	    $no = $this->input->post('no');
	    $feedback = $this->input->post('feedback');
	    $this->db->query("update tbl_invoice set feedback = '$feedback', comment='$comment', comment_detail = '$detail' where inv_no = '$no' ");
	    redirect('mobile/menu/cetak_invoice');
	}


}