@@ -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 | |||
]) ; | |||
} | |||
@@ -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']) ?> | |||
<?= 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']) ?> |
@@ -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() ; | |||
} | |||
} | |||
} | |||
} |