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.

ClosingOrdersController.php 5.3KB

преди 8 месеца
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace console\commands;
  3. use common\helpers\MeanPayment;
  4. use domain\Distribution\Distribution\Distribution;
  5. use domain\Distribution\Distribution\DistributionModule;
  6. use domain\Distribution\Distribution\ExportManager;
  7. use domain\Order\Order\Order;
  8. use domain\Order\Order\OrderModule;
  9. use domain\Payment\PaymentModule;
  10. use domain\Producer\Producer\Producer;
  11. use domain\Producer\Producer\ProducerModule;
  12. use domain\User\User\User;
  13. use domain\User\User\UserModule;
  14. use yii\console\Controller;
  15. class ClosingOrdersController extends Controller
  16. {
  17. public $date;
  18. public $hour;
  19. public $notification;
  20. public function options($actionID)
  21. {
  22. return [
  23. 'date',
  24. 'hour',
  25. 'notification'
  26. ];
  27. }
  28. public function optionAliases()
  29. {
  30. return [
  31. 'date' => 'date',
  32. 'hour' => 'hour',
  33. 'notification' => 'notification'
  34. ];
  35. }
  36. public function actionIndex()
  37. {
  38. // exemple tâche planifiée : ./yii closing-orders -notification=1
  39. $producerModule = ProducerModule::getInstance();
  40. $distributionModule = DistributionModule::getInstance();
  41. $orderModule = OrderModule::getInstance();
  42. $userModule = UserModule::getInstance();
  43. [$date, $hour] = $this->getDateHour();
  44. $arrayProducers = $producerModule->getRepository()->findProducers();
  45. $userSystem = $userModule->getRepository()->findOneUserById(User::ID_USER_SYSTEM)
  46. foreach($arrayProducers as $producer) {
  47. \Yii::$app->logic->setProducerContext($producer);
  48. $distribution = $distributionModule->getRepository()->findOneDistribution($date, true);
  49. if($distribution) {
  50. // @TODO : gérer les horaires spécifiques
  51. if($hour == $producer->order_deadline || !$hour) {
  52. $ordersArray = $orderModule->getRepository()->findOrdersByDistribution($distribution);
  53. foreach($ordersArray as $order) {
  54. $orderModule->getBuilder()->initOrder($order);
  55. $this->payOrder($order, $userSystem);
  56. $this->notifyProducer($distribution);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. public function notifyProducer(Distribution $distribution)
  63. {
  64. $producerModule = ProducerModule::getInstance();
  65. if ($this->notification
  66. && $producerModule->getSolver()->getConfig('option_notify_producer_order_summary')) {
  67. $mail = $this->buildMailNotificationProducer($distribution);
  68. $this->sendMailNotificationProducer($mail, $distribution->producer);
  69. }
  70. }
  71. public function buildMailNotificationProducer(Distribution $distribution)
  72. {
  73. $orderModule = OrderModule::getInstance();
  74. $distributionModule = DistributionModule::getInstance();
  75. $ordersArray = $orderModule->getRepository()->findOrdersByDistribution($distribution);
  76. $mail = \Yii::$app->mailer->compose(
  77. [
  78. 'html' => '@common/mail/cronOrdersSummary-html',
  79. 'text' => '@common/mail/cronOrdersSummary-text',
  80. ], [
  81. 'date' => $distribution->date,
  82. 'orders' => $ordersArray
  83. ]
  84. )->setFrom([\Yii::$app->parameterBag->get('adminEmail') => 'Souke']);
  85. if (count($ordersArray)) {
  86. $subject = '[Souke] Commandes du ' . date('d/m', strtotime($distribution->date));
  87. // @TODO : envoyer uniquement les exports qui sont activés dans les paramètres du producteur
  88. $distributionModule->getExportManager()->generate(ExportManager::ORDERS_PDF, $distribution, true);
  89. $mail->attach(\Yii::getAlias('@app/web/pdf/Commandes-' . $distribution->date . '-' . $distribution->producer->id . '.pdf'));
  90. } else {
  91. $subject = '[Souke] Aucune commande';
  92. }
  93. $mail->setSubject($subject);
  94. return $mail;
  95. }
  96. public function sendMailNotificationProducer($mail, Producer $producer)
  97. {
  98. $userModule = UserModule::getInstance();
  99. $usersArray = $userModule->getRepository()->findUsersByProducer($producer);
  100. foreach ($usersArray as $user) {
  101. if($user->email && strlen($user->email)) {
  102. $mail->setTo($user->email)->send();
  103. }
  104. }
  105. }
  106. public function payOrder(Order $order, User $userSystem)
  107. {
  108. $paymentModule = PaymentModule::getInstance();
  109. $orderModule = OrderModule::getInstance();
  110. if($orderModule->getRepository()->isCreditAutoPayment($order)
  111. && $orderModule->getSolver()->getOrderAmount($order, Order::AMOUNT_REMAINING) > 0) {
  112. $paymentModule->getManager()->payOrder(
  113. $order,
  114. MeanPayment::CREDIT,
  115. $userSystem,
  116. false
  117. );
  118. }
  119. }
  120. public function getDateHour(): array
  121. {
  122. $hour = $this->hour;
  123. $date = $this->date;
  124. if(!$date) {
  125. $hour = date('H');
  126. if ($hour == '00') {
  127. $date = date('Y-m-d');
  128. $hour = 24;
  129. } else {
  130. $date = date('Y-m-d', time() + 24 * 60 * 60);
  131. }
  132. }
  133. return [$date, $hour];
  134. }
  135. }
  136. ?>