|
|
|
|
|
|
|
|
namespace frontend\controllers; |
|
|
namespace frontend\controllers; |
|
|
|
|
|
|
|
|
use common\models\ProductionProduit; |
|
|
use common\models\ProductionProduit; |
|
|
|
|
|
|
|
|
use common\models\CommandeProduit; |
|
|
use common\models\CommandeProduit; |
|
|
|
|
|
|
|
|
use Yii; |
|
|
use Yii; |
|
|
use yii\filters\AccessControl; |
|
|
use yii\filters\AccessControl; |
|
|
use common\models\Commande ; |
|
|
|
|
|
use common\models\PointVente ; |
|
|
|
|
|
use common\models\Production ; |
|
|
|
|
|
use common\models\Produit ; |
|
|
|
|
|
|
|
|
|
|
|
class CommandeController extends \yii\web\Controller |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
public function behaviors() |
|
|
|
|
|
{ |
|
|
|
|
|
return [ |
|
|
|
|
|
'access' => [ |
|
|
|
|
|
'class' => AccessControl::className(), |
|
|
|
|
|
'rules' => [ |
|
|
|
|
|
[ |
|
|
|
|
|
'allow' => true, |
|
|
|
|
|
'roles' => ['@'], |
|
|
|
|
|
] |
|
|
|
|
|
], |
|
|
|
|
|
], |
|
|
|
|
|
]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function actionInfosProduction($date) { |
|
|
|
|
|
|
|
|
|
|
|
$production = Production::find()->where(['date'=>$date])->one() ; |
|
|
|
|
|
|
|
|
|
|
|
if($production) { |
|
|
|
|
|
|
|
|
|
|
|
$produits_dispos = ProductionProduit::findProduits($production->id) ; |
|
|
|
|
|
|
|
|
|
|
|
return json_encode([ |
|
|
|
|
|
'produits_dispos' => $produits_dispos, |
|
|
|
|
|
'livraison' => (int) $production->livraison |
|
|
|
|
|
]) ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return json_encode([]) ; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static function initForm($commande = null) { |
|
|
|
|
|
|
|
|
|
|
|
// points de vente |
|
|
|
|
|
$points_vente = PointVente::find()->all() ; |
|
|
|
|
|
$arr_points_vente = $points_vente ; |
|
|
|
|
|
/*$arr_points_vente = array(''=>'--') ; |
|
|
|
|
|
foreach($points_vente as $pv) |
|
|
|
|
|
$arr_points_vente[$pv->id] = $pv->nom.' ('.$pv->localite.')' ;*/ |
|
|
|
|
|
|
|
|
|
|
|
// jours de production; |
|
|
|
|
|
if(date('H') >= 20) { |
|
|
|
|
|
$date = date('Y-m-d',strtotime(date('Y-m-d')) + 60*60*24) ; |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
$date = date('Y-m-d') ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$jours_production = Production::find() |
|
|
|
|
|
->where(['actif'=>1]) |
|
|
|
|
|
->andWhere('date > :date') |
|
|
|
|
|
->addParams([':date'=>$date]) |
|
|
|
|
|
->all() ; |
|
|
|
|
|
|
|
|
|
|
|
$arr_jours_production = array(''=>'--') ; |
|
|
|
|
|
foreach($jours_production as $j) |
|
|
|
|
|
$arr_jours_production[$j->id] = date('d/m/Y',strtotime($j->date)) ; |
|
|
|
|
|
|
|
|
|
|
|
// produits |
|
|
|
|
|
$produits = Produit::find()->where(['actif'=>1])->andWhere('vrac IS NULL OR vrac = 0')->orderBy('order ASC')->all() ; |
|
|
|
|
|
$arr_produits = array() ; |
|
|
|
|
|
foreach($produits as $p) |
|
|
|
|
|
$arr_produits[] = $p ; |
|
|
|
|
|
|
|
|
|
|
|
// produits vrac |
|
|
|
|
|
$produits_vrac = Produit::find()->where(['actif'=>1, 'vrac'=>1])->orderBy('order ASC')->all() ; |
|
|
|
|
|
|
|
|
|
|
|
// produits selec |
|
|
|
|
|
$posts = Yii::$app->request->post() ; |
|
|
|
|
|
$produits_selec = [] ; |
|
|
|
|
|
if(isset($posts['Produit'])) { |
|
|
|
|
|
foreach($posts['Produit'] as $key => $quantity) { |
|
|
|
|
|
$key = (int) str_replace('produit_', '', $key) ; |
|
|
|
|
|
$p = Produit::find()->where(['id'=>$key])->one() ; |
|
|
|
|
|
if($p && $quantity) |
|
|
|
|
|
$produits_selec[$p->id]= (int) $quantity ; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
elseif(!is_null($commande)) { |
|
|
|
|
|
$produits_commande = CommandeProduit::find()->where(['id_commande'=>$commande->id])->all() ; |
|
|
|
|
|
foreach($produits_commande as $pc) { |
|
|
|
|
|
$produits_selec[$pc->id_produit]= (int) $pc->quantite ; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$produits_dispos = [] ; |
|
|
|
|
|
$production = null ; |
|
|
|
|
|
if(!is_null($commande)) { |
|
|
|
|
|
$produits_dispos = ProductionProduit::findProduits($commande->id_production) ; |
|
|
|
|
|
$production = Production::find()->where(['id'=>$commande->id_production])->one() ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// commandes déjà réalisées |
|
|
|
|
|
/*if(!is_null($commande)) { |
|
|
|
|
|
$commandes = Commande::find() |
|
|
|
|
|
->where(['id_user'=> Yii::$app->user->identity->id]) |
|
|
|
|
|
->andWhere('date > :date') |
|
|
|
|
|
->andWhere('id != '.$commande->id) |
|
|
|
|
|
->addParams([':date'=>date('Y-m-d')]) |
|
|
|
|
|
->all() ; |
|
|
|
|
|
} |
|
|
|
|
|
else {*/ |
|
|
|
|
|
$commandes = Commande::find() |
|
|
|
|
|
->where(['id_user'=> Yii::$app->user->identity->id]) |
|
|
|
|
|
//->andWhere('date > :date') |
|
|
|
|
|
//->addParams([':date'=>date('Y-m-d 00:00:00')]) |
|
|
|
|
|
->all() ; |
|
|
|
|
|
//} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return [ |
|
|
|
|
|
'points_vente' => $arr_points_vente, |
|
|
|
|
|
'jours_production' => $arr_jours_production, |
|
|
|
|
|
'produits' => $produits, |
|
|
|
|
|
'produits_selec' => $produits_selec, |
|
|
|
|
|
'produits_dispos' => $produits_dispos, |
|
|
|
|
|
'production' => $production, |
|
|
|
|
|
'commandes_en_cours' => $commandes, |
|
|
|
|
|
'produits_vrac' => $produits_vrac |
|
|
|
|
|
] ; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function actionIndex() |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
// liste des commandes |
|
|
|
|
|
$commandes = Commande::find() |
|
|
|
|
|
->with('commandeProduits','pointVente') |
|
|
|
|
|
->joinWith('production') |
|
|
|
|
|
->where(['id_user'=>Yii::$app->user->id]) |
|
|
|
|
|
//->andWhere('production.date < '.) |
|
|
|
|
|
->orderBy('production.date DESC') |
|
|
|
|
|
->limit(40) |
|
|
|
|
|
->all() ; |
|
|
|
|
|
|
|
|
|
|
|
// initilisation commandes |
|
|
|
|
|
foreach($commandes as $c) |
|
|
|
|
|
$c->init() ; |
|
|
|
|
|
|
|
|
|
|
|
return $this->render('index',[ |
|
|
|
|
|
'commandes' => $commandes, |
|
|
|
|
|
'commande_ok' => Yii::$app->getRequest()->get('commande_ok',false), |
|
|
|
|
|
'annule_ok' => Yii::$app->getRequest()->get('annule_ok',false), |
|
|
|
|
|
'pate_deja_petrie' => Yii::$app->getRequest()->get('pate_deja_petrie',false), |
|
|
|
|
|
]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function actionCreate() |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
$commande = new Commande ; |
|
|
|
|
|
|
|
|
|
|
|
$posts = Yii::$app->request->post() ; |
|
|
|
|
|
|
|
|
|
|
|
if ($commande->load($posts)) |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
$commande = Commande::find()->where('id_production = '.$posts['Commande']['id_production'])->andWhere('id_user = '.Yii::$app->user->id)->one() ; |
|
|
|
|
|
if(!$commande) |
|
|
|
|
|
{ |
|
|
|
|
|
$commande = new Commande ; |
|
|
|
|
|
$commande->load(Yii::$app->request->post()) ; |
|
|
|
|
|
$commande->id_user = Yii::$app->user->id ; |
|
|
|
|
|
$commande->date = date('Y-m-d H:i:s') ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$this->gestionForm($commande) ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return $this->render('create', array_merge(self::initForm(),[ |
|
|
|
|
|
'model' => $commande, |
|
|
|
|
|
])); |
|
|
|
|
|
|
|
|
use common\models\Commande; |
|
|
|
|
|
use common\models\PointVente; |
|
|
|
|
|
use common\models\Production; |
|
|
|
|
|
use common\models\Produit; |
|
|
|
|
|
|
|
|
|
|
|
class CommandeController extends \yii\web\Controller { |
|
|
|
|
|
|
|
|
|
|
|
public function behaviors() { |
|
|
|
|
|
return [ |
|
|
|
|
|
'access' => [ |
|
|
|
|
|
'class' => AccessControl::className(), |
|
|
|
|
|
'rules' => [ |
|
|
|
|
|
[ |
|
|
|
|
|
'allow' => true, |
|
|
|
|
|
'roles' => ['@'], |
|
|
|
|
|
] |
|
|
|
|
|
], |
|
|
|
|
|
], |
|
|
|
|
|
]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function actionInfosProduction($date) { |
|
|
|
|
|
|
|
|
|
|
|
$production = Production::find()->where(['date' => $date])->one(); |
|
|
|
|
|
|
|
|
|
|
|
if ($production) { |
|
|
|
|
|
|
|
|
|
|
|
$produits_dispos = ProductionProduit::findProduits($production->id); |
|
|
|
|
|
|
|
|
|
|
|
return json_encode([ |
|
|
|
|
|
'produits_dispos' => $produits_dispos, |
|
|
|
|
|
'livraison' => (int) $production->livraison |
|
|
|
|
|
]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return json_encode([]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static function initForm($commande = null) { |
|
|
|
|
|
|
|
|
|
|
|
// points de vente |
|
|
|
|
|
$points_vente = PointVente::find()->all(); |
|
|
|
|
|
$arr_points_vente = $points_vente; |
|
|
|
|
|
/* $arr_points_vente = array(''=>'--') ; |
|
|
|
|
|
foreach($points_vente as $pv) |
|
|
|
|
|
$arr_points_vente[$pv->id] = $pv->nom.' ('.$pv->localite.')' ; */ |
|
|
|
|
|
|
|
|
|
|
|
// jours de production; |
|
|
|
|
|
if (date('H') >= 20) { |
|
|
|
|
|
$date = date('Y-m-d', strtotime(date('Y-m-d')) + 60 * 60 * 24); |
|
|
|
|
|
} else { |
|
|
|
|
|
$date = date('Y-m-d'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$jours_production = Production::find() |
|
|
|
|
|
->where(['actif' => 1]) |
|
|
|
|
|
->andWhere('date > :date') |
|
|
|
|
|
->addParams([':date' => $date]) |
|
|
|
|
|
->all(); |
|
|
|
|
|
|
|
|
|
|
|
$arr_jours_production = array('' => '--'); |
|
|
|
|
|
foreach ($jours_production as $j) |
|
|
|
|
|
$arr_jours_production[$j->id] = date('d/m/Y', strtotime($j->date)); |
|
|
|
|
|
|
|
|
|
|
|
// produits |
|
|
|
|
|
$produits = Produit::find()->where(['actif' => 1])->andWhere('vrac IS NULL OR vrac = 0')->orderBy('order ASC')->all(); |
|
|
|
|
|
$arr_produits = array(); |
|
|
|
|
|
foreach ($produits as $p) |
|
|
|
|
|
$arr_produits[] = $p; |
|
|
|
|
|
|
|
|
|
|
|
// produits vrac |
|
|
|
|
|
$produits_vrac = Produit::find()->where(['actif' => 1, 'vrac' => 1])->orderBy('order ASC')->all(); |
|
|
|
|
|
|
|
|
|
|
|
// produits selec |
|
|
|
|
|
$posts = Yii::$app->request->post(); |
|
|
|
|
|
$produits_selec = []; |
|
|
|
|
|
if (isset($posts['Produit'])) { |
|
|
|
|
|
foreach ($posts['Produit'] as $key => $quantity) { |
|
|
|
|
|
$key = (int) str_replace('produit_', '', $key); |
|
|
|
|
|
$p = Produit::find()->where(['id' => $key])->one(); |
|
|
|
|
|
if ($p && $quantity) |
|
|
|
|
|
$produits_selec[$p->id] = (int) $quantity; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
elseif (!is_null($commande)) { |
|
|
|
|
|
$produits_commande = CommandeProduit::find()->where(['id_commande' => $commande->id])->all(); |
|
|
|
|
|
foreach ($produits_commande as $pc) { |
|
|
|
|
|
$produits_selec[$pc->id_produit] = (int) $pc->quantite; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$produits_dispos = []; |
|
|
|
|
|
$production = null; |
|
|
|
|
|
if (!is_null($commande)) { |
|
|
|
|
|
$produits_dispos = ProductionProduit::findProduits($commande->id_production); |
|
|
|
|
|
$production = Production::find()->where(['id' => $commande->id_production])->one(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// commandes déjà réalisées |
|
|
|
|
|
/* if(!is_null($commande)) { |
|
|
|
|
|
$commandes = Commande::find() |
|
|
|
|
|
->where(['id_user'=> Yii::$app->user->identity->id]) |
|
|
|
|
|
->andWhere('date > :date') |
|
|
|
|
|
->andWhere('id != '.$commande->id) |
|
|
|
|
|
->addParams([':date'=>date('Y-m-d')]) |
|
|
|
|
|
->all() ; |
|
|
|
|
|
} |
|
|
|
|
|
else { */ |
|
|
|
|
|
$commandes = Commande::find() |
|
|
|
|
|
->where(['id_user' => Yii::$app->user->identity->id]) |
|
|
|
|
|
//->andWhere('date > :date') |
|
|
|
|
|
//->addParams([':date'=>date('Y-m-d 00:00:00')]) |
|
|
|
|
|
->all(); |
|
|
|
|
|
//} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return [ |
|
|
|
|
|
'points_vente' => $arr_points_vente, |
|
|
|
|
|
'jours_production' => $arr_jours_production, |
|
|
|
|
|
'produits' => $produits, |
|
|
|
|
|
'produits_selec' => $produits_selec, |
|
|
|
|
|
'produits_dispos' => $produits_dispos, |
|
|
|
|
|
'production' => $production, |
|
|
|
|
|
'commandes_en_cours' => $commandes, |
|
|
|
|
|
'produits_vrac' => $produits_vrac |
|
|
|
|
|
]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function actionIndex() { |
|
|
|
|
|
|
|
|
|
|
|
// liste des boulangeries |
|
|
|
|
|
$boulangeries = Yii::$app->user->identity->getBoulangeriesFavoris(); |
|
|
|
|
|
|
|
|
|
|
|
// liste des commandes |
|
|
|
|
|
$commandes = Commande::find() |
|
|
|
|
|
->with('commandeProduits', 'pointVente') |
|
|
|
|
|
->joinWith('production') |
|
|
|
|
|
->where(['id_user' => Yii::$app->user->id]) |
|
|
|
|
|
//->andWhere('production.date < '.) |
|
|
|
|
|
->orderBy('production.date DESC') |
|
|
|
|
|
->limit(40) |
|
|
|
|
|
->all(); |
|
|
|
|
|
|
|
|
|
|
|
// initilisation commandes |
|
|
|
|
|
foreach ($commandes as $c) |
|
|
|
|
|
$c->init(); |
|
|
|
|
|
|
|
|
|
|
|
return $this->render('index', [ |
|
|
|
|
|
'commandes' => $commandes, |
|
|
|
|
|
'commande_ok' => Yii::$app->getRequest()->get('commande_ok', false), |
|
|
|
|
|
'annule_ok' => Yii::$app->getRequest()->get('annule_ok', false), |
|
|
|
|
|
'pate_deja_petrie' => Yii::$app->getRequest()->get('pate_deja_petrie', false), |
|
|
|
|
|
'boulangeries' => $boulangeries |
|
|
|
|
|
]); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function actionUpdate($id) |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
$commande = Commande::find()->where(['id'=>$id])->one(); |
|
|
|
|
|
|
|
|
|
|
|
if ($commande && $commande->load(Yii::$app->request->post())) { |
|
|
|
|
|
$commande->date_update = date('Y-m-d H:i:s') ; |
|
|
|
|
|
$this->gestionForm($commande) ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $this->render('update', array_merge(self::initForm($commande),[ |
|
|
|
|
|
'model' => $commande, |
|
|
|
|
|
'commande_introuvable' => !$commande, |
|
|
|
|
|
])); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function actionCreate() { |
|
|
|
|
|
|
|
|
|
|
|
$commande = new Commande; |
|
|
|
|
|
|
|
|
|
|
|
$posts = Yii::$app->request->post(); |
|
|
|
|
|
|
|
|
|
|
|
if ($commande->load($posts)) { |
|
|
|
|
|
|
|
|
|
|
|
$commande = Commande::find()->where('id_production = ' . $posts['Commande']['id_production'])->andWhere('id_user = ' . Yii::$app->user->id)->one(); |
|
|
|
|
|
if (!$commande) { |
|
|
|
|
|
$commande = new Commande; |
|
|
|
|
|
$commande->load(Yii::$app->request->post()); |
|
|
|
|
|
$commande->id_user = Yii::$app->user->id; |
|
|
|
|
|
$commande->date = date('Y-m-d H:i:s'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$this->gestionForm($commande); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return $this->render('create', array_merge(self::initForm(), [ |
|
|
|
|
|
'model' => $commande, |
|
|
|
|
|
])); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function actionUpdate($id) { |
|
|
|
|
|
|
|
|
|
|
|
$commande = Commande::find()->where(['id' => $id])->one(); |
|
|
|
|
|
|
|
|
|
|
|
if ($commande && $commande->load(Yii::$app->request->post())) { |
|
|
|
|
|
$commande->date_update = date('Y-m-d H:i:s'); |
|
|
|
|
|
$this->gestionForm($commande); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $this->render('update', array_merge(self::initForm($commande), [ |
|
|
|
|
|
'model' => $commande, |
|
|
|
|
|
'commande_introuvable' => !$commande, |
|
|
|
|
|
])); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function gestionForm($commande) { |
|
|
public function gestionForm($commande) { |
|
|
$posts = Yii::$app->request->post() ; |
|
|
|
|
|
$produits = array() ; |
|
|
|
|
|
|
|
|
|
|
|
$quantite_totale = 0 ; |
|
|
|
|
|
|
|
|
|
|
|
foreach($posts['Produit'] as $key => $quantity) { |
|
|
|
|
|
$key = (int) str_replace('produit_', '', $key) ; |
|
|
|
|
|
$p = Produit::find()->where(['id'=>$key])->one() ; |
|
|
|
|
|
$quantite_totale += $quantity ; |
|
|
|
|
|
if($p && $quantity) |
|
|
|
|
|
$produits[]= $p ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// nombre de produits |
|
|
|
|
|
$err_nb_produits = false ; |
|
|
|
|
|
if(!Yii::$app->user->identity->confiance && $quantite_totale > 3) { |
|
|
|
|
|
$err_nb_produits = true ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// date |
|
|
|
|
|
$err_date = false ; |
|
|
|
|
|
$pate_deja_petrie = false ; |
|
|
|
|
|
if(isset($commande->id_production)) { |
|
|
|
|
|
// date de commande |
|
|
|
|
|
$production = Production::find()->where(['id'=>$commande->id_production])->one() ; |
|
|
|
|
|
if(date('H') >= 20) { |
|
|
|
|
|
$date = date('Y-m-d',strtotime(date('Y-m-d')) + 60*60*24) ; |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
$date = date('Y-m-d') ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//die($production->date.' '.$date) ; |
|
|
|
|
|
|
|
|
|
|
|
if($production->date < $date) { |
|
|
|
|
|
$err_date = true ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// pate déjà pétrie ou non |
|
|
|
|
|
/*if(date('Y-m-d', strtotime(date('Y-m-d')) + 60*60*24*3) > $production->date && date('H', strtotime(date('Y-m-d')) + 60*60*24*3) < 20) { |
|
|
|
|
|
$pate_deja_petrie = true ; |
|
|
|
|
|
}*/ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ($commande->validate() && count($produits) && !$err_nb_produits && !$err_date) { |
|
|
|
|
|
// sauvegarde de la commande |
|
|
|
|
|
$commande->save() ; |
|
|
|
|
|
|
|
|
|
|
|
// suppression de tous les enregistrements CommandeProduit |
|
|
|
|
|
if(!is_null($commande)) { |
|
|
|
|
|
CommandeProduit::deleteAll(['id_commande'=>$commande->id]) ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// produits dispos |
|
|
|
|
|
$produits_dispos = ProductionProduit::findProduits($production->id) ; |
|
|
|
|
|
|
|
|
|
|
|
// sauvegarde des produits |
|
|
|
|
|
foreach($produits as $p) { |
|
|
|
|
|
// le produit doit etre dispo à la vente |
|
|
|
|
|
if(isset($produits_dispos[$p->id]) && $produits_dispos[$p->id]['actif']) { |
|
|
|
|
|
|
|
|
|
|
|
$commande_produit = new CommandeProduit() ; |
|
|
|
|
|
$commande_produit->id_commande = $commande->id ; |
|
|
|
|
|
$commande_produit->id_produit = $p->id ; |
|
|
|
|
|
|
|
|
|
|
|
$commande_produit->prix = $p->prix ; |
|
|
|
|
|
|
|
|
|
|
|
$quantite_voulue = (int) $posts['Produit']['produit_'.$p->id] ; |
|
|
|
|
|
if($quantite_voulue > $produits_dispos[$p->id]['quantite_restante'] && !$produits_dispos[$p->id]['vrac']) |
|
|
|
|
|
$quantite_voulue = $produits_dispos[$p->id]['quantite_restante'] ; |
|
|
|
|
|
|
|
|
|
|
|
$commande_produit->quantite = $quantite_voulue ; |
|
|
|
|
|
|
|
|
|
|
|
$commande_produit->save() ; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
// redirection |
|
|
|
|
|
$this->redirect(Yii::$app->urlManager->createUrl(['commande/index','commande_ok'=>true, 'pate_deja_petrie'=>$pate_deja_petrie])) ; |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
if(!count($produits)) |
|
|
|
|
|
Yii::$app->session->setFlash('error', "Vous n'avez choisi aucun produit"); |
|
|
|
|
|
if($err_nb_produits) |
|
|
|
|
|
Yii::$app->session->setFlash('error', "Vous ne pouvez pas commander plus de 3 produits"); |
|
|
|
|
|
if($err_date) |
|
|
|
|
|
Yii::$app->session->setFlash('error', "Vous ne pouvez pas commander pour cette date."); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
$posts = Yii::$app->request->post(); |
|
|
|
|
|
$produits = array(); |
|
|
|
|
|
|
|
|
|
|
|
$quantite_totale = 0; |
|
|
|
|
|
|
|
|
|
|
|
foreach ($posts['Produit'] as $key => $quantity) { |
|
|
|
|
|
$key = (int) str_replace('produit_', '', $key); |
|
|
|
|
|
$p = Produit::find()->where(['id' => $key])->one(); |
|
|
|
|
|
$quantite_totale += $quantity; |
|
|
|
|
|
if ($p && $quantity) |
|
|
|
|
|
$produits[] = $p; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// nombre de produits |
|
|
|
|
|
$err_nb_produits = false; |
|
|
|
|
|
if (!Yii::$app->user->identity->confiance && $quantite_totale > 3) { |
|
|
|
|
|
$err_nb_produits = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// date |
|
|
|
|
|
$err_date = false; |
|
|
|
|
|
$pate_deja_petrie = false; |
|
|
|
|
|
if (isset($commande->id_production)) { |
|
|
|
|
|
// date de commande |
|
|
|
|
|
$production = Production::find()->where(['id' => $commande->id_production])->one(); |
|
|
|
|
|
if (date('H') >= 20) { |
|
|
|
|
|
$date = date('Y-m-d', strtotime(date('Y-m-d')) + 60 * 60 * 24); |
|
|
|
|
|
} else { |
|
|
|
|
|
$date = date('Y-m-d'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//die($production->date.' '.$date) ; |
|
|
|
|
|
|
|
|
|
|
|
if ($production->date < $date) { |
|
|
|
|
|
$err_date = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// pate déjà pétrie ou non |
|
|
|
|
|
/* if(date('Y-m-d', strtotime(date('Y-m-d')) + 60*60*24*3) > $production->date && date('H', strtotime(date('Y-m-d')) + 60*60*24*3) < 20) { |
|
|
|
|
|
$pate_deja_petrie = true ; |
|
|
|
|
|
} */ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ($commande->validate() && count($produits) && !$err_nb_produits && !$err_date) { |
|
|
|
|
|
// sauvegarde de la commande |
|
|
|
|
|
$commande->save(); |
|
|
|
|
|
|
|
|
|
|
|
// suppression de tous les enregistrements CommandeProduit |
|
|
|
|
|
if (!is_null($commande)) { |
|
|
|
|
|
CommandeProduit::deleteAll(['id_commande' => $commande->id]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// produits dispos |
|
|
|
|
|
$produits_dispos = ProductionProduit::findProduits($production->id); |
|
|
|
|
|
|
|
|
|
|
|
// sauvegarde des produits |
|
|
|
|
|
foreach ($produits as $p) { |
|
|
|
|
|
// le produit doit etre dispo à la vente |
|
|
|
|
|
if (isset($produits_dispos[$p->id]) && $produits_dispos[$p->id]['actif']) { |
|
|
|
|
|
|
|
|
|
|
|
$commande_produit = new CommandeProduit(); |
|
|
|
|
|
$commande_produit->id_commande = $commande->id; |
|
|
|
|
|
$commande_produit->id_produit = $p->id; |
|
|
|
|
|
|
|
|
|
|
|
$commande_produit->prix = $p->prix; |
|
|
|
|
|
|
|
|
|
|
|
$quantite_voulue = (int) $posts['Produit']['produit_' . $p->id]; |
|
|
|
|
|
if ($quantite_voulue > $produits_dispos[$p->id]['quantite_restante'] && !$produits_dispos[$p->id]['vrac']) |
|
|
|
|
|
$quantite_voulue = $produits_dispos[$p->id]['quantite_restante']; |
|
|
|
|
|
|
|
|
|
|
|
$commande_produit->quantite = $quantite_voulue; |
|
|
|
|
|
|
|
|
|
|
|
$commande_produit->save(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
// redirection |
|
|
|
|
|
$this->redirect(Yii::$app->urlManager->createUrl(['commande/index', 'commande_ok' => true, 'pate_deja_petrie' => $pate_deja_petrie])); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
if (!count($produits)) |
|
|
|
|
|
Yii::$app->session->setFlash('error', "Vous n'avez choisi aucun produit"); |
|
|
|
|
|
if ($err_nb_produits) |
|
|
|
|
|
Yii::$app->session->setFlash('error', "Vous ne pouvez pas commander plus de 3 produits"); |
|
|
|
|
|
if ($err_date) |
|
|
|
|
|
Yii::$app->session->setFlash('error', "Vous ne pouvez pas commander pour cette date."); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function actionAnnuler($id) { |
|
|
public function actionAnnuler($id) { |
|
|
|
|
|
|
|
|
$commande = Commande::find()->where(['id'=>$id])->one(); |
|
|
|
|
|
if($commande && Yii::$app->user->id == $commande->id_user) { |
|
|
|
|
|
$commande->delete() ; |
|
|
|
|
|
CommandeProduit::deleteAll(['id_commande'=>$commande->id]) ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$this->redirect(Yii::$app->urlManager->createUrl(['commande/index','annule_ok'=>true])) ; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$commande = Commande::find()->where(['id' => $id])->one(); |
|
|
|
|
|
if ($commande && Yii::$app->user->id == $commande->id_user) { |
|
|
|
|
|
$commande->delete(); |
|
|
|
|
|
CommandeProduit::deleteAll(['id_commande' => $commande->id]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$this->redirect(Yii::$app->urlManager->createUrl(['commande/index', 'annule_ok' => true])); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |