Current File : /home/karenpetzb/application/modules/backoffice/controllers/EbpController.php
<?php
class Backoffice_EbpController extends Modules_Backoffice_Controllers_MainController {
	function init() {
		$this->view->title = "Administration";
		$this->view->currentMenu = "Ebp";
		$this->isConnectedWithRole ( 'isStats' );
	}
	public function indexAction() {
			$this->view->titlePage = "Gestion commerciale EBP";
		$this->view->messageSuccess = "";
		$this->view->messageError = "";
	}
	private $_SEPARATOR = ';';
	private function cleanUp($value) {
		$result = $value;
		$filter = new Zend_Filter ();
		$filter->addFilter ( new Zend_Filter_StringTrim () )->addFilter ( new Zend_Filter_StripTags () );
		$result = $filter->filter ( $result );
		$result = html_entity_decode ( $result, ENT_QUOTES, "ISO-8859-1" );
		$result = str_replace ( "\r\n", "", $result );
		$result = str_replace ( '�', " ", $result );
		// $result = str_replace("'","",$result);
		$result = str_replace ( $this->_SEPARATOR, "", $result );
		return $result;
	}
	private function cleanUpSpace($value) {
		$filter = new Zend_Filter ();
		$filter->addFilter ( new Zend_Filter_Alnum () );
		$value = str_replace ( $this->_SEPARATOR, "", $value );
		return $filter->filter ( $value );
	}
	private function cleanUpAddQuote($value) {
		return '"' . $this->cleanUp ( $value ) . '"';
	}
	function exportclientsAction() {
		try {
			$this->_helper->viewRenderer->setNeverRender ();
			
			$this->view->titlePage = "Gestion commerciale EBP";
			$this->view->messageSuccess = "";
			$this->view->messageError = "";
			
			$fichier = new FichierExcel ();
			$user = new User ();
			$select = $user->select ()->order ( "ID ASC" );
			$listusers = $user->fetchAll ( $select );
			
			foreach ( $listusers as $row ) {
				$result = $row ['ID'] . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['NOM'] ) . $this->_SEPARATOR;
				$result .= $this->_SEPARATOR; // Code Famille
				                              // Facturation
				$result .= $row ['CIVILITE'] . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['ADRESSE'] ) . $this->_SEPARATOR;
				$result .= $this->_SEPARATOR; // Adresse 2
				$result .= $this->_SEPARATOR; // Adresse 3
				$result .= $this->_SEPARATOR; // Adresse 4
				$result .= $this->cleanUpSpace ( $row ['CP'] ) . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['VILLE'] ) . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['DEPARTEMENT'] ) . $this->_SEPARATOR;
				$result .= "FR" . $this->_SEPARATOR;
				$result .= $this->_SEPARATOR; // Site Web
				$result .= $row ['CIVILITE'] . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['PRENOM'] ) . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['NOM'] ) . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['FONCTION'] ) . $this->_SEPARATOR;
				$result .= $row ['TEL'] . $this->_SEPARATOR;
				$result .= $this->_SEPARATOR; // Tel Portable
				$result .= $row ['FAX'] . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['EMAIL'] ) . $this->_SEPARATOR;
				// Livraison
				$result .= $row ['CIVILITE'] . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['NOM'] ) . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['ADRESSE'] ) . $this->_SEPARATOR;
				$result .= $this->_SEPARATOR; // Adresse 2
				$result .= $this->_SEPARATOR; // Adresse 3
				$result .= $this->_SEPARATOR; // Adresse 4
				$result .= $this->cleanUpSpace ( $row ['CP'] ) . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['VILLE'] ) . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['DEPARTEMENT'] ) . $this->_SEPARATOR;
				$result .= "FR" . $this->_SEPARATOR;
				$result .= $this->_SEPARATOR; // Site Web
				                              // Contact
				$result .= $row ['CIVILITE'] . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['PRENOM'] ) . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['NOM'] ) . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['FONCTION'] ) . $this->_SEPARATOR;
				$result .= $row ['TEL'] . $this->_SEPARATOR;
				$result .= $this->_SEPARATOR; // Tel Portable
				$result .= $row ['FAX'] . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['EMAIL'] ) . $this->_SEPARATOR;
				$result .= $this->_SEPARATOR; // Compte comptable
				$result .= $this->cleanUpSpace ( $row ['NUMIDFISC'] ) . $this->_SEPARATOR;
				$result .= $this->cleanUpSpace ( $row ['SIRET'] ) . $this->_SEPARATOR;
				$result .= $this->_SEPARATOR; // Code NAF
				$result .= "0";
				
				$fichier->Insertion ( $result );
			}
			$fichier->output ( 'ebp_clients' );
		} catch ( Zend_Exception $e ) {
			$this->log ( $e->getMessage (), 'err' );
		}
	}
	private function getEmptyFournisseur() {
		$result = "0" . $this->_SEPARATOR;
		$result .= "Aucun" . $this->_SEPARATOR;
		$result .= $this->_SEPARATOR; // Code Famille
		                              // Facturation
		$result .= $this->_SEPARATOR; // Civilite
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR; // Adresse 2
		$result .= $this->_SEPARATOR; // Adresse 3
		$result .= $this->_SEPARATOR; // Adresse 4
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR; // Departement
		$result .= "FR" . $this->_SEPARATOR;
		$result .= $this->_SEPARATOR; // Site Web
		$result .= $this->_SEPARATOR; // Civilite
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR; // Tel Portable
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR;
		// Livraison
		$result .= $this->_SEPARATOR; // Civilite
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR; // Adresse 2
		$result .= $this->_SEPARATOR; // Adresse 3
		$result .= $this->_SEPARATOR; // Adresse 4
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR; // Departement
		$result .= "FR" . $this->_SEPARATOR;
		$result .= $this->_SEPARATOR; // Site Web
		                              // Contact
		$result .= $this->_SEPARATOR; // Civilite
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR; // Tel Portable
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR;
		$result .= $this->_SEPARATOR; // Compte comptable
		$result .= $this->_SEPARATOR; // NUMIDFISC
		$result .= $this->_SEPARATOR; // SIRET
		$result .= $this->_SEPARATOR; // Code NAF
		$result .= "0";
		return $result;
	}
	function exportfournisseursAction() {
		try {
			$this->_helper->viewRenderer->setNeverRender ();
			
			$this->view->titlePage = "Gestion commerciale : EBP";
			$this->view->messageSuccess = "";
			$this->view->messageError = "";
			
			$fichier = new FichierExcel ();
			$supplier = new Supplier ();
			$select = $supplier->select ()->order ( "ID ASC" );
			$listsuppliers = $supplier->fetchAll ( $select );
			
			$supplierBrend = new SupplierBrend ();
			
			$fichier->Insertion ( $this->getEmptyFournisseur () );
			
			foreach ( $listsuppliers as $row ) {
				
				$listbrends = $supplierBrend->select ()->where ( "IDSUPPLIER = " . $row ['ID'] )->query ()->fetchAll ();
				
				foreach ( $listbrends as $rowBrend ) {
					$result = $rowBrend ['ID'] . $this->_SEPARATOR;
					$result .= $this->cleanUp ( $rowBrend ['BREND'] ) . $this->_SEPARATOR;
					$result .= $this->_SEPARATOR; // Code Famille
					                              // Facturation
					$result .= $this->_SEPARATOR; // Civilite
					$result .= $this->cleanUp ( $row ['ADDRESSE'] ) . $this->_SEPARATOR;
					$result .= $this->_SEPARATOR; // Adresse 2
					$result .= $this->_SEPARATOR; // Adresse 3
					$result .= $this->_SEPARATOR; // Adresse 4
					$result .= $this->cleanUpSpace ( $row ['CP'] ) . $this->_SEPARATOR;
					$result .= $this->cleanUp ( $row ['VILLE'] ) . $this->_SEPARATOR;
					$result .= $this->_SEPARATOR; // Departement
					$result .= "FR" . $this->_SEPARATOR;
					$result .= $this->_SEPARATOR; // Site Web
					$result .= $this->_SEPARATOR; // Civilite
					$result .= $this->cleanUp ( $row ['PRENOM'] ) . $this->_SEPARATOR;
					$result .= $this->cleanUp ( $row ['NOM'] ) . $this->_SEPARATOR;
					$result .= $this->_SEPARATOR;
					$result .= $row ['TEL'] . $this->_SEPARATOR;
					$result .= $this->_SEPARATOR; // Tel Portable
					$result .= $row ['FAX'] . $this->_SEPARATOR;
					$result .= $this->cleanUp ( $row ['EMAIL'] ) . $this->_SEPARATOR;
					// Livraison
					$result .= $this->_SEPARATOR; // Civilite
					$result .= $this->cleanUp ( $row ['NOM'] ) . $this->_SEPARATOR;
					$result .= $this->cleanUp ( $row ['ADDRESSE'] ) . $this->_SEPARATOR;
					$result .= $this->_SEPARATOR; // Adresse 2
					$result .= $this->_SEPARATOR; // Adresse 3
					$result .= $this->_SEPARATOR; // Adresse 4
					$result .= $this->cleanUpSpace ( $row ['CP'] ) . $this->_SEPARATOR;
					$result .= $this->cleanUp ( $row ['VILLE'] ) . $this->_SEPARATOR;
					$result .= $this->_SEPARATOR; // Departement
					$result .= "FR" . $this->_SEPARATOR;
					$result .= $this->_SEPARATOR; // Site Web
					                              // Contact
					$result .= $this->_SEPARATOR; // Civilite
					$result .= $this->cleanUp ( $row ['PRENOM'] ) . $this->_SEPARATOR;
					$result .= $this->cleanUp ( $row ['NOM'] ) . $this->_SEPARATOR;
					$result .= $this->_SEPARATOR;
					$result .= $row ['TEL'] . $this->_SEPARATOR;
					$result .= $this->_SEPARATOR; // Tel Portable
					$result .= $row ['FAX'] . $this->_SEPARATOR;
					$result .= $this->cleanUp ( $row ['EMAIL'] ) . $this->_SEPARATOR;
					$result .= $this->_SEPARATOR; // Compte comptable
					$result .= $this->_SEPARATOR; // NUMIDFISC
					$result .= $this->_SEPARATOR; // SIRET
					$result .= $this->_SEPARATOR; // Code NAF
					$result .= "0";
					
					$fichier->Insertion ( $result );
				}
			}
			$fichier->output ( 'ebp_fournisseurs' );
		} catch ( Zend_Exception $e ) {
			$this->log ( $e->getMessage (), 'err' );
		}
	}
	function exportcategoriesAction() {
		try {
			$this->_helper->viewRenderer->setNeverRender ();
			
			$this->view->titlePage = "Gestion commerciale : EBP";
			$this->view->messageSuccess = "";
			$this->view->messageError = "";
			
			$fichier = new FichierExcel ();
			$category = new Category ();
			$select = $category->select ()->order ( "ID ASC" );
			$listcategories = $category->fetchAll ( $select );
			
			foreach ( $listcategories as $row ) {
				$result = $row ['ID'] . $this->_SEPARATOR;
				$result .= $this->cleanUp ( $row ['NOM'] ) . $this->_SEPARATOR;
				$result .= $this->_SEPARATOR;
				$result .= $this->_SEPARATOR;
				$result .= $this->_SEPARATOR;
				$result .= "False" . $this->_SEPARATOR;
				$result .= "0" . $this->_SEPARATOR;
				$result .= "0";
				
				$fichier->Insertion ( $result );
			}
			$fichier->output ( 'ebp_categories' );
		} catch ( Zend_Exception $e ) {
			$this->log ( $e->getMessage (), 'err' );
		}
	}
	private function getUrlPage($params) {
		return $this->baseUrl_SiteCommerceUrl . "/" . $params ['PAGE'] . "-" . $params ['ID'] . ".html";
	}
	function exportarticlesAction() {
		try {
			$this->_helper->viewRenderer->setNeverRender ();
			
			$this->view->titlePage = "Gestion commerciale : EBP";
			$this->view->messageSuccess = "";
			$this->view->messageError = "";
			
			$fichier = new FichierExcel ();
			
			$sql = "SELECT p.ID ID, p.NOM NOM,p.NAVNOM NAVNOM, p.DESCRIPTIONSHORT DESCSHORT, p.DESCRIPTIONLONG DESCLONG, p.PRIX PRIX, p.isDEVIS isDEVISPRODUCT,
					p.DOCNAME DOCNAME, p.DOCURL DOCURL, p.IDBREND IDBREND,
					p.isPROMO isPROMO, pic.URL URLIMAGE, sb.BREND BREND, sb.URL BRENDURL, p.IDCATEGORY IDCATEGORY, c.NOM CATEGORYNOM,
					p.DESCRIPTIONTECH DESCTECH, p.DESCRIPTIONNORME DESCNORME
					FROM product p
					LEFT JOIN picture pic ON pic.IDPRODUCT = p.ID
					LEFT JOIN supplier_brend sb ON sb.ID = p.IDBREND
					LEFT JOIN category AS c ON c.ID = p.IDCATEGORY
					WHERE p.isACTIVE = 0
					AND pic.POSITION = 1
					GROUP BY ID
					ORDER BY p.ID ASC";
			
			$product = new Product ();
			$productChild = new ProductChild ();
			$listProducts = $product->getAdapter ()->fetchAll ( $sql );
			
			foreach ( $listProducts as $row ) {
				$sqlChild = "
						SELECT pc.ID ID ,pc.REFERENCE REFERENCE,pc.DESIGNATION DESIGNATION,pc.PRIX PRIX, pc.isPROMO isPROMO ,
						pc.IMAGEPROMO IMAGEPROMO,pc.isDEVIS isDEVIS, pc.QUANTITYMIN QUANTITYMIN
							FROM productchild AS pc
						WHERE pc.IDPRODUCT = " . $row ['ID'] . "
						ORDER BY pc.PRIX ASC";
				
				$productChildTemp = $productChild->getAdapter ()->fetchAll ( $sqlChild );
				
				$navnom = $this->verifyNavigationString ( $row ['NAVNOM'], $row ['NOM'], '' );
				
				$urlImage = $this->baseUrl_SiteCommerceUrl . "/" . $row ['URLIMAGE'];
				$urlPage = $this->getUrlPage ( array (
						'PAGE' => $navnom,
						'ID' => $row ['ID'] 
				) );
				
				$fraislivraison = "";
				$garantie = "";
				
				$reference = "";
				$description = "";
				foreach ( $productChildTemp as $rowChild ) {
					$reference = $this->cleanUp ( $rowChild ['REFERENCE'] );
					
					$description = $this->cleanUp ( $rowChild ['DESIGNATION'] );
					
					$result = $rowChild ['ID'] . $this->_SEPARATOR;
					$result .= $this->cleanUp ( $row ['NOM'] ) . $this->_SEPARATOR;
					$result .= $row ['IDCATEGORY'] . $this->_SEPARATOR;
					$result .= $this->_SEPARATOR; // Prix de revient
					$result .= $rowChild ['PRIX'] . $this->_SEPARATOR;
					$result .= $this->tva . $this->_SEPARATOR;
					$result .= $description . $this->_SEPARATOR;
					$result .= $reference . $this->_SEPARATOR; // Code barre
					$result .= $this->_SEPARATOR; // Code unite
					$result .= $this->_SEPARATOR; // Type d'article
					$result .= $this->_SEPARATOR; // Code emplacement
					$result .= $row ['IDBREND'] . $this->_SEPARATOR;
					$result .= $this->_SEPARATOR; // Code �co
					
					$fichier->Insertion ( $result );
				}
			}
			
			$fichier->output ( 'ebp_articles' );
		} catch ( Zend_Exception $e ) {
			$this->log ( $e->getMessage (), 'err' );
		}
	}
	private function formatDate($datevalue) {
		$date = new Zend_Date ();
		$date->set ( $datevalue );
		return $date->toString ( 'dd/MM/YYYY' );
	}
	private function getCommandDevisLine($listcommands) {
		$commandLine = new CommandProduct ();
		$fichier = new FichierExcel ();
		$result = "Document - Num�ro du document;Document - Date;Document - Code client;Document - Nom du client;Document - Adresse 1 (facturation);Document - Code postal (facturation);Document - Ville (facturation);Document - Code Pays (facturation);Document - Nom (contact) (facturation);Document - Pr�nom (facturation);Document - T�l�phone fixe (facturation);Document - Fax (facturation);Document - E-mail (facturation);Document - Nom (adresse) (livraison);Document - Adresse 1 (livraison);Document - Code postal (livraison);Document - Ville (livraison);Document - Code Pays (livraison);Document - T�l�phone fixe (livraison);Document - Fax (livraison);Document - E-mail (livraison);Document - Frais de port HT;Document - Total Brut HT;Document - Total TTC;Document - Taux de TVA port;Ligne - Code article;Ligne - Quantit�;Ligne - Taux de TVA;Ligne - PV HT;Ligne - Montant Net HT;Ligne - Montant de remise unitaire HT cumul�";
		$fichier->Colonne ( $result );
		foreach ( $listcommands as $row ) {
			$result = '"' . $row ['REFERENCE'] . '"' . $this->_SEPARATOR;
			$result .= '"' . $this->formatDate ( $row ['DATESTART'] ) . '"' . $this->_SEPARATOR;
			$result .= '"' . $row ['IDUSER'] . '"' . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['USER_NOM'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['FACT_ADRESSE'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['FACT_CP'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['FACT_VILLE'] ) . $this->_SEPARATOR;
			$result .= "FR" . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['USER_NOM'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['USER_PRENOM'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['USER_TEL'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['USER_FAX'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['USER_EMAIL'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['LIV_RAISONSOCIAL'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['LIV_ADRESSE'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['LIV_CP'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['LIV_VILLE'] ) . $this->_SEPARATOR;
			$result .= "FR" . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['USER_TEL'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['USER_FAX'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['USER_EMAIL'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['PRIXFRAISPORT'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['PRIXTOTALHTFP'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $row ['PRIXTOTALTTC'] ) . $this->_SEPARATOR;
			$result .= $this->cleanUpAddQuote ( $this->getCurrentTva ( $row ['DATESTART'] ) ) . $this->_SEPARATOR;
			
			$selectLine = $commandLine->select ()->where ( "IDCOMMAND = " . $row ["ID"] );
			$listLines = $commandLine->fetchAll ( $selectLine );
			foreach ( $listLines as $rowLine ) {
				$resultLine = $result;
				
				$resultLine .= $this->cleanUpAddQuote ( $rowLine ['CHILDID'] ) . $this->_SEPARATOR;
				$resultLine .= $this->cleanUpAddQuote ( $rowLine ['CHILDQUANTITY'] ) . $this->_SEPARATOR;
				$resultLine .= $this->cleanUpAddQuote ( $this->getCurrentTva ( $row ['DATESTART'] ) ) . $this->_SEPARATOR;
				$resultLine .= $this->cleanUpAddQuote ( $rowLine ['CHILDPRIX'] ) . $this->_SEPARATOR;
				$resultLine .= $this->cleanUpAddQuote ( $rowLine ['CHILDPRIXTOTAL'] ) . $this->_SEPARATOR;
				$resultLine .= $this->cleanUpAddQuote ( $rowLine ['CHILDPRIXREMISE'] ) . $this->_SEPARATOR;
				
				$fichier->Insertion ( $resultLine );
			}
		}
		return $fichier;
	}
	function exportcommandesAction() {
		try {
			$this->_helper->viewRenderer->setNeverRender ();
			
			$this->view->titlePage = "Gestion commerciale : EBP";
			$this->view->messageSuccess = "";
			$this->view->messageError = "";
			
			$command = new Command ();
			
			$date = new Zend_Date ();
			$date->addMonth ( - 3 );
			$select = "
				SELECT *
				FROM command
				WHERE isARCHIVE = 1
				AND STATUT IN (1, 2, 3)
				AND DATESTART >= '" . $date->toString ( 'YYYY-MM-dd' ) . "'";
			
			$listcommands = $command->getAdapter ()->fetchAll ( $select );
			
			$fichier = $this->getCommandDevisLine ( $listcommands );
			
			$fichier->output ( 'ebp_commandes' );
		} catch ( Zend_Exception $e ) {
			$this->log ( $e->getMessage (), 'err' );
		}
	}
	function exportdevisAction() {
		try {
			$this->_helper->viewRenderer->setNeverRender ();
			
			$this->view->titlePage = "Gestion commerciale : EBP";
			$this->view->messageSuccess = "";
			$this->view->messageError = "";
			
			$command = new Command ();
			
			$date = new Zend_Date ();
			$date->addMonth ( - 3 );
			$select = "
				SELECT *
				FROM command
				WHERE isARCHIVE = 1
				AND STATUT IN (10, 11)
				AND DATESTART >= '" . $date->toString ( 'YYYY-MM-dd' ) . "'";
			
			$listcommands = $command->getAdapter ()->fetchAll ( $select );
			
			$fichier = $this->getCommandDevisLine ( $listcommands );
			
			$fichier->output ( 'ebp_devis' );
		} catch ( Zend_Exception $e ) {
			$this->log ( $e->getMessage (), 'err' );
		}
	}
	function importcategoriesAction() {
		try {
			$this->_helper->viewRenderer->setNeverRender ();
			
			$this->view->titlePage = "Gestion commerciale : EBP";
			$this->view->messageSuccess = "";
			$this->view->messageError = "";
			
			$nomTemp = 'csvfile';
			
			if ($this->isValidCSVFile ( $nomTemp )) {
				$nomOrigine = $_FILES [$nomTemp] ['name'];
				
				$date = new Zend_Date ();
				$nomDestination = "importcategories-" . $date->toString ( 'dd-MM-YYYY_HH-mm-ss' ) . ".csv";
				
				if ($this->uploadCsvFile ( $_FILES [$nomTemp] ["tmp_name"], $nomDestination )) {
					
					$header = array (
							0 => 'ID',
							1 => 'NOM',
							2 => 'NOUSE1',
							3 => 'NOUSE2',
							4 => 'NOUSE3',
							5 => 'NOUSE4',
							6 => 'NOUSE5',
							7 => 'NOUSE6' 
					);
					$dataRows = $this->csv_to_array ( $this->csv_import . $nomDestination, $header );
					if (sizeof ( $dataRows [0] ) == sizeof ( $header )) {
						
						$category = new Category ();
						$filesAdded = 0;
						$filesUpdated = 0;
						$filesError = 0;
						foreach ( $dataRows as $row ) {
							
							$id = $row ["ID"];
							$nom = $this->cleanUp ( $row ["NOM"] );
							
							$currentCat = $category->select ()->where ( "ID = ?", $id )->query ()->fetch ();
							
							if ($currentCat != null && ! empty ( $currentCat )) {
								try {
									// Update
									if ($currentCat ['NOM'] != $nom) {
										$data = array (
												'NOM' => $nom 
										);
										$category->update ( $data, 'ID = ' . $id );
										$filesUpdated ++;
									}
								} catch ( Zend_Exception $e ) {
									$filesError ++;
									$this->log ( $e->getMessage (), 'warn' );
								}
							} else {
								try {
									$navnom = $this->verifyNavigationString ( "", $nom, '' );
									$keywords = $this->generateKeyWords ( $navnom );
									// Insert
									$data = array (
											'NOM' => $nom,
											'NAVTITRENOM' => $nom,
											'DESCRIPTION' => $nom,
											'NAVDESCRIPTION' => $nom,
											'NAVNOM' => $navnom,
											'KEYWORDS' => $keywords,
											'IDPARENT' => 0 
									);
									$category->insert ( $data );
									
									$lastid = $category->getAdapter()->lastInsertId();
									if ($lastid > 0) {
										mkdir ("items/category/".$lastid, 0777);
										mkdir ("items/product/".$lastid, 0777);
									}
									
									$filesAdded ++;
								} catch ( Zend_Exception $e ) {
									$filesError ++;
									$this->log ( $e->getMessage (), 'warn' );
								}
							}
						}
						$this->view->messageSuccess = "Importation des fiches Familles articles<br/>
														Fiches ajout�es : " . $filesAdded . "<br/>
														Fiches modifi�es : " . $filesUpdated . "<br/>
														Fiches non import�es : " . $filesError . "<br/>";
					} else {
						$this->view->messageError = "Le fichier n'est pas au format attendu pour les Familles articles";
					}
				} else {
					$this->view->messageError = "Le fichier n'a pas pu etre import�.";
				}
			}
		} catch ( Zend_Exception $e ) {
			$this->log ( $e->getMessage (), 'err' );
		}
		$this->render ( '/index' );
	}
	function importarticlesAction() {
		try {
			$this->_helper->viewRenderer->setNeverRender ();
			
			$this->view->titlePage = "Gestion commerciale : EBP";
			$this->view->messageSuccess = "";
			$this->view->messageError = "";
			
			$nomTemp = 'csvfile';
			
			if ($this->isValidCSVFile ( $nomTemp )) {
				$nomOrigine = $_FILES [$nomTemp] ['name'];
				
				$date = new Zend_Date ();
				$nomDestination = "importarticles-" . $date->toString ( 'dd-MM-YYYY_HH-mm-ss' ) . ".csv";
				
				if ($this->uploadCsvFile ( $_FILES [$nomTemp] ["tmp_name"], $nomDestination )) {
					
					$header = array (
							0 => 'ID',
							1 => 'NOM',
							2 => 'IDCATEGORY',
							3 => 'NOUSE1',
							4 => 'PRIX',
							5 => 'TVA',
							6 => 'DESCRIPTION',
							7 => 'REFERENCE',
							8 => 'NOUSE2',
							9 => 'NOUSE3',
							10 => 'NOUSE4',
							11 => 'IDBREND',
							12 => 'NOUSE5' 
					);
					
					$dataRows = $this->csv_to_array ( $this->csv_import . $nomDestination, $header );
					if (sizeof ( $dataRows [0] ) == sizeof ( $header )) {
						
						$product = new Product ();
						$productLine = new ProductChild ();
						$filesAdded = 0;
						$filesUpdated = 0;
						$filesError = 0;
						
						$date = new Zend_Date ();
						foreach ( $dataRows as $row ) {
							
							$id = $row ["ID"];
							$prix = $this->cleanUp ( $row ["PRIX"] );
							$description = $this->cleanUp ( $row ["DESCRIPTION"] );
							$reference = $this->cleanUp ( $row ["REFERENCE"] );
							
							$tva = $this->cleanUp ( $row ["TVA"] );
							$nom_prod = $this->cleanUp ( $row ["NOM"] );
							$idCat_prod = $this->cleanUp ( $row ["IDCATEGORY"] );
							$idbrend_prod = $this->cleanUp ( $row ["IDBREND"] );
							
							$currentProdLine = $productLine->select ()->where ( "REFERENCE = ?", $reference )->query ()->fetch ();
							
							if ($currentProdLine != null && ! empty ( $currentProdLine )) {
								try {
									
									// Update
									$dataChild = array (
											'DESIGNATION' => $description,
											'PRIX' => $prix,
											'DATEMODIF' => $date->toString ( 'YYYY-MM-dd HH:mm:ss' ) 
									);
									
									$productChild = new ProductChild ();
									$productChild->update ( $dataChild, 'ID = ' . $id );
									$filesUpdated ++;
								} catch ( Zend_Exception $e ) {
									$filesError ++;
									$this->log ( $e->getMessage (), 'warn' );
								}
							} else {
								try {
									// Insert
									$navnom = $this->verifyNavigationString ( "", $nom_prod, '' );
									$keywords = $this->generateKeyWords ( $navnom );
									
									$data = array (
											'NOM' => $nom_prod,
											'NAVNOM' => $navnom,
											'KEYWORDS' => $keywords,
											'NAVTITRE' => '',
											'NAVDESC' => '',
											'DESCRIPTIONSHORT' => $nom_prod,
											'DESCRIPTIONLONG' => $nom_prod,
											'PRIX' => $prix,
											'isACTIVE' => 1,
											'DATEPROMO' => $date->toString ( 'YYYY-MM-dd' ),
											'isPROMO' => '1',
											'IDCATEGORY' => $idCat_prod,
											'ISSHOWBREND' => 1 
									);
									$product->insert ( $data );
									$lastId = $product->getAdapter ()->lastInsertId ();
									
									$dataChild = array (
											'REFERENCE' => $reference,
											'DESIGNATION' => $description,
											'PRIX' => $prix,
											'ETATSTOCK' => 0,
											'QUANTITYMIN' => 1,
											'isDEVIS' => 1,
											'IMAGEPROMO' => 0,
											'DATEMODIF' => $date->toString ( 'YYYY-MM-dd HH:mm:ss' ),
											'IDPRODUCT' => $lastId 
									);
									
									$productChild = new ProductChild ();
									$productChild->insert ( $dataChild );
									
									$filesAdded ++;
								} catch ( Zend_Exception $e ) {
									$filesError ++;
									$this->log ( $e->getMessage (), 'warn' );
								}
							}
						}
						$this->view->messageSuccess = "Importation des fiches Articles<br/>
														Fiches ajout�es : " . $filesAdded . "<br/>
														Fiches modifi�es : " . $filesUpdated . "<br/>
														Fiches non import�es : " . $filesError . "<br/>";
					} else {
						$this->view->messageError = "Le fichier n'est pas au format attendu pour les Articles";
					}
				} else {
					$this->view->messageError = "Le fichier n'a pas pu etre import�.";
				}
			}
		} catch ( Zend_Exception $e ) {
			$this->log ( $e->getMessage (), 'err' );
		}
		$this->render ( '/index' );
	}
	var $csv_import = 'csvfiles/import/';
	private function uploadCsvFile($tmpName, $destName) {
		if (! file_exists ( $this->csv_import )) {
			mkdir ( $this->csv_import, 0777, true );
		}
		return move_uploaded_file ( $tmpName, $this->csv_import . $destName );
	}
	private function isValidCSVFile($name) {
		if (! empty ( $_FILES [$name] ) && ! empty ( $_FILES [$name] ['name'] )) {
			$nomOrigine = $_FILES [$name] ['name'];
			$elementsChemin = pathinfo ( $nomOrigine );
			$extensionFichier = strtolower($elementsChemin ['extension']);
			$extensionsAutorisees = array (
					"csv" 
			);
			if (! (in_array ( $extensionFichier, $extensionsAutorisees ))) {
				$this->view->messageError = "Le fichier n'a pas l'extension attendue (.csv)";
			} else {
				return true;
			}
		} else {
			$this->view->messageError = "Vous pouvez exporter les donn�es via EBP -> Outils -> Exportation de donn�es";
		}
		return false;
	}
	private function csv_to_array($filename = '', $header, $delimiter = ';') {
		if (! file_exists ( $filename ) || ! is_readable ( $filename ))
			return FALSE;
		
		$data = array ();
		if (($handle = fopen ( $filename, 'r' )) !== FALSE) {
			while ( ($row = fgetcsv ( $handle, 1000, $delimiter )) !== FALSE ) {
				if (! $header) {
					$header = $row;
				} else if (sizeof ( $header ) == sizeof ( $row )) {
					$data [] = array_combine ( $header, $row );
				}
			}
			fclose ( $handle );
		}
		return $data;
	}
}
?>