Pārlūkot izejas kodu

Merge branch 'dev'

prodstable
Guillaume pirms 2 gadiem
vecāks
revīzija
d2c7390abf
1 mainītis faili ar 194 papildinājumiem un 199 dzēšanām
  1. +194
    -199
      backend/controllers/CronController.php

+ 194
- 199
backend/controllers/CronController.php Parādīt failu

@@ -57,229 +57,224 @@ use common\models\CreditHistory;
*/
class CronController extends BackendController
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['pay-orders'],
'allow' => true,
'roles' => ['@']
],
],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['pay-orders'],
'allow' => true,
'roles' => ['@']
],
],
];
}

/**
* Initialise le compte de démonstration.
*
* @param string $key
*/
public function actionInitDemo($key = '')
{
if ($key == '45432df6e842ac71aa0b5bb6b9f25d44') {
$producer = Producer::getDemoAccount();

if ($producer) {
// initialisation de la distribution à J+7
$dateTime = strtotime("+7 day");
$dayStr = strtolower(date('l', $dateTime));
$fieldDeliveryDay = 'delivery_' . $dayStr;
$pointsSaleArray = PointSale::searchAll(['point_sale.id_producer' => $producer->id]);
$activeDistribution = false;
foreach ($pointsSaleArray as $pointSale) {
if ($pointSale->$fieldDeliveryDay) {
$activeDistribution = true;
}
}

if ($activeDistribution) {
$distribution = Distribution::initDistribution(date('Y-m-d', $dateTime), $producer->id);
$distribution->active(true);
}
}
],
];
}

