<?php defined('BASEPATH') OR exit('No direct script access allowed');
	class Receiptmdl extends CI_model{
		protected $master;
		protected $trans;
		public function __construct(){
			parent::__construct();

			$this->master = 'voucher_master';
			$this->trans  = 'voucher_trans';
			$this->load->model('voucher/Commonmdl');
			$this->config->load('extra');
		}
		public function isExist($id){
			
			return false;
		}
		public function get_record($condition, $wantDropDown = false){
			$record = [];
			$data 	= $this->db->get_where($this->master,$condition)->result_array();
			if(!$wantDropDown) return $data;
			if(empty($data)){
				$record[0] = 'NO ENTRY ADDED';
			}else{
				$record[0] = 'SELECT';
				foreach ($data as $key => $value) {
					$record[$value['sale_id']] = strtoupper($value['sale_entry_no']);
				}
			}
			return $record;
		}
		public function get_data($wantCount, $per_page = 20, $offset = 0){
			$record 	= [];
			$subsql 	= '';
			$limit  	= '';
			$ofset  	= '';
			
			if(!$wantCount){
				$limit .= " LIMIT $per_page";
				$ofset .= " OFFSET $offset";
			}
			
			if(isset($_GET['entry_no']) && !empty($_GET['entry_no'])){
                $subsql .=" AND vm.vm_entry_no = ".$_GET['entry_no'];
                $record['search']['entry_no']['text'] = $_GET['entry_no'];
                $record['search']['entry_no']['value'] = $_GET['entry_no'];
            }
            if(isset($_GET['date_from']) && !empty($_GET['date_from'])){
				$subsql .=" AND vm.vm_entry_date >= '".date('Y-m-d', strtotime($_GET['date_from']))."'";
			}
			if(isset($_GET['date_to']) && !empty($_GET['date_to'])){
				$subsql .=" AND vm.vm_entry_date <= '".date('Y-m-d', strtotime($_GET['date_to']))."'";
			}
			if(isset($_GET['group']) && !empty($_GET['group'])){
				$subsql .=" AND vm.vm_group = '".$_GET['group']."'";
			}
			if(isset($_GET['accounts']) && !empty($_GET['accounts'])){
				$subsql .=" AND vm.vm_acc_id = ".$_GET['accounts'];
			}
			if(isset($_GET['_party_id']) && !empty($_GET['_party_id'])){
				$subsql .=" AND vm.vm_party_id = ".$_GET['_party_id'];
			}
			if(isset($_GET['modes']) && !empty($_GET['modes'])){
				$subsql .=" AND vm.vm_mode = '".$_GET['modes']."'";
			}
			if(isset($_GET['amt_from']) && !empty($_GET['amt_from'])){
				$subsql .=" AND vm.vm_total_amt >= '".$_GET['amt_from']."'";
			}
			if(isset($_GET['amt_to']) && !empty($_GET['amt_to'])){
				$subsql .=" AND vm.vm_total_amt <= '".$_GET['amt_to']."'";
			}
			$query ="
						SELECT vm.*, acc.acc_name, 
						CONCAT(customer.acc_contact, ' - ', customer.acc_aumobile) as customer_name
						FROM ".$this->master." vm
						LEFT JOIN account_master acc ON(acc.acc_id = vm.vm_acc_id)
						LEFT JOIN account_master customer ON(customer.acc_id = vm.vm_party_id)
						WHERE vm.vm_type = 'RECEIPT'
						AND vm.vm_total_amt > 0
						$subsql
						ORDER BY vm.vm_id DESC
						$limit
						$ofset
					";
			// echo "<pre>"; print_r($query); exit;
			if($wantCount){
				return $this->db->query($query)->num_rows();
			}
			$record['data'] = $this->db->query($query)->result_array();

			if(!empty($record['data'])){
				foreach ($record['data'] as $key => $value) {
					$record['data'][$key]['isExist'] = $this->isExist($value['vm_id']);
				}
			}
			$accounts = $this->Commonmdl->get_accounts();
			if(!empty($accounts)){
				$record['accounts'][0] = 'ALL';
				foreach ($accounts as $key => $value) {
					$record['accounts'][$key] = strtoupper($value);	
				}
			}else{
				$record['accounts'][0] = 'NO TYPE ADDED';
			}
			$modes = $this->config->item('payment_mode');
			if(!empty($modes)){
				$record['modes'][0] = 'ALL';
				foreach ($modes as $key => $value) {
					$record['modes'][$key] = strtoupper($value);	
				}
			}else{
				$record['modes'][0] = 'NO MODE ADDED';
			}
			// echo "<pre>"; print_r($record);exit();
			return $record;
		}
		
		public function get_data_for_add(){
			$party_id = isset($_GET['party_id']) && !empty($_GET['party_id']) ? $_GET['party_id'] : 0;
			$record['vm_entry_no'] = $this->db_operations->get_fin_year_branch_max_id($this->master, 'vm_entry_no', 'vm_fin_year', $_SESSION['fin_year'],'');
			$record['accounts'] = $this->Commonmdl->get_accounts('CUSTOMER');
			$record['users'] 	= $this->Commonmdl->get_users();
			$record['groups'] 	= $this->config->item('group');
			$record['modes'] 	= $this->config->item('payment_mode');
			$record['party'] 	= $this->db_operations->get_record('account_master', ['acc_id' => $party_id]);
			return $record;
		}
		public function get_data_for_edit($id){
			$master_query="
							SELECT vm.*, acc.acc_name, 
							CONCAT(customer.acc_name, ' - ', customer.acc_aumobile) as customer_name, 
							prep.user_full_name as prepared, chec.user_full_name as checked
							FROM voucher_master vm
							LEFT JOIN account_master acc ON(acc.acc_id = vm.vm_acc_id)
							LEFT JOIN account_master customer ON(customer.acc_id = vm.vm_party_id)
							LEFT JOIN user_master prep ON(prep.user_id = vm.vm_prepared_id)
							LEFT JOIN user_master chec ON(chec.user_id = vm.vm_checked_id)
							WHERE vm.vm_id = $id
						  ";
			$trans_query ="
							SELECT vt.*
							FROM voucher_trans vt
							WHERE vt.vt_vm_id = $id
						  ";
			$record['master_data'] 	= $this->db->query($master_query)->result_array();
			$record['trans_data'] 	= $this->db->query($trans_query)->result_array();
			$record['accounts'] 	= $this->Commonmdl->get_accounts();
			$record['users'] 		= $this->Commonmdl->get_users();
			$record['groups'] 		= $this->config->item('group');
			$record['modes'] 	= $this->config->item('payment_mode');
			// echo "<pre>"; print_r($record);exit;
			return $record;
		}
		public function get_trans($id){
			$query ="
						SELECT sale.sale_create_userid = 0 as am_id, sale.sale_id,
						CASE
    						WHEN sale.sale_entry_no = '' OR sale.sale_entry_no IS NULL THEN sale.sale_unregi_entry_no
    						ELSE sale.sale_entry_no
  						END AS sale_entry_no,
						DATE_FORMAT(sale.sale_entry_date, '%d-%m-%Y') as sale_entry_date,
						sale.sale_current_dollar_rate,
						(sale.sale_total_amt - (sale.sale_allocated_amt + sale.sale_round_off)) as sale_balance,
						(sale.sale_allocated_amt + sale.sale_round_off) as sale_allocated_amt
						FROM sales_master sale
						WHERE sale.sale_customer_id = $id
						AND ((sale.sale_total_amt) - (sale.sale_allocated_amt + sale.sale_round_off)) > 0
					";
			// echo $query; exit;
			$data = $this->db->query($query)->result_array();

			// $query1 ="
			// 			SELECT am.am_created_by = 0 as om_id, am.am_id, am.am_entry_no as om_entry_no, 
			// 			DATE_FORMAT(am.am_entry_date, '%d-%m-%Y') as om_entry_date,
			// 			(am.am_total_amt - (am.am_advance_amt + am.am_allocated_amt + am.am_allocated_round)) as om_balance,
			// 			(am.am_allocated_amt + am.am_allocated_round) as om_allocated_amt
			// 			FROM alter_master am
			// 			WHERE am.am_customer_id = $id
			// 			AND ((am.am_total_amt - am.am_advance_amt) - (am.am_allocated_amt + am.am_allocated_round)) > 0
			// 		";
			// // echo $query; exit;
			// $data1 = $this->db->query($query1)->result_array();
			// if(!empty($data1)){
			// 	$sr_no = count($data);
			// 	foreach ($data1 as $key => $value) {
			// 		$data[$sr_no] = $value;
			// 		$sr_no++;
			// 	}
			// }
			return $data;
		}
		public function get_select2_entry_no(){
            $subsql = "";
            if(isset($_GET['name']) && !empty($_GET['name'])){
                $name   = $_GET['name'];
                $subsql .= " AND (vm_entry_no LIKE '%".$name."%') ";
            }
            $query ="
                        SELECT vm_entry_no as id, vm_entry_no as name
                        FROM voucher_master
                        WHERE vm_fin_year = '".$_SESSION['fin_year']."'
                        AND vm_type = 'RECEIPT'
                        $subsql
                        ORDER BY vm_entry_no ASC
                        LIMIT 10
                    ";
            // echo $query; exit();
            return $this->db->query($query)->result_array();
        }
        public function get_select2_party_id(){
            $subsql = "";
            if(isset($_GET['name']) && !empty($_GET['name'])){
                $name   = $_GET['name'];
                $subsql .= " AND (km.km_code LIKE '%".$name."%' OR km.km_mobile LIKE '%".$name."%') ";
            }
            $query ="
                        SELECT km.km_id as id, CONCAT(km.km_code,' - ',km.km_mobile) as name
                        FROM voucher_master vm
                        INNER JOIN karigar_master km ON(km.km_id = vm.vm_party_id)
                        WHERE vm_fin_year = '".$_SESSION['fin_year']."'
                        AND vm_type = 'RECEIPT'
                        $subsql
                        GROUP BY km.km_id
                        ORDER BY vm_entry_no ASC
                        LIMIT 10
                    ";
            // echo $query; exit();
            return $this->db->query($query)->result_array();
        }
	}
?>