Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

OrderController.php 46KB

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