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

1249 行
50KB

  1. /**
  2. Copyright distrib (2018)
  3. contact@opendistrib.net
  4. Ce logiciel est un programme informatique servant à aider les producteurs
  5. à distribuer leur production en circuits courts.
  6. Ce logiciel est régi par la licence CeCILL soumise au droit français et
  7. respectant les principes de diffusion des logiciels libres. Vous pouvez
  8. utiliser, modifier et/ou redistribuer ce programme sous les conditions
  9. de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
  10. sur le site "http://www.cecill.info".
  11. En contrepartie de l'accessibilité au code source et des droits de copie,
  12. de modification et de redistribution accordés par cette licence, il n'est
  13. offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
  14. seule une responsabilité restreinte pèse sur l'auteur du programme, le
  15. titulaire des droits patrimoniaux et les concédants successifs.
  16. A cet égard l'attention de l'utilisateur est attirée sur les risques
  17. associés au chargement, à l'utilisation, à la modification et/ou au
  18. développement et à la reproduction du logiciel par l'utilisateur étant
  19. donné sa spécificité de logiciel libre, qui peut le rendre complexe à
  20. manipuler et qui le réserve donc à des développeurs et des professionnels
  21. avertis possédant des connaissances informatiques approfondies. Les
  22. utilisateurs sont donc invités à charger et tester l'adéquation du
  23. logiciel à leurs besoins dans des conditions permettant d'assurer la
  24. sécurité de leurs systèmes et ou de leurs données et, plus généralement,
  25. à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
  26. Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
  27. pris connaissance de la licence CeCILL, et que vous en avez accepté les
  28. termes.
  29. */
  30. var app = new Vue({
  31. el: '#app-distribution-index',
  32. data() {
  33. return Object.assign(
  34. {
  35. UrlManager: UrlManager,
  36. baseUrl: $('meta[name=baseurl]').attr('content'),
  37. date: null,
  38. dateFormat: null,
  39. loading: true,
  40. distribution: {
  41. active: false
  42. },
  43. producer: null,
  44. oneDistributionWeekActive: false,
  45. products: [],
  46. countActiveProducts: 0,
  47. pointsSale: [],
  48. meansPayment: [],
  49. idActivePointSale: 0,
  50. pointSaleActive: null,
  51. idDefaultPointSale: 0,
  52. countActivePointsSale: 0,
  53. countOrdersByPointSale: [],
  54. orders: [],
  55. ordersUpdate: [],
  56. countOrders: 0,
  57. users: [],
  58. deliveryNotes: [],
  59. showModalProducts: false,
  60. showModalPointsSale: false,
  61. showModalFormOrderCreate: false,
  62. orderCreate: null,
  63. showModalFormOrderUpdate: false,
  64. idOrderUpdate: 0,
  65. showViewProduct: false,
  66. idOrderView: 0,
  67. showModalPayment: false,
  68. idOrderPayment: 0,
  69. showLoading: false,
  70. tillerIsAuthenticated: false,
  71. tillerUrlAuthorizeCode: '#',
  72. tillerIsSynchro: false,
  73. checkboxSelectAllOrders: false,
  74. messageGenerateDeliveryNoteDisplayed: false,
  75. missingSubscriptions: false,
  76. loadingUpdateProductOrder: false,
  77. units: [],
  78. calendar: {
  79. mode: 'single',
  80. attrs: [],
  81. themeStyles: {
  82. wrapper: {
  83. background: '#F7F7F7',
  84. color: '#333',
  85. border: 'solid 1px #e0e0e0'
  86. },
  87. header: {
  88. padding: '10px 10px',
  89. },
  90. headerHorizontalDivider: {
  91. borderTop: 'solid rgba(255, 255, 255, 0.2) 1px',
  92. width: '80%',
  93. },
  94. weekdays: {
  95. color: '#e0e0e0',
  96. fontWeight: '600',
  97. padding: '10px 10px',
  98. fontSize: '2rem'
  99. },
  100. weeks: {
  101. padding: '0 15px 15px 15px',
  102. },
  103. dayContent: function (object) {
  104. var style = {
  105. fontSize: '2rem',
  106. padding: '16px',
  107. };
  108. if (object.isHovered || object.isFocus) {
  109. style.backgroundColor = '#F39C12';
  110. }
  111. return style;
  112. },
  113. },
  114. formats: {
  115. dayPopover: 'DD/MM/YYYY'
  116. }
  117. },
  118. }
  119. , window.vueValues);
  120. },
  121. mounted: function () {
  122. if ($('#distribution-date').size()) {
  123. this.date = new Date($('#distribution-date').html());
  124. this.dateFormat = ('0' + this.date.getDate()).slice(-2) + '/'
  125. + ('0' + (this.date.getMonth() + 1)).slice(-2) + '/'
  126. + this.date.getFullYear();
  127. }
  128. this.init();
  129. this.loading = false;
  130. },
  131. methods: {
  132. getDate: function () {
  133. return this.formatDate(this.date);
  134. },
  135. formatDate: function (date) {
  136. if (date) {
  137. return date.getFullYear() + '-'
  138. + ('0' + (date.getMonth() + 1)).slice(-2) + '-'
  139. + ('0' + date.getDate()).slice(-2);
  140. }
  141. return false;
  142. },
  143. isDistributionToday: function() {
  144. var today = new Date();
  145. return this.date
  146. && today.getFullYear() == this.date.getFullYear()
  147. && today.getMonth() == this.date.getMonth()
  148. && today.getDay() == this.date.getDay();
  149. },
  150. init: function (idActivePointSale) {
  151. var app = this;
  152. this.showLoading = true;
  153. axios.get("ajax-infos", {params: {date: this.getDate()}})
  154. .then(function (response) {
  155. app.calendar.attrs = [];
  156. app.units = response.data.units;
  157. app.distribution = response.data.distribution;
  158. app.producer = response.data.producer;
  159. app.products = response.data.products;
  160. app.initCountActiveProducts();
  161. app.meansPayment = response.data.means_payment;
  162. app.oneDistributionWeekActive = response.data.one_distribution_week_active;
  163. app.missingSubscriptions = response.data.missing_subscriptions;
  164. app.countOrders = 0;
  165. if (response.data.orders) {
  166. app.orders = JSON.parse(JSON.stringify(response.data.orders));
  167. app.ordersUpdate = JSON.parse(JSON.stringify(response.data.orders));
  168. for (i = 0; i < app.orders.length; i++) {
  169. if (!app.orders[i].date_delete) {
  170. app.countOrders++;
  171. }
  172. }
  173. } else {
  174. app.orders = [];
  175. }
  176. if (response.data.order_create) {
  177. app.orderCreate = response.data.order_create;
  178. app.idDefaultPointSale = app.orderCreate.id_point_sale;
  179. }
  180. if (response.data.points_sale) {
  181. app.pointsSale = response.data.points_sale;
  182. app.initPointsSale(idActivePointSale);
  183. } else {
  184. app.pointsSale = [];
  185. }
  186. if (response.data.users) {
  187. app.users = response.data.users;
  188. }
  189. if (response.data.delivery_notes) {
  190. app.deliveryNotes = response.data.delivery_notes;
  191. }
  192. app.tillerUrlAuthorizeCode = response.data.tiller_url_authorize_code;
  193. app.tillerIsAuthenticated = response.data.tiller_is_authenticated;
  194. app.tillerIsSynchro = response.data.tiller_is_synchro;
  195. app.calendar.attrs = [];
  196. var distributions = response.data.distributions;
  197. var leave_period_dates = response.data.leave_period_dates;
  198. var dayCurrentIsDistributionActive = false;
  199. var dateFormatCompare = false;
  200. if (app.date) {
  201. dateFormatCompare = app.date.getFullYear() + '-'
  202. + ('0' + (app.date.getMonth() + 1)).slice(-2) + '-'
  203. + ('0' + app.date.getDate()).slice(-2);
  204. }
  205. if (distributions.length) {
  206. var fillMode = 'solid';
  207. var dayToday = app.formatDate(new Date());
  208. for (var i = 0; i < distributions.length; i++) {
  209. if(distributions[i].date < dayToday) {
  210. fillMode = 'outline';
  211. }
  212. else {
  213. fillMode = 'solid';
  214. }
  215. app.calendar.attrs.push({
  216. key: distributions[i].date,
  217. dates: distributions[i].date,
  218. highlight: {
  219. color: 'green',
  220. fillMode: fillMode
  221. }
  222. });
  223. if (distributions[i].date == dateFormatCompare) {
  224. dayCurrentIsDistributionActive = true;
  225. }
  226. }
  227. }
  228. // leave period
  229. if (leave_period_dates.length) {
  230. app.calendar.attrs.push({
  231. key: 'leave_period',
  232. dates: leave_period_dates,
  233. highlight: {
  234. color: 'blue',
  235. fillMode: 'solid'
  236. }
  237. });
  238. }
  239. app.showLoading = false;
  240. app.checkboxSelectAllOrders = false;
  241. let searchParams = new URLSearchParams(window.location.search);
  242. if (searchParams.has('message_generate_bl') && !app.messageGenerateDeliveryNoteDisplayed) {
  243. appAlerts.alert('info', 'Pour générer un bon de livraison, sélectionnez tout d\'abord un jour et un lieu de distribution.', 6000);
  244. app.messageGenerateDeliveryNoteDisplayed = true;
  245. }
  246. if (app.idOrderUpdate) {
  247. app.updateOrderFromUrl();
  248. }
  249. setTimeout("opendistrib_popover(); opendistrib_dropdown_tooltip();", 500);
  250. var highlightStyle = {
  251. color: 'black',
  252. fillMode: 'light'
  253. }
  254. if (dayCurrentIsDistributionActive) {
  255. highlightStyle = {
  256. color: 'orange',
  257. fillMode: 'solid'
  258. }
  259. }
  260. app.calendar.attrs.push({
  261. key: 'current',
  262. highlight: highlightStyle,
  263. dates: app.date
  264. });
  265. });
  266. },
  267. initCountActiveProducts: function () {
  268. this.countActiveProducts = 0;
  269. for (var i = 0; i < this.products.length; i++) {
  270. if (this.products[i].productDistribution && this.products[i].productDistribution[0].active == 1) {
  271. this.countActiveProducts++;
  272. }
  273. }
  274. },
  275. initPointsSale: function (idActivePointSale) {
  276. this.countActivePointsSale = 0;
  277. this.setIdActivePointSale(0);
  278. for (var i = 0; i < this.pointsSale.length; i++) {
  279. if (this.pointsSale[i].pointSaleDistribution[0].delivery == 1) {
  280. this.countActivePointsSale++;
  281. this.setIdActivePointSale(this.pointsSale[i].id);
  282. }
  283. }
  284. if (this.countActivePointsSale > 1) {
  285. this.setIdActivePointSale(0);
  286. }
  287. if (idActivePointSale) {
  288. this.setIdActivePointSale(idActivePointSale);
  289. }
  290. this.countOrdersByPointSale = [];
  291. for (var i = 0; i < this.pointsSale.length; i++) {
  292. this.countOrdersByPointSale[this.pointsSale[i].id] = 0;
  293. }
  294. for (var i = 0; i < this.orders.length; i++) {
  295. this.countOrdersByPointSale[this.orders[i].id_point_sale]++;
  296. }
  297. },
  298. dayClicked: function (day) {
  299. this.date = day.date;
  300. this.dateFormat = ('0' + this.date.getDate()).slice(-2) + '/'
  301. + ('0' + (this.date.getMonth() + 1)).slice(-2) + '/'
  302. + this.date.getFullYear();
  303. this.init();
  304. },
  305. productQuantityMaxChange: function (event) {
  306. var quantityMax = event.currentTarget.value;
  307. axios.get("ajax-process-product-quantity-max", {
  308. params: {
  309. idDistribution: this.distribution.id,
  310. idProduct: event.currentTarget.getAttribute('data-id-product'),
  311. quantityMax: (!quantityMax || quantityMax.length === 0) ? -1 : quantityMax
  312. }
  313. })
  314. .then(function (response) {
  315. });
  316. },
  317. productActiveClick: function (event) {
  318. var idProduct = event.currentTarget.getAttribute('data-id-product');
  319. var activeProduct = event.currentTarget.getAttribute('data-active-product');
  320. axios.get("ajax-process-active-product", {
  321. params: {
  322. idDistribution: this.distribution.id,
  323. idProduct: idProduct,
  324. active: activeProduct
  325. }
  326. })
  327. .then(function (response) {
  328. });
  329. for (i = 0; i < this.products.length; i++) {
  330. if (this.products[i].productDistribution && this.products[i].id == idProduct) {
  331. this.products[i].productDistribution[0].active = activeProduct;
  332. }
  333. }
  334. this.initCountActiveProducts();
  335. },
  336. isOneProductMaximumQuantityExceeded: function () {
  337. for (var i = 0; i < this.products.length; i++) {
  338. if (this.isProductMaximumQuantityExceeded(this.products[i])) {
  339. return true;
  340. }
  341. }
  342. return false;
  343. },
  344. isProductMaximumQuantityExceeded: function (product) {
  345. return
  346. this.getProductDistribution(product)
  347. && this.getProductDistribution(product).quantity_max
  348. && product.quantity_ordered
  349. && product.quantity_ordered > this.getProductDistribution(product).quantity_max;
  350. },
  351. pointSaleActiveClick: function (event) {
  352. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale');
  353. var deliveryPointSale = event.currentTarget.getAttribute('data-delivery-point-sale');
  354. axios.get("ajax-process-active-point-sale", {
  355. params: {
  356. idDistribution: this.distribution.id,
  357. idPointSale: idPointSale,
  358. delivery: deliveryPointSale
  359. }
  360. })
  361. .then(function (response) {
  362. });
  363. for (i = 0; i < this.pointsSale.length; i++) {
  364. if (this.pointsSale[i].id == idPointSale) {
  365. this.pointsSale[i].pointSaleDistribution[0].delivery = deliveryPointSale;
  366. }
  367. }
  368. this.initPointsSale();
  369. },
  370. activeDistribution: function (event) {
  371. var app = this;
  372. var active = parseInt(event.currentTarget.getAttribute('data-active'));
  373. axios.get("ajax-process-active-distribution", {
  374. params: {
  375. idDistribution: this.distribution.id,
  376. active: active
  377. }
  378. })
  379. .then(function (response) {
  380. app.init();
  381. app.alertsActiveDistribution(active, 'Distribution');
  382. });
  383. },
  384. activeWeekDistribution: function (event) {
  385. var app = this;
  386. var active = parseInt(event.currentTarget.getAttribute('data-active'));
  387. axios.get("ajax-process-active-week-distribution", {
  388. params: {
  389. date: this.date.getFullYear() + '-'
  390. + ('0' + (this.date.getMonth() + 1)).slice(-2) + '-'
  391. + ('0' + this.date.getDate()).slice(-2),
  392. active: active
  393. }
  394. })
  395. .then(function (response) {
  396. app.init();
  397. app.alertsActiveDistribution(active, 'Semaine de distribution');
  398. });
  399. },
  400. alertsActiveDistribution: function (active, label) {
  401. if (!active) {
  402. appAlerts.alert(
  403. 'success',
  404. label + ' désactivée.',
  405. );
  406. appAlerts.alert(
  407. 'info',
  408. 'Pensez à bien recréditer les clients qui auraient passé commande en utilisant leur crédit.',
  409. 6000
  410. );
  411. } else {
  412. appAlerts.alert(
  413. 'success',
  414. label + ' activée.',
  415. );
  416. }
  417. },
  418. pointSaleClick: function (event) {
  419. this.setIdActivePointSale(event.currentTarget.getAttribute('data-id-point-sale'));
  420. },
  421. setIdActivePointSale: function (id) {
  422. this.idActivePointSale = id;
  423. for(key in this.pointsSale) {
  424. if(this.pointsSale[key].id == id) {
  425. this.pointSaleActive = this.pointsSale[key];
  426. }
  427. }
  428. if (!id) {
  429. this.orderCreate.id_point_sale = this.idDefaultPointSale;
  430. } else {
  431. this.orderCreate.id_point_sale = id;
  432. }
  433. setTimeout("opendistrib_popover(); opendistrib_dropdown_tooltip();", 500);
  434. },
  435. orderCreatedUpdated: function () {
  436. this.showModalFormOrderCreate = false;
  437. this.showModalFormOrderUpdate = false;
  438. this.init(this.idActivePointSale);
  439. },
  440. deleteOrderClick: function (event) {
  441. var app = this;
  442. var idOrder = event.currentTarget.getAttribute('data-id-order');
  443. axios.get(UrlManager.getBaseUrlAbsolute() + "order/ajax-delete", {
  444. params: {
  445. idOrder: idOrder
  446. }
  447. })
  448. .then(function (response) {
  449. app.init(app.idActivePointSale);
  450. });
  451. },
  452. updateOrderFromUrl: function () {
  453. this.initModalFormOrder();
  454. this.updateProductOrderPrices(false);
  455. },
  456. updateOrderClick: function (event) {
  457. var idOrder = event.currentTarget.getAttribute('data-id-order');
  458. this.idOrderUpdate = idOrder;
  459. this.showModalFormOrderUpdate = true;
  460. this.initModalFormOrder();
  461. this.updateProductOrderPrices(false);
  462. },
  463. openModalFormOrderCreate: function () {
  464. this.showModalFormOrderCreate = true;
  465. this.initModalFormOrder();
  466. this.updateProductOrderPrices(false);
  467. },
  468. initModalFormOrder: function () {
  469. var app = this;
  470. setTimeout(function () {
  471. $('.modal-body').css('height', $(window).height());
  472. $('.modal-body').css('maxHeight', 'unset');
  473. $('.select2-order-form').select2({
  474. width: 'resolve'
  475. });
  476. $('.select2-order-form').on('select2:select', function (e) {
  477. var idUser = e.params.data.id;
  478. if (app.showModalFormOrderCreate) {
  479. Vue.set(app.orderCreate, 'id_user', idUser);
  480. }
  481. if (app.showModalFormOrderUpdate) {
  482. Vue.set(app.ordersUpdate[app.getOrderUpdateKey()], 'id_user', idUser);
  483. }
  484. });
  485. // Passage à la ligne du dessous quand on appuie sur "Entrée"
  486. $('.modal-form-order .input-quantity').keypress(function (e) {
  487. if (e.which == 13) {
  488. $(this).parent().parent().parent()
  489. .next().find('.input-quantity').focus();
  490. }
  491. });
  492. }, 500);
  493. },
  494. getOrderUpdateKey: function () {
  495. if (app.showModalFormOrderUpdate && app.idOrderUpdate) {
  496. for (keyOrderUpdate in app.ordersUpdate) {
  497. if (app.ordersUpdate[keyOrderUpdate].id == app.idOrderUpdate) {
  498. return keyOrderUpdate;
  499. }
  500. }
  501. }
  502. },
  503. orderPaymentModalClick: function (event) {
  504. var idOrder = event.currentTarget.getAttribute('data-id-order');
  505. this.idOrderPayment = idOrder;
  506. this.showModalPayment = true;
  507. },
  508. getTypePayment: function(order) {
  509. if(order.amount_paid < order.amount) {
  510. return 'payment';
  511. }
  512. else {
  513. return 'refund';
  514. }
  515. },
  516. isTypePayment: function(order) {
  517. return this.getTypePayment(order) == 'payment';
  518. },
  519. getLabelPaymentRefund: function(order, labelPay, labelRefund, labelMeanPayment) {
  520. var str = '';
  521. if(this.isTypePayment(order)) {
  522. str += labelPay;
  523. }
  524. else {
  525. str += labelRefund;
  526. }
  527. str += ' '+labelMeanPayment;
  528. return str;
  529. },
  530. orderPaymentClick: function (event) {
  531. var app = this;
  532. var idOrder = event.currentTarget.getAttribute('data-id-order');
  533. if (!idOrder) {
  534. idOrder = this.idOrderPayment;
  535. }
  536. axios.get(UrlManager.getBaseUrlAbsolute() + "order/ajax-payment", {
  537. params: {
  538. idOrder: idOrder,
  539. type: event.currentTarget.getAttribute('data-type'),
  540. meanPayment: event.currentTarget.getAttribute('data-mean-payment')
  541. //amount: event.currentTarget.getAttribute('data-amount')
  542. }
  543. })
  544. .then(function (response) {
  545. app.init(app.idActivePointSale);
  546. });
  547. },
  548. orderViewClick: function (event) {
  549. var currentIdOrderView = event.currentTarget.getAttribute('data-id-order');
  550. if (this.idOrderView == currentIdOrderView) {
  551. this.showViewProduct = !this.showViewProduct;
  552. } else {
  553. this.showViewProduct = true;
  554. this.idOrderView = currentIdOrderView;
  555. }
  556. },
  557. closeModalProducts: function () {
  558. this.showModalProducts = false;
  559. this.init(this.idActivePointSale);
  560. },
  561. closeModalOrderForm: function(create) {
  562. if(create) {
  563. this.showModalFormOrderCreate = false
  564. }
  565. else {
  566. this.showModalFormOrderUpdate = false
  567. }
  568. this.init(this.idActivePointSale);
  569. },
  570. cloneOrder: function (order) {
  571. var clone = Object.assign({}, order);
  572. clone.productOrder = {};
  573. for (var key in order.productOrder) {
  574. clone.productOrder[key] = order.productOrder[key];
  575. }
  576. return clone;
  577. },
  578. addSubscriptions: function () {
  579. var app = this;
  580. axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-process-add-subscriptions", {
  581. params: {
  582. date: this.getDate()
  583. }
  584. })
  585. .then(function (response) {
  586. app.init(app.idActivePointSale);
  587. appAlerts.alertResponse(response);
  588. });
  589. },
  590. synchroTiller: function () {
  591. var app = this;
  592. this.showLoading = true;
  593. axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-process-synchro-tiller", {
  594. params: {
  595. date: this.getDate()
  596. }
  597. })
  598. .then(function (response) {
  599. app.init(app.idActivePointSale);
  600. });
  601. },
  602. authTiller: function() {
  603. },
  604. totalActivePointSale: function () {
  605. var total = 0;
  606. for (var i = 0; i < this.orders.length; i++) {
  607. if (this.orders[i].id_point_sale == this.idActivePointSale) {
  608. total += parseFloat(this.orders[i].amount);
  609. }
  610. }
  611. return total.toFixed(2);
  612. },
  613. weightActivePointSale: function () {
  614. var weight = 0;
  615. for (var i = 0; i < this.orders.length; i++) {
  616. if (this.orders[i].id_point_sale == this.idActivePointSale) {
  617. weight += parseFloat(this.orders[i].weight);
  618. }
  619. }
  620. return weight.toFixed(2);
  621. },
  622. changeSynchroTiller: function (event) {
  623. var app = this;
  624. var idOrder = event.currentTarget.getAttribute('data-id-order');
  625. axios.get(UrlManager.getBaseUrlAbsolute() + "order/ajax-change-synchro-tiller", {
  626. params: {
  627. idOrder: idOrder,
  628. boolSynchroTiller: event.currentTarget.checked ? 1 : 0
  629. }
  630. })
  631. .then(function (response) {
  632. app.init();
  633. });
  634. },
  635. selectAllOrdersEvent: function (event) {
  636. var bool = event.currentTarget.checked;
  637. this.selectAllOrders(bool);
  638. },
  639. selectAllOrders: function (bool) {
  640. for (var key in this.orders) {
  641. if (this.orders[key].id_point_sale == this.idActivePointSale) {
  642. this.orders[key].selected = bool;
  643. }
  644. }
  645. },
  646. oneOrderSelected: function () {
  647. for (var key in this.orders) {
  648. if (this.orders[key].selected == true) {
  649. return true;
  650. }
  651. }
  652. return false;
  653. },
  654. generateDeliveryNotePointSale: function () {
  655. if (!this.oneOrderSelected()) {
  656. this.selectAllOrders(true);
  657. }
  658. if (this.oneOrderSelected()) {
  659. var app = this;
  660. var idOrders = {};
  661. for (var key in this.orders) {
  662. if (this.orders[key].selected == true) {
  663. idOrders[key] = this.orders[key].id;
  664. }
  665. }
  666. axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-generate-delivery-note-point-sale", {
  667. params: {
  668. idOrders: JSON.stringify(idOrders)
  669. }
  670. })
  671. .then(function (response) {
  672. appAlerts.alertResponse(response);
  673. app.init(app.idActivePointSale);
  674. });
  675. } else {
  676. appAlerts.alert('danger', 'Veuillez sélectionner au moins une commande pour générer un bon de livraison');
  677. }
  678. },
  679. generateDeliveryNoteEachUser: function () {
  680. if (!this.oneOrderSelected()) {
  681. this.selectAllOrders(true);
  682. }
  683. if (this.oneOrderSelected()) {
  684. var app = this;
  685. var idOrders = {};
  686. for (var key in this.orders) {
  687. if (this.orders[key].selected == true) {
  688. idOrders[key] = this.orders[key].id;
  689. }
  690. }
  691. axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-generate-delivery-note-each-user", {
  692. params: {
  693. idOrders: JSON.stringify(idOrders)
  694. }
  695. })
  696. .then(function (response) {
  697. appAlerts.alertResponse(response);
  698. app.init(app.idActivePointSale);
  699. });
  700. } else {
  701. appAlerts.alert('danger', 'Veuillez sélectionner au moins une commande pour générer un bon de livraison');
  702. }
  703. },
  704. validateDeliveryNotes: function () {
  705. if (!this.oneOrderSelected()) {
  706. this.selectAllOrders(true);
  707. }
  708. if (this.oneOrderSelected()) {
  709. var app = this;
  710. var idOrders = {};
  711. for (var key in this.orders) {
  712. if (this.orders[key].selected == true) {
  713. idOrders[key] = this.orders[key].id;
  714. }
  715. }
  716. axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-validate-delivery-notes", {
  717. params: {
  718. idOrders: JSON.stringify(idOrders)
  719. }
  720. })
  721. .then(function (response) {
  722. appAlerts.alertResponse(response);
  723. app.init(app.idActivePointSale);
  724. });
  725. } else {
  726. appAlerts.alert('danger', 'Veuillez sélectionner au moins une commande pour valider un bon de livraison');
  727. }
  728. },
  729. generateDeliveryNote: function (event) {
  730. var app = this;
  731. var idOrder = event.currentTarget.getAttribute('data-id-order');
  732. axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-generate-delivery-note", {
  733. params: {
  734. idOrder: idOrder
  735. }
  736. })
  737. .then(function (response) {
  738. appAlerts.alertResponse(response);
  739. app.init(app.idActivePointSale);
  740. });
  741. },
  742. deliveryNoteExist: function (idPointSale) {
  743. return typeof this.deliveryNotes[this.idActivePointSale] != 'undefined';
  744. },
  745. payOrders: function () {
  746. var app = this;
  747. axios.get(UrlManager.getBaseUrlAbsolute() + "cron/pay-orders", {
  748. params: {
  749. date: app.getDate()
  750. }
  751. })
  752. .then(function (response) {
  753. appAlerts.alertResponse(response);
  754. app.init(app.idActivePointSale);
  755. });
  756. },
  757. updateProductOrderPrices: function (updatePricesOnUpdateOrder) {
  758. var app = this;
  759. app.loadingUpdateProductOrder = true;
  760. var order = null;
  761. if (app.showModalFormOrderCreate) {
  762. order = app.orderCreate;
  763. }
  764. if (app.showModalFormOrderUpdate && app.idOrderUpdate) {
  765. for (keyOrderUpdate in app.ordersUpdate) {
  766. if (app.ordersUpdate[keyOrderUpdate].id == app.idOrderUpdate) {
  767. order = app.ordersUpdate[keyOrderUpdate];
  768. }
  769. }
  770. }
  771. if (order) {
  772. axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-update-product-order", {
  773. params: {
  774. idDistribution: app.distribution.id,
  775. idUser: order.id_user,
  776. idPointSale: order.id_point_sale,
  777. idOrder: order.id
  778. }
  779. })
  780. .then(function (response) {
  781. if (response.data) {
  782. for (idProduct in response.data) {
  783. if (app.showModalFormOrderCreate) {
  784. Vue.set(app.orderCreate.productOrder[idProduct], 'prices', response.data[idProduct].prices);
  785. Vue.set(app.orderCreate.productOrder[idProduct], 'active', response.data[idProduct].active);
  786. Vue.set(app.orderCreate.productOrder[idProduct], 'price', app.getBestProductPrice(app.orderCreate, idProduct, app.orderCreate.productOrder[idProduct].quantity, false));
  787. Vue.set(app.orderCreate.productOrder[idProduct], 'price_with_tax', app.getBestProductPrice(app.orderCreate, idProduct, app.orderCreate.productOrder[idProduct].quantity, true));
  788. }
  789. if (app.showModalFormOrderUpdate && app.idOrderUpdate) {
  790. for (keyOrderUpdate in app.ordersUpdate) {
  791. if (order.id == app.idOrderUpdate) {
  792. Vue.set(app.ordersUpdate[keyOrderUpdate].productOrder[idProduct], 'prices', response.data[idProduct].prices);
  793. Vue.set(app.ordersUpdate[keyOrderUpdate].productOrder[idProduct], 'active', response.data[idProduct].active);
  794. Vue.set(app.ordersUpdate[keyOrderUpdate].productOrder[idProduct], 'invoice_price', response.data[idProduct].invoice_price);
  795. if (updatePricesOnUpdateOrder) {
  796. Vue.set(
  797. app.ordersUpdate[keyOrderUpdate].productOrder[idProduct],
  798. 'price',
  799. app.getBestProductPrice(app.ordersUpdate[keyOrderUpdate], idProduct, app.ordersUpdate[keyOrderUpdate].productOrder[idProduct].quantity, false));
  800. Vue.set(
  801. app.ordersUpdate[keyOrderUpdate].productOrder[idProduct],
  802. 'price_with_tax',
  803. app.getBestProductPrice(app.ordersUpdate[keyOrderUpdate], idProduct, app.ordersUpdate[keyOrderUpdate].productOrder[idProduct].quantity, true));
  804. }
  805. }
  806. }
  807. }
  808. }
  809. if (updatePricesOnUpdateOrder) {
  810. appAlerts.alert('info', 'Prix rechargés');
  811. }
  812. }
  813. app.loadingUpdateProductOrder = false;
  814. });
  815. }
  816. },
  817. updateInvoicePrices: function () {
  818. var order = null;
  819. if (app.showModalFormOrderUpdate && app.idOrderUpdate) {
  820. for (keyOrderUpdate in app.ordersUpdate) {
  821. if (app.ordersUpdate[keyOrderUpdate].id == app.idOrderUpdate) {
  822. order = app.ordersUpdate[keyOrderUpdate];
  823. }
  824. }
  825. }
  826. if (order) {
  827. axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-update-invoice-prices", {
  828. params: {
  829. idOrder: order.id
  830. }
  831. })
  832. .then(function (response) {
  833. app.updateProductOrderPrices(false);
  834. appAlerts.alert('info', 'Prix facturés réinitialisés.');
  835. });
  836. }
  837. },
  838. getBestProductPrice: function (order, idProduct, theQuantity, withTax) {
  839. var product = this.getProduct(idProduct);
  840. var thePrice = 9999;
  841. var pricesArray = order.productOrder[idProduct].prices;
  842. var unitCoefficient = this.getUnitCoefficient(product.unit);
  843. if (theQuantity) {
  844. theQuantity = theQuantity / unitCoefficient;
  845. }
  846. if (pricesArray) {
  847. for (var i = 0; i < pricesArray.length; i++) {
  848. var price = pricesArray[i].price;
  849. if (withTax) {
  850. price = pricesArray[i].price_with_tax
  851. }
  852. var fromQuantity = pricesArray[i].from_quantity;
  853. if (price < thePrice && fromQuantity <= theQuantity) {
  854. thePrice = price;
  855. }
  856. }
  857. } else {
  858. if (withTax) {
  859. thePrice = getPriceWithTax(product.price, product.taxRate.value);
  860. } else {
  861. thePrice = product.price;
  862. }
  863. }
  864. if (thePrice == 9999) {
  865. return 0;
  866. } else {
  867. return thePrice;
  868. }
  869. },
  870. getProduct: function (idProduct) {
  871. for (var i = 0; i < this.products.length; i++) {
  872. if (this.products[i].id == idProduct) {
  873. return this.products[i];
  874. }
  875. }
  876. return false;
  877. },
  878. countDocuments: function(order) {
  879. var count = 0;
  880. if(order.id_delivery_note) {
  881. count ++;
  882. }
  883. if(order.id_quotation) {
  884. count ++;
  885. }
  886. if(order.id_invoice) {
  887. count ++;
  888. }
  889. return count;
  890. },
  891. getUnitCoefficient: function(unit) {
  892. return this.units[unit].coefficient;
  893. },
  894. getProductDistribution: function(product) {
  895. if(typeof product.productDistribution !== 'undefined' && product.productDistribution[0]) {
  896. return product.productDistribution[0];
  897. }
  898. return null;
  899. },
  900. copyLinkOrder: function($event, urlOrder) {
  901. $event.preventDefault();
  902. navigator.clipboard.writeText(urlOrder);
  903. appAlerts.alert('success', 'Lien vers la page de commande de la distribution copié.');
  904. return false;
  905. }
  906. },
  907. });
  908. Vue.component('order-state-payment', {
  909. props: ['order', 'producer'],
  910. template: '#order-state-payment'
  911. });
  912. Vue.component('modal', {
  913. template: '#modal-template'
  914. });
  915. Vue.component('order-form', {
  916. props: ['distribution', 'date', 'dateFormat', 'pointsSale', 'idActivePointSale', 'meansPayment', 'users', 'products', 'order', 'orders', 'producer', 'loadingUpdateProductOrder', 'create', 'units'],
  917. emits: ['updateProductPrice', 'updateInvoicePrices'],
  918. data: function () {
  919. return {
  920. errors: [],
  921. idPointSale: 0,
  922. idUser: 0,
  923. username: '',
  924. comment: '',
  925. baseUrl: $('meta[name=baseurl]').attr('content'),
  926. vatMode: 'all' // 'with_tax'
  927. };
  928. },
  929. template: '#order-form-template',
  930. watch: {
  931. 'order.id_user': function () {
  932. this.userChange();
  933. }
  934. },
  935. methods: {
  936. checkForm: function () {
  937. this.errors = [];
  938. var countProducts = 0;
  939. for (var key in this.order.productOrder) {
  940. if (this.order.productOrder[key].quantity > 0) {
  941. countProducts++;
  942. }
  943. }
  944. if (this.order.id_point_sale
  945. && (this.order.id_user > 0 || (this.order.username && this.order.username.length))
  946. && countProducts > 0) {
  947. return true;
  948. }
  949. if (!this.order.id_point_sale) {
  950. this.errors.push('Veuillez sélectionner un point de vente');
  951. }
  952. if ((!this.order.id_user || this.order.id_user == 0) && !this.order.username.length) {
  953. this.errors.push('Veuillez sélectionner ou saisir un utilisateur');
  954. }
  955. if (!countProducts) {
  956. this.errors.push('Veuillez sélectionner au moins un produit');
  957. }
  958. },
  959. getProductOrderArrayRequest: function () {
  960. var productOrderArrayRequest = {};
  961. for (var key in this.order.productOrder) {
  962. productOrderArrayRequest[key] = {
  963. quantity: this.order.productOrder[key].quantity,
  964. unit: this.order.productOrder[key].unit,
  965. price: this.order.productOrder[key].price,
  966. invoice_price: this.order.productOrder[key].invoice_price
  967. };
  968. }
  969. return JSON.stringify(productOrderArrayRequest);
  970. },
  971. getPointSale: function(idPointSale) {
  972. for(key in this.pointsSale) {
  973. if (this.pointsSale[key].id == idPointSale) {
  974. return this.pointsSale[key];
  975. }
  976. }
  977. return false;
  978. },
  979. isPointSaleCreditFunctioningOptional: function(idPointSale) {
  980. var pointSale = this.getPointSale(idPointSale);
  981. if(pointSale && pointSale.credit_functioning == 'optional') {
  982. return true;
  983. }
  984. return false;
  985. },
  986. submitFormCreate: function (event) {
  987. var app = this;
  988. if (this.checkForm()) {
  989. axios.get(UrlManager.getBaseUrlAbsolute() + "order/ajax-create", {
  990. params: {
  991. date: this.date.getFullYear() + '-'
  992. + ('0' + (this.date.getMonth() + 1)).slice(-2) + '-'
  993. + ('0' + this.date.getDate()).slice(-2),
  994. idPointSale: this.order.id_point_sale,
  995. idUser: this.order.id_user,
  996. username: this.order.username,
  997. meanPayment: this.order.mean_payment,
  998. products: app.getProductOrderArrayRequest(),
  999. comment: this.order.comment,
  1000. debitCredit: this.order.debitCredit
  1001. }
  1002. })
  1003. .then(function (response) {
  1004. app.order.id_point_sale = 0;
  1005. app.order.id_user = 0;
  1006. app.order.username = '';
  1007. app.order.comment = '';
  1008. for (i = 0; i < app.order.productOrder.length; i++) {
  1009. app.order.productOrder[i] = 0;
  1010. }
  1011. app.$emit('ordercreatedupdated');
  1012. });
  1013. }
  1014. },
  1015. submitFormUpdate: function (event) {
  1016. var app = this;
  1017. if (this.checkForm()) {
  1018. var data = new FormData();
  1019. data.append('date', this.date.getFullYear() + '-'
  1020. + ('0' + (this.date.getMonth() + 1)).slice(-2) + '-'
  1021. + ('0' + this.date.getDate()).slice(-2));
  1022. data.append('idOrder', this.order.id);
  1023. data.append('idPointSale', this.order.id_point_sale);
  1024. data.append('meanPayment', this.order.mean_payment);
  1025. data.append('idUser', this.order.id_user);
  1026. data.append('username', '' + this.order.username);
  1027. data.append('products', app.getProductOrderArrayRequest());
  1028. data.append('comment', this.order.comment && this.order.comment.length ? this.order.comment : '');
  1029. data.append('debitCredit', this.order.debitCredit ? this.order.debitCredit : 0);
  1030. axios.post(UrlManager.getBaseUrlAbsolute() + "order/ajax-update", data)
  1031. .then(function (response) {
  1032. app.$emit('ordercreatedupdated');
  1033. });
  1034. }
  1035. },
  1036. productQuantityClick: function (id_product, quantity) {
  1037. if (!this.order.productOrder[id_product].quantity) {
  1038. this.order.productOrder[id_product].quantity = 0;
  1039. }
  1040. if (parseFloat(this.order.productOrder[id_product].quantity) + quantity >= 0) {
  1041. var theQuantity = (parseFloat(this.order.productOrder[id_product].quantity) + parseFloat(quantity)).toFixed(2);
  1042. var theQuantityDecimal = theQuantity % 1;
  1043. if (theQuantityDecimal == 0) {
  1044. theQuantity = parseInt(theQuantity);
  1045. }
  1046. Vue.set(this.order.productOrder[id_product], 'quantity', theQuantity);
  1047. Vue.set(this.order.productOrder[id_product], 'price', app.getBestProductPrice(this.order, id_product, theQuantity, false));
  1048. Vue.set(this.order.productOrder[id_product], 'price_with_tax', app.getBestProductPrice(this.order, id_product, theQuantity, true));
  1049. }
  1050. },
  1051. getProduct: function (idProduct) {
  1052. for (var i = 0; i < this.products.length; i++) {
  1053. if (this.products[i].id == idProduct) {
  1054. return this.products[i];
  1055. }
  1056. }
  1057. return false;
  1058. },
  1059. productPriceChange: function (event) {
  1060. var idProduct = event.currentTarget.getAttribute('data-id-product');
  1061. var product = this.getProduct(idProduct);
  1062. if (product) {
  1063. var taxRateValue = parseFloat(product.taxRate.value);
  1064. var withTax = event.currentTarget.getAttribute('data-with-tax');
  1065. var price = 0;
  1066. var priceWithTax = 0;
  1067. var priceValue = parseFloat(event.currentTarget.value.replace(',', '.'));
  1068. if (withTax) {
  1069. priceWithTax = priceValue.toFixed(2);
  1070. price = getPrice(priceWithTax, taxRateValue);
  1071. } else {
  1072. price = priceValue.toFixed(5);
  1073. priceWithTax = getPriceWithTax(price, taxRateValue);
  1074. }
  1075. if (isNaN(price)) {
  1076. price = 0;
  1077. }
  1078. if (isNaN(priceWithTax)) {
  1079. priceWithTax = 0;
  1080. }
  1081. Vue.set(this.order.productOrder, idProduct, {
  1082. active: this.order.productOrder[idProduct].active,
  1083. quantity: this.order.productOrder[idProduct].quantity,
  1084. unit: this.order.productOrder[idProduct].unit,
  1085. price: price,
  1086. price_with_tax: priceWithTax
  1087. });
  1088. }
  1089. },
  1090. userChange: function (event) {
  1091. var app = this;
  1092. axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-point-sale-favorite", {
  1093. params: {
  1094. idUser: app.order.id_user,
  1095. idDistribution: app.distribution.id
  1096. }
  1097. })
  1098. .then(function (response) {
  1099. if (app.idActivePointSale == 0) {
  1100. app.order.id_point_sale = response.data.id_favorite_point_sale;
  1101. }
  1102. app.updateProductOrderPrices(true);
  1103. });
  1104. },
  1105. pointSaleChange: function (event) {
  1106. this.updateProductOrderPrices(true);
  1107. },
  1108. updateProductOrderPrices: function (updateProductOrderPrices) {
  1109. this.$emit('updateproductorderprices', updateProductOrderPrices);
  1110. },
  1111. updateInvoicePrices: function () {
  1112. this.$emit('updateinvoiceprices');
  1113. },
  1114. toggleVatMode: function () {
  1115. if (this.vatMode == 'all') {
  1116. this.vatMode = 'with_tax';
  1117. } else {
  1118. this.vatMode = 'all';
  1119. }
  1120. },
  1121. getProductQuantityRemaining: function(product) {
  1122. var quantityRemaining = 0;
  1123. var productQuantityOrder = 0;
  1124. var unit = product.unit;
  1125. for(key in this.orders) {
  1126. productQuantityOrder += this.getProductQuantityProductOrder(this.orders[key], product);
  1127. }
  1128. if(this.create == 1) {
  1129. productQuantityOrder += this.getProductQuantityProductOrder(this.order, product);
  1130. }
  1131. quantityRemaining = this.getProductDistribution(product).quantity_max - productQuantityOrder;
  1132. if(unit != 'piece') {
  1133. quantityRemaining = quantityRemaining.toFixed(2);
  1134. }
  1135. return quantityRemaining;
  1136. },
  1137. getProductQuantityProductOrder: function(order, product) {
  1138. var productOrder = order.productOrder[product.id];
  1139. var unit = productOrder.unit;
  1140. var unitCoefficient = this.getUnitCoefficient(unit);
  1141. return parseFloat(productOrder.quantity / unitCoefficient);
  1142. },
  1143. getUnitCoefficient: function(unit) {
  1144. return this.units[unit].coefficient;
  1145. },
  1146. getProductDistribution: function(product) {
  1147. if(typeof product.productDistribution !== 'undefined' && product.productDistribution[0]) {
  1148. return product.productDistribution[0];
  1149. }
  1150. }
  1151. }
  1152. });