<?php
class ModelSaleGemstoneweight extends Model {
	public function addGemStoneWeight($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "gemstone_weight SET  code = '" . $this->db->escape($data['code']) . "', gemstone_id = '" . $this->db->escape($data['gemstone_id']) . "',weight = '" . $this->db->escape($data['weight']) . "',size_code = '" . $this->db->escape($data['size_code']) . "',shape = '" . $this->db->escape($data['shape']) . "'");
	}
	 
	public function editGemStoneWeight($gemstoneweight_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "gemstone_weight SET  code = '" . $this->db->escape($data['code']) . "', gemstone_id = '" . $this->db->escape($data['gemstone_id']) . "',weight = '" . $this->db->escape($data['weight']) . "',size_code = '" . $this->db->escape($data['size_code']) . "',shape = '" . $this->db->escape($data['shape']) . "' WHERE gemstoneweight_id = '" . (int)$gemstoneweight_id . "'");
	}
	
	public function deleteGemStoneWeight($gemstoneweight_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "gemstone_weight WHERE gemstoneweight_id = '" . (int)$gemstoneweight_id . "'");
	//	$this->db->query("DELETE FROM " . DB_PREFIX . "product_discount WHERE gemstoneweight_id = '" . (int)$gemstoneweight_id . "'");
	}
	
	public function getGemStoneInfo($gemstoneweight_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "gemstone_weight WHERE gemstoneweight_id = '" . (int)$gemstoneweight_id . "'");
		
		return $query->row;
	}
	
	public function getGemStoneWeights($data = array()) {
		$sql = "SELECT a.*,b.gemname  FROM " . DB_PREFIX . "gemstone_weight a,gemstone b where a.gemstone_id=b.gemstone_id";
		
		$sql .= " ORDER BY  code";	
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	
	public function getTotalGemStoneWeights() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "gemstone_weight");
		
		return $query->row['total'];
	}
}
?>