Current File : /home/karenpetzb/application/modules/backoffice/controllers/PromotionController.php
<?php
class Backoffice_PromotionController extends Modules_Backoffice_Controllers_MainController
{

	function init()
	{
		$this->view->title = "Administration";
		$this->view->currentMenu = "Promos";
		$this->isConnectedWithRole('isPromo'); 
	}
	function indexAction()
	{
		$this->_forward('/product');

	}

	function frontAction()
	{
		$this->view->titlePage = "Les promotions de la page d'Accueil";
		$promoFront = new PromoFront();
			
		$listFrontSQL = "
		SELECT p.NOM NOM, p.DESCRIPTIONSHORT DESCSHORT, p.ID IDPRODUCT,
				pf.REFERENCE REFERENCE, pf.POSITION POSITION, pf.ID ID
				FROM promo_front AS pf
				LEFT JOIN productchild AS pc ON pc.REFERENCE = pf.REFERENCE
				LEFT JOIN product AS p ON pc.IDPRODUCT = p.ID
				ORDER BY pf.POSITION ASC";
		$listFront = $promoFront->getAdapter()->fetchAll($listFrontSQL);
		$this->view->listFront = $listFront;
	}
	function frontaddAction()
	{
		if ($this->getRequest()->isPost()) {

			//filtres pour changer les chaines
			$filter = new Zend_Filter();
			$filter	->addFilter(new Zend_Filter_StripTags())
			->addFilter(new Zend_Filter_StringTrim());

			//valideurs pour les chaines
			$validator = new Zend_Validate();
			$validator -> addValidator(new Zend_Validate_NotEmpty());

			$promoFront = new PromoFront();
			$productChild = new ProductChild();

			$params = array();
			$params = $this->getRequest()->getPost();

			if ($validator->isValid($params['reference']))
			{
				$isExistProduct = $productChild->fetchRow("REFERENCE = '".$params['reference']."'");
				if ($isExistProduct ) {
					if ($isExistProduct['isPROMO'] == 0) {
						$isExistFront = $promoFront->fetchRow("REFERENCE = '".$params['reference']."'");
						if (!$isExistFront) {
							$date = new Zend_Date();
							$data = array (
		 		 						'REFERENCE' => $filter->filter($params['reference']),
										'POSITION' => (int)$params['position'],
 	 									'DATEINSERTPROMO' => $date->toString('YYYY-MM-dd HH-mm-ss')
							);
							$promoFront->insert($data);
						} else {
							$this->view->messageError = "Le produit est d�j� affich�";
						}
					} else {
						$this->view->messageError = "La r�f�rence n'est pas en promotion";
					}
				} else {
					$this->view->messageError = "La r�f�rence ".$params['reference']." n'existe pas.";
				}
			} else {
				foreach ($validator->getErrors() as $errorCode) {
					$this->view->messageError .=  $this->getErrorValidator($errorCode);
				}
			}
		}
		$this->_forward('front');
	}

	function fronteditAction() {
		if ($this->getRequest()->isPost()) {

			$promoFront = new PromoFront();
			$params = $this->getRequest()->getPost();

			$date = new Zend_Date();
			$data = array (
 	 						'DATEINSERTPROMO' => $date->toString('YYYY-MM-dd HH-mm-ss'),
					 		'POSITION' => (int)$params['position']
			);
			$promoFront->update($data,'ID = '.$params['promoid']);

			$this->view->messageSuccess = "Le produit est affich�";

		}
		$this->_forward('front');
	}
	function frontdelAction() {
		if ($this->getRequest()->getParam('id')) {
			$id = (int)$this->getRequest()->getParam('id');
			if ($id > 0 ) {
				$promoFront = new PromoFront();
				$promoFront->delete('ID = '.$id);
				$this->view->messageSuccess = "L'article n'est plus affich� sur la page d'accueil";
			}
		}
		$this->_forward('front');

	}

