<?php
/* **
 * @application PhpMyShopping
 * @part        Admin Boutique (AB)
 * @file        class.paiement.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 Paiement
 *
 * Gestion des modes de paiements disponibles sur les boutiques
 */

require_once 'class.lib.php';

class Paiement extends Lib
{




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




	/** *****************************************************************************************************************************
	 * Methode listTypePayment
	 *
	 * @desc   Retourne la liste des modes de paiements disponibles
	 * @return string
	 * @access public
	 */
	public function listPaymentType()
	{
		$list = '
		<div class="heading">Liste des diff&eacute;rents mode de paiements disponibles</div>
		<div id="list">

		<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="700">
		  <tr bgcolor="#F1F3F5">
			<th align="center" width="1%">Modifier</th>
			<th align="center">M&eacute;thode de paiement</th>
			<th align="center" width="1%">Status</th>
			<th align="center">Action</th>
			<th align="center">&nbsp;</th>
		  </tr>';

		/**
		 * Selection des modes de paiements
		 */
		$sql = 'SELECT ID_PaymentType, PaymentType, PaymentCode, Status, Action, Note,
					   (CASE Action
						  WHEN "R"
						  THEN CONCAT("<img src=\"./images/link.png\" border=\"0\" title=\"Redirection\" alt=\"R\" />")
						  ELSE CONCAT("<img src=\"./images/view_data.png\" border=\"0\" title=\"Affichage donnees\" alt=\"A\" />")
						END) AS ImgAction
				FROM PaymentType';
		$query = $this->query($sql);

		while ($result = mysql_fetch_array($query)) {
			$listPayment[$result['PaymentType']] = $result['Status'];
			$ID_PaymentType[]                    = $result['ID_PaymentType'];
			$imgAction[]                         = $result['ImgAction'];
			$note[]                              = $result['Note'];
		}

		$nb = 0;

		foreach ($listPayment as $key => $val) {
			if ($val == '1') {
				$title = $key.' disponible';
				$value = '0';
				$submit = 'Rendre indisponible';
			} else {
				$title = $key.' non disponible';
				$value = '1';
				$submit = 'Rendre disponible';
			}

			$list .= '
			<tr class="row2" onmouseover="this.className=\'highlight\'" onmouseout="this.className=\'row2\'">
			  <td align="center">
				<a href="?C1=CO&C2=MP&C3=edit&pt='.$ID_PaymentType[$nb].'">
				<img src="./images/update.png" alt="Mod" title="Modifier \'action a effectuer pour ce mode de paiement" border="0" /></a>
			  </td>
			  <td align="right">'.$key.' : </td>
			  <td align="center"><img src="./images/'.$val.'.png" alt="'.$val.'" title="'.$title.'" /></td>
			  <td align="center">
				<form action="" method="POST">
				  <input type="hidden" name="C1" value="CO">
				  <input type="hidden" name="C2" value="MP">
				  <input type="hidden" name="C3" value="update">
				  <input type="hidden" name="ID_PaymentType" value="'.$ID_PaymentType[$nb].'" />
				  <input type="hidden" name="Status" value="'.$value.'" />
				  <input type="submit" name="Valid" value="'.$submit.'" />
				</form>
			  </td>
			  <td align="right">'.$note[$nb].'&nbsp;</td>
			</tr>';

			$nb++;
		}

		$list .= '
		  <tr bgcolor="#F1F3F5">
			<td colspan="6">
			  <img src="./images/1.png" alt="1" title="Actif" /> Moyen de paiement disponible sur la boutique<br />
			  <img src="./images/0.png" alt="0" title="non actif" /> Moyen de paiement non disponible
			</td>
		  </tr>
		</table>

		</div>';

		return $list;
	}




