Bladeren bron

Gestion de la re-génération des commandes dans les distributions futures après la modification d'un abonnement

refactoring
Guillaume Bourgeois 5 jaren geleden
bovenliggende
commit
88428b3745
3 gewijzigde bestanden met toevoegingen van 55 en 7 verwijderingen
  1. +16
    -4
      backend/controllers/SubscriptionController.php
  2. +4
    -2
      backend/views/subscription/update_distributions.php
  3. +35
    -1
      common/models/Subscription.php

+ 16
- 4
backend/controllers/SubscriptionController.php Bestand weergeven

@@ -205,7 +205,15 @@ class SubscriptionController extends BackendController
}
if ($model->save()) {
Yii::$app->getSession()->setFlash('success', 'Abonnement modifié');
return $this->redirect(['subscription/index']);
$subscription = Subscription::findOne($model->id) ;
$matchedDistributionsArray = $subscription->searchMatchedIncomingDistributions() ;
if(count($matchedDistributionsArray)) {
return $this->redirect(['subscription/update-distributions', 'idSubscription' => $subscription->id,'update' => true]);
}
else {
return $this->redirect(['subscription/index']);
}
}
}

@@ -228,22 +236,26 @@ class SubscriptionController extends BackendController
return $this->redirect(['subscription/index']);
}

public function actionUpdateDistributions($idSubscription, $generate = false)
public function actionUpdateDistributions($idSubscription, $generate = false, $update = false)
{
$subscription = Subscription::findOne($idSubscription) ;
$matchedDistributionsArray = $subscription->searchMatchedIncomingDistributions() ;
if($generate) {
if($update) {
$subscription->deleteOrdersIncomingDistributions() ;
}
foreach($matchedDistributionsArray as $distribution) {
$subscription->add($distribution->date) ;
}
Yii::$app->getSession()->setFlash('success', 'Commandes ajoutées dans les distributions futures.');
Yii::$app->getSession()->setFlash('success', 'Commandes '.($update ? 're-' : '').'générées dans les distributions futures.');
return $this->redirect(['subscription/index']) ;
}
return $this->render('update_distributions',[
'matchedDistributionsArray' => $matchedDistributionsArray,
'idSubscription' => $idSubscription
'idSubscription' => $idSubscription,
'update' => $update
]) ;
}

+ 4
- 2
backend/views/subscription/update_distributions.php Bestand weergeven

@@ -45,9 +45,11 @@ $this->addBreadcrumb($this->getTitle()) ;
?>

<p>
Souhaitez-vous générer les commandes de cet abonnement pour les distributions futures ?
Souhaitez-vous <?php if($update): ?>re-<?php endif; ?>générer les commandes de cet abonnement pour les distributions futures ?
</p>

<div class="alert alert-warning">Attention, les éventuelles commandes futures modifiées par l'utilisateur seront supprimées pour être re-générées.</div>

<p>
<?php

@@ -64,4 +66,4 @@ foreach($matchedDistributionsArray as $distribution) {
</p>

<?= Html::a('Non', ['subscription/index'], ['class' => 'btn btn-default']) ?>&nbsp;
<?= Html::a('Oui, générer les commandes', ['subscription/update-distributions', 'idSubscription' => $idSubscription, 'generate' => true], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Oui, '.($update ? 're-' : '').'générer les commandes', ['subscription/update-distributions', 'idSubscription' => $idSubscription, 'generate' => true, 'update' => $update], ['class' => 'btn btn-primary']) ?>

+ 35
- 1
common/models/Subscription.php Bestand weergeven

@@ -306,13 +306,15 @@ class Subscription extends ActiveRecordCommon
public function searchMatchedIncomingDistributions()
{
$params = [
':date_today' => date('Y-m-d'),
':date_begin' => date('Y-m-d', strtotime($this->date_begin)),
':id_producer' => Producer::getId()
] ;
$incomingDistributions = Distribution::find()
->where('id_producer = :id_producer')
->andWhere('date > :date_begin') ;
->andWhere('date >= :date_begin')
->andWhere('date >= :date_today') ;
if($this->date_end) {
$incomingDistributions->andWhere('date < :date_end') ;
@@ -344,4 +346,36 @@ class Subscription extends ActiveRecordCommon
return $matchedIncomingDistributionsArray ;
}
public function deleteOrdersIncomingDistributions()
{
$params = [
':id_producer' => Producer::getId(),
':date_today' => date('Y-m-d'),
':date_begin' => $this->date_begin,
':id_subscription' => $this->id
];
$orders = Order::find()
->joinWith('distribution')
->where('distribution.id_producer = :id_producer')
->andWhere('distribution.date >= :date_today')
->andWhere('distribution.date >= :date_begin')
->andWhere('order.id_subscription = :id_subscription') ;
if($this->date_end) {
$orders->andWhere('distribution.date =< :date_end') ;
$params[':date_end'] = $this->date_end ;
}

$orders->params($params) ;
$ordersArray = $orders->all() ;
if($ordersArray && count($ordersArray)) {
foreach($ordersArray as $order) {
$order->delete() ;
}
}
}
}

Laden…
Annuleren
Opslaan