<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Cash extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->load_global();
		$this->load->model('country_model','country');
	}

	public function index(){
		$this->permission_check('places_view');
		$data=$this->data;
		$data['page_title']= 'Cash Summary';
		$this->load->view('country-list', $data);
	}
	public function newcountry(){
		$this->form_validation->set_rules('amount', 'Amount', 'trim|required');
		if ($this->form_validation->run() == TRUE) {
			$result=$this->country->verify_and_save();
			echo $result;
		} else {
			echo "Please Enter country name.";
		}
	}
	public function update($id){
		$this->permission_check('places_edit');
		$result=$this->country->get_details($id);
		$data=array_merge($this->data,$result);
		$data['page_title']=$this->lang->line('country');
		$this->load->view('country', $data);
	}
	public function update_country(){
		
		$this->form_validation->set_rules('country_name', 'country', 'trim|required');
		$this->form_validation->set_rules('q_id', '', 'trim|required');

		if ($this->form_validation->run() == TRUE) {
			$result=$this->country->update_country();
			echo $result;
		} else {
			echo "Please Enter country name.";
		}
	}
	public function add(){
		$this->permission_check('places_add');
		$data=$this->data;
		$data['page_title']='Cash In';
		$this->load->view('country', $data);
	}

	public function ajax_list()
	{
		$list = $this->country->get_datatables();
		
		$data = array();
		$no = $_POST['start'];
		$c=1;
		foreach ($list as $cash) {
			$no++;
			$row = array();
			$row[] = $c;
			$row[] = $cash->amount;
			$row[] = $cash->added_by;
			$row[] = $cash->added_date;
			$row[] = $cash->added_time;

			$str2='<center><a style="cursor:pointer" title="Delete Record ?" onclick="delete_country('.$cash->id.')"><i class="fa fa-fw fa-trash text-red"></i></a></center>';			
			$row[] = $str2;

			$data[] = $row;
			$c++;
		}

		$output = array(
						"draw" => $_POST['draw'],
						"recordsTotal" => $this->country->count_all(),
						"recordsFiltered" => $this->country->count_filtered(),
						"data" => $data,
				);
		//output to json format
		echo json_encode($output);
	}

	public function update_status(){
		$this->permission_check_with_msg('places_edit');
		$id=$this->input->post('id');
		$status=$this->input->post('status');
		$result=$this->country->update_status($id,$status);
		return $result;
	}
	public function delete_country(){
		$this->permission_check_with_msg('places_delete');
		$id=$this->input->post('q_id');
		$result=$this->country->delete_country($id);
		return $result;
	}

	public function cash_info_get()
	{
		$formDate=$this->input->post('from_date');
			$query_1 = $this->db->query("SELECT IFNULL(SUM(amount),0) AS open_amt FROM db_cash_in WHERE added_date='$formDate';");
			$res['open_balance_info'] = $query_1->result();
			$query_2 =$this->db->query("SELECT IFNULL(SUM(db_salespayments.payment),0) AS cash_sale FROM db_sales INNER JOIN db_salespayments ON ( db_sales.id = db_salespayments.sales_id ) WHERE db_sales.created_date = '$formDate' AND db_salespayments.payment_type = 'Cash'");
			$res['cash_sale_info'] = $query_2->result();
			$query_3 =$this->db->query("SELECT IFNULL(SUM(db_salespaymentsreturn.payment),0) AS cash_return FROM db_salespaymentsreturn WHERE db_salespaymentsreturn.created_date='$formDate' AND db_salespaymentsreturn.payment_type='Cash';");
			$res['cash_return_info'] = $query_3->result();
			$query_4 =$this->db->query("SELECT IFNULL(SUM(expense_amt),0) AS expense_amt FROM db_expense WHERE expense_date='$formDate' AND (NOT category_id='1')");
			$res['expenses_info'] = $query_4->result();
			$query_5 =$this->db->query("SELECT IFNULL(SUM(expense_amt),0) AS bank_amt FROM db_expense WHERE expense_date='$formDate' AND category_id='1'");
			$res['bank_info'] = $query_5->result();
			$query_6 =$this->db->query("SELECT IFNULL(SUM(db_salespayments.payment),0) AS cash_payment, db_sales.sales_date, db_salespayments.payment_date FROM db_sales INNER JOIN db_salespayments ON ( db_sales.id = db_salespayments.sales_id ) WHERE db_sales.sales_date <> db_salespayments.payment_date AND db_salespayments.payment_date='$formDate' AND db_salespayments.payment_type = 'Cash'");
			$res['payment_info'] = $query_6->result();
		echo json_encode($res);		
	}
}

