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.

1134 lines
46KB

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