You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1115 line
41KB

  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\CommandeProduit;
  12. use common\models\PointVente;
  13. use common\models\Produit;
  14. use common\helpers\CSV;
  15. use common\models\User;
  16. use kartik\mpdf\Pdf;
  17. use common\models\CommandeAutoForm ;
  18. use common\models\CommandeAuto ;
  19. class CommandeController extends BackendController {
  20. var $enableCsrfValidation = false;
  21. public function behaviors() {
  22. return [
  23. 'access' => [
  24. 'class' => AccessControl::className(),
  25. 'rules' => [
  26. [
  27. 'actions' => ['report-cron'],
  28. 'allow' => true,
  29. 'roles' => ['?']
  30. ],
  31. [
  32. 'allow' => true,
  33. 'roles' => ['@'],
  34. 'matchCallback' => function ($rule, $action) {
  35. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  36. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
  37. }
  38. ]
  39. ],
  40. ],
  41. ];
  42. }
  43. public function actionReportCron($date = '', $save = false, $id_etablissement = 0, $key = '')
  44. {
  45. if($key == '64ac0bdab7e9f5e48c4d991ec5201d57')
  46. {
  47. $this->actionReport($date, $save, $id_etablissement) ;
  48. }
  49. }
  50. public function actionReport($date = '', $save = false, $id_etablissement = 0)
  51. {
  52. if(!Yii::$app->user->isGuest)
  53. $id_etablissement = Yii::$app->user->identity->id_etablissement ;
  54. $commandes = Commande::find()
  55. ->with('commandeProduits', 'user')
  56. ->joinWith('production')
  57. ->where(['production.date' => $date])
  58. ->andWhere(['production.id_etablissement' => $id_etablissement])
  59. ->orderBy('date ASC')
  60. ->all();
  61. foreach ($commandes as $c)
  62. $c->init();
  63. $production = Production::find()
  64. ->where('date LIKE \'' . $date . '\'')
  65. ->one();
  66. if($production)
  67. {
  68. $produits_selec = ProductionProduit::findProduits($production->id);
  69. $points_vente = PointVente::find()->all();
  70. foreach ($points_vente as $pv)
  71. $pv->initCommandes($commandes);
  72. // produits
  73. $produits = Produit::find()
  74. ->where(['id_etablissement' => $id_etablissement])
  75. ->orderBy('order ASC')
  76. ->all();
  77. // get your HTML raw content without any layouts or scripts
  78. $content = $this->renderPartial('report',[
  79. 'production' => $production,
  80. 'produits_selec' => $produits_selec,
  81. 'points_vente' => $points_vente,
  82. 'date' => $date,
  83. 'produits' => $produits
  84. ]);
  85. $date_str = date('d/m/Y',strtotime($date)) ;
  86. if($save)
  87. {
  88. $destination = Pdf::DEST_FILE ;
  89. }
  90. else {
  91. $destination = Pdf::DEST_BROWSER ;
  92. }
  93. $pdf = new Pdf([
  94. // set to use core fonts only
  95. 'mode' => Pdf::MODE_UTF8,
  96. // A4 paper format
  97. 'format' => Pdf::FORMAT_A4,
  98. // portrait orientation
  99. 'orientation' => Pdf::ORIENT_LANDSCAPE,
  100. // stream to browser inline
  101. 'destination' => $destination,
  102. 'filename' => Yii::getAlias('@app/web/pdf/Commandes-'.$date.'-'.$id_etablissement.'.pdf'),
  103. // your html content input
  104. 'content' => $content,
  105. // format content from your own css file if needed or use the
  106. // enhanced bootstrap css built by Krajee for mPDF formatting
  107. //'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
  108. // any css to be embedded if required
  109. //'cssInline' => '.kv-heading-1{font-size:18px}',
  110. // set mPDF properties on the fly
  111. //'options' => ['title' => 'Krajee Report Title'],
  112. // call mPDF methods on the fly
  113. 'methods' => [
  114. 'SetHeader'=>['Commandes du '.$date_str],
  115. 'SetFooter'=>['{PAGENO}'],
  116. ]
  117. ]);
  118. // return the pdf output as per the destination setting
  119. return $pdf->render();
  120. }
  121. }
  122. public function actionDeleteCommande($date, $id_commande) {
  123. $commande = Commande::find()->where(['id' => $id_commande])->one();
  124. if ($commande) {
  125. $commande->delete();
  126. CommandeProduit::deleteAll(['id_commande' => $id_commande]);
  127. }
  128. $this->redirect(['index', 'date' => $date]);
  129. }
  130. public function gestionFormCommandes($production, $date, $points_vente, $produits, $users) {
  131. if ($date != '') {
  132. // commandes
  133. $commandes = Commande::find()
  134. ->with('commandeProduits', 'user')
  135. ->joinWith('production')
  136. ->where(['production.date' => $date])
  137. ->andWhere(['production.id_etablissement' => Yii::$app->user->identity->id_etablissement])
  138. ->all();
  139. foreach ($commandes as $c)
  140. $c->init();
  141. foreach ($points_vente as $pv) {
  142. $pv->initCommandes($commandes);
  143. if (isset($_POST['submit_pv']) && $_POST['submit_pv']) {
  144. // modifs
  145. foreach ($pv->commandes as $c) {
  146. // suppression des commande_produit
  147. $commande_produits = CommandeProduit::find()->where(['id_commande' => $c->id])->all();
  148. foreach ($commande_produits as $cp)
  149. $cp->delete();
  150. // création des commande_produit modifiés
  151. foreach ($produits as $p) {
  152. $quantite = Yii::$app->getRequest()->post('produit_' . $c->id . '_' . $p->id, 0);
  153. if ($quantite) {
  154. $commande_produit = new CommandeProduit;
  155. $commande_produit->id_commande = $c->id;
  156. $commande_produit->id_produit = $p->id;
  157. $commande_produit->quantite = $quantite;
  158. $commande_produit->prix = $p->prix;
  159. $commande_produit->save();
  160. }
  161. }
  162. }
  163. // ajout
  164. //$id_client = Yii::$app->getRequest()->post('user_pv_'.$pv->id, 0) ;
  165. $username = Yii::$app->getRequest()->post('username_pv_' . $pv->id, 0);
  166. $date = Yii::$app->getRequest()->post('date_commande_pv_' . $pv->id, 0);
  167. $one_product = false;
  168. foreach ($produits as $p) {
  169. $quantite = Yii::$app->getRequest()->post('produit_pv_' . $pv->id . '_' . $p->id, 0);
  170. if ($quantite) {
  171. $one_product = true;
  172. }
  173. }
  174. if (strlen($username) && $date && $one_product) {
  175. $commande = new Commande;
  176. $commande->id_point_vente = $pv->id;
  177. $commande->id_production = $production->id;
  178. $commande->id_user = 0;
  179. $commande->username = $username;
  180. $tab_date = explode('/', $date);
  181. $commande->date = $tab_date[2] . '-' . $tab_date[1] . '-' . $tab_date[0] . ' 00:00:00';
  182. $commande->save();
  183. foreach ($produits as $p) {
  184. $quantite = Yii::$app->getRequest()->post('produit_pv_' . $pv->id . '_' . $p->id, 0);
  185. if ($quantite) {
  186. $commande_produit = new CommandeProduit;
  187. $commande_produit->id_commande = $commande->id;
  188. $commande_produit->id_produit = $p->id;
  189. $commande_produit->quantite = $quantite;
  190. $commande_produit->prix = $p->prix;
  191. $commande_produit->save();
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. public function actionIndex($date = '', $return_data = false) {
  200. $commandes = [];
  201. // points de vente
  202. $points_vente = PointVente::find()
  203. ->where(['id_etablissement' => Yii::$app->user->identity->id_etablissement])
  204. ->all();
  205. // users
  206. $arr_users = [0 => '--'];
  207. $users = User::find()->orderBy('prenom, nom ASC')->all();
  208. foreach ($users as $u) {
  209. $arr_users[$u->id] = $u->prenom . ' ' . $u->nom;
  210. }
  211. // création du jour de production
  212. $production = null;
  213. if ($date != '') {
  214. $production = Production::find()
  215. ->where(['date' => $date])
  216. ->andWhere(['id_etablissement'=>Yii::$app->user->identity->id_etablissement])
  217. ->one();
  218. if (!$production) {
  219. $production = new Production;
  220. $production->date = $date;
  221. $production->livraison = 1;
  222. $production->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  223. $production->save();
  224. }
  225. }
  226. // produits
  227. $produits = Produit::getByProduction($production->id) ;
  228. // gestion des commandes
  229. $this->gestionFormCommandes($production, $date, $points_vente, $produits, $users);
  230. // commandes
  231. $commandes = Commande::find()
  232. ->with('commandeProduits', 'user')
  233. ->joinWith('production')
  234. ->where(['production.date' => $date])
  235. ->andWhere(['production.id_etablissement' => Yii::$app->user->identity->id_etablissement])
  236. ->orderBy('date ASC')
  237. ->all();
  238. $recettes = 0;
  239. $recettes_pain = 0;
  240. $recettes_vrac = 0;
  241. $recettes_pain_livre = 0;
  242. $poids_pain = 0;
  243. $poids_vrac = 0;
  244. foreach ($commandes as $c) {
  245. $c->init();
  246. $recettes += $c->montant;
  247. $recettes_pain += $c->montant_pain;
  248. $recettes_vrac += $c->montant_vrac;
  249. if ($c->id_point_vente != 1)
  250. $recettes_pain_livre += $c->montant_pain;
  251. $poids_pain += $c->poids_pain;
  252. $poids_vrac += $c->poids_vrac;
  253. }
  254. $recettes = number_format($recettes, 2);
  255. $recettes_pain = number_format($recettes_pain, 2);
  256. $recettes_vrac = number_format($recettes_vrac, 2);
  257. // init commandes point de vente
  258. foreach ($points_vente as $pv) {
  259. $pv->initCommandes($commandes);
  260. $data_select_commandes = [] ;
  261. $data_options_commandes = [] ;
  262. foreach($pv->commandes as $c)
  263. {
  264. if($c->user)
  265. {
  266. $data_select_commandes[$c->id] = $c->user->nom.' '.$c->user->prenom ;
  267. }
  268. else {
  269. $data_select_commandes[$c->id] = $c->username;
  270. }
  271. $data_options_commandes[$c->id] = [] ;
  272. $array_options = [] ;
  273. $array_options[$c->id]['montant'] = number_format($c->montant, 2, ',', '') ;
  274. $array_options[$c->id]['produits'] = [] ;
  275. foreach($c->commandeProduits as $cp)
  276. {
  277. $array_options[$c->id]['produits'][$cp->id_produit] = $cp->quantite ;
  278. }
  279. $data_options_commandes[$c->id]['data-commande'] = json_encode($array_options[$c->id]) ;
  280. $data_options_commandes[$c->id]['value'] = $c->id ;
  281. }
  282. $pv->data_select_commandes = $data_select_commandes ;
  283. $pv->data_options_commandes = $data_options_commandes ;
  284. }
  285. // gestion produits selec
  286. if (isset($_POST['valider_produit_selec'])) {
  287. if (isset($_POST['Produit'])) {
  288. foreach ($produits as $p) {
  289. $produit_production = ProductionProduit::find()->where(['id_production' => $production->id, 'id_produit' => $p->id])->one();
  290. if (!$produit_production) {
  291. $produit_production = new ProductionProduit();
  292. $produit_production->id_production = $production->id;
  293. $produit_production->id_produit = $p->id;
  294. $produit_production->actif = 0;
  295. if (isset($p->quantite_max))
  296. $produit_production->quantite_max = $p->quantite_max;
  297. $produit_production->save();
  298. }
  299. if (isset($_POST['Produit'][$p->id]['actif'])) {
  300. $produit_production->actif = 1;
  301. } else {
  302. $produit_production->actif = 0;
  303. }
  304. if ((isset($_POST['Produit'][$p->id]['quantite_max']) && $_POST['Produit'][$p->id]['quantite_max'] != '')) {
  305. $produit_production->quantite_max = (int) $_POST['Produit'][$p->id]['quantite_max'];
  306. } else {
  307. if (isset($p->quantite_max) && is_numeric($p->quantite_max) && $p->quantite_max > 0) {
  308. $produit_production->quantite_max = $p->quantite_max;
  309. }
  310. }
  311. $produit_production->save();
  312. }
  313. }
  314. }
  315. // init produits sélectionnés pour cette production
  316. $produits_selec = [];
  317. if ($production) {
  318. $day_production = date('N', strtotime($production->date));
  319. $produits_production = ProductionProduit::find()->where(['id_production' => $production->id])->all();
  320. if (!count($produits_production)) {
  321. foreach ($produits as $p) {
  322. $pp = new ProductionProduit();
  323. $pp->id_production = $production->id;
  324. $pp->id_produit = $p->id;
  325. $pp->actif = 0;
  326. if ($day_production == 1 && $p->lundi)
  327. $pp->actif = 1;
  328. if ($day_production == 2 && $p->mardi)
  329. $pp->actif = 1;
  330. if ($day_production == 3 && $p->mercredi)
  331. $pp->actif = 1;
  332. if ($day_production == 4 && $p->jeudi)
  333. $pp->actif = 1;
  334. if ($day_production == 5 && $p->vendredi)
  335. $pp->actif = 1;
  336. if ($day_production == 6 && $p->samedi)
  337. $pp->actif = 1;
  338. if ($day_production == 7 && $p->dimanche)
  339. $pp->actif = 1;
  340. $pp->quantite_max = $p->quantite_max;
  341. $pp->save();
  342. }
  343. }
  344. // produits selec pour production
  345. $produits_selec = ProductionProduit::findProduits($production->id);
  346. }
  347. // produit en vrac forcément activé
  348. if ($date != '') {
  349. foreach ($produits as $p) {
  350. $produit_production = ProductionProduit::find()->where(['id_production' => $production->id, 'id_produit' => $p->id])->one();
  351. if ($p->vrac) {
  352. if (!$produit_production) {
  353. $produit_production = new ProductionProduit();
  354. $produit_production->id_production = $production->id;
  355. $produit_production->id_produit = $p->id;
  356. $produit_production->quantite_max = 0;
  357. $produit_production->actif = 1;
  358. $produit_production->save();
  359. } else {
  360. $produit_production->actif = 1;
  361. $produit_production->save();
  362. }
  363. }
  364. }
  365. }
  366. // produits
  367. $produits = Produit::getByProduction($production->id) ;
  368. // poids total de la production et CA potentiel
  369. $ca_potentiel = 0;
  370. $poids_total = 0;
  371. foreach ($produits_selec as $id_produit_selec => $produit_selec) {
  372. if ($produit_selec['actif']) {
  373. foreach ($produits as $produit) {
  374. if ($produit->id == $id_produit_selec) {
  375. //echo $produit->nom.' : '.$produit_selec['quantite_max'].'<br />' ;
  376. $ca_potentiel += $produit_selec['quantite_max'] * $produit->prix;
  377. $poids_total += $produit_selec['quantite_max'] * $produit->poids/1000;
  378. }
  379. }
  380. }
  381. }
  382. // jours de production
  383. $jours_production = Production::find()
  384. ->where([
  385. 'actif' => 1,
  386. 'id_etablissement' => Yii::$app->user->identity->id_etablissement
  387. ])
  388. ->all();
  389. // commandes auto
  390. $model_commande_auto = new CommandeAutoForm ;
  391. $datas = [
  392. 'produits' => $produits,
  393. 'points_vente' => $points_vente,
  394. 'commandes' => $commandes,
  395. 'date' => $date,
  396. 'production' => $production,
  397. 'jours_production' => $jours_production,
  398. 'produits_selec' => $produits_selec,
  399. 'users' => $arr_users,
  400. 'recettes' => $recettes,
  401. 'recettes_pain' => $recettes_pain,
  402. 'recettes_vrac' => $recettes_vrac,
  403. 'recettes_pain_livre' => $recettes_pain_livre,
  404. 'poids_pain' => $poids_pain,
  405. 'poids_vrac' => $poids_vrac,
  406. 'ca_potentiel' => $ca_potentiel,
  407. 'poids_total' => $poids_total,
  408. 'model_commande_auto' => $model_commande_auto,
  409. ];
  410. if ($return_data) {
  411. return $datas;
  412. } else {
  413. return $this->render('index', $datas);
  414. }
  415. }
  416. public function actionDownload($date = '', $id_point_vente = 0, $global = 0) {
  417. // commandes
  418. $commandes = Commande::find()
  419. ->with('commandeProduits', 'user')
  420. ->joinWith('production')
  421. ->where(['production.date' => $date])
  422. ->orderBy('date ASC')
  423. ->all();
  424. foreach ($commandes as $c)
  425. $c->init();
  426. // points de vente
  427. $points_vente = PointVente::find()->all();
  428. foreach ($points_vente as $pv)
  429. $pv->initCommandes($commandes);
  430. // produits
  431. $produits = Produit::find()->orderBy('order ASC')->all();
  432. $production = Production::find()->where('date LIKE \'' . $date . '\'')->one();
  433. $produits_selec = ProductionProduit::findProduits($production->id);
  434. /*
  435. * export global
  436. */
  437. if ($global) {
  438. $data = [];
  439. $filename = 'export_' . $date . '_global';
  440. $num_jour_semaine = date('w', strtotime($date));
  441. $arr_jour_semaine = [0 => 'dimanche', 1 => 'lundi', 2 => 'mardi', 3 => 'mercredi', 4 => 'jeudi', 5 => 'vendredi', 6 => 'samedi'];
  442. $champs_horaires_point_vente = 'horaires_' . $arr_jour_semaine[$num_jour_semaine];
  443. // header
  444. /* $line = [''] ;
  445. foreach($produits as $p) {
  446. if(isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  447. $line[] = $p->getLibelleAdmin() ;
  448. }
  449. }
  450. $data[] = $line ; */
  451. // par point de vente
  452. foreach ($points_vente as $pv) {
  453. if (count($pv->commandes) && strlen($pv->$champs_horaires_point_vente)) {
  454. //$data[] = [$pv->nom] ;
  455. $line = [$pv->nom, 'Produits', 'Montant', 'Commentaire'];
  456. /* foreach($produits as $p) {
  457. if(isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  458. $line[] = $p->getLibelleAdmin() ;
  459. }
  460. } */
  461. $data[] = $line;
  462. $res = $this->contentPointVenteCSV($date, $produits, $points_vente, $pv->id);
  463. foreach ($res['data'] as $line) {
  464. $data[] = $line;
  465. }
  466. }
  467. if (count($pv->commandes) && strlen($pv->$champs_horaires_point_vente)) {
  468. $line = ['Total pain'];
  469. $str_produits = '';
  470. foreach ($produits as $p) {
  471. if (!$p->vrac && isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  472. $quantite = Commande::getQuantiteProduit($p->id, $pv->commandes);
  473. $str_quantite = '';
  474. if ($quantite) {
  475. $str_quantite = $quantite;
  476. $str_produits .= $str_quantite . $p->diminutif . ', ';
  477. }
  478. }
  479. }
  480. $line[] = substr($str_produits, 0, strlen($str_produits) - 2);
  481. $line[] = number_format($pv->recettes_pain, 2) . ' €';
  482. $data[] = $line;
  483. $line = ['Total vrac'];
  484. $str_produits = '';
  485. foreach ($produits as $p) {
  486. if ($p->vrac && isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  487. $quantite = Commande::getQuantiteProduit($p->id, $pv->commandes);
  488. $str_quantite = '';
  489. if ($quantite) {
  490. $str_quantite = $quantite;
  491. $str_produits .= $str_quantite . $p->diminutif . ', ';
  492. }
  493. }
  494. }
  495. $line[] = substr($str_produits, 0, strlen($str_produits) - 2);
  496. $line[] = number_format($pv->recettes_vrac, 2) . ' €';
  497. $data[] = $line;
  498. $data[] = [];
  499. }
  500. }
  501. // récap
  502. //$line = ['Totaux'] ;
  503. // pain
  504. $line = ['Total pain'];
  505. $str_produits = '';
  506. foreach ($produits as $p) {
  507. if (!$p->vrac && isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  508. $quantite = Commande::getQuantiteProduit($p->id, $commandes);
  509. $str_quantite = '';
  510. if ($quantite) {
  511. $str_quantite = $quantite;
  512. $str_produits .= $str_quantite . '' . $p->diminutif . ', ';
  513. }
  514. }
  515. }
  516. $line[] = substr($str_produits, 0, strlen($str_produits) - 2);
  517. $data[] = $line;
  518. // vrac
  519. $line = ['Total vrac'];
  520. $str_produits = '';
  521. foreach ($produits as $p) {
  522. if ($p->vrac && isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  523. $quantite = Commande::getQuantiteProduit($p->id, $commandes);
  524. $str_quantite = '';
  525. if ($quantite) {
  526. $str_quantite = $quantite;
  527. $str_produits .= $str_quantite . '' . $p->diminutif . ', ';
  528. }
  529. }
  530. }
  531. $line[] = substr($str_produits, 0, strlen($str_produits) - 2);
  532. $data[] = $line;
  533. $infos = $this->actionIndex($date, true);
  534. // $data[] = [] ;
  535. /* $data[] = [
  536. 'CA potentiel boutique',
  537. number_format($infos['ca_potentiel'] - $infos['recettes_pain_livre'], 2).' €',
  538. ] ; */
  539. /* $res = $this->contentRecapCSV($date, $produits, $points_vente, $commandes) ;
  540. $data[] = ['Récapitulatif global'] ;
  541. foreach($res['data'] as $line) {
  542. $data[] = $line ;
  543. } */
  544. CSV::downloadSendHeaders($filename . '.csv');
  545. echo CSV::array2csv($data);
  546. die();
  547. }
  548. /*
  549. * export individuel
  550. */ else {
  551. if ($commandes && count($commandes)) {
  552. $data = [];
  553. // par point de vente
  554. if ($id_point_vente) {
  555. $res = $this->contentPointVenteCSV($date, $produits, $points_vente, $id_point_vente);
  556. $data = $res['data'];
  557. $filename = $res['filename'];
  558. }
  559. // récapitulatif
  560. else {
  561. $res = $this->contentRecapCSV($date, $produits, $points_vente, $commandes);
  562. $filename = 'recapitulatif_' . $date;
  563. $data = $res['data'];
  564. }
  565. CSV::downloadSendHeaders($filename . '.csv');
  566. echo CSV::array2csv($data);
  567. die();
  568. }
  569. }
  570. }
  571. public function contentRecapCSV($date, $produits, $points_vente, $commandes) {
  572. $data = [];
  573. $filename = 'recapitulatif_' . $date;
  574. $production = Production::find()->where('date LIKE \'' . $date . '\'')->one();
  575. $produits_selec = ProductionProduit::findProduits($production->id);
  576. // head
  577. $data[0] = ['Lieu'];
  578. foreach ($produits as $p) {
  579. if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  580. $data[0][] = $p->description;
  581. }
  582. }
  583. $num_jour_semaine = date('w', strtotime($date));
  584. $arr_jour_semaine = [0 => 'dimanche', 1 => 'lundi', 2 => 'mardi', 3 => 'mercredi', 4 => 'jeudi', 5 => 'vendredi', 6 => 'samedi'];
  585. $champs_horaires_point_vente = 'horaires_' . $arr_jour_semaine[$num_jour_semaine];
  586. // datas
  587. foreach ($points_vente as $pv) {
  588. if (count($pv->commandes) && strlen($pv->$champs_horaires_point_vente)) {
  589. $data_add = [$pv->nom];
  590. foreach ($produits as $p) {
  591. if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  592. $data_add[] = Commande::getQuantiteProduit($p->id, $pv->commandes);
  593. }
  594. }
  595. $data[] = $data_add;
  596. }
  597. }
  598. $data_add = ['Total'];
  599. foreach ($produits as $p) {
  600. if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  601. $data_add[] = Commande::getQuantiteProduit($p->id, $commandes);
  602. }
  603. }
  604. $data[] = $data_add;
  605. return [
  606. 'data' => $data,
  607. 'filename' => $filename
  608. ];
  609. }
  610. public function contentPointVenteCSV($date, $produits, $points_vente, $id_point_vente) {
  611. $data = [];
  612. $production = Production::find()->where('date LIKE \'' . $date . '\'')->one();
  613. $produits_selec = ProductionProduit::findProduits($production->id);
  614. // head
  615. /* $data[0] = ['Client', 'Date commande'] ;
  616. foreach($produits as $p) {
  617. if(isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  618. $data[0][] = $p->description ;
  619. }
  620. } */
  621. // datas
  622. foreach ($points_vente as $pv) {
  623. if ($pv->id == $id_point_vente) {
  624. $filename = 'export_' . $date . '_' . strtolower(str_replace(' ', '-', $pv->nom));
  625. foreach ($pv->commandes as $c) {
  626. $str_user = '';
  627. // username
  628. if ($c->user) {
  629. $str_user = $c->user->prenom . " " . $c->user->nom; //.' - '.date('d/m', strtotime($c->date)) ;
  630. } else {
  631. $str_user = $c->username; //.' - '.date('d/m', strtotime($c->date)) ;
  632. }
  633. // téléphone
  634. if (isset($c->user) && strlen($c->user->telephone)) {
  635. $str_user .= ' (' . $c->user->telephone . ')';
  636. }
  637. $data_add = [$str_user];
  638. // produits
  639. $str_produits = '';
  640. foreach ($produits as $p) {
  641. if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
  642. $add = false;
  643. foreach ($c->commandeProduits as $cp) {
  644. if ($p->id == $cp->id_produit) {
  645. $str_produits .= $cp->quantite . '' . $p->diminutif . ', ';
  646. $add = true;
  647. }
  648. }
  649. }
  650. }
  651. $data_add[] = substr($str_produits, 0, strlen($str_produits) - 2);
  652. $data_add[] = number_format($c->montant, 2) . ' €';
  653. $data_add[] = $c->commentaire;
  654. $data[] = $data_add;
  655. }
  656. }
  657. }
  658. return [
  659. 'data' => $data,
  660. 'filename' => $filename
  661. ];
  662. }
  663. public function actionChangeState($date, $actif)
  664. {
  665. // changement état
  666. $production = Production::find()->where(['date' => $date])->one();
  667. $production->actif = $actif;
  668. $production->save();
  669. // add commandes automatiques
  670. if($actif)
  671. {
  672. CommandeAuto::addAll($date) ;
  673. }
  674. $this->redirect(['index', 'date' => $date]);
  675. }
  676. public function actionChangeLivraison($date, $livraison)
  677. {
  678. $production = Production::find()->where(['date' => $date])->one();
  679. $production->livraison = $livraison;
  680. $production->save();
  681. $this->redirect(['index', 'date' => $date]);
  682. }
  683. public function actionAjaxUpdate($id_commande, $produits, $date)
  684. {
  685. $commande = Commande::find()->with('production')->where(['id' => $id_commande])->one() ;
  686. if($commande &&
  687. $commande->production->id_etablissement == Yii::$app->user->identity->id_etablissement)
  688. {
  689. $produits = json_decode($produits) ;
  690. foreach($produits as $key => $quantite)
  691. {
  692. $commande_produit = CommandeProduit::findOne([
  693. 'id_commande' => $id_commande,
  694. 'id_produit' => $key
  695. ]) ;
  696. if($quantite)
  697. {
  698. if($commande_produit)
  699. {
  700. $commande_produit->quantite = $quantite ;
  701. }
  702. else {
  703. $produit = Produit::findOne($key) ;
  704. if($produit)
  705. {
  706. $commande_produit = new CommandeProduit ;
  707. $commande_produit->id_commande = $id_commande ;
  708. $commande_produit->id_produit = $key ;
  709. $commande_produit->quantite = $quantite ;
  710. $commande_produit->prix = $produit->prix ;
  711. }
  712. }
  713. $commande_produit->save() ;
  714. }
  715. else {
  716. if($commande_produit)
  717. $commande_produit->delete() ;
  718. }
  719. }
  720. $commande->date_update = date('Y-m-d H:i:s') ;
  721. $commande->save() ;
  722. // total commande
  723. $commande = Commande::find()->with('commandeProduits')->where(['id' => $id_commande])->one() ;
  724. $commande->init() ;
  725. $json_commande = ['produits'=> [], 'montant' => number_format($commande->montant, 2)] ;
  726. foreach($commande->commandeProduits as $commande_produit)
  727. {
  728. $json_commande['produits'][$commande_produit->id_produit] = $commande_produit->quantite ;
  729. }
  730. // total point de vente
  731. $point_vente = PointVente::findOne($commande->id_point_vente) ;
  732. $commandes = Commande::find()
  733. ->with('commandeProduits', 'user')
  734. ->joinWith('production')
  735. ->where(['production.date' => $date])
  736. ->andWhere(['production.id_etablissement' => Yii::$app->user->identity->id_etablissement])
  737. ->orderBy('date ASC')
  738. ->all();
  739. foreach($commandes as $c) $c->init() ;
  740. $point_vente->initCommandes($commandes) ;
  741. echo json_encode([
  742. 'total_commande' => number_format($commande->montant, 2).' €',
  743. 'total_pv' => number_format($point_vente->recettes, 2).' €',
  744. 'json_commande' => json_encode($json_commande)
  745. ]) ;
  746. die() ;
  747. }
  748. }
  749. public function actionAjaxDelete($date, $id_commande)
  750. {
  751. $commande = Commande::find()->where(['id' => $id_commande])->one();
  752. // delete
  753. if ($commande) {
  754. $commande->delete();
  755. CommandeProduit::deleteAll(['id_commande' => $id_commande]);
  756. }
  757. // total point de vente
  758. $point_vente = PointVente::findOne($commande->id_point_vente) ;
  759. $commandes = Commande::find()
  760. ->with('commandeProduits', 'user')
  761. ->joinWith('production')
  762. ->where(['production.date' => $date])
  763. ->andWhere(['production.id_etablissement' => Yii::$app->user->identity->id_etablissement])
  764. ->orderBy('date ASC')
  765. ->all();
  766. foreach($commandes as $c) $c->init() ;
  767. $point_vente->initCommandes($commandes) ;
  768. echo json_encode([
  769. 'total_pv' => number_format($point_vente->recettes, 2).' €',
  770. ]) ;
  771. die() ;
  772. }
  773. public function actionAjaxCreate($date, $id_pv, $id_user, $username, $produits)
  774. {
  775. $produits = json_decode($produits) ;
  776. $point_vente = PointVente::findOne($id_pv) ;
  777. $production = Production::findOne(['date' => $date]) ;
  778. if(preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date) &&
  779. ($id_user || strlen($username)) &&
  780. $point_vente &&
  781. count($produits) &&
  782. $production)
  783. {
  784. $commande = new Commande ;
  785. $commande->date = date('Y-m-d H:i:s', strtotime($date.' '.date('H:i:s'))) ;
  786. $commande->id_point_vente = $id_pv ;
  787. $commande->id_production = $production->id ;
  788. $commande->type = Commande::TYPE_ADMIN ;
  789. if($id_user)
  790. {
  791. $commande->id_user = $id_user ;
  792. }
  793. else {
  794. $commande->username = $username ;
  795. $commande->id_user = 0 ;
  796. }
  797. $commande->save() ;
  798. foreach($produits as $key => $quantite)
  799. {
  800. $produit = Produit::findOne($key) ;
  801. if($produit)
  802. {
  803. $commande_produit = new CommandeProduit ;
  804. $commande_produit->id_commande = $commande->id ;
  805. $commande_produit->id_produit = $key ;
  806. $commande_produit->quantite = $quantite ;
  807. $commande_produit->prix = $produit->prix ;
  808. $commande_produit->save() ;
  809. }
  810. }
  811. // total point de vente
  812. $point_vente = PointVente::findOne($commande->id_point_vente) ;
  813. $commandes = Commande::find()
  814. ->with('commandeProduits', 'user')
  815. ->joinWith('production')
  816. ->where(['production.date' => $date])
  817. ->andWhere(['production.id_etablissement' => Yii::$app->user->identity->id_etablissement])
  818. ->orderBy('date ASC')
  819. ->all();
  820. foreach($commandes as $c) $c->init() ;
  821. $point_vente->initCommandes($commandes) ;
  822. // json commande
  823. $commande = Commande::find()
  824. ->with('commandeProduits', 'user')
  825. ->where(['commande.id' => $commande->id])
  826. ->one();
  827. $commande->init() ;
  828. $produits = [] ;
  829. foreach($commande->commandeProduits as $cp)
  830. {
  831. $produits[$cp->id_produit] = $cp->quantite ;
  832. }
  833. $json_commande = json_encode(['montant' => number_format($commande->montant, 2), 'produits' => $produits]) ;
  834. $str_user = '' ;
  835. if($commande->user)
  836. $str_user = $commande->user->nom.' '.$commande->user->prenom ;
  837. else
  838. $str_user = $commande->username ;
  839. $str_commentaire = '' ;
  840. if(strlen($commande->commentaire))
  841. {
  842. $str_commentaire = '<span class="glyphicon glyphicon-comment">'.Html::encode($commande->commentaire).'</span>' ;
  843. }
  844. $str_label_type_commande = '';
  845. if($commande->type)
  846. {
  847. $str_label_type_commande = ' <span class="label label-warning">vous</span>' ;
  848. }
  849. echo json_encode([
  850. 'id_commande' => $commande->id,
  851. 'total_pv' => number_format($point_vente->recettes, 2).' €',
  852. 'commande' => '<li>'
  853. . '<a class="btn btn-default" href="javascript:void(0);" '
  854. . 'data-pv-id="'.$id_pv.'" '
  855. . 'data-id-commande="'.$commande->id.'" '
  856. . 'data-commande=\''.$json_commande.'\' '
  857. . 'data-date="'.date('d/m H:i', strtotime($commande->date)).'">'
  858. . '<span class="montant">'.number_format($commande->montant, 2).' €</span>'
  859. . '<span class="user">'.$str_user.$str_label_type_commande.'</span>'
  860. . $str_commentaire
  861. . '</a></li>',
  862. ]) ;
  863. die() ;
  864. }
  865. }
  866. public function actionAjaxTotalCommandes($date)
  867. {
  868. $production = Production::find()
  869. ->where(['date' => $date])
  870. ->andWhere(['id_etablissement'=>Yii::$app->user->identity->id_etablissement])
  871. ->one();
  872. if($production)
  873. {
  874. // produits
  875. $produits = Produit::find()
  876. ->where(['id_etablissement' => Yii::$app->user->identity->id_etablissement])
  877. ->orderBy('order ASC')
  878. ->all();
  879. // commandes
  880. $commandes = Commande::find()
  881. ->with('commandeProduits', 'user')
  882. ->joinWith('production')
  883. ->where(['production.date' => $date])
  884. ->andWhere(['production.id_etablissement' => Yii::$app->user->identity->id_etablissement])
  885. ->orderBy('date ASC')
  886. ->all();
  887. $recettes = 0 ;
  888. $poids_pain = 0 ;
  889. foreach ($commandes as $c)
  890. {
  891. $c->init();
  892. $recettes += $c->montant ;
  893. $poids_pain += $c->poids_pain;
  894. }
  895. // produits selec pour production
  896. $produits_selec = ProductionProduit::findProduits($production->id);
  897. $ca_potentiel = 0;
  898. $poids_total = 0;
  899. foreach ($produits_selec as $id_produit_selec => $produit_selec) {
  900. if ($produit_selec['actif']) {
  901. foreach ($produits as $produit) {
  902. if ($produit->id == $id_produit_selec) {
  903. $ca_potentiel += $produit_selec['quantite_max'] * $produit->prix;
  904. $poids_total += $produit_selec['quantite_max'] * $produit->poids/1000;
  905. }
  906. }
  907. }
  908. }
  909. $html_totaux = $this->renderPartial('_total_commandes.php',[
  910. 'produits' => $produits,
  911. 'commandes' => $commandes,
  912. 'produits_selec' => $produits_selec,
  913. 'recettes_pain' => $recettes,
  914. 'poids_total' => $poids_total,
  915. 'ca_potentiel' => $ca_potentiel,
  916. 'poids_pain' => $poids_pain,
  917. ]);
  918. echo json_encode([
  919. 'html_totaux' => $html_totaux,
  920. ]) ;
  921. }
  922. die() ;
  923. }
  924. }