<?php 

class Barang extends CI_Controller{

	function __construct(){
		parent::__construct();
	 	$this->load->model('m_kontak');/* sesuaikan dengan nama file pada model */
		$this->load->model('m_login'); 
		$this->load->model('m_admin'); 
		$this->load->model('m_rak'); 
		$this->load->model('m_barang');
		$this->load->model('m_kantor');
		$this->load->model('m_satuan'); 
		$this->load->model('m_katagori');
		$this->load->library('session');
		$this->load->library('upload');
		 $this->load->library('pagination'); 
		$this->load->helper(array('form', 'url','tanggal'));
		if($this->session->userdata('status') != "loginbro"  and empty($this->session->userdata('email')) ){
			$this->session->set_flashdata('msg','<div class="alert alert-danger alert-dismissible">
						<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
						<strong> Admin Bukan Admin </strong>Ulangi Kembali
						</div> ');

			redirect(base_url("")); 
			
		}
	 
	 
	}

	 
	function index(){		
		 $username = $this->session->userdata("username");  
		   $this->db->where('username',$username);
		   $query = $this->db->get('tb_admin');
		   $row = $query->row();
		   $data['id_admin_login']= $row->id;


		$data['title1'] = 'Data Barang'; 	
		$data['title2'] = 'Ubah Data'; 	
		$data['title3'] = 'Tambah Data';
		$data['title4'] = 'Barang';
		$data['kontak'] = $this->m_kontak->kontak();
		$data['kantor'] = $this->m_kantor->kantor();
		$data['admin'] = $this->m_admin->data_admin(); 
		$data['katagori'] = $this->m_katagori->katagori();
		$data['rak'] = $this->m_rak->rak(); 
		$data['satuan'] = $this->m_satuan->satuan();
		$data['admin_login'] = $this->m_admin->data_admin_login();  
		$data['barang'] = $this->m_barang->barang(); 

		  


		$this->load->view('master/header',$data); 
		$this->load->view('master/barang/v_barang',$data);
		$this->load->view('master/footer',$data); 
	}
	function cetak_label(){		
		 $username = $this->session->userdata("username");  
		   $this->db->where('username',$username);
		   $query = $this->db->get('tb_admin');
		   $row = $query->row();
		   $data['id_admin_login']= $row->id;


		$data['title1'] = 'Data Barang'; 	
		$data['title2'] = 'Ubah Data'; 	
		$data['title3'] = 'Tambah Data';
		$data['title4'] = 'Barang';
		$data['kontak'] = $this->m_kontak->kontak();
		$data['kantor'] = $this->m_kantor->kantor();
		$data['admin'] = $this->m_admin->data_admin(); 
		$data['katagori'] = $this->m_katagori->katagori();
		$data['rak'] = $this->m_rak->rak(); 
		$data['satuan'] = $this->m_satuan->satuan();
		$data['admin_login'] = $this->m_admin->data_admin_login(); 
		$id_barang=$this->uri->segment(3) ;
		$data['label_barang'] = $this->m_barang->label_barang($id_barang); 

		  


	 
		$this->load->view('master/barang/cetak_label',$data);
		 
	}
	function tambah_data(){		
		$username = $this->session->userdata("username");  
		$this->db->where('username',$username);
		$query = $this->db->get('tb_admin');
		$row = $query->row();
		$data['id_admin_login']= $row->id;


		$data['title1'] = 'Data Barang'; 	
		$data['title2'] = 'Ubah Data'; 	
		$data['title3'] = 'Tambah Data';
		$data['title4'] = 'Barang';
		$data['kontak'] = $this->m_kontak->kontak();
		$data['kantor'] = $this->m_kantor->kantor();
		$data['admin'] = $this->m_admin->data_admin(); 
		$data['katagori'] = $this->m_katagori->katagori();
		$data['rak'] = $this->m_rak->rak(); 
		$data['satuan'] = $this->m_satuan->satuan();
		$data['admin_login'] = $this->m_admin->data_admin_login();  
		
		$jum_data = $this->m_barang->jumlah_data_barang(); 

		$config['base_url'] = base_url('barang/tambah_data'); //site url
        $config['total_rows'] = $jum_data; //total row
        $config['per_page'] = 20;  //show record per halaman
        $config["uri_segment"] = 3;  // uri parameter
        $choice = $config["total_rows"] / $config["per_page"];
        $config["num_links"] = floor($choice);

        // Membuat Style pagination untuk BootStrap v4
        $config['first_link']       = 'First';
        $config['last_link']        = 'Last';
        $config['next_link']        = 'Next';
        $config['prev_link']        = 'Prev';
        $config['full_tag_open']    = '<div class="pagging text-center"><nav><ul class="pagination justify-content-center">';
        $config['full_tag_close']   = '</ul></nav></div>';
        $config['num_tag_open']     = '<li class="page-item"><span class="page-link">';
        $config['num_tag_close']    = '</span></li>';
        $config['cur_tag_open']     = '<li class="page-item active"><span class="page-link">';
        $config['cur_tag_close']    = '<span class="sr-only">(current)</span></span></li>';
        $config['next_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['next_tagl_close']  = '<span aria-hidden="true">&raquo;</span></span></li>';
        $config['prev_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['prev_tagl_close']  = '</span>Next</li>';
        $config['first_tag_open']   = '<li class="page-item"><span class="page-link">';
        $config['first_tagl_close'] = '</span></li>';
        $config['last_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['last_tagl_close']  = '</span></li>';

        $this->pagination->initialize($config);
        $data['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

        //panggil function data_barangq yang ada pada mmodel m_barang.
        $data['data'] = $this->m_barang->tampilkan($config["per_page"], $data['page']);

        $data['pagination'] = $this->pagination->create_links(); 

		  


		$this->load->view('master/header',$data); 
		$this->load->view('master/barang/t_barang',$data);
		$this->load->view('master/footer',$data); 
	}
	public function code_hapus_expired(){
		$id = $this->input->post('id'); 
		$jumlah = $this->input->post('jumlah'); 
		$id_barang = $this->input->post('id_barang'); 
		$id_kantor = $this->input->post('id_kantor');
		$pilihan = $this->input->post('pilihan'); 
		$username = $this->session->userdata("username");  
              $this->db->where('username',$username);
              $query = $this->db->get('tb_admin');
              $row = $query->row();
             
              $id_admin = $row->id; 

		$date = date("dmY");
		if($pilihan == 'hapus_info') {
		 //input data history
		}else if($pilihan == 'retur') {
			$this->db->where('id_barang',$id_barang);
			$this->db->where('id_kantor',$id_kantor);
			$query1	 = $this->db->get('tb_stok');
			$row1 	 = $query1->row();
			$cek_stok	 = $row1->stok_aplikasi;
			$new_stok =   $cek_stok - $jumlah ;
			

			//update stok gudang
			$datax=array(

				'stok_aplikasi'=> $new_stok

			);
			$this->db->where('id_barang',$id_barang);
			$this->db->where('id_kantor',$id_kantor);
			$this->db->update('tb_stok',$datax);
			//input hitory
			$data2=array(

			'id_barang'			=>  $id_barang, 
			'id_kantor'			=>  $id_kantor,
			'jumlah'			=>  $jumlah,
			'id_admin'			=>  $id_admin,
			'date'				=>  $date,
			'keterangan'		=>  $pilihan,
			 
			'id_transaksi'		=>  $id, 

			);

			$this->db->insert('tb_history_barang_in_out',$data2);

		}else if($pilihan == 'buang') {
			$this->db->where('id_barang',$id_barang);
			$this->db->where('id_kantor',$id_kantor);
			$query1	 = $this->db->get('tb_stok');
			$row1 	 = $query1->row();
			$cek_stok	 = $row1->stok_aplikasi;
			$new_stok =   $cek_stok - $jumlah ;
			

			//update stok gudang
			$datax=array(

				'stok_aplikasi'=> $new_stok

			);
			$this->db->where('id_barang',$id_barang);
			$this->db->where('id_kantor',$id_kantor);
			$this->db->update('tb_stok',$datax);
			//input hitory
			
			$data2=array(

			'id_barang'			=>  $id_barang, 
			'id_kantor'			=>  $id_kantor,
			'jumlah'			=>  $jumlah,
			'id_admin'			=>  $id_admin,
			'date'				=>  $date,
			'keterangan'		=>  $pilihan,
			 
			'id_transaksi'		=>  $id, 

			);

			$this->db->insert('tb_history_barang_in_out',$data2);
			
		}else{
			$data2=array(

			'id_barang'			=>  $id_barang, 
			'id_kantor'			=>  $id_kantor,
			'jumlah'			=>  $jumlah,
			'id_admin'			=>  $id_admin,
			'date'				=>  $date,
			'keterangan'		=>  $pilihan,
			 
			'id_transaksi'		=>  $id, 

			);

			$this->db->insert('tb_history_barang_in_out',$data2);
		}
		
		 
	    $this->db->delete('tb_info_expired', array('id' => $id)); 
	  	$this->session->set_flashdata('msg','success-hapus');
	    redirect('barang/expired');
		
	}
	function expired(){		
		$username = $this->session->userdata("username");  
		$this->db->where('username',$username);
		$query = $this->db->get('tb_admin');
		$row = $query->row();
		$data['id_admin_login']= $row->id;
		$id_admin = $row->id;
		$level = $row->level;
		$id_kantor = $row->id_kantor;

		$data['title1'] = 'Data Barang Expired'; 	
		$data['title2'] = 'Ubah Data'; 	
		$data['title3'] = 'Tambah Data';
		$data['title4'] = 'Barang';
		$data['kontak'] = $this->m_kontak->kontak();
		$data['kantor'] = $this->m_kantor->kantor();
		$data['admin'] = $this->m_admin->data_admin(); 
		$data['katagori'] = $this->m_katagori->katagori();
		$data['rak'] = $this->m_rak->rak(); 
		$data['satuan'] = $this->m_satuan->satuan();
		$data['admin_login'] = $this->m_admin->data_admin_login();  
		$data['barang'] = $this->m_barang->barang(); 
		$data['barang_expired'] = $this->m_barang->barang_expired_all($id_admin,$id_kantor,$level); 
		  


		$this->load->view('master/header',$data); 
		$this->load->view('master/barang/v_barang_expired',$data);
		$this->load->view('master/footer',$data); 
	}
	function history(){		
		$username = $this->session->userdata("username");  
		$this->db->where('username',$username);
		$query = $this->db->get('tb_admin');
		$row = $query->row();
		$data['id_admin_login']= $row->id;
		$id_admin = $row->id;
		$level = $row->level;
		$id_kantor = $row->id_kantor;

		$data['title1'] = 'Data Barang History'; 	
		$data['title2'] = 'Ubah Data'; 	
		$data['title3'] = 'Tambah Data';
		$data['title4'] = 'History Barang';
		$data['kontak'] = $this->m_kontak->kontak();
		$data['kantor'] = $this->m_kantor->kantor();
		$data['admin'] = $this->m_admin->data_admin(); 
		$data['katagori'] = $this->m_katagori->katagori();
		$data['rak'] = $this->m_rak->rak(); 
		$data['satuan'] = $this->m_satuan->satuan();
		$data['admin_login'] = $this->m_admin->data_admin_login();  
		$data['barang'] = $this->m_barang->barang(); 
		 




			if($level =='manager'){
				$this->db->order_by('date', 'asc');
				$query = $this->db->get('tb_history_barang_in_out', $limit, $start);

			}else{
				$this->db->order_by('date', 'asc');
				$this->db->where('id_admin',$id_admin);
				$this->db->where('id_kantor',$id_kantor);
				$query = $this->db->get('tb_history_barang_in_out', $limit, $start);
			}

		$config['base_url'] = base_url('barang/history'); //site url
        $config['total_rows'] = $query; //total row
        $config['per_page'] = 20;  //show record per halaman
        $config["uri_segment"] = 3;  // uri parameter
        $choice = $config["total_rows"] / $config["per_page"];
        $config["num_links"] = floor($choice);

        // Membuat Style pagination untuk BootStrap v4
      	$config['first_link']       = 'First';
        $config['last_link']        = 'Last';
        $config['next_link']        = 'Next';
        $config['prev_link']        = 'Prev';
        $config['full_tag_open']    = '<div class="pagging text-center"><nav><ul class="pagination justify-content-center">';
        $config['full_tag_close']   = '</ul></nav></div>';
        $config['num_tag_open']     = '<li class="page-item"><span class="page-link">';
        $config['num_tag_close']    = '</span></li>';
        $config['cur_tag_open']     = '<li class="page-item active"><span class="page-link">';
        $config['cur_tag_close']    = '<span class="sr-only">(current)</span></span></li>';
        $config['next_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['next_tagl_close']  = '<span aria-hidden="true">&raquo;</span></span></li>';
        $config['prev_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['prev_tagl_close']  = '</span>Next</li>';
        $config['first_tag_open']   = '<li class="page-item"><span class="page-link">';
        $config['first_tagl_close'] = '</span></li>';
        $config['last_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['last_tagl_close']  = '</span></li>';

        $this->pagination->initialize($config);
        $data['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

        //panggil function data_barangq yang ada pada mmodel m_barang.
        $data['data'] = $this->m_barang->history_barang($config["per_page"], $data['page']);

        $data['pagination'] = $this->pagination->create_links();




		$this->load->view('master/header',$data); 
		$this->load->view('master/barang/v_barang_history',$data);
		$this->load->view('master/footer',$data); 
	}


	function tampil_barang_rak(){		
		 $username = $this->session->userdata("username");  
		   $this->db->where('username',$username);
		   $query = $this->db->get('tb_admin');
		   $row = $query->row();
		   $data['id_admin_login']= $row->id;


		$data['title1'] = 'Data Barang'; 	
		$data['title2'] = 'Ubah Data'; 	
		$data['title3'] = 'Tambah Data';
		$data['title4'] = 'Barang';
		$data['kontak'] = $this->m_kontak->kontak();
		$data['kantor'] = $this->m_kantor->kantor();
		$data['admin'] = $this->m_admin->data_admin(); 
		$data['katagori'] = $this->m_katagori->katagori();
		$data['rak'] = $this->m_rak->rak(); 
		$data['satuan'] = $this->m_satuan->satuan();
		$data['admin_login'] = $this->m_admin->data_admin_login();  
		$data['barang'] = $this->m_barang->barang_rak(); 

		  


		$this->load->view('master/header',$data); 
		$this->load->view('master/barang/v_barang_rak');
		$this->load->view('master/footer',$data); 
	}
	function tampil($id){		
		$id=$this->uri->segment(4);
		$data['data_admin']=$this->db->query("SELECT * FROM tb_admin WHERE id='$id'")->result();
		
		$data['kontak'] = $this->m_kontak->kontak();
		$data['log'] = $this->m_admin->data_log();
		$data['admin'] = $this->m_admin->data_admin(); 
		$this->load->view('master/header',$data);
		$this->load->view('master/admin/tampil',$data);
		$this->load->view('master/footer_tabel',$data);
	}
	public function code_hapus(){
		$id = $this->input->post('id');  
	

	    $this->db->delete('tb_barang', array('id' => $id)); 
	  	$this->session->set_flashdata('msg','success-hapus');
	    redirect('barang');
		
	}
	public function code_hapus2(){
		$id = $this->input->post('id');  
	

	    $this->db->delete('tb_barang', array('id' => $id)); 
	  	$this->session->set_flashdata('msg','success-hapus');
	    redirect('barang/tambah_data');

		
	}
	 
	public function code_tambah()
	{	 
		   $harga_beli_dos =  $this->input->post('harga_beli_dos');
		   $isi=  $this->input->post('isi');
		   $harga_beli_ecer =  $harga_beli_dos /$isi;

		   $barcode_ecer=$this->input->post('barcode_ecer');

		   if(empty($barcode_ecer)){

		   	$date = date("dmY");
		   	$query = $this->db->query("SELECT max(barcode_ecer) as max_id FROM tb_barang WHERE barcode_ecer LIKE '$date%' ");
		   	$row = $query->row_array();
		   	$max_id = $row['max_id'];
		   	$max_id1 =(int) substr($max_id,8,4);
		   	$kode_tr = $max_id1 +1;
		   	$barcode_ecer_new = $date.sprintf("%04s",$kode_tr);

		   }else{
		   	$barcode_ecer_new=$this->input->post('barcode_ecer');
		   }




		    $config['upload_path'] = './images/barang/'; //path folder
	        $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
	        $config['max_size']      = 2024;
	        $config['encrypt_name'] = TRUE; //Enkripsi nama yang terupload
	        $this->upload->initialize($config);
	        if(!empty($_FILES['foto']['name'])){
	        	
	           if ($this->upload->do_upload('foto')){
	        		$gbr = $this->upload->data();
	        		
	        		$gambar=$gbr['file_name'];
	        	}
	        	

	        	$data=array(
	        		'id'=>  null,
	        		'barcode_ecer'=> $barcode_ecer_new,
	        		'barcode_dos'=>  $this->input->post('barcode_dos'),
	        		'id_rak'=>  $this->input->post('id_rak'),  
	        		'nama_barang'=>  $this->input->post('nama_barang'), 
	        		'id_katagori'=>  $this->input->post('id_katagori'), 
	        		'id_satuan_beli'=>  $this->input->post('id_satuan_beli'), 
	        		'isi'=>  $this->input->post('isi'), 
	        		'harga_beli_dos'=>  $this->input->post('harga_beli_dos'), 
	        		'Harga_beli_ecer'=>  $harga_beli_ecer, 
	        	 
	        		'id_satuan_jual'=>  $this->input->post('id_satuan_jual'), 
	        		'harga_jual_ecer'=>  $this->input->post('harga_jual_ecer'), 
	        		'harga_jual_dos'=>  $this->input->post('harga_jual_dos'), 
	        		'harga_grosir'=>  $this->input->post('harga_grosir'), 
	        		'min_grosir'=>  $this->input->post('min_grosir'), 
	        		'foto'=> $gambar
	        	);

	        	$this->db->insert('tb_barang',$data);

	        	

	        	$this->session->set_flashdata('msg','success');
	        	redirect('barang');
	        }else{
	        	
	        	$data=array(
	        		'id'=>  null,
	        		'barcode_ecer'=> $barcode_ecer_new,
	        		'barcode_dos'=>  $this->input->post('barcode_dos'),
	        		'id_rak'=>  $this->input->post('id_rak'),  
	        		'nama_barang'=>  $this->input->post('nama_barang'), 
	        		'id_katagori'=>  $this->input->post('id_katagori'), 
	        		'id_satuan_beli'=>  $this->input->post('id_satuan_beli'), 
	        		'isi'=>  $this->input->post('isi'), 
	        		'harga_beli_dos'=>  $this->input->post('harga_beli_dos'), 
	        		'Harga_beli_ecer'=>  $harga_beli_ecer, 
	        	 
	        		'id_satuan_jual'=>  $this->input->post('id_satuan_jual'), 
	        		'harga_jual_ecer'=>  $this->input->post('harga_jual_ecer'), 
	        		'harga_jual_dos'=>  $this->input->post('harga_jual_dos'), 
	        		'harga_grosir'=>  $this->input->post('harga_grosir'), 
	        		'min_grosir'=>  $this->input->post('min_grosir'), 
	        		'foto'=> 'brg.jpg'
	        	);

	        	$this->db->insert('tb_barang',$data);
 
	        	$this->session->set_flashdata('msg','success');
	        	redirect('barang');
	        } 

        
        
    }  

    
   public function code_tambah_x()
	{	 
		   $harga_beli_dos =  $this->input->post('harga_beli_dos');
		   $isi=  $this->input->post('isi');
		   $harga_beli_ecer =  $harga_beli_dos /$isi;

		   $barcode_ecer=$this->input->post('barcode_ecer');

		   if(empty($barcode_ecer)){

		   	$date = date("dmY");
		   	$query = $this->db->query("SELECT max(barcode_ecer) as max_id FROM tb_barang WHERE barcode_ecer LIKE '$date%' ");
		   	$row = $query->row_array();
		   	$max_id = $row['max_id'];
		   	$max_id1 =(int) substr($max_id,8,4);
		   	$kode_tr = $max_id1 +1;
		   	$barcode_ecer_new = $date.sprintf("%04s",$kode_tr);

		   }else{
		   	$barcode_ecer_new=$this->input->post('barcode_ecer');
		   }


		    $config['upload_path'] = './images/barang/'; //path folder
	        $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
	        $config['max_size']      = 2024;
	        $config['encrypt_name'] = TRUE; //Enkripsi nama yang terupload
	        $this->upload->initialize($config);
	        if(!empty($_FILES['foto']['name'])){
	        	
	           if ($this->upload->do_upload('foto')){
	        		$gbr = $this->upload->data();
	        		
	        		$gambar=$gbr['file_name'];
	        	}
	        	
	        	$data=array(
	        		'id'=>  null,
	        		'barcode_ecer'=> $barcode_ecer_new,
	        		'barcode_dos'=>  $this->input->post('barcode_dos'),
	        		'id_rak'=>  $this->input->post('id_rak'),  
	        		'nama_barang'=>  $this->input->post('nama_barang'), 
	        		'id_katagori'=>  $this->input->post('id_katagori'), 
	        		'id_satuan_beli'=>  $this->input->post('id_satuan_beli'), 
	        		'isi'=>  $this->input->post('isi'), 
	        		'harga_beli_dos'=>  $this->input->post('harga_beli_dos'), 
	        		'Harga_beli_ecer'=>  $harga_beli_ecer, 
	        	 
	        		'id_satuan_jual'=>  $this->input->post('id_satuan_jual'), 
	        		'harga_jual_ecer'=>  $this->input->post('harga_jual_ecer'), 
	        		'harga_jual_dos'=>  $this->input->post('harga_jual_dos'), 
	        		'harga_grosir'=>  $this->input->post('harga_grosir'), 
	        		'min_grosir'=>  $this->input->post('min_grosir'), 
	        		'foto'=> $gambar
	        	);

	        	$this->db->insert('tb_barang',$data);

	        	$this->session->set_flashdata('msg','success');
	        	redirect('barang/tambah_data');
	        }else{
	        	
	        	$data=array(
	        		'id'=>  null,
	        		'barcode_ecer'=> $barcode_ecer_new,
	        		'barcode_dos'=>  $this->input->post('barcode_dos'),
	        		'id_rak'=>  $this->input->post('id_rak'),  
	        		'nama_barang'=>  $this->input->post('nama_barang'), 
	        		'id_katagori'=>  $this->input->post('id_katagori'), 
	        		'id_satuan_beli'=>  $this->input->post('id_satuan_beli'), 
	        		'isi'=>  $this->input->post('isi'), 
	        		'harga_beli_dos'=>  $this->input->post('harga_beli_dos'), 
	        		'Harga_beli_ecer'=>  $harga_beli_ecer, 
	        	 
	        		'id_satuan_jual'=>  $this->input->post('id_satuan_jual'), 
	        		'harga_jual_ecer'=>  $this->input->post('harga_jual_ecer'), 
	        		'harga_jual_dos'=>  $this->input->post('harga_jual_dos'), 
	        		'harga_grosir'=>  $this->input->post('harga_grosir'), 
	        		'min_grosir'=>  $this->input->post('min_grosir'), 
	        		'foto'=> 'brg.jpg'
	        	);

	        	$this->db->insert('tb_barang',$data);
	        	$this->session->set_flashdata('msg','success');
	        	redirect('barang/tambah_data');
	        } 

        
        
    }  

     
	public function code_ubah() {
      
       	$id = $this->input->post('id');
       	$harga_beli_dos =  $this->input->post('harga_beli_dos');
		$isi=  $this->input->post('isi');
		$harga_beli_ecer =  $harga_beli_dos /$isi;
        
        $config['upload_path'] = './images/barang/'; //path folder
	        $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
	        $config['max_size']      = 2024;
	        $config['encrypt_name'] = TRUE; //Enkripsi nama yang terupload
	        $this->upload->initialize($config);
	        if(!empty($_FILES['foto']['name'])){
	        	
	           if ($this->upload->do_upload('foto')){
	        		$gbr = $this->upload->data();
	        		
	        		$gambar=$gbr['file_name'];
	        	}
	        	
	        	$data=array(
	        		 
	        		'barcode_ecer'=> $this->input->post('barcode_ecer'),
	        		'barcode_dos'=>  $this->input->post('barcode_dos'),
	        		 'id_rak'=>  $this->input->post('id_rak'), 
	        		'nama_barang'=>  $this->input->post('nama_barang'), 
	        		'id_katagori'=>  $this->input->post('id_katagori'), 
	        		'id_satuan_beli'=>  $this->input->post('id_satuan_beli'), 
	        		'isi'=>  $this->input->post('isi'), 
	        		 
	        		'harga_beli_dos'=>  $this->input->post('harga_beli_dos'), 
	        		'Harga_beli_ecer'=>  $harga_beli_ecer, 
	        		'id_satuan_jual'=>  $this->input->post('id_satuan_jual'), 
	        		'harga_jual_ecer'=>  $this->input->post('harga_jual_ecer'), 
	        		'harga_jual_dos'=>  $this->input->post('harga_jual_dos'), 
	        		'harga_grosir'=>  $this->input->post('harga_grosir'), 
	        		'min_grosir'=>  $this->input->post('min_grosir'), 
	        		 
	        		'foto'=> $gambar
	        	);

	        	$this->db->where('id',$this->input->post('id'));
                $this->db->update('tb_barang',$data);
	        	$this->session->set_flashdata('msg','success');
	        	redirect('barang');
	        }else{
	        	
	        	$data=array(
	        	 
	        		'barcode_ecer'=> $this->input->post('barcode_ecer'),
	        		'barcode_dos'=>  $this->input->post('barcode_dos'),
	        		'id_rak'=>  $this->input->post('id_rak'), 
	        		'nama_barang'=>  $this->input->post('nama_barang'), 
	        		'id_katagori'=>  $this->input->post('id_katagori'), 
	        		'id_satuan_beli'=>  $this->input->post('id_satuan_beli'), 
	        		'isi'=>  $this->input->post('isi'), 
	        		
	        		'harga_beli_dos'=>  $this->input->post('harga_beli_dos'), 
	        		'Harga_beli_ecer'=>  $harga_beli_ecer, 
	        		'id_satuan_jual'=>  $this->input->post('id_satuan_jual'), 
	        		'harga_jual_ecer'=>  $this->input->post('harga_jual_ecer'), 
	        		'harga_jual_dos'=>  $this->input->post('harga_jual_dos'), 
	        		'harga_grosir'=>  $this->input->post('harga_grosir'), 
	        		'min_grosir'=>  $this->input->post('min_grosir'),  
	        		 
	        		 
	        	);

	        	$this->db->where('id',$this->input->post('id'));
                $this->db->update('tb_barang',$data);
                $this->session->set_flashdata('msg','success');
	        	redirect('barang');
	        } 
 
	
	}
	public function code_ubah2() {
      
       	$id = $this->input->post('id');
       	$harga_beli_dos =  $this->input->post('harga_beli_dos');
		$isi=  $this->input->post('isi');
		$harga_beli_ecer =  $harga_beli_dos /$isi;
        
        $config['upload_path'] = './images/barang/'; //path folder
	        $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
	        $config['max_size']      = 2024;
	        $config['encrypt_name'] = TRUE; //Enkripsi nama yang terupload
	        $this->upload->initialize($config);
	        if(!empty($_FILES['foto']['name'])){
	        	
	           if ($this->upload->do_upload('foto')){
	        		$gbr = $this->upload->data();
	        		
	        		$gambar=$gbr['file_name'];
	        	}
	        	
	        	$data=array(
	        		 
	        		'barcode_ecer'=> $this->input->post('barcode_ecer'),
	        		'barcode_dos'=>  $this->input->post('barcode_dos'),
	        		 'id_rak'=>  $this->input->post('id_rak'), 
	        		'nama_barang'=>  $this->input->post('nama_barang'), 
	        		'id_katagori'=>  $this->input->post('id_katagori'), 
	        		'id_satuan_beli'=>  $this->input->post('id_satuan_beli'), 
	        		'isi'=>  $this->input->post('isi'), 
	        		 
	        		'harga_beli_dos'=>  $this->input->post('harga_beli_dos'), 
	        		'Harga_beli_ecer'=>  $harga_beli_ecer, 
	        		'id_satuan_jual'=>  $this->input->post('id_satuan_jual'), 
	        		'harga_jual_ecer'=>  $this->input->post('harga_jual_ecer'), 
	        		'harga_jual_dos'=>  $this->input->post('harga_jual_dos'), 
	        		'harga_grosir'=>  $this->input->post('harga_grosir'), 
	        		'min_grosir'=>  $this->input->post('min_grosir'), 
	        		 
	        		'foto'=> $gambar
	        	);

	        	$this->db->where('id',$this->input->post('id'));
                $this->db->update('tb_barang',$data);
	        	$this->session->set_flashdata('msg','success');
	        	 redirect('barang/tambah_data');
	        }else{
	        	
	        	$data=array(
	        	 
	        		'barcode_ecer'=> $this->input->post('barcode_ecer'),
	        		'barcode_dos'=>  $this->input->post('barcode_dos'),
	        		'id_rak'=>  $this->input->post('id_rak'), 
	        		'nama_barang'=>  $this->input->post('nama_barang'), 
	        		'id_katagori'=>  $this->input->post('id_katagori'), 
	        		'id_satuan_beli'=>  $this->input->post('id_satuan_beli'), 
	        		'isi'=>  $this->input->post('isi'), 
	        		
	        		'harga_beli_dos'=>  $this->input->post('harga_beli_dos'), 
	        		'Harga_beli_ecer'=>  $harga_beli_ecer, 
	        		'id_satuan_jual'=>  $this->input->post('id_satuan_jual'), 
	        		'harga_jual_ecer'=>  $this->input->post('harga_jual_ecer'), 
	        		'harga_jual_dos'=>  $this->input->post('harga_jual_dos'), 
	        		'harga_grosir'=>  $this->input->post('harga_grosir'), 
	        		'min_grosir'=>  $this->input->post('min_grosir'),  
	        		 
	        		 
	        	);

	        	$this->db->where('id',$this->input->post('id'));
                $this->db->update('tb_barang',$data);
                $this->session->set_flashdata('msg','success');
	        	 redirect('barang/tambah_data');
	        } 
 
	
	}
	 
	 
	 
}