	function productAction() {
		$this->view->titlePage = "Les promotions sur les Produits";
		$supplierBrend = new SupplierBrend();
		$this->view->listbrend = $supplierBrend->fetchAll($supplierBrend->select()->order('BREND ASC'));

		$promoProduct = new PromoProduct();

		//Promo par article
		$listPromo1SQL = "
		SELECT p.NOM NOM, p.DESCRIPTIONSHORT DESCSHORT, p.ID ID, ppc.ID IDPROMO,
				pc.REFERENCE REFERENCE, ppc.REMISEEURO REMISEEURO, ppc.REMISEPOUR REMISEPOUR
				FROM promo_productchild AS ppc
				LEFT JOIN productchild AS pc ON pc.REFERENCE = ppc.REFERENCE
				LEFT JOIN product AS p ON pc.IDPRODUCT = p.ID
				WHERE ppc.REFERENCE IS NOT NULL
				ORDER BY pc.REFERENCE ASC";
		$listPromo1 = $promoProduct->getAdapter()->fetchAll($listPromo1SQL);
		$this->view->listPromo1 = $listPromo1;

		//Promotion sur X selon Y
		$listPromo2SQL = "
		SELECT ppc.REMISEEURO REMISEEURO, ppc.REMISEPOUR REMISEPOUR,ppc.ID IDPROMO,
				 ppc.CHILDREFBUY CHILDREFBUY, ppc.CHILDREFPROMO CHILDREFPROMO,
				 ppc.CHILDREFBUYNB CHILDREFBUYNB, ppc.CHILDREFPROMONB CHILDREFPROMONB
				 FROM promo_productchild AS ppc
				WHERE ppc.CHILDREFBUY IS NOT NULL 
				AND ppc.CHILDREFPROMO IS NOT NULL
				ORDER BY ppc.CHILDREFBUY, ppc.CHILDREFPROMO ASC";

		$this->view->listPromo2 = $promoProduct->getAdapter()->fetchAll($listPromo2SQL);

		//Promotion Date
		$listPromo3SQL = "
		SELECT ppc.DATEPROMOSTART DATEPROMOSTART, ppc.DATEPROMOEND DATEPROMOEND,ppc.ID IDPROMO,
			ppc.REMISEEURO REMISEEURO, ppc.REMISEPOUR REMISEPOUR
				FROM promo_productchild AS ppc
				WHERE ppc.DATEPROMOSTART IS NOT NULL 
				AND ppc.DATEPROMOEND IS NOT NULL 
				ORDER BY ppc.DATEPROMOSTART, ppc.DATEPROMOEND ASC";
		$this->view->listPromo3 = $promoProduct->getAdapter()->fetchAll($listPromo3SQL);

		//Promotion Gamme
		$listPromo4SQL = "
		SELECT c.NOM NOMCATEGORY, c.ID IDCATEGORY, ppc.ID IDPROMO,
			ppc.REMISEEURO REMISEEURO, ppc.REMISEPOUR REMISEPOUR
				FROM promo_productchild AS ppc
				LEFT JOIN category AS c ON c.ID = ppc.IDCATEGORY
				WHERE ppc.IDCATEGORY IS NOT NULL
				ORDER BY c.NOM ASC";
		$this->view->listPromo4 = $promoProduct->getAdapter()->fetchAll($listPromo4SQL);

		//Promotion Marque
		$listPromo5SQL = "
		SELECT sb.BREND BREND, sb.ID IDBREND, ppc.ID IDPROMO,
			ppc.REMISEEURO REMISEEURO, ppc.REMISEPOUR REMISEPOUR
				FROM promo_productchild AS ppc
				LEFT JOIN supplier_brend AS sb ON sb.ID = ppc.IDBREND
				WHERE ppc.IDBREND IS NOT NULL
				ORDER BY sb.BREND ASC";
		$this->view->listPromo5 = $promoProduct->getAdapter()->fetchAll($listPromo5SQL);


	}
	function productdelAction() {
        try {
		    if ($this->getRequest()->getParam('id')) {
			    $id = (int)$this->getRequest()->getParam('id');
			    if ($id > 0 ) {
				    $promoProduct = new PromoProduct();

				    $promoProduct->delete('ID = '.$id);
				    $this->view->messageSuccess = "La promotion a �t� supprim�e ";

			    } else {
				    $this->view->messageError = "La promotion n'a pas �t� supprim�e ";

			    }
		    }
		    if ($this->getRequest()->getParam('idref')) {
			    $idRef = (int)$this->getRequest()->getParam('idref');
			    if ($idRef > 0 ) {
				    $promoProduct = new PromoProduct();
				    $productChild = new ProductChild();

				    $resultSQL = "
				    SELECT p.NOM NOM,p.ID IDPRODUCT, p.isPROMO isPROMOPRODUCT, 
						    pp.ID, pp.REFERENCE REFERENCE
					    FROM promo_productchild AS pp
					    LEFT JOIN productchild AS pc ON pc.REFERENCE = pp.REFERENCE
					    LEFT JOIN product AS p ON pc.IDPRODUCT = p.ID
					    WHERE pp.ID = ".$idRef;
                        
				    $result = $promoProduct->getAdapter()->fetchRow($resultSQL);

				    $data = array (
					    'isPROMO' => 1
				    );
				    $productChild->update($data,"REFERENCE = '".$result['REFERENCE']."'");

				    $isLastPromo = $productChild->fetchRow("isPROMO = 0 AND IDPRODUCT = ".$result['IDPRODUCT']);
				    if (!$isLastPromo) {
					    $product = new Product();
					    $product->update($data,"ID = ".$result['IDPRODUCT']);
				    }
					
				    $promoProduct->delete('ID = '.$idRef);
				    $this->view->messageSuccess = "La promotion a �t� supprim�e ";

			    } else {
				    $this->view->messageError = "La promotion n'a pas �t� supprim�e ";

			    }
		    }
        } catch (Zend_Exception $e) {
			$this->log('productdelAction : '.$e->getMessage(),'err');
		}
		$this->_forward('product');
	}
	function productaddAction() {
        try {
		    if ($this->getRequest()->isPost()) {

			    //filtres pour changer les chaines
			    $filter = new Zend_Filter();
			    $filter	->addFilter(new Zend_Filter_StripTags())
			    ->addFilter(new Zend_Filter_StringTrim());

			    //valideurs pour les chaines
			    $validator = new Zend_Validate();
			    $validator -> addValidator(new Zend_Validate_NotEmpty());


			    $promoProduct = new PromoProduct();
			    $productChild = new ProductChild();

			    $params = array();
			    $params = $this->getRequest()->getPost();

			    $remiseeuro = !empty($params['remiseeuro']) ? $params['remiseeuro'] : 0;
			    $remisepour = (int)$params['remisepour'];
			    $isOK = true;
			    if ($remiseeuro > 0 && $remisepour > 0) {
				    $this->view->messageError = "La remise est soit en EURO, soit en POURCENTAGE. ";
				    $isOK = false;
			    }
			    if ($remiseeuro == 0 && $remisepour == 0) {
				    $this->view->messageError = "La remise est soit en EURO, soit en POURCENTAGE et est obligatoire. ";
				    $isOK = false;
			    }
			    if (($validator->isValid($remiseeuro) ||
			    $validator->isValid($remisepour)) && $isOK == true
			    ) {
				    switch ($params['promo']) {
					    case 1 :
						    if ($validator->isValid($params['reference'])) {

							    $isExistChild = $productChild->fetchRow("REFERENCE = '".$params['reference']."'");
							    $isExistPromo = $promoProduct->fetchRow("REFERENCE = '".$params['reference']."'");
							    if ($isExistChild) {
								    if ($isExistPromo) {
									    $this->view->messageError = "La r�f�rence : ".$params['reference']." est d�ja en promotion. ";
								    } else {
									    $date = new Zend_Date();

									    $data = array (
				 		 						    'DATEINSERTPROMO' => $date->toString('YYYY-MM-dd HH-mm-ss'),
												    'REMISEEURO' => $remiseeuro,
										 		    'REMISEPOUR' => $remisepour,
										 		    'REFERENCE' => $filter->filter($params['reference'])
									    );
									    $promoProduct->insert($data);

									    $product = new Product();
									    $data2 = array (
				 		 						    'isPROMO' => 0
									    );
									    $product->update($data2, 'ID = '.$isExistChild['IDPRODUCT']);
									    $productChild->update($data2, "REFERENCE = '".$isExistChild['REFERENCE']."'");

									    $this->view->messageSuccess = "La r�f�rence : ".$data['REFERENCE']." est maintenant est promotion. ";
								    }
							    } else {
								    $this->view->messageError = "La r�f�rence : ".$params['reference']." n'existe pas. ";
							    }
						    } else {
							    foreach ($validator->getErrors() as $errorCode) {
								    $this->view->messageError .=  $this->getErrorValidator($errorCode);
							    }
						    }
						    break;
					    case 2 :
						
						    if ($validator->isValid($params['referencebuy']) &&
						    $validator->isValid($params['referencepromo']) &&
						    ((int)$params['referencepromonb']>0) &&
						    ((int)$params['referencebuynb']>0)
						    ) {

							    $isExistChild1 = $productChild->fetchRow("REFERENCE = '".$params['referencebuy']."'");
							    $isExistChild2 = $productChild->fetchRow("REFERENCE = '".$params['referencepromo']."'");

							    if ($isExistChild1 && $isExistChild2 && $params['referencebuy'] != $params['referencepromo']) {
								    $isExistPromo = $promoProduct->fetchRow("CHILDREFBUY = '".$params['referencebuy']."' AND CHILDREFPROMO = '".$params['referencepromo']."'");
									
								    if ($isExistPromo) {
									    $this->view->messageError = "La r�f�rence : ".$params['referencepromo']." est d�j� en promotion. ";
								    } else {
									    $date = new Zend_Date();

									    $data = array (
				 		 						    'DATEINSERTPROMO' => $date->toString('YYYY-MM-dd HH-mm-ss'),
												    'REMISEEURO' => $remiseeuro,
										 		    'REMISEPOUR' => $remisepour,
										 		    'CHILDREFBUY' => $filter->filter($params['referencebuy']),
										 		    'CHILDREFPROMO' => $filter->filter($params['referencepromo']),
										 		    'CHILDREFPROMONB' => $filter->filter((int)$params['referencepromonb']),
										 		    'CHILDREFBUYNB' => $filter->filter((int)$params['referencebuynb'])
									    );
									    $promoProduct->insert($data);

									    $this->view->messageSuccess = "Si <b>".$data['CHILDREFBUYNB']."</b> articles de <b>".$data['CHILDREFBUY']."</b> sont achet�s alors <b>".$data['CHILDREFPROMONB']."</b> articles de <b>".$data['CHILDREFPROMO']."</b> sont en promotion ";
								    }
							    } else {
								    if ($params['referencebuy'] == $params['referencepromo']) {
									    $this->view->messageError = "Les r�f�rences sont identiques";

								    } else {
									    $this->view->messageError = "La r�f�rence : ".$params['referencebuy']." ou ".$params['referencepromo']." n'existe pas. ";

								    }
							    }
						    } else {
							    foreach ($validator->getErrors() as $errorCode) {
								    $this->view->messageError .=  $this->getErrorValidator($errorCode);
							    }
						    }
						    break;
					    case 3 :
						    $validator->addValidator(new Zend_Validate_Date());

						    if ($validator->isValid($params['sd']) &&
						    $validator->isValid($params['ed'])
						    ) {
							    $isExistPromo = $promoProduct->fetchRow("DATEPROMOSTART = '".$params['sd']."' AND DATEPROMOEND = '".$params['ed']."' ");

							    if ($isExistPromo) {
								    $this->view->messageError = "La promotion pour cette p�riode existe d�j� ";
							    } else {
								    $date = new Zend_Date();

								    $data = array (
			 		 						    'DATEINSERTPROMO' => $date->toString('YYYY-MM-dd HH-mm-ss'),
											    'REMISEEURO' => $remiseeuro,
									 		    'REMISEPOUR' => $remisepour,
									 		    'DATEPROMOSTART' => $filter->filter($params['sd']),
									 		    'DATEPROMOEND' => $filter->filter($params['ed'])
								    );
								    $promoProduct->insert($data);
									
								    $this->view->messageSuccess = "Les produits o� la date de promotion est comprise entre ".$params['sd']." et ".$params['ed']." sont en promotions. ";
							    }
						    } else {
							    foreach ($validator->getErrors() as $errorCode) {
								    $this->view->messageError .=  $this->getErrorValidator($errorCode);
							    }
						    }
						    break;
							
					    case 4 :
						    if ($params['listcategory'] > 0 ) {

							    $isExistPromo = $promoProduct->fetchRow("IDCATEGORY = ".$params['listcategory']);

							    if ($isExistPromo) {
								    $this->view->messageError = "La promotion pour cette gamme existe d�j� ";
							    } else {
								    $date = new Zend_Date();

								    $data = array (
			 		 						    'DATEINSERTPROMO' => $date->toString('YYYY-MM-dd HH-mm-ss'),
											    'REMISEEURO' => $remiseeuro,
									 		    'REMISEPOUR' => $remisepour,
									 		    'IDCATEGORY' => $filter->filter($params['listcategory'])
								    );
								    $promoProduct->insert($data);
									
								    $this->view->messageSuccess = "Les produits de la gamme sont maintenant en promotion. ";
							    }
						    } else {
							    $this->view->messageError = "S�lectionner une cat�gorie";

						    }
						    break;
							
					    case 5 :
						    if ($params['listbrend'] > 0 ) {

							    $isExistPromo = $promoProduct->fetchRow("IDBREND = ".$params['listbrend']);

							    if ($isExistPromo) {
								    $this->view->messageError = "La promotion pour cette marque existe d�j� ";
							    } else {
								    $date = new Zend_Date();

								    $data = array (
			 		 						    'DATEINSERTPROMO' => $date->toString('YYYY-MM-dd HH-mm-ss'),
											    'REMISEEURO' => $remiseeuro,
									 		    'REMISEPOUR' => $remisepour,
									 		    'IDBREND' => $filter->filter($params['listbrend'])
								    );
								    $promoProduct->insert($data);
									
								    $this->view->messageSuccess = "Les produits de la marque sont maintenant en promotion. ";
							    }
						    } else {
							    $this->view->messageError = "S�lectionner une marque";

						    }
						    break;
					    default:
						    $this->_forward('product');
						    break;
							
				    }
			    } else {
				    foreach ($validator->getErrors() as $errorCode) {
					    $this->view->messageError .=  $this->getErrorValidator($errorCode);
				    }
			    }
		    }
        } catch (Zend_Exception $e) {
			$this->log('productaddAction : '.$e->getMessage(),'err');
			$this->view->messageError = "V�rifier les param�tres";
		}
		$this->_forward('product');
	}

