<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends AdminController {
	function __construct() {
        parent::__construct();
		$c = $this->session->all_userdata();
		$this->template['datasession'] = $c["userinfo"];
    }

	public function index(){ 	
		$this->render('dashboard'); 
	}
		
	public function inv($id){
		$data['invoice'] = $this->model_global->get_data(array('data' => 'row','table' => 'invoice', 'where' => array( 'id_invoice' => $id)));
		$data['tiket'] = $this->model_global->get_data(array('select' => '*', 'table' => 'additional','where' => array('status' => 1,'id_invoice' => $id), 'order_by' => 'id_additional desc'));
		$data['dbayar'] = $this->model_global->get_data(array('select' => '*', 'table' => 'bayar','where' => array('status' => 1,'id_invoice' => $id), 'order_by' => 'id_bayar desc'));
		$this->load->view('cetakinvoice',$data);
	}
	
	public function slide(){
		$this->template["judul"] = "Slide";
		$this->template['data'] = $this->model_global->get_data(array('select' => '*', 'table' => 'slide','where' => array('status !=' => -1), 'order_by' => 'id_slide desc'));
		$this->render('slide');
	}
	
	public function slide_new(){
		$this->template["judul"] = "Insert Slide";
		if(isset($_GET['_id']) && $_GET['_id'] != ''){
			$this->template["judul"] = "Edit Slide";
			$this->template['data'] = $this->model_global->get_data(array('data' => 'row','table' => 'slide', 'where' => array('status !=' => -1, 'id_slide' => $_GET['_id'])));
		}
		$this->render('slide-new');
	}
	public function laporan_stok_excel(){
		$d = date('Y-m-d');  
		$this->load->library('excel');
		$objPHPExcel = new PHPExcel();
		$this->excel->setActiveSheetIndex(0);
		$objPHPExcel->getActiveSheet()->setTitle('Data Stok');
		$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Apotek');
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16);
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->mergeCells('A1:C1');
		//$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->mergeCells('A2:B2');
		$objPHPExcel->getActiveSheet()->mergeCells('A3:B3');
		$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Tanggal Dibuat'); 
		$objPHPExcel->getActiveSheet()->setCellValue('C2', $d); 
		$objPHPExcel->setActiveSheetIndex(0);
 
		$objPHPExcel->getActiveSheet()->setCellValue('A5', "No")
		 ->setCellValue('B5', "Code")
		 ->setCellValue('C5', "Nama")
		 ->setCellValue('D5', "Kategori")
		 ->setCellValue('E5', "Stok")
		 ->setCellValue('F5', "Satuan")
		 ->setCellValue('G5', "H. Beli")
		 ->setCellValue('H5', "H. Resep")
		 ->setCellValue('I5', "H. Satuan")
		 ->setCellValue('J5', "H. Box")
		 ->setCellValue('K5', "Jum. Modal");
		 
		$barang= $this->model_global->get_data(array(
			'select' => 'a.*, b.nama as satuan', 
			'table' => 'barang a',
			'join' => array('satuan b','b.id_satuan = a.id_satuan'),
			'where' => array('a.status'=>1,'stok !='=>'0'), 
			'order_by' => 'a.nama asc'
		));
		@$search = $_GET['s'];
		$sqlquery = "SELECT  a.*, b.nama as satuan,c.nama as kategori FROM barang a left join satuan b on b.id_satuan = a.id_satuan left join kategori c on c.id_kategori = a.id_kategori where a.status!='-1'
			and (BINARY lower(a.kode) LIKE '%".$search."%' or BINARY  lower(a.nama) LIKE '%".$search."%' or BINARY  lower(b.nama) LIKE '%".$search."%'  or BINARY  lower(c.nama) LIKE '%".$search."%') ORDER BY a.nama asc"; 
			
		$barang = $this->db->query($sqlquery)->result_array(); 		
		$p = date("d")."/".date("m")."/".date("Y");
		$now = date(Y);
		 
		$dataArray= array();
		$no=0;		 
		$nos=1;		 
		$totresep = 0;
		$totsatuan = 0;
		$totbox = 0;
		$totdapat = 0;
		$totmodal = 0;
		
		$tmodal=0;
		$tstok=0;
		/*
				echo"
				<tr>
					<td>".$no."</td>
					<td>".$row['kode']."</td>
					<td>".$row['nama']."</td>
					<td>".$row['kategori']."</td>
					<td><div align='right'>".$row['stok']." ".$row['satuan']."</div></td>
					<td><div align='right'>".number_format($row['harga_beli'])."</div></td>
					<td><div align='right'>".number_format(($row['harga_resep']-$row['harga_beli']) * $row['stok'])."</div></td>
					<td><div align='right'>".number_format(($row['harga_satuan']-$row['harga_beli']) * $row['stok'])."</div></td> 
					<td><div align='right'>".number_format(($row['harga_box']-$row['harga_beli']) * $row['stok'])."</div></td> 
					<td><div align='right'>".number_format($tmodal)."</div></td>
				</tr>";
				$no++;endforeach; endif;
				echo"
				</tbody><tr>
					<th colspan='4'><div align='right'></div></th> 
					<th><div align='right'>".$tstok."</div></th>
					<th><div align='right'> ".number_format($thb)."</div></th>
					<th><div align='right'> ".number_format($totresep)."</div></th>
					<th><div align='right'> ".number_format($totsatuan)."</div></th> 
					<th><div align='right'> ".number_format($totbox)."</div></th>
					<th><div align='right'> ".number_format($totmodal)."</div></th>
				</tr>				
				";	
		*/
		if(isset($barang) && count($barang) > 0): foreach($barang as $row):   
			$tmodal = $row['harga_beli'] * $row['stok'];
			$thb = $thb + (int)$row['harga_beli'];
			
			$totresep+= ($row['harga_resep']-$row['harga_beli']) * $row['stok'];
			$totsatuan+= ($row['harga_satuan']-$row['harga_beli']) * $row['stok'];
			$totbox+= ($row['harga_box']-$row['harga_beli']) * $row['stok'];
			$totmodal+=$tmodal;
			$tstok = $tstok + (int)$row['stok'];
			
			$row_array['no'] = $nos;
			$row_array['code'] = $row['kode'];
			$row_array['nama'] = $row['nama'];
			$row_array['kategori'] = $row['kategori'];
			$row_array['stok'] = $row['stok'];
			$row_array['satuan'] = $row['satuan'];
			$row_array['hbeli'] = $row['harga_beli'];
			/*$row_array['hresep'] = ($row['harga_resep']-$row['harga_beli']) * $row['stok'];
			$row_array['hsatuan'] = ($row['harga_satuan']-$row['harga_beli']) * $row['stok'];
			$row_array['hbox'] = ($row['harga_box']-$row['harga_beli']) * $row['stok'];*/
			$row_array['hresep'] = $row['harga_resep'];
			$row_array['hsatuan'] = $row['harga_satuan'];
			$row_array['hbox'] = $row['harga_box'];
			$row_array['tmodal'] = $tmodal;
			array_push($dataArray,$row_array);
			$no++;
			$nos++;
		endforeach; endif; 	
		$nox=$no+6;
		$objPHPExcel->getActiveSheet()->fromArray($dataArray, NULL, 'A6');
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$nox, "")
		 ->setCellValue('B'.$nox, "")
		 ->setCellValue('C'.$nox, "")
		 ->setCellValue('D'.$nox, "")
		 ->setCellValue('E'.$nox, $tstok)
		 ->setCellValue('G'.$nox, $thb)
		 ->setCellValue('H'.$nox, $totresep)
		 ->setCellValue('I'.$nox, $totsatuan)
		 ->setCellValue('J'.$nox, $totbox)
		 ->setCellValue('K'.$nox, $totmodal);
		
		$objPHPExcel->getActiveSheet()->getStyle('A5:K5')->getFont()->setBold(true);
		// Set fills
		$objPHPExcel->getActiveSheet()->getStyle('A5:K5')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
		$objPHPExcel->getActiveSheet()->getStyle('A5:K5')->getFill()->getStartColor()->setARGB('##000000');		
		$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(40.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true);
		
		$sharedStyle1 = new PHPExcel_Style();
		$sharedStyle2 = new PHPExcel_Style();
		 
		$sharedStyle1->applyFromArray(
		 array('borders' => array(
		 'bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
		 'top' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
		 'right' => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
		 'left' => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
		 ),
		 ));
		 
		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1, "A5:K$nox");
		$objPHPExcel->getActiveSheet()->getRowDimension('5')->setRowHeight(27);
		// Set style for header row using alternative method
		$objPHPExcel->getActiveSheet()->getStyle('A5:K5')->applyFromArray(
		 array(
		 'font' => array(
		 'bold' => true,
		 'color' => array('rgb' => 'FFFFFF')
		 ),
		 'alignment' => array(
		 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
		 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
		 ),
		 'borders' => array(
		 'top' => array(
		 'style' => PHPExcel_Style_Border::BORDER_THIN
		 )
		 ),
		 'fill' => array(
		 'type' => PHPExcel_Style_Fill::FILL_SOLID,
		 'color' => array('rgb' => '000000')
		 )
		 )
		);
		
		$filename='Data_Stok_'.$d.'.xls';
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.$filename.'"');
		header('Cache-Control: max-age=0');
					 
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
		$objWriter->save('php://output');		
	}
	
	public function laporan_stok(){
		@$search = $_GET['s'];
		$this->template["judul"] = "Laporan Stok";
		$this->template['w'] = $this->model_global->get_data(array('data' => 'row','table' => 'website', 'where' => array('id_website' => 1)));
		$this->template['result'] = array();
		$barang= $this->model_global->get_data(array(
			'select' => 'a.*, b.nama as satuan,c.nama as kategori', 
			'table' => 'barang a',
			'join' => array('satuan b','b.id_satuan = a.id_satuan'),
			'join2' => array('kategori c','c.id_kategori = a.id_kategori'),
			'where' => array('a.status'=>1,'stok !='=>'0'), 
			'order_by' => 'a.nama asc'
		));	
		$sqlquery = "SELECT  a.*, b.nama as satuan,c.nama as kategori FROM barang a left join satuan b on b.id_satuan = a.id_satuan left join kategori c on c.id_kategori = a.id_kategori where a.status!='-1'
			and (BINARY lower(a.kode) LIKE '%".$search."%' or BINARY  lower(a.nama) LIKE '%".$search."%' or BINARY  lower(b.nama) LIKE '%".$search."%'  or BINARY  lower(c.nama) LIKE '%".$search."%') ORDER BY a.nama asc"; 
			
		$barang = $this->db->query($sqlquery)->result_array(); 
		 
		$p = date("d")."/".date("m")."/".date("Y");
		$now = date(Y);
		echo "<title>Laporan Stok</title>
		<script>window.print();</script>
			<style>
				@font-face {
					font-family: roboto;
					src: url(".base_url()."assets/webadmin/fonts/robotor.ttf);
				} 			
				body{
					margin:5px;
					padding:0px;
					font-family:roboto;
					font-size:14px;
					color:#000000;
				}
				h1{
					margin:2px 0px;
					font-size:18px;
				}
				h2{
					margin:2px 0px;
					font-size:15px;
				}
				td{
					font-size:12px;
				}
				th{
					font-size:13px;
				}
				.tb{
					width:100%;
					
				}
				.tb td{
					padding:3px;
				}
				.tb th{
					border-top:1px solid #000000;
					border-bottom:1px solid #000000;
					text-transform:uppercase;
					font-weight:bold;
					padding:5px; 
					text-align:left;
				}
			</style>
			
			<div class='option'></div>
			<div class='page'>
				<h1>APOTEK LIDDO FARMA</h1>
				Jl. Kp Siliwangi RT. 010 RW. 003<br>
				Kec. Cigombong - Bogor.
				<br><br>
				<h2 style='text-decoration:underline;'>LAPORAN STOK</h2>
				<table width='100%' cellspacing='0' cellpadding='0'>
					<tr>
						<td width='80'>PERIODE</td><td width='10' align='center'>:</td><td>$now</td>
					</tr>
					<tr>
						<td >TGL CETAK</td><td width='20' align='center'>:</td><td>$p</td>
					</tr> 
				</table>
				<table class='tb' cellpadding='0' cellspacing='0'>
					<thead>
					<tr>
						<th width='20' align='center' rowspan='2'>No</th>
						<th width='80'  rowspan='2'>Code</th> 
						<th width=''  rowspan='2'>Nama Obat</th> 
						<th width='150'  rowspan='2'>Kategori</th> 
						<th width='50'  rowspan='2'><div align='right'>Stok</div></th> 
						<th width='60' rowspan='2'><div align='right'>H. Beli</div></th> 
						<th style='border-bottom:none;' colspan='3'><div align='center'>Provit</div></th> 
						<th width='50'  rowspan='2'><div align='right'>Jum. Modal</div></th> 
					</tr>
					<tr>
						<th width='60' ><div align='right'>H. Resep</div></th> 
						<th width='60' ><div align='right'>H. Satuan</div></th> 
						<th width='60' ><div align='right'>H. Box</div></th> 
					</tr>
					</thead><tbody>"; 
					
 				
					$no=1;
					$totresep = 0;
					$totsatuan = 0;
					$totbox = 0;
					$totdapat = 0;
					$totmodal = 0;
					
					$tmodal=0;
					$tstok=0;
					if(isset($barang) && count($barang) > 0): foreach($barang as $row):   
						$tmodal = $row['harga_beli'] * $row['stok'];
						$thb = $thb + (int)$row['harga_beli'];
						
						$totresep+= ($row['harga_resep']-$row['harga_beli']) * $row['stok'];
						$totsatuan+= ($row['harga_satuan']-$row['harga_beli']) * $row['stok'];
						$totbox+= ($row['harga_box']-$row['harga_beli']) * $row['stok'];
						$totmodal+=$tmodal;
						$tstok = $tstok + (int)$row['stok'];
				echo"
				<tr>
					<td>".$no."</td>
					<td>".$row['kode']."</td>
					<td>".$row['nama']."</td>
					<td>".$row['kategori']."</td>
					<td><div align='right'>".$row['stok']." ".$row['satuan']."</div></td>
					<td><div align='right'>".number_format($row['harga_beli'])."</div></td>
					<!--<td><div align='right'>".number_format(($row['harga_resep']-$row['harga_beli']) * $row['stok'])."</div></td>
					<td><div align='right'>".number_format(($row['harga_satuan']-$row['harga_beli']) * $row['stok'])."</div></td> 
					<td><div align='right'>".number_format(($row['harga_box']-$row['harga_beli']) * $row['stok'])."</div></td> -->
					<td><div align='right'>".number_format($row['harga_resep'])."</div></td>
					<td><div align='right'>".number_format($row['harga_satuan'])."</div></td> 
					<td><div align='right'>".number_format($row['harga_box'])."</div></td> 
					<td><div align='right'>".number_format($tmodal)."</div></td>
				</tr>";
				$no++;endforeach; endif;
				echo"
				</tbody><tr>
					<th colspan='4'><div align='right'></div></th> 
					<th><div align='right'>".$tstok."</div></th>
					<th><div align='right'> ".number_format($thb)."</div></th>
					<th><div align='right'> ".number_format($totresep)."</div></th>
					<th><div align='right'> ".number_format($totsatuan)."</div></th> 
					<th><div align='right'> ".number_format($totbox)."</div></th>
					<th><div align='right'> ".number_format($totmodal)."</div></th>
				</tr>				
				";
			echo"</div>";		 
	}
	public function laporan_stok_old(){
		$this->template["judul"] = "Laporan Stok";
		$this->template['w'] = $this->model_global->get_data(array('data' => 'row','table' => 'website', 'where' => array('id_website' => 1)));
		$this->template['result'] = array();
		$barang= $this->model_global->get_data(array(
			'select' => 'a.*, b.nama as satuan', 
			'table' => 'barang a',
			'join' => array('satuan b','b.id_satuan = a.id_satuan'),
			'where' => array('a.status'=>1), 
			'order_by' => 'a.nama asc'
		));	
		$p = date("d")."/".date("m")."/".date("Y");
		$now = date(Y);
		echo "<title>Laporan Stok</title>
		<script>window.print();</script>
			<style>
				@font-face {
					font-family: roboto;
					src: url(".base_url()."assets/webadmin/fonts/robotor.ttf);
				} 			
				body{
					margin:5px;
					padding:0px;
					font-family:roboto;
					font-size:14px;
					color:#000000;
				}
				h1{
					margin:2px 0px;
					font-size:18px;
				}
				h2{
					margin:2px 0px;
					font-size:15px;
				}
				td{
					font-size:12px;
				}
				th{
					font-size:13px;
				}
				.tb{
					width:100%;
					
				}
				.tb td{
					padding:3px;
				}
				.tb th{
					border-top:1px solid #000000;
					border-bottom:1px solid #000000;
					text-transform:uppercase;
					font-weight:bold;
					padding:5px; 
					text-align:left;
				}
			</style>
			
			<div class='option'></div>
			<div class='page'>
				<h1>APOTEK LIDDO FARMA</h1>
				Jl. Kp Siliwangi RT. 010 RW. 003<br>
				Kec. Cigombong - Bogor.
				<br><br>
				<h2 style='text-decoration:underline;'>LAPORAN PENJUALAN</h2>
				<table width='100%' cellspacing='0' cellpadding='0'>
					<tr>
						<td width='80'>PERIODE</td><td width='10' align='center'>:</td><td>$now</td>
					</tr>
					<tr>
						<td >TGL CETAK</td><td width='20' align='center'>:</td><td>$p</td>
					</tr> 
				</table>
				<table class='tb' cellpadding='0' cellspacing='0'>
					<thead>
					<tr>
						<th width='20' align='center'>No</th>
						<th width='80' >Code</th> 
						<th width='' >Nama Obat</th> 
						<th width='80' >Qty</th> 
						<th width='60' ><div align='right'>H. Beli</div></th> 
						<th width='60' ><div align='right'>H. Jual</div></th> 
						<th width='60' ><div align='right'>Jum. Jual</div></th>  
						<th width='60' ><div align='right'>Provit</div></th> 
						<th width='50' ><div align='right'>Stok</div></th> 
						<th width='50' ><div align='right'>Jum. Modal</div></th> 
					</tr></thead><tbody>"; 
					
 				
					$no=1;
					$totjual = 0;
					$totdapat = 0;
					$totmodal = 0;
					
					$tqty=0;
					$thb=0;
					$thj=0;
					$tmodal=0;
					$tstok=0;
					if(isset($barang) && count($barang) > 0): foreach($barang as $row):   
						$qorder = $this->db->query("SELECT sum(total) total,sum(harga) harga, sum(qty) qty FROM item  where id_barang='$row[id_barang]' and status=1 and year(created_date)='".date("Y")."'")->result_array();
						$pendapatan = $qorder[0]['total'] - ($qorder[0]['qty'] * $row['harga_beli']);	 
						$totdapat+=$pendapatan;
						$totjual+=$qorder[0]['total'];
						$tqty = $tqty + (int)$qorder[0]['qty'];
						$thb = $thb + (int)$row['harga_beli'];
						$thj = $thj + (int)$qorder[0]['harga'];
						$tmodal = $row['harga_beli'] * $row['stok'];
						$totmodal+=$tmodal;
						$tstok = $tstok + (int)$row['stok'];
				echo"
				<tr>
					<td>".$no."</td>
					<td>".$row['kode']."</td>
					<td>".$row['nama']."</td>
					<td>".(int)$qorder[0]['qty']." ".$row['satuan']."</td>
					<td><div align='right'>".number_format($row['harga_beli'])."</div></td>
					<td><div align='right'>".number_format($qorder[0]['harga'])."</div></td>
					<td><div align='right'>".number_format($qorder[0]['total'])."</div></td> 
					<td><div align='right'>".number_format($pendapatan)."</div></td>
					<td><div align='right'>".$row['stok']."</div></td>
					<td><div align='right'>".number_format($tmodal)."</div></td>
				</tr>";
				$no++;endforeach; endif;
				echo"
				</tbody><tr>
					<th colspan='3'><div align='right'></div></th> 
					<th>".(int)$tqty."</th>
					<th><div align='right'> ".number_format($thb)."</div></th>
					<th><div align='right'> ".number_format($thj)."</div></th>
					<th><div align='right'> ".number_format($totjual)."</div></th> 
					<th><div align='right'> ".number_format($totdapat)."</div></th>
					<th><div align='right'>".$tstok."</div></th>
					<th><div align='right'> ".number_format($totmodal)."</div></th>
				</tr>				
				";
			echo"</div>";		 
	}
	public function laporan_laba(){
		$y = date("Y");
		if(isset($_GET['y'])){
			$y = $_GET['y'];
		}
		$this->template["judul"] = "Laporan Penjualan";
		$this->template['w'] = $this->model_global->get_data(array('data' => 'row','table' => 'website', 'where' => array('id_website' => 1)));
		$d1 = cal_days_in_month(CAL_GREGORIAN,"01",date("Y"));
		$d2 = cal_days_in_month(CAL_GREGORIAN,"12",date("Y"));
		$start = $y."-01-01";
		$end = $y."-12-".$d2; 	
		$querygraph = $this->db->query("SELECT sum(total) total,month(created_date) bln,year(created_date) thn FROM item  where date(created_date) BETWEEN '$start' and '$end' group by month(created_date),year(created_date) order by created_date asc")->result_array(); 
		//lama ambil dari master $querygraph = $this->db->query("SELECT sum((a.harga-(select harga_beli from barang where id_barang = a.id_barang)) * a.qty) total,
		$querygraph = $this->db->query("SELECT sum((a.harga-a.harga_beli) * a.qty) total,
		month(a.created_date) bln,year(a.created_date) thn
		FROM item a where date(a.created_date) BETWEEN '$start' and '$end'
		and a.status=1		
		group by month(a.created_date),year(a.created_date)
		order by a.created_date asc")->result_array(); 
		$ar = array();
		$ind = 0;
		$totdapat = 0;
		for($a=0;$a<=11;$a++){
			$bln = $a + 1;
			
			if($querygraph[$ind]['bln']==$bln){
				$ar[$a]['bln'] = $this->bulanarr[$querygraph[$ind]['bln']]." ".date("Y"); 
				$ar[$a]['total'] = $querygraph[$ind]['total'];
				$ind++;
			}else{
				$ind = 0;
				$ar[$a]['bln'] = $this->bulanarr[$bln]." ".date("Y"); 
				$ar[$a]['total'] = "0";
			}
		}  
		foreach($querygraph as $key => $val){
			$totdapat+=$val['total'];
		}
		$this->template['resultgraph'] = $ar;
		$this->template['totdapat'] = $totdapat;
		$this->render('laporan-laba');
		
	}
	public function print_laporan_penjualan(){
		$now = date("d")."/".date("m")."/".date(Y)." ".date("H").":".date("i").":".date("s");
		$start = $_GET['ps'];$end = $_GET['pe'];$kapan = $_GET['kapan'];
		$st = explode("-",$start);
		$ed = explode("-",$end);
		if($start==$end){
			$p = $st[2]."/".$st[1]."/".$st[0];
		}else{
			$p = $st[2]."/".$st[1]."/".$st[0]." s/d ".$ed[2]."/".$ed[1]."/".$ed[0];
			if($kapan=="2"){ 
				$p = $this->bulanarr[($st[1] * 1)]." ".$st[0]; 
			}
		}		
		$distributor = $_GET['d'];
		$diss="ALL";
		if($distributor!=""){
			$ds = $this->model_global->get_data(array('data' => 'row','table' => 'supplier', 'where' => array('id_supplier' => $distributor)));
			$diss = strtoupper($ds['nama']);
		}
		echo "<title>Laporan Penjualan</title>
		<script>window.print();</script>
			<style>
				@font-face {
					font-family: roboto;
					src: url(".base_url()."assets/webadmin/fonts/robotor.ttf);
				} 			
				body{
					margin:5px;
					padding:0px;
					font-family:roboto;
					font-size:14px;
					color:#000000;
				}
				h1{
					margin:2px 0px;
					font-size:18px;
				}
				h2{
					margin:2px 0px;
					font-size:15px;
				}
				td{
					font-size:12px;
				}
				th{
					font-size:13px;
				}
				.tb{
					width:100%;
					
				}
				.tb td{
					padding:3px;
				}
				.tb th{
					border-top:1px solid #000000;
					border-bottom:1px solid #000000;
					text-transform:uppercase;
					font-weight:bold;
					padding:5px; 
					text-align:left;
				}
			</style>
			
			<div class='option'></div>
			<div class='page'>
				<h1>APOTEK LIDDO FARMA</h1>
				Jl. Kp Siliwangi RT. 010 RW. 003<br>
				Kec. Cigombong - Bogor.
				<br><br>
				<h2 style='text-decoration:underline;'>LAPORAN PENJUALAN</h2>
				<table width='100%' cellspacing='0' cellpadding='0'>
					<tr>
						<td width='80'>PERIODE</td><td width='10' align='center'>:</td><td>$p</td>
					</tr>
					<tr>
						<td >TGL CETAK</td><td width='20' align='center'>:</td><td>$now</td>
					</tr>
					<tr>
						<td >DISTRIBUTOR</td><td width='10' align='center'>:</td><td>$diss</td>
					</tr>
				</table>
				<table class='tb' cellpadding='0' cellspacing='0'>
					<thead>
					<tr>
						<th width='20' align='center'>No</th>
						<th width='100' >Tanggal</th> 
						<th width='' >Nama Obat</th> 
						<th width='200' >Kategori</th> 
						<th width='60' >Qty</th> 
						<th width='60' ><div align='right'>H. Beli</div></th> 
						<th width='60' ><div align='right'>Jum. Beli</div></th> 
						<th width='60' ><div align='right'>H. Jual</div></th> 
						<th width='60' ><div align='right'>Jum. Jual</div></th>  
						<th width='60' ><div align='right'>Provit</div></th> 
						<th width='50' ><div align='right'>Stok</div></th> 
					</tr></thead><tbody>";
					$dis="";
					$disvalue="ALL";
					if($distributor!=""){
						$dis = "AND b.id_supplier='$distributor'";
						$dv = $this->model_global->get_data(array('data' => 'row','table' => 'supplier', 'where' => array('id_supplier'=>$distributor)));
						$disvalue = $dv['nama']; 					
					} 
					$sql = "SELECT d.nama satuan,a.harga_beli, b.stok, b.id_supplier,b.kode,b.nama,a.qty,a.jenis_harga,a.harga,a.total,a.created_date,c.nama distributor
					,e.nama as kategori
					FROM item a
					left join barang b on b.id_barang = a.id_barang
					left join supplier c on c.id_supplier = b.id_supplier
					left join satuan d on d.id_satuan = b.id_satuan
					left join kategori e on e.id_kategori = b.id_kategori
					where date(a.created_date) BETWEEN '$start' and '$end' and a.status='1' order by a.created_date asc";
					$result = $this->db->query($sql." $dis")->result_array(); 
					
 				
					$no=1;
					$totjual = 0;
					$totdapat = 0;
					$totbeli = 0;
					
					$tqty=0;
					$thb=0;
					$thj=0;
					$tstok=0;
					if(isset($result) && count($result) > 0): foreach($result as $row):  
						switch($row['jenis_harga']){
							case "1": $jenis = "Resep"; break;
							case "2": $jenis = "Satuan"; break;
							case "3": $jenis = "Box"; break;
						}
						$totbeli = $row['harga_beli'] * $row['qty'];
						$pendapatan = $row['total'] - ($row['harga_beli'] * $row['qty']);
						$totdapat+=$pendapatan;
						$totjual+=$row['total'];
						$t = explode(" ",$row['created_date']);
						$tm = explode(":",$t[1]);
						$t2 = explode("-",$t[0]);
						$tgl = $t2[2]."/".$t2[1]."/".$t2[0]." ".$tm[0].":".$tm[1];
						$tqty = $tqty + (int)$row['qty'];
						$thb = $thb + (int)$row['harga_beli'];
						$thj = $thj + (int)$row['harga'];
						$tstok = $tstok + (int)$row['stok'];
						$tb = $tb + (int)$totbeli;
				echo"
				<tr>
					<td>".$no."</td>
					<td>".$tgl."</td>
					<td>".$row['nama']."</td>
					<td>".$row['kategori']."</td>
					<td>".(int)$row['qty']." ".$row['satuan']."</td>
					<td><div align='right'>".number_format($row['harga_beli'])."</div></td>
					<td><div align='right'>".number_format($totbeli)."</div></td>
					<td><div align='right'>".number_format($row['harga'])."</div></td>
					<td><div align='right'>".number_format($row['total'])."</div></td> 
					<td><div align='right'>".number_format($pendapatan)."</div></td>
					<td><div align='right'>".$row['stok']."</div></td>
				</tr>";
				$no++;endforeach; endif;
				echo"
				</tbody><tr>
					<th colspan='3'><div align='right'></div></th> 
					<th>".(int)$tqty."</th>
					<th><div align='right'> ".number_format($thb)."</div></th>
					<th><div align='right'> ".number_format($tb)."</div></th>
					<th><div align='right'> ".number_format($thj)."</div></th>
					<th><div align='right'> ".number_format($totjual)."</div></th> 
					<th><div align='right'> ".number_format($totdapat)."</div></th>
					<th><div align='right'>".$tstok."</div></th>
				</tr>				
				";
			echo"</div>";
	}
	public function laporan_penjualan_excel(){
		set_time_limit(0);
		$now = date("d")."/".date("m")."/".date(Y)." ".date("H").":".date("i").":".date("s");
		$start = $_GET['ps'];$end = $_GET['pe'];$kapan = $_GET['kapan'];
		$st = explode("-",$start);
		$ed = explode("-",$end);
		if($start==$end){
			$p = $st[2]."/".$st[1]."/".$st[0];
		}else{
			$p = $st[2]."/".$st[1]."/".$st[0]." s/d ".$ed[2]."/".$ed[1]."/".$ed[0];
			if($kapan=="2"){ 
				$p = $this->bulanarr[($st[1] * 1)]." ".$st[0]; 
			}
		}		
		$distributor = $_GET['d'];
		$diss="ALL";
		if($distributor!=""){
			$ds = $this->model_global->get_data(array('data' => 'row','table' => 'supplier', 'where' => array('id_supplier' => $distributor)));
			$diss = strtoupper($ds['nama']);
		}		
		$d = date('Y-m-d');  
		$this->load->library('excel');
		$objPHPExcel = new PHPExcel();
		$this->excel->setActiveSheetIndex(0);
		$objPHPExcel->getActiveSheet()->setTitle('Laporan Penjualan');
		$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Laporan Penjualan');
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16);
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->mergeCells('A1:C1');
		//$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->mergeCells('A2:B2');
		$objPHPExcel->getActiveSheet()->mergeCells('A3:B3');
		$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Periode'); 
		$objPHPExcel->getActiveSheet()->setCellValue('C2', $p); 
		$objPHPExcel->setActiveSheetIndex(0);
 
		$objPHPExcel->getActiveSheet()->setCellValue('A5', "No")
		 ->setCellValue('B5', "Pelanggan")
		 ->setCellValue('C5', "Tanggal")
		 ->setCellValue('D5', "Code")
		 ->setCellValue('E5', "Nama")
		 ->setCellValue('F5', "Kategori")
		 ->setCellValue('G5', "Qty")
		 ->setCellValue('H5', "H. Beli")
		 ->setCellValue('I5', "Jum. Beli")
		 ->setCellValue('J5', "H. Jual")
		 ->setCellValue('K5', "Jum. Jual")
		 ->setCellValue('L5', "Provit")
		 ->setCellValue('M5', "Stok");
		 
		$dis="";
		$disvalue="ALL";
		if($distributor!=""){
			$dis = "AND b.id_supplier='$distributor'";
			$dv = $this->model_global->get_data(array('data' => 'row','table' => 'supplier', 'where' => array('id_supplier'=>$distributor)));
			$disvalue = $dv['nama']; 					
		} 
		$sql = "SELECT t.pelanggan, d.nama satuan,a.harga_beli, b.stok, b.id_supplier,b.kode,b.nama,a.qty,a.jenis_harga,a.harga,a.total,a.created_date,c.nama distributor
		,e.nama as kategori
		FROM item a
		left join transaksi t on t.id_transaksi = a.id_transaksi
		left join barang b on b.id_barang = a.id_barang
		left join supplier c on c.id_supplier = b.id_supplier
		left join satuan d on d.id_satuan = b.id_satuan
		left join kategori e on e.id_kategori = b.id_kategori
		where date(a.created_date) BETWEEN '$start' and '$end' and a.status='1' order by a.created_date asc";
		$result = $this->db->query($sql." $dis")->result_array(); 
		
		$dataArray= array();
		$nox=1;
		$no=1;
		$totjual = 0;
		$totdapat = 0;
		$totbeli = 0;
		
		$tqty=0;
		$thb=0;
		$thj=0;
		$tstok=0;
		if(isset($result) && count($result) > 0): foreach($result as $row):   
			switch($row['jenis_harga']){
				case "1": $jenis = "Resep"; break;
				case "2": $jenis = "Satuan"; break;
				case "3": $jenis = "Box"; break;
			}
			$totbeli = $row['harga_beli'] * $row['qty'];
			$pendapatan = $row['total'] - ($row['harga_beli'] * $row['qty']);
			$totdapat+=$pendapatan;
			$totjual+=$row['total'];
			$t = explode(" ",$row['created_date']);
			$tm = explode(":",$t[1]);
			$t2 = explode("-",$t[0]);
			$tgl = $t2[2]."/".$t2[1]."/".$t2[0]." ".$tm[0].":".$tm[1];
			$tqty = $tqty + (int)$row['qty'];
			$thb = $thb + (int)$row['harga_beli'];
			$thj = $thj + (int)$row['harga'];
			$tstok = $tstok + (int)$row['stok'];
			$tb = $tb + (int)$totbeli;
			
			$row_array['no'] = $nox;
			$row_array['pelanggan'] = $row['pelanggan'];
			$row_array['tanggal'] = $tgl;
			$row_array['code'] = $row['kode'];
			$row_array['nama'] = $row['nama'];
			$row_array['kategori'] = $row['kategori'];
			$row_array['qty'] = (int)$row['qty']." ".$row['satuan'];
			$row_array['hbeli'] = $row['harga_beli'];
			$row_array['totbeli'] = $totbeli;
			$row_array['harga'] = $row['harga'];
			$row_array['total'] = $row['total'];
			$row_array['provit'] = $pendapatan;
			$row_array['stok'] = $row['stok'];
			array_push($dataArray,$row_array);
			$no++;
			$nox++;
		endforeach; endif;		
		$nox=$no+6;
		$objPHPExcel->getActiveSheet()->fromArray($dataArray, NULL, 'A6'); 
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$nox, "")
		 ->setCellValue('B'.$nox, "")
		 ->setCellValue('C'.$nox, "")
		 ->setCellValue('D'.$nox, "")
		 ->setCellValue('E'.$nox, "")
		 ->setCellValue('F'.$nox, "")
		 ->setCellValue('G'.$nox, "")
		 ->setCellValue('H'.$nox, $thb)
		 ->setCellValue('I'.$nox, $tb)
		 ->setCellValue('J'.$nox, $thj)
		 ->setCellValue('K'.$nox, $totjual)
		 ->setCellValue('L'.$nox, $totdapat)
		 ->setCellValue('M'.$nox, $tstok);		
		
		$objPHPExcel->getActiveSheet()->getStyle('A5:M5')->getFont()->setBold(true);
		// Set fills
		$objPHPExcel->getActiveSheet()->getStyle('A5:M5')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
		$objPHPExcel->getActiveSheet()->getStyle('A5:M5')->getFill()->getStartColor()->setARGB('##000000');		
		$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(50.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(40.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(40.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setAutoSize(true);
		
		$sharedStyle1 = new PHPExcel_Style();
		$sharedStyle2 = new PHPExcel_Style();
		 
		$sharedStyle1->applyFromArray(
		 array('borders' => array(
		 'bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
		 'top' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
		 'right' => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
		 'left' => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
		 ),
		 ));
		 
		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1, "A5:M$nox");
		$objPHPExcel->getActiveSheet()->getRowDimension('5')->setRowHeight(27);
		// Set style for header row using alternative method
		$objPHPExcel->getActiveSheet()->getStyle('A5:M5')->applyFromArray(
		 array(
		 'font' => array(
		 'bold' => true,
		 'color' => array('rgb' => 'FFFFFF')
		 ),
		 'alignment' => array(
		 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
		 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
		 ),
		 'borders' => array(
		 'top' => array(
		 'style' => PHPExcel_Style_Border::BORDER_THIN
		 )
		 ),
		 'fill' => array(
		 'type' => PHPExcel_Style_Fill::FILL_SOLID,
		 'color' => array('rgb' => '000000')
		 )
		 )
		);		
		$p = str_replace(" ","_",$p); 
		$p = str_replace("/","_",$p); 
		
		$filename='Data_Penjualan_'.$d.'.xls';
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.$filename.'"');
		header('Cache-Control: max-age=0');
					 
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
		$objWriter->save('php://output');		
	}	
	public function print_laporan_pembelian(){
		$now = date("d")."/".date("m")."/".date(Y)." ".date("H").":".date("i").":".date("s");
		$start = $_GET['ps'];$end = $_GET['pe'];$kapan = $_GET['kapan'];
		$st = explode("-",$start);
		$ed = explode("-",$end);
		if($start==$end){
			$p = $st[2]."/".$st[1]."/".$st[0];
		}else{
			$p = $st[2]."/".$st[1]."/".$st[0]." s/d ".$ed[2]."/".$ed[1]."/".$ed[0];
			if($kapan=="2"){ 
				$p = $this->bulanarr[($st[1] * 1)]." ".$st[0]; 
			}
		}		
		$distributor = $_GET['d'];
		$diss="ALL";
		if($distributor!=""){
			$ds = $this->model_global->get_data(array('data' => 'row','table' => 'supplier', 'where' => array('id_supplier' => $distributor)));
			$diss = strtoupper($ds['nama']);
		}
		echo "<title>Laporan Pembelian</title>
		<script>window.print();</script>
			<style>
				@font-face {
					font-family: roboto;
					src: url(".base_url()."assets/webadmin/fonts/robotor.ttf);
				} 			
				body{
					margin:5px;
					padding:0px;
					font-family:roboto;
					font-size:14px;
					color:#000000;
				}
				h1{
					margin:2px 0px;
					font-size:18px;
				}
				h2{
					margin:2px 0px;
					font-size:15px;
				}
				td{
					font-size:12px;
				}
				th{
					font-size:13px;
				}
				.tb{
					width:100%;
					
				}
				.tb td{
					padding:3px;
				}
				.tb th{
					border-top:1px solid #000000;
					border-bottom:1px solid #000000;
					text-transform:uppercase;
					font-weight:bold;
					padding:5px; 
					text-align:left;
				}
			</style>
			
			<div class='option'></div>
			<div class='page'>
				<h1>APOTEK LIDDO FARMA</h1>
				Jl. Kp Siliwangi RT. 010 RW. 003<br>
				Kec. Cigombong - Bogor.
				<br><br>
				<h2 style='text-decoration:underline;'>LAPORAN PEMBELIAN</h2>
				<table width='100%' cellspacing='0' cellpadding='0'>
					<tr>
						<td width='80'>PERIODE</td><td width='10' align='center'>:</td><td>$p</td>
					</tr>
					<tr>
						<td >TGL CETAK</td><td width='20' align='center'>:</td><td>$now</td>
					</tr>
					<tr>
						<td >DISTRIBUTOR</td><td width='10' align='center'>:</td><td>$diss</td>
					</tr>
				</table>
				<table class='tb' cellpadding='0' cellspacing='0'>
					<thead>
					<tr>
						<th width='20' align='center'>No</th>
						<th width='60' >T. Faktur</th> 
						<th width='60' >T. Tempo</th> 
						<th width='50' >Code Obat</th> 
						<th width='' >Nama Obat</th> 
						<th width='90' >Kategori</th> 
						<th width='50' >Qty</th> 
						<th width='50' ><div align='right'>H. Awal</div></th> 
						<th width='50' ><div align='right'>H. Beli</div></th> 
						<th width='50' ><div align='right'>H. Rata²</div></th>  
						<th width='60' ><div align='right'>Jum. Beli</div></th>  
						<th width='50' ><div align='right'>Stok</div></th> 
					</tr></thead><tbody>";
					$dis="";
					$disvalue="ALL";
					if($distributor!=""){
						$dis = "AND c.id_supplier='$distributor'";
						$dv = $this->model_global->get_data(array('data' => 'row','table' => 'supplier', 'where' => array('id_supplier'=>$distributor)));
						$disvalue = $dv['nama']; 					
					} 
					$sql = "SELECT d.nama satuan,a.harga_awal,a.avg, b.stok, aa.id_supplier,b.kode,b.nama,a.qty ,a.harga,a.total,a.tanggal,a.jatuh_tempo,c.nama distributor
					,e.nama as kategori
					FROM beli a
					left join pembelian aa on aa.id_pembelian = a.id_pembelian
					left join barang b on b.id_barang = a.id_barang
					left join supplier c on c.id_supplier = aa.id_supplier
					left join satuan d on d.id_satuan = b.id_satuan
					left join kategori e on e.id_kategori = b.id_kategori
					where date(a.tanggal) BETWEEN '$start' and '$end' and a.status='1'"; 
					$result = $this->db->query($sql." $dis  order by a.tanggal asc")->result_array(); 
					
 				
					$no=1;   
					
					$tqty=0;
					$thb=0;
					$thj=0;
					$tstok=0;
					$totbeli=0;
					if(isset($result) && count($result) > 0): foreach($result as $row):  
						$totbeli = $row['harga'] * $row['qty'];
						$totjual+=$row['total']; 
						$t2 = explode("-",$row['tanggal']);
						$tgl = $t2[2]."/".$t2[1]."/".$t2[0]." ".$tm[0] ;
						$t2 = explode("-",$row['jatuh_tempo']);
						$tglt = $t2[2]."/".$t2[1]."/".$t2[0]." ".$tm[0] ;
						$tqty = $tqty + (int)$row['qty'];
						$thb = $thb + (int)$row['harga_awal'];
						$thj = $thj + (int)$row['harga'];
						$tstok = $tstok + (int)$row['stok'];
						$tb = $tb + (int)$row['avg']; 
						$tob = $tob + (int)$totbeli; 
				echo"
				<tr>
					<td align='center'>".$no."</td>
					<td>".$tgl."</td>
					<td>".$tglt."</td>
					<td>".$row['kode']."</td>
					<td>".$row['nama']."</td>
					<td>".$row['kategori']."</td>
					<td>".(int)$row['qty']." ".$row['satuan']."</td>
					<td><div align='right'>".number_format($row['harga_awal'])."</div></td>
					<td><div align='right'>".number_format($row['harga'])."</div></td>
					<td><div align='right'>".number_format($row['avg'])."</div></td> 
					<td><div align='right'>".number_format($totbeli)."</div></td> 
					<td><div align='right'>".$row['stok']."</div></td>
				</tr>";
				$no++;endforeach; endif;
				echo"
				</tbody><tr>
					<th colspan='6'><div align='right'></div></th> 
					<th>".(int)$tqty."</th>
					<th><div align='right'> ".number_format($thb)."</div></th>
					<th><div align='right'> ".number_format($thj)."</div></th> 
					<th><div align='right'> ".number_format($tb)."</div></th>
					<th><div align='right'> ".number_format($tob)."</div></th>
					<th><div align='right'>".$tstok."</div></th>
				</tr>				
				";
			echo"</div>";
	}
	public function laporan_pembelian_excel(){
		set_time_limit(0);
		$now = date("d")."/".date("m")."/".date(Y)." ".date("H").":".date("i").":".date("s");
		$start = $_GET['ps'];$end = $_GET['pe'];$kapan = $_GET['kapan'];
		$st = explode("-",$start);
		$ed = explode("-",$end);
		if($start==$end){
			$p = $st[2]."/".$st[1]."/".$st[0];
		}else{
			$p = $st[2]."/".$st[1]."/".$st[0]." s/d ".$ed[2]."/".$ed[1]."/".$ed[0];
			if($kapan=="2"){ 
				$p = $this->bulanarr[($st[1] * 1)]." ".$st[0]; 
			}
		}		
		$distributor = $_GET['d'];
		$diss="ALL";
		if($distributor!=""){
			$ds = $this->model_global->get_data(array('data' => 'row','table' => 'supplier', 'where' => array('id_supplier' => $distributor)));
			$diss = strtoupper($ds['nama']);
		}		
		$d = date('Y-m-d');  
		$this->load->library('excel');
		$objPHPExcel = new PHPExcel();
		$this->excel->setActiveSheetIndex(0);
		$objPHPExcel->getActiveSheet()->setTitle('Laporan Pembelian');
		$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Laporan Pembelian');
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16);
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->mergeCells('A1:C1');
		//$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->mergeCells('A2:B2');
		$objPHPExcel->getActiveSheet()->mergeCells('A3:B3');
		$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Periode'); 
		$objPHPExcel->getActiveSheet()->setCellValue('C2', $p); 
		$objPHPExcel->setActiveSheetIndex(0);
 
		$objPHPExcel->getActiveSheet()->setCellValue('A5', "No")
		 ->setCellValue('B5', "Supplier")
		 ->setCellValue('C5', "Tgl. Faktur")
		 ->setCellValue('D5', "Tgl. Tempo")
		 ->setCellValue('E5', "Code")
		 ->setCellValue('F5', "Nama")
		 ->setCellValue('G5', "Kategori")
		 ->setCellValue('H5', "Qty")
		 ->setCellValue('I5', "H. Awal")
		 ->setCellValue('J5', "H. Beli")
		 ->setCellValue('K5', "H. Rata²") 
		 ->setCellValue('L5', "Jum. Beli") 
		 ->setCellValue('M5', "Stok");
		 
		$dis="";
		$disvalue="ALL";
		if($distributor!=""){
			$dis = "AND c.id_supplier='$distributor'";
			$dv = $this->model_global->get_data(array('data' => 'row','table' => 'supplier', 'where' => array('id_supplier'=>$distributor)));
			$disvalue = $dv['nama']; 					
		} 
		$sql = "SELECT   d.nama satuan,a.harga_awal,a.avg, b.stok, b.id_supplier,b.kode,b.nama,a.qty ,a.harga,a.total,a.tanggal,a.jatuh_tempo,c.nama distributor
		,e.nama as kategori
		FROM beli a
		left join pembelian t on t.id_pembelian = a.id_pembelian
		left join barang b on b.id_barang = a.id_barang
		left join supplier c on c.id_supplier = t.id_supplier
		left join satuan d on d.id_satuan = b.id_satuan
		left join kategori e on e.id_kategori = b.id_kategori
		where date(a.tanggal) BETWEEN '$start' and '$end' and a.status='1'";
		$result = $this->db->query($sql." $dis  order by a.tanggal asc")->result_array(); 
		
		$dataArray= array();
		$nox=1;
		$no=1;
		$totjual = 0;
		$totdapat = 0;
		$totbeli = 0;
		
		$tqty=0;
		$thb=0;
		$thj=0;
		$tstok=0;
		if(isset($result) && count($result) > 0): foreach($result as $row):   
 
			$totbeli = $row['harga'] * $row['qty'];
			$t2 = explode("-",$row['tanggal']);
			$tgl = $t2[2]."/".$t2[1]."/".$t2[0]." ".$tm[0] ;
			$t2 = explode("-",$row['jatuh_tempo']);
			$tglt = $t2[2]."/".$t2[1]."/".$t2[0]." ".$tm[0] ;
			$tqty = $tqty + (int)$row['qty'];
			$thb = $thb + (int)$row['harga_awal'];
			$thj = $thj + (int)$row['harga'];
			$tstok = $tstok + (int)$row['stok'];
			$tb = $tb + (int)$row['avg'];
			$tob = $tob + (int)$totbeli;
			
			$row_array['no'] = $nox;
			$row_array['pelanggan'] = $row['distributor'];
			$row_array['tanggal'] = $tgl;
			$row_array['tempo'] = $tglt;
			$row_array['code'] = $row['kode'];
			$row_array['nama'] = $row['nama'];
			$row_array['kategori'] = $row['kategori'];
			$row_array['qty'] = (int)$row['qty']." ".$row['satuan'];
			$row_array['hbeli'] = $row['harga_awal'];
			$row_array['totbeli'] = $row['harga'];
			$row_array['harga'] = $row['avg'];
			$row_array['tob'] = $totbeli;
			$row_array['stok'] = $row['stok'];
			array_push($dataArray,$row_array);
			$no++;
			$nox++;
		endforeach; endif;		
		$nox=$no+6;
		$objPHPExcel->getActiveSheet()->fromArray($dataArray, NULL, 'A6'); 
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$nox, "")
		 ->setCellValue('B'.$nox, "")
		 ->setCellValue('C'.$nox, "")
		 ->setCellValue('D'.$nox, "")
		 ->setCellValue('E'.$nox, "")
		 ->setCellValue('F'.$nox, "")
		 ->setCellValue('G'.$nox, "")
		 ->setCellValue('H'.$nox, $tqty)
		 ->setCellValue('I'.$nox, $thb)
		 ->setCellValue('J'.$nox, $thj)
		 ->setCellValue('K'.$nox, $tb) 
		 ->setCellValue('L'.$nox, $tob)		
		 ->setCellValue('M'.$nox, $tstok);		
		
		$objPHPExcel->getActiveSheet()->getStyle('A5:M5')->getFont()->setBold(true);
		// Set fills
		$objPHPExcel->getActiveSheet()->getStyle('A5:M5')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
		$objPHPExcel->getActiveSheet()->getStyle('A5:M5')->getFill()->getStartColor()->setARGB('##000000');		
		$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(50.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(40.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(40.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true); 
		$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true); 
		$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setAutoSize(true); 
		
		$sharedStyle1 = new PHPExcel_Style();
		$sharedStyle2 = new PHPExcel_Style();
		 
		$sharedStyle1->applyFromArray(
		 array('borders' => array(
		 'bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
		 'top' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
		 'right' => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
		 'left' => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
		 ),
		 ));
		 
		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1, "A5:M$nox");
		$objPHPExcel->getActiveSheet()->getRowDimension('5')->setRowHeight(27);
		// Set style for header row using alternative method
		$objPHPExcel->getActiveSheet()->getStyle('A5:M5')->applyFromArray(
		 array(
		 'font' => array(
		 'bold' => true,
		 'color' => array('rgb' => 'FFFFFF')
		 ),
		 'alignment' => array(
		 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
		 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
		 ),
		 'borders' => array(
		 'top' => array(
		 'style' => PHPExcel_Style_Border::BORDER_THIN
		 )
		 ),
		 'fill' => array(
		 'type' => PHPExcel_Style_Fill::FILL_SOLID,
		 'color' => array('rgb' => '000000')
		 )
		 )
		);		
		$p = str_replace(" ","_",$p); 
		$p = str_replace("/","_",$p); 
		
		$filename='Data_Pembelian_'.$d.'.xls';
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.$filename.'"');
		header('Cache-Control: max-age=0');
					 
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
		$objWriter->save('php://output');		
	}	
	
	public function print_laporan_mutasi(){
		$now = date("d")."/".date("m")."/".date(Y)." ".date("H").":".date("i").":".date("s");
		$start = $_GET['ps'];$end = $_GET['pe'];$kapan = $_GET['kapan'];
		$st = explode("-",$start);
		$ed = explode("-",$end);
		if($start==$end){
			$p = $st[2]."/".$st[1]."/".$st[0];
		}else{
			$p = $st[2]."/".$st[1]."/".$st[0]." s/d ".$ed[2]."/".$ed[1]."/".$ed[0];
			if($kapan=="2"){ 
				$p = $this->bulanarr[($st[1] * 1)]." ".$st[0]; 
			}
		}		
		$distributor = $_GET['d'];
		$diss="ALL";
		if($distributor!=""){
			$ds = $this->model_global->get_data(array('data' => 'row','table' => 'supplier', 'where' => array('id_supplier' => $distributor)));
			$diss = strtoupper($ds['nama']);
		}
		echo "<title>Laporan Mutasi</title>
		<script>window.print();</script>
			<style>
				@font-face {
					font-family: roboto;
					src: url(".base_url()."assets/webadmin/fonts/robotor.ttf);
				} 			
				body{
					margin:5px;
					padding:0px;
					font-family:roboto;
					font-size:14px;
					color:#000000;
				}
				h1{
					margin:2px 0px;
					font-size:18px;
				}
				h2{
					margin:2px 0px;
					font-size:15px;
				}
				td{
					font-size:12px;
				}
				th{
					font-size:13px;
				}
				.tb{
					width:100%;
					
				}
				.tb td{
					padding:3px;
				}
				.tb th{
					border-top:1px solid #000000;
					border-bottom:1px solid #000000;
					text-transform:uppercase;
					font-weight:bold;
					padding:5px; 
					text-align:left;
				}
			</style>
			
			<div class='option'></div>
			<div class='page'>
				<h1>APOTEK LIDDO FARMA</h1>
				Jl. Kp Siliwangi RT. 010 RW. 003<br>
				Kec. Cigombong - Bogor.
				<br><br>
				<h2 style='text-decoration:underline;'>LAPORAN MUTASI</h2>
				<table width='100%' cellspacing='0' cellpadding='0'>
					<tr>
						<td width='80'>PERIODE</td><td width='10' align='center'>:</td><td>$p</td>
					</tr>
					<tr>
						<td >TGL CETAK</td><td width='20' align='center'>:</td><td>$now</td>
					</tr> 
				</table>
				<table class='tb' cellpadding='0' cellspacing='0'>
					<thead>
					<tr>
						<th width='20' align='center'>No</th>
						<th width='60' >T. Mutasi</th>  
						<th width='50' >Code Obat</th> 
						<th width='' >Nama Obat</th> 
						<th width='90' >Kategori</th> 
						<th width='50' >Qty</th> 
						<th width='50' ><div align='right'>Harga</div></th>  
						<th width='60' ><div align='right'>Jum. Harga</div></th>  
						<th width='50' ><div align='right'>Stok</div></th> 
					</tr></thead><tbody>";
					$dis="";
					$disvalue="ALL"; 
					$sql = "SELECT d.nama satuan, b.stok, b.kode,b.nama,a.qty ,a.harga,a.total,a.tanggal 
					,e.nama as kategori
					FROM mutasi_item a
					left join mutasi aa on aa.id_mutasi = a.id_mutasi
					left join barang b on b.id_barang = a.id_barang 
					left join satuan d on d.id_satuan = b.id_satuan
					left join kategori e on e.id_kategori = b.id_kategori
					where date(a.tanggal) BETWEEN '$start' and '$end' and a.status='1'"; 
					$result = $this->db->query($sql." $dis  order by a.tanggal asc")->result_array(); 
					
 				
					$no=1;   
					
					$tqty=0;
					$thb=0;
					$thj=0;
					$tstok=0;
					$totbeli=0;
					if(isset($result) && count($result) > 0): foreach($result as $row):  
						$totbeli = $row['harga'] * $row['qty'];
						$totjual+=$row['total']; 
						$t2 = explode("-",$row['tanggal']);
						$tgl = $t2[2]."/".$t2[1]."/".$t2[0]." ".$tm[0] ; 
						$tqty = $tqty + (int)$row['qty']; 
						$thj = $thj + (int)$row['harga'];
						$tstok = $tstok + (int)$row['stok'];
						$tb = $tb + (int)$row['avg']; 
						$tob = $tob + $totbeli; 
				echo"
				<tr>
					<td align='center'>".$no."</td>
					<td>".$tgl."</td> 
					<td>".$row['kode']."</td>
					<td>".$row['nama']."</td>
					<td>".$row['kategori']."</td>
					<td>".(int)$row['qty']." ".$row['satuan']."</td> 
					<td><div align='right'>".number_format($row['harga'])."</div></td> 
					<td><div align='right'>".number_format($totbeli)."</div></td> 
					<td><div align='right'>".$row['stok']."</div></td>
				</tr>";
				$no++;endforeach; endif;
				echo"
				</tbody><tr>
					<th colspan='5'><div align='right'></div></th> 
					<th>".(int)$tqty."</th> 
					<th><div align='right'> ".number_format($thj)."</div></th>  
					<th><div align='right'> ".number_format($tob)."</div></th>
					<th><div align='right'>".$tstok."</div></th>
				</tr>				
				";
			echo"</div>";
	}
	public function laporan_mutasi_excel(){
		set_time_limit(0);
		$now = date("d")."/".date("m")."/".date(Y)." ".date("H").":".date("i").":".date("s");
		$start = $_GET['ps'];$end = $_GET['pe'];$kapan = $_GET['kapan'];
		$st = explode("-",$start);
		$ed = explode("-",$end);
		if($start==$end){
			$p = $st[2]."/".$st[1]."/".$st[0];
		}else{
			$p = $st[2]."/".$st[1]."/".$st[0]." s/d ".$ed[2]."/".$ed[1]."/".$ed[0];
			if($kapan=="2"){ 
				$p = $this->bulanarr[($st[1] * 1)]." ".$st[0]; 
			}
		}		
		$distributor = $_GET['d'];
		$diss="ALL"; 		
		$d = date('Y-m-d');  
		$this->load->library('excel');
		$objPHPExcel = new PHPExcel();
		$this->excel->setActiveSheetIndex(0);
		$objPHPExcel->getActiveSheet()->setTitle('Laporan Mutasi');
		$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Laporan Mutasi');
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16);
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->mergeCells('A1:C1');
		//$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->mergeCells('A2:B2');
		$objPHPExcel->getActiveSheet()->mergeCells('A3:B3');
		$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Periode'); 
		$objPHPExcel->getActiveSheet()->setCellValue('C2', $p); 
		$objPHPExcel->setActiveSheetIndex(0);
 
		$objPHPExcel->getActiveSheet()->setCellValue('A5', "No")
		 ->setCellValue('B5', "Tgl. Mutasi")
		 ->setCellValue('C5', "Code")
		 ->setCellValue('D5', "Nama")
		 ->setCellValue('E5', "Kategori")
		 ->setCellValue('F5', "Qty")
		 ->setCellValue('G5', "Harga")
		 ->setCellValue('H5', "Jum. Beli") 
		 ->setCellValue('I5', "Stok")
		 ->setCellValue('J5', "Tujuan");
		 
		$dis="";
		$disvalue="ALL";  
		$sql = "SELECT   d.nama satuan, b.stok,  b.kode,b.nama,a.qty ,a.harga,a.total,a.tanggal
		,e.nama as kategori,t.tujuan
		FROM mutasi_item a
		left join mutasi t on t.id_mutasi = a.id_mutasi
		left join barang b on b.id_barang = a.id_barang 
		left join satuan d on d.id_satuan = b.id_satuan
		left join kategori e on e.id_kategori = b.id_kategori
		where date(a.tanggal) BETWEEN '$start' and '$end' and a.status='1'";
		$result = $this->db->query($sql." $dis  order by a.tanggal asc")->result_array(); 
		
		$dataArray= array();
		$nox=1;
		$no=1;
		$totjual = 0;
		$totdapat = 0;
		$totbeli = 0;
		
		$tqty=0;
		$thb=0;
		$thj=0;
		$tstok=0;
		if(isset($result) && count($result) > 0): foreach($result as $row):   
 
			$totbeli = $row['harga'] * $row['qty'];
			$t2 = explode("-",$row['tanggal']);
			$tgl = $t2[2]."/".$t2[1]."/".$t2[0]." ".$tm[0] ; 
			$tqty = $tqty + (int)$row['qty']; 
			$thj = $thj + (int)$row['harga'];
			$tstok = $tstok + (int)$row['stok'];
			$tb = $tb + (int)$row['avg'];
			$tob = $tob + $totbeli;
			
			$row_array['no'] = $nox; 
			$row_array['tanggal'] = $tgl; 
			$row_array['code'] = $row['kode'];
			$row_array['nama'] = $row['nama'];
			$row_array['kategori'] = $row['kategori'];
			$row_array['qty'] = (int)$row['qty']." ".$row['satuan']; 
			$row_array['totbeli'] = $row['harga']; 
			$row_array['tob'] = $totbeli;
			$row_array['stok'] = $row['stok'];
			$row_array['tujuan'] = $row['tujuan'];
			array_push($dataArray,$row_array);
			$no++;
			$nox++;
		endforeach; endif;		
		$nox=$no+6;
		$objPHPExcel->getActiveSheet()->fromArray($dataArray, NULL, 'A6'); 
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$nox, "")
		 ->setCellValue('B'.$nox, "")
		 ->setCellValue('C'.$nox, "")
		 ->setCellValue('D'.$nox, "")
		 ->setCellValue('E'.$nox, "")
		 ->setCellValue('F'.$nox, $tqty) 
		 ->setCellValue('G'.$nox, $thj) 
		 ->setCellValue('H'.$nox, $tob)		
		 ->setCellValue('I'.$nox, $tstok)		
		 ->setCellValue('J'.$nox, $row['tujuan']);		
		
		$objPHPExcel->getActiveSheet()->getStyle('A5:J5')->getFont()->setBold(true);
		// Set fills
		$objPHPExcel->getActiveSheet()->getStyle('A5:J5')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
		$objPHPExcel->getActiveSheet()->getStyle('A5:J5')->getFill()->getStartColor()->setARGB('##000000');		
		$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(50.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(40.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(40.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true); 
		$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true); 
		
		$sharedStyle1 = new PHPExcel_Style();
		$sharedStyle2 = new PHPExcel_Style();
		 
		$sharedStyle1->applyFromArray(
		 array('borders' => array(
		 'bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
		 'top' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
		 'right' => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
		 'left' => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
		 ),
		 ));
		 
		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1, "A5:J$nox");
		$objPHPExcel->getActiveSheet()->getRowDimension('5')->setRowHeight(27);
		// Set style for header row using alternative method
		$objPHPExcel->getActiveSheet()->getStyle('A5:J5')->applyFromArray(
		 array(
		 'font' => array(
		 'bold' => true,
		 'color' => array('rgb' => 'FFFFFF')
		 ),
		 'alignment' => array(
		 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
		 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
		 ),
		 'borders' => array(
		 'top' => array(
		 'style' => PHPExcel_Style_Border::BORDER_THIN
		 )
		 ),
		 'fill' => array(
		 'type' => PHPExcel_Style_Fill::FILL_SOLID,
		 'color' => array('rgb' => '000000')
		 )
		 )
		);		
		$p = str_replace(" ","_",$p); 
		$p = str_replace("/","_",$p); 
		
		$filename='Data_Mutasi_'.$d.'.xls';
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.$filename.'"');
		header('Cache-Control: max-age=0');
					 
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
		$objWriter->save('php://output');		
	}	
	
	public function laporan(){
		$this->template["judul"] = "Laporan Penjualan";
		$this->template['w'] = $this->model_global->get_data(array('data' => 'row','table' => 'website', 'where' => array('id_website' => 1)));
		$this->template['distributor'] = $this->model_global->get_data(array('select' => '*', 'table' => 'supplier','where' => array('status !=' => -1), 'order_by' => 'nama asc'));
		$this->template['result'] = array();
		$linklaporan = base_url()."webadmin/dashboard/print-laporan-penjualan";
		$linklaporanexcel = base_url()."webadmin/dashboard/laporan-penjualan-excel";
		if(isset($_POST['search'])){
			$exec = false;
			$kapan = $_POST['kapan'];
			$daily = $_POST['daily'];
			$mm1 = $_POST['mm1'];
			$mt1 = $_POST['mt1'];
			$mm2 = $_POST['mm2'];
			$mt2 = $_POST['mt2'];
			$distributor = $_POST['distributor'];
			if($kapan=="1"){
				if($daily==""){
					$this->response = $this->session->flashdata('response');
					$this->session->set_flashdata('response', array('status' => 'error', 'message' => 'Date range daily kosong'));	
					redirect(base_url().'webadmin/dashboard/laporan');
					
				}else{
					$exec = true;
					$ex = explode(" - ",$daily);
					$start = $ex[0];
					$end = $ex[1]; 
				}
			}else{
				$isi = true;
				if($mm1==""){
					$isi = false;
				}else{
					if($mt1==""){
						$isi = false;
					}else{
						if($mm2==""){
							$isi = false;
						}else{
							if($mt2==""){
								$isi = false;
							}
						}
					}
				}
				if($isi==true){
					$d1 = cal_days_in_month(CAL_GREGORIAN,$mm1,$mt1);
					$d2 = cal_days_in_month(CAL_GREGORIAN,$mm2,$mt2);
					$start = $mt1."-".$mm1."-01";
					$end = $mt2."-".$mm2."-".$d2; 
					$end2 = $mt2."-".$mm2."-01"; 
					if(date($start) > date($end)){
						$this->response = $this->session->flashdata('response');
						$this->session->set_flashdata('response', array('status' => 'error', 'message' => 'Periode awal tidak boleh lebih dari periode akhir'));	
						redirect(base_url().'webadmin/dashboard/laporan');
					}else{
						$exec = true;
					}
				}else{
					$this->response = $this->session->flashdata('response');
					$this->session->set_flashdata('response', array('status' => 'error', 'message' => 'Periksa kembali bulan dan tahun'));	
					redirect(base_url().'webadmin/dashboard/laporan');
					
				}
			}
			
			if($exec==true){
				$dis="";
				$disvalue="ALL";
				if($distributor!=""){
					$dis = "AND c.id_supplier='$distributor'";
					$dv = $this->model_global->get_data(array('data' => 'row','table' => 'supplier', 'where' => array('id_supplier'=>$distributor)));
					$disvalue = $dv['nama'];
					$querygraph = $this->db->query("SELECT sum(a.total) total,date(a.created_date) tanggal FROM item a
					left join barang b on b.id_barang = a.id_barang
					left join supplier c on c.id_supplier = b.id_supplier
					where date(a.created_date) BETWEEN '$start' and '$end'
					and a.status='1'
					and c.id_supplier='$distributor'
					group by date(a.created_date)
					order by a.created_date asc")->result_array(); 					
				}else{
					$querygraph = $this->db->query("SELECT sum(total) total,date(created_date) tanggal FROM item  where date(created_date) BETWEEN '$start' and '$end' and  status='1' group by date(created_date) order by created_date asc")->result_array(); 
				}
				$sql = "SELECT d.nama satuan,a.harga_beli, b.stok, b.id_supplier,b.kode,b.nama,a.qty,a.jenis_harga,a.harga,a.total,a.created_date,c.nama distributor
				,e.nama as kategori
				FROM item a
				left join barang b on b.id_barang = a.id_barang
				left join supplier c on c.id_supplier = b.id_supplier
				left join satuan d on d.id_satuan = b.id_satuan
				left join kategori e on e.id_kategori = b.id_kategori
				where date(a.created_date) BETWEEN '$start' and '$end'
				and a.status='1' ";
				$query = $this->db->query($sql." $dis order by a.created_date asc")->result_array(); 
				

				$this->template['result'] = $query;
				$this->template['resultgraph'] = $querygraph;
				
				$this->template['distributorvalue'] = $disvalue;
				$st = explode("-",$start);
				$ed = explode("-",$end);
				if($start==$end){
					$p = $st[2]."/".$st[1]."/".$st[0];
				}else{
					$p = $st[2]."/".$st[1]."/".$st[0]." s/d ".$ed[2]."/".$ed[1]."/".$ed[0];
					if($_POST['kapan']=="2"){
						if($start==$end2){
							$p = $this->bulanarr[($st[1] * 1)]." ".$st[0]; 
						}
					}
				}
				$this->template['periodevalue'] = $p;
				$linklaporan.= "?kapan=".$kapan."&ps=".$start."&pe=".$end."&d=".$distributor;
				$linklaporanexcel.= "?kapan=".$kapan."&ps=".$start."&pe=".$end."&d=".$distributor;
			}
		}
		$this->template['linklaporan'] = $linklaporan;
		$this->template['linklaporanexcel'] = $linklaporanexcel;
		$this->render('laporan');
		
	}
	public function laporanp(){
		$this->template["judul"] = "Laporan Pembelian";
		$this->template['w'] = $this->model_global->get_data(array('data' => 'row','table' => 'website', 'where' => array('id_website' => 1)));
		$this->template['distributor'] = $this->model_global->get_data(array('select' => '*', 'table' => 'supplier','where' => array('status !=' => -1), 'order_by' => 'nama asc'));
		$this->template['result'] = array();
		$linklaporan = base_url()."webadmin/dashboard/print-laporan-pembelian";
		$linklaporanexcel = base_url()."webadmin/dashboard/laporan-pembelian-excel";
		if(isset($_POST['search'])){
			$exec = false;
			$kapan = $_POST['kapan'];
			$daily = $_POST['daily'];
			$mm1 = $_POST['mm1'];
			$mt1 = $_POST['mt1'];
			$mm2 = $_POST['mm2'];
			$mt2 = $_POST['mt2'];
			$distributor = $_POST['distributor'];
			if($kapan=="1"){
				if($daily==""){
					$this->response = $this->session->flashdata('response');
					$this->session->set_flashdata('response', array('status' => 'error', 'message' => 'Date range daily kosong'));	
					redirect(base_url().'webadmin/dashboard/laporanp');
					
				}else{
					$exec = true;
					$ex = explode(" - ",$daily);
					$start = $ex[0];
					$end = $ex[1]; 
				}
			}else{
				$isi = true;
				if($mm1==""){
					$isi = false;
				}else{
					if($mt1==""){
						$isi = false;
					}else{
						if($mm2==""){
							$isi = false;
						}else{
							if($mt2==""){
								$isi = false;
							}
						}
					}
				}
				if($isi==true){
					$d1 = cal_days_in_month(CAL_GREGORIAN,$mm1,$mt1);
					$d2 = cal_days_in_month(CAL_GREGORIAN,$mm2,$mt2);
					$start = $mt1."-".$mm1."-01";
					$end = $mt2."-".$mm2."-".$d2; 
					$end2 = $mt2."-".$mm2."-01"; 
					if(date($start) > date($end)){
						$this->response = $this->session->flashdata('response');
						$this->session->set_flashdata('response', array('status' => 'error', 'message' => 'Periode awal tidak boleh lebih dari periode akhir'));	
						redirect(base_url().'webadmin/dashboard/laporanp');
					}else{
						$exec = true;
					}
				}else{
					$this->response = $this->session->flashdata('response');
					$this->session->set_flashdata('response', array('status' => 'error', 'message' => 'Periksa kembali bulan dan tahun'));	
					redirect(base_url().'webadmin/dashboard/laporanp');
					
				}
			}
			
			if($exec==true){
				$dis="";
				$disvalue="ALL";
				if($distributor!=""){
					$dis = "AND aa.id_supplier='$distributor'";
					$dv = $this->model_global->get_data(array('data' => 'row','table' => 'supplier', 'where' => array('id_supplier'=>$distributor)));
					$disvalue = $dv['nama'];
					$querygraph = $this->db->query("SELECT sum(a.total) total,date(aa.tanggal) tanggal FROM beli a
					left join pembelian aa on aa.id_pembelian = a.id_pembelian
					left join barang b on b.id_barang = a.id_barang
					left join supplier c on c.id_supplier = aa.id_supplier
					where date(a.tanggal) BETWEEN '$start' and '$end'
					and a.status='1'
					and c.id_supplier='$distributor'
					group by date(a.tanggal)
					order by a.tanggal asc")->result_array(); 					
				}else{
					$querygraph = $this->db->query("SELECT sum(total) total,date(tanggal) tanggal FROM beli  where date(tanggal) BETWEEN '$start' and '$end' and  status='1' group by date(tanggal) order by tanggal asc")->result_array(); 
				}
				$sql = "SELECT d.nama satuan,a.harga_awal, a.avg, b.stok, aa.id_supplier,b.kode,b.nama,a.qty,a.harga,a.total,a.tanggal,a.jatuh_tempo,c.nama distributor
				,e.nama as kategori
				FROM beli a
				left join pembelian aa on aa.id_pembelian = a.id_pembelian
				left join barang b on b.id_barang = a.id_barang
				left join supplier c on c.id_supplier = aa.id_supplier
				left join satuan d on d.id_satuan = b.id_satuan
				left join kategori e on e.id_kategori = b.id_kategori
				where date(a.tanggal) BETWEEN '$start' and '$end'
				and a.status='1' ";
				$sqls = $sql." $dis order by a.tanggal asc";
				$query = $this->db->query($sqls)->result_array(); 
 

				$this->template['result'] = $query;
				$this->template['resultgraph'] = $querygraph;
				
				$this->template['distributorvalue'] = $disvalue;
				$st = explode("-",$start);
				$ed = explode("-",$end);
				if($start==$end){
					$p = $st[2]."/".$st[1]."/".$st[0];
				}else{
					$p = $st[2]."/".$st[1]."/".$st[0]." s/d ".$ed[2]."/".$ed[1]."/".$ed[0];
					if($_POST['kapan']=="2"){
						if($start==$end2){
							$p = $this->bulanarr[($st[1] * 1)]." ".$st[0]; 
						}
					}
				}
				$this->template['periodevalue'] = $p;
				$linklaporan.= "?kapan=".$kapan."&ps=".$start."&pe=".$end."&d=".$distributor;
				$linklaporanexcel.= "?kapan=".$kapan."&ps=".$start."&pe=".$end."&d=".$distributor;
			}
		}
		$this->template['linklaporan'] = $linklaporan;
		$this->template['linklaporanexcel'] = $linklaporanexcel;
		$this->render('laporanp');
		
	}
	public function laporanm(){
		$this->template["judul"] = "Laporan Mutasi";
		$this->template['w'] = $this->model_global->get_data(array('data' => 'row','table' => 'website', 'where' => array('id_website' => 1))); 
		$this->template['result'] = array();
		$linklaporan = base_url()."webadmin/dashboard/print-laporan-mutasi";
		$linklaporanexcel = base_url()."webadmin/dashboard/laporan-mutasi-excel";
		if(isset($_POST['search'])){
			$exec = false;
			$kapan = $_POST['kapan'];
			$daily = $_POST['daily'];
			$mm1 = $_POST['mm1'];
			$mt1 = $_POST['mt1'];
			$mm2 = $_POST['mm2'];
			$mt2 = $_POST['mt2']; 
			if($kapan=="1"){
				if($daily==""){
					$this->response = $this->session->flashdata('response');
					$this->session->set_flashdata('response', array('status' => 'error', 'message' => 'Date range daily kosong'));	
					redirect(base_url().'webadmin/dashboard/laporanm');
					
				}else{
					$exec = true;
					$ex = explode(" - ",$daily);
					$start = $ex[0];
					$end = $ex[1]; 
				}
			}else{
				$isi = true;
				if($mm1==""){
					$isi = false;
				}else{
					if($mt1==""){
						$isi = false;
					}else{
						if($mm2==""){
							$isi = false;
						}else{
							if($mt2==""){
								$isi = false;
							}
						}
					}
				}
				if($isi==true){
					$d1 = cal_days_in_month(CAL_GREGORIAN,$mm1,$mt1);
					$d2 = cal_days_in_month(CAL_GREGORIAN,$mm2,$mt2);
					$start = $mt1."-".$mm1."-01";
					$end = $mt2."-".$mm2."-".$d2; 
					$end2 = $mt2."-".$mm2."-01"; 
					if(date($start) > date($end)){
						$this->response = $this->session->flashdata('response');
						$this->session->set_flashdata('response', array('status' => 'error', 'message' => 'Periode awal tidak boleh lebih dari periode akhir'));	
						redirect(base_url().'webadmin/dashboard/laporanm');
					}else{
						$exec = true;
					}
				}else{
					$this->response = $this->session->flashdata('response');
					$this->session->set_flashdata('response', array('status' => 'error', 'message' => 'Periksa kembali bulan dan tahun'));	
					redirect(base_url().'webadmin/dashboard/laporanm');
					
				}
			}
			
			if($exec==true){
				$dis="";
				$disvalue="ALL"; 
				$sss = "SELECT sum(total) total,date(tanggal) tanggal FROM mutasi_item  where date(tanggal) BETWEEN '$start' and '$end' and  status='1' group by date(tanggal) order by tanggal asc";
				 
				$querygraph = $this->db->query($sss)->result_array(); 

				$sql = "SELECT d.nama satuan, b.stok ,b.kode,b.nama,a.qty,a.harga,a.total,a.tanggal
				,e.nama as kategori
				FROM mutasi_item a
				left join mutasi aa on aa.id_mutasi = a.id_mutasi
				left join barang b on b.id_barang = a.id_barang 
				left join satuan d on d.id_satuan = b.id_satuan
				left join kategori e on e.id_kategori = b.id_kategori
				where date(a.tanggal) BETWEEN '$start' and '$end'
				and a.status='1' ";
				$sqls = $sql." $dis order by a.tanggal asc";
				$query = $this->db->query($sqls)->result_array(); 
 

				$this->template['result'] = $query;
				$this->template['resultgraph'] = $querygraph;
				
				$this->template['distributorvalue'] = $disvalue;
				$st = explode("-",$start);
				$ed = explode("-",$end);
				if($start==$end){
					$p = $st[2]."/".$st[1]."/".$st[0];
				}else{
					$p = $st[2]."/".$st[1]."/".$st[0]." s/d ".$ed[2]."/".$ed[1]."/".$ed[0];
					if($_POST['kapan']=="2"){
						if($start==$end2){
							$p = $this->bulanarr[($st[1] * 1)]." ".$st[0]; 
						}
					}
				}
				$this->template['periodevalue'] = $p;
				$linklaporan.= "?kapan=".$kapan."&ps=".$start."&pe=".$end;
				$linklaporanexcel.= "?kapan=".$kapan."&ps=".$start."&pe=".$end;
			}
		}
		$this->template['linklaporan'] = $linklaporan;
		$this->template['linklaporanexcel'] = $linklaporanexcel;
		$this->render('laporanm');
		
	}
	public function penjualan(){
		$this->template["judul"] = "Penjualan";
		$this->template['w'] = $this->model_global->get_data(array('data' => 'row','table' => 'website', 'where' => array('id_website' => 1)));
		$this->template['data'] = $this->model_global->get_data(array('select' => '*', 'table' => 'transaksi','where' => array('status !=' => -1,'date(created_date)'=>date("Y-m-d")), 'order_by' => 'id_transaksi desc'));
		//$this->template['data'] = $this->db->query("SELECT * FROM `transaksi` WHERE status !=-1 and  DATE(created_date) >= CURDATE()  - INTERVAL 7 DAY order by id_transaksi desc")->result_array();
		
		$this->render('penjualan');
	}
	public function history_penjualan(){
		$this->template["judul"] = "History Penjualan";
		if(isset($_POST['search'])){
			if($_POST['date']!=""){
				$date = $_POST['date'];
				$this->template['date'] = $date;
				$this->template['w'] = $this->model_global->get_data(array('data' => 'row','table' => 'website', 'where' => array('id_website' => 1)));
				$this->template['data'] = $this->model_global->get_data(array('select' => '*', 'table' => 'transaksi','where' => array('status !=' => -1,'date(created_date)'=>$date), 'order_by' => 'id_transaksi desc'));
				//$this->template['data'] = $this->db->query("SELECT * FROM `transaksi` WHERE status !=-1 and  DATE(created_date) >= CURDATE()  - INTERVAL 7 DAY order by id_transaksi desc")->result_array();
				
			}
		}
		$this->render('history-penjualan');
	}
	
	public function cetakstruk(){ 
		$id = $_GET['id'];
		$w = $this->model_global->get_data(array('data' => 'row','table' => 'website', 'where' => array('id_website' => 1)));
		$head = $this->model_global->get_data(array('data' => 'row','table' => 'transaksi', 'where' => array('id_transaksi' => $id))); 
		$item = $this->model_global->get_data(array(
			'select' => 'b.*,a.*, c.nama as satuan', 
			'table' => 'item a',
			'join' => array('barang b','b.id_barang = a.id_barang'),
			'join2' => array('satuan c','c.id_satuan = b.id_satuan'),
			'where' => array('a.status'=>1,'a.id_transaksi'=>$head['id_transaksi']), 
			'order_by' => 'b.nama asc'
		));		
		$isi = "<table width='100%' cellpadding='0' cellspacing='0'>";
		if(isset($item) && count($item) > 0):
		$no=1;
		foreach($item as $row):
			$qty = $row['qty'] * 1;
			$isi.="
				<tr>
					<td width='10 align='left' valign='top'>$no</td>
					<td width='' style='padding-bottom:5px;'>
						$row[nama] <br>
						$qty $row[satuan] x ".number_format($row['harga'])."
						
					</td>
					<td width='50' align='right' valign='top'>".number_format($row['total'])."</td>
				</tr>
			";
			$no++;
		endforeach; endif;
		$isi.= "</table>";
		echo " 
			<title>Cetak Struk</title>
			<script>window.opener.location.reload();window.print();</script>
			<style>
				@font-face {
					font-family: merchant;
					src: url(".base_url()."assets/webadmin/fonts/merchant.ttf);
				}
				@font-face {
					font-family: receipt;
					src: url(".base_url()."assets/webadmin/fonts/receipt.ttf);
				} 
				@font-face {
					font-family: roboto;
					src: url(".base_url()."assets/webadmin/fonts/robotor.ttf);
				} 
				body,html{
					padding:0px;
					margin:10px 0px; 
					color:#000000;
					font-size:11px;
				}
				body,html,div,td,p{
					letter-spacing:0px;
					font-family:roboto;
				}
				td{
					text-transform:uppercases;
					font-size:11px;
					/*font-family:'courier new';*/
				}
				p{
					margin:0px;padding:0px;
				}
				@media print{
					.cetak{display:none;}
					body.receipt { width: 58mm}
				}
				@page {
					size: 58mm;
					margin-left: 0px;
					margin-right: 0px;
					margin-top: 0px;
					margin-bottom: 0px;
				}
				.receipt{
					width:170px; 
				}
			</style>
			<div class='receipt'>
				<div align='left'>
					<div style='font-size:11px !important;'>$w[struk]</div><br>
					$head[no]<br>$head[created_date]<br><br>
					Yth Bpk/Ibu : ".strtoupper($head['pelanggan'])."
				</div>
				<div style='border-top:1px dashed #000000; margin:10px 0px; padding:5px 0px;'>
					$isi
				</div> 
				<div style='border-top:1px dashed #000000;border-bottom:1px dashed #000000; margin:5px 0px; padding:5px;'>
					<table width='100%' cellpadding='0' cellspacing='0'>
						<tr>
							<td></td>
							<td align='right'>Total</td><td width='10' align='center'>:</td>
							<td align='right' width='40'>".number_format($head['total'])."</td>
						</tr>
						<tr>
							<td></td>
							<td align='right'>Diskon</td><td width='10' align='center'>:</td>
							<td align='right'>".$head['diskon']."%</td>
						</tr>
						<tr>
							<td></td>
							<td align='right'></td><td width='10' align='center'></td>
							<td align='right'>".number_format($head['potongan'])."</td>
						</tr>
						<tr>
							<td></td>
							<td align='right'>Grand Total</td><td width='10' align='center'>:</td>
							<td align='right'>".number_format($head['grand_total'])."</td>
						</tr>
						<tr>
							<td></td>
							<td align='right'>Bayar</td><td width='10' align='center'>:</td>
							<td align='right'>".number_format($head['bayar'])."</td>
						</tr>
						<tr>
							<td></td>
							<td align='right'>Sisa</td><td width='10' align='center'>:</td>
							<td align='right'>".number_format($head['sisa'])."</td>
						</tr>
					</table>
				</div> 
				<div style='margin:5px 0px; padding:5px;'>
					<em>*) : $head[keterangan]</em> 
					
				</div>
				<div style='text-align:center;margin:10px 0px; padding:5px;'>
					* TERIMA KASIH * 
					
				</div>
				<div class='cetak' align='center'>
					<br>
					<a href='javascript:void(0);' onclick=\"window.print();\">C E T A K</a>
				</div>
			</div>
			
		";
	}
	public function cetakstrukmutasi(){ 
		$id = $_GET['id'];
		$w = $this->model_global->get_data(array('data' => 'row','table' => 'website', 'where' => array('id_website' => 1)));
		$head = $this->model_global->get_data(array('data' => 'row','table' => 'mutasi', 'where' => array('id_mutasi' => $id))); 
		$item = $this->model_global->get_data(array(
			'select' => 'b.*,a.*, c.nama as satuan', 
			'table' => 'mutasi_item a',
			'join' => array('barang b','b.id_barang = a.id_barang'),
			'join2' => array('satuan c','c.id_satuan = b.id_satuan'),
			'where' => array('a.status'=>1,'a.id_mutasi'=>$head['id_mutasi']),  
			'order_by' => 'b.nama asc'
		));		
		$isi = "<table width='100%' cellpadding='0' cellspacing='0'>";
		if(isset($item) && count($item) > 0):
		$no=1;
		foreach($item as $row):
			$qty = $row['qty'] * 1;
			$isi.="
				<tr>
					<td width='10 align='left' valign='top'>$no</td>
					<td width='' style='padding-bottom:5px;'>
						$row[nama] <br>
						$qty $row[satuan] x ".number_format($row['harga'])."
						
					</td>
					<td width='50' align='right' valign='top'>".number_format($row['total'])."</td>
				</tr>
			";
			$no++;
		endforeach; endif;
		$isi.= "</table>";
		echo " 
			<title>Cetak Struk</title>
			<script>window.opener.location.reload();window.print();</script>
			<style>
				@font-face {
					font-family: merchant;
					src: url(".base_url()."assets/webadmin/fonts/merchant.ttf);
				}
				@font-face {
					font-family: receipt;
					src: url(".base_url()."assets/webadmin/fonts/receipt.ttf);
				} 
				@font-face {
					font-family: roboto;
					src: url(".base_url()."assets/webadmin/fonts/robotor.ttf);
				} 
				body,html{
					padding:0px;
					margin:10px 0px; 
					color:#000000;
					font-size:11px;
				}
				body,html,div,td,p{
					letter-spacing:0px;
					font-family:roboto;
				}
				td{
					text-transform:uppercases;
					font-size:11px;
					/*font-family:'courier new';*/
				}
				p{
					margin:0px;padding:0px;
				}
				@media print{
					.cetak{display:none;}
					body.receipt { width: 58mm}
				}
				@page {
					size: 58mm;
					margin-left: 0px;
					margin-right: 0px;
					margin-top: 0px;
					margin-bottom: 0px;
				}
				.receipt{
					width:170px; 
				}
			</style>
			<div class='receipt'>
				<div align='left'>
					<div style='font-size:11px !important;'>$w[struk]</div><br>
					$head[no]<br>$head[created_date]<br><br> 
					Tujuan : ".strtoupper($head['tujuan'])."
				</div>
				<div style='border-top:1px dashed #000000; margin:10px 0px; padding:5px 0px;'>
					$isi
				</div> 
				<div style='border-top:1px dashed #000000;border-bottom:1px dashed #000000; margin:5px 0px; padding:5px;'>
					<table width='100%' cellpadding='0' cellspacing='0'>
						<tr>
							<td></td>
							<td align='right'>Total</td><td width='10' align='center'>:</td>
							<td align='right' width='40'>".number_format($head['total'])."</td>
						</tr>
						<!--<tr>
							<td></td>
							<td align='right'>Diskon</td><td width='10' align='center'>:</td>
							<td align='right'>".$head['diskon']."%</td>
						</tr>
						<tr>
							<td></td>
							<td align='right'></td><td width='10' align='center'></td>
							<td align='right'>".number_format($head['potongan'])."</td>
						</tr>
						<tr>
							<td></td>
							<td align='right'>Grand Total</td><td width='10' align='center'>:</td>
							<td align='right'>".number_format($head['grand_total'])."</td>
						</tr>
						<tr>
							<td></td>
							<td align='right'>Bayar</td><td width='10' align='center'>:</td>
							<td align='right'>".number_format($head['bayar'])."</td>
						</tr>
						<tr>
							<td></td>
							<td align='right'>Sisa</td><td width='10' align='center'>:</td>
							<td align='right'>".number_format($head['sisa'])."</td>
						</tr>--> 
					</table>
				</div> 
				<div style='margin:5px 0px; padding:5px;'>
					<em>*) : $head[keterangan]</em> 
					
				</div>
				<div style='text-align:center;margin:10px 0px; padding:5px;'>
					* TERIMA KASIH * 
					
				</div>
				<div class='cetak' align='center'>
					<br>
					<a href='javascript:void(0);' onclick=\"window.print();\">C E T A K</a>
				</div>
			</div>
			
		";
	}
	public function struk(){ 
		$c = $this->session->all_userdata();
		$this->template['datasession'] = $c["userinfo"];
		$this->template["judul"] = "Header Struk"; 
		$this->template['data'] = $this->model_global->get_data(array('data' => 'row','table' => 'website', 'where' => array('id_website' => 1)));
		$this->render('struk');
	}
	public function cariobat(){
		$ret['status']="false";
		$ret['message']="";
		//$k = $this->model_global->get_data(array('data' => 'row','table' => 'barang', 'where' => array('status' => 1, 'kode' => $_POST['kode'])));
		$k = $this->db->query("SELECT * from barang where status='1' and BINARY kode ='".$_POST['kode']."'")->result_array();
		if(count($k)>0){
			$ret['status']="true";
			$ret['id'] = $k[0]['id_barang'];
		}else{
			$ret['status']="false";
			$ret['message']="Data tidak ada";
		}
		echo json_encode($ret);
	}	
	public function transaksi(){ 
		$c = $this->session->all_userdata();
		$this->template['datasession'] = $c["userinfo"];
		$this->template["judul"] = "Transaksi Penjualan";
		$this->template["no"] = $this->cekode();
		$this->template['barang'] = $this->model_global->get_data(array(
			'select' => 'a.*, b.nama as satuan', 
			'table' => 'barang a',
			'join' => array('satuan b','b.id_satuan = a.id_satuan'),
			'where' => array('a.status !='=>-1,'a.stok >'=>'0'), 
			//'where' => array('a.status !='=>-1), 
			'order_by' => 'a.nama asc'
		));			
		$this->render('transaksi');
	}
	public function cekode(){ 
		$query = $this->db->query("SELECT CASE WHEN MAX(SUBSTRING(BINARY no,-5)) IS NULL THEN 0 ELSE MAX(SUBSTRING(BINARY no,-5)) END AS nilai FROM transaksi where SUBSTRING(created_date,6,2)='".date('m')."'  LIMIT 1 OFFSET 0")->result_array();
		//print_r($query);
		//SN17PMXXXX
		$dapet =  $query[0]['nilai'];
		$panjang = strlen($dapet *1 );
		$last = $dapet + 1; 
		$t = substr(date('Y'),2,2);
		$kode = $this->getKodeRand('13',$panjang, $last,"NT/".date('m').$t."/");
 
		return $kode;
	}
	public function cekodemutasi(){ 
		$query = $this->db->query("SELECT CASE WHEN MAX(SUBSTRING(BINARY no,-5)) IS NULL THEN 0 ELSE MAX(SUBSTRING(BINARY no,-5)) END AS nilai FROM mutasi where SUBSTRING(created_date,6,2)='".date('m')."'  LIMIT 1 OFFSET 0")->result_array();
		//print_r($query);
		//SN17PMXXXX
		$dapet =  $query[0]['nilai'];
		$panjang = strlen($dapet *1 );
		$last = $dapet + 1; 
		$t = substr(date('Y'),2,2);
		$kode = $this->getKodeRand('13',$panjang, $last,"MT/".date('m').$t."/");
 
		return $kode;
	}
	public function getKodeRand($lenght_max,$panjang, $laju_kode,$initial){
		$nol = $lenght_max - $panjang;
		$nol_lagi = str_repeat("0", $nol - strlen($initial));
		$kode = $initial . $nol_lagi . $laju_kode;
		return $kode;
	}
	public function mutasi(){  
		$a_date = date("Y-m-d");
		$last =  date("Y-m-t", strtotime($a_date));	
		$this->template["judul"] = "Mutasi";
		$this->template['data'] = $this->model_global->get_data(array(
			'select' => 'a.* ', 
			'table' => 'mutasi a', 
			'where' => array('a.status'=>1), 
			'order_by' => 'a.id_mutasi asc'
		));		
		$this->template['data'] = $this->db->query("SELECT a.*  FROM mutasi a   WHERE a.status =1  and date(tanggal)>='".date("Y-m")."-01'  and date(tanggal)<='$last' order by a.id_mutasi desc")->result_array();
		
		$this->render('mutasi');
	}
	public function mutasi_new(){ 
		$this->template["no"] = $this->cekodemutasi();
		$this->template["judul"] = "Insert Mutasi";
		$this->template['supplier'] = $this->model_global->get_data(array('select' => '*', 'table' => 'supplier','where' => array('status' => 1), 'order_by' => 'nama asc'));
		$this->template['barang'] = $this->model_global->get_data(array(
			'select' => 'a.*, b.nama as satuan', 
			'table' => 'barang a',
			'join' => array('satuan b','b.id_satuan = a.id_satuan'),
			'where' => array('a.status !='=>-1,'a.stok >='=>0), 
			'order_by' => 'a.nama asc'
		));			
		if(isset($_GET['_id']) && $_GET['_id'] != ''){
			$this->template["judul"] = "Edit Mutasi";
			$this->template['data'] = $this->model_global->get_data(array('data' => 'row','table' => 'mutasi', 'where' => array('status !=' => -1, 'id_mutasi' => $_GET['_id'])));
		}
		$this->render('mutasi-new');
	}	
	public function pembelian(){  
	$a_date = date("Y-m-d");
	$last =  date("Y-m-t", strtotime($a_date));
		$this->template["judul"] = "Pembelian";
		$this->template['data'] = $this->model_global->get_data(array(
			'select' => 'a.*, b.nama as supplier', 
			'table' => 'pembelian a',
			'join' => array('supplier b','b.id_supplier = a.id_supplier'),
			'where' => array('a.status !='=>-1), 
			'order_by' => 'a.id_pembelian asc'
		));		
		//$this->template['data'] = $this->db->query("SELECT a.*, b.nama as supplier FROM pembelian a left join supplier b on b.id_supplier = a.id_supplier WHERE a.status !=-1 and  DATE(a.created_date) >= CURDATE()  - INTERVAL 7 DAY order by a.id_pembelian desc")->result_array();
		$this->template['data'] = $this->db->query("SELECT a.*, b.nama as supplier FROM pembelian a left join supplier b on b.id_supplier = a.id_supplier WHERE a.status !=-1 and date(a.created_date)>='".date("Y-m")."-01'  and date(a.created_date)<='$last' order by a.id_pembelian desc")->result_array();
		
		$this->render('pembelian');
	}
	public function pembelian_new(){ 
		$this->template["judul"] = "Insert Pembelian";
		$this->template['supplier'] = $this->model_global->get_data(array('select' => '*', 'table' => 'supplier','where' => array('status' => 1), 'order_by' => 'nama asc'));
		$this->template['barang'] = $this->model_global->get_data(array(
			'select' => 'a.*, b.nama as satuan', 
			'table' => 'barang a',
			'join' => array('satuan b','b.id_satuan = a.id_satuan'),
			'where' => array('a.status !='=>-1), 
			'order_by' => 'a.nama asc'
		));			
		if(isset($_GET['_id']) && $_GET['_id'] != ''){
			$this->template["judul"] = "Edit Pembelian";
			$this->template['data'] = $this->model_global->get_data(array('data' => 'row','table' => 'pembelian', 'where' => array('status !=' => -1, 'id_pembelian' => $_GET['_id'])));
		}
		$this->render('pembelian-new');
	}	
	public function supplier(){  
		$this->template["judul"] = "Distributor";
		$this->template['data'] = $this->model_global->get_data(array('select' => '*', 'table' => 'supplier','where' => array('status !=' => -1), 'order_by' => 'nama asc'));
		$this->render('supplier');
	}
	public function supplier_new(){ 
		$this->template["judul"] = "Insert Distributor";
		if(isset($_GET['_id']) && $_GET['_id'] != ''){
			$this->template["judul"] = "Edit Distributor";
			$this->template['data'] = $this->model_global->get_data(array('data' => 'row','table' => 'supplier', 'where' => array('status !=' => -1, 'id_supplier' => $_GET['_id'])));
		}
		$this->render('supplier-new');
	}	
	public function satuan(){  
		$this->template["judul"] = "Satuan";
		$this->template['data'] = $this->model_global->get_data(array('select' => '*', 'table' => 'satuan','where' => array('status !=' => -1), 'order_by' => 'nama asc'));
		$this->render('satuan');
	}
	public function satuan_new(){ 
		$this->template["judul"] = "Insert Satuan";
		if(isset($_GET['_id']) && $_GET['_id'] != ''){
			$this->template["judul"] = "Edit Satuan";
			$this->template['data'] = $this->model_global->get_data(array('data' => 'row','table' => 'satuan', 'where' => array('status !=' => -1, 'id_satuan' => $_GET['_id'])));
		}
		$this->render('satuan-new');
	}	
	public function kategori(){  
		$this->template["judul"] = "Kategori";
		$this->template['data'] = $this->model_global->get_data(array('select' => '*', 'table' => 'kategori','where' => array('status !=' => -1), 'order_by' => 'nama asc'));
		$this->render('kategori');
	}
	public function kategori_new(){ 
		$this->template["judul"] = "Insert Kategori";
		if(isset($_GET['_id']) && $_GET['_id'] != ''){
			$this->template["judul"] = "Edit Kategori";
			$this->template['data'] = $this->model_global->get_data(array('data' => 'row','table' => 'kategori', 'where' => array('status !=' => -1, 'id_kategori' => $_GET['_id'])));
		}
		$this->render('kategori-new');
	}	
	public function barangajax(){  
  
		$search = strtolower($_POST['search']['value']);
		$limit = $_POST['length'];
		$start = $_POST['start'];
		$sql = $this->db->query("SELECT a.*, b.nama as satuan  FROM barang a left join satuan b on b.id_satuan = a.id_satuan  where a.status!='-1'")->result_array();
		$sql_count = count($sql);
		$order_field = $_POST['order'][0]['column'];
		$order_ascdesc = $_POST['order'][0]['dir'];
		$order = " ORDER BY a.".$_POST['columns'][$order_field]['data']." ".$order_ascdesc;
		$sqlqueryold = "SELECT  @NUM:=@NUM + 1 AS no,a.*, b.nama as satuan FROM barang a left join satuan b on b.id_satuan = a.id_satuan , (SELECT @NUM:=0)A where a.status!='-1'
			and (a.kode LIKE '%".$search."%' or a.nama LIKE '%".$search."%' or b.nama LIKE '%".$search."%') "; 
		$sqlquery = "SELECT  a.*, b.nama as satuan,c.nama as kategori FROM barang a left join satuan b on b.id_satuan = a.id_satuan left join kategori c on c.id_kategori = a.id_kategori where a.status!='-1'
			and (BINARY lower(a.kode) LIKE '%".$search."%' or BINARY  lower(a.nama) LIKE '%".$search."%' or BINARY  lower(b.nama) LIKE '%".$search."%'  or BINARY  lower(c.nama) LIKE '%".$search."%') "; 
			
		$embel = $order." LIMIT ".$limit." OFFSET ".$start;
		$query = $this->db->query($sqlquery)->result_array(); 
		$querydata = $this->db->query($sqlquery.$embel)->result_array(); 
		$sql_filter_count = count($query);
		$data = $querydata;		
		$callback = array(
			'draw'=>$_POST['draw'], 
			'recordsTotal'=>$sql_count,
			'recordsFiltered'=>$sql_filter_count,
			'data'=>$data
		);
		header('Content-Type: application/json');
		echo json_encode($callback);
	
	}
	public function barang(){  
		$this->template["judul"] = "Obat"; 
		$this->render('barangajax');
	}
	public function import(){  
		$this->template["judul"] = "Import Excel Data Obat"; 
		$this->render('import');
	}
	public function importdownload(){
		$d = date('Y-m-d');  
		$this->load->library('excel');
		$objPHPExcel = new PHPExcel();
		$this->excel->setActiveSheetIndex(0);
		$objPHPExcel->getActiveSheet()->setTitle('Data Stok Import');
 
		$objPHPExcel->getActiveSheet()->setCellValue('A1', "No")
		 ->setCellValue('B1', "Code")
		 ->setCellValue('C1', "Nama")
		 ->setCellValue('D1', "Kategori")
		 ->setCellValue('E1', "Stok")
		 ->setCellValue('F1', "Satuan")
		 ->setCellValue('G1', "H. Beli")
		 ->setCellValue('H1', "H. Resep")
		 ->setCellValue('I1', "H. Satuan")
		 ->setCellValue('J1', "H. Box")
		 ->setCellValue('K1', "Distributor");
		 
		$barang= $this->model_global->get_data(array(
			'select' => 'a.*, b.nama as satuan', 
			'table' => 'barang a',
			'join' => array('satuan b','b.id_satuan = a.id_satuan'),
			'where' => array('a.status'=>1,'stok !='=>'0'), 
			'order_by' => 'a.nama asc'
		));
		@$search = $_GET['s'];
		$sqlquery = "SELECT  a.*, b.nama as satuan,c.nama as kategori,s.nama as supplier
			FROM barang a
			left join satuan b on b.id_satuan = a.id_satuan
			left join kategori c on c.id_kategori = a.id_kategori
			left join supplier s on s.id_supplier = a.id_supplier
			where a.status!='-1'
			and (BINARY lower(a.kode) LIKE '%".$search."%' or BINARY  lower(a.nama) LIKE '%".$search."%' or BINARY  lower(b.nama) LIKE '%".$search."%'  or BINARY  lower(c.nama) LIKE '%".$search."%') ORDER BY a.nama asc"; 
			
		$barang = $this->db->query($sqlquery)->result_array(); 		
		$p = date("d")."/".date("m")."/".date("Y");
		$now = date(Y);
		 
		$dataArray= array();
		$no=0;		 
		$nos=1;		 
		$totresep = 0;
		$totsatuan = 0;
		$totbox = 0;
		$totdapat = 0;
		$totmodal = 0;
		
		$tmodal=0;
		$tstok=0;

		if(isset($barang) && count($barang) > 0): foreach($barang as $row):   
			$tmodal = $row['harga_beli'] * $row['stok'];
			$row_array['no'] = $nos;
			$row_array['code'] = $row['kode'];
			$row_array['nama'] = $row['nama'];
			$row_array['kategori'] = $row['kategori'];
			$row_array['stok'] = $row['stok'];
			$row_array['satuan'] = $row['satuan'];
			$row_array['hbeli'] = $row['harga_beli']; 
			$row_array['hresep'] = $row['harga_resep'];
			$row_array['hsatuan'] = $row['harga_satuan'];
			$row_array['hbox'] = $row['harga_box'];
			$row_array['supplier'] = $row['supplier'];
			array_push($dataArray,$row_array);
			$no++;
			$nos++;
		endforeach; endif; 	 
		$objPHPExcel->getActiveSheet()->fromArray($dataArray, NULL, 'A2');
		$objPHPExcel->getActiveSheet()->getStyle('A1:K1')->getFont()->setBold(true);
		// Set fills
		$objPHPExcel->getActiveSheet()->getStyle('A1:K1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
		$objPHPExcel->getActiveSheet()->getStyle('A1:K1')->getFill()->getStartColor()->setARGB('##000000');		
		$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(40.00);
		$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true);
		
		$sharedStyle1 = new PHPExcel_Style(); 
		 
		$sharedStyle1->applyFromArray(
		 array('borders' => array(
		 'bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
		 'top' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
		 'right' => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
		 'left' => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
		 ),
		 ));
		$nox = $no+1;
		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1, "A1:K$nox");
		$objPHPExcel->getActiveSheet()->getRowDimension('5')->setRowHeight(27);
		// Set style for header row using alternative method
		$objPHPExcel->getActiveSheet()->getStyle('A1:K1')->applyFromArray(
		 array(
		 'font' => array(
		 'bold' => true,
		 'color' => array('rgb' => 'FFFFFF')
		 ),
		 'alignment' => array(
		 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
		 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
		 ),
		 'borders' => array(
		 'top' => array(
		 'style' => PHPExcel_Style_Border::BORDER_THIN
		 )
		 ),
		 'fill' => array(
		 'type' => PHPExcel_Style_Fill::FILL_SOLID,
		 'color' => array('rgb' => '000000')
		 )
		 )
		);
		
		$filename='Data_Stok_Import_'.$d.'.xls';
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.$filename.'"');
		header('Cache-Control: max-age=0');
					 
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
		$objWriter->save('php://output');		
	}	
	public function barang_new(){  
		$this->template["judul"] = "Insert Obat";
		$this->template['kategori'] = $this->model_global->get_data(array('select' => '*', 'table' => 'kategori','where' => array('status' => 1), 'order_by' => 'nama asc'));
		$this->template['supplier'] = $this->model_global->get_data(array('select' => '*', 'table' => 'supplier','where' => array('status' => 1), 'order_by' => 'nama asc'));
		$this->template['satuan'] = $this->model_global->get_data(array('select' => '*', 'table' => 'satuan','where' => array('status' => 1), 'order_by' => 'nama asc'));
		if(isset($_GET['_id']) && $_GET['_id'] != ''){
			$this->template["judul"] = "Edit Obat";
			$this->template['data'] = $this->model_global->get_data(array('data' => 'row','table' => 'barang', 'where' => array('status !=' => -1, 'id_barang' => $_GET['_id'])));
		}
		$this->render('barang-new');
	}
	public function user(){
		$this->template['website'] = $this->model_global->get_data(array('data' => 'row','table' => 'website', 'where' => array('id_website' => 1)));
		$this->template["judul"] = "User CMS";
		$c = $this->session->all_userdata();
		$this->template['datasession'] = $c["userinfo"];
		$this->template['data'] = $this->model_global->get_data(array(
			'select' => '', 
			'table' => 'user a',
			//'join' => array('user b','b.id_user = a.id_user'),
			'where' => array('status !='=>-1), 
			'order_by' => 'id_user desc'
		));		
		$this->render('user');
	}
	public function user_new(){ 
		$this->template['website'] = $this->model_global->get_data(array('data' => 'row','table' => 'website', 'where' => array('id_website' => 1)));
		$c = $this->session->all_userdata();
		$this->template['datasession'] = $c["userinfo"];
		$this->template["judul"] = "User CMS Baru";
		if(isset($_GET['_id']) && $_GET['_id'] != ''){
			$this->template["judul"] = "Edit User CMS";
			$this->template['data'] = $this->model_global->get_data(array('data' => 'row','table' => 'user', 'where' => array('status !=' => -1, 'id_user' => $_GET['_id'])));
		}
		$this->render('user-new');
	}	
	public function validate_slug($table, $column, $slug, $id = 0) {
		if ($id > 0)
			$this->db->where('id !=', $id);

		$this->db->where($column." REGEXP '^".$slug."(\-[0-9]+)?$'");
		
		$this->db->order_by($column, 'desc');
		$this->db->limit(1, 0);
		$res = $this->db->get($table);
		if ($res->num_rows() == 0) {
			return $slug;
		} else {
			$row = $res->row_array();
			$slug2 = $row[$column];
			preg_match('/^(.+)([0-9]+)$/', $slug2, $found);
			if (empty($found)) {
				return $slug.'-1';
			} else {
				return $slug.'-'.((int)$found[2]+1);
			}
		}
	}
	public function reroute() {
		$action = str_replace('-', '_', $this->uri->segment(3));
		// echo $action;
		// die();
		if (method_exists($this, $action)) {
			call_user_func_array(array($this, $action), array());
		} else {
			show_404();
		}
	}
	
}

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