<?php 

 class Invoice extends CI_Controller{
	public function __construct(){
		parent::__construct();

		if($this->session->userdata('role_id') != '1'){
			date_default_timezone_set('Asia/Jakarta');
			$this->session->set_flashdata('pesan','<div class="alert alert-danger alert-dismissible fade show" role="alert">
				  Anda Belum Login!
				  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
				  <span aria-hidden="true">&times; </span>
				  </button>
				</div>');
			redirect('auth/login');
		}
	}
 	public function index()
 	{
		$tgl[1] = (string)$this->input->post('tgl1');
		$tgl[2] = $this->input->post('tgl2');
		$status = $this->input->post('ket');
		$uri = $this->input->post('uri');
		if($tgl[1] !=null && $tgl[2] !=null){
			if($status==null){
				$pencarian = $this->db->query("SELECT * FROM tb_invoice WHERE  tgl_pesan BETWEEN '$tgl[1]' AND '$tgl[2]' ")->result();
			} else{
				$pencarian = $this->db->query("SELECT * FROM tb_invoice WHERE  tgl_pesan BETWEEN '$tgl[1]' AND '$tgl[2]' AND ket=$status ")->result();
			}
		}else if($tgl[1] !=null){
			$tgl[2]=mdate('%Y-%m-%d',now());
			if($status==null){
				$pencarian = $this->db->query("SELECT * FROM tb_invoice WHERE  tgl_pesan BETWEEN '$tgl[1]' AND '$tgl[2]' ")->result();
			} else{
				$pencarian = $this->db->query("SELECT * FROM tb_invoice WHERE  tgl_pesan BETWEEN '$tgl[1]' AND '$tgl[2]' AND ket=$status ")->result();
			}
		}else{
			$pencarian = $this->model_invoice->tampil_data();
		}
 		$data = [
			'str'=>'invoice',
			'tgl1' => $tgl[1],
			'tgl2' => $tgl[2],
			'status' => $status,
			'invoice' => $pencarian
		];
 		$this->load->view('templates_admin/header');
 		$this->load->view('templates_admin/sidebar', $data);
 		$this->load->view('admin/invoice', $data);
 		$this->load->view('templates_admin/footer');
 	}

	 public function detail($id_invoice)
	 {
		 $data = [
			'str'=>'invoice',
			'pesanan' => $this->model_invoice->ambil_id_pesanan($id_invoice),
			'invoice' => $this->model_invoice->ambil_id_invoice($id_invoice)
		];
		 $this->load->view('templates_admin/header');
		 $this->load->view('templates_admin/sidebar', $data);
		 $this->load->view('admin/detail_invoice', $data);
		 $this->load->view('templates_admin/footer');
	 }
	 
	 public function ket($id,$ket)
	 {
		 $this->model_invoice->update('tb_invoice',['id'=>$id],['ket'=>$ket,'tgl_lunas'=>date('Y-m-d H:i:s')]);
		 if($ket == 0)
		 {
			 $this->model_invoice->update('tb_invoice',['id'=>$id],['tgl_lunas'=>null]);
		 }
		 return redirect('admin/invoice');
	 }

	 public function cetak($id_invoice)
	 {
		 $data = [
			'str'=>'invoice',
			'pesanan' => $this->model_invoice->ambil_id_pesanan($id_invoice),
			'invoice' => $this->model_invoice->ambil_id_invoice($id_invoice)
		];
		$this->load->view('templates_admin/header');
		 $this->load->view('admin/cetak', $data);
		 $this->load->view('templates_admin/footer');
	 }

	 public function printe()
	 {
		 $t1 = $this->input->post('tgl1');
		 $t2 = $this->input->post('tgl2');
		 if($t1!=null && $t2!=null){
			 $invoice = $this->db->query("SELECT * FROM tb_invoice WHERE  tgl_lunas BETWEEN '$t1' AND '$t2' AND ket=1 ")->result_array();
			//  $invoice['harga'] = 0;
			//  $invoice['keuntungan'] = 0;
			 foreach($invoice as $i => $in){
				 $in['harga'] = 0;
				 $in['keuntungan'] = 0;
				 $pesanan = $this->model_invoice->ambil_id_pesanan($in['id']);
				 foreach($pesanan as $p){
					 $in['harga']=$in['harga']+($p->harga * $p->jumlah);
					 $in['keuntungan']=$in['keuntungan']+($p->keuntungan * $p->jumlah);
					}
				}
			} else {
				$invoice = $this->db->query("SELECT * FROM tb_invoice WHERE  ket=1 ")->result_array();
				foreach($invoice as $i => $in){
					$in['harga'] = 0;
					$in['keuntungan'] = 0;
				$pesanan = $this->model_invoice->ambil_id_pesanan($in['id']);
				foreach($pesanan as $p){
				$in['harga']=$in['harga']+($p->harga * $p->jumlah);
				$in['keuntungan']=$in['keuntungan']+($p->keuntungan * $p->jumlah);
			}
			$invoice[$i]['harga'] = $in['harga']; 
			$invoice[$i]['keuntungan'] = $in['keuntungan']; 
		}
	}
	// var_dump($this->session->userdata('id_user'));die;
		 $data = [
			'str'=>'invoice',
			'tgl1'=> $t1,
			'tgl2'=> $t2,
			'inv' => $invoice
			// 'invoice' => $this->model_invoice->ambil_id_invoice($id_invoice)
		];
		 $this->load->view('templates_admin/header');
		 $this->load->view('admin/print', $data);
		 $this->load->view('templates_admin/footer');
	 }

	  public function hapus ($id)
	{
		$unling = $this->model_barang->get1($id);
		if(file_exists('./uploads/'.$unling['gambar']))
		{
			unlink('./uploads/'.$unling['gambar']);
			$this->model_barang->hapus_data(['id' => $id], 'tb_invoice');
		}
		redirect('admin/invoice/index');
	}
	  
	public function hapusinv ($id)
	{
		$unling = $this->model_invoice->ambil_id_invoice($id);
		if(file_exists('./uploads/kwitansi/'.$unling->img))
		{
			unlink('./uploads/kwitansi/'.$unling->img);
			$data = [
				'ket' => 0,
				'img' => null
			];
			$this->model_invoice->update('tb_invoice',['id' => $id],$data);
		}
		redirect('admin/invoice/index');
	}
}