	function commandAction() {
		$this->view->titlePage = "Les remises sur les Commandes";
		$promoCommand = new PromoCommand();

		$isAll = $promoCommand->fetchRow('isALL = 0 AND isALL IS NOT NULL');
		if ($isAll) {
			$this->view->remiseAll = $isAll;
		}
		$isMontant = $promoCommand->fetchRow('MONTANTEQUAL > 0 AND MONTANTEQUAL IS NOT NULL');
		if ($isMontant) {
			$this->view->remiseMontant = $isMontant;
		}
		
		$listRemise1SQL = "
		SELECT p.NOM NOM,p.ID IDPRODUCT, p.DESCRIPTIONSHORT DESCSHORT, prc.ID ID,prc.CHILDNBR CHILDNBR,
				prc.CHILDREFERENCE CHILDREFERENCE, prc.REMISEEURO REMISEEURO, prc.REMISEPOUR REMISEPOUR
				FROM promo_command AS prc
				LEFT JOIN productchild AS pc ON pc.REFERENCE = prc.CHILDREFERENCE
				LEFT JOIN product AS p ON pc.IDPRODUCT = p.ID
				WHERE prc.CHILDREFERENCE IS NOT NULL
				ORDER BY prc.CHILDREFERENCE ASC";
		$listRemise1 = $promoCommand->getAdapter()->fetchAll($listRemise1SQL);
		$this->view->listRemise1 = $listRemise1;

	}

