Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

522 lines
19KB

  1. <?php
  2. namespace frontend\controllers;
  3. use common\models\ProductionProduit;
  4. use common\models\CommandeProduit;
  5. use Yii;
  6. use yii\filters\AccessControl;
  7. use common\models\Commande;
  8. use common\models\PointVente;
  9. use common\models\Production;
  10. use common\models\Produit;
  11. use common\models\Etablissement;
  12. use common\helpers\Departements;
  13. use yii\helpers\Html;
  14. use frontend\models\AddEtablissementForm;
  15. use common\models\UserEtablissement;
  16. use common\models\CreditHistorique ;
  17. use yii\web\NotFoundHttpException ;
  18. use common\models\ProductionPointVente ;
  19. class CommandeController extends \yii\web\Controller {
  20. public function behaviors() {
  21. return [
  22. 'access' => [
  23. 'class' => AccessControl::className(),
  24. 'rules' => [
  25. [
  26. 'allow' => true,
  27. 'roles' => ['@'],
  28. ]
  29. ],
  30. ],
  31. ];
  32. }
  33. public function actionInfosProduction($date) {
  34. $production = Production::find()->where(['date' => $date])->one();
  35. if ($production) {
  36. $arr = [] ;
  37. $produits_dispos = ProductionProduit::findProduits($production->id);
  38. $arr['produits_dispos'] = $produits_dispos ;
  39. $points_vente = PointVente::find()
  40. ->joinWith(['productionPointVente'=> function($q) use ($production) {
  41. $q->where(['id_production' => $production->id]) ;
  42. } ])
  43. ->where([
  44. 'id_etablissement' => Yii::$app->user->identity->id_etablissement,
  45. ])
  46. ->all();
  47. $arr['points_vente'] = [] ;
  48. foreach($points_vente as $pv)
  49. {
  50. if(isset($pv->productionPointVente) &&
  51. isset($pv->productionPointVente[0]))
  52. {
  53. $arr['points_vente'][$pv->id] = $pv->productionPointVente[0]->livraison ;
  54. }
  55. else {
  56. $arr['points_vente'][$pv->id] = false ;
  57. }
  58. }
  59. return json_encode($arr);
  60. }
  61. return json_encode([]);
  62. }
  63. public static function initForm($commande = null) {
  64. // etablissements
  65. $etablissements = Yii::$app->user->identity->getEtablissementsFavoris() ;
  66. $id_etablissement = Yii::$app->request->get('id_etablissement', 0) ;
  67. $etablissement_paiement_ok = false ;
  68. if($id_etablissement)
  69. {
  70. $etablissement = Etablissement::findOne($id_etablissement);
  71. if($etablissement->etatPaiement() == Etablissement::PAIEMENT_OK || $etablissement->etatPaiement() == Etablissement::PAIEMENT_ESSAI)
  72. {
  73. $etablissement_paiement_ok = true ;
  74. }
  75. }
  76. // points de vente
  77. $points_vente = PointVente::find()
  78. ->with('pointVenteUser')
  79. ->where(['id_etablissement'=>$id_etablissement])
  80. ->andWhere('acces_restreint = 0 OR (acces_restreint = 1 AND (SELECT COUNT(*) FROM point_vente_user WHERE point_vente.id = point_vente_user.id_point_vente AND point_vente_user.id_user = :id_user) > 0)')
  81. ->params([':id_user' => Yii::$app->user->identity->id])
  82. ->all();
  83. $arr_points_vente = $points_vente;
  84. // jours de production
  85. $heure_limite = 20 ;
  86. $date = date('Y-m-d') ;
  87. if(isset($etablissement))
  88. {
  89. $heure_limite = $etablissement->heure_limite_commande ;
  90. if (date('H') >= $heure_limite) {
  91. $date = date('Y-m-d', strtotime(date('Y-m-d')) + ($etablissement->delai_commande)*(24*60*60) );
  92. } else {
  93. $date = date('Y-m-d', strtotime(date('Y-m-d')) + ($etablissement->delai_commande - 1)*(24*60*60));
  94. }
  95. }
  96. $jours_production = Production::find()
  97. ->where(['actif' => 1])
  98. ->andWhere('date > :date')
  99. ->andWhere(['id_etablissement'=>$id_etablissement])
  100. ->addParams([':date' => $date])
  101. ->all();
  102. $arr_jours_production = array('' => '--');
  103. foreach ($jours_production as $j)
  104. $arr_jours_production[$j->id] = date('d/m/Y', strtotime($j->date));
  105. // produits
  106. $produits = Produit::find()
  107. ->where(['actif' => 1, 'id_etablissement' => $id_etablissement])
  108. ->andWhere('vrac IS NULL OR vrac = 0')
  109. ->orderBy('order ASC')->all();
  110. $arr_produits = array();
  111. foreach ($produits as $p)
  112. $arr_produits[] = $p;
  113. // produits vrac
  114. $produits_vrac = Produit::find()->where(['actif' => 1, 'vrac' => 1])->orderBy('order ASC')->all();
  115. // produits selec
  116. $posts = Yii::$app->request->post();
  117. $produits_selec = [];
  118. if (isset($posts['Produit'])) {
  119. foreach ($posts['Produit'] as $key => $quantity) {
  120. $key = (int) str_replace('produit_', '', $key);
  121. $p = Produit::find()->where(['id' => $key])->one();
  122. if ($p && $quantity)
  123. $produits_selec[$p->id] = (int) $quantity;
  124. }
  125. }
  126. elseif (!is_null($commande)) {
  127. $produits_commande = CommandeProduit::find()->where(['id_commande' => $commande->id])->all();
  128. foreach ($produits_commande as $pc) {
  129. $produits_selec[$pc->id_produit] = (int) $pc->quantite;
  130. }
  131. }
  132. $produits_dispos = [];
  133. $production = null;
  134. if (!is_null($commande) && $commande->id_production) {
  135. $produits_dispos = ProductionProduit::findProduits($commande->id_production);
  136. $production = Production::find()->where(['id' => $commande->id_production])->one();
  137. }
  138. $commandes = Commande::find()
  139. ->where(['id_user' => Yii::$app->user->identity->id])
  140. ->all();
  141. if($id_etablissement)
  142. {
  143. $user_etablissement = UserEtablissement::find()
  144. ->where([
  145. 'id_etablissement' => $id_etablissement,
  146. 'id_user' => Yii::$app->user->identity->id
  147. ])
  148. ->one() ;
  149. $credit = $user_etablissement->credit ;
  150. }
  151. else {
  152. $credit = 0 ;
  153. }
  154. return [
  155. 'points_vente' => $arr_points_vente,
  156. 'jours_production' => $arr_jours_production,
  157. 'produits' => $produits,
  158. 'produits_selec' => $produits_selec,
  159. 'produits_dispos' => $produits_dispos,
  160. 'production' => $production,
  161. 'commandes_en_cours' => $commandes,
  162. 'produits_vrac' => $produits_vrac,
  163. 'etablissements' => $etablissements,
  164. 'id_etablissement' => $id_etablissement,
  165. 'etablissement_paiement_ok' => $etablissement_paiement_ok,
  166. 'credit' => $credit
  167. ];
  168. }
  169. public function actionIndex() {
  170. $model_form_etablissement = new AddEtablissementForm() ;
  171. if($model_form_etablissement->load(Yii::$app->request->post())
  172. && $model_form_etablissement->validate())
  173. {
  174. $model_form_etablissement->add() ;
  175. $model_form_etablissement->code = '' ;
  176. }
  177. // liste des etablissements
  178. $etablissements = Yii::$app->user->identity->getEtablissementsFavoris();
  179. // liste des boulangeries disponibles
  180. $arr_etablissements = Etablissement::getEtablissementsPopulateDropdown() ;
  181. $data_etablissements_dispos = $arr_etablissements['data'] ;
  182. $options_etablissements_dispos = $arr_etablissements['options'] ;
  183. // liste des commandes
  184. $commandes = Commande::find()
  185. ->with('commandeProduits', 'pointVente', 'creditHistorique')
  186. ->joinWith('production','production.etablissement')
  187. ->where(['id_user' => Yii::$app->user->id])
  188. //->andWhere('production.date < '.)
  189. ->orderBy('production.date DESC')
  190. ->limit(40)
  191. ->all();
  192. // initilisation commandes
  193. foreach ($commandes as $c)
  194. $c->init();
  195. return $this->render('index', [
  196. 'commandes' => $commandes,
  197. 'commande_ok' => Yii::$app->getRequest()->get('commande_ok', false),
  198. 'annule_ok' => Yii::$app->getRequest()->get('annule_ok', false),
  199. 'pate_deja_petrie' => Yii::$app->getRequest()->get('pate_deja_petrie', false),
  200. 'etablissements' => $etablissements,
  201. 'model_form_etablissement' => $model_form_etablissement,
  202. 'data_etablissements_dispos' => $data_etablissements_dispos,
  203. 'options_etablissements_dispos' => $options_etablissements_dispos,
  204. ]);
  205. }
  206. public function actionRemoveEtablissement($id = 0)
  207. {
  208. $user_etablissement = UserEtablissement::find()
  209. ->where(['id_etablissement'=>$id, 'id_user' => Yii::$app->user->identity->id])
  210. ->one() ;
  211. $user_etablissement->actif = 0 ;
  212. $user_etablissement->save() ;
  213. $this->redirect(['commande/index']) ;
  214. }
  215. public function actionCreate($id_etablissement = 0) {
  216. $commande = new Commande;
  217. $posts = Yii::$app->request->post();
  218. if($id_etablissement)
  219. $this->_verifEtablissementActif($id_etablissement) ;
  220. if ($commande->load($posts)) {
  221. $commande = Commande::find()->where('id_production = ' . $posts['Commande']['id_production'])->andWhere('id_user = ' . Yii::$app->user->id)->one();
  222. if (!$commande) {
  223. $commande = new Commande;
  224. $commande->load(Yii::$app->request->post());
  225. $commande->id_user = Yii::$app->user->id;
  226. $commande->date = date('Y-m-d H:i:s');
  227. $commande->type = Commande::TYPE_USER ;
  228. }
  229. $this->gestionForm($commande);
  230. }
  231. return $this->render('create', array_merge(self::initForm($commande), [
  232. 'model' => $commande,
  233. ]));
  234. }
  235. public function actionUpdate($id) {
  236. $commande = Commande::find()
  237. ->with('production')
  238. ->where(['id' => $id])
  239. ->one();
  240. $this->_verifEtablissementActif($commande->production->id_etablissement) ;
  241. if ($commande && $commande->load(Yii::$app->request->post()))
  242. {
  243. $commande->date_update = date('Y-m-d H:i:s');
  244. $this->gestionForm($commande);
  245. }
  246. return $this->render('update', array_merge(self::initForm($commande), [
  247. 'model' => $commande,
  248. 'commande_introuvable' => !$commande,
  249. ]));
  250. }
  251. public function _verifEtablissementActif($id_etablissement)
  252. {
  253. $etablissement = Etablissement::findOne($id_etablissement) ;
  254. if($etablissement && !$etablissement->actif)
  255. {
  256. throw new NotFoundHttpException('Cet établissement est actuellement hors ligne.');
  257. }
  258. }
  259. public function gestionForm($commande) {
  260. $posts = Yii::$app->request->post();
  261. $produits = array();
  262. $quantite_totale = 0;
  263. foreach ($posts['Produit'] as $key => $quantity) {
  264. $key = (int) str_replace('produit_', '', $key);
  265. $p = Produit::find()->where(['id' => $key])->one();
  266. $quantite_totale += $quantity;
  267. if ($p && $quantity)
  268. $produits[] = $p;
  269. }
  270. // nombre de produits
  271. $err_nb_produits = false;
  272. if (!Yii::$app->user->identity->confiance && $quantite_totale > 3) {
  273. $err_nb_produits = true;
  274. }
  275. // date
  276. $err_date = false;
  277. $pate_deja_petrie = false;
  278. if (isset($commande->id_production)) {
  279. // date de commande
  280. $production = Production::find()->where(['id' => $commande->id_production])->one();
  281. if (date('H') >= 20) {
  282. $date = date('Y-m-d', strtotime(date('Y-m-d')) + 60 * 60 * 24);
  283. } else {
  284. $date = date('Y-m-d');
  285. }
  286. if ($production->date < $date) {
  287. $err_date = true;
  288. }
  289. }
  290. // point de vente
  291. $err_point_vente = false ;
  292. if(isset($production) && $production)
  293. {
  294. $ppv = ProductionPointVente::find()
  295. ->where([
  296. 'id_production' => $production->id,
  297. 'id_point_vente' => $posts['Commande']['id_point_vente']
  298. ])
  299. ->one() ;
  300. if(!$ppv || !$ppv->livraison)
  301. {
  302. $err_point_vente = true ;
  303. }
  304. $point_vente = PointVente::findOne($posts['Commande']['id_point_vente']) ;
  305. if($point_vente)
  306. {
  307. if(strlen($point_vente->code) && !$point_vente->verifCode($posts['code_point_vente_'.$point_vente->id]))
  308. {
  309. $err_point_vente = true ;
  310. }
  311. }
  312. else {
  313. $err_point_vente = true ;
  314. }
  315. }
  316. if ($commande->validate() && count($produits) && !$err_nb_produits && !$err_date && !$err_point_vente) {
  317. // gestion point de vente
  318. $pv = PointVente::find()
  319. ->with('pointVenteUser')
  320. ->where(['id' => $commande->id_point_vente])
  321. ->one() ;
  322. if($pv && strlen($pv->getCommentaire()))
  323. $commande->commentaire_point_vente = $pv->getCommentaire() ;
  324. else
  325. $commande->commentaire_point_vente = '' ;
  326. // sauvegarde de la commande
  327. $commande->save();
  328. // suppression de tous les enregistrements CommandeProduit
  329. if (!is_null($commande)) {
  330. CommandeProduit::deleteAll(['id_commande' => $commande->id]);
  331. }
  332. // produits dispos
  333. $produits_dispos = ProductionProduit::findProduits($production->id);
  334. // sauvegarde des produits
  335. foreach ($produits as $p) {
  336. // le produit doit etre dispo à la vente
  337. if (isset($produits_dispos[$p->id]) && $produits_dispos[$p->id]['actif']) {
  338. $commande_produit = new CommandeProduit();
  339. $commande_produit->id_commande = $commande->id;
  340. $commande_produit->id_produit = $p->id;
  341. $commande_produit->prix = $p->prix;
  342. $quantite_voulue = (int) $posts['Produit']['produit_' . $p->id];
  343. if ($produits_dispos[$p->id]['quantite_max'] && $quantite_voulue > $produits_dispos[$p->id]['quantite_restante'])
  344. $quantite_voulue = $produits_dispos[$p->id]['quantite_restante'];
  345. $commande_produit->quantite = $quantite_voulue;
  346. $commande_produit->save();
  347. }
  348. }
  349. // credit pain
  350. $credit_pain = isset($posts['credit_pain']) && $posts['credit_pain'] ;
  351. if($credit_pain && ($pv->credit_pain || $commande->getMontantPaye()))
  352. {
  353. $commande = Commande::find()
  354. ->with('commandeProduits')
  355. ->where(['id' => $commande->id])
  356. ->one() ;
  357. $commande->init() ;
  358. $montant_paye = $commande->getMontantPaye() ;
  359. // à payer
  360. if($commande->getStatutPaiement() == Commande::STATUT_IMPAYEE)
  361. {
  362. $montant_payer = $commande->montant - $montant_paye ;
  363. $credit = Yii::$app->user->identity->getCredit($production->id_etablissement) ;
  364. if($montant_payer > $credit)
  365. {
  366. $montant_payer = $credit ;
  367. }
  368. $commande->creditHistorique(
  369. CreditHistorique::TYPE_PAIEMENT,
  370. $montant_payer,
  371. $production->id_etablissement,
  372. Yii::$app->user->identity->id
  373. ) ;
  374. }
  375. // surplus à rembourser
  376. elseif($commande->getStatutPaiement() == Commande::STATUT_SURPLUS)
  377. {
  378. $montant_rembourser = $montant_paye - $commande->montant ;
  379. $commande->creditHistorique(
  380. CreditHistorique::TYPE_REMBOURSEMENT,
  381. $montant_rembourser,
  382. $production->id_etablissement,
  383. Yii::$app->user->identity->id
  384. ) ;
  385. }
  386. }
  387. // redirection
  388. $this->redirect(Yii::$app->urlManager->createUrl(['commande/index', 'commande_ok' => true, 'pate_deja_petrie' => $pate_deja_petrie]));
  389. }
  390. else {
  391. if (!count($produits))
  392. Yii::$app->session->setFlash('error', "Vous n'avez choisi aucun produit");
  393. if ($err_nb_produits)
  394. Yii::$app->session->setFlash('error', "Vous ne pouvez pas commander plus de 3 produits");
  395. if ($err_date)
  396. Yii::$app->session->setFlash('error', "Vous ne pouvez pas commander pour cette date.");
  397. if($err_point_vente)
  398. Yii::$app->session->setFlash('error', "Point de vente invalide.");
  399. }
  400. }
  401. public function actionAnnuler($id) {
  402. $commande = Commande::find()
  403. ->with('production','creditHistorique','commandeProduits')
  404. ->where(['id' => $id])
  405. ->one();
  406. $commande->init() ;
  407. if ($commande && Yii::$app->user->id == $commande->id_user) {
  408. // remboursement
  409. if($commande->getMontantPaye())
  410. {
  411. $commande->creditHistorique(
  412. CreditHistorique::TYPE_REMBOURSEMENT,
  413. $commande->getMontantPaye(),
  414. $commande->production->id_etablissement,
  415. Yii::$app->user->identity->id
  416. ) ;
  417. }
  418. // delete
  419. $commande->delete();
  420. CommandeProduit::deleteAll(['id_commande' => $commande->id]);
  421. }
  422. $this->redirect(Yii::$app->urlManager->createUrl(['commande/index', 'annule_ok' => true]));
  423. }
  424. public function actionVerifCodePointVente($id_point_vente, $code)
  425. {
  426. $point_vente = PointVente::findOne($id_point_vente) ;
  427. if($point_vente)
  428. {
  429. if($point_vente->verifCode($code))
  430. {
  431. return true ;
  432. }
  433. }
  434. return false ;
  435. }
  436. }