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

class Customers extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->load_global();
		$this->load->model('customers_model','customers');
	}

	public function index()
	{
		$this->permission_check('customers_view');
		$data=$this->data;
		$data['page_title']=$this->lang->line('customers_list');
		$this->load->view('customers-view',$data);
	}
	public function add()
	{
		$this->permission_check('customers_add');
		$data=$this->data;
		$data['page_title']=$this->lang->line('customers');
		$this->load->view('customers',$data);
	}

	public function newcustomers(){
		$this->form_validation->set_rules('customer_name', 'Customer Name', 'trim|required');
		$this->form_validation->set_rules('mobile', 'Mobile', 'trim|required');
		$this->form_validation->set_rules('city', 'City', 'trim|required');
		$this->form_validation->set_rules('address', 'Address', 'trim|required');
		
		if ($this->form_validation->run() == TRUE) {
			$result=$this->customers->verify_and_save();
			echo $result;
		} else {
			echo "Please Fill Compulsory(* marked) Fields.";
		}
	}
	public function update($id){
		$this->permission_check('customers_edit');
		$data=$this->data;
		$result=$this->customers->get_details($id,$data);
		$data=array_merge($data,$result);
		$data['page_title']=$this->lang->line('customers');
		$this->load->view('customers', $data);
	}
	public function update_customers(){
		$this->form_validation->set_rules('customer_name', 'Customer Name', 'trim|required');
		
		if ($this->form_validation->run() == TRUE) {			
			$result=$this->customers->update_customers();
			echo $result;
		} else {
			echo "Please Fill Compulsory(* marked) Fields.";
		}
	}

	public function show_total_customer_paid_amount($customer_id){
		return $this->db->select("coalesce(sum(paid_amount),0) as tot")->where("customer_id",$customer_id)->get("db_sales")->row()->tot;
	}
	public function ajax_list()
	{
		$data['cus_info'] = $this->customers->get_datatables();
		// $data['sum_of_ob_paid'] = $this->db->query("select coalesce(sum(payment),0) sum_of_ob_paid from db_cobpayments where customer_id=$customers->id")->row()->sum_of_ob_paid;
		
		// $data = array();
		// $no = $_POST['start'];
		// foreach ($list as $customers) {

		// 	
		// 	$customer_balance_due = (float)$customers->opening_balance - (float)$sum_of_ob_paid;

		// 	$tot_due = (float)$customers->sales_due+(float)$customer_balance_due;

		// 	$row[] = (!empty($tot_due) && $tot_due!=0) ? app_number_format($tot_due) : app_number_format(0);
		
		// $output = array(
		// 				"draw" => $_POST['draw'],
		// 				"recordsTotal" => $this->customers->count_all(),
		// 				"recordsFiltered" => $this->customers->count_filtered(),
		// 				"data" => $data,
		// 		);
		//output to json format
		echo json_encode($data);
	}

	public function get_paid_amount()
	{
		$c_id=$this->input->post('c_id');
		$data['sum_of_ob_paid'] = $this->db->query("select coalesce(sum(payment),0) sum_of_ob_paid from db_cobpayments where customer_id=$c_id")->row()->sum_of_ob_paid;

		echo json_encode($data);
	}

	public function update_status(){
		$this->permission_check_with_msg('customers_edit');
		$id=$this->input->post('id');
		$status=$this->input->post('status');

		$result=$this->customers->update_status($id,$status);
		return $result;
	}
	
	public function delete_customers(){
		$this->permission_check_with_msg('customers_delete');
		$id=$this->input->post('q_id');
		return $this->customers->delete_customers_from_table($id);
	}
	public function multi_delete(){
		$this->permission_check_with_msg('customers_delete');
		$ids=implode (",",$_POST['checkbox']);
		return $this->customers->delete_customers_from_table($ids);
	}
	public function show_pay_now_modal(){
		$this->permission_check_with_msg('sales_payment_add');
		$customer_id=$this->input->post('customer_id');
		echo $this->customers->show_pay_now_modal($customer_id);
	}
	public function save_payment(){
		$this->permission_check_with_msg('sales_payment_add');
		echo $this->customers->save_payment();
	}
	public function show_pay_return_due_modal(){
		$this->permission_check_with_msg('sales_return_payment_add');
		$customer_id=$this->input->post('customer_id');
		echo $this->customers->show_pay_return_due_modal($customer_id);
	}
	public function save_return_due_payment(){
		$this->permission_check_with_msg('sales_payment_add');
		echo $this->customers->save_return_due_payment();
	}
	public function delete_opening_balance_entry(){
		$this->permission_check_with_msg('sales_payment_delete');
		$entry_id = $this->input->post('entry_id');
		echo $this->customers->delete_opening_balance_entry($entry_id);
	}
	
	public function credit_view($customer_id)
	{
		$data=$this->data;
		$data=array_merge($data,array('customer_id'=>$customer_id));
		$data['page_title']=$this->lang->line('Credit Summary');
		$this->load->view('credit_summary',$data);
	}
	
	public function credit_view_pdf($customer_id){
				
		$data=$this->data;
		$data['page_title']=$this->lang->line('Credit Summary');
        $data=array_merge($data,array('customer_id'=>$customer_id));
        $this->load->view('credit_summary_pdf',$data);   
		
		$q_customer_name=$this->db->query("select customer_name from db_customers where id='$customer_id';");
		$customer_name=$q_customer_name->row()->customer_name;
		
		$pageName = $customer_id.'_'.$customer_name;

        // Get output html
        $html = $this->output->get_output();
        // Load pdf library
        $this->load->library('pdf');
        
        // Load HTML content
        $this->dompdf->loadHtml($html);
        
        // (Optional) Setup the paper size and orientation
        $this->dompdf->setPaper('A4', 'portrait');/*landscape or portrait*/
        
        // Render the HTML as PDF
        $this->dompdf->render();
        
        // Output the generated PDF (1 = download and 0 = preview)
        $this->dompdf->stream("Credit Summary_$pageName", array("Attachment"=>1));
	}
}