	function commandaddAction() {
		if ($this->getRequest()->isPost()) {

			//filtres pour changer les chaines
			$filter = new Zend_Filter();
			$filter	->addFilter(new Zend_Filter_StripTags())
			->addFilter(new Zend_Filter_StringTrim());

			//valideurs pour les chaines
			$validator = new Zend_Validate();
			$validator -> addValidator(new Zend_Validate_NotEmpty());


			$promoCommand = new PromoCommand();
			$productChild = new ProductChild();

			$params = array();
			$params = $this->getRequest()->getPost();

			if ($params['remise'] == '5') {
				if (isset($params['montantfrais']) && (int)$params['montantfrais'] > 0 && $validator->isValid($params['montantfrais'])) {
					$isExistFrais = $promoCommand->fetchRow("MONTANTVALID IS NOT NULL");
					if ($isExistFrais) {
						$date = new Zend_Date();

						$data = array (
  						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
  						'MONTANTVALID' => $filter->filter($params['montantfrais'])
						);
						$promoCommand->update($data,'ID = '.$isExistFrais['ID']);

						$this->view->messageSuccess = "La validit� de la commande a �t� modifi�e ";

					} else {
						$date = new Zend_Date();

						$data = array (
  						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
  						'MONTANTVALID' => $filter->filter($params['montantfrais'])
						);
						$promoCommand->insert($data);
						$this->view->messageSuccess = "La validit� de la commande a �t� modifi�e ";
					}
				} else {
					$this->view->messageError = "Le montant de la commande doit etre sup�rieur � 0";
				}
			}
			if (isset($params['remisepour']) && isset($params['remiseeuro'])) {
					
				$remiseeuro = (int)$params['remiseeuro'];
				$remisepour = (int)$params['remisepour'];
				$isOK = true;
				if ($remiseeuro > 0 && $remisepour > 0) {
					$this->view->messageError = "La remise est soit en EURO, soit en POURCENTAGE. ";
					$isOK = false;
				}
				if ($remiseeuro == 0 && $remisepour == 0) {
					$this->view->messageError = "La remise est soit en EURO, soit en POURCENTAGE et est obligatoire. ";
					$isOK = false;
				}
				if (($validator->isValid($remiseeuro) ||
				$validator->isValid($remisepour)) && $isOK == true
				) {
					switch ($params['remise']) {
						case 1 :
							$isExistAll = $promoCommand->fetchRow("isALL IS NOT NULL");
							if ($isExistAll) {
								$date = new Zend_Date();

								$data = array (
				 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
				 		 						'isALL' => '0',
												'REMISEEURO' => $filter->filter($params['remiseeuro']),
										 		'REMISEPOUR' => $filter->filter($params['remisepour'])
								);
								$promoCommand->update($data,'ID = '.$isExistAll['ID']);
									
								$this->view->messageSuccess = "Toutes les commandes ont maintenant une remise ";

							} else {
								$date = new Zend_Date();

								$data = array (
				 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
				 		 						'isALL' => '0',
												'REMISEEURO' => $filter->filter($params['remiseeuro']),
										 		'REMISEPOUR' => $filter->filter($params['remisepour'])
								);
								$promoCommand->insert($data);
								$this->view->messageSuccess = "Toutes les commandes ont maintenant une remise ";
							}

							break;
						case 2 :
							if ($validator->isValid($params['montant']) &&
							(int)$params['montant'] > 0
							) {
									
								$isExistMontant = $promoCommand->fetchRow("MONTANTEQUAL IS NOT NULL");

								if ($isExistMontant) {
									$date = new Zend_Date();

									$data = array (
				 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
				 		 						'MONTANTEQUAL' => $filter->filter($params['montant']),
												'REMISEEURO' => $filter->filter($params['remiseeuro']),
										 		'REMISEPOUR' => $filter->filter($params['remisepour'])
									);
									$promoCommand->update($data,'ID = '.$isExistMontant['ID']);

									$this->view->messageSuccess = "Toutes les commandes ont maintenant une remise ";

								} else {
									$date = new Zend_Date();

									$data = array (
				 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
				 		 						'MONTANTEQUAL' => $filter->filter($params['montant']),
												'REMISEEURO' => $filter->filter($params['remiseeuro']),
										 		'REMISEPOUR' => $filter->filter($params['remisepour'])
									);
									$promoCommand->insert($data);
									$this->view->messageSuccess = "Toutes les commandes ont maintenant une remise ";
								}
							} else {
								foreach ($validator->getErrors() as $errorCode) {
									$this->view->messageError .=  $this->getErrorValidator($errorCode);
								}
							}
							break;
						case 3 :
							if ($validator->isValid($params['nbrreference']) &&
							$validator->isValid($params['reference']) &&
							(int)$params['nbrreference'] > 0
							) {
									
								$isExistReferenceChild = $productChild->fetchRow("REFERENCE = '".$params['reference']."' ");
								if ($isExistReferenceChild) {
									$isExistReference = $promoCommand->fetchRow("CHILDREFERENCE = '".$params['reference']."' ");

									if ($isExistReference) {
										$this->view->messageError = "Les commandes ont d�j� une remise sur cet article ";

									} else {
										$date = new Zend_Date();

										$data = array (
						 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
						 		 						'CHILDREFERENCE' => $filter->filter($params['reference']),
						 		 						'CHILDNBR' => $filter->filter($params['nbrreference']),
														'REMISEEURO' => $filter->filter($params['remiseeuro']),
												 		'REMISEPOUR' => $filter->filter($params['remisepour'])
										);
										$promoCommand->insert($data);
										$this->view->messageSuccess = "Toutes les commandes ont maintenant une remise ";
									}
								} else {
									$this->view->messageError =  "La r�f�rence ".$params['reference']." n'existe pas";

								}
							} else {
								foreach ($validator->getErrors() as $errorCode) {
									$this->view->messageError .=  $this->getErrorValidator($errorCode);
								}
								if ((int)$params['nbrreference'] == 0) {
									$this->view->messageError .=  "Le nombre d'articles doit etre sup�rieur � 0";

								}
							}
							break;
						case 4 :
							if (isset($params['montantfrais']) && (int)$params['montantfrais'] > 0) {
								$isExistFrais = $promoCommand->fetchRow("MONTANTFRAISCMD IS NOT NULL AND MONTANTFRAISCMD = ".$params['montantfrais']);
								if ($isExistFrais) {
									$date = new Zend_Date();

									$data = array (
	 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
	 		 						'MONTANTFRAISCMD' => $filter->filter($params['montantfrais']),
									'REMISEEURO' => $filter->filter($params['remiseeuro']),
									'REMISEPOUR' => $filter->filter($params['remisepour'])
									);
									$promoCommand->update($data,'ID = '.$isExistFrais['ID']);

									$this->view->messageSuccess = "Les frais de livraison sont modifi�s ";

								} else {
									$date = new Zend_Date();

									$data = array (
	 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
	 		 						'MONTANTFRAISCMD' => $filter->filter($params['montantfrais']),
									'REMISEEURO' => $filter->filter($params['remiseeuro']),
									'REMISEPOUR' => $filter->filter($params['remisepour'])
									);
									$promoCommand->insert($data);
									$this->view->messageSuccess = "Les frais de livraison sont modifi�s ";
								}
							} else {
								$this->view->messageError = "Le montant de la commande doit etre sup�rieur � 0";
							}
							break;
						default:
							$this->_forward('command');
							break;

					}
				} else {
					foreach ($validator->getErrors() as $errorCode) {
						$this->view->messageError .=  $this->getErrorValidator($errorCode);
					}
				}
			}
		}
		$this->_forward('command');
	}
	function commanddelAction() {
		if ($this->getRequest()->getParam('id')) {
			$id = (int)$this->getRequest()->getParam('id');
			if ($id > 0 ) {
				$promoCommand = new PromoCommand();
				$promoCommand->delete('ID = '.$id);
				$this->view->messageSuccess = "La remise a �t� supprim�e ";
			} else {
				$this->view->messageError = "La remise n'a pas �t� supprim�e ";

			}
		}
		$this->_forward('command');
	}
	function userAction() {
		$this->view->titlePage = "Les remises des Clients";
		$promoUser = new PromoUser();
		$user = new User();
		$supplierBrend = new SupplierBrend();
		$this->view->listbrend = $supplierBrend->fetchAll($supplierBrend->select()->order('BREND ASC'));

		$codeIntern = new CodeIntern();
		$this->view->listAllCIntern = $codeIntern->fetchAll($codeIntern->select()->order('CODE ASC'));

		$listAllUserSQL = "
		SELECT u.NOM NOM,u.ID IDUSER, u.PRENOM PRENOM
				FROM user AS u 
				ORDER BY u.NOM, u.PRENOM ASC";
		$listAllUser = $user->getAdapter()->fetchAll($listAllUserSQL);
		if ($listAllUser) {
			$this->view->listAllUser = $listAllUser;
		}

		$listUserSQL = "
		SELECT u.NOM NOM,u.ID IDUSER, u.PRENOM PRENOM, 
				pu.ID ID, pu.REMISEEURO REMISEEURO, pu.REMISEPOUR REMISEPOUR
				FROM promo_user AS pu
				LEFT JOIN user AS u ON u.ID = pu.IDUSER
				WHERE pu.IDUSER IS NOT NULL
				ORDER BY u.NOM, u.PRENOM ASC";
		$listUser = $promoUser->getAdapter()->fetchAll($listUserSQL);
		if ($listUser) {
			$this->view->listUser = $listUser;
		}

		$isDateInsc = $promoUser->fetchRow('isDATEINSCR = 0');
		if ($isDateInsc) {
			$this->view->dateInsc = $isDateInsc;
		}
		$isFirstCmd = $promoUser->fetchRow('isFIRSTCMD = 0');
		if ($isFirstCmd) {
			$this->view->firstCmd = $isFirstCmd;
		}

		$listUserBrendSQL = "
		SELECT u.NOM NOM,u.ID IDUSER, u.PRENOM PRENOM, 
				pu.ID ID, pu.REMISEEURO REMISEEURO, pu.REMISEPOUR REMISEPOUR, pu.USERIDBREND USERIDBREND, pu.USERBRENDID USERBRENDID, sb.BREND BREND, sb.IDSUPPLIER IDSUPPLIER
				FROM promo_user AS pu
				LEFT JOIN user AS u ON u.ID = pu.USERIDBREND
				LEFT JOIN supplier_brend AS sb ON sb.ID = pu.USERBRENDID
				WHERE pu.USERIDBREND IS NOT NULL
				AND pu.USERBRENDID IS NOT NULL
				ORDER BY u.NOM, u.PRENOM ASC";
		$listUserBrend = $promoUser->getAdapter()->fetchAll($listUserBrendSQL);
		if ($listUserBrend) {
			$this->view->listUserBrend = $listUserBrend;
		}

		$listCinternBrendSQL = "
		SELECT pu.ID ID, pu.REMISEEURO REMISEEURO, pu.REMISEPOUR REMISEPOUR, uci.LABEL LABELINTERN, uci.CODE CODEINTERN,
			pu.CINTERNIDBREND CINTERNIDBREND, sb.BREND BREND, pu.CINTERNBRENDID CINTERNBRENDID, sb.IDSUPPLIER IDSUPPLIER
				FROM promo_user AS pu
				LEFT JOIN user_cintern AS uci ON uci.ID = pu.CINTERNIDBREND
				LEFT JOIN supplier_brend AS sb ON sb.ID = pu.CINTERNBRENDID
				WHERE pu.CINTERNIDBREND IS NOT NULL 
				AND pu.CINTERNBRENDID IS NOT NULL 
				ORDER BY uci.CODE,sb.BREND ASC";
		$listCinternBrend = $promoUser->getAdapter()->fetchAll($listCinternBrendSQL);
		if ($listCinternBrend) {
			$this->view->listCinternBrend = $listCinternBrend;
		}



	}
	function useraddAction() {
		if ($this->getRequest()->isPost()) {

			//filtres pour changer les chaines
			$filter = new Zend_Filter();
			$filter	->addFilter(new Zend_Filter_StripTags())
			->addFilter(new Zend_Filter_StringTrim());

			//valideurs pour les chaines
			$validator = new Zend_Validate();
			$validator -> addValidator(new Zend_Validate_NotEmpty());


			$promoUser = new PromoUser();

			$params = array();
			$params = $this->getRequest()->getPost();

			$remiseeuro = (int)$params['remiseeuro'];
			$remisepour = (int)$params['remisepour'];
			$isOK = true;
			if ($remiseeuro > 0 && $remisepour > 0) {
				$this->view->messageError = "La remise est soit en EURO, soit en POURCENTAGE. ";
				$isOK = false;
			}
			if ($remiseeuro == 0 && $remisepour == 0) {
				$this->view->messageError = "La remise est soit en EURO, soit en POURCENTAGE et est obligatoire. ";
				$isOK = false;
			}
			if (($validator->isValid($remiseeuro) ||
			$validator->isValid($remisepour)) && $isOK == true
			) {
				switch ($params['remise']) {
					case 1 :
						$isExistAnnif = $promoUser->fetchRow("isDATEINSCR IS NOT NULL");
						if ($isExistAnnif) {
							$date = new Zend_Date();

							$data = array (
	 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
	 		 						'isDATEINSCR' => '0',
									'REMISEEURO' => $filter->filter($params['remiseeuro']),
							 		'REMISEPOUR' => $filter->filter($params['remisepour'])
							);
							$promoUser->update($data,'ID = '.$isExistAnnif['ID']);

							$this->view->messageSuccess = "Tout les clients ont maintenant une remise selon leurs dates d'inscription";

						} else {
							$date = new Zend_Date();

							$data = array (
	 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
	 		 						'isDATEINSCR' => '0',
									'REMISEEURO' => $filter->filter($params['remiseeuro']),
							 		'REMISEPOUR' => $filter->filter($params['remisepour'])
							);
							$promoUser->insert($data);
							$this->view->messageSuccess = "Tout les clients ont maintenant une remise selon leurs dates d'inscription";

						}

						break;
					case 2 :
						$isExistFirst = $promoUser->fetchRow("isFIRSTCMD IS NOT NULL");
						if ($isExistFirst) {
							$date = new Zend_Date();

							$data = array (
	 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
	 		 						'isFIRSTCMD' => '0',
									'REMISEEURO' => $filter->filter($params['remiseeuro']),
							 		'REMISEPOUR' => $filter->filter($params['remisepour'])
							);
							$promoUser->update($data,'ID = '.$isExistFirst['ID']);

							$this->view->messageSuccess = "Tout les clients ont maintenant une remise si c'est la premi�re commande";

						} else {
							$date = new Zend_Date();

							$data = array (
	 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
	 		 						'isFIRSTCMD' => '0',
									'REMISEEURO' => $filter->filter($params['remiseeuro']),
							 		'REMISEPOUR' => $filter->filter($params['remisepour'])
							);
							$promoUser->insert($data);
							$this->view->messageSuccess = "Tout les clients ont maintenant une remise si c'est la premi�re commande";

						}

						break;
					case 3 :
						$iduser = (int)$params['iduser'];
						if ($iduser > 0) {
							$isExistUser = $promoUser->fetchRow("IDUSER IS NOT NULL AND IDUSER = ".$iduser);
							if ($isExistUser) {
								$date = new Zend_Date();

								$data = array (
		 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
		 		 						'IDUSER' => (int)$params['iduser'],
										'REMISEEURO' => $filter->filter($params['remiseeuro']),
								 		'REMISEPOUR' => $filter->filter($params['remisepour'])
								);
								$promoUser->update($data,'ID = '.$isExistUser['ID']);

								$this->view->messageSuccess = "Le client � maintenant une remise sur sa prochaine commande";

							} else {
								$date = new Zend_Date();

								$data = array (
		 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
		 		 						'IDUSER' => (int)$params['iduser'],
										'REMISEEURO' => $filter->filter($params['remiseeuro']),
								 		'REMISEPOUR' => $filter->filter($params['remisepour'])
								);
								$promoUser->insert($data);
								$this->view->messageSuccess = "Le client � maintenant une remise sur sa prochaine commande";

							}
						} else {
							$this->view->messageError = "S�lectionner un utilisateur";

						}
						break;

					case 4 :
						$iduser = (int)$params['iduser'];
						$idbrend = (int)$params['idbrend'];
						if ($iduser > 0 && $idbrend > 0) {
							$isExistUserBrend = $promoUser->fetchRow("USERIDBREND IS NOT NULL AND USERIDBREND = ".$iduser." AND USERBRENDID IS NOT NULL AND USERBRENDID = ".$idbrend);
							if ($isExistUserBrend) {
								$date = new Zend_Date();

								$data = array (
		 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
		 		 						'USERIDBREND' => $iduser,
		 		 						'USERBRENDID' => $idbrend,
										'REMISEEURO' => $filter->filter($params['remiseeuro']),
								 		'REMISEPOUR' => $filter->filter($params['remisepour'])
								);
								$promoUser->update($data,'ID = '.$isExistUserBrend['ID']);

								$this->view->messageSuccess = "Le client � maintenant une remise sur les produits de la marque";

							} else {
								$date = new Zend_Date();

								$data = array (
		 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
		 		 						'USERIDBREND' => $iduser,
		 		 						'USERBRENDID' => $idbrend,
										'REMISEEURO' => $filter->filter($params['remiseeuro']),
								 		'REMISEPOUR' => $filter->filter($params['remisepour'])
								);
								$promoUser->insert($data);
								$this->view->messageSuccess = "Le client � maintenant une remise sur les produits de la marque";

							}
						} else {
							$this->view->messageError = "S�lectionner un client et une marque";

						}
						break;

					case 5 :
						$idcintern = (int)$params['idcintern'];
						$idbrend = (int)$params['idbrend'];
						if ($idcintern > 0 && $idbrend > 0) {
							$isExistCinternBrend = $promoUser->fetchRow("CINTERNIDBREND IS NOT NULL AND CINTERNIDBREND = ".$idcintern." AND CINTERNBRENDID IS NOT NULL AND CINTERNBRENDID = ".$idbrend);
							if ($isExistCinternBrend) {
								$date = new Zend_Date();

								$data = array (
		 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
		 		 						'CINTERNIDBREND' => $idcintern,
		 		 						'CINTERNBRENDID' => $idbrend,
										'REMISEEURO' => $filter->filter($params['remiseeuro']),
								 		'REMISEPOUR' => $filter->filter($params['remisepour'])
								);
								$promoUser->update($data,'ID = '.$isExistCinternBrend['ID']);

								$this->view->messageSuccess = "Le code interne a maintenant une remise sur les produits de la marque";

							} else {
								$date = new Zend_Date();

								$data = array (
		 		 						'DATEINSERTREMISE' => $date->toString('YYYY-MM-dd HH-mm-ss'),
		 		 						'CINTERNIDBREND' => $idcintern,
		 		 						'CINTERNBRENDID' => $idbrend,
										'REMISEEURO' => $filter->filter($params['remiseeuro']),
								 		'REMISEPOUR' => $filter->filter($params['remisepour'])
								);
								$promoUser->insert($data);
								$this->view->messageSuccess = "Le code interne � maintenant une remise sur les produits de la marque";

							}
						} else {
							$this->view->messageError = "S�lectionner un code interne et une marque";

						}
						break;
					default:
						$this->_forward('user');
						break;

				}
			} else {
				foreach ($validator->getErrors() as $errorCode) {
					$this->view->messageError .=  $this->getErrorValidator($errorCode);
				}
			}
		}
		$this->_forward('user');
	}
	function userdelAction() {
		if ($this->getRequest()->getParam('id')) {
			$id = (int)$this->getRequest()->getParam('id');
			if ($id > 0 ) {
				$promoUser = new PromoUser();
				$promoUser->delete('ID = '.$id);
				$this->view->messageSuccess = "La remise a �t� supprim�e ";
			} else {
				$this->view->messageError = "La remise n'a pas �t� supprim�e ";

			}
		}
		$this->_forward('user');
	}
	function giftAction() {
		$this->view->titlePage = "Les Avantages";
		$promoGift = new PromoGift();

		$listProductGiftSQL = "
		SELECT pg.PRODREFBUY PRODREFBUY, pg.PRODREFGIFT PRODREFGIFT,pg.ID ID,
				pg.PRODNBBUY PRODNBBUY,pg.PRODNBGIFT PRODNBGIFT 
				 FROM promo_gift AS pg
				WHERE pg.PRODREFBUY IS NOT NULL 
				AND pg.PRODREFGIFT IS NOT NULL
				ORDER BY pg.PRODREFBUY, pg.PRODREFGIFT ASC";
		$listProductGift = $promoGift->getAdapter()->fetchAll($listProductGiftSQL);
		$this->view->listProductGift = $listProductGift;

		$listCmdGiftSQL = "
		SELECT pg.CMDMONTANT CMDMONTANT, pg.CMDPRODREFGIFT CMDPRODREFGIFT,pg.ID ID, pc.IDPRODUCT
				 FROM promo_gift AS pg
				 LEFT JOIN productchild AS pc ON pc.REFERENCE = pg.CMDPRODREFGIFT
				WHERE pg.CMDMONTANT IS NOT NULL 
				AND pg.CMDPRODREFGIFT IS NOT NULL
				ORDER BY pg.CMDPRODREFGIFT ASC";
		$listCmdGift = $promoGift->getAdapter()->fetchAll($listCmdGiftSQL);
		$this->view->listCmdGift = $listCmdGift;
	}

