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.

1171 lines
44KB

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