/**
* Initialise le compte de démonstration.
*
* @param string $key
*/
public function actionInitDemo($key = '')
{
if ($key == '45432df6e842ac71aa0b5bb6b9f25d44') {
$producer = Producer::getDemoAccount();

if ($producer) {
// initialisation de la distribution à J+7
$dateTime = strtotime("+7 day");
$dayStr = strtolower(date('l', $dateTime));
$fieldDeliveryDay = 'delivery_' . $dayStr;
$pointsSaleArray = PointSale::searchAll(['point_sale.id_producer' => $producer->id]);
$activeDistribution = false;
foreach ($pointsSaleArray as $pointSale) {
if ($pointSale->$fieldDeliveryDay) {
$activeDistribution = true;
}
}

}

public function actionPayOrders($date)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

if(strlen($date)) {
$this->actionProcessOrders('64ac0bdab7e9f5e48c4d991ec5201d57', $date) ;
if ($activeDistribution) {
$distribution = Distribution::initDistribution(date('Y-m-d', $dateTime), $producer->id);
$distribution->active(true);
}

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($key = '', $forceDate = '')
{
if ($key == '64ac0bdab7e9f5e48c4d991ec5201d57') {

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Yii::error('Cron process orders', 'log-cron');
$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 = Producer::searchAll();
public function actionPayOrders($date)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

foreach ($arrayProducers as $producer) {

$countOrders = 0;
$mailOrdersSend = false;

$distribution = Distribution::findOne([
'date' => $date,
'active' => 1,
'id_producer' => $producer->id,
]);

if($distribution) {

if ($hour == $producer->order_deadline || strlen($forceDate)) {
if (strlen($date)) {
$this->actionProcessOrders('64ac0bdab7e9f5e48c4d991ec5201d57', $date);
}

/*
* Paiement des commandes (paiement automatique)
*/
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($key = '', $forceDate = '')
{
if ($key == '64ac0bdab7e9f5e48c4d991ec5201d57') {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Yii::error('Cron process orders', 'log-cron');
$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);
}
}

$arrayOrders = Order::searchAll([
'distribution.date' => $date,
'distribution.id_producer' => $producer->id
], [
'conditions' => 'date_delete IS NULL'
]);
$arrayProducers = Producer::searchAll();

$configCredit = Producer::getConfig('credit', $producer->id);
foreach ($arrayProducers as $producer) {
$countOrders = 0;
$mailOrdersSend = false;

if ($arrayOrders && is_array($arrayOrders)) {
foreach ($arrayOrders as $order) {
if ($order->auto_payment && $configCredit) {
$distribution = Distribution::findOne([
'date' => $date,
'active' => 1,
'id_producer' => $producer->id,
]);

if ($order->getAmount(Order::AMOUNT_REMAINING) > 0) {
$order->saveCreditHistory(
CreditHistory::TYPE_PAYMENT,
$order->getAmount(Order::AMOUNT_REMAINING),
$order->distribution->id_producer,
$order->id_user,
User::ID_USER_SYSTEM
);
$countOrders++;
}
}
}
}
if ($distribution) {
if ($hour == $producer->order_deadline || strlen($forceDate)) {
/*
* Paiement des commandes (paiement automatique)
*/

/*
* Envoi des commandes par email au producteur
*/
if (!strlen($forceDate)) {
$arrayOrders = Order::searchAll([
$arrayOrders = Order::searchAll([
'distribution.date' => $date,
'distribution.id_producer' => $producer->id
], [
'conditions' => 'date_delete IS NULL'
]);

$user = User::searchOne([
'id_producer' => $producer->id,
'status' => User::STATUS_PRODUCER
]);

$mail = Yii::$app->mailer->compose(
[
'html' => 'cronOrdersSummary-html',
'text' => 'cronOrdersSummary-text',
], [
'date' => $date,
'orders' => $arrayOrders
]
)
->setTo($user->email)
->setFrom([Yii::$app->params['adminEmail'] => 'distrib']);

if (is_array($arrayOrders) && count($arrayOrders)) {
$subject = '[distrib] Commandes du ' . date('d/m', strtotime($date));

// génération du pdf de commande
Yii::$app->runAction('distribution/report-cron', [
'date' => $date,
'save' => true,
'idProducer' => $producer->id,
'key' => '64ac0bdab7e9f5e48c4d991ec5201d57'
]);
$mail->attach(Yii::getAlias('@app/web/pdf/Commandes-' . $date . '-' . $producer->id . '.pdf'));
} else {
$subject = '[distrib] Aucune commande';
}

$mail->setSubject($subject)
->send();
$mailOrdersSend = true;

/*$mail->setTo('contact@opendistrib.net')
->send();*/
}

if ($producer->active) {
$messageLog = $producer->name . ' : Distribution du ' . $date . ', ' . count($arrayOrders) . ' commande(s) enregistrée(s), ' . $countOrders . ' commande(s) payée(s), ' . ($mailOrdersSend ? 'Récapitulatif de commandes envoyé' : 'Aucun email envoyé');
$configCredit = Producer::getConfig('credit', $producer->id);

if ($arrayOrders && is_array($arrayOrders)) {
foreach ($arrayOrders as $order) {
if ($order->auto_payment && $configCredit) {
if ($order->getAmount(Order::AMOUNT_REMAINING) > 0) {
$order->saveCreditHistory(
CreditHistory::TYPE_PAYMENT,
$order->getAmount(Order::AMOUNT_REMAINING),
$order->distribution->id_producer,
$order->id_user,
User::ID_USER_SYSTEM
);
$countOrders++;
}
}
}
}

/*Yii::$app->mailer->compose()
->setFrom('contact@opendistrib.net')
->setTo('contact@opendistrib.net')
->setSubject('[Opendistrib] Log '.$producer->name)
->setTextBody($messageLog)
->send();*/
/*
* Envoi des commandes par email au producteur
*/
if (!strlen($forceDate)) {
$arrayOrders = Order::searchAll([
'distribution.date' => $date,
'distribution.id_producer' => $producer->id
], [
'conditions' => 'date_delete IS NULL'
]);

$mail = Yii::$app->mailer->compose(
[
'html' => 'cronOrdersSummary-html',
'text' => 'cronOrdersSummary-text',
], [
'date' => $date,
'orders' => $arrayOrders
]
)
->setFrom([Yii::$app->params['adminEmail'] => 'distrib'])
->setSubject($subject);;

if (is_array($arrayOrders) && count($arrayOrders)) {
$subject = '[distrib] Commandes du ' . date('d/m', strtotime($date));

// génération du pdf de commande
Yii::$app->runAction('distribution/report-cron', [
'date' => $date,
'save' => true,
'idProducer' => $producer->id,
'key' => '64ac0bdab7e9f5e48c4d991ec5201d57'
]);
$mail->attach(
Yii::getAlias('@app/web/pdf/Commandes-' . $date . '-' . $producer->id . '.pdf')
);
} else {
$subject = '[distrib] Aucune commande';
}

// 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) {
$mail->setTo($user->email)->send();
$mailOrdersSend = true;
}
}

}
}
}
if ($producer->active) {
$messageLog = $producer->name . ' : Distribution du ' . $date . ', ' . count(
$arrayOrders
) . ' commande(s) enregistrée(s), ' . $countOrders . ' commande(s) payée(s), ' . ($mailOrdersSend ? 'Récapitulatif de commandes envoyé' : 'Aucun email envoyé');
/*Yii::$app->mailer->compose()
->setFrom('contact@opendistrib.net')
->setTo('contact@opendistrib.net')
->setSubject('[Opendistrib] Log '.$producer->name)
->setTextBody($messageLog)
->send();*/
}
}
}
}
}
}
}

Notiek ielāde…
Atcelt
Saglabāt