/** * @name enregistrerBonDeCommande($pParam) * @return AfficheListeProduitBonDeCommandeResponse * @desc Enregistre le bon de commande. */ public function enregistrerBonDeCommande($pParam) { $lVr = ProduitsBonDeCommandeValid::validAjout($pParam); if ($lVr->getValid()) { $lIdMarche = $pParam["id_commande"]; $lIdCompteFerme = $pParam["id_compte_ferme"]; $lProduits = $pParam["produits"]; // On enregistre uniquement les produits avec à minima quantité (même si prix à 0) // Calcul du total $lTotal = 0; $lProduitsValide = array(); foreach ($lProduits as $lProduit) { if ($lProduit["quantite"] > 0) { array_push($lProduitsValide, $lProduit); } $lTotal += $lProduit["prix"]; } // Récupère l'opération Bon de commande si elle existe $lOperationService = new OperationService(); $lOperations = $lOperationService->getBonCommande($lIdMarche, $lIdCompteFerme); $lIdOperation = $lOperations[0]->getId(); if (is_null($lIdOperation)) { // Si il n'y a pas d'opération de Bon de commande $lOperation = new OperationDetailVO(); $lOperation->setIdCompte($lIdCompteFerme); $lOperation->setLibelle('Bon de Commande'); $lOperation->setTypePaiement(5); $lOperationChampComplementaire = new OperationChampComplementaireVO(); $lOperationChampComplementaire->setChcpId(1); $lOperationChampComplementaire->setValeur($lIdMarche); $lOperation->setChampComplementaire(array($lOperationChampComplementaire)); } else { $lOperation = $lOperations[0]; } $lOperation->setMontant($lTotal); $lIdOperation = $lOperationService->set($lOperation); // Ajout ou mise à jour de l'operation $lBonCommande = InfoBonCommandeViewManager::selectInfoBonCommande($lIdMarche, $lIdCompteFerme); $lDetailOperationService = new DetailOperationService(); $lStockService = new StockService(); foreach ($lProduitsValide as $lProduit) { $lMaj = false; foreach ($lBonCommande as $lBon) { if ($lProduit["dcomId"] == $lBon->getDcomId()) { $lMaj = true; //$lDcom = DetailCommandeManager::selectByIdProduit($lProduit["id"]); $lStock = new StockVO(); $lStock->setId($lBon->getStoId()); $lStock->setQuantite($lProduit["quantite"]); $lStock->setType(3); $lStock->setIdCompte($lIdCompteFerme); $lStock->setIdDetailCommande($lProduit["dcomId"]); $lStock->setIdOperation($lIdOperation); $lStockService->set($lStock); $lDetailOperation = $lDetailOperationService->get($lBon->getDopeId()); $lDetailOperation->setIdOperation($lIdOperation); $lDetailOperation->setIdCompte($lIdCompteFerme); $lDetailOperation->setMontant($lProduit["prix"]); $lDetailOperation->setLibelle('Bon de Commande'); $lDetailOperation->setTypePaiement(5); $lDetailOperationService->set($lDetailOperation); } } if (!$lMaj) { //$lDcom = DetailCommandeManager::selectByIdProduit($lProduit["id"]); $lStock = new StockVO(); $lStock->setQuantite($lProduit["quantite"]); $lStock->setType(3); $lStock->setIdCompte($lIdCompteFerme); $lStock->setIdDetailCommande($lProduit["dcomId"]); $lStock->setIdOperation($lIdOperation); $lStockService->set($lStock); $lDetailOperation = new DetailOperationVO(); $lDetailOperation->setIdOperation($lIdOperation); $lDetailOperation->setIdCompte($lIdCompteFerme); $lDetailOperation->setMontant($lProduit["prix"]); $lDetailOperation->setLibelle('Bon de Commande'); $lDetailOperation->setTypePaiement(5); //$lDetailOperation->setTypePaiementChampComplementaire($lProduit["id"]); $lDetailOperation->setIdDetailCommande($lProduit["dcomId"]); $lDetailOperationService->set($lDetailOperation); } } foreach ($lBonCommande as $lBon) { $lDelete = true; foreach ($lProduitsValide as $lProduit) { if ($lProduit["dcomId"] == $lBon->getDcomId()) { $lDelete = false; } } if ($lDelete) { $lStockService->delete($lBon->getStoId()); $lDetailOperationService->delete($lBon->getDopeId()); } } } return $lVr; }