<?php
/* **
 * @application PhpMyShopping
 * @part        Admin Boutique (AB)
 * @file        class.taxe.php
 * @path        ./admin/include/
 * @author      Stephane DINE <stephane@phpmyshopping.org>
 * @site        http://www.phpmyshopping.org
 * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
 *
 * PHP version 5
 *
 * Classe Order
 *
 * Gestion des taxes
 */

require_once 'class.lib.php';

class Taxe extends Lib
{




	/** ****************************************************************************************************
	 * Constructeur
	 */
	public function __construct()
	{
	}




	/** ****************************************************************************************************
	 * Methode listTax
	 *
	 * @desc   Retourne la liste des taxes definies pour une boutique donnee
	 * @return string
	 * @access public
	 */
	public function listTax()
	{
		$list = '
		<div class="heading">Liste des taxes</div>
		<div id="list">

		<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="500">
		  <tr bgcolor="#F1F3F5">
			<th align="center" width="*%">Lib&eacute;ll&eacute;</th>
			<th align="center" width="20%">Valeur</th>
			<th align="center" width="1%">Modifier</th>
			<th align="center" width="1%">Effacer</th>
		  </tr>';

		$sql = 'SELECT * FROM Tax ORDER BY TaxName ASC';
		$query = $this->query($sql);

		while ($result = mysql_fetch_array($query)) {
			$list .= '
			<tr class="row2" onmouseover="this.className=\'highlight\'" onmouseout="this.className=\'row2\'">
			  <td align="left">&nbsp;'.$result['TaxName'].'</td>
			  <td align="right">&nbsp;'.$result['TaxValue'].'&nbsp;%</td>
			  <td align="center"><a href="?C1=CO&C2=TA&C3=edit&ID_Tax='.$result['ID_Tax'].'"><img src="./images/update.png" alt="Edit" title="Editer cette taxe" /></a></td>
			  <td align="center"><a href="?C1=CO&C2=TA&C3=delete_confirm&ID_Tax='.$result['ID_Tax'].'"><img src="./images/delete.png" alt="Del" title="Effacer cette taxe" /></a></td>
			</tr>';
		}

		$list .= '
		</table>
		</div>';

		return $list;
	}




	/** ****************************************************************************************************
	 * Methode formTax
	 *
	 * @desc   Retourne un formulaire pour l'ajout ou la modification d'une taxe
	 * @param  int $ID_Tax Identifiant de la taxe
	 * @return string
	 * @access public
	 */
	public function formTax($ID_Tax = 0)
	{
		if ($ID_Tax > 0) {
			/**
			 * Modification d'une taxe
			 */
			$sql           = 'SELECT * FROM Tax WHERE ID_Tax = '.$ID_Tax;
			$query         = $this->query($sql);
			$result        = mysql_fetch_array($query);
			$C3            = 'update';
			$hidden_action = '<input type="hidden" name="ID_Tax" value="'.$result['ID_Tax'].'">';
			$title         = 'Modification d\'une taxe';
		} else {
			/**
			 * Ajout d'une taxe
			 */
			$C3            = 'insert_ok';
			$hidden_action = '';
			$title         = 'D&eacute;finition d\'une nouvelle taxe';
		}

		$form = '
		<div class="heading">'.$title.'</div>
		<div id="list">
		<form action="" method="POST" id="formTax">
		  <input type="hidden" name="C1" value="CO">
		  <input type="hidden" name="C2" value="TA">
		  <input type="hidden" name="C3" value="'.$C3.'">
		  '.$hidden_action.'

		  <table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5">
			<tr bgcolor="#F1F3F5">
			  <td colspan="2">&nbsp;</td>
			</tr>';
			$form .= $this->create_field('Libell&eacute; de la taxe :', array('R','T','L','M','TaxName'), 'input', 'text', 'TaxName', 40, 40, $result['TaxName']);
			$form .= $this->create_field('Valeur de la taxe :', array('R','T','L','M','TaxValue'), 'input', 'text', 'TaxValue', 8, 5, $result['TaxValue'], 0, 0, '<b class="red">%</b>');
			$form .= '
			<tr bgcolor="#F1F3F5">
			  <td colspan="2" align="center">
				<a href="#" onClick="checkForm(\'formTax\')" class="formButton">Enregistrer</a>
				<b>::</b>
				<a href="?C1=CO&C2=TA&C3=list" class="formButton">Annuler</a>
			  </td>
			</tr>
		  </table>

		</form>
		</div>';

		return $form;
	}




	/** ****************************************************************************************************
	 * Methode verrifProductTax
	 *
	 * @desc   Verrification des produits ayant pour taxe celle qui sera effacee
	 * @param  int $ID_Tax Identifiant de la taxe
	 * @return int/bool
	 * @access public
	 */
	public function verrifProductTax($ID_Tax)
	{
		$sql = 'SELECT COUNT(Product.ID_Product) AS NbProducts
				FROM Product, Tax
				WHERE Product.ID_Tax = Tax.ID_Tax
				AND Product.ID_Tax = '.$ID_Tax;

		$nbRows = mysql_fetch_array($this->query($sql));

		if ($nbRows['NbProducts'] > 0) {
			/**
			 * Il y a des produits dont il faut modifier l'ID_Tax
			 */
			return $nbRows['NbProducts'];
		} else {
			/**
			 * Aucuns produits trouves avec l'ID_Tax qui sera efface
			 */
			return false;
		}
	}




	/** ****************************************************************************************************
	 * Methode nbTax
	 *
	 * @desc   Retourne le nombre de tax definies pour une boutique donnee
	 * @param  int $ID_Shop Identifiant de la boutique
	 * @return bool
	 * @access public
	 */
	public function nbTax()
	{
		$query = $this->query('SELECT COUNT(ID_Tax) AS NbTax FROM Tax');
		$result = mysql_fetch_array($query);

		if ($result['NbTax'] == 1) {
			return false;
		} else {
			return true;
		}
	}





}
?>
