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

class Quotation_s extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->load_global();
		$this->load->model('quotation_s_model','sales');
		$this->load->helper('sms_template_helper');
	}

	public function is_sms_enabled(){
		return is_sms_enabled();
	}

	public function index()
	{
		$this->permission_check('sales_view');
		$data=$this->data;
		$data['page_title']="Quotation List";
		$this->load->view('quotation_list',$data);
	}
	public function add()
	{	
		$this->permission_check('sales_add');
		$data=$this->data;
		$data['page_title']=$this->lang->line('sales');
		$this->load->view('sales',$data);
	}
	

	public function sales_save_and_update(){
		$this->form_validation->set_rules('sales_date', 'Sales Date', 'trim|required');
		$this->form_validation->set_rules('customer_id', 'Customer Name', 'trim|required');
		
		if ($this->form_validation->run() == TRUE) {
	    	$result = $this->sales->verify_save_and_update();
	    	echo $result;
		} else {
			echo "Please Fill Compulsory(* marked) Fields.";
		}
	}
	
	
	public function update($id){
		$this->permission_check('sales_edit');
		$data=$this->data;
		$data=array_merge($data,array('sales_id'=>$id));
		$data['page_title']=$this->lang->line('sales');
		$this->load->view('sales', $data);
	}
	

	public function ajax_list()
	{
		$formDate=$this->input->post('from_date');
		$toDate=$this->input->post('to_date');

		// $formDate='2022-10-27';
		// $toDate= '2022-10-28';

		$list = $this->sales->get_datatables($formDate,$toDate);
		
		$data = array();
		$no = $_POST['start'];
		foreach ($list as $sales) {
			
			$no++;
			$row = array();
			$row[] = '<input type="checkbox" name="checkbox[]" value='.$sales->id.' class="checkbox column_checkbox" >';
			$row[] = show_date($sales->sales_date);

			$info = (!empty($sales->return_bit)) ? "\n<span class='label label-danger' style='cursor:pointer'><i class='fa fa-fw fa-undo'></i>Return Raised</span>" : '';

			$row[] = $sales->sales_code.$info;
			$row[] = $sales->sales_status;
			$row[] = $sales->reference_no;
			$row[] = $sales->customer_name;
			//$row[] = $sales->warehouse_name;
			$row[] = app_number_format($sales->grand_total);
			$row[] = app_number_format($sales->paid_amount);
			$row[] = app_number_format($sales->sales_due);
					$str='';
					if($sales->payment_status=='Unpaid')
			          $str= "<span class='label label-danger' style='cursor:pointer'>Unpaid </span>";
			        if($sales->payment_status=='Partial')
			          $str="<span class='label label-warning' style='cursor:pointer'> Partial </span>";
			        if($sales->payment_status=='Paid')
			          $str="<span class='label label-success' style='cursor:pointer'> Paid </span>";

			$row[] = $str;
			$row[] = ucfirst($sales->created_by);

					 if($sales->pos ==1):
					 	$str1='pos/edit/';
					 else:
					 	$str1='sales/update/';
					 endif;

					$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('sales_view'))
											$str2.='<li>
												<a title="View Invoice" href="sales/invoice/'.$sales->id.'" >
													<i class="fa fa-fw fa-eye text-blue"></i>View sales
												</a>
											</li>';
											if($this->session->userdata('role_id')=='1'||$this->session->userdata('role_id')=='2')
												if($this->permissions('sales_edit'))
												$str2.='<li>
													<a title="Update Record ?" href="'.$str1.$sales->id.'">
														<i class="fa fa-fw fa-edit text-blue"></i>Edit
													</a>
												</li>';
											
											
											if($this->permissions('sales_payment_view'))
											$str2.='<li>
												<a title="Pay" class="pointer" onclick="view_payments('.$sales->id.')" >
													<i class="fa fa-fw fa-money text-blue"></i>View Payments
												</a>
											</li>';

											if($this->permissions('sales_payment_add'))
											$str2.='<li>
												<a title="Pay" class="pointer" onclick="pay_now('.$sales->id.')" >
													<i class="fa fa-fw fa-hourglass-half text-blue"></i>Payment Receive
												</a>
											</li>';
											
											if($this->session->userdata('role_id')=='1'||$this->session->userdata('role_id')=='2')
											if($this->permissions('sales_add') || $this->permissions('sales_edit'))
											$str2.='<li>
												<a title="Update Record ?" target="_blank" href="sales/print_invoice/'.$sales->id.'">
													<i class="fa fa-fw fa-print text-blue"></i>Print
												</a>
											</li>
											
											<li>
												<a title="Update Record ?" target="_blank" href="sales/pdf/'.$sales->id.'">
													<i class="fa fa-fw fa-file-pdf-o text-blue"></i>PDF
												</a>
											</li>
											<li>
												<a style="cursor:pointer" title="Print POS Invoice ?" onclick="print_invoice('.$sales->id.')">
													<i class="fa fa-fw fa-file-text text-blue"></i>POS Invoice
												</a>
											</li>';

											if($this->permissions('sales_return'))
											$str2.='<li>
												<a title="Sales Return" href="sales_return/add/'.$sales->id.'">
													<i class="fa fa-fw fa-undo text-blue"></i>Sales Return
												</a>
											</li>';

											if($this->session->userdata('role_id')=='1'||$this->session->userdata('role_id')=='2')
											if($this->permissions('sales_delete'))
											$str2.='<li>
												<a style="cursor:pointer" title="Delete Record ?" onclick="delete_sales(\''.$sales->id.'\')">
													<i class="fa fa-fw fa-trash text-red"></i>Cancel
												</a>
											</li>
											
										</ul>
									</div>';			

			$row[] = $str2;

			$data[] = $row;
		}

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

	public function invoice_info_get()
	{
		$formDate=$this->input->post('from_date');
		$toDate=$this->input->post('to_date');

			$c_query = $this->db->query("SELECT COUNT(db_quotation.id) AS d_count FROM db_quotation WHERE (DATE(db_quotation.sales_date) BETWEEN '$formDate' AND '$toDate')");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT db_quotation.id , db_quotation.sales_code , db_quotation.sales_date , db_quotation.reference_no, db_quotation.sales_status, db_quotation.created_time , db_quotation.grand_total , db_quotation.paid_amount ,db_quotation.pos , db_quotation.return_bit , db_customers.customer_name, db_quotation.payment_status , db_quotation.created_by FROM db_quotation LEFT JOIN db_customers ON (db_quotation.customer_id = db_customers.id) WHERE (DATE(db_quotation.sales_date) BETWEEN '$formDate' AND '$toDate') ORDER BY db_quotation.sales_date DESC, db_quotation.sales_code DESC");
			$res['inv_info'] = $query->result();
				
		echo json_encode($res);
				
	}

	public function invoice_info_get_2()
	{
		$formDate=$this->input->post('from_date');
		$toDate=$this->input->post('to_date');

			$c_query = $this->db->query("SELECT COUNT(db_sales.id) AS d_count FROM db_sales WHERE (DATE(db_sales.sales_date) BETWEEN '$formDate' AND '$toDate')");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT db_sales.id , db_sales.sales_code , db_sales.sales_date , db_sales.reference_no, db_sales.sales_status, db_sales.created_time , db_sales.grand_total , db_sales.paid_amount ,db_sales.pos , db_sales.return_bit , db_customers.customer_name, db_sales.payment_status , db_sales.created_by FROM db_sales LEFT JOIN db_customers ON (db_sales.customer_id = db_customers.id) WHERE (DATE(db_sales.sales_date) BETWEEN '$formDate' AND '$toDate') ORDER BY db_sales.sales_date DESC, db_sales.sales_code DESC");
			$res['inv_info'] = $query->result();

			$res['total_invoice']=$this->db->query("SELECT COUNT(*) as total FROM db_sales WHERE db_sales.created_date=CURRENT_DATE();")->row()->total;
			/*Total Invoices Total*/
			$res['sal_total']=$this->db->query("SELECT FORMAT(IFNULL(SUM(grand_total),0.00),2) AS tot_sal_grand_total FROM db_sales WHERE db_sales.created_date=CURRENT_DATE();")->row()->tot_sal_grand_total;

			/*PAID AMOUNT*/
			$res['tot_received_amt']=$this->db->select("FORMAT(IFNULL(SUM(paid_amount),0.00),2) AS paid_amount")->from("db_sales")->where("db_sales.created_date=CURRENT_DATE();")->get()->row()->paid_amount;

			$res['sales_due_total']=$this->db->query("SELECT FORMAT(IFNULL(SUM(sales_due),0.00),2) AS sales_due FROM db_customers")->row()->sales_due;
			
			$res['sal_return']=$this->db->query("SELECT FORMAT(IFNULL(SUM(grand_total),0.00),2) AS tot_sal_grand_total FROM db_salesreturn WHERE db_salesreturn.created_date=CURRENT_DATE();")->row()->tot_sal_grand_total;

			$date = new DateTime("now", new DateTimeZone('Asia/Colombo') ); 
            $today_date= $date->format('Y-m-d');
            $year = $date->format('Y');
            $month= $date->format('m');
            $s_text = $year.'-'.$month;

			$res['month_sal_total']=$this->db->query("SELECT FORMAT(IFNULL(SUM(grand_total),0.00),2) AS tot_sal_grand_total FROM db_sales WHERE db_sales.created_date LIKE '$s_text%';")->row()->tot_sal_grand_total;

			$res['month_tot_received_amt']=$this->db->select("FORMAT(IFNULL(SUM(paid_amount),0.00),2) AS paid_amount")->from("db_sales")->where("db_sales.created_date LIKE '$s_text%';")->get()->row()->paid_amount;

			$res['month_sal_return']=$this->db->query("SELECT FORMAT(IFNULL(SUM(grand_total),0.00),2) AS tot_sal_grand_total FROM db_salesreturn WHERE db_salesreturn.created_date LIKE '$s_text%';")->row()->tot_sal_grand_total;

		echo json_encode($res);
				
	}

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

		
		$result=$this->sales->update_status($id,$status);
		return $result;
	}
	public function delete_sales(){
		$this->permission_check_with_msg('sales_delete');
		$id=$this->input->post('q_id');
		echo $this->sales->delete_sales($id);
	}
	public function multi_delete(){
		$this->permission_check_with_msg('sales_delete');
		$ids=implode (",",$_POST['checkbox']);
		echo $this->sales->delete_sales($ids);
	}


	//Table ajax code
	public function search_item(){
		$q=$this->input->get('q');
		$result=$this->sales->search_item($q);
		echo $result;
	}
	public function find_item_details(){
		$id=$this->input->post('id');
		
		$result=$this->sales->find_item_details($id);
		echo $result;
	}

	//sales invoice form
	public function invoice($id)
	{	
		if(!$this->permissions('sales_add') && !$this->permissions('sales_edit')){
			$this->show_access_denied_page();
		}
		$data=$this->data;
		$data=array_merge($data,array('sales_id'=>$id));
		$data['page_title']="Quotation";
		$this->load->view('quo_inv_view',$data);
	}
	
	//Print sales invoice 
	public function print_quotation($sales_id)
	{
		if(!$this->permissions('sales_add') && !$this->permissions('sales_edit')){
			$this->show_access_denied_page();
		}
		$data=$this->data;
		$data=array_merge($data,array('sales_id'=>$sales_id));
		$data['page_title']='Quotation';

			$this->load->view('print_quo',$data);
	}

	//Print sales POS invoice 
	public function print_invoice_pos($sales_id)
	{
		if(!$this->permissions('sales_add') && !$this->permissions('sales_edit')){
			$this->show_access_denied_page();
		}
		$data=$this->data;
		$data=array_merge($data,array('sales_id'=>$sales_id));
		$data['page_title']=$this->lang->line('sales_invoice');
		$this->load->view('sal-invoice-pos',$data);
	}
	
	
	public function pdf($sales_id){
		if(!$this->permissions('sales_add') && !$this->permissions('sales_edit')){
			$this->show_access_denied_page();
		}
		
		$data=$this->data;
		$data['page_title']=$this->lang->line('sales_invoice');
        $data=array_merge($data,array('sales_id'=>$sales_id));
        if(get_invoice_format_id()==3){
			$this->load->view('print_quo',$data);
		}
		else if(get_invoice_format_id()==2){
			$this->load->view('print_quo',$data);
		}
		else{
			$this->load->view('print_quo',$data);
		}
       

        // 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("Quotation_$sales_id", array("Attachment"=>1));
	}

	
	/*v1.1*/
	public function return_row_with_data($rowcount,$item_id){
		echo $this->sales->get_items_info($rowcount,$item_id);
	}
	public function return_sales_list($sales_id){
		echo $this->sales->return_sales_list($sales_id);
	}
	public function delete_payment(){
		$this->permission_check_with_msg('sales_payment_delete');
		$payment_id = $this->input->post('payment_id');
		echo $this->sales->delete_payment($payment_id);
	}
	public function show_pay_now_modal(){
		$this->permission_check_with_msg('sales_view');
		$sales_id=$this->input->post('sales_id');
		echo $this->sales->show_pay_now_modal($sales_id);
	}
	public function save_payment(){
		$this->permission_check_with_msg('sales_add');
		echo $this->sales->save_payment();
	}
	public function view_payments_modal(){
		$this->permission_check_with_msg('sales_view');
		$sales_id=$this->input->post('sales_id');
		echo $this->sales->view_payments_modal($sales_id);
	}
}
