} | } | ||||
if ($model->save()) { | if ($model->save()) { | ||||
Yii::$app->getSession()->setFlash('success', 'Abonnement modifié'); | 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']); | |||||
} | |||||
} | } | ||||
} | } | ||||
return $this->redirect(['subscription/index']); | return $this->redirect(['subscription/index']); | ||||
} | } | ||||
public function actionUpdateDistributions($idSubscription, $generate = false) | |||||
public function actionUpdateDistributions($idSubscription, $generate = false, $update = false) | |||||
{ | { | ||||
$subscription = Subscription::findOne($idSubscription) ; | $subscription = Subscription::findOne($idSubscription) ; | ||||
$matchedDistributionsArray = $subscription->searchMatchedIncomingDistributions() ; | $matchedDistributionsArray = $subscription->searchMatchedIncomingDistributions() ; | ||||
if($generate) { | if($generate) { | ||||
if($update) { | |||||
$subscription->deleteOrdersIncomingDistributions() ; | |||||
} | |||||
foreach($matchedDistributionsArray as $distribution) { | foreach($matchedDistributionsArray as $distribution) { | ||||
$subscription->add($distribution->date) ; | $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->redirect(['subscription/index']) ; | ||||
} | } | ||||
return $this->render('update_distributions',[ | return $this->render('update_distributions',[ | ||||
'matchedDistributionsArray' => $matchedDistributionsArray, | 'matchedDistributionsArray' => $matchedDistributionsArray, | ||||
'idSubscription' => $idSubscription | |||||
'idSubscription' => $idSubscription, | |||||
'update' => $update | |||||
]) ; | ]) ; | ||||
} | } | ||||
?> | ?> | ||||
<p> | <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> | </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> | <p> | ||||
<?php | <?php | ||||
</p> | </p> | ||||
<?= Html::a('Non', ['subscription/index'], ['class' => 'btn btn-default']) ?> | <?= 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']) ?> |
public function searchMatchedIncomingDistributions() | public function searchMatchedIncomingDistributions() | ||||
{ | { | ||||
$params = [ | $params = [ | ||||
':date_today' => date('Y-m-d'), | |||||
':date_begin' => date('Y-m-d', strtotime($this->date_begin)), | ':date_begin' => date('Y-m-d', strtotime($this->date_begin)), | ||||
':id_producer' => Producer::getId() | ':id_producer' => Producer::getId() | ||||
] ; | ] ; | ||||
$incomingDistributions = Distribution::find() | $incomingDistributions = Distribution::find() | ||||
->where('id_producer = :id_producer') | ->where('id_producer = :id_producer') | ||||
->andWhere('date > :date_begin') ; | |||||
->andWhere('date >= :date_begin') | |||||
->andWhere('date >= :date_today') ; | |||||
if($this->date_end) { | if($this->date_end) { | ||||
$incomingDistributions->andWhere('date < :date_end') ; | $incomingDistributions->andWhere('date < :date_end') ; | ||||
return $matchedIncomingDistributionsArray ; | 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() ; | |||||
} | |||||
} | |||||
} | |||||
} | } |