You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 line
5.2KB

  1. <?php
  2. namespace domain\Distribution\Distribution;
  3. use domain\Distribution\Distribution\Export\DistributionReport2CsvGenerator;
  4. use domain\Distribution\Distribution\Export\DistributionReportCsvGenerator;
  5. use domain\Distribution\Distribution\Export\DistributionReportGridPdfGenerator;
  6. use domain\Distribution\Distribution\Export\DistributionReportPdfGenerator;
  7. use domain\Distribution\Distribution\Export\DistributionReportTotalProductCsvGenerator;
  8. use domain\Distribution\Distribution\Export\DistributionShoppingCartLabelsPdfGenerator;
  9. use domain\Producer\Producer\Producer;
  10. use domain\_\AbstractManager;
  11. use yii\base\ErrorException;
  12. use yii\widgets\ActiveForm;
  13. class ExportManager extends AbstractManager
  14. {
  15. const ORDERS_PDF = 'orders_pdf';
  16. const ORDERS_GRID_PDF = 'orders_grid_pdf';
  17. const ORDERS1_CSV = 'orders1_csv';
  18. const ORDERS2_CSV = 'orders2_csv';
  19. const TOTAL_PRODUCTS_CSV = 'total_products_csv';
  20. const SHOPPING_CART_LABELS_PDF = 'shopping_cart_labels_pdf';
  21. public function getAll(): array
  22. {
  23. return [
  24. self::ORDERS_PDF => [
  25. 'Commandes (PDF)',
  26. DistributionReportPdfGenerator::class,
  27. ],
  28. self::ORDERS_GRID_PDF => [
  29. 'Commandes grille (PDF)',
  30. DistributionReportGridPdfGenerator::class,
  31. ],
  32. self::ORDERS1_CSV => [
  33. 'Commandes #1 (CSV)',
  34. DistributionReportCsvGenerator::class,
  35. ],
  36. self::ORDERS2_CSV => [
  37. 'Commandes #2 (CSV)',
  38. DistributionReport2CsvGenerator::class,
  39. ],
  40. self::TOTAL_PRODUCTS_CSV => [
  41. 'Totaux produits (CSV)',
  42. DistributionReportTotalProductCsvGenerator::class,
  43. ],
  44. self::SHOPPING_CART_LABELS_PDF => [
  45. 'Étiquettes (PDF)',
  46. DistributionShoppingCartLabelsPdfGenerator::class,
  47. ]
  48. ];
  49. }
  50. public function getAllEnabled(): array
  51. {
  52. $exportsArray = $this->getAll();
  53. $exportsEnabledArray = [];
  54. foreach($exportsArray as $name => $export) {
  55. if($this->isEnabled($name)) {
  56. $exportsEnabledArray[$name] = $export;
  57. }
  58. }
  59. return $exportsEnabledArray;
  60. }
  61. public function getExport(string $exportName): array
  62. {
  63. $exportsArray = $this->getAll();
  64. return $exportsArray[$exportName];
  65. }
  66. public function getExportLabel(string $exportName): string
  67. {
  68. $export = $this->getExport($exportName);
  69. return $export[0];
  70. }
  71. public function generate(string $exportName, Distribution $distribution, bool $save = false)
  72. {
  73. return $this->getGenerator($exportName)->generate($distribution, $save);
  74. }
  75. public function getGenerator(string $exportName)
  76. {
  77. if($this->exist($exportName)) {
  78. $export = $this->getExport($exportName);
  79. $classGenerator = $export[1];
  80. return $classGenerator::getInstance();
  81. }
  82. else {
  83. throw new ErrorException("Cet export n'existe pas.");
  84. }
  85. }
  86. public function exist(string $exportName): bool
  87. {
  88. return array_key_exists($exportName, $this->getAll());
  89. }
  90. public function isEnabled(string $exportName): bool
  91. {
  92. return $this->exist($exportName) && $this->producerHasOptionExportEnabled($exportName);
  93. }
  94. public function getAjaxArray(Distribution $distribution): array
  95. {
  96. $datas = [];
  97. $exportsArray = $this->getAll();
  98. foreach($exportsArray as $exportName => $export) {
  99. if($this->isEnabled($exportName)) {
  100. $datas[] = $this->buildAjaxArray($distribution, $exportName, $export);
  101. }
  102. }
  103. return $datas;
  104. }
  105. public function buildAjaxArray(Distribution $distribution, string $exportName, array $export): array
  106. {
  107. return [
  108. 'name' => $exportName,
  109. 'label' => $export[0],
  110. 'url' => \Yii::$app->urlManagerBackend->createUrl([
  111. 'distribution/export',
  112. 'date' => $distribution->date,
  113. 'name' => $exportName
  114. ])
  115. ];
  116. }
  117. private function producerHasOptionExportEnabled(string $exportName): ?bool
  118. {
  119. $producer = $this->getProducerContext();
  120. $fieldOptionExport = $this->getProducerFieldOptionExport($exportName);
  121. return $producer->$fieldOptionExport;
  122. }
  123. private function getProducerFieldOptionExport(string $exportName): string
  124. {
  125. return 'option_distribution_export_'.$exportName;
  126. }
  127. public function getProducerFormCheckboxes(ActiveForm $form, Producer $producer): string
  128. {
  129. $html = '';
  130. $exportsArray = $this->getAll();
  131. foreach($exportsArray as $exportName => $export) {
  132. $html .= $this->getProducerFormCheckbox($form, $producer, $exportName);
  133. }
  134. return $html;
  135. }
  136. public function getProducerFormCheckbox(ActiveForm $form, Producer $producer, string $exportName): string
  137. {
  138. return $form->field($producer, $this->getProducerFieldOptionExport($exportName))
  139. ->checkbox(['label' => $this->getExportLabel($exportName)]);
  140. }
  141. }