<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Common extends CI_Controller{
	protected $master;
	protected $trans;
	protected $session_expired;
	public $excel;
	public $model;
	public $Paymentmdl;
	public $Receiptmdl;
	public $Commonmdl;
	public function __construct(){
		parent::__construct();

		$this->master 			= 'voucher_master';
		$this->trans 			= 'voucher_trans';
		$this->session_expired  = ['status' => false, 'flag' => -1, 'data' => [], 'msg' => ''];
		
		$this->load->model('voucher/Commonmdl', 'model');
		$this->load->model('voucher/Paymentmdl');
		$this->load->model('voucher/Receiptmdl');
		$this->load->library('session');
		$this->load->library('db_operations');
	}
	public function get_account_balance($group, $acc_id, $party_id){
		if(!sessionExist()){
			echo json_encode($this->session_expired);
			return;
		}
		if($group == 'SUPPLIER'){
			$temp = $this->db_operations->get_record('account_master', ['acc_id' => $party_id]);
			if(empty($temp)){
				echo json_encode(['status' => true, 'flag' => 0, 'data' => [], 'msg' => 'Supplier not found']);
				return;	
			}
			$data['bal_data'] 	= $this->model->get_supplier_balance($group, $acc_id, $party_id);
			$data['trans_data'] = $this->Paymentmdl->get_trans($party_id);

			echo json_encode(['status' => true, 'flag' => 1, 'data' => $data, 'msg' => 'Data fetched successfully.']);
			return;
		}
		if($group == 'CUSTOMER'){
			$temp = $this->db_operations->get_record('account_master', ['acc_id' => $party_id]);
			if(empty($temp)){
				echo json_encode(['status' => true, 'flag' => 0, 'data' => [], 'msg' => 'Customer not found']);
				return;	
			}
			$data['bal_data'] 	= $this->model->get_customer_balance($group, $acc_id, $party_id);
			$data['trans_data'] = $this->Receiptmdl->get_trans($party_id);

			// echo "<pre>"; print_r($data); die;
			echo json_encode(['status' => true, 'flag' => 1, 'data' => $data, 'msg' => 'Data fetched successfully.']);
			return;		
		}
		echo json_encode(['status' => true, 'flag' => 2, 'data' => [], 'msg' => 'Group not define.']);
	}
	public function get_type($group){
		if(!sessionExist()){
			echo json_encode($this->session_expired);
			return;
		}
		$data = $this->db_operations->get_record('account_master', ['acc_type' => $group, 'acc_status' => true]);
		echo json_encode(['status' => true, 'flag' => 1, 'data' => $data, 'msg' => 'Data fetched successfully.']);
		return;
	}
	public function get_party($id){
		if(!sessionExist()){
			echo json_encode($this->session_expired);
			return;
		}
		$data = $this->db_operations->get_record('account_master', ['acc_id' => $id, 'acc_status1' => true]);
		echo json_encode(['status' => true, 'flag' => 1, 'data' => $data, 'msg' => 'Data fetched successfully.']);
		return;
	}
	public function get_select2_account(){
		$json = [];
		$data = $this->model->get_select2_account();
		foreach ($data as $key => $value){
			$json[] = ['id'=>$value['id'], 'text'=>$value['name']];
		}
		echo json_encode($json);
	}
}
?>
