<?php defined('BASEPATH') OR exit('No direct script access allowed');
	class Paymentmdl 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['om_id']] = strtoupper($value['om_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_contact,
							CONCAT(supplier.acc_contact, ' - ', supplier.acc_aumobile) as supplier_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 supplier ON(supplier.acc_id = vm.vm_party_id)
						LEFT JOIN account_master customer ON(customer.acc_id = vm.vm_party_id)
						WHERE vm.vm_type = 'PAYMENT'
						$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(){
			$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('SUPPLIER');
			$record['users'] 	= $this->Commonmdl->get_users();
			$record['groups'] 	= $this->config->item('group');
			$record['modes'] 	= $this->config->item('payment_mode');
			
			return $record;
		}
		public function get_data_for_edit($id){
			$master_query="
							SELECT vm.*, acc.acc_contact, 
							CONCAT(supplier.acc_contact, ' - ', supplier.acc_aumobile) as supplier_name, 
							CONCAT(customer.acc_contact, ' - ', 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 supplier ON(supplier.acc_id = vm.vm_party_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('SUPPLIER');
			$record['users'] 		= $this->Commonmdl->get_users();
			$record['groups'] 		= $this->config->item('group');
			$record['modes'] 		= $this->config->item('payment_mode');

			return $record;
		}
		public function get_trans($id){
			$query ="
						SELECT purchase.per_create_userid = 0 as am_id, purchase.per_id,
						CASE
    						WHEN purchase.per_entry_no = '' OR purchase.per_entry_no IS NULL THEN purchase.per_unregi_entry_no
    						ELSE purchase.per_entry_no
  						END AS per_entry_no,
						DATE_FORMAT(purchase.per_entry_date, '%d-%m-%Y') as per_entry_date,
						purchase.per_current_dollar_rate,
						(purchase.per_total_amt - (purchase.per_allocated_amt + purchase.per_round_off)) as per_balance,
						(purchase.per_allocated_amt + purchase.per_round_off) as per_allocated_amt
						FROM purchase_master purchase
						WHERE purchase.per_supplier_id = $id
						AND ((purchase.per_total_amt) - (purchase.per_allocated_amt + purchase.per_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 = 'PAYMENT'
                        $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 (supplier.acc_contact LIKE '%".$name."%' OR supplier.acc_aumobile LIKE '%".$name."%') ";
            }
            $query ="
                        SELECT supplier.acc_id as id,  
							CONCAT(supplier.acc_contact, ' - ', supplier.acc_aumobile) as name
                        FROM voucher_master vm
                        INNER JOIN account_master supplier ON(supplier.acc_id = vm.vm_party_id)
                        WHERE vm_fin_year = '".$_SESSION['fin_year']."'
                        AND vm_type = 'PAYMENT'
                        $subsql
                        GROUP BY supplier.acc_id
                        ORDER BY vm_entry_no ASC
                        LIMIT 10
                    ";
            // echo $query; exit();
            return $this->db->query($query)->result_array();
        }
	}
?>