$model->id_user = $subscription->id_user; | $model->id_user = $subscription->id_user; | ||||
$model->username = $subscription->username; | $model->username = $subscription->username; | ||||
$model->id_point_sale = $subscription->id_point_sale; | $model->id_point_sale = $subscription->id_point_sale; | ||||
$model->date_begin = date('d/m/Y', strtotime($subscription->date_begin)); | |||||
if (strlen($subscription->date_end)) { | |||||
$model->date_end = date('d/m/Y', strtotime($subscription->date_end)); | |||||
} | |||||
$model->monday = $subscription->monday; | $model->monday = $subscription->monday; | ||||
$model->tuesday = $subscription->tuesday; | $model->tuesday = $subscription->tuesday; | ||||
$model->wednesday = $subscription->wednesday; | $model->wednesday = $subscription->wednesday; | ||||
$model->sunday = $subscription->sunday; | $model->sunday = $subscription->sunday; | ||||
$model->auto_payment = $subscription->auto_payment; | $model->auto_payment = $subscription->auto_payment; | ||||
$model->week_frequency = $subscription->week_frequency; | $model->week_frequency = $subscription->week_frequency; | ||||
$model->date_begin = date('d/m/Y', strtotime($subscription->date_begin)); | |||||
if ($subscription->date_end) { | |||||
$model->date_end = date('d/m/Y', strtotime($subscription->date_end)); | |||||
} | |||||
if(strlen($subscription->comment)) { | if(strlen($subscription->comment)) { | ||||
$model->comment = $subscription->comment ; | $model->comment = $subscription->comment ; | ||||
} | } | ||||
]) ; | ]) ; | ||||
if ($model->load(Yii::$app->request->post()) && $model->validate()) { | if ($model->load(Yii::$app->request->post()) && $model->validate()) { | ||||
if (!strlen($model->date_end)) { | |||||
if (!$model->date_end) { | |||||
$model->date_end = null; | $model->date_end = null; | ||||
} | } | ||||
if ($model->save()) { | if ($model->save()) { | ||||
Yii::$app->getSession()->setFlash('success', 'Abonnement modifié'); | |||||
$subscription = Subscription::findOne($model->id) ; | $subscription = Subscription::findOne($model->id) ; | ||||
$messageOrdersDeleted = ''; | |||||
if($model->date_end) { | |||||
$countOrdersDeleted = $subscription->deleteOrdersIncomingDistributions(true); | |||||
if($countOrdersDeleted) { | |||||
$messageOrdersDeleted = '<br />'.$countOrdersDeleted.' commandes supprimées'; | |||||
} | |||||
} | |||||
Yii::$app->getSession()->setFlash('success', 'Abonnement modifié'.$messageOrdersDeleted); | |||||
$matchedDistributionsArray = $subscription->searchMatchedIncomingDistributions() ; | $matchedDistributionsArray = $subscription->searchMatchedIncomingDistributions() ; | ||||
if(count($matchedDistributionsArray)) { | if(count($matchedDistributionsArray)) { | ||||
return $this->redirect(['subscription/update-distributions', 'idSubscription' => $subscription->id,'update' => true]); | return $this->redirect(['subscription/update-distributions', 'idSubscription' => $subscription->id,'update' => true]); |
} | } | ||||
} | } | ||||
public function delete() | |||||
public function delete($force = false) | |||||
{ | { | ||||
// remboursement si l'utilisateur a payé pour cette commande | // remboursement si l'utilisateur a payé pour cette commande | ||||
$amountPaid = $this->getAmount(Order::AMOUNT_PAID); | $amountPaid = $this->getAmount(Order::AMOUNT_PAID); | ||||
if (Producer::getConfig('option_behavior_cancel_order') == Producer::BEHAVIOR_DELETE_ORDER_DELETE || | if (Producer::getConfig('option_behavior_cancel_order') == Producer::BEHAVIOR_DELETE_ORDER_DELETE || | ||||
(Producer::getConfig( | (Producer::getConfig( | ||||
'option_behavior_cancel_order' | 'option_behavior_cancel_order' | ||||
) == Producer::BEHAVIOR_DELETE_ORDER_STATUS && strlen($this->date_delete))) { | |||||
) == Producer::BEHAVIOR_DELETE_ORDER_STATUS && strlen($this->date_delete)) || | |||||
$force) { | |||||
ProductOrder::deleteAll(['id_order' => $this->id]); | ProductOrder::deleteAll(['id_order' => $this->id]); | ||||
return parent::delete(); | return parent::delete(); | ||||
} // status 'delete' | } // status 'delete' |
[['id_user', 'id_producer', 'id_point_sale', 'monday', 'tuesday', | [['id_user', 'id_producer', 'id_point_sale', 'monday', 'tuesday', | ||||
'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'week_frequency'], 'integer'], | 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'week_frequency'], 'integer'], | ||||
[['auto_payment'], 'boolean'], | [['auto_payment'], 'boolean'], | ||||
[['date_begin', 'date_end', 'username', 'comment'], 'safe'], | |||||
[['username', 'comment', 'date_begin', 'date_end'], 'safe'], | |||||
]; | ]; | ||||
} | } | ||||
} | } | ||||
// produits | // produits | ||||
$amountTotal = 0; | |||||
$productsAdd = false; | $productsAdd = false; | ||||
foreach ($this->productSubscription as $productSubscription) { | foreach ($this->productSubscription as $productSubscription) { | ||||
$productOrder = new ProductOrder; | $productOrder = new ProductOrder; | ||||
*/ | */ | ||||
public function searchMatchedIncomingDistributions() | public function searchMatchedIncomingDistributions() | ||||
{ | { | ||||
$producer = GlobalParam::getCurrentProducer(); | |||||
$params = [ | $params = [ | ||||
':date_earliest_order' => date('Y-m-d'), | ':date_earliest_order' => date('Y-m-d'), | ||||
':date_begin' => date('Y-m-d', strtotime($this->date_begin)), | ':date_begin' => date('Y-m-d', strtotime($this->date_begin)), | ||||
->andWhere('date > :date_earliest_order'); | ->andWhere('date > :date_earliest_order'); | ||||
if ($this->date_end) { | if ($this->date_end) { | ||||
$incomingDistributions->andWhere('date < :date_end'); | |||||
$incomingDistributions->andWhere('date <= :date_end'); | |||||
$params[':date_end'] = date('Y-m-d', strtotime($this->date_end)); | $params[':date_end'] = date('Y-m-d', strtotime($this->date_end)); | ||||
} | } | ||||
$incomingDistributions->params($params); | $incomingDistributions->params($params); | ||||
$incomingDistributionsArray = $incomingDistributions->all(); | $incomingDistributionsArray = $incomingDistributions->all(); | ||||
$incomingDistributions = Distribution::filterDistributionsByDateDelay($incomingDistributionsArray); | |||||
Distribution::filterDistributionsByDateDelay($incomingDistributionsArray); | |||||
$matchedIncomingDistributionsArray = []; | $matchedIncomingDistributionsArray = []; | ||||
foreach ($incomingDistributionsArray as $incomingDistribution) { | foreach ($incomingDistributionsArray as $incomingDistribution) { | ||||
return $matchedIncomingDistributionsArray; | return $matchedIncomingDistributionsArray; | ||||
} | } | ||||
public function deleteOrdersIncomingDistributions() | |||||
public function deleteOrdersIncomingDistributions($deleteAfterDateEnd = false) | |||||
{ | { | ||||
$dateStart = $this->date_begin; | |||||
$comparatorDateStart = '>='; | |||||
if($deleteAfterDateEnd) { | |||||
$dateStart = $this->date_end; | |||||
$comparatorDateStart = '>'; | |||||
} | |||||
$params = [ | $params = [ | ||||
':id_producer' => GlobalParam::getCurrentProducerId(), | ':id_producer' => GlobalParam::getCurrentProducerId(), | ||||
':date_today' => date('Y-m-d'), | ':date_today' => date('Y-m-d'), | ||||
':date_begin' => $this->date_begin, | |||||
':date_start' => $dateStart, | |||||
':id_subscription' => $this->id | ':id_subscription' => $this->id | ||||
]; | ]; | ||||
$orderDeadline = Producer::getConfig('order_deadline'); | $orderDeadline = Producer::getConfig('order_deadline'); | ||||
$hour = date('G'); | $hour = date('G'); | ||||
->joinWith('distribution') | ->joinWith('distribution') | ||||
->where('distribution.id_producer = :id_producer') | ->where('distribution.id_producer = :id_producer') | ||||
->andWhere($conditionDistributionDate) | ->andWhere($conditionDistributionDate) | ||||
->andWhere('distribution.date >= :date_begin') | |||||
->andWhere('distribution.date '.$comparatorDateStart.' :date_start') | |||||
->andWhere('order.id_subscription = :id_subscription'); | ->andWhere('order.id_subscription = :id_subscription'); | ||||
$orders->params($params); | $orders->params($params); | ||||
$ordersArray = $orders->all(); | $ordersArray = $orders->all(); | ||||
$configCredit = Producer::getConfig('credit'); | $configCredit = Producer::getConfig('credit'); | ||||
$countOrdersDeleted = 0; | |||||
if ($ordersArray && count($ordersArray)) { | if ($ordersArray && count($ordersArray)) { | ||||
foreach ($ordersArray as $order) { | foreach ($ordersArray as $order) { | ||||
); | ); | ||||
} | } | ||||
$order->delete(); | |||||
$order->delete(true); | |||||
$countOrdersDeleted ++; | |||||
} | } | ||||
} | } | ||||
return $countOrdersDeleted; | |||||
} | } | ||||
public function updateIncomingDistributions($update = false) | public function updateIncomingDistributions($update = false) |
'Y-m-d', | 'Y-m-d', | ||||
strtotime(str_replace('/', '-', $this->date_begin) | strtotime(str_replace('/', '-', $this->date_begin) | ||||
)); | )); | ||||
if (strlen($this->date_end)) { | |||||
$subscription->date_end = null; | |||||
if ($this->date_end) { | |||||
$subscription->date_end = date( | $subscription->date_end = date( | ||||
'Y-m-d', | 'Y-m-d', | ||||
strtotime(str_replace('/', '-', $this->date_end) | strtotime(str_replace('/', '-', $this->date_end) |