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

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

	public function is_sms_enabled(){
		return is_sms_enabled();
	}
	
	public function index()
	{
		$this->permission_check('sales_add');
		$data=$this->data;
		$data['page_title']='Quotation';
		$data['result'] = $this->get_hold_invoice_list();
		$data['tot_count'] = $this->get_hold_invoice_count();
		$this->load->view('quotation',$data);
	}

	public function get_json_items_details(){
		$data = array();
		$display_json = array();
		//if (!empty($_GET['name'])) {
			$name = strtolower(trim($_GET['name']));
			$sql =$this->db->query("SELECT db_items.id , db_items.item_code , db_items.item_name , db_items.sku , db_stock.color_id , db_stock.size_id , db_stock.id AS stock_id , db_stock.stock_qty FROM db_items INNER JOIN db_stock ON (db_items.id = db_stock.item_id) WHERE db_stock.stock_qty>0 AND db_items.status = 1 AND ( LOWER(item_name) LIKE '%$name%' OR LOWER(db_items.sku) LIKE '%$name%' ) LIMIT 15;");
			
			foreach ($sql->result() as $res) {
			      $json_arr["id"] = $res->id;
				  $json_arr["label"] = $res->item_name;
				  $json_arr["item_name"] = $res->item_name;
				  $json_arr["item_code"] = $res->sku;
				//   $json_arr["item_code"] = $res->item_code;
				  $json_arr["stock"] = $res->stock_qty;
				  $json_arr["stock_id"] = $res->stock_id;
				  array_push($display_json, $json_arr);
				 /* $display_json[] =$res->id;
				  $display_json[] =$res->item_name;
				  $display_json[] =$res->item_code;*/
			}
		//}
		//echo json_encode($data);exit;
		echo json_encode($display_json);exit;
	}

	public function get_json_items_details_2(){
		$data = array();
		$display_json = array();
		//if (!empty($_GET['name'])) {
			$name = strtolower(trim($_GET['name']));
			$sql =$this->db->query("SELECT db_items.id , db_items.item_code , db_items.item_name , db_items.sku , db_stock.color_id , db_stock.size_id , db_stock.id AS stock_id , db_stock.stock_qty FROM db_items INNER JOIN db_stock ON (db_items.id = db_stock.item_id) WHERE db_stock.stock_qty>0 AND db_items.status = 1 AND db_items.sku='$name' LIMIT 15;");
			
			foreach ($sql->result() as $res) {
			      $json_arr["id"] = $res->id;
				  $json_arr["label"] = $res->item_name;
				  $json_arr["item_name"] = $res->item_name;
				  $json_arr["item_code"] = $res->sku;
				//   $json_arr["item_code"] = $res->item_code;
				  $json_arr["stock"] = $res->stock_qty;
				  $json_arr["stock_id"] = $res->stock_id;
				  array_push($display_json, $json_arr);
				 /* $display_json[] =$res->id;
				  $display_json[] =$res->item_name;
				  $display_json[] =$res->item_code;*/
			}
		//}
		//echo json_encode($data);exit;
		echo json_encode($display_json);exit;
	}

	public function profit_details_get(){
		$payment_type = $this->input->post('payment_type');
		$agent_id = $this->input->post('agent_id');
		$to_date = $this->input->post('to_date');
		$from_date = $this->input->post('from_date');
		$platform_id = $this->input->post('platform_id');

		if($agent_id=="all"&&$platform_id=="all"){
			$c_query = $this->db->query("SELECT COUNT(db_sales.id) AS d_count FROM db_salesitems, db_sales  WHERE db_salesitems.sales_id = db_sales.id AND (DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type';");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT db_sales.id , db_sales.sales_code , db_sales.waybill , db_sales.sales_date , db_salesitems.id AS itm_s_id , db_salesitems.sales_qty , db_salesitems.price_per_unit , db_sales.other_charges_amt , (db_salesitems.sales_qty * db_salesitems.price_per_unit)+db_sales.other_charges_amt AS TotalInvoiceAmount , (db_salesitems.item_a_price-((db_salesitems.sales_qty * db_salesitems.price_per_unit)+db_sales.other_charges_amt)) AS AgentCommition , (db_salesitems.sales_qty * db_salesitems.price_per_unit) -(db_salesitems.sales_qty * db_salesitems.item_cost) AS AdminProfit , db_customers.customer_name , db_stock.sku , db_agent.agent FROM db_salesitems INNER JOIN db_sales ON (db_salesitems.sales_id = db_sales.id) INNER JOIN db_stock ON (db_salesitems.stock_id = db_stock.id) INNER JOIN db_agent ON (db_sales.agent = db_agent.id) INNER JOIN db_customers ON (db_sales.customer_id = db_customers.id) WHERE (DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type';");
			$res['itm_info'] = $query->result();
		}else if($agent_id!="all"&&$platform_id=="all"){
			$c_query = $this->db->query("SELECT COUNT(db_sales.id) AS d_count FROM db_salesitems, db_sales  WHERE db_salesitems.sales_id = db_sales.id AND (DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.agent='$agent_id';");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT db_sales.id , db_sales.sales_code , db_sales.waybill , db_sales.sales_date , db_salesitems.id AS itm_s_id , db_salesitems.sales_qty , db_salesitems.price_per_unit , db_sales.other_charges_amt , (db_salesitems.sales_qty * db_salesitems.price_per_unit)+db_sales.other_charges_amt AS TotalInvoiceAmount , (db_salesitems.item_a_price-((db_salesitems.sales_qty * db_salesitems.price_per_unit)+db_sales.other_charges_amt)) AS AgentCommition , (db_salesitems.sales_qty * db_salesitems.price_per_unit) -(db_salesitems.sales_qty * db_salesitems.item_cost) AS AdminProfit , db_customers.customer_name , db_stock.sku , db_agent.agent FROM db_salesitems INNER JOIN db_sales ON (db_salesitems.sales_id = db_sales.id) INNER JOIN db_stock ON (db_salesitems.stock_id = db_stock.id) INNER JOIN db_agent ON (db_sales.agent = db_agent.id) INNER JOIN db_customers ON (db_sales.customer_id = db_customers.id) WHERE (DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.agent='$agent_id';");
			$res['itm_info'] = $query->result();
		}else if($agent_id=="all"&&$platform_id!="all"){
			$c_query = $this->db->query("SELECT COUNT(db_sales.id) AS d_count FROM db_salesitems, db_sales  WHERE db_salesitems.sales_id = db_sales.id AND (DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.platform='$platform_id';");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT db_sales.id , db_sales.sales_code , db_sales.waybill , db_sales.sales_date , db_salesitems.id AS itm_s_id , db_salesitems.sales_qty , db_salesitems.price_per_unit , db_sales.other_charges_amt , (db_salesitems.sales_qty * db_salesitems.price_per_unit)+db_sales.other_charges_amt AS TotalInvoiceAmount , (db_salesitems.item_a_price-((db_salesitems.sales_qty * db_salesitems.price_per_unit)+db_sales.other_charges_amt)) AS AgentCommition , (db_salesitems.sales_qty * db_salesitems.price_per_unit) -(db_salesitems.sales_qty * db_salesitems.item_cost) AS AdminProfit , db_customers.customer_name , db_stock.sku , db_agent.agent FROM db_salesitems INNER JOIN db_sales ON (db_salesitems.sales_id = db_sales.id) INNER JOIN db_stock ON (db_salesitems.stock_id = db_stock.id) INNER JOIN db_agent ON (db_sales.agent = db_agent.id) INNER JOIN db_customers ON (db_sales.customer_id = db_customers.id) WHERE (DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.platform='$platform_id';");
			$res['itm_info'] = $query->result();
		}else if($agent_id!="all"&&$platform_id!="all"){
			$c_query = $this->db->query("SELECT COUNT(db_sales.id) AS d_count FROM db_salesitems, db_sales  WHERE db_salesitems.sales_id = db_sales.id AND (DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.agent='$agent_id' AND db_sales.platform='$platform_id';");
			$res['data_count_info'] = $c_query->result();
			$query=$this->db->query("SELECT db_sales.id , db_sales.sales_code , db_sales.waybill , db_sales.sales_date , db_salesitems.id AS itm_s_id , db_salesitems.sales_qty , db_salesitems.price_per_unit , db_sales.other_charges_amt , (db_salesitems.sales_qty * db_salesitems.price_per_unit)+db_sales.other_charges_amt AS TotalInvoiceAmount , (db_salesitems.item_a_price-((db_salesitems.sales_qty * db_salesitems.price_per_unit)+db_sales.other_charges_amt)) AS AgentCommition , (db_salesitems.sales_qty * db_salesitems.price_per_unit) -(db_salesitems.sales_qty * db_salesitems.item_cost) AS AdminProfit , db_customers.customer_name , db_stock.sku , db_agent.agent FROM db_salesitems INNER JOIN db_sales ON (db_salesitems.sales_id = db_sales.id) INNER JOIN db_stock ON (db_salesitems.stock_id = db_stock.id) INNER JOIN db_agent ON (db_sales.agent = db_agent.id) INNER JOIN db_customers ON (db_sales.customer_id = db_customers.id) WHERE (DATE(db_sales.sales_date) BETWEEN '$from_date' AND '$to_date') AND db_sales.payment_status='$payment_type' AND db_sales.agent='$agent_id' AND db_sales.platform='$platform_id';");
			$res['itm_info'] = $query->result();
		}

		
		echo json_encode($res);
	}

	public function save_pos_itm(){
		$data=$this->input->post('data_table');
		$stock_data=$this->input->post('stock_table');		
		$query=$this->db->query("SELECT MAX(id) AS max_ID FROM db_sales");
		$sale_id=$query->row()->max_ID;
		//print_r($sale_id);
        $result = $this->pos_model->pos_itm_list_save($data,$sale_id,$stock_data);
		$result =$result."<<<###>>>".$this->pos_model->details_get();
		$result =$result."<<<###>>>".$this->pos_model->hold_invoice_list();
		$q1=$this->db->query("SELECT * FROM db_hold");
		$data['tot_count']=$q1->num_rows();
		$result =$result."<<<###>>>".$q1->num_rows();
	    echo json_encode($result);
	}

	public function cod_save_pos_itm(){
		$data=$this->input->post('data_table');
		$stock_data=$this->input->post('stock_table');		
		$query=$this->db->query("SELECT MAX(id) AS max_ID FROM db_sales");
		$sale_id=$query->row()->max_ID;
		//print_r($sale_id);
        $result = $this->pos_model->cod_pos_itm_list_save($data,$sale_id,$stock_data);
		$result =$result."<<<###>>>".$this->pos_model->details_get();
		$result =$result."<<<###>>>".$this->pos_model->hold_invoice_list();
		$q1=$this->db->query("SELECT * FROM db_hold");
		$data['tot_count']=$q1->num_rows();
		$result =$result."<<<###>>>".$q1->num_rows();
	    echo json_encode($result);
	}

	//adding new item from Modal
	public function newcustomer(){
	
		$this->form_validation->set_rules('customer_name', 'Customer Name', 'trim|required');
		
		if ($this->form_validation->run() == TRUE) {
			$this->load->model('customers_model');
			$result=$this->customers_model->verify_and_save();
			//fetch latest item details
			$res=array();
			$query=$this->db->query("select id,customer_name from db_customers order by id desc limit 1");
			$res['id']=$query->row()->id;
			$res['customer_name']=$query->row()->customer_name;
			$res['result']=$result;
			
			echo json_encode($res);

		} 
		else {
			echo "Please Fill Compulsory(* marked) Fields.";
		}
	}

	public function get_details(){
		echo $this->pos_model->get_details();
	}
	public function receive_order(){
	    echo $this->pos_model->receive_order();
	}
	public function pos_save_update(){
	    $result='';
	    if($this->input->post('command')=='update'){//Update
	    	$result = $this->pos_model->pos_save_update();
	    }
	    else{//Save
	    	$result = $this->pos_model->pos_save_update();
	    }
	    echo json_encode($result);exit();
	}

	public function cod_pos_save_update(){
	    $result='';
	    if($this->input->post('command')=='update'){//Update
	    	$result = $this->pos_model->cod_pos_save_update();
	    }
	    else{//Save
	    	$result = $this->pos_model->cod_pos_save_update();
	    }
	    echo json_encode($result);exit();
	}



	public function edit($sales_id){
		$this->permission_check('sales_edit');
	    $data=$this->data;
	    $data['sales_id']=$sales_id;
	    $data['page_title']='POS Update';
	    $data['result'] = $this->get_hold_invoice_list();
		$data['tot_count'] = $this->get_hold_invoice_count();
		$this->load->view('pos',$data);
	}
	public function fetch_sales($sales_id){
	    $result=$this->pos_model->edit_pos($sales_id);
	}
	/* ######################################## HOLD INVOICE ############################# */
	public function hold_invoice(){
	    echo $this->pos_model->hold_list_save_update();
	}
	public function hold_invoice_list(){
		$data =array();
		$data['result'] = $this->get_hold_invoice_list();
		$data['tot_count'] = $this->get_hold_invoice_count();
		echo json_encode($data);
	}

	public function get_hold_invoice_list(){
		$data =array();
		$result= $this->pos_model->hold_invoice_list();
		return $result;
	}
	public function get_hold_invoice_count(){
		$q1=$this->db->query("SELECT * FROM db_hold");
		return $q1->num_rows();
	}

	public function hold_invoice_delete($invoice_id){
		$result=$this->pos_model->hold_invoice_delete($invoice_id);
		echo trim($result);
	}
	public function hold_invoice_edit(){
		echo $this->pos_model->hold_invoice_edit();
	}
	public function add_payment_row(){
		return $this->load->view('modals_pos_payment/modal_payments_multi_sub');
	}
	//Print sales POS 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['page_title']="";
		$data=array_merge($data,array('sales_id'=>$sales_id));
		$this->load->view('quo-invoice',$data);
	}

	// public function update_salePrice2(){
	// 	$sql =$this->db->query("SELECT * FROM db_salesitems");
	// 	$c=0;
			
	// 	foreach ($sql->result() as $res) {
	// 		$salesitems_ID = $res->id;
	// 		$stock_id = $res->stock_id;
	// 		$sku = $res->sku;

	// 		$sql_2 =$this->db->query("SELECT id, item_id, sku, unit_sales_price_01, unit_sales_price_02, unit_sales_price_03 FROM db_stock WHERE db_stock.id = '$stock_id';");
	// 		foreach ($sql_2->result() as $res_2) {
	// 			$unit_sales_price_02 = $res_2->unit_sales_price_02;

	// 			$u_data = array(
	// 				'sale_price_2' => $unit_sales_price_02
	// 			);
			
	// 			$this->db->where('id', $salesitems_ID);
	// 			$this->db->update('db_salesitems', $u_data);
				
	// 		}
	// 		$c++;
	// 	}
	// 	print_r($c);
	// }

	public function update_saleFinal(){
		$sql =$this->db->query("SELECT * FROM db_sales");
		$c=0;
			
		foreach ($sql->result() as $res) {
			$sales_ID = $res->id;
			$grand_total = $res->grand_total;
			$paid_amount = $res->paid_amount;

			if($grand_total==$paid_amount){
				$u_data = array(
					'sales_status' => 'Final'
				);
			}else{
				$u_data = array(
					'sales_status' => 'Pending'
				);
			}

			$this->db->where('id', $sales_ID);
			$this->db->update('db_sales', $u_data);
				
			$c++;
		}
		print_r($c);
	}

	public function update_round_off(){
		$sql =$this->db->query("SELECT * FROM db_sales");
		$c=0;
		foreach ($sql->result() as $res) {
			$sales_ID = $res->id;
			$grand_total = $res->grand_total;
			$paid_amount = $res->paid_amount;

			if($grand_total==$paid_amount){
				$u_data = array(
					'sales_status' => 'Final'
				);
			}else{
				$u_data = array(
					'sales_status' => 'Pending'
				);
			}

			$this->db->where('id', $sales_ID);
			$this->db->update('db_sales', $u_data);
				
			$c++;
		}
		print_r($c);
		
	}

}
