Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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