PageRenderTime 78ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/service/MarcheService.php

http://zeybux.googlecode.com/
PHP | 889 lines | 591 code | 102 blank | 196 comment | 94 complexity | fd096792c1d336fdc7c9d97993c55483 MD5 | raw file
  1. <?php
  2. //****************************************************************
  3. //
  4. // Createur : Julien PIERRE
  5. // Date de creation : 13/07/2011
  6. // Fichier : MarcheService.php
  7. //
  8. // Description : Classe MarcheService
  9. //
  10. //****************************************************************
  11. // Inclusion des classes
  12. include_once(CHEMIN_CLASSES_MANAGERS . "CommandeManager.php");
  13. include_once(CHEMIN_CLASSES_VO . "MarcheVO.php");
  14. include_once(CHEMIN_CLASSES_VIEW_MANAGER . "DetailMarcheViewManager.php");
  15. include_once(CHEMIN_CLASSES_VIEW_MANAGER . "GestionCommandeReservationProducteurViewManager.php");
  16. include_once(CHEMIN_CLASSES_VIEW_MANAGER . "CompteNomProduitViewManager.php");
  17. include_once(CHEMIN_CLASSES_UTILS . "TestFonction.php");
  18. //include_once(CHEMIN_CLASSES_MANAGERS . "NomProduitManager.php");
  19. include_once(CHEMIN_CLASSES_MANAGERS . "ProduitManager.php");
  20. include_once(CHEMIN_CLASSES_MANAGERS . "DetailCommandeManager.php");
  21. include_once(CHEMIN_CLASSES_MANAGERS . "CommandeManager.php");
  22. include_once(CHEMIN_CLASSES_SERVICE . "StockService.php" );
  23. include_once(CHEMIN_CLASSES_SERVICE . "OperationService.php" );
  24. include_once(CHEMIN_CLASSES_SERVICE . "AbonnementService.php" );
  25. include_once(CHEMIN_CLASSES_SERVICE . "ReservationService.php");
  26. /**
  27. * @name MarcheService
  28. * @author Julien PIERRE
  29. * @since 13/07/2011
  30. * @desc Classe Service d'un Marche
  31. */
  32. class MarcheService
  33. {
  34. /**
  35. * @name insert($pMarche)
  36. * @param MarcheVO
  37. * @return integer
  38. * @desc Insčre une nouvelle ligne dans la table, ŕ partir des informations de la CommandeVO en paramčtre (l'id sera automatiquement calculé par la BDD)
  39. */
  40. public function insert($pMarche) {
  41. // Entęte du marché
  42. $lMarche = new CommandeVO();
  43. $lMarche->setNom($pMarche->getNom());
  44. $lMarche->setDescription($pMarche->getDescription());
  45. $lMarche->setDateMarcheDebut($pMarche->getDateMarcheDebut());
  46. $lMarche->setDateMarcheFin($pMarche->getDateMarcheFin());
  47. $lMarche->setDateDebutReservation($pMarche->getDateDebutReservation());
  48. $lMarche->setDateFinReservation($pMarche->getDateFinReservation());
  49. $lMarche->setArchive($pMarche->getArchive());
  50. $lIdMarche = CommandeManager::insert($lMarche);
  51. // Le Numéro du marche
  52. $lMarche->setId($lIdMarche);
  53. $lMarche->setNumero($lIdMarche);
  54. CommandeManager::update($lMarche);
  55. // Le détail du marché
  56. if($lIdMarche != null) {
  57. $lReservationAbonnement = array();
  58. $lAbonnementService = new AbonnementService();
  59. $lStockService = new StockService();
  60. foreach($pMarche->getProduits() as $lNouveauProduit) {
  61. //$lProducteur = ProducteurManager::select($lNouveauProduit->getIdProducteur());
  62. $lComptes = CompteNomProduitViewManager::select($lNouveauProduit->getIdNom());
  63. $lComptes = $lComptes[0];
  64. $lIdCompteFerme = $lComptes->getFerIdCompte();
  65. // Insertion du produit
  66. $lProduit = new ProduitVO();
  67. $lProduit->setIdCommande($lIdMarche);
  68. $lProduit->setIdNomProduit($lNouveauProduit->getIdNom());
  69. $lProduit->setUniteMesure($lNouveauProduit->getUnite());
  70. if($lNouveauProduit->getQteMaxCommande() == "" || $lNouveauProduit->getQteMaxCommande() == -1) {
  71. $lProduit->setMaxProduitCommande(-1);
  72. } else {
  73. $lProduit->setMaxProduitCommande($lNouveauProduit->getQteMaxCommande());
  74. }
  75. $lProduit->setIdCompteFerme($lIdCompteFerme);
  76. if($lNouveauProduit->getQteRestante() == "" || $lNouveauProduit->getQteRestante() == -1) {
  77. $lProduit->setStockReservation(0);
  78. $lProduit->setStockInitial(-1);
  79. } else {
  80. $lProduit->setStockReservation($lNouveauProduit->getQteRestante());
  81. $lProduit->setStockInitial($lNouveauProduit->getQteRestante());
  82. }
  83. $lProduit->setType($lNouveauProduit->getType());
  84. $lIdProduit = ProduitManager::insert($lProduit);
  85. //Insertion des lots
  86. $lCorrespondanceLotAbonnement = array();
  87. foreach($lNouveauProduit->getLots() as $lNouveauLot) {
  88. $lDetailCommande = new DetailCommandeVO();
  89. $lDetailCommande->setIdProduit($lIdProduit);
  90. $lDetailCommande->setTaille($lNouveauLot->getTaille());
  91. $lDetailCommande->setPrix($lNouveauLot->getPrix());
  92. $lDcomId = DetailCommandeManager::insert($lDetailCommande);
  93. $lCorrespondanceLotAbonnement[$lNouveauLot->getId()] = $lDcomId;
  94. }
  95. //Insertion du stock -> Met ŕ jour le stock reservation dans le produit
  96. $lStock = new StockVO();
  97. if($lNouveauProduit->getQteRestante() == "" || $lNouveauProduit->getQteRestante() == -1) {
  98. $lStock->setQuantite(0);
  99. } else {
  100. $lStock->setQuantite($lNouveauProduit->getQteRestante());
  101. }
  102. $lStock->setType(0);
  103. $lStock->setIdCompte($lIdCompteFerme);
  104. $lStock->setIdDetailCommande($lDcomId);
  105. //$lStock->setIdOperation(0);
  106. $lStockService->set($lStock);
  107. // Ajout des réservations pour abonnement
  108. if($lProduit->getType() == 2) {
  109. $lIdNomProduit = $lNouveauProduit->getIdNom();
  110. $lAbonnes = $lAbonnementService->getAbonnesByIdNomProduit($lIdNomProduit);
  111. if(!is_null($lAbonnes[0]->getCptAboIdProduitAbonnement())) { // Si il y a des abonnés
  112. foreach($lAbonnes as $lAbonne) {
  113. // Pas de suspension de l'abonnement
  114. if( (! (TestFonction::dateTimeEstPLusGrandeEgale($pMarche->getDateMarcheDebut(),$lAbonne->getCptAboDateDebutSuspension(),'db')
  115. && TestFonction::dateTimeEstPLusGrandeEgale($lAbonne->getCptAboDateFinSuspension(),$pMarche->getDateMarcheDebut(),'db') )
  116. ) && (
  117. ! (TestFonction::dateTimeEstPLusGrandeEgale($pMarche->getDateMarcheFin(),$lAbonne->getCptAboDateDebutSuspension(),'db')
  118. && TestFonction::dateTimeEstPLusGrandeEgale($lAbonne->getCptAboDateFinSuspension(),$pMarche->getDateMarcheFin(),'db') )
  119. )
  120. ) {
  121. $lIdCompte = $lAbonne->getCptAboIdCompte();
  122. if(!isset($lReservationAbonnement[$lIdCompte])) {
  123. $lReservationAbonnement[$lIdCompte] = array("idCompte" => $lIdCompte, "produits" => array());
  124. }
  125. $lReservationAbonnement[$lIdCompte]["produits"][$lIdNomProduit] = array("id" => $lIdNomProduit, "idLot" => $lCorrespondanceLotAbonnement[$lAbonne->getCptAboIdLotAbonnement()], "quantite" => $lAbonne->getCptAboQuantite());
  126. }
  127. }
  128. }
  129. }
  130. }
  131. // Positionnement des réservations
  132. $lReservationService = new ReservationService();
  133. foreach($lReservationAbonnement as $lReservation) {
  134. $lReservationVO = new ReservationVO();
  135. $lReservationVO->getId()->setIdCompte( $lReservation["idCompte"] );
  136. $lReservationVO->getId()->setIdCommande( $lIdMarche );
  137. foreach($lReservation["produits"] as $lDetail){
  138. $lDetailCommande = DetailCommandeManager::select($lDetail["idLot"]);
  139. $lPrix = $lDetail["quantite"] / $lDetailCommande->getTaille() * $lDetailCommande->getPrix();
  140. $lDetailReservation = new DetailReservationVO();
  141. $lDetailReservation->setIdDetailCommande($lDetail["idLot"]);
  142. $lDetailReservation->setQuantite($lDetail["quantite"] * -1);
  143. $lDetailReservation->setMontant($lPrix * -1);
  144. $lReservationVO->addDetailReservation($lDetailReservation);
  145. }
  146. $lReservationService->set($lReservationVO);
  147. }
  148. }
  149. return $lIdMarche;
  150. }
  151. /**
  152. * @name ajoutProduit($pProduit)
  153. * @param ProduitMarcheVO
  154. * @desc Ajoute une produit au marche
  155. */
  156. public function ajoutProduit($pProduit) {
  157. $lComptes = CompteNomProduitViewManager::select($pProduit->getIdNom());
  158. $lComptes = $lComptes[0];
  159. $lIdCompteFerme = $lComptes->getFerIdCompte();
  160. // Insertion du produit
  161. $lProduit = new ProduitVO();
  162. $lProduit->setIdCommande($pProduit->getId());
  163. $lProduit->setIdNomProduit($pProduit->getIdNom());
  164. $lProduit->setUniteMesure($pProduit->getUnite());
  165. if($pProduit->getQteMaxCommande() == "" || $pProduit->getQteMaxCommande() == -1) {
  166. $lProduit->setMaxProduitCommande(-1);
  167. } else {
  168. $lProduit->setMaxProduitCommande($pProduit->getQteMaxCommande());
  169. }
  170. $lProduit->setIdCompteFerme($lIdCompteFerme);
  171. if($pProduit->getQteRestante() == "" || $pProduit->getQteRestante() == -1) {
  172. $lProduit->setStockReservation(0);
  173. $lProduit->setStockInitial(-1);
  174. } else {
  175. $lProduit->setStockReservation($pProduit->getQteRestante());
  176. $lProduit->setStockInitial($pProduit->getQteRestante());
  177. }
  178. $lProduit->setType($pProduit->getType());
  179. // var_dump($lProduit);
  180. $lIdProduit = ProduitManager::insert($lProduit);
  181. //Insertion des lots
  182. $lCorrespondanceLotAbonnement = array();
  183. foreach($pProduit->getLots() as $lNouveauLot) {
  184. $lDetailCommande = new DetailCommandeVO();
  185. $lDetailCommande->setIdProduit($lIdProduit);
  186. $lDetailCommande->setTaille($lNouveauLot->getTaille());
  187. $lDetailCommande->setPrix($lNouveauLot->getPrix());
  188. $lDcomId = DetailCommandeManager::insert($lDetailCommande);
  189. $lCorrespondanceLotAbonnement[$lNouveauLot->getId()] = $lDcomId;
  190. }
  191. $lStockService = new StockService();
  192. //Insertion du stock -> Met ŕ jour le stock reservation dans le produit
  193. $lStock = new StockVO();
  194. if($pProduit->getQteRestante() == "" || $pProduit->getQteRestante() == -1) {
  195. $lStock->setQuantite(0);
  196. } else {
  197. $lStock->setQuantite($pProduit->getQteRestante());
  198. }
  199. $lStock->setType(0);
  200. $lStock->setIdCompte($lIdCompteFerme);
  201. $lStock->setIdDetailCommande($lDcomId);
  202. //$lStock->setIdOperation(0);
  203. $lStockService->set($lStock);
  204. // Ajout des réservations pour abonnement
  205. if($lProduit->getType() == 2) {
  206. $lIdMarche = $lProduit->getIdCommande();
  207. $lMarche = $this->getInfoMarche($lIdMarche);
  208. $lAbonnementService = new AbonnementService();
  209. $lReservationService = new ReservationService();
  210. $lIdNomProduit = $lProduit->getIdNomProduit();
  211. $lAbonnes = $lAbonnementService->getAbonnesByIdNomProduit($lIdNomProduit);
  212. if(!is_null($lAbonnes[0]->getCptAboIdProduitAbonnement())) { // Si il y a des abonnés
  213. foreach($lAbonnes as $lAbonne) {
  214. // Pas de suspension de l'abonnement
  215. if( (! (TestFonction::dateTimeEstPLusGrandeEgale($lMarche->getDateMarcheDebut(),$lAbonne->getCptAboDateDebutSuspension(),'db')
  216. && TestFonction::dateTimeEstPLusGrandeEgale($lAbonne->getCptAboDateFinSuspension(),$lMarche->getDateMarcheDebut(),'db') )
  217. ) && (
  218. ! (TestFonction::dateTimeEstPLusGrandeEgale($lMarche->getDateMarcheFin(),$lAbonne->getCptAboDateDebutSuspension(),'db')
  219. && TestFonction::dateTimeEstPLusGrandeEgale($lAbonne->getCptAboDateFinSuspension(),$lMarche->getDateMarcheFin(),'db') )
  220. )
  221. ) {
  222. $lIdCompte = $lAbonne->getCptAboIdCompte();
  223. $lIdReservationVO = new IdReservationVO();
  224. $lIdReservationVO->setIdCompte( $lIdCompte );
  225. $lIdReservationVO->setIdCommande( $lIdMarche );
  226. $lReservationVO = new ReservationVO();
  227. $lReservationVO->setId($lIdReservationVO);
  228. if($lReservationService->enCours($lIdReservationVO)) {
  229. $lReservationVO = $lReservationService->get($lIdReservationVO);
  230. }
  231. $lDetailCommande = DetailCommandeManager::select($lCorrespondanceLotAbonnement[$lAbonne->getCptAboIdLotAbonnement()]);
  232. $lPrix = $lAbonne->getCptAboQuantite() / $lDetailCommande->getTaille() * $lDetailCommande->getPrix();
  233. $lDetailReservation = new DetailReservationVO();
  234. $lDetailReservation->setIdDetailCommande($lCorrespondanceLotAbonnement[$lAbonne->getCptAboIdLotAbonnement()]);
  235. $lDetailReservation->setQuantite($lAbonne->getCptAboQuantite() * -1);
  236. $lDetailReservation->setMontant($lPrix * -1);
  237. $lReservationVO->addDetailReservation($lDetailReservation);
  238. $lReservationService->set($lReservationVO);
  239. }
  240. }
  241. }
  242. }
  243. }
  244. /**
  245. * @name update($pMarche)
  246. * @param MarcheVO
  247. * @return integer
  248. * @desc Insčre une nouvelle ligne dans la table, ŕ partir des informations de la CommandeVO en paramčtre (l'id sera automatiquement calculé par la BDD)
  249. */
  250. public function update($pMarche) {
  251. $lIdMarche = $pMarche->getId();
  252. $lMarche = new CommandeVO();
  253. $lMarche->setId($lIdMarche);
  254. $lMarche->setNumero($pMarche->getNumero());
  255. $lMarche->setNom($pMarche->getNom());
  256. $lMarche->setDescription($pMarche->getDescription());
  257. $lMarche->setDateMarcheDebut($pMarche->getDateMarcheDebut());
  258. $lMarche->setDateMarcheFin($pMarche->getDateMarcheFin());
  259. $lMarche->setDateDebutReservation($pMarche->getDateDebutReservation());
  260. $lMarche->setDateFinReservation($pMarche->getDateFinReservation());
  261. $lMarche->setArchive($pMarche->getArchive());
  262. CommandeManager::update($lMarche); // Maj des infos de la commande
  263. $lMarcheActuel = $this->get($lIdMarche);
  264. if($lIdMarche != null && $lMarcheActuel->getId() != null) {
  265. $lStockService = new StockService();
  266. foreach($lMarcheActuel->getProduits() as $lProduitActuel) {
  267. $lMaj = true;
  268. // Produits Modifiés
  269. foreach($pMarche->getProduits() as $lProduitNv) {
  270. if($lProduitActuel->getId() == $lProduitNv->getId()) {
  271. $lMaj = false;
  272. //Les lots
  273. foreach($lProduitActuel->getLots() as $lLotActuel) {
  274. $lMajLot = true;
  275. foreach($lProduitNv->getLots() as $lLotNv) {
  276. // Maj Lot
  277. if($lLotActuel->getId() == $lLotNv->getId()) {
  278. $lDcomId = $lLotActuel->getId();
  279. $lMajLot = false;
  280. $lDetailCommande = new DetailCommandeVO();
  281. $lDetailCommande->setId($lLotActuel->getId());
  282. $lDetailCommande->setIdProduit($lProduitActuel->getId());
  283. $lDetailCommande->setTaille($lLotNv->getTaille());
  284. $lDetailCommande->setPrix($lLotNv->getPrix());
  285. DetailCommandeManager::update($lDetailCommande);
  286. }
  287. }
  288. // Supprimer Lot
  289. if($lMajLot) {
  290. $lDeleteLot = DetailCommandeManager::select($lLotActuel->getId());
  291. $lDeleteLot->setEtat(1);
  292. DetailCommandeManager::update($lDeleteLot);
  293. }
  294. }
  295. // Nouveau Lot
  296. foreach($lProduitNv->getLots() as $lLotNv) {
  297. $lAjout = true;
  298. foreach($lProduitActuel->getLots() as $lLotActuel) {
  299. if($lLotActuel->getId() == $lLotNv->getId()) {
  300. $lAjout = false;
  301. }
  302. }
  303. if($lAjout) {
  304. $lDetailCommande = new DetailCommandeVO();
  305. $lDetailCommande->setIdProduit($lProduitActuel->getId());
  306. $lDetailCommande->setTaille($lLotNv->getTaille());
  307. $lDetailCommande->setPrix($lLotNv->getPrix());
  308. $lDcomId = DetailCommandeManager::insert($lDetailCommande);
  309. }
  310. }
  311. $lResaActuel = GestionCommandeReservationProducteurViewManager::getStockReservationProducteur($lProduitActuel->getIdCompteProducteur(),$lProduitActuel->getId());
  312. $lStockActuel = $lStockService->get($lResaActuel[0]->getStoId());
  313. // Maj du stock
  314. $lStockActuel->setQuantite($lProduitNv->getQteRestante());
  315. $lStockActuel->setIdCompte($lProduitNv->getIdProducteur());
  316. $lStockActuel->setIdDetailCommande($lDcomId);
  317. $lStockService->set($lStockActuel);
  318. $lProduit = ProduitManager::select($lProduitActuel->getId());
  319. $lProduit->setIdCommande($lIdMarche);
  320. $lProduit->setIdNomProduit($lProduitNv->getIdNom());
  321. $lProduit->setUniteMesure($lProduitNv->getUnite());
  322. $lProduit->setMaxProduitCommande($lProduitNv->getQteMaxCommande());
  323. $lProduit->setIdCompteProducteur($lProduitNv->getIdProducteur()); // C'est bien le compte il faut changer le nom du champ
  324. $lProduit->setType($lProduitNv->getType());
  325. ProduitManager::update($lProduit);
  326. }
  327. }
  328. // Produits supprimés
  329. if($lMaj) {
  330. // Suppression des lots
  331. $lLots = DetailCommandeManager::selectByIdProduit($lProduitActuel->getId());
  332. foreach($lLots as $lLot) {
  333. $lLot->setEtat(1);
  334. DetailCommandeManager::update($lLot);
  335. }
  336. $lProduit = new ProduitVO();
  337. $lProduit->setId($lProduitActuel->getId());
  338. $lProduit->setIdCommande($lIdMarche);
  339. $lProduit->setIdNomProduit($lProduitActuel->getIdNom());
  340. $lProduit->setUniteMesure($lProduitActuel->getUnite());
  341. $lProduit->setMaxProduitCommande($lProduitActuel->getQteMaxCommande());
  342. $lProduit->setIdCompteProducteur($lProduitActuel->getIdCompteProducteur());
  343. $lProduit->setType($lProduitActuel->getType());
  344. $lProduit->setEtat(1);
  345. ProduitManager::update($lProduit);
  346. }
  347. }
  348. // Les nouveaux produits
  349. foreach($pMarche->getProduits() as $lProduitNv) {
  350. $lAjout = true;
  351. foreach($lMarcheActuel->getProduits() as $lProduitActuel) {
  352. if($lProduitActuel->getId() == $lProduitNv->getId()) {
  353. $lAjout = false;
  354. }
  355. }
  356. if($lAjout) {
  357. // Insertion du produit
  358. $lProduit = new ProduitVO();
  359. $lProduit->setIdCommande($lIdMarche);
  360. $lProduit->setIdNomProduit($lProduitNv->getIdNom());
  361. $lProduit->setUniteMesure($lProduitNv->getUnite());
  362. $lProduit->setMaxProduitCommande($lProduitNv->getQteMaxCommande());
  363. $lProduit->setIdCompteProducteur($lProduitNv->getIdProducteur()); // C'est bien le compte il faut changer le nom du champ
  364. $lProduit->setStockReservation($lProduitNv->getQteRestante());
  365. $lProduit->setStockInitial($lProduitNv->getQteRestante());
  366. $lProduit->setType($lProduitNv->getType());
  367. $lIdProduit = ProduitManager::insert($lProduit);
  368. //Insertion des lots
  369. foreach($lProduitNv->getLots() as $lNouveauLot) {
  370. $lDetailCommande = new DetailCommandeVO();
  371. $lDetailCommande->setIdProduit($lIdProduit);
  372. $lDetailCommande->setTaille($lNouveauLot->getTaille());
  373. $lDetailCommande->setPrix($lNouveauLot->getPrix());
  374. $lDcomId = DetailCommandeManager::insert($lDetailCommande);
  375. }
  376. //Insertion du stock -> Met ŕ jour le stock reservation dans le produit
  377. $lStock = new StockVO();
  378. $lStock->setQuantite($lProduitNv->getQteRestante());
  379. $lStock->setType(0);
  380. $lStock->setIdCompte($lProduitNv->getIdProducteur());// C'est bien le compte il faut changer le nom du champ
  381. $lStock->setIdDetailCommande($lDcomId);
  382. $lStockService = new StockService();
  383. $lStockService->set($lStock);
  384. }
  385. }
  386. }
  387. return $lIdMarche;
  388. }
  389. /**
  390. * @name updateInformation($pMarche)
  391. * @param MarcheVO
  392. * @return integer
  393. * @desc Met ŕ jour l'entęte du marché
  394. */
  395. public function updateInformation($pMarche) {
  396. $lIdMarche = $pMarche->getId();
  397. $lMarche = $this->getInfoMarche($lIdMarche);
  398. $lMarche->setNom($pMarche->getNom());
  399. $lMarche->setDescription($pMarche->getDescription());
  400. $lMarche->setDateMarcheDebut($pMarche->getDateMarcheDebut());
  401. $lMarche->setDateMarcheFin($pMarche->getDateMarcheFin());
  402. $lMarche->setDateDebutReservation($pMarche->getDateDebutReservation());
  403. $lMarche->setDateFinReservation($pMarche->getDateFinReservation());
  404. CommandeManager::update($lMarche); // Maj des infos de la commande
  405. return $lIdMarche;
  406. }
  407. /**
  408. * @name updateProduit($pProduit)
  409. * @param ProduitVO
  410. * @desc Met ŕ jour le produit du marché
  411. */
  412. public function updateProduit($pProduit, $pLotRemplacement = array()) {
  413. $lProduitActuel = $this->selectProduit($pProduit->getId());
  414. //Les lots
  415. $lLotModif = array();
  416. $lLotSupp = array();
  417. foreach($lProduitActuel->getLots() as $lLotActuel) {
  418. $lMajLot = true;
  419. foreach($pProduit->getLots() as $lLotNv) {
  420. // Maj Lot
  421. if($lLotActuel->getId() == $lLotNv->getId()) {
  422. $lDcomId = $lLotActuel->getId();
  423. $lMajLot = false;
  424. $lDetailCommande = new DetailCommandeVO();
  425. $lDetailCommande->setId($lLotActuel->getId());
  426. $lDetailCommande->setIdProduit($lProduitActuel->getId());
  427. $lDetailCommande->setTaille($lLotNv->getTaille());
  428. $lDetailCommande->setPrix($lLotNv->getPrix());
  429. DetailCommandeManager::update($lDetailCommande);
  430. array_push($lLotModif,$lDetailCommande);
  431. }
  432. }
  433. // Supprimer Lot
  434. if($lMajLot) {
  435. $lDeleteLot = DetailCommandeManager::select($lLotActuel->getId());
  436. $lDeleteLot->setEtat(1);
  437. DetailCommandeManager::update($lDeleteLot);
  438. array_push($lLotSupp,$lLotActuel->getId());
  439. }
  440. }
  441. // Nouveau Lot
  442. $lLotAdd = array();
  443. foreach($pProduit->getLots() as $lLotNv) {
  444. $lAjout = true;
  445. foreach($lProduitActuel->getLots() as $lLotActuel) {
  446. if($lLotActuel->getId() == $lLotNv->getId()) {
  447. $lAjout = false;
  448. }
  449. }
  450. if($lAjout) {
  451. $lDetailCommande = new DetailCommandeVO();
  452. $lDetailCommande->setIdProduit($lProduitActuel->getId());
  453. $lDetailCommande->setTaille($lLotNv->getTaille());
  454. $lDetailCommande->setPrix($lLotNv->getPrix());
  455. $lDcomId = DetailCommandeManager::insert($lDetailCommande);
  456. $lLotAdd[$lLotNv->getId()] = $lDcomId; // Si supression d'un lot et positionnement de ce nouveau lot permet de récupérer l'ID
  457. }
  458. }
  459. $lStockService = new StockService();
  460. $lResaActuel = GestionCommandeReservationProducteurViewManager::getStockReservationProducteur($lProduitActuel->getIdCompteFerme(),$lProduitActuel->getId());
  461. $lStockActuel = $lStockService->get($lResaActuel[0]->getStoId());
  462. // Maj du stock
  463. $lStockActuel->setQuantite($pProduit->getQteRestante());
  464. $lStockActuel->setIdDetailCommande($lDcomId);
  465. $lStockService->updateStockProduit($lStockActuel);
  466. $lProduit = ProduitManager::select($lProduitActuel->getId());
  467. $lProduit->setUniteMesure($pProduit->getUnite());
  468. if($pProduit->getQteMaxCommande() == "" || $pProduit->getQteMaxCommande() == -1) {
  469. $lProduit->setMaxProduitCommande(-1);
  470. } else {
  471. $lProduit->setMaxProduitCommande($pProduit->getQteMaxCommande());
  472. }
  473. $lProduit->setType($pProduit->getType());
  474. ProduitManager::update($lProduit);
  475. // Modif des réservations
  476. $lReservationService = new ReservationService();
  477. $lIdMarche = $lProduitActuel->getIdMarche();
  478. //var_dump($lLotModif);
  479. /*foreach($lLotModif as $lLot) { // Chaque lot modifié
  480. $lListeDetailReservation = $lReservationService->getReservationSurLot($lLot->getId());
  481. if(!is_null($lListeDetailReservation[0]->getDopeIdCompte())) { // Si il y a des réservations
  482. foreach($lListeDetailReservation as $lDetailReservation) { // Chaque réservation de lot modifié
  483. $lIdReservationVO = new IdReservationVO();
  484. $lIdReservationVO->setIdCompte( $lDetailReservation->getDopeIdCompte() );
  485. $lIdReservationVO->setIdCommande( $lIdMarche );
  486. $lReservationVO = $lReservationService->get($lIdReservationVO);
  487. $lNvDetailReservation = array();
  488. foreach($lReservationVO->getDetailReservation() as $lDetailReservationActuelle) {
  489. if($lDetailReservationActuelle->getIdDetailCommande() == $lLot->getId()) { // Maj de la reservation pour ce produit
  490. $lPrix = $lDetailReservation->getStoQuantite() / $lLot->getTaille() * $lLot->getPrix();
  491. $lDetailReservationVO = new DetailReservationVO();
  492. $lDetailReservationVO->setIdDetailCommande($lLot->getId());
  493. $lDetailReservationVO->setQuantite($lDetailReservation->getStoQuantite());
  494. $lDetailReservationVO->setMontant($lPrix);
  495. array_push($lNvDetailReservation,$lDetailReservationVO);
  496. } else { // Ajout des autres produits
  497. array_push($lNvDetailReservation,$lDetailReservationActuelle);
  498. }
  499. }
  500. $lReservationVO->setDetailReservation($lNvDetailReservation);
  501. $lReservationService->set($lReservationVO); // Maj de la reservation
  502. }
  503. }
  504. }*/
  505. foreach($lLotSupp as $lIdLot) { // Chaque lot supprimé => La réservation est positionnée sur un autre lot
  506. if(isset($pLotRemplacement[$lIdLot]) ) {
  507. $lIdLotRemplacement = $pLotRemplacement[$lIdLot];
  508. if($lIdLotRemplacement < 0) {
  509. $lIdLotRemplacement = $lLotAdd[$lIdLotRemplacement];
  510. }
  511. $lListeDetailReservation = $lReservationService->getReservationSurLot($lIdLot);
  512. if(!is_null($lListeDetailReservation[0]->getDopeIdCompte())) { // Si il y a des réservations
  513. foreach($lListeDetailReservation as $lDetailReservation) { // Chaque réservation de lot modifié
  514. $lIdReservationVO = new IdReservationVO();
  515. $lIdReservationVO->setIdCompte( $lDetailReservation->getDopeIdCompte() );
  516. $lIdReservationVO->setIdCommande( $lIdMarche );
  517. $lReservationVO = $lReservationService->get($lIdReservationVO);
  518. $lNvDetailReservation = array();
  519. foreach($lReservationVO->getDetailReservation() as $lDetailReservationActuelle) {
  520. if($lDetailReservationActuelle->getIdDetailCommande() == $lIdLot) { // Maj de la reservation pour ce produit
  521. $lDetailCommande = DetailCommandeManager::select($lIdLotRemplacement);
  522. $lPrix = $lDetailReservation->getStoQuantite() / $lDetailCommande->getTaille() * $lDetailCommande->getPrix();
  523. $lDetailReservationVO = new DetailReservationVO();
  524. $lDetailReservationVO->setIdDetailCommande($lIdLotRemplacement);
  525. $lDetailReservationVO->setQuantite($lDetailReservation->getStoQuantite());
  526. $lDetailReservationVO->setMontant($lPrix);
  527. array_push($lNvDetailReservation,$lDetailReservationVO);
  528. } else { // Ajout des autres produits
  529. array_push($lNvDetailReservation,$lDetailReservationActuelle);
  530. }
  531. }
  532. $lReservationVO->setDetailReservation($lNvDetailReservation);
  533. $lReservationService->set($lReservationVO); // Maj de la reservation
  534. }
  535. }
  536. }
  537. }
  538. }
  539. /**
  540. * @name supprimerProduit($pId)
  541. * @param integer
  542. * @desc Supprime un produit du marché
  543. */
  544. public function supprimerProduit($pId) {
  545. $lProduit = ProduitManager::select($pId);
  546. // Suppression des lots
  547. $lLots = DetailCommandeManager::selectByIdProduit($pId);
  548. foreach($lLots as $lLot) {
  549. $lLot->setEtat(1);
  550. DetailCommandeManager::update($lLot);
  551. }
  552. $lProduit->setEtat(1);
  553. ProduitManager::update($lProduit);
  554. // Modif des réservations
  555. $lReservationService = new ReservationService();
  556. $lIdMarche = $lProduit->getIdCommande();
  557. //var_dump($lLotModif);
  558. foreach($lLots as $lLot) { // Chaque lot modifié
  559. $lListeDetailReservation = $lReservationService->getReservationSurLot($lLot->getId());
  560. if(!is_null($lListeDetailReservation[0]->getDopeIdCompte())) { // Si il y a des réservations
  561. foreach($lListeDetailReservation as $lDetailReservation) { // Chaque réservation de lot modifié
  562. $lIdReservationVO = new IdReservationVO();
  563. $lIdReservationVO->setIdCompte( $lDetailReservation->getDopeIdCompte() );
  564. $lIdReservationVO->setIdCommande( $lIdMarche );
  565. $lReservationVO = $lReservationService->get($lIdReservationVO);
  566. $lNvDetailReservation = array();
  567. foreach($lReservationVO->getDetailReservation() as $lDetailReservationActuelle) {
  568. if($lDetailReservationActuelle->getIdDetailCommande() != $lLot->getId()) { // Ne positionne que les autres produits
  569. array_push($lNvDetailReservation,$lDetailReservationActuelle);
  570. }
  571. }
  572. $lReservationVO->setDetailReservation($lNvDetailReservation);
  573. $lReservationService->set($lReservationVO); // Maj de la reservation
  574. }
  575. }
  576. }
  577. }
  578. /**
  579. * @name getNonReserveeParCompte($pIdCompte)
  580. * @param integer
  581. * @return array(CommandeVO)
  582. * @desc Récupčres les commandes en cours non réservées par l'adhérent
  583. */
  584. public function getNonReserveeParCompte($pIdCompte) { // TODO les tests
  585. return CommandeManager::selectNonReserveeParCompte($pIdCompte);
  586. }
  587. /**
  588. * @name getNonAchatParCompte($pIdCompte)
  589. * @param integer
  590. * @return array(CommandeVO)
  591. * @desc Récupčres les commandes en cours sans achat par l'adhérent
  592. */
  593. public function getNonAchatParCompte($pIdCompte) { // TODO les tests
  594. return CommandeManager::selectNonAchatParCompte($pIdCompte);
  595. }
  596. /**
  597. * @name get($pId)
  598. * @param integer
  599. * @return array(CommandeVO) ou CommandeVO
  600. * @desc Retourne une liste de Commande
  601. */
  602. public function get($pId = null) {
  603. if($pId != null) {
  604. if(is_int((int)$pId)) {
  605. return $this->select($pId);
  606. } else {
  607. return false;
  608. }
  609. } else {
  610. return $this->selectAll();
  611. }
  612. }
  613. /**
  614. * @name select($pId)
  615. * @param integer
  616. * @return MarcheVO
  617. * @desc Retourne une Commande
  618. */
  619. public function select($pId) {
  620. // Information du marche
  621. /*$lMarche->setId($lDetailMarche[0]->getComId());
  622. $lMarche->setNumero($lDetailMarche[0]->getComNumero());
  623. $lMarche->setNom($lDetailMarche[0]->getComNom());
  624. $lMarche->setDescription($lDetailMarche[0]->getComDescription());
  625. $lMarche->setDateMarcheDebut($lDetailMarche[0]->getComDateMarcheDebut());
  626. $lMarche->setDateMarcheFin($lDetailMarche[0]->getComDateMarcheFin());
  627. $lMarche->setDateDebutReservation($lDetailMarche[0]->getComDateDebutReservation());
  628. $lMarche->setDateFinReservation($lDetailMarche[0]->getComDateFinReservation());
  629. $lMarche->setArchive($lDetailMarche[0]->getComArchive());*/
  630. $lInfoMarche = $this->getInfoMarche($pId);
  631. $lMarche = new MarcheVO();
  632. // Information du marche
  633. $lMarche->setId($lInfoMarche->getId());
  634. $lMarche->setNumero($lInfoMarche->getNumero());
  635. $lMarche->setNom($lInfoMarche->getNom());
  636. $lMarche->setDescription($lInfoMarche->getDescription());
  637. $lMarche->setDateMarcheDebut($lInfoMarche->getDateMarcheDebut());
  638. $lMarche->setDateMarcheFin($lInfoMarche->getDateMarcheFin());
  639. $lMarche->setDateDebutReservation($lInfoMarche->getDateDebutReservation());
  640. $lMarche->setDateFinReservation($lInfoMarche->getDateFinReservation());
  641. $lMarche->setArchive($lInfoMarche->getArchive());
  642. $lDetailMarche = DetailMarcheViewManager::select($pId);
  643. foreach($lDetailMarche as $lDetail) {
  644. if($lDetail->getProId() != '') {
  645. // Le Produit
  646. $lProduits = $lMarche->getProduits();
  647. if(!isset($lProduits[$lDetail->getProId()])) {
  648. $lProduit = new ProduitMarcheVO();
  649. $lProduit->setId($lDetail->getProId());
  650. $lProduit->setIdCompteFerme($lDetail->getProIdCompteFerme());
  651. $lProduit->setIdNom($lDetail->getNproId());
  652. $lProduit->setNom($lDetail->getNproNom());
  653. $lProduit->setDescription($lDetail->getNproDescription());
  654. $lProduit->setIdCategorie($lDetail->getNproIdCategorie());
  655. $lProduit->setCproNom($lDetail->getCproNom());
  656. $lProduit->setUnite($lDetail->getProUniteMesure());
  657. $lProduit->setQteMaxCommande($lDetail->getProMaxProduitCommande());
  658. $lProduit->setStockReservation($lDetail->getProStockReservation());
  659. $lProduit->setStockInitial($lDetail->getProStockInitial());
  660. $lProduit->setType($lDetail->getProType());
  661. $lProduit->setFerId($lDetail->getFerId());
  662. $lProduit->setFerNom($lDetail->getFerNom());
  663. $lProduits[$lDetail->getProId()] = $lProduit;
  664. }
  665. // Le Lot
  666. $lLot = new DetailMarcheVO();
  667. $lLot->setId($lDetail->getDcomId());
  668. $lLot->setTaille($lDetail->getDcomTaille());
  669. $lLot->setPrix($lDetail->getDcomPrix());
  670. $lLots = $lProduits[$lDetail->getProId()]->getLots();
  671. $lLots[$lDetail->getDcomId()] = $lLot;
  672. $lProduits[$lDetail->getProId()]->setLots($lLots);
  673. $lMarche->setProduits($lProduits);
  674. }
  675. }
  676. return $lMarche;
  677. }
  678. /**
  679. * @name selectProduit($pId)
  680. * @param integer
  681. * @return ProduitMarcheVO
  682. * @desc Retourne un Produit
  683. */
  684. public function selectProduit($pId) {
  685. $lDetailMarche = DetailMarcheViewManager::selectByIdProduit($pId);
  686. $lProduit = new ProduitMarcheVO();
  687. // Le Produit
  688. $lProduit->setId($lDetailMarche[0]->getProId());
  689. $lProduit->setIdMarche($lDetailMarche[0]->getComId());
  690. $lProduit->setIdCompteFerme($lDetailMarche[0]->getProIdCompteFerme());
  691. $lProduit->setIdNom($lDetailMarche[0]->getNproId());
  692. $lProduit->setNom($lDetailMarche[0]->getNproNom());
  693. $lProduit->setDescription($lDetailMarche[0]->getNproDescription());
  694. $lProduit->setIdCategorie($lDetailMarche[0]->getNproIdCategorie());
  695. $lProduit->setCproNom($lDetailMarche[0]->getCproNom());
  696. $lProduit->setUnite($lDetailMarche[0]->getProUniteMesure());
  697. $lProduit->setQteMaxCommande($lDetailMarche[0]->getProMaxProduitCommande());
  698. $lProduit->setStockReservation($lDetailMarche[0]->getProStockReservation());
  699. $lProduit->setStockInitial($lDetailMarche[0]->getProStockInitial());
  700. $lProduit->setType($lDetailMarche[0]->getProType());
  701. $lProduit->setFerId($lDetailMarche[0]->getFerId());
  702. $lProduit->setFerNom($lDetailMarche[0]->getFerNom());
  703. $lProduit->setFerNom($lDetailMarche[0]->getFerNom());
  704. foreach($lDetailMarche as $lDetail) {
  705. // Le Lot
  706. $lLot = new DetailMarcheVO();
  707. $lLot->setId($lDetail->getDcomId());
  708. $lLot->setTaille($lDetail->getDcomTaille());
  709. $lLot->setPrix($lDetail->getDcomPrix());
  710. $lProduit->addLots($lLot);
  711. }
  712. return $lProduit;
  713. }
  714. /**
  715. * @name selectAll()
  716. * @return array(CommandeVO)
  717. * @desc Retourne une liste de Commande
  718. */
  719. public function selectAll() {
  720. return CommandeManager::selectAll();
  721. }
  722. /**
  723. * @name setPause($IdMarche)
  724. * @param integer
  725. * @desc Met en pause un marche
  726. */
  727. public function setPause($IdMarche) {
  728. $lMarche = CommandeManager::select($IdMarche);
  729. $lMarche->setArchive(1);
  730. return CommandeManager::update($lMarche);
  731. }
  732. /**
  733. * @name setPlay($IdMarche)
  734. * @param integer
  735. * @desc Met en play un marche
  736. */
  737. public function setPlay($IdMarche) {
  738. $lMarche = CommandeManager::select($IdMarche);
  739. $lMarche->setArchive(0);
  740. return CommandeManager::update($lMarche);
  741. }
  742. /**
  743. * @name setCloturer($IdMarche)
  744. * @param integer
  745. * @desc Cloture le marche
  746. */
  747. public function setCloturer($IdMarche) {
  748. $lMarche = CommandeManager::select($IdMarche);
  749. $lMarche->setArchive(2);
  750. $lOperationService = new OperationService();
  751. // On Passe les opérations réservées non récupérées en statut cloturé
  752. $lListeOperation = $lOperationService->getReservationCommande($IdMarche);
  753. foreach ( $lListeOperation as $lOperation ) {
  754. if($lOperation->getId() != null) {
  755. $lOperation->setTypePaiement(15);
  756. $lOperationService->set( $lOperation );
  757. }
  758. }
  759. return CommandeManager::update($lMarche);
  760. }
  761. /**
  762. * @name getInfoMarche($IdMarche)
  763. * @param integer
  764. * @desc Retourne les infos d'entęte du marche
  765. */
  766. public function getInfoMarche($IdMarche) {
  767. return CommandeManager::select($IdMarche);
  768. }
  769. /**
  770. * @name selectCaisseListeMarche()
  771. * @return array(OperationVO)
  772. * @desc Récupčres toutes les lignes de la table ayant pour IdCompte $pId et les renvoie sous forme d'une collection de OperationVO
  773. */
  774. public function selectCaisseListeMarche() {
  775. return CommandeManager::recherche(
  776. array(CommandeManager::CHAMP_COMMANDE_ARCHIVE),
  777. array('='),
  778. array(0),
  779. array(CommandeManager::CHAMP_COMMANDE_DATE_MARCHE_DEBUT),
  780. array('ASC'));
  781. }
  782. }
  783. ?>