<?php

class ModelExtensionTrackingDiscount extends Model {
	
	private $name = 'tracking_discount';
	
	public function install() {
		$this->db->query("CREATE TABLE if not exists " . DB_PREFIX . "affiliate_" . $this->name . " (
			customer_id int(11) unsigned not null default '0',
			`amount` decimal(6,2) NOT NULL DEFAULT '0.00',
			`type` enum('percent','fixed') not null default 'percent',
			primary key (customer_id)
		) ENGINE=MYISAM");
	}
	
	public function getAffiliate($customer_id) {		
		$res = $this->db->query("SELECT `amount` AS tracking_discount_amount, `type` AS tracking_discount_type FROM " . DB_PREFIX . "affiliate_tracking_discount WHERE customer_id = '" . (int)$customer_id . "' LIMIT 1");
		return ($res->num_rows ? $res->row : array('tracking_discount_amount' => '', 'tracking_discount_type' => ''));
	}
	
	public function deleteAffiliate($customer_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "affiliate_tracking_discount WHERE customer_id = '" . (int)$customer_id . "'");
	}
	
	public function addAffiliate($customer_id, $amount, $type) {
		$this->db->query("REPLACE INTO " . DB_PREFIX . "affiliate_tracking_discount SET customer_id = '" . (int)$customer_id . "', `amount` = '" . (float)$amount . "', `type` = '" . ($type === 'fixed' ? 'fixed' : 'percent') . "'");		
	}	
}
