<?php
class ModelShippingMondialrelay extends Model {
	public function checkinstall() {
		$needInstall = 'nok';
        $query = $this->db->query(" SHOW TABLES LIKE '" . DB_PREFIX . "products' ");

		foreach ($query->rows as $ligne) {
			$needInstall = 'ok';
			break;
		}
		
		// Si pas encore installer on deploie les tables
		if ($needInstall == 'nok') {
			$this->install_tables();
		}
           
		return $needInstall;
	}
	
	public function install_tables() {		
		/** les requetes de creation ci dessous **/
		
		if (!$this->mysql_table_exists(DB_PREFIX . "mr_ptrelais_commande")) {
		    $query = $this->db->query("
				CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "mr_ptrelais_commande` (
				`id_mr_rel_pt_order` int(10) unsigned NOT NULL auto_increment,
				`id_mr_num_point_relais` int(10) unsigned NOT NULL,
				`id_order` int(10) unsigned NOT NULL,
				`status` varchar(3) DEFAULT 'ATT',
				`date_creation` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
				`date_creation_etiquette` TIMESTAMP NOT NULL,
				`num_expe` int(10) NOT NULL,
				`url_pdf_etiq` varchar(400) NOT NULL,
				`poids_colis` int(10) DEFAULT 100,
				`pays_livraison` varchar(2) DEFAULT 'FR',
				PRIMARY KEY  (`id_mr_rel_pt_order`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
			");
		}
	}
	
	public function getMondialRelayOrders($filter="") {
			$req = 
                    " SELECT DISTINCT t1.id_mr_rel_pt_order, t1.id_mr_num_point_relais, t1.id_order, " .
					"                 t1.num_expe, t1.url_pdf_etiq, t1.status, t1.date_creation, " .
					"                 t1.date_creation_etiquette " .
                    " FROM `" . DB_PREFIX . "mr_ptrelais_commande` t1, " .
					"      `" . DB_PREFIX . "order` t3  " .
					" WHERE t1.id_order = t3.order_id ";
			
			if (!empty($filter)) {
				$req .= " ORDER BY " . $filter['colonne'] . " " . $filter['tri'] . " ";
			} else {
				$req .= " ORDER BY t1.date_creation_etiquette DESC ";
			}
			
            $query = $this->db->query($req);
					
			if ($query->num_rows) {
				$data = array();
				foreach($query->rows as $ligne) {
					$data[] = $ligne;
				}
				return $data;
			} else {
				return false;
			}
	}	
	
	
	public function getMondialRelayOrdersSansEtiquette($filter="") {
			$req = 
                    " SELECT DISTINCT t1.id_mr_rel_pt_order, t1.id_mr_num_point_relais, t1.id_order, " .
					"                 t1.num_expe, t1.url_pdf_etiq, t1.status, t1.date_creation, " .
					"                 t1.date_creation_etiquette, " .
					"                 t1.poids_colis " .
                    " FROM `" . DB_PREFIX . "mr_ptrelais_commande` t1, " .
					"      `" . DB_PREFIX . "order` t3  " .
					" WHERE t1.id_order = t3.order_id " .
					"       AND t1.status = 'ATT' ";
			
			if (!empty($filter)) {
				$req .= " ORDER BY " . $filter['colonne'] . " " . $filter['tri'] . " ";
			} else {
				$req .= " ORDER BY t1.id_mr_rel_pt_order DESC ";
			}
			
            $query = $this->db->query($req);
					
			if ($query->num_rows) {
				$data = array();
				foreach($query->rows as $ligne) {
					$data[] = $ligne;
				}
				return $data;
			} else {
				return false;
			}
	}	
	
	public function getSecurityCode($enseigne, $code_pays, $ville = "", $cp, $taille = "", $poids = "", $action, $key) {
            $securityCode = "";
			
			$securityCode = md5($enseigne . $code_pays . $ville . $cp . $taille . $poids . $action . $key);
			
            return $securityCode;
	}
	
	function mysql_table_exists($table) {
		$tables = $this->db->query('SHOW TABLES');
		foreach($tables as $tablename) { 
			if($table == $tablename) {
				return 1; 
			} 
		}
		return 0;
	}
}
?>