PageRenderTime 79ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/service/AchatService.php

http://zeybux.googlecode.com/
PHP | 807 lines | 467 code | 79 blank | 261 comment | 40 complexity | 252cd1c496ad4b08ae097828cef309d4 MD5 | raw file
  1. <?php
  2. //****************************************************************
  3. //
  4. // Createur : Julien PIERRE
  5. // Date de creation : 22/07/2011
  6. // Fichier : AchatService.php
  7. //
  8. // Description : Classe AchatService
  9. //
  10. //****************************************************************
  11. // Inclusion des classes
  12. /*include_once(CHEMIN_CLASSES_MANAGERS . "ReservationManager.php");
  13. include_once(CHEMIN_CLASSES_MANAGERS . "HistoriqueReservationManager.php");
  14. include_once(CHEMIN_CLASSES_VALIDATEUR . "ReservationValid.php");
  15. include_once(CHEMIN_CLASSES_SERVICE . "CompteService.php" );*/
  16. include_once(CHEMIN_CLASSES_VO . "AchatVO.php");
  17. include_once(CHEMIN_CLASSES_VO . "ListeAchatReservationVO.php");
  18. include_once(CHEMIN_CLASSES_SERVICE . "StockService.php" );
  19. include_once(CHEMIN_CLASSES_SERVICE . "DetailOperationService.php" );
  20. include_once(CHEMIN_CLASSES_SERVICE . "OperationService.php" );
  21. include_once(CHEMIN_CLASSES_VALIDATEUR . "AchatValid.php");
  22. include_once(CHEMIN_CLASSES_VIEW_MANAGER . "AchatDetailSolidaireViewManager.php");
  23. include_once(CHEMIN_CLASSES_VIEW_MANAGER . "AchatDetailViewManager.php");
  24. include_once(CHEMIN_CLASSES_VIEW_MANAGER . "AdherentViewManager.php");
  25. /**
  26. * @name AchatService
  27. * @author Julien PIERRE
  28. * @since 22/07/2011
  29. * @desc Classe Service d'une Achat
  30. */
  31. class AchatService
  32. {
  33. /**
  34. * @name set($pAchat)
  35. * @param AchatVO
  36. * @return integer
  37. * @desc Ajoute ou modifie une réservation
  38. */
  39. public function set($pAchat) {
  40. $lAchatValid = new AchatValid();
  41. if($lAchatValid->insert($pAchat)) {
  42. return $this->insert($pAchat);
  43. } else if($lAchatValid->updateReservation($pAchat)) {
  44. return $this->updateReservation($pAchat);
  45. } else if($lAchatValid->updateAchat($pAchat)) {
  46. return $this->updateAchat($pAchat);
  47. }
  48. return false;
  49. }
  50. /**
  51. * @name insert($pAchat)
  52. * @param AchatVO
  53. * @return integer
  54. * @desc Ajoute une réservation
  55. */
  56. private function insert($pAchat) {
  57. // Calcul du total
  58. $lTotal = 0;
  59. foreach($pAchat->getDetailAchat() as $lProduit) {
  60. $lTotal += $lProduit->getMontant();
  61. }
  62. $lOperationService = new OperationService();
  63. if($lTotal < 0) {
  64. // L'operation
  65. $lOperation = new OperationVO();
  66. $lOperation->setIdCompte($pAchat->getId()->getIdCompte());
  67. $lOperation->setMontant($lTotal);
  68. $lOperation->setLibelle("Marché N°" . $pAchat->getId()->getIdCommande());
  69. $lOperation->setTypePaiement(7);
  70. $lOperation->setIdCommande($pAchat->getId()->getIdCommande());
  71. $lIdOperation = $lOperationService->set($lOperation);
  72. // Operation sur le compte Zeybu
  73. $lOperationZeybu = new OperationVO();
  74. $lOperationZeybu->setIdCompte(-1);
  75. $lOperationZeybu->setMontant($lTotal * -1);
  76. $lOperationZeybu->setLibelle("Marché N°" . $pAchat->getId()->getIdCommande());
  77. $lOperationZeybu->setTypePaiement(7);
  78. $lOperationZeybu->setTypePaiementChampComplementaire($lIdOperation);
  79. $lOperationZeybu->setIdCommande($pAchat->getId()->getIdCommande());
  80. $lIdOperationZeybu = $lOperationService->set($lOperationZeybu);
  81. $lOperation->setTypePaiementChampComplementaire($lIdOperationZeybu);
  82. $lOperationService->set($lOperation);
  83. // Ajout detail operation
  84. $lStockService = new StockService;
  85. $lDetailOperationService = new DetailOperationService;
  86. foreach($pAchat->getDetailAchat() as $lProduit) {
  87. // Ajout du stock
  88. $lStock = new StockVO();
  89. $lStock->setQuantite($lProduit->getQuantite());
  90. $lStock->setType(1);
  91. $lStock->setIdCompte($pAchat->getId()->getIdCompte());
  92. $lStock->setIdDetailCommande($lProduit->getIdDetailCommande());
  93. $lStock->setIdOperation($lIdOperation);
  94. $lStockService->set($lStock);
  95. // Ajout du détail de l'operation
  96. $lDetailOperation = new DetailOperationVO();
  97. $lDetailOperation->setIdOperation($lIdOperation);
  98. $lDetailOperation->setIdCompte($pAchat->getId()->getIdCompte());
  99. $lDetailOperation->setMontant($lProduit->getMontant());
  100. $lDetailOperation->setLibelle("Marché N°" . $pAchat->getId()->getIdCommande());
  101. $lDetailOperation->setTypePaiement(7);
  102. $lDetailOperation->setIdDetailCommande($lProduit->getIdDetailCommande());
  103. $lDetailOperationService->set($lDetailOperation);
  104. }
  105. }
  106. // Ajout des produits solidaire
  107. $lIdOperation = $this->insertProduitAchatSolidaire($pAchat);
  108. return $lIdOperation;
  109. }
  110. /**
  111. * @name updateReservation($pAchat)
  112. * @param AchatVO
  113. * @return integer
  114. * @desc Met à jour une réservation pour la passer en achat
  115. */
  116. private function updateReservation($pAchat) {
  117. $lIdReservation = new IdReservationVO();
  118. $lIdReservation->setIdCompte($pAchat->getId()->getIdCompte());
  119. $lIdReservation->setIdCommande($pAchat->getId()->getIdCommande());
  120. $lReservationService = new ReservationService();
  121. $lReservationActuelle = $lReservationService->get($lIdReservation);
  122. return $this->update($lReservationActuelle,$pAchat,$pAchat->getId()->getIdReservation());
  123. }
  124. /**
  125. * @name updateAchat($pAchat)
  126. * @param AchatVO
  127. * @return integer
  128. * @desc Met à jour un achat
  129. */
  130. private function updateAchat($pAchat) {
  131. $lAchat = $this->get($pAchat->getId());
  132. return $this->update($lAchat,$pAchat,$pAchat->getId()->getIdAchat());
  133. }
  134. /**
  135. * @name update($pAchat)
  136. * @param AchatVO
  137. * @return integer
  138. * @desc Met à jour une réservation
  139. */
  140. private function update($pAchatActuel,$pNouvelAchat,$pIdOperation) {
  141. // Récupération de l'opération
  142. $lOperationService = new OperationService();
  143. $lOperation = $lOperationService->get($pIdOperation);
  144. switch($lOperation->getTypePaiement()) {
  145. case 0: // Une réservation
  146. // Mise à jour du détail
  147. $lTotal = $this->updateProduitReservationAchat($pAchatActuel,$pNouvelAchat,$pIdOperation);
  148. $lIdOperationSolidaire = $this->insertProduitAchatSolidaire($pNouvelAchat);
  149. if($lTotal < 0) {
  150. // Mise à jour de l'opération de réservation en achat
  151. $lOperation->setMontant($lTotal);
  152. $lOperation->setTypePaiement(7);
  153. $lIdOperation = $lOperationService->set($lOperation);
  154. // Operation sur le compte Zeybu
  155. $lOperationZeybu = new OperationVO();
  156. $lOperationZeybu->setIdCompte(-1);
  157. $lOperationZeybu->setMontant($lTotal * -1);
  158. $lOperationZeybu->setLibelle("Marché N°" . $pNouvelAchat->getId()->getIdCommande());
  159. $lOperationZeybu->setTypePaiement(7);
  160. $lOperationZeybu->setTypePaiementChampComplementaire($lIdOperation);
  161. $lOperationZeybu->setIdCommande($pNouvelAchat->getId()->getIdCommande());
  162. $lIdOperationZeybu = $lOperationService->set($lOperationZeybu);
  163. $lOperation->setTypePaiementChampComplementaire($lIdOperationZeybu);
  164. $lOperationService->set($lOperation);
  165. } else {
  166. // Mise à jour de l'opération de réservation en annulation
  167. //$lOperation->setTypePaiement(16);
  168. $lOperationService->delete($lOperation);
  169. }
  170. break;
  171. case 7: // Achat
  172. $lTotal = $this->updateProduitAchat($pAchatActuel,$pNouvelAchat,$pIdOperation);
  173. if($pNouvelAchat->getTotal() < 0) {
  174. // Mise à jour de l'opération d'achat
  175. $lOperation->setMontant($pNouvelAchat->getTotal());
  176. $lOperationService->set($lOperation);
  177. $lOperationZeybu = $lOperationService->get($lOperation->getTypePaiementChampComplementaire());
  178. $lOperationZeybu->setMontant($pNouvelAchat->getTotal() * -1);
  179. $lOperationService->set($lOperationZeybu);
  180. } else {
  181. // Mise à jour de l'opération en annulation
  182. //$lOperation->setTypePaiement(18);
  183. $lOperationService->delete($lOperation->getId());
  184. $lOperationZeybu = $lOperationService->get($lOperation->getTypePaiementChampComplementaire());
  185. //$lOperationZeybu->setTypePaiement(18);
  186. $lOperationService->delete($lOperationZeybu->getId());
  187. }
  188. break;
  189. case 8: // Achat Solidaire
  190. $lTotalSolidaire = $this->updateProduitAchatSolidaire($pAchatActuel,$pNouvelAchat,$pIdOperation);
  191. if($pNouvelAchat->getTotal() < 0) {
  192. // Mise à jour de l'opération d'achat
  193. $lOperation->setMontant($pNouvelAchat->getTotal());
  194. $lOperationService->set($lOperation);
  195. $lOperationZeybu = $lOperationService->get($lOperation->getTypePaiementChampComplementaire());
  196. $lOperationZeybu->setMontant($pNouvelAchat->getTotal() * -1);
  197. $lOperationService->set($lOperationZeybu);
  198. } else {
  199. // Mise à jour de l'opération en annulation
  200. //$lOperation->setTypePaiement(20);
  201. $lOperationService->delete($lOperation->getId());
  202. $lOperationZeybu = $lOperationService->get($lOperation->getTypePaiementChampComplementaire());
  203. //$lOperationZeybu->setTypePaiement(20);
  204. $lOperationService->delete($lOperationZeybu->getId());
  205. }
  206. break;
  207. }
  208. return true;
  209. }
  210. /**
  211. * @name updateProduitAchat($pAchatActuel,$pNouvelAchat,$pIdOperation)
  212. * @param AchatVO
  213. * @param AchatVO
  214. * @param integer
  215. * @return decimal(10,2)
  216. * @desc Met à jour les produits
  217. */
  218. private function updateProduitAchat($pAchatActuel,$pNouvelAchat,$pIdOperation) {
  219. $lTotal = 0;
  220. $lStockService = new StockService();
  221. $lDetailOperationService = new DetailOperationService();
  222. foreach($pAchatActuel->getDetailAchat() as $lAchatActuelle) {
  223. $lTestUpdate = false;
  224. foreach($pNouvelAchat->getDetailAchat() as $lAchatNouvelle) {
  225. if($lAchatActuelle->getIdDetailCommande() == $lAchatNouvelle->getIdDetailCommande()) {
  226. $lTotal += $lAchatNouvelle->getMontant();
  227. // Maj du stock
  228. $lStock = new StockVO();
  229. $lStock->setId($lAchatActuelle->getId()->getIdStock());
  230. $lStock->setQuantite($lAchatNouvelle->getQuantite());
  231. $lStock->setType(1);
  232. $lStock->setIdCompte($pNouvelAchat->getId()->getIdCompte());
  233. $lStock->setIdDetailCommande($lAchatActuelle->getIdDetailCommande());
  234. $lStock->setIdOperation($pIdOperation);
  235. $lStockService->set($lStock);
  236. // Maj du détail Opération
  237. $lDetailOperation = new DetailOperationVO();
  238. $lDetailOperation->setId($lAchatActuelle->getId()->getIdDetailOperation());
  239. $lDetailOperation->setIdOperation($pIdOperation);
  240. $lDetailOperation->setIdCompte($pNouvelAchat->getId()->getIdCompte());
  241. $lDetailOperation->setMontant($lAchatNouvelle->getMontant());
  242. $lDetailOperation->setLibelle("Marché N°" . $pNouvelAchat->getId()->getIdCommande());
  243. $lDetailOperation->setTypePaiement(7);
  244. $lDetailOperation->setIdDetailCommande($lAchatActuelle->getIdDetailCommande());
  245. $lDetailOperationService->set($lDetailOperation);
  246. $lTestUpdate = true;
  247. }
  248. }
  249. if(!$lTestUpdate) {
  250. // Suppression du stock et du detail operation
  251. $lStockService->delete($lAchatActuelle->getId()->getIdStock());
  252. $lDetailOperationService->delete($lAchatActuelle->getId()->getIdDetailOperation());
  253. }
  254. }
  255. foreach($pNouvelAchat->getDetailAchat() as $lAchatNouvelle) {
  256. $lTestInsert = true;
  257. foreach($pAchatActuel->getDetailAchat() as $lAchatActuelle) {
  258. if($lAchatActuelle->getIdDetailCommande() == $lAchatNouvelle->getIdDetailCommande()) {
  259. $lTestInsert = false;
  260. }
  261. }
  262. if($lTestInsert) {
  263. $lTotal += $lAchatNouvelle->getMontant();
  264. // Ajout du stock
  265. $lStock = new StockVO();
  266. $lStock->setQuantite($lAchatNouvelle->getQuantite());
  267. $lStock->setType(1);
  268. $lStock->setIdCompte($pNouvelAchat->getId()->getIdCompte());
  269. $lStock->setIdDetailCommande($lAchatNouvelle->getIdDetailCommande());
  270. $lStock->setIdOperation($pIdOperation);
  271. $lStockService->set($lStock);
  272. // Ajout du détail Opération
  273. $lDetailOperation = new DetailOperationVO();
  274. $lDetailOperation->setIdOperation($pIdOperation);
  275. $lDetailOperation->setIdCompte($pNouvelAchat->getId()->getIdCompte());
  276. $lDetailOperation->setMontant($lAchatNouvelle->getMontant());
  277. $lDetailOperation->setLibelle("Marché N°" . $pNouvelAchat->getId()->getIdCommande());
  278. $lDetailOperation->setTypePaiement(7);
  279. $lDetailOperation->setIdDetailCommande($lAchatNouvelle->getIdDetailCommande());
  280. $lDetailOperationService->set($lDetailOperation);
  281. }
  282. }
  283. return $lTotal;
  284. }
  285. /**
  286. * @name updateProduitReservationAchat($pAchatActuel,$pNouvelAchat,$pIdOperation)
  287. * @param AchatVO
  288. * @param AchatVO
  289. * @param integer
  290. * @return decimal(10,2)
  291. * @desc Met à jour les produits
  292. */
  293. private function updateProduitReservationAchat($pAchatActuel,$pNouvelAchat,$pIdOperation) {
  294. $lTotal = 0;
  295. $lStockService = new StockService();
  296. $lDetailOperationService = new DetailOperationService();
  297. foreach($pAchatActuel->getDetailReservation() as $lAchatActuelle) {
  298. $lTestUpdate = false;
  299. foreach($pNouvelAchat->getDetailAchat() as $lAchatNouvelle) {
  300. if($lAchatActuelle->getIdDetailCommande() == $lAchatNouvelle->getIdDetailCommande()) {
  301. $lTotal += $lAchatNouvelle->getMontant();
  302. // Maj du stock
  303. $lStock = new StockVO();
  304. //$lStock->setId($lAchatActuelle->getId()->getIdStock());
  305. $lStock->setQuantite($lAchatNouvelle->getQuantite());
  306. $lStock->setType(1);
  307. $lStock->setIdCompte($pNouvelAchat->getId()->getIdCompte());
  308. $lStock->setIdDetailCommande($lAchatActuelle->getIdDetailCommande());
  309. $lStock->setIdOperation($pIdOperation);
  310. $lStockService->set($lStock);
  311. // Maj du détail Opération
  312. $lDetailOperation = new DetailOperationVO();
  313. //$lDetailOperation->setId($lAchatActuelle->getId()->getIdDetailOperation());
  314. $lDetailOperation->setIdOperation($pIdOperation);
  315. $lDetailOperation->setIdCompte($pNouvelAchat->getId()->getIdCompte());
  316. $lDetailOperation->setMontant($lAchatNouvelle->getMontant());
  317. $lDetailOperation->setLibelle("Marché N°" . $pNouvelAchat->getId()->getIdCommande());
  318. $lDetailOperation->setTypePaiement(7);
  319. $lDetailOperation->setIdDetailCommande($lAchatActuelle->getIdDetailCommande());
  320. $lDetailOperationService->set($lDetailOperation);
  321. $lTestUpdate = true;
  322. }
  323. }
  324. if(!$lTestUpdate) {
  325. // Suppression du stock et du detail operation
  326. $lStockService->delete($lAchatActuelle->getId()->getIdStock());
  327. $lDetailOperationService->delete($lAchatActuelle->getId()->getIdDetailOperation());
  328. }
  329. }
  330. foreach($pNouvelAchat->getDetailAchat() as $lAchatNouvelle) {
  331. $lTestInsert = true;
  332. foreach($pAchatActuel->getDetailReservation() as $lAchatActuelle) {
  333. if($lAchatActuelle->getIdDetailCommande() == $lAchatNouvelle->getIdDetailCommande()) {
  334. $lTestInsert = false;
  335. }
  336. }
  337. if($lTestInsert) {
  338. $lTotal += $lAchatNouvelle->getMontant();
  339. // Ajout du stock
  340. $lStock = new StockVO();
  341. $lStock->setQuantite($lAchatNouvelle->getQuantite());
  342. $lStock->setType(1);
  343. $lStock->setIdCompte($pNouvelAchat->getId()->getIdCompte());
  344. $lStock->setIdDetailCommande($lAchatNouvelle->getIdDetailCommande());
  345. $lStock->setIdOperation($pIdOperation);
  346. $lStockService->set($lStock);
  347. // Ajout du détail Opération
  348. $lDetailOperation = new DetailOperationVO();
  349. $lDetailOperation->setIdOperation($pIdOperation);
  350. $lDetailOperation->setIdCompte($pNouvelAchat->getId()->getIdCompte());
  351. $lDetailOperation->setMontant($lAchatNouvelle->getMontant());
  352. $lDetailOperation->setLibelle("Marché N°" . $pNouvelAchat->getId()->getIdCommande());
  353. $lDetailOperation->setTypePaiement(7);
  354. $lDetailOperation->setIdDetailCommande($lAchatNouvelle->getIdDetailCommande());
  355. $lDetailOperationService->set($lDetailOperation);
  356. }
  357. }
  358. return $lTotal;
  359. }
  360. /**
  361. * @name updateProduitAchatSolidaire($pAchatActuel,$pNouvelAchat,$pIdOperation)
  362. * @param AchatVO
  363. * @param AchatVO
  364. * @param integer
  365. * @return decimal(10,2)
  366. * @desc Met à jour les produits solidaire
  367. */
  368. private function updateProduitAchatSolidaire($pAchatActuel,$pNouvelAchat,$pIdOperation) {
  369. $lTotalSolidaire = 0;
  370. $lStockService = new StockService();
  371. $lDetailOperationService = new DetailOperationService();
  372. foreach($pAchatActuel->getDetailAchatSolidaire() as $lAchatActuelle) {
  373. $lTestUpdate = false;
  374. foreach($pNouvelAchat->getDetailAchatSolidaire() as $lAchatNouvelle) {
  375. if($lAchatActuelle->getIdDetailCommande() == $lAchatNouvelle->getIdDetailCommande()) {
  376. $lTotalSolidaire += $lAchatNouvelle->getMontant();
  377. // Maj du stock
  378. $lStock = new StockVO();
  379. $lStock->setId($lAchatActuelle->getId()->getIdStock());
  380. $lStock->setQuantite($lAchatNouvelle->getQuantite());
  381. $lStock->setType(2);
  382. $lStock->setIdCompte($pNouvelAchat->getId()->getIdCompte());
  383. $lStock->setIdDetailCommande($lAchatActuelle->getIdDetailCommande());
  384. $lStock->setIdOperation($pIdOperation);
  385. $lStockService->set($lStock);
  386. // Maj du détail Opération
  387. $lDetailOperation = new DetailOperationVO();
  388. $lDetailOperation->setId($lAchatActuelle->getId()->getIdDetailOperation());
  389. $lDetailOperation->setIdOperation($pIdOperation);
  390. $lDetailOperation->setIdCompte($pNouvelAchat->getId()->getIdCompte());
  391. $lDetailOperation->setMontant($lAchatNouvelle->getMontant());
  392. $lDetailOperation->setLibelle("Marché Solidaire N°" . $pNouvelAchat->getId()->getIdCommande());
  393. $lDetailOperation->setTypePaiement(8);
  394. $lDetailOperation->setIdDetailCommande($lAchatActuelle->getIdDetailCommande());
  395. $lDetailOperationService->set($lDetailOperation);
  396. $lTestUpdate = true;
  397. }
  398. }
  399. if(!$lTestUpdate) {
  400. // Suppression du stock et du detail operation
  401. $lStockService->delete($lAchatActuelle->getId()->getIdStock());
  402. $lDetailOperationService->delete($lAchatActuelle->getId()->getIdDetailOperation());
  403. }
  404. }
  405. foreach($pNouvelAchat->getDetailAchatSolidaire() as $lAchatNouvelle) {
  406. $lTestInsert = true;
  407. foreach($pAchatActuel->getDetailAchatSolidaire() as $lAchatActuelle) {
  408. if($lAchatActuelle->getIdDetailCommande() == $lAchatNouvelle->getIdDetailCommande()) {
  409. $lTestInsert = false;
  410. }
  411. }
  412. if($lTestInsert) {
  413. $lTotalSolidaire += $lAchatNouvelle->getMontant();
  414. // Ajout du stock
  415. $lStock = new StockVO();
  416. $lStock->setQuantite($lAchatNouvelle->getQuantite());
  417. $lStock->setType(2);
  418. $lStock->setIdCompte($pNouvelAchat->getId()->getIdCompte());
  419. $lStock->setIdDetailCommande($lAchatNouvelle->getIdDetailCommande());
  420. $lStock->setIdOperation($pIdOperation);
  421. $lStockService->set($lStock);
  422. // Ajout du détail Opération
  423. $lDetailOperation = new DetailOperationVO();
  424. $lDetailOperation->setIdOperation($pIdOperation);
  425. $lDetailOperation->setIdCompte($pNouvelAchat->getId()->getIdCompte());
  426. $lDetailOperation->setMontant($lAchatNouvelle->getMontant());
  427. $lDetailOperation->setLibelle("Marché Solidaire N°" . $pNouvelAchat->getId()->getIdCommande());
  428. $lDetailOperation->setTypePaiement(8);
  429. $lDetailOperation->setIdDetailCommande($lAchatNouvelle->getIdDetailCommande());
  430. $lDetailOperationService->set($lDetailOperation);
  431. }
  432. }
  433. return $lTotalSolidaire;
  434. }
  435. /**
  436. * @name insertProduitAchatSolidaire($pAchat)
  437. * @param AchatVO
  438. * @return decimal(10,2)
  439. * @desc Ajoute les produits solidaire
  440. */
  441. private function insertProduitAchatSolidaire($pAchat) {
  442. $lTotalSolidaire = 0;
  443. foreach($pAchat->getDetailAchatSolidaire() as $lProduit) {
  444. $lTotalSolidaire += $lProduit->getMontant();
  445. }
  446. // L'operation Solidaire
  447. $lOperationSolidaire = new OperationVO();
  448. $lOperationSolidaire->setIdCompte($pAchat->getId()->getIdCompte());
  449. $lOperationSolidaire->setMontant($lTotalSolidaire);
  450. $lOperationSolidaire->setLibelle("Marché Solidaire N°" . $pAchat->getId()->getIdCommande());
  451. $lOperationSolidaire->setTypePaiement(8);
  452. $lOperationSolidaire->setIdCommande($pAchat->getId()->getIdCommande());
  453. $lOperationService = new OperationService();
  454. if($lTotalSolidaire < 0) {
  455. $lIdOperationSolidaire = $lOperationService->set($lOperationSolidaire);
  456. // Ajout Détail Operation Solidaire
  457. $lStockService = new StockService();
  458. $lDetailOperationService = new DetailOperationService();
  459. foreach($pAchat->getDetailAchatSolidaire() as $lProduit) {
  460. // Ajout du stock
  461. $lStock = new StockVO();
  462. $lStock->setQuantite($lProduit->getQuantite());
  463. $lStock->setType(2);
  464. $lStock->setIdCompte($pAchat->getId()->getIdCompte());
  465. $lStock->setIdDetailCommande($lProduit->getIdDetailCommande());
  466. $lStock->setIdOperation($lIdOperationSolidaire);
  467. $lStockService->set($lStock);
  468. // Ajout du détail de l'operation
  469. $lDetailOperation = new DetailOperationVO();
  470. $lDetailOperation->setIdOperation($lIdOperationSolidaire);
  471. $lDetailOperation->setIdCompte($pAchat->getId()->getIdCompte());
  472. $lDetailOperation->setMontant($lProduit->getMontant());
  473. $lDetailOperation->setLibelle("Marché Solidaire N°" . $pAchat->getId()->getIdCommande());
  474. $lDetailOperation->setTypePaiement(8);
  475. $lDetailOperation->setIdDetailCommande($lProduit->getIdDetailCommande());
  476. $lDetailOperationService->set($lDetailOperation);
  477. }
  478. // Operation sur le compte Zeybu
  479. $lOperationZeybu = new OperationVO();
  480. $lOperationZeybu->setIdCompte(-1);
  481. $lOperationZeybu->setMontant($lTotalSolidaire * -1);
  482. $lOperationZeybu->setLibelle("Marché Solidaire N°" . $pAchat->getId()->getIdCommande());
  483. $lOperationZeybu->setTypePaiement(8);
  484. $lOperationZeybu->setTypePaiementChampComplementaire($lIdOperationSolidaire);
  485. $lOperationZeybu->setIdCommande($pAchat->getId()->getIdCommande());
  486. $lIdOperationZeybu = $lOperationService->set($lOperationZeybu);
  487. $lOperationSolidaire->setTypePaiementChampComplementaire($lIdOperationZeybu);
  488. $lOperationService->set($lOperationSolidaire);
  489. return $lIdOperationSolidaire;
  490. }
  491. return null;
  492. }
  493. /**
  494. * @name delete($pId)
  495. * @param IdAchatVO
  496. * @desc Supprime un achat
  497. */
  498. public function delete($pId) {
  499. $lAchatValid = new AchatValid();
  500. if(!is_null($pId) && $lAchatValid->select($pId)) {
  501. $lAchatActuel = $this->get($pId);
  502. // Suppression de l'opération
  503. $lOperationService = new OperationService();
  504. $lIdOperationZeybu = $lOperationService->get($pId->getIdAchat())->getTypePaiementChampComplementaire();
  505. $lOperationService->delete($pId->getIdAchat());
  506. $lOperationService->delete($lIdOperationZeybu);
  507. $lStockService = new StockService();
  508. $lDetailOperationService = new DetailOperationService();
  509. foreach($lAchatActuel->getDetailAchat() as $lDetail) {
  510. // Suppression du stock et du detail operation
  511. $lStockService->delete($lDetail->getId()->getIdStock());
  512. $lDetailOperationService->delete($lDetail->getId()->getIdDetailOperation());
  513. }
  514. foreach($lAchatActuel->getDetailAchatSolidaire() as $lDetail) {
  515. // Suppression du stock et du detail operation
  516. $lStockService->delete($lDetail->getId()->getIdStock());
  517. $lDetailOperationService->delete($lDetail->getId()->getIdDetailOperation());
  518. }
  519. }
  520. return false;
  521. }
  522. /**
  523. * @name get($pId)
  524. * @param integer
  525. * @return AchatVO
  526. * @desc Retourne une liste d'achat
  527. */
  528. public function get($pId = null) {
  529. $lAchatValid = new AchatValid();
  530. if(!is_null($pId) && $lAchatValid->select($pId)) {
  531. return $this->select($pId);
  532. }
  533. return false;
  534. }
  535. /**
  536. * @name getAll($pId)
  537. * @param integer
  538. * @return array(AchatVO)
  539. * @desc Retourne une liste d'achat
  540. */
  541. public function getAll($pId = null) {
  542. $lAchatValid = new AchatValid();
  543. if(!is_null($pId) && $lAchatValid->selectAll($pId)) {
  544. return $this->selectAll($pId);
  545. }
  546. return false;
  547. }
  548. /**
  549. * @name selectOperationAchat($pId)
  550. * @param IdReservation
  551. * @return array(OperationVO)
  552. * @desc Retourne une liste d'operation
  553. */
  554. private function selectOperationAchat($pId) {
  555. // ORDER BY date -> récupère la dernière operation en lien avec la commande
  556. return OperationManager::recherche(
  557. array(OperationManager::CHAMP_OPERATION_TYPE_PAIEMENT,OperationManager::CHAMP_OPERATION_ID_COMPTE,OperationManager::CHAMP_OPERATION_ID_COMMANDE),
  558. array('in','=','='),
  559. array(array(7,8), $pId->getIdCompte(),$pId->getIdCommande()),
  560. array(OperationManager::CHAMP_OPERATION_TYPE_PAIEMENT,OperationManager::CHAMP_OPERATION_DATE),
  561. array('ASC','DESC'));
  562. }
  563. /**
  564. * @name selectAll($pId)
  565. * @param IdReservation
  566. * @return array(AchatVO)
  567. * @desc Retourne les achats du compte pour un marché
  568. */
  569. private function selectAll($pId) {
  570. $lOperations = $this->selectOperationAchat($pId);
  571. $lAchats = array();
  572. if(!is_null($lOperations[0]->getId())) {
  573. foreach($lOperations as $lOperation) {
  574. $pId->setIdAchat($lOperation->getId());
  575. array_push($lAchats,$this->select($pId));
  576. }
  577. }
  578. return $lAchats;
  579. }
  580. /**
  581. * @name select($pId)
  582. * @param IdAchatVO
  583. * @return AchatVO
  584. * @desc Retourne une Reservation
  585. */
  586. private function select($pId) {
  587. $lOperation = OperationManager::select($pId->getIdAchat());
  588. $lAchat = new AchatVO();
  589. $lAchat->getId()->setIdCompte($pId->getIdCompte());
  590. $lAchat->getId()->setIdCommande($pId->getIdCommande());
  591. $lAchat->getId()->setIdAchat($pId->getIdAchat());
  592. // Recherche du détail de la reservation
  593. switch($lOperation->getTypePaiement()) {
  594. case 7: // Un achat
  595. $lDetailsAchat = AchatDetailViewManager::select($lOperation->getId());
  596. foreach($lDetailsAchat as $lDetail) {
  597. if(!is_null($lDetail->getStoId())) {
  598. $lDetailAchat = new DetailReservationVO();
  599. $lDetailAchat->getId()->setIdStock($lDetail->getStoId());
  600. $lDetailAchat->getId()->setIdDetailOperation($lDetail->getDopeId());
  601. $lDetailAchat->setIdDetailCommande($lDetail->getStoIdDetailCommande());
  602. $lDetailAchat->setMontant($lDetail->getDopeMontant());
  603. $lDetailAchat->setQuantite($lDetail->getStoQuantite());
  604. $lDetailAchat->setIdProduit($lDetail->getDcomIdProduit());
  605. $lAchat->addDetailAchat($lDetailAchat);
  606. }
  607. }
  608. $lAchat->setTotal($lOperation->getMontant());
  609. break;
  610. case 8: // Achat Solidaire
  611. $lDetailsAchat = AchatDetailSolidaireViewManager::select($lOperation->getId());
  612. foreach($lDetailsAchat as $lDetail) {
  613. if(!is_null($lDetail->getStoId())) {
  614. $lDetailAchat = new DetailReservationVO();
  615. $lDetailAchat->getId()->setIdStock($lDetail->getStoId());
  616. $lDetailAchat->getId()->setIdDetailOperation($lDetail->getDopeId());
  617. $lDetailAchat->setIdDetailCommande($lDetail->getStoIdDetailCommande());
  618. $lDetailAchat->setMontant($lDetail->getDopeMontant());
  619. $lDetailAchat->setQuantite($lDetail->getStoQuantite());
  620. $lDetailAchat->setIdProduit($lDetail->getDcomIdProduit());
  621. $lAchat->addDetailAchatSolidaire($lDetailAchat);
  622. }
  623. }
  624. $lAchat->setTotalSolidaire($lOperation->getMontant());
  625. break;
  626. }
  627. return $lAchat;
  628. }
  629. /**
  630. * @name selectMarcheAll($pIdMarche)
  631. * @param IdMarche
  632. * @return
  633. * @desc Retourne une Reservation
  634. */
  635. /*public function selectMarcheAll($pIdMarche) {
  636. // Initialisation du Logger
  637. $lLogger = &Log::singleton('file', CHEMIN_FICHIER_LOGS);
  638. $lLogger->setMask(Log::MAX(LOG_LEVEL));
  639. $lRequete =
  640. "SELECT
  641. ADHERENT." . AdherentManager::CHAMP_ADHERENT_ID .
  642. ", ADHERENT." . AdherentManager::CHAMP_ADHERENT_NUMERO .
  643. ", ADHERENT." . AdherentManager::CHAMP_ADHERENT_ID_COMPTE .
  644. ", ADHERENT." . CompteManager::CHAMP_COMPTE_LABEL .
  645. ", ADHERENT." . AdherentManager::CHAMP_ADHERENT_NOM .
  646. ", ADHERENT." . AdherentManager::CHAMP_ADHERENT_PRENOM .
  647. ", OPERATION." . OperationManager::CHAMP_OPERATION_MONTANT . "_reservation" .
  648. ", OPERATION." . OperationManager::CHAMP_OPERATION_MONTANT . "_achat
  649. FROM " . AdherentViewManager::VUE_ADHERENT . " AS ADHERENT
  650. LEFT JOIN (
  651. SELECT "
  652. . AdherentManager::CHAMP_ADHERENT_ID .
  653. ", RESERVATION." . OperationManager::CHAMP_OPERATION_MONTANT . " AS " . OperationManager::CHAMP_OPERATION_MONTANT . "_reservation" .
  654. ", ACHAT." . OperationManager::CHAMP_OPERATION_MONTANT . " AS " . OperationManager::CHAMP_OPERATION_MONTANT . "_achat
  655. FROM " . AdherentViewManager::VUE_ADHERENT . "
  656. LEFT JOIN (
  657. SELECT "
  658. . OperationManager::CHAMP_OPERATION_ID_COMMANDE .
  659. "," . OperationManager::CHAMP_OPERATION_MONTANT .
  660. "," . OperationManager::CHAMP_OPERATION_ID_COMPTE . "
  661. FROM " . OperationManager::TABLE_OPERATION . "
  662. WHERE " . OperationManager::CHAMP_OPERATION_TYPE_PAIEMENT . " = 0) AS RESERVATION
  663. ON RESERVATION." . OperationManager::CHAMP_OPERATION_ID_COMPTE . " = " . AdherentViewManager::VUE_ADHERENT . "." . AdherentManager::CHAMP_ADHERENT_ID_COMPTE . "
  664. LEFT JOIN (
  665. SELECT "
  666. . OperationManager::CHAMP_OPERATION_ID_COMMANDE .
  667. ", sum(" . OperationManager::CHAMP_OPERATION_MONTANT . ") AS " . OperationManager::CHAMP_OPERATION_MONTANT .
  668. "," . OperationManager::CHAMP_OPERATION_ID_COMPTE . "
  669. FROM " . OperationManager::TABLE_OPERATION . "
  670. WHERE " . OperationManager::CHAMP_OPERATION_TYPE_PAIEMENT . " in (7,8)
  671. GROUP BY " . OperationManager::CHAMP_OPERATION_ID_COMMANDE . "," . OperationManager::CHAMP_OPERATION_ID_COMPTE . ") AS ACHAT
  672. ON ACHAT." . OperationManager::CHAMP_OPERATION_ID_COMPTE . " = " . AdherentViewManager::VUE_ADHERENT . "." . AdherentManager::CHAMP_ADHERENT_ID_COMPTE . "
  673. WHERE RESERVATION." . OperationManager::CHAMP_OPERATION_ID_COMMANDE . " = " . StringUtils::securiser($pIdMarche) . "
  674. OR ACHAT." . OperationManager::CHAMP_OPERATION_ID_COMMANDE . " = " . StringUtils::securiser($pIdMarche) . "
  675. GROUP BY " . AdherentViewManager::VUE_ADHERENT . "." . AdherentManager::CHAMP_ADHERENT_ID . ") AS OPERATION
  676. ON ADHERENT." . AdherentManager::CHAMP_ADHERENT_ID . " = OPERATION." . AdherentManager::CHAMP_ADHERENT_ID;
  677. $lLogger->log("Execution de la requete : " . $lRequete,PEAR_LOG_DEBUG); // Maj des logs
  678. $lSql = Dbutils::executerRequete($lRequete);
  679. $lListeAchatEtReservation = array();
  680. if( mysql_num_rows($lSql) > 0 ) {
  681. while ($lLigne = mysql_fetch_assoc($lSql)) {
  682. array_push($lListeAchatEtReservation,
  683. $this->remplirAchatEtReservation(
  684. $lLigne[AdherentManager::CHAMP_ADHERENT_ID],
  685. $lLigne[AdherentManager::CHAMP_ADHERENT_NUMERO],
  686. $lLigne[AdherentManager::CHAMP_ADHERENT_ID_COMPTE],
  687. $lLigne[CompteManager::CHAMP_COMPTE_LABEL],
  688. $lLigne[AdherentManager::CHAMP_ADHERENT_NOM],
  689. $lLigne[AdherentManager::CHAMP_ADHERENT_PRENOM],
  690. $lLigne[OperationManager::CHAMP_OPERATION_MONTANT . "_reservation"],
  691. $lLigne[OperationManager::CHAMP_OPERATION_MONTANT . "_achat"]));
  692. }
  693. } else {
  694. $lListeAchatEtReservation[0] = new ListeAchatReservationVO();
  695. }
  696. return $lListeAchatEtReservation;
  697. }
  698. */
  699. /**
  700. * @name remplirAchatEtReservation($pAdhId, $pAdhNumero, $pAdhIdCompte, $pCptLabel, $pAdhNom, $pAdhPrenom, $pOpeMontantReservation, $pOpeMontantAchat)
  701. * @param int(11)
  702. * @param int(11)
  703. * @param int(11)
  704. * @param varchar(30)
  705. * @param varchar(50)
  706. * @param varchar(50)
  707. * @param decimal(10,2)
  708. * @param decimal(10,2)
  709. * @return ListeAchatReservationVO
  710. * @desc Retourne une ListeAchatReservationVO remplie
  711. */
  712. /*private function remplirAchatEtReservation($pAdhId, $pAdhNumero, $pAdhIdCompte, $pCptLabel, $pAdhNom, $pAdhPrenom, $pOpeMontantReservation, $pOpeMontantAchat) {
  713. $lListeAchatReservation = new ListeAchatReservationVO();
  714. $lListeAchatReservation->setAdhId($pAdhId);
  715. $lListeAchatReservation->setAdhNumero($pAdhNumero);
  716. $lListeAchatReservation->setAdhIdCompte($pAdhIdCompte);
  717. $lListeAchatReservation->setCptLabel($pCptLabel);
  718. $lListeAchatReservation->setAdhNom($pAdhNom);
  719. $lListeAchatReservation->setAdhPrenom($pAdhPrenom);
  720. $lListeAchatReservation->setOpeMontantReservation($pOpeMontantReservation);
  721. $lListeAchatReservation->setOpeMontantAchat($pOpeMontantAchat);
  722. return $lListeAchatReservation;
  723. }*/
  724. }
  725. ?>