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.

1135 line
54KB

  1. <?php
  2. /**
  3. * Copyright distrib (2018)
  4. *
  5. * contact@opendistrib.net
  6. *
  7. * Ce logiciel est un programme informatique servant à aider les producteurs
  8. * à distribuer leur production en circuits courts.
  9. *
  10. * Ce logiciel est régi par la licence CeCILL soumise au droit français et
  11. * respectant les principes de diffusion des logiciels libres. Vous pouvez
  12. * utiliser, modifier et/ou redistribuer ce programme sous les conditions
  13. * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
  14. * sur le site "http://www.cecill.info".
  15. *
  16. * En contrepartie de l'accessibilité au code source et des droits de copie,
  17. * de modification et de redistribution accordés par cette licence, il n'est
  18. * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
  19. * seule une responsabilité restreinte pèse sur l'auteur du programme, le
  20. * titulaire des droits patrimoniaux et les concédants successifs.
  21. *
  22. * A cet égard l'attention de l'utilisateur est attirée sur les risques
  23. * associés au chargement, à l'utilisation, à la modification et/ou au
  24. * développement et à la reproduction du logiciel par l'utilisateur étant
  25. * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
  26. * manipuler et qui le réserve donc à des développeurs et des professionnels
  27. * avertis possédant des connaissances informatiques approfondies. Les
  28. * utilisateurs sont donc invités à charger et tester l'adéquation du
  29. * logiciel à leurs besoins dans des conditions permettant d'assurer la
  30. * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
  31. * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
  32. *
  33. * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
  34. * pris connaissance de la licence CeCILL, et que vous en avez accepté les
  35. * termes.
  36. */
  37. namespace backend\controllers;
  38. use common\helpers\GlobalParam;
  39. use common\models\Order;
  40. use common\models\ProductOrder;
  41. use common\models\Product;
  42. use common\models\User;
  43. use common\models\ProductDistribution;
  44. use common\models\Distribution;
  45. use common\models\Producer;
  46. class OrderController extends BackendController
  47. {
  48. var $enableCsrfValidation = false;
  49. public function behaviors()
  50. {
  51. return [
  52. 'access' => [
  53. 'class' => AccessControl::className(),
  54. 'rules' => [
  55. [
  56. 'allow' => true,
  57. 'roles' => ['@'],
  58. 'matchCallback' => function ($rule, $action) {
  59. return User::getCurrentStatus() == USER::STATUS_ADMIN
  60. || User::getCurrentStatus() == USER::STATUS_PRODUCER;
  61. }
  62. ]
  63. ],
  64. ],
  65. ];
  66. }
  67. /**
  68. * Traite le formulaire d'ajout/modification de commande.
  69. *
  70. * @param Distribution $distribution
  71. * @param string $date
  72. * @param array $points_vente
  73. * @param array $produits
  74. * @param array $users
  75. */
  76. public function processOrderForm(
  77. $distribution, $date, $pointsSale, $products, $users)
  78. {
  79. if ($date != '') {
  80. // commandes
  81. $orders = Order::searchAll([
  82. 'distribution.date' => $date
  83. ]);
  84. foreach ($pointsSale as $point) {
  85. $point->initOrders($orders);
  86. if (isset($_POST['submit_pv']) && $_POST['submit_pv']) {
  87. // modifs
  88. foreach ($point->orders as $order) {
  89. // suppression des product_order
  90. ProductOrder::deleteAll(['id_order' => $order->id]);
  91. // création des commande_produit modifiés
  92. foreach ($products as $product) {
  93. $quantity = Yii::$app->getRequest()->post('product_' . $point->id . '_' . $product->id, 0);
  94. if ($quantity) {
  95. $productOrder = new ProductOrder;
  96. $productOrder->id_order = $order->id;
  97. $productOrder->id_product = $product->id;
  98. $productOrder->quantity = $quantity;
  99. $productOrder->price = $p->price;
  100. $productOrder->unit = $p->unit;
  101. $productOrder->step = $p->step;
  102. $productOrder->id_tax_rate = $p->taxRate->id ;
  103. $productOrder->save();
  104. }
  105. }
  106. }
  107. $username = Yii::$app->getRequest()->post('username_point_sale_' . $point->id, 0);
  108. $date = Yii::$app->getRequest()->post('date_order_point_sale_' . $point->id, 0);
  109. $oneProduct = false;
  110. foreach ($products as $product) {
  111. $quantity = Yii::$app->getRequest()->post('product_point_sale_' . $point->id . '_' . $product->id, 0);
  112. if ($quantity) {
  113. $oneProduct = true;
  114. }
  115. }
  116. if (strlen($username) && $date && $oneProduct) {
  117. $order = new Order;
  118. $order->id_point_sale = $point->id;
  119. $order->id_production = $distribution->id;
  120. $order->id_user = 0;
  121. $order->username = $username;
  122. $arrayDate = explode('/', $date);
  123. $order->date = $arrayDate[2] . '-' . $arrayDate[1] . '-' . $arrayDate[0] . ' 00:00:00';
  124. $order->save();
  125. foreach ($products as $product) {
  126. $quantity = Yii::$app->getRequest()->post('product_point_sale_' . $point->id . '_' . $product->id, 0);
  127. if ($quantity) {
  128. $productOrder = new ProductOrder;
  129. $productOrder->id_order = $order->id;
  130. $productOrder->id_product = $product->id;
  131. $productOrder->quantity = $quantity;
  132. $productOrder->price = $p->price;
  133. $productOrder->unit = $p->unit;
  134. $productOrder->step = $p->step;
  135. $productOrder->id_tax_rate = $p->taxRate->id ;
  136. $productOrder->save();
  137. }
  138. }
  139. $order->initReference() ;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. /**
  146. * Page principale de la gestion des commandes.
  147. *
  148. * @param string $date
  149. * @param boolean $returnData
  150. * @return string
  151. */
  152. public function actionIndex($date = '', $returnData = false)
  153. {
  154. if (!Product::searchCount() || !PointSale::searchCount()) {
  155. $this->redirect(['site/index', 'error_products_points_sale' => 1]);
  156. }
  157. $orders = [];
  158. // users
  159. $arrayUsers = [0 => '--'];
  160. $users = User::searchAll([], ['orderby' => 'lastname, name ASC']);
  161. foreach ($users as $user) {
  162. $arrayUsers[$user->id] = $user->name . ' ' . $user->lastname;
  163. }
  164. // création du jour de distribution
  165. $distribution = Distribution::initDistribution($date);
  166. // points de vente
  167. if ($distribution) {
  168. $arrayPointsSale = PointSale::find()
  169. ->joinWith(['pointSaleDistribution' => function ($q) use ($distribution) {
  170. $q->where(['id_distribution' => $distribution->id]);
  171. }])
  172. ->where([
  173. 'id_producer' => GlobalParam::getCurrentProducerId(),
  174. ])
  175. ->all();
  176. } else {
  177. $arrayPointsSale = PointSale::searchAll();
  178. }
  179. // produits
  180. $arrayProducts = Product::searchAll();
  181. // gestion des commandes
  182. $this->processOrderForm($distribution, $date, $arrayPointsSale, $arrayProducts, $users);
  183. // commandes
  184. $arrayOrders = Order::searchAll([
  185. 'distribution.date' => $date,
  186. ]);
  187. $revenues = 0;
  188. $weight = 0;
  189. $revenuesDelivered = 0;
  190. if ($arrayOrders) {
  191. foreach ($arrayOrders as $order) {
  192. if (is_null($order->date_delete)) {
  193. $revenues += $order->amount;
  194. if ($order->id_point_sale != 1) {
  195. $revenuesDelivered += $order->amount;
  196. }
  197. $weight += $order->weight;
  198. }
  199. }
  200. }
  201. $revenues = number_format($revenues, 2);
  202. // init commandes point de vente
  203. foreach ($arrayPointsSale as $pointSale) {
  204. $pointSale->initOrders($arrayOrders);
  205. $dataSelectOrders = [];
  206. $dataOptionsOrders = [];
  207. foreach ($pointSale->orders as $order) {
  208. if ($order->user) {
  209. $dataSelectOrders[$order->id] = $order->user->name . ' ' . $order->user->lastname;
  210. } else {
  211. $dataSelectOrders[$order->id] = $order->username;
  212. }
  213. $dataOptionsOrders[$order->id] = [];
  214. $arrayOptions = [];
  215. $arrayOptions[$order->id]['amount'] = $order->amount;
  216. $arrayOptions[$order->id]['str_amount'] = number_format($order->amount, 2, ',', '') . ' €';
  217. $arrayOptions[$order->id]['paid_amount'] = $order->paid_amount;
  218. $arrayOptions[$order->id]['products'] = [];
  219. $arrayOptions[$order->id]['comment'] = Html::encode($order->comment);
  220. foreach ($order->productOrder as $productOrder) {
  221. $arrayOptions[$order->id]['products'][$productOrder->id_product] = $productOrder->quantity;
  222. }
  223. $dataOptionsOrders[$order->id]['data-order'] = json_encode($arrayOptions[$order->id]);
  224. $dataOptionsOrders[$order->id]['value'] = $order->id;
  225. }
  226. $pointSale->data_select_orders = $dataSelectOrders;
  227. $pointSale->data_options_orders = $dataOptionsOrders;
  228. }
  229. // gestion produits selec
  230. if (isset($_POST['valider_produit_selec'])) {
  231. if (isset($_POST['Product'])) {
  232. foreach ($arrayProducts as $product) {
  233. $productDistribution = ProductDistribution::searchOne([
  234. 'id_distribution' => $distribution->id,
  235. 'id_product' => $product->id
  236. ]);
  237. if (!$productDistribution) {
  238. $productDistribution = new ProductDistribution();
  239. $productDistribution->id_distribution = $distribution->id;
  240. $productDistribution->id_product = $product->id;
  241. $productDistribution->active = 0;
  242. if (isset($product->quantity_max)) {
  243. $productDistribution->quantity_max = $product->quantity_max;
  244. } else {
  245. $productDistribution->quantity_max = null;
  246. }
  247. $productDistribution->save();
  248. }
  249. if (isset($_POST['Product'][$product->id]['active'])) {
  250. $productDistribution->active = 1;
  251. } else {
  252. $productDistribution->active = 0;
  253. }
  254. if ((isset($_POST['Product'][$product->id]['quantity_max']) && $_POST['Product'][$product->id]['quantity_max'] != '')) {
  255. $productDistribution->quantity_max = (int)$_POST['Product'][$product->id]['quantity_max'];
  256. } else {
  257. $productDistribution->quantity_max = null;
  258. }
  259. $productDistribution->save();
  260. }
  261. }
  262. }
  263. $arrayProductsSelected = [];
  264. if ($distribution) {
  265. // produits selec pour production
  266. $arrayProductsSelected = ProductDistribution::searchByDistribution($distribution->id);
  267. }
  268. // produits
  269. if ($distribution) {
  270. $arrayProducts = Product::searchByDistribution($distribution->id);
  271. }
  272. // poids total de la production et CA potentiel
  273. $potentialTurnover = 0;
  274. $totalWeight = 0;
  275. foreach ($arrayProductsSelected as $idSelectedProduct => $selectedProduct) {
  276. if ($selectedProduct['active']) {
  277. foreach ($arrayProducts as $product) {
  278. if ($product->id == $idSelectedProduct) {
  279. $potentialTurnover += $selectedProduct['quantity_max'] * $product->price;
  280. $totalWeight += $selectedProduct['quantity_max'] * $product->weight / 1000;
  281. }
  282. }
  283. }
  284. }
  285. // jours de distribution
  286. $arrayDistributionDays = Distribution::searchAll([
  287. 'active' => 1
  288. ]);
  289. // commandes auto
  290. $subscriptionForm = new SubscriptionForm;
  291. // productions point vente
  292. $pointSaleDistribution = new PointSaleDistribution;
  293. $oointsSaleDistribution = [];
  294. if ($distribution) {
  295. $pointsSaleDistribution = PointSaleDistribution::searchAll([
  296. 'id_distribution' => $distribution->id
  297. ]);
  298. }
  299. $arrayPointsSaleDistribution = [];
  300. if (isset($pointsSaleDistribution)) {
  301. foreach ($pointsSaleDistribution as $pointSaleDistrib) {
  302. $key = $pointSaleDistrib->id_distribution . '-' . $pointSaleDistrib->id_point_sale;
  303. if ($pointSaleDistrib->delivery == 1) {
  304. $pointSaleDistribution->points_sale_distribution[] = $key;
  305. }
  306. if (isset($pointSaleDistrib->pointSale) && strlen($pointSaleDistrib->pointSale->name)) {
  307. $arrayPointsSaleDistribution[$key] = Html::encode($pointSaleDistrib->pointSale->name);
  308. }
  309. }
  310. }
  311. // une production de la semaine activée ou non
  312. $oneDistributionWeekActive = false;
  313. $week = sprintf('%02d', date('W', strtotime($date)));
  314. $start = strtotime(date('Y', strtotime($date)) . 'W' . $week);
  315. $dateMonday = date('Y-m-d', strtotime('Monday', $start));
  316. $dateTuesday = date('Y-m-d', strtotime('Tuesday', $start));
  317. $dateWednesday = date('Y-m-d', strtotime('Wednesday', $start));
  318. $dateThursday = date('Y-m-d', strtotime('Thursday', $start));
  319. $dateFriday = date('Y-m-d', strtotime('Friday', $start));
  320. $dateSaturday = date('Y-m-d', strtotime('Saturday', $start));
  321. $dateSunday = date('Y-m-d', strtotime('Sunday', $start));
  322. $weekDistribution = Distribution::find()
  323. ->andWhere([
  324. 'id_producer' => GlobalParam::getCurrentProducerId(),
  325. 'active' => 1,
  326. ])
  327. ->andWhere(['or',
  328. ['date' => $dateMonday],
  329. ['date' => $dateTuesday],
  330. ['date' => $dateWednesday],
  331. ['date' => $dateThursday],
  332. ['date' => $dateFriday],
  333. ['date' => $dateSaturday],
  334. ['date' => $dateSunday],
  335. ])
  336. ->one();
  337. if ($weekDistribution) {
  338. $oneDistributionWeekActive = true;
  339. }
  340. $datas = [
  341. 'arrayProducts' => $arrayProducts,
  342. 'arrayPointsSale' => $arrayPointsSale,
  343. 'arrayOrders' => $arrayOrders,
  344. 'date' => $date,
  345. 'distribution' => $distribution,
  346. 'arrayDistributionDays' => $arrayDistributionDays,
  347. 'selectedProducts' => $arrayProductsSelected,
  348. 'users' => $arrayUsers,
  349. 'revenues' => $revenues,
  350. 'revenuesDelivered' => $revenuesDelivered,
  351. 'weight' => $weight,
  352. 'potentialTurnover' => $potentialTurnover,
  353. 'totalWeight' => $totalWeight,
  354. 'subscriptionForm' => $subscriptionForm,
  355. 'pointSaleDistribution' => $pointSaleDistribution,
  356. 'arrayPointsSaleDistribution' => $arrayPointsSaleDistribution,
  357. 'oneDistributionWeekActive' => $oneDistributionWeekActive
  358. ];
  359. if ($returnData) {
  360. return $datas;
  361. } else {
  362. return $this->render('index', $datas);
  363. }
  364. }
  365. /**
  366. * Génère un fichier d'export des commandes au format CSV.
  367. *
  368. * @param string $date
  369. * @param integer $id_point_vente
  370. * @param boolean $global
  371. */
  372. public function actionDownload($date = '', $idPointSale = 0, $global = 0)
  373. {
  374. // commandes
  375. $ordersArray = Order::searchAll([
  376. 'distribution.date' => $date
  377. ]);
  378. // points de vente
  379. $pointsSaleArray = PointSale::searchAll();
  380. foreach ($pointsSaleArray as $pointSale) {
  381. $pv->initOrders($ordersArray);
  382. }
  383. // produits
  384. $productsArray = Product::find()->orderBy('order ASC')->all();
  385. $distribution = Distribution::find()
  386. ->where('date LIKE \':date\'')
  387. ->params([':date' => $date])
  388. ->one();
  389. $selectedProductsArray = ProductDistribution::searchByDistribution($distribution->id);
  390. /*
  391. * export global
  392. */
  393. if ($global) {
  394. $data = [];
  395. $filename = 'export_' . $date . '_global';
  396. $dayWeek = date('w', strtotime($date));
  397. $dayWeekArray = [0 => 'sunday', 1 => 'monday', 2 => 'tuesday', 3 => 'wednesday', 4 => 'thursday', 5 => 'friday', 6 => 'saturday'];
  398. $fieldsHoursPointSale = 'infos_' . $dayWeekArray[$dayWeek];
  399. // par point de vente
  400. foreach ($pointsSaleArray as $pointSale) {
  401. if (count($pointSale->orders) && strlen($pointSale->$fieldsHoursPointSale)) {
  402. $line = [$pointSale->name, 'Produits', 'Montant', 'Commentaire'];
  403. $data[] = $line;
  404. $res = $this->contentPointSaleCSV($date, $productsArray, $pointsSaleArray, $pointSale->id);
  405. foreach ($res['data'] as $line) {
  406. $data[] = $line;
  407. }
  408. }
  409. if (count($pointSale->orders) && strlen($pointSale->$fieldsHoursPointSale)) {
  410. $line = ['Total'];
  411. $strProducts = '';
  412. foreach ($productsArray as $product) {
  413. if (isset($selectedProductsArray[$product->id]['active']) && $selectedProductsArray[$product->id]['active']) {
  414. $quantity = Order::getProductQuantity($product->id, $pointSale->orders);
  415. $strQuantity = '';
  416. if ($quantity) {
  417. $strQuantity = $quantity;
  418. $strProducts .= $strQuantity . ', ';
  419. }
  420. }
  421. }
  422. $line[] = substr($strProducts, 0, strlen($strProducts) - 2);
  423. $line[] = number_format($pointSale->revenues, 2) . ' €';
  424. $data[] = $line;
  425. $data[] = [];
  426. }
  427. }
  428. $line = ['Total'];
  429. $strProducts = '';
  430. foreach ($productsArray as $product) {
  431. if (isset($selectedProductsArray[$product->id]['active']) && $selectedProductsArray[$product->id]['active']) {
  432. $quantity = Order::getProductQuantity($product->id, $ordersArray);
  433. $strQuantity = '';
  434. if ($quantity) {
  435. $strQuantity = $quantity;
  436. $strQuantity .= $strQuantity . ', ';
  437. }
  438. }
  439. }
  440. $line[] = substr($strProducts, 0, strlen($strProducts) - 2);
  441. $data[] = $line;
  442. $infos = $this->actionIndex($date, true);
  443. CSV::downloadSendHeaders($filename . '.csv');
  444. echo CSV::array2csv($data);
  445. die();
  446. } /*
  447. * export individuel
  448. */
  449. else {
  450. if ($ordersArray && count($ordersArray)) {
  451. $data = [];
  452. // par point de vente
  453. if ($idPointSale) {
  454. $res = $this->contentPointSaleCSV($date, $productsArray, $pointsSaleArray, $idPointSale);
  455. $data = $res['data'];
  456. $filename = $res['filename'];
  457. } // récapitulatif
  458. else {
  459. $res = $this->contentRecapCSV($date, $productsArray, $pointsSaleArray, $ordersArray);
  460. $filename = 'summary_' . $date;
  461. $data = $res['data'];
  462. }
  463. CSV::downloadSendHeaders($filename . '.csv');
  464. echo CSV::array2csv($data);
  465. die();
  466. }
  467. }
  468. }
  469. /**
  470. * Génère le contenu nécessaire aux exports au format CSV.
  471. *
  472. * @see OrderController::actionDownload()
  473. * @param string $date
  474. * @param array $products
  475. * @param array $pointsSale
  476. * @param array $orders
  477. * @return array
  478. */
  479. public function contentRecapCSV($date, $products, $pointsSale, $orders)
  480. {
  481. $data = [];
  482. $filename = 'summary_' . $date;
  483. $distribution = Distribution::find()
  484. ->where('date LIKE \':date\'')
  485. ->params([':date' => $date])
  486. ->one();
  487. $selectedProductsArray = ProductDistribution::searchByDistribution($distribution->id);
  488. // head
  489. $data[0] = ['Lieu'];
  490. foreach ($products as $product) {
  491. if (isset($selectedProductsArray[$product->id]['active']) && $selectedProductsArray[$product->id]['active']) {
  492. $data[0][] = $product->description;
  493. }
  494. }
  495. $dayWeek = date('w', strtotime($date));
  496. $dayWeekArray = [0 => 'sunday', 1 => 'monday', 2 => 'tuesday', 3 => 'wednesday', 4 => 'thursday', 5 => 'friday', 6 => 'saturday'];
  497. $fieldHoursPointSale = 'infos_' . $dayWeekArray[$dayWeek];
  498. // datas
  499. foreach ($pointsSale as $pointSale) {
  500. if (count($pointSale->orders) && strlen($pointSale->$fieldHoursPointSale)) {
  501. $dataAdd = [$pointSale->name];
  502. foreach ($products as $product) {
  503. if (isset($selectedProductsArray[$product->id]['active']) && $selectedProductsArray[$product->id]['active']) {
  504. $dataAdd[] = Order::getProductQuantity($product->id, $pointSale->orders);
  505. }
  506. }
  507. $data[] = $dataAdd;
  508. }
  509. }
  510. $dataAdd = ['Total'];
  511. foreach ($products as $product) {
  512. if (isset($selectedProductsArray[$product->id]['active']) && $selectedProductsArray[$product->id]['active']) {
  513. $dataAdd[] = Order::getProductQuantity($product->id, $orders);
  514. }
  515. }
  516. $data[] = $dataAdd;
  517. return [
  518. 'data' => $data,
  519. 'filename' => $filename
  520. ];
  521. }
  522. /**
  523. * Génère le contenu relatif aux points de vente nécessaires aux exports au
  524. * format CSV.
  525. *
  526. * @param string $date
  527. * @param array $produits
  528. * @param array $points_vente
  529. * @param integer $id_point_vente
  530. * @return array
  531. */
  532. public function contentPointSaleCSV($date, $products, $pointsSale, $idPointSale)
  533. {
  534. $data = [];
  535. $distribution = Distribution::find()->where('date LIKE \':date\'')->params([':date' => $date])->one();
  536. $selectedProductsArray = ProductDistribution::searchByDistribution($distribution->id);
  537. // datas
  538. foreach ($pointsSale as $pointSale) {
  539. if ($pointSale->id == $idPointSale) {
  540. $filename = 'export_' . $date . '_' . strtolower(str_replace(' ', '-', $pointSale->name));
  541. foreach ($pointSale->orders as $order) {
  542. $strUser = '';
  543. // username
  544. if ($order->user) {
  545. $strUser = $order->user->name . " " . $order->user->lastname;
  546. } else {
  547. $strUser = $order->username;
  548. }
  549. // téléphone
  550. if (isset($order->user) && strlen($order->user->phone)) {
  551. $strUser .= ' (' . $order->user->phone . ')';
  552. }
  553. $dataAdd = [$strUser];
  554. // produits
  555. $strProducts = '';
  556. foreach ($products as $product) {
  557. if (isset($selectedProductsArray[$product->id]['active']) && $selectedProductsArray[$product->id]['active']) {
  558. $add = false;
  559. foreach ($product->productOrder as $productOrder) {
  560. if ($product->id == $productOrder->id_product) {
  561. $strProducts .= $productOrder->quantity;
  562. $add = true;
  563. }
  564. }
  565. }
  566. }
  567. $dataAdd[] = substr($strProducts, 0, strlen($strProducts) - 2);
  568. $dataAdd[] = number_format($order->amount, 2) . ' €';
  569. $dataAdd[] = $order->comment;
  570. $data[] = $dataAdd;
  571. }
  572. }
  573. }
  574. return [
  575. 'data' => $data,
  576. 'filename' => $filename
  577. ];
  578. }
  579. /**
  580. * Change l'état d'un jour de production (activé, désactivé).
  581. *
  582. * @param string $date
  583. * @param integer $actif
  584. * @param boolean $redirect
  585. */
  586. public function actionChangeState($date, $active, $redirect = true)
  587. {
  588. // changement état
  589. $distribution = Distribution::initDistribution($date);
  590. $distribution->active = $active;
  591. $distribution->save();
  592. if ($active) {
  593. // add commandes automatiques
  594. Subscription::addAll($date);
  595. }
  596. if ($redirect) {
  597. $this->redirect(['index', 'date' => $date]);
  598. }
  599. }
  600. /**
  601. * Change l'état d'une semaine de production (activé, désactivé).
  602. *
  603. * @param string $date
  604. * @param integer $actif
  605. */
  606. public function actionChangeStateWeek($date, $active)
  607. {
  608. $week = sprintf('%02d', date('W', strtotime($date)));
  609. $start = strtotime(date('Y', strtotime($date)) . 'W' . $week);
  610. $dateMonday = date('Y-m-d', strtotime('Monday', $start));
  611. $dateTuesday = date('Y-m-d', strtotime('Tuesday', $start));
  612. $dateWednesday = date('Y-m-d', strtotime('Wednesday', $start));
  613. $dateThursday = date('Y-m-d', strtotime('Thursday', $start));
  614. $dateFriday = date('Y-m-d', strtotime('Friday', $start));
  615. $dateSaturday = date('Y-m-d', strtotime('Saturday', $start));
  616. $dateSunday = date('Y-m-d', strtotime('Sunday', $start));
  617. $pointsSaleArray = PointSale::searchAll();
  618. $activeMonday = false;
  619. $activeTuesday = false;
  620. $activeWednesday = false;
  621. $activeThursday = false;
  622. $activeFriday = false;
  623. $activeSaturday = false;
  624. $activeSunday = false;
  625. foreach ($pointsSaleArray as $pointSale) {
  626. if ($pointSale->delivery_monday) $activeMonday = true;
  627. if ($pointSale->delivery_tuesday) $activeTuesday = true;
  628. if ($pointSale->delivery_wednesday) $activeWednesday = true;
  629. if ($pointSale->delivery_thursday) $activeThursday = true;
  630. if ($pointSale->delivery_friday) $activeFriday = true;
  631. if ($pointSale->delivery_saturday) $activeSaturday = true;
  632. if ($pointSale->delivery_sunday) $activeSunday = true;
  633. }
  634. if ($activeMonday || !$active) $this->actionChangeState($dateMonday, $active, false);
  635. if ($activeTuesday || !$active) $this->actionChangeState($activeTuesday, $active, false);
  636. if ($activeWednesday || !$active) $this->actionChangeState($activeWednesday, $active, false);
  637. if ($activeThursday || !$active) $this->actionChangeState($activeThursday, $active, false);
  638. if ($activeFriday || !$active) $this->actionChangeState($activeFriday, $active, false);
  639. if ($activeSaturday || !$active) $this->actionChangeState($activeSaturday, $active, false);
  640. if ($activeSunday || !$active) $this->actionChangeState($activeSunday, $active, false);
  641. $this->redirect(['index', 'date' => $date]);
  642. }
  643. /**
  644. * Supprime une commande via une requête AJAX.
  645. *
  646. * @param string $date
  647. * @param integer $idOrder
  648. */
  649. public function actionAjaxDelete($idOrder)
  650. {
  651. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  652. $order = Order::searchOne([
  653. 'id' => $idOrder
  654. ]);
  655. // delete
  656. if ($order) {
  657. $order->delete();
  658. }
  659. return ['success'];
  660. }
  661. /**
  662. * Supprime une commande.
  663. *
  664. * @param string $date
  665. * @param integer $idOrder
  666. */
  667. public function actionDelete($date, $idOrder)
  668. {
  669. $order = Order::searchOne(['id' => $idOrder]);
  670. if ($order) {
  671. // remboursement de la commande
  672. if ($order->id_user && $order->getAmount(Order::AMOUNT_PAID) && Producer::getConfig('credit')) {
  673. $order->saveCreditHistory(
  674. CreditHistory::TYPE_REFUND,
  675. $order->getAmount(Order::AMOUNT_PAID),
  676. $order->distribution->id_producer,
  677. $order->id_user,
  678. User::getCurrentId()
  679. );
  680. }
  681. $order->delete();
  682. ProductOrder::deleteAll(['id_order' => $idOrder]);
  683. }
  684. $this->redirect(['index', 'date' => $date]);
  685. }
  686. /**
  687. * Crée une commande via une requête AJAX.
  688. *
  689. * @param string $date
  690. * @param integer $idPointSale
  691. * @param integer $idUser
  692. * @param string $username
  693. * @param array $produits
  694. * @param string $commentaire
  695. * @param string $processCredit
  696. */
  697. public function actionAjaxCreate(
  698. $date, $idPointSale, $idUser, $username, $meanPayment = '', $products, $comment, $processCredit = 0)
  699. {
  700. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  701. $products = json_decode($products);
  702. $pointSale = PointSale::findOne($idPointSale);
  703. $distribution = Distribution::searchOne([
  704. 'date' => $date
  705. ]);
  706. if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $date) &&
  707. ($idUser || strlen($username)) &&
  708. $pointSale &&
  709. count(get_object_vars($products)) &&
  710. $distribution) {
  711. $order = new Order;
  712. $order->date = date('Y-m-d H:i:s');
  713. $order->id_point_sale = $idPointSale;
  714. $order->mean_payment = $meanPayment;
  715. $order->id_distribution = $distribution->id;
  716. $order->origin = Order::ORIGIN_ADMIN;
  717. $order->comment = $comment;
  718. $order->status = 'tmp-order';
  719. if ($idUser) {
  720. $order->id_user = $idUser;
  721. // commentaire du point de vente
  722. $userPointSale = UserPointSale::searchOne([
  723. 'id_point_sale' => $idPointSale,
  724. 'id_user' => $idUser
  725. ]);
  726. if ($userPointSale && strlen($userPointSale->comment)) {
  727. $order->comment_point_sale = $userPointSale->comment;
  728. }
  729. } else {
  730. $order->username = $username;
  731. $order->id_user = 0;
  732. }
  733. $order->save();
  734. foreach ($products as $key => $dataProductOrder) {
  735. $product = Product::findOne($key);
  736. $quantity = $dataProductOrder->quantity / Product::$unitsArray[$dataProductOrder->unit]['coefficient'];
  737. if ($product && $quantity) {
  738. $productOrder = new ProductOrder;
  739. $productOrder->id_order = $order->id;
  740. $productOrder->id_product = $key;
  741. $productOrder->quantity = $quantity;
  742. $productOrder->unit = $product->unit;
  743. $productOrder->step = $product->step;
  744. if($dataProductOrder->price) {
  745. $productOrder->price = number_format(Price::getPrice($dataProductOrder->price, $product->taxRate->value), 3) ;
  746. }
  747. else {
  748. $productOrder->price = $product->price;
  749. }
  750. $productOrder->id_tax_rate = $product->taxRate->id;
  751. $productOrder->save();
  752. }
  753. }
  754. $order = Order::searchOne(['id' => $order->id]);
  755. if ($order && $processCredit) {
  756. $order->processCredit();
  757. }
  758. if($order) {
  759. $order->initReference() ;
  760. $order->setTillerSynchronization() ;
  761. }
  762. // lien utilisateur / point de vente
  763. if ($idUser && $pointSale) {
  764. $pointSale->linkUser($idUser);
  765. }
  766. }
  767. return ['success'];
  768. }
  769. /**
  770. * Met à jour une commande via une requête AJAX.
  771. *
  772. * @param integer $idOrder
  773. * @param array $products
  774. * @param string $date
  775. * @param string $comment
  776. */
  777. public function actionAjaxUpdate()
  778. {
  779. $request = Yii::$app->request ;
  780. $date = $request->post('date') ;
  781. $idOrder = $request->post('idOrder') ;
  782. $idPointSale = $request->post('idPointSale') ;
  783. $idUser = $request->post('idUser') ;
  784. $username = $request->post('username') ;
  785. $meanPayment = $request->post('meanPayment') ;
  786. $products = $request->post('products') ;
  787. $comment = $request->post('comment') ;
  788. $processCredit = $request->post('processCredit') ;
  789. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  790. $order = Order::searchOne(['id' => $idOrder]);
  791. if ($order &&
  792. $order->distribution->id_producer == GlobalParam::getCurrentProducerId()) {
  793. $products = json_decode($products);
  794. foreach ($products as $key => $dataProductOrder) {
  795. $productOrder = ProductOrder::findOne([
  796. 'id_order' => $idOrder,
  797. 'id_product' => $key
  798. ]);
  799. $quantity = $dataProductOrder->quantity
  800. / Product::$unitsArray[$dataProductOrder->unit]['coefficient'];
  801. if ($quantity) {
  802. if ($productOrder) {
  803. $productOrder->quantity = $quantity;
  804. $productOrder->price = $dataProductOrder->price;
  805. } else {
  806. $product = Product::findOne($key);
  807. if ($product) {
  808. $productOrder = new ProductOrder;
  809. $productOrder->id_order = $idOrder;
  810. $productOrder->id_product = $key;
  811. $productOrder->quantity = $quantity;
  812. $productOrder->unit = $product->unit;
  813. $productOrder->step = $product->step;
  814. if($dataProductOrder->price) {
  815. $productOrder->price = number_format(Price::getPrice($dataProductOrder->price, $product->taxRate->value), 3) ;
  816. }
  817. else {
  818. $productOrder->price = $product->price;
  819. }
  820. $productOrder->id_tax_rate = $product->taxRate->id;
  821. }
  822. }
  823. $productOrder->save();
  824. } else {
  825. if ($productOrder) {
  826. $productOrder->delete();
  827. }
  828. }
  829. }
  830. $order->id_point_sale = $idPointSale;
  831. $order->date_update = date('Y-m-d H:i:s');
  832. $order->mean_payment = $meanPayment;
  833. $order->comment = $comment;
  834. if ($idUser) {
  835. $order->username = '';
  836. $order->id_user = $idUser;
  837. // commentaire du point de vente
  838. $userPointSale = UserPointSale::searchOne([
  839. 'id_point_sale' => $order->id_point_sale,
  840. 'id_user' => $idUser
  841. ]);
  842. if ($userPointSale && strlen($userPointSale->comment)) {
  843. $order->comment_point_sale = $userPointSale->comment;
  844. }
  845. } else {
  846. $order->username = $username;
  847. $order->id_user = 0;
  848. }
  849. $order->save();
  850. $order = Order::searchOne(['id' => $order->id]);
  851. if ($order && $processCredit) {
  852. $order->processCredit();
  853. }
  854. }
  855. }
  856. /**
  857. * Retourne l'état du paiement (historique, crédit) d'une commande donnée.
  858. *
  859. * @param integer $idOrder
  860. */
  861. public function actionPaymentStatus($idOrder)
  862. {
  863. $order = Order::searchOne(['id' => $idOrder]);
  864. if ($order) {
  865. $html = '';
  866. if ($order->id_user) {
  867. $userProducer = UserProducer::find()
  868. ->where([
  869. 'id_user' => $order->id_user,
  870. 'id_producer' => $order->distribution->id_producer
  871. ])
  872. ->one();
  873. $amountPaid = $order->getAmount(Order::AMOUNT_PAID);
  874. if (abs($order->amount - $amountPaid) < 0.0001) {
  875. $html .= '<span class="label label-success">Payé</span>';
  876. $buttonsCredit = Html::a('Rembourser ' . $order->getAmountWithTax(Order::AMOUNT_TOTAL, true), 'javascript:void(0);', ['class' => 'btn btn-default btn-xs rembourser', 'data-montant' => $order->amount, 'data-type' => 'refund']);
  877. } elseif ($order->amount > $amountPaid) {
  878. $amountToPay = $order->amount - $amountPaid;
  879. $html .= '<span class="label label-danger">Non payé</span> reste <strong>' . number_format($amountToPay, 2) . ' €</strong> à payer';
  880. $buttonsCredit = Html::a('Payer ' . number_format($amountToPay, 2) . ' €', 'javascript:void(0);', ['class' => 'btn btn-default btn-xs payer', 'data-montant' => $amountToPay, 'data-type' => 'payment']);
  881. } elseif ($order->amount < $amountPaid) {
  882. $amountToRefund = $amountPaid - $order->amount;
  883. $html .= ' <span class="label label-success">Payé</span> <strong>' . number_format($amountToRefund, 2) . ' €</strong> à rembourser';
  884. $buttonsCredit = Html::a('Rembourser ' . number_format($amountToRefund, 2) . ' €', 'javascript:void(0);', ['class' => 'btn btn-default btn-xs rembourser', 'data-montant' => $amountToRefund, 'data-type' => 'refund']);
  885. }
  886. $html .= '<span class="buttons-credit">'
  887. . 'Crédit : <strong>' . number_format($userProducer->credit, 2) . ' €</strong><br />'
  888. . $buttonsCredit
  889. . '</span>';
  890. // historique
  891. $history = CreditHistory::find()
  892. ->with('userAction')
  893. ->where(['id_order' => $idOrder])
  894. ->all();
  895. $html .= '<br /><br /><strong>Historique</strong><br /><table class="table table-condensed table-bordered">'
  896. . '<thead><tr><th>Date</th><th>Utilisateur</th><th>Action</th><th>- Débit</th><th>+ Crédit</th></tr></thead>'
  897. . '<tbody>';
  898. if ($history && is_array($history) && count($history)) {
  899. foreach ($history as $h) {
  900. $html .= '<tr>'
  901. . '<td>' . date('d/m/Y H:i:s', strtotime($h->date)) . '</td>'
  902. . '<td>' . Html::encode($h->strUserAction()) . '</td>'
  903. . '<td>' . $h->getStrWording() . '</td>'
  904. . '<td>' . ($h->isTypeDebit() ? '- ' . $h->getAmountWithTax(Order::AMOUNT_TOTAL, true) : '') . '</td>'
  905. . '<td>' . ($h->isTypeCredit() ? '+ ' . $h->getAmountWithTax(Order::AMOUNT_TOTAL, true) : '') . '</td>'
  906. . '</tr>';
  907. }
  908. } else {
  909. $html .= '<tr><td colspan="4">Aucun résultat</td></tr>';
  910. }
  911. $html .= '</tbody></table>';
  912. } else {
  913. $html .= '<div class="alert alert-warning">Pas de gestion de crédit pain pour cette commande car elle n\'est pas liée à un compte utilisateur.</div>';
  914. }
  915. echo json_encode([
  916. 'html_payment_status' => $html,
  917. 'json_order' => $order->getDataJson()
  918. ]);
  919. }
  920. die();
  921. }
  922. /**
  923. * Effectue le paiement/remboursement d'une commande.
  924. *
  925. * @param integer $idOrder
  926. * @param string $type
  927. * @param float $amount
  928. * @return string
  929. */
  930. public function actionAjaxPayment($idOrder, $type, $amount)
  931. {
  932. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  933. $order = Order::searchOne([
  934. 'id' => $idOrder
  935. ]);
  936. if ($order) {
  937. $order->saveCreditHistory(
  938. $type,
  939. $amount,
  940. GlobalParam::getCurrentProducerId(),
  941. $order->id_user,
  942. User::getCurrentId()
  943. );
  944. }
  945. return ['success'];
  946. }
  947. /**
  948. * Modifie l'état de la synchronisation Tiller d'une commande.
  949. *
  950. * @param int $idOrder
  951. * @param boolean $boolSynchroTiller
  952. * @return array
  953. */
  954. public function actionAjaxChangeSynchroTiller($idOrder, $boolSynchroTiller)
  955. {
  956. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  957. $order = Order::searchOne([
  958. 'id' => (int)$idOrder
  959. ]);
  960. if ($order) {
  961. $order->tiller_synchronization = (int)$boolSynchroTiller;
  962. $res = $order->save();
  963. return ['success'];
  964. }
  965. return ['error'];
  966. }
  967. }