<?php 

class Transaksi 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_transaksi'); 
		$this->load->model('m_admin'); 
		$this->load->model('m_kasir');
		$this->load->model('m_metode');
		$this->load->model('m_member');
		$this->load->model('m_kantor');
		$this->load->model('m_supliyer'); 
		$this->load->library('session');
		$this->load->library('upload');
		 
		$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']= $row->id;
		$data['nama_admin']= $row->nama_admin;
		$data['id_kantor']= $row->id_kantor;
		$id_admin = $row->id;
		$level = $row->level;
		$id_kantor = $row->id_kantor;



		$this->db->where('id_kantor',$id_kantor);
		$query = $this->db->get('tb_kantor');
		$row = $query->row();
		$data['nama_kantor']= $row->nama_kantor;
		date_default_timezone_set('Asia/Jakarta');
		$bulan = date('m');
		$tahun = date('Y');


		$sql_menu = "SELECT tb_transaksi.id_transaksi,tb_transaksi.date FROM tb_transaksi WHERE   month(tb_transaksi.date) =  '$bulan' and year(tb_transaksi.date) = '$tahun' order BY tb_transaksi.date DESC LIMIT 5 ";

		 
		$main_menu = $this->db->query($sql_menu)->result();
		foreach ($main_menu as $main) {
			$ID_TRX=$main->id_transaksi;

			$cek= $this->db->query("SELECT tb_jual_detail.id_transaksi FROM tb_jual_detail  where id_transaksi='$ID_TRX'")->num_rows();
			if($cek == 0){
				$hapus_barjual = "DELETE FROM tb_transaksi where id_transaksi='$ID_TRX'     ";
				$data1 = $this->db->query($hapus_barjual);

			}
		}

		// ==============================================

 	// 	$sql_menu2 = "SELECT tb_transaksi.id_transaksi,tb_transaksi.date FROM tb_transaksi WHERE DATE(tb_transaksi.date) =  CURDATE()   ";
		// $main_menu2 = $this->db->query($sql_menu2)->result();
		// foreach ($main_menu2 as $main2) {
		// 	$ID_TRX2=$main2->id_transaksi;

		// 	$cek2= $this->db->query("SELECT tb_jual_detail.id_transaksi FROM tb_jual_detail  where id_transaksi='$ID_TRX2'")->num_rows();
		// 	if($cek2 == 0){
		// 		$hapus_barjual2 = "DELETE FROM tb_transaksi where id_transaksi='$ID_TRX2'     ";
		// 		$data2 = $this->db->query($hapus_barjual2);

		// 	}
		// }

		$data['title1'] = 'Data Transaksi Penjualan'; 	
	 
		$data['title2'] = 'Transaksi';
		$data['kontak'] = $this->m_kontak->kontak();
		$data['kantor3'] = $this->m_kantor->kantor();
		$data['metode'] = $this->m_metode->metode();
		$data['transaksi'] = $this->m_transaksi->data_transaksi($id_admin,$level,$id_kantor);  
		$data['admin'] = $this->m_admin->data_admin(); 
		$data['admin_login'] = $this->m_admin->data_admin_login();  
		$data['supliyer'] = $this->m_supliyer->supliyer(); 
		 
		$this->load->view('master/header',$data); 
		$this->load->view('master/transaksi/v_transaksi',$data);
		$this->load->view('master/footer',$data); 
		
		 
		  
	}
	public function detail_transaksi(){		
		$data['title1'] = 'Detail Transaksi'; 	
		$data['title2'] = 'Transaksi'; 	
		$data['title3'] = 'Tambah Data';

		$id_transaksi= $this->uri->segment(3);

		$data['kontak'] = $this->m_kontak->kontak();
		$data['kantor'] = $this->m_kantor->kantor();
		 
		$data['admin'] = $this->m_admin->data_admin(); 
		$data['detail_transaksi'] = $this->m_transaksi->detail_transaksi($id_transaksi); 
		$data['admin_login'] = $this->m_admin->data_admin_login();  
		$data['kantor'] = $this->m_kantor->kantor(); 
		 
	
		$this->load->view('master/header',$data); 
		$this->load->view('master/transaksi/detail_transaksi',$data);
		$this->load->view('master/footer',$data); 
	}
	 
 
	 
 
   
     
	 
    public function code_retur_pembelian() { 

    	 $id_transaksi= $this->input->post('id_transaksi');
    	 //array 	tb_barjual_kasir
			 $sql = "SELECT * FROM tb_jual_detail  where id_transaksi='$id_transaksi' ";
        	 $cari = $this->db->query($sql)->result();
        	 foreach ($cari as $data) {
		        	 
					$cek_id_transaksi = $data->id_transaksi; 
		            $id_barang = $data->id_barang;
		            $jumlah = $data->jumlah;
		            $id_kantor=	$data->id_kantor;

		            //cek stok
		            $this->db->where('id_barang',$id_barang);
		            $this->db->where('id_kantor',$id_kantor);
					$query1	 = $this->db->get('tb_stok');

				    $row1 	 = $query1->row();
				    $stok_aplikasi	 = $row1->stok_aplikasi;
				  

				 	$NEWJUM =   $stok_aplikasi + $jumlah ;
				 	//update stok pengembalian
				 	$datax=array(
									 
						      		'stok_aplikasi'=> $NEWJUM
						      		 
					            );
					$this->db->where('id_barang',$id_barang);
		            $this->db->where('id_kantor',$id_kantor);
					$this->db->update('tb_stok',$datax);


					

					 
					//pindah data detail ke detail retur//
					$pindah = "INSERT INTO tb_jual_detail_retur SELECT * FROM
										tb_jual_detail WHERE id_transaksi='$cek_id_transaksi'  ";
		       		$data12 = $this->db->query($pindah);

		       	 
					

		        }
				 
				//pengembalian bonus point
				$sqlc = "SELECT * FROM tb_history_point_member  where id_transaksi='$id_transaksi' and status='tambahan' ";
        		$caric = $this->db->query($sqlc)->result();
	        	foreach ($caric as $datac) {	
	        		$id_member = $datac->id_member;
	        		$point_masuk = $datac->point;
	        		$id_trans = $datac->id_transaksi;
	        		//cek point
		            $this->db->where('id_member',$id_member);
		           	$queryc	 = $this->db->get('tb_member');

				    $rowc 	 = $queryc->row();
				    $point	 = $rowc->point;
				  

				 	$newpoint =   $point - $point_masuk ;
				 	//update pengembalian point
				 	$dataxx=array(
									 
						      		'point'=> $newpoint
						      		 
					            );
					$this->db->where('id_member',$id_member);
					$this->db->update('tb_member',$dataxx);


					//tambah informasi
					$data6=array(
							 
							'id'=> null, 
							'id_member'=>  $id_member, 
							'point'=>   $point_masuk, 
							'id_transaksi'=>   $id_trans,
							'status' => 'potongan-retur',
							 
							);
					$this->db->insert('tb_history_point_member',$data6);

	        	} 
				//pindah ke tabel retur
				$pindahy = "INSERT INTO tb_transaksi_retur SELECT * FROM
										tb_transaksi WHERE id_transaksi='$id_transaksi'  ";
		        $datay = $this->db->query($pindahy);
				//hapus barjual kasir
				$hapus_barjual = "DELETE FROM tb_jual_detail where id_transaksi='$cek_id_transaksi'   ";
				$dat15a = $this->db->query($hapus_barjual);
				//hitory
				// $hapus_history = "DELETE FROM tb_history_barang_in_out where id_transaksi='$cek_id_transaksi'   ";
					//Ubah status
				$dataretur=array(

					'keterangan'=> 'retur-jual'

				);
				$this->db->where('id_transaksi',$cek_id_transaksi);
				$this->db->update('tb_history_barang_in_out',$dataretur);
				//U-------------------------------------------
				// $data13 = $this->db->query($hapus_history);
				// //hapus data transaksi
		         $hapux = "DELETE FROM tb_transaksi where id_transaksi='$id_transaksi'   ";
		         $datat = $this->db->query($hapux);

		        
		       $this->session->set_flashdata('msg','success-retur');
			   redirect('transaksi');
		 
	} 
	
	
	
	public function code_reset_transaksi() { 

    	$username = $this->session->userdata("username"); 
         
         if( $username !='rohim118899@gmail.com'){
              redirect('home');
         }else{	 
    	 
				$sqlz = "SELECT * FROM tb_transaksi    ";
				$cariz = $this->db->query($sqlz)->result();
				foreach ($cariz as $dataz) {
					$id_transaksi = $dataz->id_transaksi;
					 //array 	tb_barjual_kasir
					 $sql = "SELECT * FROM tb_jual_detail  where id_transaksi='$id_transaksi' ";
		        	 $cari = $this->db->query($sql)->result();
		        	 foreach ($cari as $data) {
				        	 
							$cek_id_transaksi = $data->id_transaksi; 
				            $id_barang = $data->id_barang;
				            $jumlah = $data->jumlah;
				            $id_kantor=	$data->id_kantor;

				            //cek stok
				            $this->db->where('id_barang',$id_barang);
				            $this->db->where('id_kantor',$id_kantor);
							$query1	 = $this->db->get('tb_stok');

						    $row1 	 = $query1->row();
						    $stok_aplikasi	 = $row1->stok_aplikasi;
						  

						 	$NEWJUM =   $stok_aplikasi + $jumlah ;
						 	//update stok pengembalian
						 	$datax=array(
											 
								      		'stok_aplikasi'=> $NEWJUM
								      		 
							            );
							$this->db->where('id_barang',$id_barang);
				            $this->db->where('id_kantor',$id_kantor);
							$this->db->update('tb_stok',$datax);


							

							 
							//pindah data detail ke detail retur//
							$pindah = "INSERT INTO tb_jual_detail_retur SELECT * FROM
												tb_jual_detail WHERE id_transaksi='$cek_id_transaksi'  ";
				       		$data12 = $this->db->query($pindah);

				       	 
							

				        }
						 
						//pengembalian bonus point
						$sqlc = "SELECT * FROM tb_history_point_member  where id_transaksi='$id_transaksi' and status='tambahan' ";
		        		$caric = $this->db->query($sqlc)->result();
			        	foreach ($caric as $datac) {	
			        		$id_member = $datac->id_member;
			        		$point_masuk = $datac->point;
			        		$id_trans = $datac->id_transaksi;
			        		//cek point
				            $this->db->where('id_member',$id_member);
				           	$queryc	 = $this->db->get('tb_member');

						    $rowc 	 = $queryc->row();
						    $point	 = $rowc->point;
						  

						 	$newpoint =   $point - $point_masuk ;
						 	//update pengembalian point
						 	$dataxx=array(
											 
								      		'point'=> $newpoint
								      		 
							            );
							$this->db->where('id_member',$id_member);
							$this->db->update('tb_member',$dataxx);


							//tambah informasi
							$data6=array(
									 
									'id'=> null, 
									'id_member'=>  $id_member, 
									'point'=>   $point_masuk, 
									'id_transaksi'=>   $id_trans,
									'status' => 'potongan-retur',
									 
									);
							$this->db->insert('tb_history_point_member',$data6);

			        	} 
						//pindah ke tabel retur
						$pindahy = "INSERT INTO tb_transaksi_retur SELECT * FROM
												tb_transaksi WHERE id_transaksi='$id_transaksi'  ";
				        $datay = $this->db->query($pindahy);
						//hapus barjual kasir
						$hapus_barjual = "DELETE FROM tb_jual_detail where id_transaksi='$cek_id_transaksi'   ";
						$dat15a = $this->db->query($hapus_barjual);
						//hitory
						// $hapus_history = "DELETE FROM tb_history_barang_in_out where id_transaksi='$cek_id_transaksi'   ";
							//Ubah status
						$dataretur=array(

							'keterangan'=> 'retur-jual'

						);
						$this->db->where('id_transaksi',$cek_id_transaksi);
						$this->db->update('tb_history_barang_in_out',$dataretur);
				}

						
				        
				       $this->session->set_flashdata('msg','success-retur');
					   redirect('transaksi');
			
		}			   


	} 
	public function code_hapus_pembelian() { 

    	 $id_transaksi= $this->input->post('id_transaksi');
    	 //array 	tb_barjual_kasir
			 $sql = "SELECT * FROM tb_jual_detail  where id_transaksi='$id_transaksi' ";
        	 $cari = $this->db->query($sql)->result();
        	 foreach ($cari as $data) {
		        	 
					$cek_id_transaksi = $data->id_transaksi; 
		            $id_barang = $data->id_barang;
		            $jumlah = $data->jumlah;
		            $id_kantor=	$data->id_kantor;

		            //cek stok
		            $this->db->where('id_barang',$id_barang);
		            $this->db->where('id_kantor',$id_kantor);
					$query1	 = $this->db->get('tb_stok');

				    $row1 	 = $query1->row();
				    $stok_aplikasi	 = $row1->stok_aplikasi;
				  

				 	$NEWJUM =   $stok_aplikasi + $jumlah ;
				 	//update stok pengembalian
				 	$datax=array(
									 
						      		'stok_aplikasi'=> $NEWJUM
						      		 
					            );
					$this->db->where('id_barang',$id_barang);
		            $this->db->where('id_kantor',$id_kantor);
					$this->db->update('tb_stok',$datax);


					

					 
					//pindah data detail ke detail retur//
					// $pindah = "INSERT INTO tb_jual_detail_retur SELECT * FROM
					// 					tb_jual_detail WHERE id_transaksi='$cek_id_transaksi'  ";
		   //     		$data12 = $this->db->query($pindah);

		       	 
					

		        }
				 
				//pengembalian bonus point
				$sqlc = "SELECT * FROM tb_history_point_member  where id_transaksi='$id_transaksi' ";
        		$caric = $this->db->query($sqlc)->result();
	        	foreach ($caric as $datac) {	
	        		$id_member = $datac->id_member;
	        		$point_masuk = $datac->tambahan_point;
	        		$id_trans = $datac->id_transaksi;
	        		//cek point
		            $this->db->where('id_member',$id_member);
		           	$queryc	 = $this->db->get('tb_member');

				    $rowc 	 = $queryc->row();
				    $point	 = $rowc->point;
				  

				 	$newpoint =   $point - $point_masuk ;
				 	//update pengembalian point
				 	$dataxx=array(
									 
						      		'point'=> $newpoint
						      		 
					            );
					$this->db->where('id_member',$id_member);
					$this->db->update('tb_member',$dataxx);


					//tambah informasi
					$data6=array(
							 
							'id'=> null, 
							'id_member'=>  $id_member, 
							'point'=>   $point_masuk, 
							'id_transaksi'=>   $id_trans,
							'status' => 'potongan hapus transaksi',
							 
							);
					$this->db->insert('tb_history_point_member',$data6);

	        	} 
				//pindah ke tabel retur
				// $pindahy = "INSERT INTO tb_transaksi_retur SELECT * FROM
				// 						tb_transaksi WHERE id_transaksi='$id_transaksi'  ";
		  //       $datay = $this->db->query($pindahy);
				//hapus barjual kasir
				$hapus_barjual = "DELETE FROM tb_jual_detail where id_transaksi='$cek_id_transaksi'   ";
				$dat15a = $this->db->query($hapus_barjual);
				//hitory
				$hapus_history = "DELETE FROM tb_history_barang_in_out where id_transaksi='$cek_id_transaksi'   ";
				$data13 = $this->db->query($hapus_history);
				//hapus data transaksi
		        $hapux = "DELETE FROM tb_transaksi where id_transaksi='$id_transaksi'   ";
		        $datat = $this->db->query($hapux);

		        
		       $this->session->set_flashdata('msg','success-retur');
			   redirect('transaksi');
		 
	}


}