<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

//getOrderUsedRewardPoint

class Orders extends CI_Model {
	public function __construct() {
		parent::__construct();
		$this->load->library('User_auth');
		$this->user_auth->check_customer_auth();
	}

	//Order List
	public function order_list() {
		$customer_id = $this->session->userdata('customer_id');
		$this->db->select('a.*,b.customer_name');
		$this->db->from('order a');
		$this->db->join('customer_information b', 'b.customer_id = a.customer_id');
		$this->db->where('a.delete_time', NULL); //added
		$this->db->where('b.customer_id', $customer_id);
		$this->db->order_by('a.order', 'desc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;
	}

	//added Order details
	public function get_order($order_id) {
		$customer_id = $this->session->userdata('customer_id');
		$this->db->select('a.*,b.customer_name');
		$this->db->from('order a');
		$this->db->join('customer_information b', 'b.customer_id = a.customer_id');
		$this->db->where('b.customer_id', $customer_id);
		$this->db->where('a.order_id', $order_id);
		$this->db->order_by('a.order', 'desc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;

	}
	public function get_order_by_orderid($order_id) {
		$customer_id = $this->session->userdata('customer_id');
		$this->db->select('a.*');
		$this->db->from('order a');
		$this->db->where('a.customer_id', $customer_id);
		$this->db->where('a.order_id', $order_id);
		$this->db->order_by('a.order', 'desc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $result = $query->row();
		}
		return false;

	}
	//added get order by orderid without restrict customer id
	//for customer fun dashboard
	public function get_order_by_orderid_nolimit_custid($order_id) {
		$this->db->select('a.*,b.customer_name');
		$this->db->from('order a');
		$this->db->join('customer_information b', 'b.customer_id = a.customer_id');
		$this->db->where('a.order_id', $order_id);
		$this->db->order_by('a.order', 'desc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;

	}
	//end added get order by  orderid without restrict customer id
	public function get_order_productlist($order_id) {
		$this->db->select('a.*,b.product_name,b.product_name_ch,b.product_name_in,c.variant_name');
		$this->db->from('order_details a');
		$this->db->join('product_information b', 'a.product_id = b.product_id');
		$this->db->join('variant c', 'a.variant_id = c.variant_id');
		$this->db->where('a.order_id', $order_id);
		$this->db->order_by('a.order_details_id', 'asc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;
	} //public function get_order_productlist($order_id){
	public function get_order_paymentmethodWithOutPoint($order_id) {
		$this->db->select('a.*');
		$this->db->from('order_payment a');
		$this->db->where('a.order_id', $order_id);
		$query = $this->db->get();
		$result = "";
		if ($query->num_rows() > 0) {
			// return $query->result_array();
			$result = $query->row();
		}
		$paymentStatus = "";
		// var_dump($result->payment_id);
		//main payment method
		if (!empty($result) && $result->payment_id == 1) {
			$paymentStatus = $paymentStatus . display('cash_on_delivery');
			// $paymentStatus = $paymentStatus . $this->session->userdata('language');

		}
		if (!empty($result) && $result->payment_id != 1) {
			$this->db->select('agent');
			$this->db->from('payment_gateway');
			$this->db->where('used_id', $result->payment_id);
			$query = $this->db->get();
			$result2 = "";
			if ($query->num_rows() > 0) {
				// return $query->result_array();
				$result2 = $query->row();
				$paymentStatus = $paymentStatus . $result2->agent;
			}
		}

		return $paymentStatus;
	} //public function get_order_paymentmethodWithOutPoint($order_id) {

	public function get_order_paymentmethod($order_id) {
		//get order payment status , coupon status ,point status
		//if payment_id =1 , cash on delivery
		//if payment_id != 1 , find payment name from payment gateway

		//other than payment method,
		//check use coupon in order table, if coupon not empty, it use coupon

		//check use point status

		$this->db->select('a.*');
		$this->db->from('order_payment a');
		$this->db->where('a.order_id', $order_id);
		$query = $this->db->get();
		$result = "";
		if ($query->num_rows() > 0) {
			// return $query->result_array();
			$result = $query->row();
		}
		$paymentStatus = "";
		// var_dump($result->payment_id);
		//main payment method
		if (!empty($result) && $result->payment_id == 1) {
			$paymentStatus = $paymentStatus . display('cash_on_delivery');
			// $paymentStatus = $paymentStatus . $this->session->userdata('language');

		}
		if (!empty($result) && $result->payment_id != 1) {
			$this->db->select('agent');
			$this->db->from('payment_gateway');
			$this->db->where('used_id', $result->payment_id);
			$query = $this->db->get();
			$result2 = "";
			if ($query->num_rows() > 0) {
				// return $query->result_array();
				$result2 = $query->row();
				$paymentStatus = $paymentStatus . $result2->agent;
			}
		}

		//end main payment method

		//check have coupon payment
		$order = $this->get_order_by_orderid($order_id);
		// $paymentStatus = $paymentStatus . ' + ' . $order->coupon;
		if (!empty($order->coupon)) {
			//check if the order coupon code is empty or not
			$paymentStatus = $paymentStatus . ' + ' . display('use_coupon_code');

		} //if(!empty($result->coupon)){
		//end check have coupon payment

		//check have use point method

		//end check have coupon payment

		return $paymentStatus;
	}
	public function checkOrderStatus($order_id) {
		$order = $this->get_order_by_orderid($order_id);

		$this->db->select('a.*,b.customer_name, IFNULL(c.invoice_status,0) as invoice_status');
		$this->db->from('order a');
		$this->db->join('customer_information b', 'b.customer_id = a.customer_id', 'left');
		$this->db->join('invoice c', 'c.order_id = a.order_id', 'left');

		// if (!empty($filter['order_no'])) {
		// 	$this->db->where('a.order', $filter['order_no']);
		// }
		// if (!empty($filter['customer_name'])) {
		// 	$this->db->like('b.customer_name', $filter['customer_name'], 'both');
		// }

		// if (!empty($filter['order_date'])) {
		// 	$this->db->where('a.date', date('m-d-Y', strtotime($filter['order_date'])));
		// }

		// if (!empty($filter['invoice_status'])) {
		// 	if (($filter['invoice_status'] == '3')) {
		// 		$this->db->where('c.invoice_status', 3);
		// 		$this->db->or_where('c.invoice_status IS NULL');
		// 	} else {
		// 		$this->db->where('c.invoice_status', $filter['invoice_status']);
		// 	}
		// }
		$this->db->where('a.order_id', $order_id);
		$this->db->order_by('a.order', 'desc');
		$query = $this->db->get();
		$result = "";
		if ($query->num_rows() > 0) {
			// return $query->result_array();
			$result = $query->row();
		}

		$status = "";
		if ($result->invoice_status == '0') {
			$status = display('pending');
		} else if ($result->invoice_status == '1') {

			$status = display('shipped');
		} else if ($result->invoice_status == '2') {

			$status = display('cancel');
		} else if ($result->invoice_status == '3') {
			// echo "<label class='label label-warning'>" . display('pending') . "</label>";
			$status = display('pending');
		} else if ($result->invoice_status == '4') {
			// echo "<label class='label label-success'>" . display('complete') . "</label>";
			$status = display('complete');
		} else if ($result->invoice_status == '5') {

			$status = display('processing');
		} else if ($result->invoice_status == '6') {

			$status = display('return');
		}
		return $status;
	} //public function checkOrderStatus($order_id){

	public function customer_order_details($order_id) {
		// $this->load->model('dashboard/Orders');
		$this->load->model('dashboard/Soft_settings');
		// $order_detail = $this->Orders->retrieve_order_html_data($order_id);
		$order_detail = $this->retrieve_order_html_data($order_id);

		// var_dump($order_detail);
		// $payinfo = $this->Orders->get_order_payinfo($order_id);
		$payinfo = $this->get_order_payinfo($order_id);

		$subTotal_quantity = 0;
		$subTotal_cartoon = 0;
		$subTotal_discount = 0;

		if (!empty($order_detail)) {
			foreach ($order_detail as $k => $v) {
				$order_detail[$k]['final_date'] = $this->occational->dateConvert($order_detail[$k]['date']);
				$subTotal_quantity = $subTotal_quantity + $order_detail[$k]['quantity'];
			}
			$i = 0;
			foreach ($order_detail as $k => $v) {
				$i++;
				$order_detail[$k]['sl'] = $i;
			}
		}

		$currency_details = $this->Soft_settings->retrieve_currency_info();
		// $company_info = $this->Orders->retrieve_company();
		$data = array(
			// 'title' => display('order_details'),
			'title' => $order_id,
			'order_id' => $order_detail[0]['order_id'],
			'order_no' => $order_detail['order'],
			'customer_address' => $order_detail['customer_short_address'],
			'ship_customer_short_address' => $order_detail['ship_customer_short_address'],
			'ship_customer_name' => $order_detail['ship_customer_name'],
			'customer_name' => $order_detail[0]['customer_name'],
			'customer_mobile' => $order_detail[0]['customer_mobile'],
			'ship_customer_mobile' => $order_detail[0]['ship_customer_mobile'],
			'customer_email' => $order_detail[0]['customer_email'],
			'ship_customer_email' => $order_detail[0]['ship_customer_email'],
			'final_date' => $order_detail[0]['final_date'],
			'total_amount' => $order_detail[0]['total_amount'],
			'order_discount' => $order_detail[0]['order_discount'],
			'service_charge' => $order_detail[0]['service_charge'],
			'paid_amount' => $order_detail[0]['paid_amount'],
			'due_amount' => $order_detail[0]['due_amount'],
			'details' => $order_detail[0]['details'],
			'subTotal_quantity' => $subTotal_quantity,
			'order_all_data' => $order_detail,
			// 'company_info' => $company_info,
			'currency' => $currency_details[0]['currency_icon'],
			'position' => $currency_details[0]['currency_position'],
			'payinfo' => $payinfo,
		);

		return $data;
	} //public function customer_order_details($order_id) {
	//end added Order details

	//Order List Count
	public function total_customer_order($customer_id) {
		$this->db->select('a.*,b.customer_name');
		$this->db->from('order a');
		$this->db->join('customer_information b', 'b.customer_id = a.customer_id');
		$this->db->where('a.delete_time', NULL); //added
		$this->db->where('b.customer_id', $customer_id);
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->num_rows();
		}
		return false;
	}

	//Order entry
	public function order_entry() {
		//Order information
		$order_id = $this->auth->generator(15);
		$quantity = $this->input->post('product_quantity', TRUE);
		$available_quantity = $this->input->post('available_quantity', TRUE);
		$product_id = $this->input->post('product_id', TRUE);

		//Stock availability check
		$result = array();
		foreach ($available_quantity as $k => $v) {
			if ($v < $quantity[$k]) {
				$this->session->set_userdata(array('error_message' => display('you_can_not_buy_greater_than_available_cartoon')));
				redirect('customer/order');
			}
		}

		//Product existing check
		if ($product_id == null) {
			$this->session->set_userdata(array('error_message' => display('please_select_product')));
			redirect('customer/order');
		}

		//Customer existing check
		if (($this->input->post('customer_name_others', TRUE) == null) && ($this->input->post('customer_id', TRUE) == null)) {
			$this->session->set_userdata(array('error_message' => display('please_select_customer')));
			redirect(base_url() . 'customer/order');
		}

		//Customer data Existence Check.
		if ($this->input->post('customer_id', TRUE) == "") {

			$customer_id = $this->auth->generator(15);
			//Customer  basic information adding.
			$data = array(
				'customer_id' => $customer_id,
				'customer_name' => $this->input->post('customer_name_others', TRUE),
				'customer_address_1' => $this->input->post('customer_name_others_address', TRUE),
				'customer_mobile' => "NONE",
				'customer_email' => "NONE",
				'status' => 1,
			);

			$result = $this->Customers->customer_entry($data);
			if ($result == false) {
				$this->session->set_userdata(array('error_message' => display('already_exists')));
				redirect('customer/order/manage_order');
			}
			//Previous balance adding -> Sending to customer model to adjust the data.
			$this->Customers->previous_balance_add(0, $customer_id);
		} else {
			$customer_id = $this->input->post('customer_id', TRUE);
		}

		//Data inserting into order table
		$data = array(
			'order_id' => $order_id,
			'customer_id' => $this->input->post('customer_id', TRUE),
			'date' => $this->input->post('invoice_date', TRUE),
			'total_amount' => $this->input->post('grand_total_price', TRUE),
			'order' => $this->number_generator_order(),
			'total_discount' => $this->input->post('total_discount', TRUE),
			'order_discount' => (int) $this->input->post('invoice_discount', TRUE) + (int) $this->input->post('total_discount', TRUE),
			'service_charge' => $this->input->post('service_charge', TRUE),
			'store_id' => $this->input->post('store_id', TRUE),
			'paid_amount' => $this->input->post('paid_amount', TRUE),
			'due_amount' => $this->input->post('due_amount', TRUE),
			'status' => 1,
		);
		$this->db->insert('order', $data);

		//Order details info
		$rate = $this->input->post('product_rate', TRUE);
		$p_id = $this->input->post('product_id', TRUE);
		$total_amount = $this->input->post('total_price', TRUE);
		$discount = $this->input->post('discount', TRUE);
		$variants = $this->input->post('variant_id', TRUE);
		$color_variants = $this->input->post('color_variant', TRUE);

		//Order details entry
		for ($i = 0, $n = count($p_id); $i < $n; $i++) {
			$product_quantity = $quantity[$i];
			$product_rate = $rate[$i];
			$product_id = $p_id[$i];
			$discount_rate = $discount[$i];
			$total_price = $total_amount[$i];
			$variant_id = $variants[$i];
			$variant_color = $color_variants[$i];
			$supplier_rate = $this->supplier_rate($product_id);

			$order_details = array(
				'order_details_id' => $this->auth->generator(15),
				'order_id' => $order_id,
				'product_id' => $product_id,
				'variant_id' => $variant_id,
				'variant_color' => $variant_color,
				'store_id' => $this->input->post('store_id', TRUE),
				'quantity' => $product_quantity,
				'rate' => $product_rate,
				'supplier_rate' => $supplier_rate[0]['supplier_price'],
				'total_price' => $total_price,
				'discount' => $discount_rate,
				'status' => 1,
			);

			if (!empty($quantity)) {

				$this->db->select('*');
				$this->db->from('order_details');
				$this->db->where('order_id', $order_id);
				$this->db->where('product_id', $product_id);
				$this->db->where('variant_id', $variant_id);
				if (!empty($variant_color)) {
					$this->db->where('variant_color', $variant_color);
				}
				$query = $this->db->get();
				$result = $query->num_rows();
				if ($result > 0) {
					$this->db->set('quantity', 'quantity+' . $product_quantity, FALSE);
					$this->db->set('total_price', 'total_price+' . $total_price, FALSE);
					$this->db->where('order_id', $order_id);
					$this->db->where('product_id', $product_id);
					$this->db->where('variant_id', $variant_id);
					if (!empty($variant_color)) {
						$this->db->where('variant_color', $variant_color);
					}
					$this->db->update('order_details');
				} else {
					$this->db->insert('order_details', $order_details);
				}

			}
		}

		//Tax info
		$cgst = $this->input->post('cgst', TRUE);
		$sgst = $this->input->post('sgst', TRUE);
		$igst = $this->input->post('igst', TRUE);
		$cgst_id = $this->input->post('cgst_id', TRUE);
		$sgst_id = $this->input->post('sgst_id', TRUE);
		$igst_id = $this->input->post('igst_id', TRUE);

		//Tax collection summary for three
		//CGST tax info
		for ($i = 0, $n = count($cgst); $i < $n; $i++) {
			$cgst_tax = $cgst[$i];
			$cgst_tax_id = $cgst_id[$i];
			$cgst_summary = array(
				'order_tax_col_id' => $this->auth->generator(15),
				'order_id' => $order_id,
				'tax_amount' => $cgst_tax,
				'tax_id' => $cgst_tax_id,
				'date' => $this->input->post('invoice_date', TRUE),
			);
			if (!empty($cgst[$i])) {
				$result = $this->db->select('*')
					->from('order_tax_col_summary')
					->where('order_id', $order_id)
					->where('tax_id', $cgst_tax_id)
					->get()
					->num_rows();
				if ($result > 0) {
					$this->db->set('tax_amount', 'tax_amount+' . $cgst_tax, FALSE);
					$this->db->where('order_id', $order_id);
					$this->db->where('tax_id', $cgst_tax_id);
					$this->db->update('order_tax_col_summary');
				} else {
					$this->db->insert('order_tax_col_summary', $cgst_summary);
				}
			}
		}

		//SGST tax info
		for ($i = 0, $n = count($sgst); $i < $n; $i++) {
			$sgst_tax = $sgst[$i];
			$sgst_tax_id = $sgst_id[$i];

			$sgst_summary = array(
				'order_tax_col_id' => $this->auth->generator(15),
				'order_id' => $order_id,
				'tax_amount' => $sgst_tax,
				'tax_id' => $sgst_tax_id,
				'date' => $this->input->post('invoice_date', TRUE),
			);
			if (!empty($sgst[$i])) {
				$result = $this->db->select('*')
					->from('order_tax_col_summary')
					->where('order_id', $order_id)
					->where('tax_id', $sgst_tax_id)
					->get()
					->num_rows();
				if ($result > 0) {
					$this->db->set('tax_amount', 'tax_amount+' . $sgst_tax, FALSE);
					$this->db->where('order_id', $order_id);
					$this->db->where('tax_id', $sgst_tax_id);
					$this->db->update('order_tax_col_summary');
				} else {
					$this->db->insert('order_tax_col_summary', $sgst_summary);
				}
			}
		}

		//IGST tax info
		if (!empty($igst)) {
			for ($i = 0, $n = count($igst); $i < $n; $i++) {
				$igst_tax = $igst[$i];
				$igst_tax_id = $igst_id[$i];

				$igst_summary = array(
					'order_tax_col_id' => $this->auth->generator(15),
					'order_id' => $order_id,
					'tax_amount' => $igst_tax,
					'tax_id' => $igst_tax_id,
					'date' => $this->input->post('invoice_date', TRUE),
				);
				if (!empty($igst[$i])) {
					$result = $this->db->select('*')
						->from('order_tax_col_summary')
						->where('order_id', $order_id)
						->where('tax_id', $igst_tax_id)
						->get()
						->num_rows();

					if ($result > 0) {
						$this->db->set('tax_amount', 'tax_amount+' . $igst_tax, FALSE);
						$this->db->where('order_id', $order_id);
						$this->db->where('tax_id', $igst_tax_id);
						$this->db->update('order_tax_col_summary');
					} else {
						$this->db->insert('order_tax_col_summary', $igst_summary);
					}
				}
			}
		}
		//Tax collection summary for three

		//Tax collection details for three
		//CGST tax info
		if (!empty($cgst)) {
			for ($i = 0, $n = count($cgst); $i < $n; $i++) {
				$cgst_tax = $cgst[$i];
				$cgst_tax_id = $cgst_id[$i];
				$product_id = $p_id[$i];
				$variant_id = $variants[$i];
				$cgst_details = array(
					'order_tax_col_de_id' => $this->auth->generator(15),
					'order_id' => $order_id,
					'amount' => $cgst_tax,
					'product_id' => $product_id,
					'tax_id' => $cgst_tax_id,
					'variant_id' => $variant_id,
					'date' => $this->input->post('invoice_date', TRUE),
				);
				if (!empty($cgst[$i])) {

					$result = $this->db->select('*')
						->from('order_tax_col_details')
						->where('order_id', $order_id)
						->where('tax_id', $cgst_tax_id)
						->where('product_id', $product_id)
						->where('variant_id', $variant_id)
						->get()
						->num_rows();
					if ($result > 0) {
						$this->db->set('amount', 'amount+' . $cgst_tax, FALSE);
						$this->db->where('order_id', $order_id);
						$this->db->where('tax_id', $cgst_tax_id);
						$this->db->where('variant_id', $variant_id);
						$this->db->update('order_tax_col_details');
					} else {
						$this->db->insert('order_tax_col_details', $cgst_details);
					}
				}
			}
		}

		//SGST tax info
		if (!empty($sgst)) {
			for ($i = 0, $n = count($sgst); $i < $n; $i++) {
				$sgst_tax = $sgst[$i];
				$sgst_tax_id = $sgst_id[$i];
				$product_id = $p_id[$i];
				$variant_id = $variants[$i];
				$sgst_summary = array(
					'order_tax_col_de_id' => $this->auth->generator(15),
					'order_id' => $order_id,
					'amount' => $sgst_tax,
					'product_id' => $product_id,
					'tax_id' => $sgst_tax_id,
					'variant_id' => $variant_id,
					'date' => $this->input->post('invoice_date', TRUE),
				);
				if (!empty($sgst[$i])) {
					$result = $this->db->select('*')
						->from('order_tax_col_details')
						->where('order_id', $order_id)
						->where('tax_id', $sgst_tax_id)
						->where('product_id', $product_id)
						->where('variant_id', $variant_id)
						->get()
						->num_rows();
					if ($result > 0) {
						$this->db->set('amount', 'amount+' . $sgst_tax, FALSE);
						$this->db->where('order_id', $order_id);
						$this->db->where('tax_id', $sgst_tax_id);
						$this->db->where('variant_id', $variant_id);
						$this->db->update('order_tax_col_details');
					} else {
						$this->db->insert('order_tax_col_details', $sgst_summary);
					}
				}
			}
		}

		//IGST tax info
		if (!empty($igst)) {
			for ($i = 0, $n = count($igst); $i < $n; $i++) {
				$igst_tax = $igst[$i];
				$igst_tax_id = $igst_id[$i];
				$product_id = $p_id[$i];
				$variant_id = $variants[$i];
				$igst_summary = array(
					'order_tax_col_de_id' => $this->auth->generator(15),
					'order_id' => $order_id,
					'amount' => $igst_tax,
					'product_id' => $product_id,
					'tax_id' => $igst_tax_id,
					'variant_id' => $variant_id,
					'date' => $this->input->post('invoice_date', TRUE),
				);
				if (!empty($igst[$i])) {
					$result = $this->db->select('*')
						->from('order_tax_col_details')
						->where('order_id', $order_id)
						->where('tax_id', $igst_tax_id)
						->where('product_id', $product_id)
						->where('variant_id', $variant_id)
						->get()
						->num_rows();
					if ($result > 0) {
						$this->db->set('amount', 'amount+' . $igst_tax, FALSE);
						$this->db->where('order_id', $order_id);
						$this->db->where('tax_id', $igst_tax_id);
						$this->db->where('variant_id', $variant_id);
						$this->db->update('order_tax_col_details');
					} else {
						$this->db->insert('order_tax_col_details', $igst_summary);
					}
				}
			}
		}
		//Tax collection details for three
		return $order_id;
	}
	//Get Supplier rate of a product
	public function supplier_rate($product_id) {
		$this->db->select('supplier_price');
		$this->db->from('product_information');
		$this->db->where(array('product_id' => $product_id));
		$query = $this->db->get();
		return $query->result_array();

	}

	//Retrieve order_html_data

	//original order html data

	// public function retrieve_order_html_data($order_id) {
	// 	$this->db->select('
	// 		a.*,
	// 		b.*,
	// 		c.*,
	// 		d.product_id,
	// 		d.product_name,
	// 		d.product_details,
	// 		d.product_model,d.unit,
	// 		e.unit_short_name,
	// 		f.variant_name,
	// 		a.details
	// 		');
	// 	$this->db->from('order a');
	// 	$this->db->join('customer_information b', 'b.customer_id = a.customer_id');
	// 	$this->db->join('order_details c', 'c.order_id = a.order_id');
	// 	$this->db->join('product_information d', 'd.product_id = c.product_id');
	// 	$this->db->join('unit e', 'e.unit_id = d.unit', 'left');
	// 	$this->db->join('variant f', 'f.variant_id = c.variant_id', 'left');
	// 	$this->db->where('a.order_id', $order_id);
	// 	$query = $this->db->get();
	// 	if ($query->num_rows() > 0) {
	// 		return $query->result_array();
	// 	}
	// 	return false;
	// }
	//end original order html data

	// Move original order html data from dashboard model orders
	public function retrieve_order_html_data($order_id) {
		// $details_page = $this->uri->segment(2);
		// $this->db->select('
		// 	a.*');
		$this->db->select('
			a.*,
			b.*,
			c.*,
			d.product_id,
			d.product_name,
			d.product_details,
			d.product_model,d.unit,
			e.unit_short_name,
			f.variant_name,
			g.customer_name as ship_customer_name,
			g.first_name as ship_first_name, g.last_name as ship_last_name,
			g.customer_short_address as ship_customer_short_address,
			g.customer_address_1 as ship_customer_address_1,
			g.customer_address_2 as ship_customer_address_2,
			g.customer_mobile as ship_customer_mobile,
			g.customer_email as ship_customer_email,
			g.city as ship_city,
			g.state as ship_state,
			g.country as ship_country,
			g.zip as ship_zip,
			g.company as ship_company
			');
		$this->db->from('order a');
		$this->db->join('customer_information b', 'b.customer_id = a.customer_id');
		// $this->db->join('shipping_info g', 'g.customer_id = a.customer_id');
		$this->db->join('shipping_info g', 'g.order_id = a.order_id');
		$this->db->join('order_details c', 'c.order_id = a.order_id');
		$this->db->join('product_information d', 'd.product_id = c.product_id');
		// $this->db->join('unit e', 'e.unit_id = d.unit', 'left');
		// $this->db->join('variant f', 'f.variant_id = c.variant_id', 'left');
		$this->db->join('unit e', 'e.unit_id = d.unit', 'left');
		$this->db->join('variant f', 'f.variant_id = c.variant_id', 'left');
		$this->db->where('a.order_id', $order_id);
		// if ($details_page == 'order_details_data') {
		// 	$this->db->where('g.order_id', $order_id);
		// }
		// $this->db->group_by('c.product_id, c.order_details_id');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();

		}
		return false;
	}
	//end  Move original order html data from dashboard model orders

	//Retrieve company Edit Data
	public function retrieve_company() {
		$this->db->select('*');
		$this->db->from('company_information');
		$this->db->limit('1');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;
	}

	//Get total product
	public function get_total_product($product_id) {

		$this->db->select('
			product_name,
			product_id,
			supplier_price,
			price,
			supplier_id,
			unit,
			variants,
			product_model,
			onsale,
			onsale_price,
			unit.unit_short_name
			');
		$this->db->from('product_information');
		$this->db->join('unit', 'unit.unit_id = product_information.unit', 'left');
		$this->db->where(array('product_id' => $product_id, 'status' => 1));
		$product_information = $this->db->get()->row();

		$html = $colorhtml = "";
		if (!empty($product_information->variants)) {
			$exploded = explode(',', $product_information->variants);

			$this->db->select('*');
			$this->db->from('variant');
			$this->db->where_in('variant_id', $exploded);
			$this->db->order_by('variant_name', 'asc');
			$variant_list = $this->db->get()->result();
			$var_types = array_column($variant_list, 'variant_type');

			$html .= '<option value=""></option>';
			foreach ($variant_list as $varitem) {

				if ($varitem->variant_type == 'size') {
					$html .= "<option value=" . $varitem->variant_id . ">" . $varitem->variant_name . "</option>";
				}
			}

			if (in_array('color', $var_types)) {
				$colorhtml .= "<option value=''></option>";
				foreach ($variant_list as $varitem2) {
					if ($varitem2->variant_type == 'color') {
						$colorhtml .= "<option value=" . $varitem2->variant_id . ">" . $varitem2->variant_name . "</option>";
					}
				}
			}

		}

		$this->db->select('tax.*,tax_product_service.product_id,tax_percentage');
		$this->db->from('tax_product_service');
		$this->db->join('tax', 'tax_product_service.tax_id = tax.tax_id', 'left');
		$this->db->where('tax_product_service.product_id', $product_id);
		$tax_information = $this->db->get()->result();

		//New tax calculation for discount
		if (!empty($tax_information)) {
			foreach ($tax_information as $k => $v) {
				if ($v->tax_id == 'H5MQN4NXJBSDX4L') {
					$tax['cgst_tax'] = ($v->tax_percentage) / 100;
					$tax['cgst_name'] = $v->tax_name;
					$tax['cgst_id'] = $v->tax_id;
				} elseif ($v->tax_id == '52C2SKCKGQY6Q9J') {
					$tax['sgst_tax'] = ($v->tax_percentage) / 100;
					$tax['sgst_name'] = $v->tax_name;
					$tax['sgst_id'] = $v->tax_id;
				} elseif ($v->tax_id == '5SN9PRWPN131T4V') {
					$tax['igst_tax'] = ($v->tax_percentage) / 100;
					$tax['igst_name'] = $v->tax_name;
					$tax['igst_id'] = $v->tax_id;
				}
			}
		}

		$purchase = $this->db->select("SUM(quantity) as totalPurchaseQnty")
			->from('product_purchase_details')
			->where('product_id', $product_id)
			->get()
			->row();

		$sales = $this->db->select("SUM(quantity) as totalSalesQnty")
			->from('invoice_details')
			->where('product_id', $product_id)
			->get()
			->row();
		$stock = $purchase->totalPurchaseQnty - $sales->totalSalesQnty;

		$discount = "";
		if ($product_information->onsale == 1) {
			$discount = ($product_information->price - $product_information->onsale_price);
		}

		$data2 = array(
			'total_product' => $stock,
			'supplier_price' => $product_information->supplier_price,
			'price' => $product_information->price,
			'variant_id' => $product_information->variants,
			'supplier_id' => $product_information->supplier_id,
			'product_name' => $product_information->product_name,
			'product_model' => $product_information->product_model,
			'product_id' => $product_information->product_id,
			'variant' => $html,
			'colorhtml' => $colorhtml,
			'discount' => $discount,
			'sgst_tax' => (!empty($tax['sgst_tax']) ? $tax['sgst_tax'] : null),
			'cgst_tax' => (!empty($tax['cgst_tax']) ? $tax['cgst_tax'] : null),
			'igst_tax' => (!empty($tax['igst_tax']) ? $tax['igst_tax'] : null),
			'cgst_id' => (!empty($tax['cgst_id']) ? $tax['cgst_id'] : null),
			'sgst_id' => (!empty($tax['sgst_id']) ? $tax['sgst_id'] : null),
			'igst_id' => (!empty($tax['igst_id']) ? $tax['igst_id'] : null),
			'unit' => $product_information->unit_short_name,
		);

		return $data2;
	}
	//NUMBER GENERATOR
	public function number_generator() {
		$this->db->select_max('invoice', 'invoice_no');
		$query = $this->db->get('invoice');
		$result = $query->result_array();
		$order_no = $result[0]['invoice_no'];
		if ($order_no != '') {
			$order_no = $order_no + 1;
		} else {
			$order_no = 1000;
		}
		return $order_no;
	}

	//NUMBER GENERATOR FOR ORDER
	public function number_generator_order() {
		$this->db->select_max('order', 'order_no');
		$query = $this->db->get('order');
		$result = $query->result_array();
		$order_no = $result[0]['order_no'];
		if ($order_no != '') {
			$order_no = $order_no + 1;
		} else {
			$order_no = 1000;
		}
		return $order_no;
	}

	// Get variant stock info
	public function check_variant_wise_stock($product_id, $store_id, $variant_id, $variant_color = false) {

		$this->db->select("SUM(quantity) as totalPurchaseQnty");
		$this->db->from('transfer');
		$this->db->where('product_id', $product_id);
		$this->db->where('variant_id', $variant_id);
		if (!empty($variant_color)) {
			$this->db->where('variant_color', $variant_color);
		}
		$this->db->where('store_id', $store_id);
		$purchase = $this->db->get()->row();

		$this->db->select("SUM(quantity) as totalSalesQnty");
		$this->db->from('invoice_details');
		$this->db->where('product_id', $product_id);
		$this->db->where('variant_id', $variant_id);
		if (!empty($variant_color)) {
			$this->db->where('variant_color', $variant_color);
		}
		$this->db->where('store_id', $store_id);
		$sales = $this->db->get()->row();

		$stock = $purchase->totalPurchaseQnty - $sales->totalSalesQnty;
		return $stock;
	}

	// check variant wise product price
	public function check_variant_wise_price($product_id, $variant_id, $variant_color = false) {
		$pinfo = $this->db->select('price, onsale, onsale_price, variant_price')
			->from('product_information')
			->where('product_id', $product_id)
			->get()->row();

		if ($pinfo->variant_price) {

			$this->db->select('price');
			$this->db->from('product_variants');
			$this->db->where('product_id', $product_id);
			$this->db->where('var_size_id', $variant_id);
			if (!empty($variant_color)) {
				$this->db->where('var_color_id', $variant_color);
			} else {
				$this->db->where("var_color_id IS NULL");
			}
			$varprice = $this->db->get()->row();

			if (!empty($varprice)) {
				$price_arr['price'] = $varprice->price;
				$price_arr['regular_price'] = $pinfo->price;
			} else {
				if (!empty($pinfo->onsale) && !empty($pinfo->onsale_price)) {
					$price_arr['price'] = $pinfo->onsale_price;
					$price_arr['regular_price'] = $pinfo->price;
				} else {
					$price_arr['price'] = $price_arr['regular_price'] = $pinfo->price;
				}
			}

		} else {

			if (!empty($pinfo->onsale) && !empty($pinfo->onsale_price)) {
				$price_arr['price'] = $pinfo->onsale_price;
				$price_arr['regular_price'] = $pinfo->price;
			} else {
				$price_arr['price'] = $price_arr['regular_price'] = $pinfo->price;
			}
		}
		return $price_arr;
	}

	//added function get_order_payinfo at 2021-05-18
	public function get_order_payinfo($order_id) {
		return $this->db->select('a.*, b.agent as payment_method')
			->from('order_payment a')
			->join('payment_gateway b', 'a.payment_id=b.used_id', 'left')
			->where('a.order_id', $order_id)
			->get()->row_array();
		// ->get()->row();

	}

	public function getOrderUsedRewardPoint($order_id) {

		//exclude buy reward product
		$usedPoint = 0;
		$this->db->select('SUM(amount) as total');
		$this->db->from('customer_usedpoint');
		$this->db->where('order_id', $order_id);
		$this->db->where('convertion_rate', 1);
		$this->db->where('delete_time', NULL);
		$result = $this->db->get()->row();
		if (!empty($result)) {
			$usedPoint = $result->total;
		}

		return $usedPoint;
	}
	//end added function get_order_payinfo

}
