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

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

	public function listAction() {
		$this->view->titlePage = "G�n�ralit�s";
		$promoCommand = new PromoCommand();

		$isFrais = $promoCommand->fetchAll('MONTANTFRAISCMD > 0 AND MONTANTFRAISCMD IS NOT NULL','MONTANTFRAISCMD ASC');
		if ($isFrais) {
			$this->view->remiseFrais = $isFrais;
		}
		$isValide = $promoCommand->fetchRow('MONTANTVALID > 0 AND MONTANTVALID IS NOT NULL');
		if ($isValide) {
			$this->view->validCommand = $isValide;
		}
		
		
		$supplierBrend = new SupplierBrend();
		$this->view->listbrend = $supplierBrend->fetchAll($supplierBrend->select()->order('BREND ASC'));
		
	}

	public function addpricecategoryAction() {
		if($this->_request->isPost()) {
			$params = $this->_request->getPost();
			try {
				$idCatSelected = $params['listcategory'];
				$pricesign = $params['pricesign'];
				$pricepour = $params['pricepour'];

				if ($idCatSelected != 'none') {
					$pricepour = intval($pricepour);
					if ($pricepour > 0) {
						$category = new Category();
						$dataID = "";
						if ($idCatSelected == 0) {
							$data = $category->fetchAll();
							foreach ($data as $row) {
								if ($dataID == "") {
									$dataID = $row['ID'];
								} else {
									$dataID .= " , ".$row['ID'];
								}
							}
						} else {
							$dataID = $category->getAllSubsIDByIDToString($idCatSelected);
						}

						$product = new Product();
						$productChild = new ProductChild();

						$listProducts = $product->fetchAll('IDCATEGORY IN ('.$dataID.')');
						foreach ($listProducts as $rowProduct) {
							$actualProductPrice = $rowProduct['PRIX'];
							if ($pricesign == '+') {
								$newProductPrice = $rowProduct['PRIX'] + (($rowProduct['PRIX'] * $pricepour) / 100);
							} else {
								$newProductPrice = $rowProduct['PRIX'] - (($rowProduct['PRIX'] * $pricepour) / 100);
							}
							$newProductPrice = sprintf("%.2f",$newProductPrice);
							$dataProductUpdated = array();
							$dataProductUpdated['PRIX'] = $newProductPrice;
							$product->update($dataProductUpdated,'ID = '.$rowProduct['ID']);
								
							$listChilds = $productChild->fetchAll('IDPRODUCT = '.$rowProduct['ID']);
							foreach ($listChilds as $rowChild) {
								$actualPrice = $rowChild['PRIX'];
								if ($pricesign == '+') {
									$newPrice = $rowChild['PRIX'] + (($rowChild['PRIX'] * $pricepour) / 100);
								} else {
									$newPrice = $rowChild['PRIX'] - (($rowChild['PRIX'] * $pricepour) / 100);
								}
								$newPrice = sprintf("%.2f",$newPrice);
								$dataChildUpdated = array();
								$dataChildUpdated['PRIX'] = $newPrice;
								$productChild->update($dataChildUpdated,'ID = '.$rowChild['ID']);
							}

						}

						if ($pricesign == '+') {
							$this->view->messageSuccess = "Les produits ont �t� augment�s de ".$pricesign.$pricepour." % ";
						} else {
							$this->view->messageSuccess = "Les produits ont �t� diminu�s de ".$pricesign.$pricepour." % ";
						}
					} else {
						$this->view->messageError = "Le pourcentage doit �tre sup�rieur � 0";
					}
				} else {
					$this->view->messageError = "Une cat�gorie doit �tre s�lectionn�e";
				}


			} catch (Zend_Exception $e) {
					$this->log($e->getMessage(),'err');
				$this->view->messageError = $e->getMessage();
			}
		}
		$this->_forward('list');
	}

	public function addpricebrendAction() {
		if($this->_request->isPost()) {
			$params = $this->_request->getPost();
			try {
				$idBrendSelected = $params['listbrend'];
				$pricesign = $params['pricesign'];
				$pricepour = $params['pricepour'];

				if ($idBrendSelected != 'none') {
					$pricepour = intval($pricepour);
					if ($pricepour > 0) {
						
						$product = new Product();
						$productChild = new ProductChild();

						$listProducts = $product->fetchAll('IDBREND = '.$idBrendSelected);
						foreach ($listProducts as $rowProduct) {
							$actualProductPrice = $rowProduct['PRIX'];
							if ($pricesign == '+') {
								$newProductPrice = $rowProduct['PRIX'] + (($rowProduct['PRIX'] * $pricepour) / 100);
							} else {
								$newProductPrice = $rowProduct['PRIX'] - (($rowProduct['PRIX'] * $pricepour) / 100);
							}
							$newProductPrice = sprintf("%.2f",$newProductPrice);
							$dataProductUpdated = array();
							$dataProductUpdated['PRIX'] = $newProductPrice;
							$product->update($dataProductUpdated,'ID = '.$rowProduct['ID']);
								
							$listChilds = $productChild->fetchAll('IDPRODUCT = '.$rowProduct['ID']);
							foreach ($listChilds as $rowChild) {
								$actualPrice = $rowChild['PRIX'];
								if ($pricesign == '+') {
									$newPrice = $rowChild['PRIX'] + (($rowChild['PRIX'] * $pricepour) / 100);
								} else {
									$newPrice = $rowChild['PRIX'] - (($rowChild['PRIX'] * $pricepour) / 100);
								}
								$newPrice = sprintf("%.2f",$newPrice);
								$dataChildUpdated = array();
								$dataChildUpdated['PRIX'] = $newPrice;
								$productChild->update($dataChildUpdated,'ID = '.$rowChild['ID']);
							}

						}

						if ($pricesign == '+') {
							$this->view->messageSuccess = "Les produits ont �t� augment�s de ".$pricesign.$pricepour." % ";
						} else {
							$this->view->messageSuccess = "Les produits ont �t� diminu�s de ".$pricesign.$pricepour." % ";
						}
					} else {
						$this->view->messageError = "Le pourcentage doit �tre sup�rieur � 0";
					}
				} else {
					$this->view->messageError = "Une marque doit �tre s�lectionn�e";
				}


			} catch (Zend_Exception $e) {
					$this->log($e->getMessage(),'err');
				$this->view->messageError = $e->getMessage();
			}
		}
		
		$this->_forward('list');
	}
	
	function commandaddAction() {
    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());
	
	
			$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 = !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['remise']) {
						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' => $remiseeuro,
											'REMISEPOUR' => $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' => $remiseeuro,
											'REMISEPOUR' => $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);
					}
				}
			}
		}
            } catch (Zend_Exception $e) {
				$this->log($e->getMessage(),'err');
			$this->view->messageError = "V�rifier les param�tres";
		}
		$this->_forward('list');
		
	}
	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 configuration � �t� supprim�e ";
			} else {
				$this->view->messageError = "La configuration n'a pas �t� supprim�e ";
	
			}
		}
		$this->_forward('list');
	}
}
?>