<?php

	/**
	 * Klasse für einen Lieferschein
	 * @author Daschmi
	 *
	 */
	class wpsg_deliverynote extends wpsg_model 
	{
		
		/**
		 * Lädt die Daten des Lieferscheins
		 * @see wpsg_model::load()
		 */
		public function load($deliverynote_id)
		{
			
			parent::load($deliverynote_id);
			
			$this->data = $this->db->fetchRow("SELECT DN.* FROM `".WPSG_TBL_DELIVERYNOTE."` AS DN WHERE DN.`id` = '".wpsg_q($deliverynote_id)."' ");
			
			if ($this->data['id'] != $deliverynote_id) $this->shop->throwErrorCode('2000_1');
			
			return true;
			
		} // public function load($deliverynote_id)
		 
		/**
		 * Gibt true zurück wenn der Lieferschein für ungültig erklärt wurde 
		 * @return boolean
		 */
		public function isCanceled()
		{
			
			if ($this->cancel == '1') return true;
			else return false;
			
		} // public function isCanceled()
		
		/**
		 * Gibt das Datum der Lieferung als Timestamp zurück
		 */
		public function getDeliveryTimestamp()
		{
			
			return strtotime($this->delivery_date);
			
		} // public function getDeliveryTimestamp()
		
		/**
		 * Gibt die Produkte dieses Lieferscheins zurück
		 */
		public function getProducts()
		{
			
			return $this->db->fetchAssoc("
				SELECT
					P.`id`, OP.`productkey`, P.`name`
				FROM
					`".WPSG_TBL_ORDERPRODUCT."` AS OP
						LEFT JOIN `".WPSG_TBL_PRODUCTS."` AS P ON (OP.`p_id` = P.`id`)
				WHERE
					OP.`o_id` = '".wpsg_q($this->order_id)."' AND
					OP.`product_index` IN (".wpsg_q($this->product_indexes).")
			", "productkey");
			
		} // public function getProducts()
		 
		/**
		 * Prüft ob ein Lieferschein für das Produkt erstellt werden kann
		 */
		public static function checkProductKey($product_indexes, $order_id)
		{
			
			$nExists = $GLOBALS['wpsg_db']->fetchOne("
				SELECT
					COUNT(*)
				FROM
					`".WPSG_TBL_DELIVERYNOTE."`
				WHERE
					`order_id` = '".wpsg_q($order_id)."' AND
					FIND_IN_SET('".wpsg_q($product_indexes)."', `product_indexes`) AND
					`cancel` != '1'	
			");
			
			if ($nExists > 0) return false;
			else return true;
			
		} // public static function checkProductKey($product_indexes, $order_id)

		/**
		 * Gibt einen Array von Lieferscheinen zurück, die auf den übergebenen Filter passen
		 * @param array $arFilter
		 */
		public static function find($arFilter = array())
		{
				
			$strQueryWHERE = "";
			$strQueryORDER = " DN.`cdate` ";
			$strQueryORDER_DIRECTION = " DESC ";
			
			if (wpsg_isSizedInt($arFilter['order_id'])) $strQueryWHERE .= " AND DN.`order_id` = '".wpsg_q($arFilter['order_id'])."' ";
			if (isset($arFilter['cancel'])) $strQueryWHERE .= " AND DN.`cancel` = '".wpsg_q($arFilter['cancel'])."' ";
			if (isset($arFilter['order'])) $strQueryORDER = wpsg_q($arFilter['order']);
			if (isset($arFilter['order_direction'])) $strQueryORDER_DIRECTION = wpsg_q($arFilter['order_direction']);			
			if (wpsg_isSizedInt($arFilter['product_index'])) $strQueryWHERE .= " AND FIND_IN_SET('".wpsg_q($arFilter['product_index'])."', DN.`product_indexes`) ";
			
			$strQuery = "
				SELECT
					DN.`id`
				FROM
					`".WPSG_TBL_DELIVERYNOTE."` AS DN
				WHERE
					1
					".$strQueryWHERE."
				GROUP BY
					DN.`id`
				ORDER BY
					".$strQueryORDER." ".$strQueryORDER_DIRECTION." 
			";
		
			$arDNID = $GLOBALS['wpsg_db']->fetchAssocField($strQuery);
			$arReturn = array();
				
			foreach ($arDNID as $dn_id)
			{
		
				$arReturn[] = wpsg_deliverynote::getInstance($dn_id);
		
			}
				
			return $arReturn;
				
		} // public static function find($arFilter = array())
		
	} // class wpsg_deliverynote extends wpsg_model

?>