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

class Variants extends CI_Model {
	public function __construct() {
		parent::__construct();
	}
	//customer List
	public function variant_list() {
		$this->db->select('*');
		$this->db->from('variant');
		$this->db->where('delete_time', NULL); //added
		$this->db->order_by('variant_name', 'asc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;
	}

	public function category_wise_variant_list($cat_id) {
		$this->db->select('cv.variant_id,v.*');
		$this->db->from('category_variant cv');
		$this->db->where('cv.category_id =', $cat_id);
		$this->db->join('variant v', 'cv.variant_id = v.variant_id', 'left');
		$this->db->order_by('v.variant_name', 'asc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;
	}
	//category wise variant week sale list
	public function category_wise_variant_week_sale_list() {
		//may be add week sale list
		$this->db->select('cv.variant_id,v.*');
		$this->db->from('category_variant cv');
		// $this->db->where('cv.category_id =',$cat_id);
		$this->db->join('variant v', 'cv.variant_id = v.variant_id', 'left');
		$this->db->order_by('v.variant_name', 'asc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;

	}

	//end category wise variant week sale list

	//category wise variant best sale list
	public function category_wise_variant_best_sale_list() {
		//may be add week sale list
		$this->db->select('cv.variant_id,v.*');
		$this->db->from('category_variant cv');
		// $this->db->where('cv.category_id =',$cat_id);
		$this->db->join('variant v', 'cv.variant_id = v.variant_id', 'left');
		$this->db->order_by('v.variant_name', 'asc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;

	}

	//end category wise variant best sale list

	//variant Search Item
	public function variant_search_item($variant_id) {
		$this->db->select('*');
		$this->db->from('variant');
		$this->db->where('variant_id', $variant_id);
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;
	}
	//Count customer
	public function variant_entry($data) {
		$this->db->select('*');
		$this->db->from('variant');
		$this->db->where('status', 1);
		$this->db->where('delete_time', NULL); //added
		$this->db->where('variant_name', $data['variant_name']);
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return FALSE;
		} else {
			$this->db->insert('variant', $data);
			return TRUE;
		}
	}
	//Retrieve customer Edit Data
	public function retrieve_variant_editdata($variant_id) {
		$this->db->select('*');
		$this->db->from('variant');
		$this->db->where('variant_id', $variant_id);
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;
	}

	//Update variants
	public function update_variant($data, $variant_id) {
		$this->db->where('variant_id', $variant_id);
		$result = $this->db->update('variant', $data);

		if ($result) {
			return true;
		}
		return false;
	}
	// Delete variant Item
	public function delete_variant($variant_id) {
		// $this->db->where('variant_id', $variant_id);
		// $this->db->delete('variant');
		// return true;

		$this->db->trans_begin();
		$this->db->set('delete_time', date("Y-m-d"));
		$this->db->where('variant_id', $variant_id);
		$query = $this->db->update('variant');
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return false;
		} else {
			$this->db->trans_commit();
			return true;
		}
	}
}