[ 'class' => VerbFilter::class, 'actions' => [ 'delete' => ['post'], ], ], 'access' => [ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => ['?'], ], [ 'actions' => ['pay-orders'], 'allow' => true, 'roles' => ['@'] ], ], ], ]; } public function actionPayOrders($date) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; if (strlen($date)) { $this->actionProcessOrders('64ac0bdab7e9f5e48c4d991ec5201d57', $date); } return [ 'return' => 'success', 'alert' => [ 'type' => 'success', 'message' => 'Commandes payées.' ] ]; } public function actionForceProcessOrders($key = '') { $this->actionProcessOrders($key, date('Y-m-d')); } /** * Routine quotidienne concernant les commandes : paiement et envoi d'un * récap aux producteurs. * * @param string $key * @param string $forceDate */ public function actionProcessOrders(string $key = '', string $forceDate = '') { $producerModule = $this->getProducerModule(); $distributionModule = $this-> getDistributionModule(); $orderModule = $this->getOrderModule(); $paymentManager = $this->getPaymentModule(); $userModule = $this->getUserModule(); if ($key == '64ac0bdab7e9f5e48c4d991ec5201d57') { ini_set('memory_limit', '-1'); set_time_limit(0); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); $hour = 20; if (strlen($forceDate)) { $date = $forceDate; } else { $hour = date('H'); if ($hour == '00') { $date = date('Y-m-d'); $hour = 24; } else { $date = date('Y-m-d', time() + 24 * 60 * 60); } } $arrayProducers = $producerModule->findProducers(); foreach ($arrayProducers as $producer) { $this->getLogic()->setProducerContext($producer); $distribution = $distributionModule ->findOneDistribution($date, true); if ($distribution) { // @TODO : attention, il faut prendre en compte les délais de commande spécifiques (lundi, mardi, mercredi etc.) if ($hour == $producer->order_deadline || strlen($forceDate)) { /* * Paiement des commandes (paiement automatique) */ $arrayOrders = Order::searchAll([ 'distribution.date' => $date, 'distribution.id_producer' => $producer->id ], [ 'conditions' => 'date_delete IS NULL' ]); $configCredit = $producerModule->getConfig('credit'); if ($arrayOrders && is_array($arrayOrders)) { foreach ($arrayOrders as $order) { $orderModule->initOrder($order); if ($order->auto_payment && $configCredit) { if ($orderModule->isCreditAutoPayment($order) && $orderModule->getOrderAmount($order, Order::AMOUNT_REMAINING) > 0) { $paymentManager->payOrder($order, MeanPayment::CREDIT, $userModule->findOneUserById(User::ID_USER_SYSTEM), false); } } } } /* * Envoi des commandes par email au producteur */ if (!strlen($forceDate) && $producerModule->getConfig('option_notify_producer_order_summary')) { $arrayOrders = Order::searchAll([ 'distribution.date' => $date, 'distribution.id_producer' => $producer->id ], [ 'conditions' => 'date_delete IS NULL' ]); $mail = Yii::$app->mailer->compose( [ 'html' => '@common/mail/cronOrdersSummary-html', 'text' => '@common/mail/cronOrdersSummary-text', ], [ 'date' => $date, 'orders' => $arrayOrders ] ) ->setFrom([Yii::$app->parameterBag->get('adminEmail') => 'Souke']); if (is_array($arrayOrders) && count($arrayOrders)) { $subject = '[Souke] Commandes du ' . date('d/m', strtotime($date)); // @TODO : envoyer uniquement les exports qui sont activés dans les paramètres du producteur $distributionModule->getExportManager()->generate(ExportManager::ORDERS_PDF, $distribution, true); $mail->attach( Yii::getAlias('@app/web/pdf/Commandes-' . $date . '-' . $producer->id . '.pdf') ); } else { $subject = '[Souke] Aucune commande'; } $mail->setSubject($subject); // envoi des emails à tous les comptes users (statut producer) associés à ce producteur $userArray = User::searchAll([ 'id_producer' => $producer->id, 'status' => User::STATUS_PRODUCER ]); foreach ($userArray as $user) { if($user->email && strlen($user->email) > 0) { $mail->setTo($user->email)->send(); } } } } } } } } }