Exemplo n.º 1
0
 /**
  * @name insert($pAchat)
  * @param AchatVO
  * @return integer
  * @desc Ajoute une réservation
  */
 private function insert($pAchat)
 {
     $lOperationService = new OperationService();
     // Rechargement
     $lIdRechargement = 0;
     $lCompteRechargement = $pAchat->getRechargement()->getIdCompte();
     if (!empty($lCompteRechargement) && !is_null($lCompteRechargement)) {
         $pAchat->getRechargement()->setLibelle('Rechargement');
         $lIdRechargement = $lOperationService->set($pAchat->getRechargement());
     }
     $lIdOperationAchat = 0;
     $lIdOperationAchatSolidaire = 0;
     $lIdCompte = 0;
     $lIdMarche = 0;
     $lLibelleOperation = '';
     $lLibelleOperationSolidaire = '';
     // Achat
     $lTestCompteAchat = $pAchat->getOperationAchat()->getIdCompte();
     if (!empty($lTestCompteAchat) && !is_null($lTestCompteAchat)) {
         $lIdCompte = $pAchat->getOperationAchat()->getIdCompte();
         $ltestChampComplementaire = $pAchat->getOperationAchat()->getChampComplementaire();
         if (isset($ltestChampComplementaire[1])) {
             $lIdMarche = $ltestChampComplementaire[1]->getValeur();
             $lMarcheService = new MarcheService();
             $lMarche = $lMarcheService->getInfoMarche($lIdMarche);
             $lLibelleOperation = "Marché N°" . $lMarche->getNumero();
         } else {
             $lLibelleOperation = "Achat du " . StringUtils::dateAujourdhuiFr();
         }
         $pAchat->getOperationAchat()->setLibelle($lLibelleOperation);
         $lIdOperationAchat = $lOperationService->set($pAchat->getOperationAchat());
         // Operation d'achat
         $lOperationZeybu = new OperationDetailVO();
         $lOperationZeybu->setIdCompte(-1);
         $lOperationZeybu->setMontant($pAchat->getOperationAchat()->getMontant() * -1);
         $lOperationZeybu->setLibelle($lLibelleOperation);
         $lOperationZeybu->setTypePaiement($pAchat->getOperationAchat()->getTypePaiement());
         $lOperationZeybuChampComplementaire = $pAchat->getOperationAchat()->getChampComplementaire();
         $lOperationZeybuChampComplementaire[8] = new OperationChampComplementaireVO(null, 8, $lIdOperationAchat);
         $lOperationZeybu->setChampComplementaire($lOperationZeybuChampComplementaire);
         $lIdOperationZeybu = $lOperationService->set($lOperationZeybu);
         // Operation Zeybu
     }
     // Achat Solidaire
     $lTestCompteAchatSolidaire = $pAchat->getOperationAchatSolidaire()->getIdCompte();
     if (!empty($lTestCompteAchatSolidaire) && !is_null($lTestCompteAchatSolidaire)) {
         $lIdCompte = $pAchat->getOperationAchatSolidaire()->getIdCompte();
         $ltestChampComplementaire = $pAchat->getOperationAchatSolidaire()->getChampComplementaire();
         if (isset($ltestChampComplementaire[1])) {
             if (!isset($lMarche)) {
                 // Pour éviter de lancer 2 fois la requête
                 $lIdMarche = $ltestChampComplementaire[1]->getValeur();
                 $lMarcheService = new MarcheService();
                 $lMarche = $lMarcheService->getInfoMarche($lIdMarche);
             }
             $lLibelleOperationSolidaire = "Marché Solidaire N°" . $lMarche->getNumero();
         } else {
             $lLibelleOperationSolidaire = "Achat Solidaire du " . StringUtils::dateAujourdhuiFr();
         }
         $pAchat->getOperationAchatSolidaire()->setLibelle($lLibelleOperationSolidaire);
         $lIdOperationAchatSolidaire = $lOperationService->set($pAchat->getOperationAchatSolidaire());
         // Operation d'achat Solidaire
         $lOperationZeybuSolidaire = new OperationDetailVO();
         $lOperationZeybuSolidaire->setIdCompte(-1);
         $lOperationZeybuSolidaire->setMontant($pAchat->getOperationAchatSolidaire()->getMontant() * -1);
         $lOperationZeybuSolidaire->setLibelle($lLibelleOperationSolidaire);
         $lOperationZeybuSolidaire->setTypePaiement($pAchat->getOperationAchatSolidaire()->getTypePaiement());
         $lOperationZeybuSolidaireChampComplementaire = $pAchat->getOperationAchatSolidaire()->getChampComplementaire();
         $lOperationZeybuSolidaireChampComplementaire[8] = new OperationChampComplementaireVO(null, 8, $lIdOperationAchatSolidaire);
         $lOperationZeybuSolidaire->setChampComplementaire($lOperationZeybuSolidaireChampComplementaire);
         $lIdOperationZeybuSolidaire = $lOperationService->set($lOperationZeybuSolidaire);
         // Operation Zeybu solidaire
     }
     // Liaison Rechargement
     if (!empty($lCompteRechargement) && !is_null($lCompteRechargement)) {
         $lMaj = false;
         $lRechargementChampComplementaire = $pAchat->getRechargement()->getChampComplementaire();
         if (!empty($lTestCompteAchat) && !is_null($lTestCompteAchat)) {
             $lMaj = true;
             $lRechargementChampComplementaire[12] = new OperationChampComplementaireVO(null, 12, $lIdOperationAchat);
         }
         if (!empty($lTestCompteAchatSolidaire) && !is_null($lTestCompteAchatSolidaire)) {
             $lMaj = true;
             $lRechargementChampComplementaire[13] = new OperationChampComplementaireVO(null, 13, $lIdOperationAchatSolidaire);
         }
         if ($lMaj) {
             $pAchat->getRechargement()->setChampComplementaire($lRechargementChampComplementaire);
             $lOperationService->set($pAchat->getRechargement());
         }
     }
     // Liaison achat
     if (!empty($lTestCompteAchat) && !is_null($lTestCompteAchat)) {
         $lChampComplementaire = $pAchat->getOperationAchat()->getChampComplementaire();
         $lChampComplementaire[8] = new OperationChampComplementaireVO(null, 8, $lIdOperationZeybu);
         if (!empty($lCompteRechargement) && !is_null($lCompteRechargement)) {
             $lChampComplementaire[14] = new OperationChampComplementaireVO(null, 14, $lIdRechargement);
         }
         if (!empty($lTestCompteAchatSolidaire) && !is_null($lTestCompteAchatSolidaire)) {
             $lChampComplementaire[13] = new OperationChampComplementaireVO(null, 13, $lIdOperationAchatSolidaire);
         }
         $pAchat->getOperationAchat()->setChampComplementaire($lChampComplementaire);
         $lOperationService->set($pAchat->getOperationAchat());
         // Operation d'achat avec lien operation zeybu
     }
     // Liaison Achat Solidaire
     if (!empty($lTestCompteAchatSolidaire) && !is_null($lTestCompteAchatSolidaire)) {
         $lChampComplementaireSolidaire = $pAchat->getOperationAchatSolidaire()->getChampComplementaire();
         $lChampComplementaireSolidaire[8] = new OperationChampComplementaireVO(null, 8, $lIdOperationZeybuSolidaire);
         if (!empty($lCompteRechargement) && !is_null($lCompteRechargement)) {
             $lChampComplementaireSolidaire[14] = new OperationChampComplementaireVO(null, 14, $lIdRechargement);
         }
         if (!empty($lTestCompteAchat) && !is_null($lTestCompteAchat)) {
             $lChampComplementaireSolidaire[12] = new OperationChampComplementaireVO(null, 12, $lIdOperationAchat);
         }
         $pAchat->getOperationAchatSolidaire()->setChampComplementaire($lChampComplementaireSolidaire);
         $lOperationService->set($pAchat->getOperationAchatSolidaire());
         // Operation d'achat solidaire avec lien operation zeybu
     }
     // Ajout des produits
     $lIdModeleLot = array();
     $lIdDetailCommande = array();
     foreach ($pAchat->getProduits() as $lProduit) {
         $lTestModeleLot = $lProduit->getIdModeleLot();
         $lTestDetailCommande = $lProduit->getIdDetailCommande();
         if (!empty($lTestModeleLot) && !is_null($lTestModeleLot)) {
             array_push($lIdModeleLot, $lTestModeleLot);
         } else {
             if (!empty($lTestDetailCommande) && !is_null($lTestDetailCommande)) {
                 array_push($lIdDetailCommande, $lTestDetailCommande);
             }
         }
         $lTestModeleLotSolidaire = $lProduit->getIdModeleLotSolidaire();
         $lTestDetailCommandeSolidaire = $lProduit->getIdDetailCommandeSolidaire();
         if (!empty($lTestModeleLotSolidaire) && !is_null($lTestModeleLotSolidaire)) {
             array_push($lIdModeleLot, $lTestModeleLotSolidaire);
         } else {
             if (!empty($lTestDetailCommandeSolidaire) && !is_null($lTestDetailCommandeSolidaire)) {
                 array_push($lIdDetailCommande, $lTestDetailCommandeSolidaire);
             }
         }
     }
     if (!empty($lIdModeleLot)) {
         $lListeModeleLot = ModeleLotManager::selectByArray($lIdModeleLot);
     }
     if (!empty($lIdDetailCommande)) {
         $lListeDetailCommande = DetailCommandeManager::selectByArrayClassByDcomId($lIdDetailCommande);
     }
     $lDetailOperationService = new DetailOperationService();
     $lStockService = new StockService();
     foreach ($pAchat->getProduits() as $lProduit) {
         // Stock
         $lIdStock = 0;
         $lIdDetailOperation = 0;
         if ($lProduit->getQuantite() < 0) {
             $lUnite = '';
             $lTestModeleLot = $lProduit->getIdModeleLot();
             $lTestDetailCommande = $lProduit->getIdDetailCommande();
             if (!empty($lTestModeleLot) && !is_null($lTestModeleLot)) {
                 $lUnite = $lListeModeleLot[$lTestModeleLot]->getUnite();
             } else {
                 if (!empty($lTestDetailCommande) && !is_null($lTestDetailCommande)) {
                     $lUnite = $lListeDetailCommande[$lTestDetailCommande]->getUnite();
                 }
             }
             $lIdStock = $lStockService->set(new StockVO(null, null, $lProduit->getQuantite(), 1, $lIdCompte, $lProduit->getIdDetailCommande(), $lProduit->getIdModeleLot(), $lIdOperationAchat, $lProduit->getIdNomProduit(), $lUnite));
             // Prix
             $lIdDetailOperation = $lDetailOperationService->set(new DetailOperationVO(null, $lIdOperationAchat, $lIdCompte, $lProduit->getMontant(), $lLibelleOperation, null, 7, $lProduit->getIdDetailCommande(), $lProduit->getIdModeleLot(), $lProduit->getIdNomProduit(), null));
         }
         // Stock Solidaire
         $lIdStockSolidaire = 0;
         $lIdDetailOperationSolidaire = 0;
         if ($lProduit->getQuantiteSolidaire() < 0) {
             $lUniteSolidaire = '';
             $lTestModeleLotSolidaire = $lProduit->getIdModeleLotSolidaire();
             $lTestDetailCommandeSolidaire = $lProduit->getIdDetailCommandeSolidaire();
             if (!empty($lTestModeleLotSolidaire) && !is_null($lTestModeleLotSolidaire)) {
                 $lUniteSolidaire = $lListeModeleLot[$lTestModeleLotSolidaire]->getUnite();
             } else {
                 if (!empty($lTestDetailCommandeSolidaire) && !is_null($lTestDetailCommandeSolidaire)) {
                     $lUniteSolidaire = $lListeDetailCommande[$lTestDetailCommandeSolidaire]->getUnite();
                 }
             }
             $lIdStockSolidaire = $lStockService->set(new StockVO(null, null, $lProduit->getQuantiteSolidaire(), 2, $lIdCompte, $lProduit->getIdDetailCommandeSolidaire(), $lProduit->getIdModeleLotSolidaire(), $lIdOperationAchatSolidaire, $lProduit->getIdNomProduit(), $lUniteSolidaire));
             // Prix
             $lIdDetailOperationSolidaire = $lDetailOperationService->set(new DetailOperationVO(null, $lIdOperationAchatSolidaire, $lIdCompte, $lProduit->getMontantSolidaire(), $lLibelleOperationSolidaire, null, 8, $lProduit->getIdDetailCommandeSolidaire(), $lProduit->getIdModeleLotSolidaire(), $lProduit->getIdNomProduit(), null));
         }
         if ($lProduit->getQuantiteSolidaire() < 0 || $lProduit->getQuantite() < 0) {
             // Pas d'ajout de ligne vide
             DetailAchatManager::insert(new DetailAchatVO($lIdOperationAchat, $lIdOperationAchatSolidaire, $lProduit->getIdNomProduit(), $lIdStock, $lIdDetailOperation, $lIdStockSolidaire, $lIdDetailOperationSolidaire));
         }
     }
     // Maj de la réservation
     if ($lIdMarche != 0) {
         $lIdReservation = new IdReservationVO();
         $lIdReservation->setIdCompte($lIdCompte);
         $lIdReservation->setIdCommande($lIdMarche);
         $lReservationService = new ReservationService();
         $lReservationService->updateEnAchat($lIdReservation);
     }
     // Retourne l'Id de l'achat ou à defaut celui de l'achat solidaire
     $lIdRetour = $lIdOperationAchat;
     if ($lIdRetour == 0 && $lIdOperationAchatSolidaire != 0) {
         $lIdRetour = $lIdOperationAchatSolidaire;
     } else {
         $lIdRetour = $lIdRechargement;
     }
     return $lIdRetour;
 }