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

class Ref_json extends CI_Controller {

	/**
	 * @keterangan : Controller untuk halaman profil
	 **/
	public function InfoBarcode()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$kode = $this->input->post('kode');
			$text = "SELECT * FROM barang WHERE barcode='$kode'";
			$tabel = $this->app_model->manualQuery($text);
			$row = $tabel->num_rows();
			if($row>0){
				foreach($tabel->result() as $t){
					$data['kode_brg'] = $t->kode_barang;
					$data['nama_barang'] = $t->nama_barang;
					$data['jns'] = $t->kode_jenis;
					$data['satuan'] = $t->satuan;
					$data['harga_beli'] = $t->harga_beli;
					$data['harga_jual'] = $t->harga_jual;
					$data['stok_awal'] = $t->stok_awal;
					echo json_encode($data);
				}
			}else{
				$data['nama_barang'] = '';
					$data['satuan'] = '';
					$data['harga_beli'] = '';
					$data['harga_jual'] = '';
					$data['stok_awal'] = '';
				echo json_encode($data);
			}
		}else{
			header('location:'.base_url());
		}
	}
	
	public function CariNoSJ(){
		$bln = date('m');
		$th = date('y');
		$text = "SELECT max(kodebeli) as no FROM h_beli";
		$data = $this->app_model->manualQuery($text);
		if($data->num_rows() > 0 ){
			foreach($data->result() as $t){
				$no = $t->no; 
				$tmp = ((int) substr($no,5,5))+1;
				$hasil = 'BL'.sprintf("%05s", $tmp);
			}
		}else{
			$hasil = 'BL'.'00001';
		}
		return $hasil;
	}
	
	public function StatusBayar()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$kode = $this->input->post('kode');
			$text = "UPDATE h_jual SET Status_Bayar='Y' where kodejual='$kode'";
			$tabel = $this->app_model->manualQuery($text);
		}
	}
	
	public function TotHrgBrg()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$kode = $this->input->post('kode');
			$text = "select sum(subtotal) as Harga_Barang from d_jual where kodejual='$kode'";
			$tabel = $this->app_model->manualQuery($text);
			$row = $tabel->num_rows();
			if($row>0){
				foreach($tabel->result() as $t){
					$data['Harga_Barang'] = $t->Harga_Barang;
					echo json_encode($data);
				}
			}else{
					$data['Harga_Barang'] = '';
				echo json_encode($data);
			}
		}else{
			header('location:'.base_url());
		}
	}
	
	public function InfoBarang()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$kode = $this->input->post('kode');
			$text = "SELECT * FROM vwbarang WHERE kode_barang='$kode'";
			$tabel = $this->app_model->manualQuery($text);
			$row = $tabel->num_rows();
			if($row>0){
				foreach($tabel->result() as $t){
					$data['nama_barang'] = $t->nama_barang;
					$data['jns'] = $t->kode_jenis;
					$data['satuan'] = $t->satuan;
					$data['harga_beli'] = $t->harga_beli;
					$data['harga_jual'] = $t->harga_jual;
					$data['stok_awal'] = $t->stok_awal;
					$data['bandul'] = $t->kategori;
					$data['persen_laba'] = $t->persen_laba;
					echo json_encode($data);
				}
			}else{
				$data['nama_barang'] = '';
					$data['satuan'] = '';
					$data['harga_beli'] = '';
					$data['harga_jual'] = '';
					$data['stok_awal'] = '';
				echo json_encode($data);
			}
		}else{
			header('location:'.base_url());
		}
	}
	
	public function InfoCustomer()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$kode = $this->input->post('kode');
			$text = "SELECT * FROM customer WHERE kode_customer='$kode'";
			$tabel = $this->app_model->manualQuery($text);
			$row = $tabel->num_rows();
			if($row>0){
				foreach($tabel->result() as $t){
					$data['nama_customer'] = $t->nama_customer;
					$data['jk'] = $t->jk;
					$data['alamat'] = $t->alamat;
					$data['tempat_lahir'] = $t->tempat_lahir;
					$data['tgl_lahir'] = $this->app_model->tgl_str($t->tgl_lahir);
					$data['telp'] = $t->telp;
					echo json_encode($data);
				}
			}else{
					$data['nama_customer'] 	= '';
					$data['jk'] 			= '';
					$data['alamat'] 		= '';
					$data['tempat_lahir'] 	= '';
					$data['tgl_lahir'] 		= '';
					$data['telp'] 			= '';
				echo json_encode($data);
			}
		}else{
			header('location:'.base_url());
		}
	}
	
	public function InfoSupplier()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$kode = $this->input->post('kode');
			$text = "SELECT * FROM supplier WHERE kode_supplier='$kode'";
			$tabel = $this->app_model->manualQuery($text);
			$row = $tabel->num_rows();
			if($row>0){
				foreach($tabel->result() as $t){
					$data['nama_supplier'] = $t->nama_supplier;
					$data['alamat'] = $t->alamat;
					echo json_encode($data);
				}
			}else{
				$data['nama_supplier'] = '';
				$data['alamat'] = '';
				echo json_encode($data);
			}
		}else{
			header('location:'.base_url());
		}
	}
	
	public function InfoJnsBarang()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$kode = $this->input->post('kode');
			$text = "SELECT * FROM tbjenis WHERE kode_jenis='$kode'";
			$tabel = $this->app_model->manualQuery($text);
			$row = $tabel->num_rows();
			if($row>0){
				foreach($tabel->result() as $t){
					$data['jenis'] = $t->jenis;
					echo json_encode($data);
				}
			}else{
				$data['jenis'] = '';
				echo json_encode($data);
			}
		}else{
			header('location:'.base_url());
		}
	}
	
	public function DataBarang(){
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$cari= $this->input->post('cari');
			if(empty($cari)){
				$text = "SELECT * FROM barang LIMIT 10";
			}else{
				$text = "SELECT kode_barang,nama_barang,satuan, barcode, harga_jual,harga_beli, stok_awal FROM barang WHERE kode_barang LIKE '%$cari%' OR nama_barang LIKE '%$cari%'";
			}
			$d['data'] = $this->app_model->manualQuery($text);
			
			$this->load->view('data_barang',$d);
		}else{
			header('location:'.base_url());
		}
	}
	
	public function DataCustomer(){
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$cari= $this->input->post('cari');
			if(empty($cari)){
				$text = "SELECT * FROM customer";
			}else{
				$text = "SELECT * FROM customer WHERE kode_customer LIKE '%$cari%' OR nama_customer LIKE '%$cari%'";
			}
			$d['data'] = $this->app_model->manualQuery($text);
			
			$this->load->view('data_customer',$d);
		}else{
			header('location:'.base_url());
		}
	}
}

/* End of file profil.php */
/* Location: ./application/controllers/profil.php */