	function giftaddAction() {
		if ($this->getRequest()->isPost()) {
			//filtres pour changer les chaines
			$filter = new Zend_Filter();
			$filter	->addFilter(new Zend_Filter_StripTags())
			->addFilter(new Zend_Filter_StringTrim());

			//valideurs pour les chaines
			$validator = new Zend_Validate();
			$validator -> addValidator(new Zend_Validate_NotEmpty());


			$params = $this->getRequest()->getPost();
			$promoGift = new PromoGift();
			$productChild = new ProductChild();

			switch ($params['gift']) {
				case 1 :
					$buy = $filter->filter($params['referencebuy']);
					$nbbuy = $filter->filter((int)$params['referencenbbuy']);
					$gift = $filter->filter($params['referencegift']);
					$nbgift = $filter->filter((int)$params['referencenbgift']);
					if ($validator->isValid($buy) &&
					$validator->isValid($gift) && 
					((int)$nbbuy>0) && 
					((int)$nbgift>0) ) {
						$isExistBuy = $productChild->fetchRow("REFERENCE = '".$buy."' ");

						if ($isExistBuy) {
							$isExistGift = $productChild->fetchRow("REFERENCE = '".$gift."' ");
							if ($isExistGift) {
								$isExistPromo = $promoGift->fetchRow("PRODREFBUY = '".$buy."' AND PRODREFGIFT = '".$gift."'");
								if (!$isExistPromo) {
									$date = new Zend_Date();
									$data = array (
			 		 						'DATEINSERTGIFT' => $date->toString('YYYY-MM-dd HH-mm-ss'),
			 		 						'PRODREFBUY' => $buy,
											'PRODREFGIFT' => $gift,
											'PRODNBGIFT' => $nbgift,
											'PRODNBBUY' => $nbbuy
									);

									$promoGift->insert($data);
									$this->view->messageSuccess = "L'avantage est maintenant disponible";
								} else {
									$this->view->messageError = "L'avantage existe d�j�";
								}
							} else {
								$this->view->messageError = "La r�f�rence ".$params['referencegift']." n'existe pas";
							}
						} else {
							$this->view->messageError = "La r�f�rence ".$params['referencebuy']." n'existe pas";
						}

					} else {
						foreach ($validator->getErrors() as $errorCode) {
							$this->view->messageError .=  $this->getErrorValidator($errorCode);
						}
					}
					break;
				case 2 :
					$reference = $filter->filter($params['reference']);
					if ($validator->isValid($params['montant']) &&
					$validator->isValid($reference)) {
						if ((int)$params['montant'] > 0) {
							$isExistGift = $productChild->fetchRow("REFERENCE = '".$reference."' ");

							if ($isExistGift) {
								$isExistPromo = $promoGift->fetchRow("CMDMONTANT = '".$params['montant']."' AND CMDPRODREFGIFT = '".$reference."'");
								if (!$isExistPromo) {
									$date = new Zend_Date();
									$data = array (
			 		 						'DATEINSERTGIFT' => $date->toString('YYYY-MM-dd HH-mm-ss'),
			 		 						'CMDMONTANT' => $filter->filter($params['montant']),
											'CMDPRODREFGIFT' => $reference
									);

									$promoGift->insert($data);
									$this->view->messageSuccess = "L'avantage est maintenant disponible";
								} else {
									$this->view->messageError = "L'avantage existe d�j�";
								}
							} else {
								$this->view->messageError = "La r�f�rence ".$reference." n'existe pas";
							}
						} else {
							$this->view->messageError = "Le montant doit etre > 0";
						}
					} else {
						foreach ($validator->getErrors() as $errorCode) {
							$this->view->messageError .=  $this->getErrorValidator($errorCode);
						}
					}
					break;

			}
		}
		$this->_forward('gift');
	}

