@@ -115,19 +115,8 @@ class ProductController extends BackendController | |||
Upload::uploadFile($model, 'photo'); | |||
$model->save(); | |||
// on ajoute un enregistrement ProductionProduit pour chaque production | |||
$distributionsArray = Distribution::find() | |||
->where('date > ' . date('Y-m-d')) | |||
->andWhere(['id_producer' => Producer::getId()]) | |||
->all(); | |||
foreach ($distributionsArray as $distribution) { | |||
$productDistribution = new ProductDistribution; | |||
$productDistribution->id_distribution = $distribution->id; | |||
$productDistribution->id_product = $model->id; | |||
$productDistribution->active = 0; | |||
$productDistribution->save(); | |||
} | |||
// link product / distribution | |||
Distribution::linkProductIncomingDistributions($model) ; | |||
Yii::$app->getSession()->setFlash('success', 'Produit <strong>'.Html::encode($model->name).'</strong> ajouté'); | |||
@@ -162,7 +151,12 @@ class ProductController extends BackendController | |||
$model->photo = ''; | |||
$model->save(); | |||
} | |||
if($model->apply_distributions) { | |||
// link product / distribution | |||
Distribution::linkProductIncomingDistributions($model) ; | |||
} | |||
Yii::$app->getSession()->setFlash('success', 'Produit <strong>'.Html::encode($model->name).'</strong> modifié'); | |||
return $this->redirect(['index']); | |||
} else { |
@@ -66,7 +66,7 @@ use yii\widgets\ActiveForm; | |||
} | |||
?> | |||
<h2>Jours de production</h2> | |||
<h2>Jours de distribution</h2> | |||
<div id="days-production"> | |||
<?= $form->field($model, 'monday')->checkbox() ?> | |||
<?= $form->field($model, 'tuesday')->checkbox() ?> | |||
@@ -80,6 +80,14 @@ use yii\widgets\ActiveForm; | |||
<?= $form->field($model, 'id_producer')->hiddenInput()->label('') ?> | |||
<?php | |||
if(!$model->isNewRecord) { | |||
echo $form->field($model, 'apply_distributions') | |||
->checkbox() | |||
->hint('Sélectionnez cette option si vous souhaitez que ces modifications soient répercutées dans les distributions à venir déjà initialisées.'); | |||
} | |||
?> | |||
<div class="form-group"> | |||
<?= Html::submitButton($model->isNewRecord ? 'Ajouter' : 'Modifier', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> | |||
</div> |
@@ -181,48 +181,76 @@ class Distribution extends ActiveRecordCommon | |||
$products = Product::searchAll(); | |||
if ($distribution) { | |||
$dayDistribution = date('N', strtotime($distribution->date)); | |||
$productsDistribution = ProductDistribution::searchAll([ | |||
'id_distribution' => $distribution->id | |||
]) ; | |||
if (!count($productsDistribution)) { | |||
foreach ($products as $product) { | |||
$productDistribution = new ProductDistribution(); | |||
$productDistribution->id_distribution = $distribution->id; | |||
$productDistribution->id_product = $product->id; | |||
$productDistribution->active = 0; | |||
if ($product->active && $dayDistribution == 1 && $product->monday) { | |||
$productDistribution->active = 1; | |||
} | |||
if ($product->active && $dayDistribution == 2 && $product->tuesday) { | |||
$productDistribution->active = 1; | |||
} | |||
if ($product->active && $dayDistribution == 3 && $product->wednesday) { | |||
$productDistribution->active = 1; | |||
} | |||
if ($product->active && $dayDistribution == 4 && $product->thursday) { | |||
$productDistribution->active = 1; | |||
} | |||
if ($product->active && $dayDistribution == 5 && $product->friday) { | |||
$productDistribution->active = 1; | |||
} | |||
if ($product->active && $dayDistribution == 6 && $product->saturday) { | |||
$productDistribution->active = 1; | |||
} | |||
if ($product->active && $dayDistribution == 7 && $product->sunday) { | |||
$productDistribution->active = 1; | |||
} | |||
$productDistribution->quantity_max = $product->quantity_max; | |||
$productDistribution->save(); | |||
$distribution->linkProduct($product) ; | |||
} | |||
} | |||
} | |||
return $distribution ; | |||
} | |||
/** | |||
* Lie un produit aux jours de distribution futurs. | |||
* | |||
* @param Product $product | |||
*/ | |||
public static function linkProductIncomingDistributions($product) | |||
{ | |||
$distributionsArray = Distribution::find() | |||
->where('date > ' . date('Y-m-d')) | |||
->andWhere(['id_producer' => Producer::getId()]) | |||
->all(); | |||
foreach ($distributionsArray as $distribution) { | |||
$distribution->linkProduct($product) ; | |||
} | |||
} | |||
/** | |||
* Lie un produit à la distribution. | |||
* | |||
* @param Product $product | |||
*/ | |||
public function linkProduct($product) | |||
{ | |||
$productDistribution = ProductDistribution::searchOne([ | |||
'id_distribution' => $this->id, | |||
'id_product' => $product->id | |||
]) ; | |||
if(!$productDistribution) { | |||
$productDistribution = new ProductDistribution(); | |||
$productDistribution->id_distribution = $this->id; | |||
$productDistribution->id_product = $product->id; | |||
} | |||
$productDistribution->quantity_max = $product->quantity_max; | |||
$dayDistribution = date('N', strtotime($this->date)); | |||
$daysArray = [ | |||
1 => 'monday', | |||
2 => 'tuesday', | |||
3 => 'wednesday', | |||
4 => 'thursday', | |||
5 => 'friday', | |||
6 => 'saturday', | |||
7 => 'sunday', | |||
] ; | |||
$productDistribution->active = 0; | |||
$day = $daysArray[$dayDistribution] ; | |||
if($product->active && $product->$day) { | |||
$productDistribution->active = 1; | |||
} | |||
$productDistribution->save(); | |||
} | |||
} |
@@ -56,7 +56,7 @@ use common\components\ActiveRecordCommon ; | |||
class Product extends ActiveRecordCommon | |||
{ | |||
var $total = 0; | |||
var $test ; | |||
var $apply_distributions = false ; | |||
const SALE_MODE_UNIT = 'unit' ; | |||
const SALE_MODE_WEIGHT = 'weight' ; | |||
@@ -77,7 +77,7 @@ class Product extends ActiveRecordCommon | |||
return [ | |||
[['name', 'id_producer'], 'required'], | |||
[['active', 'order', 'quantity_max', 'id_producer'], 'integer'], | |||
[['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'unavailable'], 'boolean'], | |||
[['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'unavailable','apply_distributions'], 'boolean'], | |||
[['price', 'weight'], 'number'], | |||
[[ 'photo'], 'file'], | |||
[['name', 'description', 'photo'], 'string', 'max' => 255], | |||
@@ -109,6 +109,7 @@ class Product extends ActiveRecordCommon | |||
'order' => 'Ordre', | |||
'quantity_max' => 'Quantité max par défaut', | |||
'unavailable' => 'Épuisé', | |||
'apply_distributions' => 'Appliquer ces modifications dans les distributions futures' | |||
]; | |||
} | |||
@@ -143,5 +143,5 @@ class ProductDistribution extends ActiveRecordCommon | |||
return $arrayProductsDistribution; | |||
} | |||
} |