Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

700 lines
26KB

  1. <?php
  2. namespace backend\controllers;
  3. use common\models\ProductionProduit;
  4. use Yii;
  5. use common\models\Production;
  6. use yii\filters\AccessControl;
  7. use yii\web\Controller;
  8. use common\models\LoginForm;
  9. use yii\filters\VerbFilter;
  10. use common\models\Commande;
  11. use common\models\PointVente;
  12. use common\models\Produit;
  13. use common\helpers\CSV;
  14. use common\models\User;
  15. use common\models\CommandeProduit;
  16. class CommandeController extends \yii\web\Controller {
  17. var $enableCsrfValidation = false;
  18. public function behaviors() {
  19. return [
  20. 'access' => [
  21. 'class' => AccessControl::className(),
  22. 'rules' => [
  23. [
  24. 'allow' => true,
  25. 'roles' => ['@'],
  26. 'matchCallback' => function ($rule, $action) {
  27. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  28. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
  29. }
  30. ]
  31. ],
  32. ],
  33. ];
  34. }
  35. public function actionDeleteCommande($date, $id_commande) {
  36. $commande = Commande::find()->where(['id' => $id_commande])->one();
  37. if ($commande) {
  38. $commande->delete();
  39. CommandeProduit::deleteAll(['id_commande' => $id_commande]);
  40. }
  41. $this->redirect(['index', 'date' => $date]);
  42. }
  43. public function gestionFormCommandes($production, $date, $points_vente, $produits, $users) {
  44. if ($date != '') {
  45. // commandes
  46. $commandes = Commande::find()
  47. ->with('commandeProduits', 'user')
  48. ->joinWith('production')
  49. ->where(['production.date' => $date])
  50. ->all();
  51. foreach ($commandes as $c)
  52. $c->init();
  53. foreach ($points_vente as $pv) {
  54. $pv->initCommandes($commandes);
  55. if (isset($_POST['submit_pv']) && $_POST['submit_pv']) {
  56. // modifs
  57. foreach ($pv->commandes as $c) {
  58. // suppression des commande_produit
  59. $commande_produits = CommandeProduit::find()->where(['id_commande' => $c->id])->all();
  60. foreach ($commande_produits as $cp)
  61. $cp->delete();
  62. // création des commande_produit modifiés
  63. foreach ($produits as $p) {
  64. $quantite = Yii::$app->getRequest()->post('produit_' . $c->id . '_' . $p->id, 0);
  65. if ($quantite) {
  66. $commande_produit = new CommandeProduit;
  67. $commande_produit->id_commande = $c->id;
  68. $commande_produit->id_produit = $p->id;
  69. $commande_produit->quantite = $quantite;
  70. $commande_produit->prix = $p->prix;
  71. $commande_produit->save();
  72. }
  73. }
  74. }
  75. // ajout
  76. //$id_client = Yii::$app->getRequest()->post('user_pv_'.$pv->id, 0) ;
  77. $username = Yii::$app->getRequest()->post('username_pv_' . $pv->id, 0);
  78. $date = Yii::$app->getRequest()->post('date_commande_pv_' . $pv->id, 0);
  79. $one_product = false;
  80. foreach ($produits as $p) {
  81. $quantite = Yii::$app->getRequest()->post('produit_pv_' . $pv->id . '_' . $p->id, 0);
  82. if ($quantite) {
  83. $one_product = true;
  84. }
  85. }
  86. if (strlen($username) && $date && $one_product) {
  87. $commande = new Commande;
  88. $commande->id_point_vente = $pv->id;
  89. $commande->id_production = $production->id;
  90. $commande->id_user = 0;
  91. $commande->username = $username;
  92. $tab_date = explode('/', $date);
  93. $commande->date = $tab_date[2] . '-' . $tab_date[1] . '-' . $tab_date[0] . ' 00:00:00';
  94. $commande->save();
  95. foreach ($produits as $p) {
  96. $quantite = Yii::$app->getRequest()->post('produit_pv_' . $pv->id . '_' . $p->id, 0);
  97. if ($quantite) {
  98. $commande_produit = new CommandeProduit;
  99. $commande_produit->id_commande = $commande->id;
  100. $commande_produit->id_produit = $p->id;
  101. $commande_produit->quantite = $quantite;
  102. $commande_produit->prix = $p->prix;
  103. $commande_produit->save();
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. public function actionIndex($date = '', $return_data = false) {
  112. $commandes = [];
  113. // points de vente
  114. $points_vente = PointVente::find()->all();
  115. // produits
  116. $produits = Produit::find()->orderBy('order ASC')->all();
  117. // users
  118. $arr_users = [0 => '--'];
  119. $users = User::find()->orderBy('prenom, nom ASC')->all();
  120. foreach ($users as $u) {
  121. $arr_users[$u->id] = $u->prenom . ' ' . $u->nom;
  122. }
  123. // création du jour de production
  124. $production = null;
  125. if ($date != '') {
  126. $production = Production::find()->where(['date' => $date])->one();
  127. if (!$production) {
  128. $production = new Production;
  129. $production->date = $date;
  130. $production->livraison = 1;
  131. $production->save();
  132. }
  133. }
  134. // gestion des commandes
  135. $this->gestionFormCommandes($production, $date, $points_vente, $produits, $users);
  136. // commandes
  137. $commandes = Commande::find()
  138. ->with('commandeProduits', 'user')
  139. ->joinWith('production')
  140. ->where(['production.date' => $date])
  141. ->orderBy('date ASC')
  142. ->all();
  143. $recettes = 0;
  144. $recettes_pain = 0;
  145. $recettes_vrac = 0;
  146. $recettes_pain_livre = 0;
  147. $poids_pain = 0;
  148. $poids_vrac = 0;
  149. foreach ($commandes as $c) {
  150. $c->init();
  151. $recettes += $c->montant;
  152. $recettes_pain += $c->montant_pain;
  153. $recettes_vrac += $c->montant_vrac;
  154. if ($c->id_point_vente != 1)
  155. $recettes_pain_livre += $c->montant_pain;
  156. $poids_pain += $c->poids_pain;
  157. $poids_vrac += $c->poids_vrac;
  158. }
  159. $recettes = number_format($recettes, 2);
  160. $recettes_pain = number_format($recettes_pain, 2);
  161. $recettes_vrac = number_format($recettes_vrac, 2);
  162. // init commandes point de vente
  163. foreach ($points_vente as $pv) {
  164. $pv->initCommandes($commandes);
  165. }
  166. // gestion produits selec
  167. if (isset($_POST['valider_produit_selec'])) {
  168. if (isset($_POST['Produit'])) {
  169. foreach ($produits as $p) {
  170. $produit_production = ProductionProduit::find()->where(['id_production' => $production->id, 'id_produit' => $p->id])->one();
  171. if (!$produit_production) {
  172. $produit_production = new ProductionProduit();
  173. $produit_production->id_production = $production->id;
  174. $produit_production->id_produit = $p->id;
  175. $produit_production->actif = 0;
  176. if (isset($p->quantite_max))
  177. $produit_production->quantite_max = $p->quantite_max;
  178. $produit_production->save();
  179. }
  180. if (isset($_POST['Produit'][$p->id]['actif'])) {
  181. $produit_production->actif = 1;
  182. } else {
  183. $produit_production->actif = 0;
  184. }
  185. if ((isset($_POST['Produit'][$p->id]['quantite_max']) && $_POST['Produit'][$p->id]['quantite_max'] != '')) {
  186. $produit_production->quantite_max = (int) $_POST['Produit'][$p->id]['quantite_max'];
  187. } else {
  188. if (isset($p->quantite_max) && is_numeric($p->quantite_max) && $p->quantite_max > 0) {
  189. $produit_production->quantite_max = $p->quantite_max;
  190. }
  191. }
  192. $produit_production->save();
  193. }
  194. }
  195. }
  196. // init produits sélectionnés pour cette production
  197. $produits_selec = [];
  198. if ($production) {
  199. $day_production = date('N', strtotime($production->date));
  200. $produits_production = ProductionProduit::find()->where(['id_production' => $production->id])->all();
  201. if (!count($produits_production)) {
  202. foreach ($produits as $p) {
  203. $pp = new ProductionProduit();
  204. $pp->id_production = $production->id;
  205. $pp->id_produit = $p->id;
  206. $pp->actif = 0;
  207. if ($day_production == 1 && $p->lundi)
  208. $pp->actif = 1;
  209. if ($day_production == 2 && $p->mardi)
  210. $pp->actif = 1;
  211. if ($day_production == 3 && $p->mercredi)
  212. $pp->actif = 1;
  213. if ($day_production == 4 && $p->jeudi)
  214. $pp->actif = 1;
  215. if ($day_production == 5 && $p->vendredi)
  216. $pp->actif = 1;
  217. if ($day_production == 6 && $p->samedi)
  218. $pp->actif = 1;
  219. if ($day_production == 7 && $p->dimanche)
  220. $pp->actif = 1;
  221. $pp->quantite_max = $p->quantite_max;
  222. $pp->save();
  223. }
  224. }
  225. // produits selec pour production
  226. $produits_selec = ProductionProduit::findProduits($production->id);
  227. }
  228. // produit en vrac forcément activé
  229. if ($date != '') {
  230. foreach ($produits as $p) {
  231. $produit_production = ProductionProduit::find()->where(['id_production' => $production->id, 'id_produit' => $p->id])->one();
  232. if ($p->vrac) {
  233. if (!$produit_production) {
  234. $produit_production = new ProductionProduit();
  235. $produit_production->id_production = $production->id;
  236. $produit_production->id_produit = $p->id;
  237. $produit_production->quantite_max = 0;
  238. $produit_production->actif = 1;
  239. $produit_production->save();
  240. } else {
  241. $produit_production->actif = 1;
  242. $produit_production->save();
  243. }
  244. }
  245. }
  246. }
  247. // poids total de la production et CA potentiel
  248. $ca_potentiel = 0;
  249. $poids_total = 0;
  250. foreach ($produits_selec as $id_produit_selec => $produit_selec) {
  251. if ($produit_selec['actif']) {
  252. foreach ($produits as $produit) {
  253. if ($produit->id == $id_produit_selec) {
  254. //echo $produit->nom.' : '.$produit_selec['quantite_max'].'<br />' ;
  255. $ca_potentiel += $produit_selec['quantite_max'] * $produit->prix;
  256. $poids_total += $produit_selec['quantite_max'] * $produit->poids;
  257. }
  258. }
  259. }
  260. }
  261. // jours de production
  262. $jours_production = Production::find()->where(['actif' => 1])->all();
  263. $datas = [
  264. 'produits' => $produits,
  265. 'points_vente' => $points_vente,
  266. 'commandes' => $commandes,
  267. 'date' => $date,
  268. 'production' => $production,
  269. 'jours_production' => $jours_production,
  270. 'produits_selec' => $produits_selec,
  271. 'users' => $arr_users,
  272. 'recettes' => $recettes,
  273. 'recettes_pain' => $recettes_pain,
  274. 'recettes_vrac' => $recettes_vrac,
  275. 'recettes_pain_livre' => $recettes_pain_livre,
  276. 'poids_pain' => $poids_pain,
  277. 'poids_vrac' => $poids_vrac,
  278. 'ca_potentiel' => $ca_potentiel,
  279. 'poids_total' => $poids_total,
  280. ];
  281. if ($return_data) {
  282. return $datas;
  283. } else {
  284. return $this->render('index', $datas);
  285. }
  286. }
  287. public function actionDownload($date = '', $id_point_vente = 0, $global = 0) {
  288. // commandes
  289. $commandes = Commande::find()
  290. ->with('commandeProduits', 'user')
  291. ->joinWith('production')
  292. ->where(['production.date' => $date])
  293. ->orderBy('date ASC')
  294. ->all();
  295. foreach ($commandes as $c)
  296. $c->init();
  297. // points de vente
  298. $points_vente = PointVente::find()->all();
  299. foreach ($points_vente as $pv)
  300. $pv->initCommandes($commandes);
  301. // produits
  302. $produits = Produit::find()->orderBy('order ASC')->all();
  303. $production = Production::find()->where('date LIKE \'' . $date . '\'')->one();
  304. $produits_selec = ProductionProduit::findProduits($production->id);
  305. /*
  306. * export global
  307. */
  308. if ($global) {
  309. $data = [];
  310. $filename = 'export_' . $date . '_global';
  311. $num_jour_semaine = date('w', strtotime($date));
  312. $arr_jour_semaine = [0 => 'dimanche', 1 => 'lundi', 2 => 'mardi', 3 => 'mercredi', 4 => 'jeudi', 5 => 'vendredi', 6 => 'samedi'];
  313. $champs_horaires_point_vente = 'horaires_' . $arr_jour_semaine[$num_jour_semaine];
  314. // header
  315. /* $line = [''] ;
  316. foreach($produits as $p) {
  317. if(isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  318. $line[] = $p->getLibelleAdmin() ;
  319. }
  320. }
  321. $data[] = $line ; */
  322. // par point de vente
  323. foreach ($points_vente as $pv) {
  324. if (count($pv->commandes) && strlen($pv->$champs_horaires_point_vente)) {
  325. //$data[] = [$pv->nom] ;
  326. $line = [$pv->nom, 'Produits', 'Montant', 'Commentaire'];
  327. /* foreach($produits as $p) {
  328. if(isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  329. $line[] = $p->getLibelleAdmin() ;
  330. }
  331. } */
  332. $data[] = $line;
  333. $res = $this->contentPointVenteCSV($date, $produits, $points_vente, $pv->id);
  334. foreach ($res['data'] as $line) {
  335. $data[] = $line;
  336. }
  337. }
  338. if (count($pv->commandes) && strlen($pv->$champs_horaires_point_vente)) {
  339. $line = ['Total pain'];
  340. $str_produits = '';
  341. foreach ($produits as $p) {
  342. if (!$p->vrac && isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  343. $quantite = Commande::getQuantiteProduit($p->id, $pv->commandes);
  344. $str_quantite = '';
  345. if ($quantite) {
  346. $str_quantite = $quantite;
  347. $str_produits .= $str_quantite . $p->diminutif . ', ';
  348. }
  349. }
  350. }
  351. $line[] = substr($str_produits, 0, strlen($str_produits) - 2);
  352. $line[] = number_format($pv->recettes_pain, 2) . ' €';
  353. $data[] = $line;
  354. $line = ['Total vrac'];
  355. $str_produits = '';
  356. foreach ($produits as $p) {
  357. if ($p->vrac && isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  358. $quantite = Commande::getQuantiteProduit($p->id, $pv->commandes);
  359. $str_quantite = '';
  360. if ($quantite) {
  361. $str_quantite = $quantite;
  362. $str_produits .= $str_quantite . $p->diminutif . ', ';
  363. }
  364. }
  365. }
  366. $line[] = substr($str_produits, 0, strlen($str_produits) - 2);
  367. $line[] = number_format($pv->recettes_vrac, 2) . ' €';
  368. $data[] = $line;
  369. $data[] = [];
  370. }
  371. }
  372. // récap
  373. //$line = ['Totaux'] ;
  374. // pain
  375. $line = ['Total pain'];
  376. $str_produits = '';
  377. foreach ($produits as $p) {
  378. if (!$p->vrac && isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  379. $quantite = Commande::getQuantiteProduit($p->id, $commandes);
  380. $str_quantite = '';
  381. if ($quantite) {
  382. $str_quantite = $quantite;
  383. $str_produits .= $str_quantite . '' . $p->diminutif . ', ';
  384. }
  385. }
  386. }
  387. $line[] = substr($str_produits, 0, strlen($str_produits) - 2);
  388. $data[] = $line;
  389. // vrac
  390. $line = ['Total vrac'];
  391. $str_produits = '';
  392. foreach ($produits as $p) {
  393. if ($p->vrac && isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  394. $quantite = Commande::getQuantiteProduit($p->id, $commandes);
  395. $str_quantite = '';
  396. if ($quantite) {
  397. $str_quantite = $quantite;
  398. $str_produits .= $str_quantite . '' . $p->diminutif . ', ';
  399. }
  400. }
  401. }
  402. $line[] = substr($str_produits, 0, strlen($str_produits) - 2);
  403. $data[] = $line;
  404. $infos = $this->actionIndex($date, true);
  405. // $data[] = [] ;
  406. /* $data[] = [
  407. 'CA potentiel boutique',
  408. number_format($infos['ca_potentiel'] - $infos['recettes_pain_livre'], 2).' €',
  409. ] ; */
  410. /* $res = $this->contentRecapCSV($date, $produits, $points_vente, $commandes) ;
  411. $data[] = ['Récapitulatif global'] ;
  412. foreach($res['data'] as $line) {
  413. $data[] = $line ;
  414. } */
  415. CSV::downloadSendHeaders($filename . '.csv');
  416. echo CSV::array2csv($data);
  417. die();
  418. }
  419. /*
  420. * export individuel
  421. */ else {
  422. if ($commandes && count($commandes)) {
  423. $data = [];
  424. // par point de vente
  425. if ($id_point_vente) {
  426. $res = $this->contentPointVenteCSV($date, $produits, $points_vente, $id_point_vente);
  427. $data = $res['data'];
  428. $filename = $res['filename'];
  429. }
  430. // récapitulatif
  431. else {
  432. $res = $this->contentRecapCSV($date, $produits, $points_vente, $commandes);
  433. $filename = 'recapitulatif_' . $date;
  434. $data = $res['data'];
  435. }
  436. CSV::downloadSendHeaders($filename . '.csv');
  437. echo CSV::array2csv($data);
  438. die();
  439. }
  440. }
  441. }
  442. public function contentRecapCSV($date, $produits, $points_vente, $commandes) {
  443. $data = [];
  444. $filename = 'recapitulatif_' . $date;
  445. $production = Production::find()->where('date LIKE \'' . $date . '\'')->one();
  446. $produits_selec = ProductionProduit::findProduits($production->id);
  447. // head
  448. $data[0] = ['Lieu'];
  449. foreach ($produits as $p) {
  450. if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  451. $data[0][] = $p->description;
  452. }
  453. }
  454. $num_jour_semaine = date('w', strtotime($date));
  455. $arr_jour_semaine = [0 => 'dimanche', 1 => 'lundi', 2 => 'mardi', 3 => 'mercredi', 4 => 'jeudi', 5 => 'vendredi', 6 => 'samedi'];
  456. $champs_horaires_point_vente = 'horaires_' . $arr_jour_semaine[$num_jour_semaine];
  457. // datas
  458. foreach ($points_vente as $pv) {
  459. if (count($pv->commandes) && strlen($pv->$champs_horaires_point_vente)) {
  460. $data_add = [$pv->nom];
  461. foreach ($produits as $p) {
  462. if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  463. $data_add[] = Commande::getQuantiteProduit($p->id, $pv->commandes);
  464. }
  465. }
  466. $data[] = $data_add;
  467. }
  468. }
  469. $data_add = ['Total'];
  470. foreach ($produits as $p) {
  471. if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  472. $data_add[] = Commande::getQuantiteProduit($p->id, $commandes);
  473. }
  474. }
  475. $data[] = $data_add;
  476. return [
  477. 'data' => $data,
  478. 'filename' => $filename
  479. ];
  480. }
  481. public function contentPointVenteCSV($date, $produits, $points_vente, $id_point_vente) {
  482. $data = [];
  483. $production = Production::find()->where('date LIKE \'' . $date . '\'')->one();
  484. $produits_selec = ProductionProduit::findProduits($production->id);
  485. // head
  486. /* $data[0] = ['Client', 'Date commande'] ;
  487. foreach($produits as $p) {
  488. if(isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  489. $data[0][] = $p->description ;
  490. }
  491. } */
  492. // datas
  493. foreach ($points_vente as $pv) {
  494. if ($pv->id == $id_point_vente) {
  495. $filename = 'export_' . $date . '_' . strtolower(str_replace(' ', '-', $pv->nom));
  496. foreach ($pv->commandes as $c) {
  497. $str_user = '';
  498. // username
  499. if ($c->user) {
  500. $str_user = $c->user->prenom . " " . $c->user->nom; //.' - '.date('d/m', strtotime($c->date)) ;
  501. } else {
  502. $str_user = $c->username; //.' - '.date('d/m', strtotime($c->date)) ;
  503. }
  504. // téléphone
  505. if (strlen($c->user->telephone)) {
  506. $str_user .= ' (' . $c->user->telephone . ')';
  507. }
  508. $data_add = [$str_user];
  509. // produits
  510. $str_produits = '';
  511. foreach ($produits as $p) {
  512. if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  513. $add = false;
  514. foreach ($c->commandeProduits as $cp) {
  515. if ($p->id == $cp->id_produit) {
  516. $str_produits .= $cp->quantite . '' . $p->diminutif . ', ';
  517. $add = true;
  518. }
  519. }
  520. }
  521. }
  522. $data_add[] = substr($str_produits, 0, strlen($str_produits) - 2);
  523. $data_add[] = number_format($c->montant, 2) . ' €';
  524. $data_add[] = $c->commentaire;
  525. $data[] = $data_add;
  526. }
  527. }
  528. }
  529. return [
  530. 'data' => $data,
  531. 'filename' => $filename
  532. ];
  533. }
  534. public function actionChangeState($date, $actif) {
  535. // changement état
  536. $production = Production::find()->where(['date' => $date])->one();
  537. $production->actif = $actif;
  538. $production->save();
  539. // envoi emails aux personnes qui souhaitent être informées
  540. if ($actif) {
  541. $jour = date('N', strtotime($date));
  542. $arr_jour_semaine = [1 => 'lundi', 2 => 'mardi', 3 => 'mercredi', 4 => 'jeudi', 5 => 'vendredi', 6 => 'samedi', 7 => 'dimanche'];
  543. $users = User::find()->where('mail_prod_' . $arr_jour_semaine[$jour] . ' = 1')->all();
  544. //$str_date = strtolower(date('l j F', strtotime($date))) ;
  545. setlocale(LC_ALL, "fr_FR");
  546. $str_date = strtolower(strftime('%A%e %B', strtotime($date)));
  547. $str_date2 = date('d/m', strtotime($date));
  548. foreach ($users as $u) {
  549. if (!$u->no_mail) {
  550. Yii::$app->mailer->compose()
  551. ->setTo($u->email)
  552. ->setFrom(['matthieu@lechatdesnoisettes.com' => 'Le Chat des Noisettes'])
  553. ->setSubject('[Le Chat des Noisettes] Production de pain du ' . $str_date2)
  554. ->setTextBody(
  555. "Bonjour,
  556. Une production de pain est prévue le " . $str_date . ".
  557. Cordialement,
  558. Matthieu
  559. PS : Si vous ne souhaitez plus recevoir ces emails, rendez-vous dans votre compte sur www.lechatdesnoisettes.com.
  560. ")->send();
  561. }
  562. }
  563. }
  564. $this->redirect(['index', 'date' => $date]);
  565. }
  566. public function actionChangeLivraison($date, $livraison) {
  567. $production = Production::find()->where(['date' => $date])->one();
  568. $production->livraison = $livraison;
  569. $production->save();
  570. $this->redirect(['index', 'date' => $date]);
  571. }
  572. }