Browse Source

Mise à jour des distributions (PointSaleDistribution) lors de l'ajout/modification de points de vente

dev
Guillaume Bourgeois 5 years ago
parent
commit
80897e4c27
2 changed files with 67 additions and 6 deletions
  1. +2
    -0
      backend/controllers/PointSaleController.php
  2. +65
    -6
      common/models/Distribution.php

+ 2
- 0
backend/controllers/PointSaleController.php View File

if ($model->load(Yii::$app->request->post()) && $model->save()) { if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->processPointProduction(); $model->processPointProduction();
$model->processRestrictedAccess(); $model->processRestrictedAccess();
Distribution::linkPointSaleIncomingDistributions($model) ;
return $this->redirect(['index']); return $this->redirect(['index']);
} else { } else {
return $this->render('create', array_merge($this->initForm(), [ return $this->render('create', array_merge($this->initForm(), [
if ($model->load(Yii::$app->request->post()) && $model->save()) { if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->processPointProduction(); $model->processPointProduction();
$model->processRestrictedAccess(); $model->processRestrictedAccess();
Distribution::linkPointSaleIncomingDistributions($model) ;
return $this->redirect(['index']); return $this->redirect(['index']);
} else { } else {
return $this->render('update', array_merge($this->initForm($id), [ return $this->render('update', array_merge($this->initForm($id), [

+ 65
- 6
common/models/Distribution.php View File

} }
/** /**
* Lie un produit aux jours de distribution futurs.
* Retourne les distributions futures.
* *
* @param Product $product
* @return array
*/ */
public static function linkProductIncomingDistributions($product)
public static function getIncomingDistributions()
{ {
$distributionsArray = Distribution::find() $distributionsArray = Distribution::find()
->where('date > ' . date('Y-m-d')) ->where('date > ' . date('Y-m-d'))
->andWhere(['id_producer' => Producer::getId()]) ->andWhere(['id_producer' => Producer::getId()])
->all(); ->all();

return $distributionsArray ;
}
/**
* Lie un produit aux jours de distribution futurs.
*
* @param Product $product
*/
public static function linkProductIncomingDistributions($product)
{
$distributionsArray = self::getIncomingDistributions() ;
foreach ($distributionsArray as $distribution) { foreach ($distributionsArray as $distribution) {
$distribution->linkProduct($product) ; $distribution->linkProduct($product) ;
} }
* *
* @param Product $product * @param Product $product
*/ */
public function linkProduct($product) public function linkProduct($product)
{ {
$productDistribution = ProductDistribution::searchOne([ $productDistribution = ProductDistribution::searchOne([
$productDistribution->quantity_max = $product->quantity_max; $productDistribution->quantity_max = $product->quantity_max;
$dayDistribution = date('N', strtotime($this->date)); $dayDistribution = date('N', strtotime($this->date));
$daysArray = [ $daysArray = [
1 => 'monday', 1 => 'monday',
2 => 'tuesday', 2 => 'tuesday',


$productDistribution->save(); $productDistribution->save();
} }
/**
* Lie un point de vente aux jours de distribution futurs.
*
* @param PointSale $pointSale
*/
public static function linkPointSaleIncomingDistributions($pointSale)
{
$distributionsArray = self::getIncomingDistributions() ;
foreach ($distributionsArray as $distribution) {
$distribution->linkPointSale($pointSale) ;
}
}
/**
*
* @param type $pointSale
*/
public function linkPointSale($pointSale)
{
$pointSaleDistribution = PointSaleDistribution::searchOne([
'id_distribution' => $this->id,
'id_point_sale' => $pointSale->id
]) ;
if(!$pointSaleDistribution) {
$pointSaleDistribution = new PointSaleDistribution();
$pointSaleDistribution->id_distribution = $this->id;
$pointSaleDistribution->id_point_sale = $pointSale->id;
}
$dayDistribution = date('N', strtotime($this->date));
$daysArray = [
1 => 'monday',
2 => 'tuesday',
3 => 'wednesday',
4 => 'thursday',
5 => 'friday',
6 => 'saturday',
7 => 'sunday',
] ;
$pointSaleDistribution->delivery = 0 ;
$deliveryDay = 'delivery_'.$daysArray[$dayDistribution] ;
if($pointSale->$deliveryDay) {
$pointSaleDistribution->delivery = 1;
}
$pointSaleDistribution->save() ;
}


} }

Loading…
Cancel
Save