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.

distribution-index.js 60KB

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