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

1293 行
57KB

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