	/** *****************************************************************************************************************************
	 * Methode formPaymentType
	 *
	 * @desc   Affichage du formulaire pour modifier l'action d'un mode de paiement
	 * @param  int $ID_PaymentType Identifiant du type de paiement
	 * @return string
	 * @access public
	 */
	public function formPaymentType($ID_PaymentType)
	{
		$sql = 'SELECT ID_PaymentType,
					   Line1,
					   LineText,
					   LineComplement,
					   '.$this->additionalFieldsForPaymentType($ID_PaymentType).'
					   PaymentType,
					   Status
				FROM PaymentType
				WHERE ID_PaymentType = '.$ID_PaymentType;

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

		// $Action  = 'Action_'.$result['Action'];
		// $$Action = ' checked="checked"';

		$form = '
		<div class="heading">Edition du mode de paiement : <b class="green">"'.$result['PaymentType'].'"</b></div>
		<div id="list">

		  <form action="" method="POST" style="margin: 0px">
			<input type="hidden" name="C1" value="CO">
			<input type="hidden" name="C2" value="MP">
			<input type="hidden" name="C3" value="update">
			<input type="hidden" name="ID_PaymentType" value="'.$ID_PaymentType.'">

			<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5">
			  <tr bgcolor="#F1F3F5">
				<td colspan="2" align="left">&nbsp;</b></td>
			  </tr>';
			// <tr bgcolor="#FFFFFF">
			  // <td align="right">Action :</td>
			  // <td align="left">
				// <input type="radio" name="Action" value="R"'.$Action_R.'>Redirection
				// <input type="radio" name="Action" value="A"'.$Action_A.'>Affichage de donn&eacute;es
			  // </td>
			// </tr>';

		$form .= $this->create_field('Texte explicatif :', array('R','T','L','M'), 'textarea', '', 'LineText', 0, 0, $result['LineText'], 80, 3);

		switch($ID_PaymentType) {
		case 2:
			/**
			 * Cheque
			 */
			$form .= $this->create_field('Compl&eacute;ment d\'infos :', array('R','T','L','M'), 'textarea', '', 'LineComplement', 0, 0, $result['LineComplement'], 80, 5);
			break;

		case 3:
			/**
			 * Contre remboursement
			 */
			break;

		case 4:
			/**
			 * Espece
			 */
			$form .= $this->create_field('Compl&eacute;ment d\'infos :', array('R','T','L','M'), 'textarea', '', 'LineComplement', 0, 0, $result['LineComplement'], 80, 5);
			break;

		case 5:
			/**
			 * Virement
			 */
			$form .= $this->create_field('Banque :', array('R','T','L','M'), 'input', '', 'LineBankName', 70, 70, $result['LineBankName']);
			$form .= $this->create_field('Coordonn&eacute;es bancaire :', array('R','T','L','M'), 'input', '', 'LineBankAccount', 70, 50, $result['LineBankAccount']);
			$form .= $this->create_field('Compl&eacute;ment d\'infos :', array('R','T','L','M'), 'textarea', '', 'LineComplement', 0, 0, $result['LineComplement'], 80, 5);
			break;

		case 6:
			/**
			 * PayPal
			 */
			$form .= $this->create_field('Adresse email du compte PayPal :', array('R','T','L','M'), 'input', '', 'LinePaypalAddr', 50, 50, $result['LinePaypalAddr']);
			break;
		}


		$form .= '
			  <tr>
				<td align="center" colspan="2" bgcolor="#F1F3F5">
				  <input type="submit" name="Valid" value="Enregistrer" />&nbsp;<b>::</b>&nbsp;<input type="submit" name="Cancel" value="Annuler" />
				</td>
			  </tr>
			</table>

		  </form>
		</div>';

		return $form;
	}




	/** *****************************************************************************************************************************
	 * Methode additionalFieldsForPaymentType
	 *
	 * @desc   Retourne une liste de champs additionnels pour la requete SQL permettant de recuperer les donnees a afficher
	 *         sur le mode de paiement choisi par le client
	 * @param  int $ID_PaymentType Identifiant du type de paiement
	 * @return string
	 * @access public
	 */
	public function additionalFieldsForPaymentType($ID_PaymentType)
	{
		switch($ID_PaymentType) {
		case 2:
			/**
			 * Cheque
			 */
			$sqlField = '';
			break;

		case 3:
			/**
			 * Contre remboursement
			 */
			$sqlField = '';
			break;

		case 4:
			/**
			 * Espece
			 */
			$sqlField = '';
			break;

		case 5:
			/**
			 * Virement
			 */
			$sqlField = 'LineBankName, LineBankAccount,';
			break;

		case 6:
			/**
			 * PayPal
			 */
			$sqlField = 'LinePaypalAddr,';
			break;

		default:
			$sqlField = '';
		}

		return $sqlField;
	}




	/** ****************************************************************************************************
	 * Methode updateTypePayment
	 *
	 * @desc   Met a jour le mode de paiement
	 * @param  array $data Donnees modifiees a enregistrer
	 * @return
	 * @access public
	 */
	public function updatePaymentType($data)
	{
		if ((isset($data['Status'])) and (($data['Status'] === '0') or ($data['Status'] === '1'))) {
			$field['Status'] = $data['Status'];
		} else {
			switch($data['ID_PaymentType']) {
			case 1:
				/**
				 * Carte bancaire
				 */
				$field['LineText'] = $data['LineText'];
				break;

			case 2:
				/**
				 * Cheque
				 */
				$field['LineText']        = $data['LineText'];
				$field['LineComplement']  = $data['LineComplement'];
				break;

			case 3:
				/**
				 * Contre remboursement
				 */
				$field['LineText'] = $data['LineText'];
				break;

			case 4:
				/**
				 * Espece
				 */
				$field['LineText']       = $data['LineText'];
				$field['LineComplement'] = $data['LineComplement'];
				break;

			case 5:
				/**
				 * Virement
				 */
				$field['LineText']        = $data['LineText'];
				$field['LineBankName']    = $data['LineBankName'];
				$field['LineBankAccount'] = $data['LineBankAccount'];
				$field['LineComplement']  = $data['LineComplement'];
				break;

			case 6:
				/**
				 * PayPal
				 */
				$field['LineText']       = $data['LineText'];
				$field['LinePaypalAddr'] = $data['LinePaypalAddr'];
				break;
			}
		}

		$this->update_sql('PaymentType', $field, $data['ID_PaymentType']);
	}






}
?>
