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

class Payment_types extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->load_global();
		$this->load->model('payment_types_model','payment_types');
	}

	public function add(){
		$this->permission_check('payment_types_add');
		$data=$this->data;
		$data['page_title']=$this->lang->line('payment_types');
		$this->load->view('payment_types', $data);
	}
	public function new_payment_type(){

		$this->form_validation->set_rules('payment_type_name', 'Payment Type Name', 'trim|required');
		
		if ($this->form_validation->run() == TRUE) {
			
			$result=$this->payment_types->verify_and_save();
			echo $result;
		} else {
			echo "Please Enter Payment Type Name.";
		}
	}
	public function update($id){
		$this->permission_check('payment_types_edit');
		$data=$this->data;
		$result=$this->payment_types->get_details($id,$data);
		$data=array_merge($data,$result);
		$data['page_title']=$this->lang->line('payment_types');
		$this->load->view('payment_types', $data);
	}
	public function update_payment_type(){
		$this->form_validation->set_rules('payment_type_name', 'Payment Type Name', 'trim|required');
		$this->form_validation->set_rules('q_id', '', 'trim|required');

		if ($this->form_validation->run() == TRUE) {
			$result=$this->payment_types->update_payment_type();
			echo $result;
		} else {
			echo "Please Enter Payment Type Name.";
		}
	}
	public function index(){
		$this->permission_check('payment_types_view');
		$data=$this->data;
		$data['page_title']=$this->lang->line('payment_types_list');
		$this->load->view('payment_types_list', $data);
	}

	public function ajax_list()
	{
		$list = $this->payment_types->get_datatables();
		
		$data = array();
		$no = $_POST['start'];
		foreach ($list as $payment_type) {
			$no++;
			$row = array();
			$row[] = $payment_type->payment_type;
			
			 		if($payment_type->status==1){ 
			 			$str= "<span onclick='update_status(".$payment_type->id.",0)' id='span_".$payment_type->id."'  class='label label-success' style='cursor:pointer'>Active </span>";}
					else{ 
						$str = "<span onclick='update_status(".$payment_type->id.",1)' id='span_".$payment_type->id."'  class='label label-danger' style='cursor:pointer'> Inactive </span>";
					}
			$row[] = $str;			
			         $str2 = '<div class="btn-group" title="View Account">
										<a class="btn btn-primary btn-o dropdown-toggle" data-toggle="dropdown" href="#">
											Action <span class="caret"></span>
										</a>
										<ul role="menu" class="dropdown-menu dropdown-light pull-right">';

											if($this->permissions('payment_types_edit'))
											$str2.='<li>
												<a title="Editd Record ?" href="'.base_url('payment_types/update/'.$payment_type->id).'">
													<i class="fa fa-fw fa-edit text-blue"></i>Edit
												</a>
											</li>';

											if($this->permissions('payment_types_delete'))
											$str2.='<li>
												<a style="cursor:pointer" title="Delete Record ?" onclick="delete_payment_type('.$payment_type->id.')">
													<i class="fa fa-fw fa-trash text-red"></i>Delete
												</a>
											</li>
											
										</ul>
									</div>';			
			$row[] = $str2;

			$data[] = $row;
		}

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

	public function update_status(){
		$this->permission_check_with_msg('payment_types_edit');
		$id=$this->input->post('id');
		$status=$this->input->post('status');
		$result=$this->payment_types->update_status($id,$status);
		return $result;
	}
	public function delete_payment_type(){
		$this->permission_check_with_msg('payment_types_delete');
		$id=$this->input->post('q_id');
		$result=$this->payment_types->delete_payment_type($id);
		return $result;
	}

	public function history(){
		$this->permission_check('cheque_history');
		$data=$this->data;
		$data['page_title']='Cheque History';
		$this->load->view('cheque-history', $data);
	}

	public function search(){
		$this->permission_check('cheque_search');
		$data=$this->data;
		$data['page_title']='Search Cheque No.';
		$this->load->view('cheque-search', $data);
	}
	
	public function cheque_info_get()
	{
		$formDate=$this->input->post('formDate1');
		$toDate=$this->input->post('toDate1');
		$customer_id = $this->input->post('customer_id');

		$formDate2=$this->input->post('formDate2');
		$toDate2=$this->input->post('toDate2');
		$supplier_id = $this->input->post('supplier_id');
		
		if($customer_id=="all"){
			$c_query = $this->db->query("SELECT COUNT(cheque_details.id) AS d_count FROM cheque_details WHERE (DATE(cheque_details.cheque_received_date) BETWEEN '$formDate' AND '$toDate') AND cheque_details.customer_id IS NOT NULL");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT cheque_details.*,db_customers.customer_name, db_sales.sales_code FROM cheque_details INNER JOIN db_customers ON  cheque_details.customer_id=db_customers.id LEFT JOIN db_sales ON cheque_details.inv_on = db_sales.id WHERE (DATE(cheque_details.cheque_received_date) BETWEEN '$formDate' AND '$toDate') AND cheque_details.customer_id IS NOT NULL ORDER BY cheque_details.cheque_received_date DESC");
			$res['chq_info'] = $query->result();
		}else{
			$c_query = $this->db->query("SELECT COUNT(cheque_details.id) AS d_count FROM cheque_details WHERE (DATE(cheque_details.cheque_received_date) BETWEEN '$formDate' AND '$toDate') AND cheque_details.customer_id='$customer_id'");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT cheque_details.*,db_customers.customer_name, db_sales.sales_code FROM cheque_details INNER JOIN db_customers ON  cheque_details.customer_id=db_customers.id LEFT JOIN db_sales ON cheque_details.inv_on = db_sales.id WHERE (DATE(cheque_details.cheque_received_date) BETWEEN '$formDate' AND '$toDate') AND cheque_details.customer_id='$customer_id' ORDER BY cheque_details.cheque_received_date DESC");
			$res['chq_info'] = $query->result();
		}

		if($supplier_id=="all"){
			$c_query = $this->db->query("SELECT COUNT(cheque_details.id) AS d_count FROM cheque_details WHERE (DATE(cheque_details.cheque_received_date) BETWEEN '$formDate2' AND '$toDate2') AND cheque_details.supplier_id IS NOT NULL");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT cheque_details.*,db_suppliers.supplier_name, db_purchase.purchase_code FROM cheque_details INNER JOIN db_suppliers ON  cheque_details.supplier_id=db_suppliers.id LEFT JOIN db_purchase ON cheque_details.grn_id = db_purchase.id WHERE (DATE(cheque_details.cheque_received_date) BETWEEN '$formDate2' AND '$toDate2') AND cheque_details.supplier_id IS NOT NULL ORDER BY cheque_details.cheque_received_date DESC");
			$res['sup_chq_info'] = $query->result();
		}else{
			$c_query = $this->db->query("SELECT COUNT(cheque_details.id) AS d_count FROM cheque_details WHERE (DATE(cheque_details.cheque_received_date) BETWEEN '$formDate2' AND '$toDate2') AND cheque_details.supplier_id='$supplier_id'");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT cheque_details.*,db_suppliers.supplier_name, db_purchase.purchase_code FROM cheque_details INNER JOIN db_suppliers ON  cheque_details.supplier_id=db_suppliers.id LEFT JOIN db_purchase ON cheque_details.grn_id = db_purchase.id WHERE (DATE(cheque_details.cheque_received_date) BETWEEN '$formDate2' AND '$toDate2') AND cheque_details.supplier_id='$supplier_id' ORDER BY cheque_details.cheque_received_date DESC");
			$res['sup_chq_info'] = $query->result();
		}

		echo json_encode($res);
	}

	public function sale_chq_info()
	{
		$p_id=$this->input->post('id');

		$query=$this->db->query("SELECT * FROM cheque_details WHERE payment_id='$p_id' AND (cheque_type='inv' OR cheque_type='cus_pay')");
		$res['chq_info'] = $query->result();

		echo json_encode($res);
	}

	public function return_cheque(){
		$chqID =$this->input->post('chqID');
		$return_reason =$this->input->post('return_reason');

		$payment_id =$this->db->query("SELECT payment_id FROM cheque_details WHERE id='$chqID'")->row()->payment_id;
		$cheque_type =$this->db->query("SELECT cheque_type FROM cheque_details WHERE id='$chqID'")->row()->cheque_type;

		// if($cheque_type=="inv"){
		// 	$this->sales->delete_payment_2($payment_id);
		// }else if($cheque_type=="cus_pay"){
		// 	$this->sales->delete_payment_2($payment_id);
		// }else if($cheque_type=="sup_pay"){
		// 	$this->db->query("delete from db_purchasepayments where id='$payment_id'");
		// }

		$u_data = array(
			'return_date' =>date('Y-m-d'),
  			'return_reason' =>$return_reason,
			'status' => 'RETURN'
		);
		
		$this->db->where('id', $chqID);
		$result=$this->db->update('cheque_details', $u_data);

		if ($result) {
            $output="success";
        }else{
            $output="failed";
        }
		echo json_encode($output);
	}

	public function active_cheque(){
		$chqID =$this->input->post('select_id');

		$u_data = array(
			'return_date' =>'',
  			'return_reason' =>'',
			'status' => 'OK'
		);
		
		$this->db->where('id', $chqID);
		$result=$this->db->update('cheque_details', $u_data);

		if ($result) {
            $output="success";
        }else{
            $output="failed";
        }
		echo json_encode($output);
	}
}

