選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

1103 行
52KB

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