	function giftdelAction() {
		if ($this->getRequest()->getParam('id')) {
			$id = (int)$this->getRequest()->getParam('id');
			if ($id > 0 ) {
				$promoGift = new PromoGift();
				$promoGift->delete('ID = '.$id);
				$this->view->messageSuccess = "L'avantage a �t� supprim�e ";
			} else {
				$this->view->messageError = "L'avantage n'a pas �t� supprim�e ";

			}
		}
		$this->_forward('gift');
	}
	
	function codereductionAction(){
		$this->view->titlePage = "Les Codes de r�duction";
		$codeReduction = new CodeReduction();
		$listCodes = $codeReduction->getAllCodes();
		$this->view->listReduc = $listCodes;
	}
	
	private function passgen() {
		$chaine ="mnoTUzS5678kVvwxy9WXYZRNCDEFrslq41GtuaHIJKpOPQA23LcdefghiBMbj0";
		srand((double)microtime()*1000000);
		for($i=0; $i<8; $i++){
			@$pass .= $chaine[rand()%strlen($chaine)];
		}
		return $pass;
	}
	
	function addcodereductionproduitAction(){
		if ($this->getRequest()->isPost()) {
			//filtres pour changer les chaines
			$filter = new Zend_Filter();
			$filter	->addFilter(new Zend_Filter_StripTags())
			->addFilter(new Zend_Filter_StringTrim());

			//valideurs pour les chaines
			$validator = new Zend_Validate();
			$validator -> addValidator(new Zend_Validate_NotEmpty());

			$params = array();
			$params = $this->getRequest()->getPost();
			
			$codereduc_euro = $params['codereduc_euro'];
			$codereduc_pour = (int)$params['codereduc_pour'];
			$isOK = true;
			if ($codereduc_euro > 0 && $codereduc_pour > 0) {
				$this->view->messageError = "La r�duction est soit en EURO, soit en POURCENTAGE. ";
				$isOK = false;
			}
			if ($codereduc_euro == 0 && $codereduc_pour == 0) {
				$this->view->messageError = "La r�duction est soit en EURO, soit en POURCENTAGE et est obligatoire. ";
				$isOK = false;
			}
			
			$codereduc_ref = $params['codereduc_ref'];
			$codereduc_nbr = (int)$params['codereduc_nbr'];
			$sd = $params['sd2'];
			$ed = $params['ed2'];
			
			if (($validator->isValid($codereduc_euro) ||
			$validator->isValid($codereduc_pour)) && 
			$isOK == true &&
			$validator->isValid($codereduc_ref) && 
			$validator->isValid($codereduc_nbr) 
			) {
				$validator2 = new Zend_Validate();
				$validator2->addValidator(new Zend_Validate_Date());

				if ($validator2->isValid($sd) &&
				$validator2->isValid($ed)
				) {
					$code = $this->passgen();
					$labelProd = "produit";
					$labelDroit = "donne";
					if ($codereduc_nbr > 1) { $labelProd .= "s"; $labelDroit .= "nt"; }
					$labelPrix = "";
					if ($codereduc_pour > 0) { $labelPrix = $codereduc_pour. " %";
					} else { $labelPrix = $codereduc_euro. " euros"; }
					$libelle = $codereduc_nbr." ".$labelProd." de la r�ference : ".$codereduc_ref." ".$labelDroit." droit � ".$labelPrix." de r�duction. ";
					
					$codeReduction = new CodeReduction();
					$data = array(
						"CODE" => $code,
						"LIBELLE" => $libelle,
						"PRODUITREF" => $codereduc_ref,
						"PRODUITNBR" => $codereduc_nbr,
						"EURO" => $codereduc_euro,
						"POUR" => $codereduc_pour,
						"DATESTART" => $sd,
						"DATEEND" => $ed,
						"isACTIF" => 1
					);
					$codeReduction->insert($data);
					$this->view->messageSuccess = "CODE : ".$code." : ".$libelle;
				} else {
					foreach ($validator->getErrors() as $errorCode) {
						$this->view->messageError .=  $this->getErrorValidator($errorCode);
					}
				}
			} else {
				foreach ($validator->getErrors() as $errorCode) {
					$this->view->messageError .=  $this->getErrorValidator($errorCode);
				}
			}
		}
		$this->_forward('codereduction');
	}
	
