Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

OrderController.php 45KB

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