You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1221 satır
49KB

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