	function addcodereductioncommandeAction(){
		if ($this->getRequest()->isPost()) {
			//filtres pour changer les chaines
			$filter = new Zend_Filter();
			$filter	->addFilter(new Zend_Filter_StripTags())
			->addFilter(new Zend_Filter_StringTrim());

			//valideurs pour les chaines
			$validator = new Zend_Validate();
			$validator -> addValidator(new Zend_Validate_NotEmpty());

			$params = array();
			$params = $this->getRequest()->getPost();
			
			$codereduc_euro = $params['codereduc_euro'];
			$codereduc_pour = (int)$params['codereduc_pour'];
			$isOK = true;
			if ($codereduc_euro > 0 && $codereduc_pour > 0) {
				$this->view->messageError = "La r�duction est soit en EURO, soit en POURCENTAGE. ";
				$isOK = false;
			}
			if ($codereduc_euro == 0 && $codereduc_pour == 0) {
				$this->view->messageError = "La r�duction est soit en EURO, soit en POURCENTAGE et est obligatoire. ";
				$isOK = false;
			}
			
			$codereduc_cmdmin = $params['codereduc_cmdmin'];
			$sd = $params['sd1'];
			$ed = $params['ed1'];
			
		if (($validator->isValid($codereduc_euro) ||
			$validator->isValid($codereduc_pour)) && 
			$isOK == true &&
			$validator->isValid($codereduc_cmdmin) 
			) {
				$validator2 = new Zend_Validate();
				$validator2->addValidator(new Zend_Validate_Date());

				if ($validator2->isValid($sd) &&
				$validator2->isValid($ed)
				) {
					if ($codereduc_cmdmin > 0) {
						
						$code = $this->passgen();
						$labelPrix = "";
						if ($codereduc_pour > 0) { $labelPrix = $codereduc_pour. " %";
						} else { $labelPrix = $codereduc_euro. " euros"; }
						$libelle = "Pour ".$codereduc_cmdmin." euros de commande, il y a ".$labelPrix." de r�duction. ";
					
						$codeReduction = new CodeReduction();
						$data = array(
							"CODE" => $code,
							"LIBELLE" => $libelle,
							"CMDTOTAL" => $codereduc_cmdmin,
							"EURO" => $codereduc_euro,
							"POUR" => $codereduc_pour,
							"DATESTART" => $sd,
							"DATEEND" => $ed,
							"isACTIF" => 1
						);
						$codeReduction->insert($data);
						$this->view->messageSuccess = "CODE : ".$code."<br/> ".$libelle;
					}
				} else {
					foreach ($validator->getErrors() as $errorCode) {
						$this->view->messageError .=  $this->getErrorValidator($errorCode);
					}
				}
			} else {
				foreach ($validator->getErrors() as $errorCode) {
					$this->view->messageError .=  $this->getErrorValidator($errorCode);
				}
			}
		}
		$this->_forward('codereduction');
	}
	
	
	function delcodereductionAction() {
		if ($this->getRequest()->getParam('id')) {
			$id = (int)$this->getRequest()->getParam('id');
			if ($id > 0 ) {
				$codeReduction = new CodeReduction();
				$codeReduction->delete('ID = '.$id);
				$this->view->messageSuccess = "Le code de r�duction a �t� supprim�e ";
			} else {
				$this->view->messageError = "Le code de r�duction n'a pas �t� supprim�e ";

			}
		}
		$this->_forward('codereduction');
	}

	function activecodereductionAction() {
		if ($this->getRequest()->getParam('id')) {
			$id = (int)$this->getRequest()->getParam('id');
			if ($id > 0 ) {
				$codeReduction = new CodeReduction();
				$data = array(
					"isACTIF" => 1
				);
				$codeReduction->update($data,'ID = '.$id);
				$this->view->messageSuccess = "Le code de r�duction est actif ";
			} 
		}
		$this->_forward('codereduction');
	}

	function unactivecodereductionAction() {
		if ($this->getRequest()->getParam('id')) {
			$id = (int)$this->getRequest()->getParam('id');
			if ($id > 0 ) {
				$codeReduction = new CodeReduction();
				$data = array(
					"isACTIF" => 0
				);
				$codeReduction->update($data,'ID = '.$id);
				$this->view->messageSuccess = "Le code de r�duction est inactif ";
			}
		}
		$this->_forward('codereduction');
	}
}
?>