/braldahim/application/controllers/AdministrationstocktabacController.php

https://github.com/alesmuth/braldahim · PHP · 182 lines · 137 code · 40 blank · 5 comment · 10 complexity · ad32d299cccc37ca277bc57760880996 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of Braldahim, under Gnu Public Licence v3.
  4. * See licence.txt or http://www.gnu.org/licenses/gpl-3.0.html
  5. * Copyright: see http://www.braldahim.com/sources
  6. */
  7. class AdministrationstocktabacController extends Zend_Controller_Action
  8. {
  9. function init()
  10. {
  11. if (!Zend_Auth::getInstance()->hasIdentity()) {
  12. $this->_redirect('/');
  13. }
  14. Zend_Loader::loadClass("Bral_Util_Securite");
  15. Bral_Util_Securite::controlAdmin();
  16. $this->initView();
  17. $this->view->user = Zend_Auth::getInstance()->getIdentity();
  18. $this->view->config = Zend_Registry::get('config');
  19. }
  20. function indexAction()
  21. {
  22. $this->render();
  23. }
  24. function tabacAction()
  25. {
  26. $creation = false;
  27. $demain = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") + 1, date("Y")));
  28. $aujourdhui = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
  29. $this->formulairePrepare();
  30. if ($this->_request->isPost() && $this->_request->getPost('dateStock')) {
  31. $this->stocksPrepare($this->_request->getPost('dateStock'));
  32. $this->view->dateStock = $this->_request->getPost('dateStock');
  33. } else {
  34. $this->view->dateStock = $aujourdhui;
  35. $this->stocksPrepare($aujourdhui);
  36. }
  37. if ($this->_request->isPost() && $this->_request->getPost('dateStock') == null) {
  38. Zend_Loader::loadClass('Zend_Filter');
  39. Zend_Loader::loadClass('Zend_Filter_StripTags');
  40. Zend_Loader::loadClass('Zend_Filter_StringTrim');
  41. $creation = true;
  42. $filter = new Zend_Filter();
  43. $filter->addFilter(new Zend_Filter_StringTrim())->addFilter(new Zend_Filter_StripTags());
  44. foreach ($this->view->idsForm as $f) {
  45. $prixVente = (int)$filter->filter($this->_request->getPost($f . "_vente"));
  46. $prixReprise = (int)$filter->filter($this->_request->getPost($f . "_reprise"));
  47. $nbInitial = (int)$filter->filter($this->_request->getPost($f . "_nbinitial"));
  48. if ($prixVente < 0 || $prixReprise < 0 && $nbInitial < 0) {
  49. throw new Zend_Exception("::tabacAction : prixVente(" . $prixVente . ") ou prixReprise(" . $prixReprise . ") ou nbInitial(" . $nbInitial . ") invalide");
  50. }
  51. $this->ajouteNouveauStock($f, $demain, $prixVente, $prixReprise, $nbInitial);
  52. }
  53. $this->formulairePrepare();
  54. $this->view->dateStock = $demain;
  55. $this->stocksPrepare($demain);
  56. }
  57. $this->view->dateCreationStock = $demain;
  58. $this->view->creation = $creation;
  59. $this->render();
  60. }
  61. private function ajouteNouveauStock($idForm, $mdate, $prixVente, $prixReprise, $nbInitial)
  62. {
  63. Zend_Loader::loadClass('StockTabac');
  64. $stockTabacTable = new StockTabac();
  65. preg_match('/(.*)_(.*)/', $idForm, $matches);
  66. $idRegion = $matches[1];
  67. $idTypeTabac = $matches[2];
  68. $data = array(
  69. "date_stock_tabac" => $mdate,
  70. "id_fk_type_stock_tabac" => $idTypeTabac,
  71. "nb_feuille_initial_stock_tabac" => $nbInitial,
  72. "nb_feuille_restant_stock_tabac" => $nbInitial,
  73. "prix_unitaire_vente_stock_tabac" => $prixVente,
  74. "prix_unitaire_reprise_stock_tabac" => $prixReprise,
  75. "id_fk_region_stock_tabac" => $idRegion,
  76. );
  77. $stockTabacTable->insert($data);
  78. }
  79. private function stocksPrepare($mDate)
  80. {
  81. Zend_Loader::loadClass('StockTabac');
  82. $stockTabacTable = new StockTabac();
  83. $stockTabacRowset = $stockTabacTable->findByDate($mDate);
  84. $listeDatesRowset = $stockTabacTable->findDistinctDate();
  85. foreach ($listeDatesRowset as $r) {
  86. $listeDates[] = $r["date_stock_tabac"];
  87. }
  88. $stocks = null;
  89. foreach ($stockTabacRowset as $r) {
  90. $stock = array(
  91. 'id_stock_tabac' => $r["id_stock_tabac"],
  92. 'date_stock_tabac' => $r["date_stock_tabac"],
  93. 'id_fk_type_stock_tabac' => $r["id_fk_type_stock_tabac"],
  94. 'nb_feuille_initial_stock_tabac' => $r["nb_feuille_initial_stock_tabac"],
  95. 'nb_feuille_restant_stock_tabac' => $r["nb_feuille_restant_stock_tabac"],
  96. 'prix_unitaire_vente_stock_tabac' => $r["prix_unitaire_vente_stock_tabac"],
  97. 'prix_unitaire_reprise_stock_tabac' => $r["prix_unitaire_reprise_stock_tabac"],
  98. 'id_fk_region_stock_tabac' => $r["id_fk_region_stock_tabac"],
  99. 'nom_type_tabac' => $r["nom_type_tabac"],
  100. 'nom_region' => $r["nom_region"],
  101. );
  102. $this->view->regions[$r["id_fk_region_stock_tabac"]]["type_tabac"][$r["id_fk_type_stock_tabac"]]["nb_feuille"] = $r["nb_feuille_initial_stock_tabac"];
  103. $this->view->regions[$r["id_fk_region_stock_tabac"]]["type_tabac"][$r["id_fk_type_stock_tabac"]]["prix_unitaire_vente"] = $r["prix_unitaire_vente_stock_tabac"];
  104. $this->view->regions[$r["id_fk_region_stock_tabac"]]["type_tabac"][$r["id_fk_type_stock_tabac"]]["prix_unitaire_reprise"] = $r["prix_unitaire_reprise_stock_tabac"];
  105. $stocks[] = $stock;
  106. }
  107. $this->view->stocks = $stocks;
  108. $this->view->listeDates = $listeDates;
  109. }
  110. private function formulairePrepare()
  111. {
  112. Zend_Loader::loadClass('Region');
  113. Zend_Loader::loadClass('TypeTabac');
  114. $typeTabacTable = new TypeTabac();
  115. $regionTable = new Region();
  116. $typeTabacRowset = $typeTabacTable->fetchall();
  117. $typeTabacRowset = $typeTabacRowset->toArray();
  118. $regionRowset = $regionTable->fetchall();
  119. $idsForm = null;
  120. foreach ($regionRowset as $r) {
  121. $typeTabacs = null;
  122. foreach ($typeTabacRowset as $t) {
  123. $idForm = $r->id_region . "_" . $t["id_type_tabac"];
  124. $idsForm[] = $idForm;
  125. $typeTabacs[$t["id_type_tabac"]] = array("id_type_tabac" => $t["id_type_tabac"],
  126. "nom" => $t["nom_type_tabac"],
  127. "id_form" => $idForm,
  128. "nb_feuille" => 10000,
  129. "prix_unitaire_vente" => 30,
  130. "prix_unitaire_reprise" => 10000,
  131. );
  132. }
  133. $regions[$r->id_region] = array(
  134. "id_region" => $r->id_region,
  135. "nom_region" => $r->nom_region,
  136. "type_tabac" => $typeTabacs,
  137. );
  138. }
  139. $this->view->idsForm = $idsForm;
  140. $this->view->regions = $regions;
  141. }
  142. }