<?php
/*	
 *	Author 	: Ferry / Tanggal : 01-01-2015	
 *	Revisi 	: 
 */
 
class M_account_history extends CI_Model {    
    function get_all(){
        $AccountNumber      = $this->input->post('AccountNumber') ? $this->input->post('AccountNumber') : '';
        $CustomerCode       = $this->input->post('CustomerCode') ? $this->input->post('CustomerCode') : '';
        $VendorCode	    = $this->input->post('VendorCode') ? $this->input->post('VendorCode') : '';
        $DateFrom           = $this->input->post('DateFrom') ? $this->input->post('DateFrom') : '';
        $DateTo             = $this->input->post('DateTo') ? $this->input->post('DateTo') : '';
        $TransType          = $this->input->post('TransType') ? $this->input->post('TransType') : '';
        $BranchCode         = $this->session->userdata('branch');
        $BranchStatus       = $this->session->userdata('branch_status');
		
		$jml = count($TransType);
		if($TransType != ""){
			for($i=0;$i<=$jml;$i++){				
				if(!empty($TransType[$i]) && $TransType[$i]== 1){					
					$this->db->select("at_purchaseinvoice.PInvDate as Date, at_purchaseinvoice.PInvNumber as SourceNo, am1.AccountNumber as AccountNo,
									   am1.AccountName as AccountName, at_pinvjournal.PInvDesc as Description, at_pinvjournal.PInvSaldoDebet as Debet,
									   at_pinvjournal.PInvSaldoCredit as Credit");
					$this->db->join('at_pinvjournal','at_purchaseinvoice.PInvNumber=at_pinvjournal.PInvNumber','INNER');
					$this->db->join('am_accountgl as am1','am1.AccountNumber=at_pinvjournal.PInvAccountNumber','INNER');
					
					if($BranchStatus != 0){
						$this->db->where('at_purchaseinvoice.BranchCode', $BranchCode);
					} 	
					
					if($AccountNumber != "") {
						$this->db->like('am1.AccountNumber', $AccountNumber);
					}
					
					if($VendorCode != "") {
						$this->db->like('at_purchaseinvoice.VendorCode', $VendorCode);
					}
					
					if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
						$this->db->where('at_purchaseinvoice.PInvDate >=', date("Y-m-d", strtotime($DateFrom)));
						$this->db->where('at_purchaseinvoice.PInvDate <=', date("Y-m-d", strtotime($DateTo)));
					}
					
					$this->db->order_by('at_purchaseinvoice.PInvDate','ASC');
					$this->db->from('at_purchaseinvoice');
					$query1 = $this->db->get()->result();	
				} 
				
				if(!empty($TransType[$i]) && $TransType[$i]== 2){
					$this->db->select("at_receiveitem.PRecDate as Date, at_receiveitem.PRecNumber as SourceNo, am2.AccountNumber as AccountNo,
									   am2.AccountName as AccountName, at_receiveitem.PRecDesc as Description, at_precjournal.PRecSaldoDebet as Debet,
									   at_precjournal.PRecSaldoCredit as Credit");
					$this->db->join('at_precjournal','at_receiveitem.PRecNumber=at_precjournal.PRecNumber','INNER');
					$this->db->join('am_accountgl as am2','am2.AccountNumber=at_precjournal.PRecAccountNumber','INNER');
					
					if($BranchStatus != 0){
						$this->db->where('at_receiveitem.BranchCode', $BranchCode);
					} 	
					
					if($AccountNumber != "") {
						$this->db->like('am2.AccountNumber', $AccountNumber);
					}
					
					if($VendorCode != "") {
						$this->db->like('at_receiveitem.VendorCode', $VendorCode);
					}
					
					if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
						$this->db->where('at_receiveitem.PRecDate >=', date("Y-m-d", strtotime($DateFrom)));
						$this->db->where('at_receiveitem.PRecDate <=', date("Y-m-d", strtotime($DateTo)));
					}
					
					$this->db->order_by('at_receiveitem.PRecDate','ASC');
					$this->db->from('at_receiveitem');
					$query2 = $this->db->get()->result();	
				}
				
				if(!empty($TransType[$i]) && $TransType[$i]== 3){
					$this->db->select("at_purchasepayment.PPayDate as Date, at_purchasepayment.PPayNumber as SourceNo, am3.AccountNumber as AccountNo,
									   am3.AccountName as AccountName, at_ppayjournal.PPayDesc as Description, at_ppayjournal.PPaySaldoDebet as Debet,
									   at_ppayjournal.PPaySaldoCredit as Credit");
					$this->db->join('at_ppayjournal','at_purchasepayment.PPayNumber=at_ppayjournal.PPayNumber','INNER');
					$this->db->join('am_accountgl as am3','am3.AccountNumber=at_ppayjournal.PPayAccountNumber','INNER');
					
					if($BranchStatus != 0){
						$this->db->where('at_purchasepayment.BranchCode', $BranchCode);
					}

					if($VendorCode != "") {
						$this->db->like('at_purchasepayment.VendorCode', $VendorCode);
					}
					
					if($AccountNumber != "") {
						$this->db->like('am3.AccountNumber', $AccountNumber);
					}
					
					if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
						$this->db->where('at_purchasepayment.PPayDate >=', date("Y-m-d", strtotime($DateFrom)));
						$this->db->where('at_purchasepayment.PPayDate <=', date("Y-m-d", strtotime($DateTo)));
					}
					
					$this->db->order_by('at_purchasepayment.PPayDate','ASC');
					$this->db->from('at_purchasepayment');
					$query3 = $this->db->get()->result();	
				}
				
				if(!empty($TransType[$i]) && $TransType[$i]== 4){
					$this->db->select("at_purchasereturn.PRetdate as Date, at_purchasereturn.PRetNumber as SourceNo, am4.AccountNumber as AccountNo,
									   am4.AccountName as AccountName, at_pretjournal.PRetDesc as Description, at_pretjournal.PRetSaldoDebet as Debet,
									   at_pretjournal.PRetSaldoCredit as Credit");
					$this->db->join('at_pretjournal','at_purchasereturn.PRetNumber=at_pretjournal.PRetNumber','INNER');
					$this->db->join('am_accountgl as am4','am4.AccountNumber=at_pretjournal.PRetAccountNumber','INNER');
					
					if($BranchStatus != 0){
						$this->db->where('at_purchasereturn.BranchCode', $BranchCode);
					} 	
					
					if($VendorCode != "") {
						$this->db->like('at_purchasereturn.VendorCode', $VendorCode);
					}
					
					if($AccountNumber != "") {
						$this->db->like('am4.AccountNumber', $AccountNumber);
					}
					
					if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
						$this->db->where('at_purchasereturn.PRetdate >=', date("Y-m-d", strtotime($DateFrom)));
						$this->db->where('at_purchasereturn.PRetdate <=', date("Y-m-d", strtotime($DateTo)));
					}
					
					$this->db->order_by('at_purchasereturn.PRetdate','ASC');
					$this->db->from('at_purchasereturn');
					$query4 = $this->db->get()->result();	
				}
				
				if(!empty($TransType[$i]) && $TransType[$i]== 5){
					$this->db->select("at_salesinvoice.SInvDate as Date, at_salesinvoice.SInvNumber as SourceNo, am5.AccountNumber as AccountNo,
									   am5.AccountName as AccountName, at_sinvjournal.SInvDesc as Description, at_sinvjournal.SInvSaldoDebet as Debet,
									   at_sinvjournal.SInvSaldoCredit as Credit");
					$this->db->join('at_sinvjournal','at_salesinvoice.SInvNumber=at_sinvjournal.SInvNumber','INNER');
					$this->db->join('am_accountgl as am5','am5.AccountNumber=at_sinvjournal.SInvAccountNumber','INNER');
					
					if($BranchStatus != 0){
						$this->db->where('at_salesinvoice.BranchCode', $BranchCode);
					} 

					if($CustomerCode != "") {
						$this->db->like('at_salesinvoice.CustomerCode', $CustomerCode);
					}
					
					if($AccountNumber != "") {
						$this->db->like('am5.AccountNumber', $AccountNumber);
					}
					
					if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
						$this->db->where('at_salesinvoice.SInvDate >=', date("Y-m-d", strtotime($DateFrom)));
						$this->db->where('at_salesinvoice.SInvDate <=', date("Y-m-d", strtotime($DateTo)));
					}
					
					$this->db->order_by('at_salesinvoice.SInvDate','ASC');
					$this->db->from('at_salesinvoice');
					$query5 = $this->db->get()->result();	
				}
				
				if(!empty($TransType[$i]) && $TransType[$i]== 6){
					$this->db->select("at_deliveryorder.DOrdDate as Date, at_deliveryorder.DOrdNumber as SourceNo, am6.AccountNumber as AccountNo,
									   am6.AccountName as AccountName, at_dordjournal.DordDesc as Description, at_dordjournal.DOrdSaldoDebet as Debet,
									   at_dordjournal.DOrdSaldoCredit as Credit");
					$this->db->join('at_dordjournal','at_deliveryorder.DOrdNumber=at_dordjournal.DOrdNumber','INNER');
					$this->db->join('am_accountgl as am6','am6.AccountNumber=at_dordjournal.DOrdAccountNumber','INNER');
					
					if($BranchStatus != 0){
						$this->db->where('at_deliveryorder.BranchCode', $BranchCode);
					} 	
					
					if($CustomerCode != "") {
						$this->db->like('at_deliveryorder.CustomerCode', $CustomerCode);
					}
					
					if($AccountNumber != "") {
						$this->db->like('am6.AccountNumber', $AccountNumber);
					}
					
					if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
						$this->db->where('at_deliveryorder.DOrdDate >=', date("Y-m-d", strtotime($DateFrom)));
						$this->db->where('at_deliveryorder.DOrdDate <=', date("Y-m-d", strtotime($DateTo)));
					}
					
					$this->db->order_by('at_deliveryorder.DOrdDate','ASC');
					$this->db->from('at_deliveryorder');
					$query6 = $this->db->get()->result();	
				}
				
				if(!empty($TransType[$i]) && $TransType[$i]== 7){
					$this->db->select("at_salesreceipt.SRecDate as Date, at_salesreceipt.SRecNumber as SourceNo, am7.AccountNumber as AccountNo,
									   am7.AccountName as AccountName, at_srecjournal.SRecDesc as Description, at_srecjournal.SRecSaldoDebet as Debet,
									   at_srecjournal.SRecSaldoCredit as Credit");
					$this->db->join('at_srecjournal','at_salesreceipt.SRecNumber=at_srecjournal.SRecNumber','INNER');
					$this->db->join('am_accountgl as am7','am7.AccountNumber=at_srecjournal.SRecAccountNumber','INNER');
					
					if($BranchStatus != 0){
						$this->db->where('at_salesreceipt.BranchCode', $BranchCode);
					} 	
					
					if($CustomerCode != "") {
						$this->db->like('at_salesreceipt.CustomerCode', $CustomerCode);
					}
					
					if($AccountNumber != "") {
						$this->db->like('am7.AccountNumber', $AccountNumber);
					}
										
					if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
						$this->db->where('at_salesreceipt.SRecDate >=', date("Y-m-d", strtotime($DateFrom)));
						$this->db->where('at_salesreceipt.SRecDate <=', date("Y-m-d", strtotime($DateTo)));
					}
					
					$this->db->order_by('at_salesreceipt.SRecDate','ASC');
					$this->db->from('at_salesreceipt');
					$query7 = $this->db->get()->result();	
				}
				
				if(!empty($TransType[$i]) && $TransType[$i]== 8){
					$this->db->select("at_salesreturn.SRetdate as Date, at_salesreturn.SRetNumber as SourceNo, am8.AccountNumber as AccountNo,
									   am8.AccountName as AccountName, at_sretjournal.SRetDesc as Description, at_sretjournal.SRetSaldoDebet as Debet,
									   at_sretjournal.SRetSaldoCredit as Credit");
					$this->db->join('at_sretjournal','at_salesreturn.SRetNumber=at_sretjournal.SRetNumber','INNER');
					$this->db->join('am_accountgl as am8','am8.AccountNumber=at_sretjournal.SRetAccountNumber','INNER');
					
					if($BranchStatus != 0){
						$this->db->where('at_salesreturn.BranchCode', $BranchCode);
					} 

					if($CustomerCode != "") {
						$this->db->like('at_salesreturn.CustomerCode', $CustomerCode);
					}					
					
					if($AccountNumber != "") {
						$this->db->like('am8.AccountNumber', $AccountNumber);
					}
					
					if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
						$this->db->where('at_salesreturn.SRetdate >=', date("Y-m-d", strtotime($DateFrom)));
						$this->db->where('at_salesreturn.SRetdate <=', date("Y-m-d", strtotime($DateTo)));
					}
					
					$this->db->order_by('at_salesreturn.SRetdate','ASC');
					$this->db->from('at_salesreturn');
					$query8 = $this->db->get()->result();	
				}
				
			}
		   
           if(!empty($query1)){		   
			   foreach($query1 as $row){				 
					 $source ="Purchase Invoice";
						
					 $hasil1[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 
						
					 ); 
				}
			} else {
				$hasil1 = array();
			}

			if(!empty($query2)){		   
			   foreach($query2 as $row){				 
					 $source ="Receive Item";
						
					 $hasil2[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 						
					 ); 
				}
			} else {
				$hasil2 = array();
			}
			
			if(!empty($query3)){		   
			   foreach($query3 as $row){				 
					 $source ="Purchase Payment";
						
					 $hasil3[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 
						
					 ); 
				}
			} else {
				$hasil3 = array();
			}
			
			if(!empty($query4)){		   
			   foreach($query4 as $row){				 
					 $source ="Purchase Return";
						
					 $hasil4[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 
						
					 ); 
				}
			} else {
				$hasil4 = array();
			}
			
			if(!empty($query5)){		   
			   foreach($query5 as $row){				 
					 $source ="Sales Invoice";
						
					 $hasil5[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 
						
					 ); 
				}
			} else {
				$hasil5 = array();
			}
			
			if(!empty($query6)){		   
			   foreach($query6 as $row){				 
					 $source ="Delivery Order";
						
					 $hasil6[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 
						
					 ); 
				}
			} else {
				$hasil6 = array();
			}
			
			if(!empty($query7)){		   
			   foreach($query7 as $row){				 
					 $source ="Sales Receipt";
						
					 $hasil7[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 
						
					 ); 
				}
			} else {
				$hasil7 = array();
			}
			
			if(!empty($query8)){
			   foreach($query8 as $row){				 
					 $source ="Sales Return";
						
					 $hasil8[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 
						
					 ); 
				}
			} else {
				$hasil8 = array();
			}
			
			
			/* if(!empty($hasil1) && !empty($hasil2)){
				 $result = array_merge($hasil1, $hasil2);
			}
			elseif(!empty($hasil1) && !empty($hasil2) && !empty($hasil3)){
				$result = array_merge($hasil1, $hasil2, $hasil3);
			}
			elseif(!empty($hasil1) && !empty($hasil2) && !empty($hasil3) && !empty($hasil4)){
				$result = array_merge($hasil1, $hasil2, $hasil3, $hasil4);
			}
			elseif(!empty($hasil1) && !empty($hasil2) && !empty($hasil3) && !empty($hasil4) && !empty($hasil5)){
				$result = array_merge($hasil1, $hasil2, $hasil3, $hasil4, $hasil5);
			}
			elseif(!empty($hasil1) && !empty($hasil2) && !empty($hasil3) && !empty($hasil4) && !empty($hasil5) && !empty($hasil6)){
				$result = array_merge($hasil1, $hasil2, $hasil3, $hasil4, $hasil5, $hasil6);
			}
			elseif(!empty($hasil1) && !empty($hasil2) && !empty($hasil3) && !empty($hasil4) && !empty($hasil5) && !empty($hasil6) && !empty($hasil7)){
				$result = array_merge($hasil1, $hasil2, $hasil3, $hasil4, $hasil5, $hasil6, $hasil7);
			}
			elseif(!empty($hasil1) && !empty($hasil2) && !empty($hasil3) && !empty($hasil4) && !empty($hasil5) && !empty($hasil6) && !empty($hasil7) && !empty($hasil8)){
				$result = array_merge($hasil1, $hasil2, $hasil3, $hasil4, $hasil5, $hasil6, $hasil7, $hasil8);
			}
			else{
			    if(!empty($hasil1)){
				 $result = array_merge($hasil1);
				}
				
				elseif(!empty($hasil2)){
				 $result = array_merge($hasil2);
				}
				
				elseif(!empty($hasil3)){
					$result = array_merge($hasil3);
				}
				
				elseif(!empty($hasil4)){
					$result = array_merge($hasil4);
				}
				
				elseif(!empty($hasil5)){
					$result = array_merge($hasil5);
				}
				
				elseif(!empty($hasil6)){
					$result = array_merge($hasil6);
				}
				
				elseif(!empty($hasil7)){
					$result = array_merge($hasil7);
				}
				
				elseif(!empty($hasil8)){
					$result = array_merge($hasil8);
				}
				else{
					$result = array();
				}
			} */
			
			$result = array_merge($hasil1, $hasil2, $hasil3, $hasil4, $hasil5, $hasil6, $hasil7, $hasil8);
			return $result;
			
		} else {
			//query 1
			$this->db->select("at_purchaseinvoice.PInvDate as Date, at_purchaseinvoice.PInvNumber as SourceNo, am1.AccountNumber as AccountNo,
							   am1.AccountName as AccountName, at_pinvjournal.PInvDesc as Description, at_pinvjournal.PInvSaldoDebet as Debet,
							   at_pinvjournal.PInvSaldoCredit as Credit");
			$this->db->join('at_pinvjournal','at_purchaseinvoice.PInvNumber=at_pinvjournal.PInvNumber','INNER');
			$this->db->join('am_accountgl as am1','am1.AccountNumber=at_pinvjournal.PInvAccountNumber','INNER');
			
			if($BranchStatus != 0){
				$this->db->where('at_purchaseinvoice.BranchCode', $BranchCode);
			} 	
			
			if($AccountNumber != "") {
				$this->db->like('am1.AccountNumber', $AccountNumber);
			}
			
			if($VendorCode != "") {
				$this->db->like('at_purchaseinvoice.VendorCode', $VendorCode);
			}
			
			if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
				$this->db->where('at_purchaseinvoice.PInvDate >=', date("Y-m-d", strtotime($DateFrom)));
				$this->db->where('at_purchaseinvoice.PInvDate <=', date("Y-m-d", strtotime($DateTo)));
			}  else {
			    $date = date("Y-m-d");
				$this->db->where('at_purchaseinvoice.PInvDate',$date);
			}
			
			$this->db->order_by('at_purchaseinvoice.PInvDate','ASC');
			$this->db->from('at_purchaseinvoice');
			$query1 = $this->db->get()->result();
			
			//query 2
			$this->db->select("at_receiveitem.PRecDate as Date, at_receiveitem.PRecNumber as SourceNo, am2.AccountNumber as AccountNo,
									   am2.AccountName as AccountName, at_receiveitem.PRecDesc as Description, at_precjournal.PRecSaldoDebet as Debet,
									   at_precjournal.PRecSaldoCredit as Credit");
			$this->db->join('at_precjournal','at_receiveitem.PRecNumber=at_precjournal.PRecNumber','INNER');
			$this->db->join('am_accountgl as am2','am2.AccountNumber=at_precjournal.PRecAccountNumber','INNER');
			
			if($BranchStatus != 0){
				$this->db->where('at_receiveitem.BranchCode', $BranchCode);
			} 	
			
			if($AccountNumber != "") {
				$this->db->like('am2.AccountNumber', $AccountNumber);
			}
			
			if($VendorCode != "") {
				$this->db->like('at_receiveitem.VendorCode', $VendorCode);
			}
			
			if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
				$this->db->where('at_receiveitem.PRecDate >=', date("Y-m-d", strtotime($DateFrom)));
				$this->db->where('at_receiveitem.PRecDate <=', date("Y-m-d", strtotime($DateTo)));
			} else {
			    $date = date("Y-m-d");
				$this->db->where('at_receiveitem.PRecDate',$date);
			}
			
			$this->db->order_by('at_receiveitem.PRecDate','ASC');
			$this->db->from('at_receiveitem');
			$query2 = $this->db->get()->result();

			//query 3
			$this->db->select("at_purchasepayment.PPayDate as Date, at_purchasepayment.PPayNumber as SourceNo, am3.AccountNumber as AccountNo,
							   am3.AccountName as AccountName, at_ppayjournal.PPayDesc as Description, at_ppayjournal.PPaySaldoDebet as Debet,
							   at_ppayjournal.PPaySaldoCredit as Credit");
			$this->db->join('at_ppayjournal','at_purchasepayment.PPayNumber=at_ppayjournal.PPayNumber','INNER');
			$this->db->join('am_accountgl as am3','am3.AccountNumber=at_ppayjournal.PPayAccountNumber','INNER');
			
			if($BranchStatus != 0){
				$this->db->where('at_purchasepayment.BranchCode', $BranchCode);
			}

			if($VendorCode != "") {
				$this->db->like('at_purchasepayment.VendorCode', $VendorCode);
			}
			
			if($AccountNumber != "") {
				$this->db->like('am3.AccountNumber', $AccountNumber);
			}
			
			if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
				$this->db->where('at_purchasepayment.PPayDate >=', date("Y-m-d", strtotime($DateFrom)));
				$this->db->where('at_purchasepayment.PPayDate <=', date("Y-m-d", strtotime($DateTo)));
			} else {
			    $date = date("Y-m-d");
				$this->db->where('at_purchasepayment.PPayDate',$date);
			}
			
			$this->db->order_by('at_purchasepayment.PPayDate','ASC');
			$this->db->from('at_purchasepayment');
			$query3 = $this->db->get()->result();	
			
			//query 4
			$this->db->select("at_purchasereturn.PRetdate as Date, at_purchasereturn.PRetNumber as SourceNo, am4.AccountNumber as AccountNo,
							   am4.AccountName as AccountName, at_pretjournal.PRetDesc as Description, at_pretjournal.PRetSaldoDebet as Debet,
							   at_pretjournal.PRetSaldoCredit as Credit");
			$this->db->join('at_pretjournal','at_purchasereturn.PRetNumber=at_pretjournal.PRetNumber','INNER');
			$this->db->join('am_accountgl as am4','am4.AccountNumber=at_pretjournal.PRetAccountNumber','INNER');
			
			if($BranchStatus != 0){
				$this->db->where('at_purchasereturn.BranchCode', $BranchCode);
			} 	
			
			if($VendorCode != "") {
				$this->db->like('at_purchasereturn.VendorCode', $VendorCode);
			}
			
			if($AccountNumber != "") {
				$this->db->like('am4.AccountNumber', $AccountNumber);
			}
			
			if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
				$this->db->where('at_purchasereturn.PRetdate >=', date("Y-m-d", strtotime($DateFrom)));
				$this->db->where('at_purchasereturn.PRetdate <=', date("Y-m-d", strtotime($DateTo)));
			}  else {
			    $date = date("Y-m-d");
				$this->db->where('at_purchasereturn.PRetdate',$date);
			}
			
			$this->db->order_by('at_purchasereturn.PRetdate','ASC');
			$this->db->from('at_purchasereturn');
			$query4 = $this->db->get()->result();

			//query 5
			$this->db->select("at_salesinvoice.SInvDate as Date, at_salesinvoice.SInvNumber as SourceNo, am5.AccountNumber as AccountNo,
							   am5.AccountName as AccountName, at_sinvjournal.SInvDesc as Description, at_sinvjournal.SInvSaldoDebet as Debet,
							   at_sinvjournal.SInvSaldoCredit as Credit");
			$this->db->join('at_sinvjournal','at_salesinvoice.SInvNumber=at_sinvjournal.SInvNumber','INNER');
			$this->db->join('am_accountgl as am5','am5.AccountNumber=at_sinvjournal.SInvAccountNumber','INNER');
			
			if($BranchStatus != 0){
				$this->db->where('at_salesinvoice.BranchCode', $BranchCode);
			} 

			if($CustomerCode != "") {
				$this->db->like('at_salesinvoice.CustomerCode', $CustomerCode);
			}
			
			if($AccountNumber != "") {
				$this->db->like('am5.AccountNumber', $AccountNumber);
			}
			
			if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
				$this->db->where('at_salesinvoice.SInvDate >=', date("Y-m-d", strtotime($DateFrom)));
				$this->db->where('at_salesinvoice.SInvDate <=', date("Y-m-d", strtotime($DateTo)));
			}   else {
			    $date = date("Y-m-d");
				$this->db->where('at_salesinvoice.SInvDate',$date);
			}
			
			$this->db->order_by('at_salesinvoice.SInvDate','ASC');
			$this->db->from('at_salesinvoice');
			$query5 = $this->db->get()->result();	
			
			//query6
			$this->db->select("at_deliveryorder.DOrdDate as Date, at_deliveryorder.DOrdNumber as SourceNo, am6.AccountNumber as AccountNo,
							   am6.AccountName as AccountName, at_dordjournal.DordDesc as Description, at_dordjournal.DOrdSaldoDebet as Debet,
							   at_dordjournal.DOrdSaldoCredit as Credit");
			$this->db->join('at_dordjournal','at_deliveryorder.DOrdNumber=at_dordjournal.DOrdNumber','INNER');
			$this->db->join('am_accountgl as am6','am6.AccountNumber=at_dordjournal.DOrdAccountNumber','INNER');
			
			if($BranchStatus != 0){
				$this->db->where('at_deliveryorder.BranchCode', $BranchCode);
			} 	
			
			if($CustomerCode != "") {
				$this->db->like('at_deliveryorder.CustomerCode', $CustomerCode);
			}
			
			if($AccountNumber != "") {
				$this->db->like('am6.AccountNumber', $AccountNumber);
			}
			
			if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
				$this->db->where('at_deliveryorder.DOrdDate >=', date("Y-m-d", strtotime($DateFrom)));
				$this->db->where('at_deliveryorder.DOrdDate <=', date("Y-m-d", strtotime($DateTo)));
			}  else {
			    $date = date("Y-m-d");
				$this->db->where('at_deliveryorder.DOrdDate',$date);
			}
			
			$this->db->order_by('at_deliveryorder.DOrdDate','ASC');
			$this->db->from('at_deliveryorder');
			$query6 = $this->db->get()->result();	
			
			//query7
			$this->db->select("at_salesreceipt.SRecDate as Date, at_salesreceipt.SRecNumber as SourceNo, am7.AccountNumber as AccountNo,
							   am7.AccountName as AccountName, at_srecjournal.SRecDesc as Description, at_srecjournal.SRecSaldoDebet as Debet,
							   at_srecjournal.SRecSaldoCredit as Credit");
			$this->db->join('at_srecjournal','at_salesreceipt.SRecNumber=at_srecjournal.SRecNumber','INNER');
			$this->db->join('am_accountgl as am7','am7.AccountNumber=at_srecjournal.SRecAccountNumber','INNER');
			
			if($BranchStatus != 0){
				$this->db->where('at_salesreceipt.BranchCode', $BranchCode);
			} 	
			
			if($CustomerCode != "") {
				$this->db->like('at_salesreceipt.CustomerCode', $CustomerCode);
			}
			
			if($AccountNumber != "") {
				$this->db->like('am7.AccountNumber', $AccountNumber);
			}
			
			if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
				$this->db->where('at_salesreceipt.SRecDate >=', date("Y-m-d", strtotime($DateFrom)));
				$this->db->where('at_salesreceipt.SRecDate <=', date("Y-m-d", strtotime($DateTo)));
			} else {
			    $date = date("Y-m-d");
				$this->db->where('at_salesreceipt.SRecDate',$date);
			}
			
			$this->db->order_by('at_salesreceipt.SRecDate','ASC');
			$this->db->from('at_salesreceipt');
			$query7 = $this->db->get()->result();	
			
			//query8
			$this->db->select("at_salesreturn.SRetdate as Date, at_salesreturn.SRetNumber as SourceNo, am8.AccountNumber as AccountNo,
							   am8.AccountName as AccountName, at_sretjournal.SRetDesc as Description, at_sretjournal.SRetSaldoDebet as Debet,
							   at_sretjournal.SRetSaldoCredit as Credit");
			$this->db->join('at_sretjournal','at_salesreturn.SRetNumber=at_sretjournal.SRetNumber','INNER');
			$this->db->join('am_accountgl as am8','am8.AccountNumber=at_sretjournal.SRetAccountNumber','INNER');
			
			if($BranchStatus != 0){
				$this->db->where('at_salesreturn.BranchCode', $BranchCode);
			} 

			if($CustomerCode != "") {
				$this->db->like('at_salesreturn.CustomerCode', $CustomerCode);
			}					
			
			if($AccountNumber != "") {
				$this->db->like('am8.AccountNumber', $AccountNumber);
			}
			
			if($DateFrom!='' && $DateTo!='' && $DateFrom < $DateTo){
				$this->db->where('at_salesreturn.SRetdate >=', date("Y-m-d", strtotime($DateFrom)));
				$this->db->where('at_salesreturn.SRetdate <=', date("Y-m-d", strtotime($DateTo)));
			} else {
			    $date = date("Y-m-d");
				$this->db->where('at_salesreturn.SRetdate',$date);
			}
			
			$this->db->order_by('at_salesreturn.SRetdate','ASC');
			$this->db->from('at_salesreturn');
			$query8 = $this->db->get()->result();
			
			if(!empty($query1)){		   
			   foreach($query1 as $row){				 
					 $source ="Purchase Invoice";
						
					 $hasil1[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 
						
					 ); 
				}
			} else {
				$hasil1 = array();
			}

			if(!empty($query2)){		   
			   foreach($query2 as $row){				 
					 $source ="Receive Item";
						
					 $hasil2[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 						
					 ); 
				}
			} else {
				$hasil2 = array();
			}
			
			if(!empty($query3)){		   
			   foreach($query3 as $row){				 
					 $source ="Purchase Payment";
						
					 $hasil3[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 
						
					 ); 
				}
			} else {
				$hasil3 = array();
			}
			
			if(!empty($query4)){		   
			   foreach($query4 as $row){				 
					 $source ="Purchase Return";
						
					 $hasil4[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 
						
					 ); 
				}
			} else {
				$hasil4 = array();
			}
			
			if(!empty($query5)){		   
			   foreach($query5 as $row){				 
					 $source ="Sales Invoice";
						
					 $hasil5[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 
						
					 ); 
				}
			} else {
				$hasil5 = array();
			}
			
			if(!empty($query6)){		   
			   foreach($query6 as $row){				 
					 $source ="Delivery Order";
						
					 $hasil6[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 
						
					 ); 
				}
			} else {
				$hasil6 = array();
			}
			
			if(!empty($query7)){		   
			   foreach($query7 as $row){				 
					 $source ="Sales Receipt";
						
					 $hasil7[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 
						
					 ); 
				}
			} else {
				$hasil7 = array();
			}			
			
			if(!empty($query8)){
			   foreach($query8 as $row){				 
					 $source ="Sales Return";
						
					 $hasil8[] = array(
						'Date' => $row->Date,
						'Source' => $source,
						'SourceNo' => $row->SourceNo,
						'AccountNo' => $row->AccountNo,
						'AccountName' => $row->AccountName,
						'Description' => $row->Description,
						'Debet' => $row->Debet,
						'Credit' => $row->Credit 
						
					 ); 
				}
			} else {
				$hasil8 = array();
			}
			
			//if(!empty($hasil1) && !empty($hasil2) && !empty($hasil3) && !empty($hasil4) && !empty($hasil5) && !empty($hasil6) && !empty($hasil7) && !empty($hasil8)){
				$result = array_merge($hasil1, $hasil2, $hasil3, $hasil4, $hasil5, $hasil6, $hasil7, $hasil8);
			//} else {
			//	$result = array();
			//}
			
			return $result;
		} 
	}
	
	function get_akun(){
		/*=========================lemparan parah dari custom.js untuk searching_data_transaksi=====================*/
		$param = $this->input->post('param');
		/*====================================end===================================================================*/
		
		/*=================================post dari find di kiri====================================================*/
		$Number			= isset($param['txt_IDNumber']) ? $param['txt_IDNumber'] : '';
		$Desc			= isset($param['txt_Description']) ? $param['txt_Description'] : '';
		
		$Type			= isset($param['cmb_AccountType']) ? $param['cmb_AccountType'] : ''; 
		$DateFrom		= isset($param['txt_DateFrom']) ? $param['txt_DateFrom'] : '';
		$DateTo			= isset($param['txt_DateTo']) ? $param['txt_DateTo'] : '';
		$Menu			= isset($param['cmb_menu']) ? $param['cmb_menu'] : ''; 
		/*============================================end============================================================*/
		
		/*======================================scrol view 1-150=======================================*/
		$page		= ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
		$rows		= ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;
		
		$beginning 	= ($page - 1) * $rows;
		$end 		= $page * $rows;
		/*==============================================================================================*/
		
		/*==========================================by session=======================================================*/
		$sess_company		= $this->session->userdata('company');
        $sess_branch		= $this->session->userdata('branch');
        $sess_branch_status	= $this->session->userdata('branch_status');
		/*===========================================================================================================*/
		
        $this->db->select("av_account_history.AccountNo, av_account_history.AccountName, av_account_history.Debet, av_account_history.Credit, av_account_history.Date, av_account_history.Description, av_account_history.Source, av_account_history.SourceNo, av_account_history.TransLink");
        $this->db->from('av_account_history');
     	$this->db->join('am_accountgl','am_accountgl.AccountNumber = av_account_history.AccountNo','INNER');	
		$this->db->join('am_accountsubtype','am_accountsubtype.AccountSubTypeID = am_accountgl.AccountSubTypeID','INNER');
		
       /*===================================find by number dan desc=========================================================*/
		if($Number != ""){$this->db->like('av_account_history.AccountNo', $Number);}
		if($Desc != ""){$this->db->like('av_account_history.AccountName', $Desc);}
		if($Type != ""){$this->db->where('am_accountsubtype.AccountTypeID', $Type);}
		if($Menu != ""){
			if($Menu == 'All'){
				
			}
			else{
				$this->db->like('av_account_history.Source', $Menu);
			}
		} //change
		
		if((($DateFrom != '') && ($DateTo != ''))){
            if($DateFrom <= $DateTo){
				$this->db->where('av_account_history.Date >=', date("Y-m-d", strtotime($DateFrom)));
            	$this->db->where('av_account_history.Date <=', date("Y-m-d", strtotime($DateTo))); 
			}
        }
		
		$this->db->like('av_account_history.BranchCode', $sess_branch);
		/*==============================================sort tabel asc==========================================================*/
		$this->db->order_by('av_account_history.TransStatus', 'ASC');
		/*===============================end=================================================================================*/
		$query  = $this->db->limit($end, $beginning);
		$query = $this->db->get();
		return $query->result();
    }
	
	function get_account_history(){
		$this->db->distinct();
		$this->db->select('Source');
		$this->db->from('av_account_history');
		
		
		
		return $this->db->get()->result();
	}
}