Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

968 lines
40KB

  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. idDefaultPointSale: 0,
  51. countActivePointsSale: 0,
  52. countOrdersByPointSale: [],
  53. orders: [],
  54. ordersUpdate: [],
  55. countOrders: 0,
  56. users: [],
  57. deliveryNotes: [],
  58. showModalProducts: false,
  59. showModalPointsSale: false,
  60. showModalFormOrderCreate: false,
  61. orderCreate: null,
  62. showModalFormOrderUpdate: false,
  63. idOrderUpdate: 0,
  64. showViewProduct: false,
  65. idOrderView: 0,
  66. showModalPayment: false,
  67. idOrderPayment: 0,
  68. showLoading: false,
  69. tillerIsSynchro: false,
  70. checkboxSelectAllOrders: false,
  71. messageGenerateDeliveryNoteDisplayed: false,
  72. missingSubscriptions: false,
  73. loadingUpdateProductOrder: false,
  74. calendar: {
  75. mode: 'single',
  76. attrs: [],
  77. themeStyles: {
  78. wrapper: {
  79. background: '#F7F7F7',
  80. color: '#333',
  81. border: 'solid 1px #e0e0e0'
  82. },
  83. header: {
  84. padding: '10px 10px',
  85. },
  86. headerHorizontalDivider: {
  87. borderTop: 'solid rgba(255, 255, 255, 0.2) 1px',
  88. width: '80%',
  89. },
  90. weekdays: {
  91. color: '#e0e0e0',
  92. fontWeight: '600',
  93. padding: '10px 10px',
  94. fontSize: '2rem'
  95. },
  96. weeks: {
  97. padding: '0 15px 15px 15px',
  98. },
  99. dayContent: function(object) {
  100. var style = {
  101. fontSize: '2rem',
  102. padding: '16px',
  103. };
  104. if(object.isHovered || object.isFocus) {
  105. style.backgroundColor = '#F39C12' ;
  106. }
  107. return style ;
  108. },
  109. },
  110. formats: {
  111. dayPopover: 'DD/MM/YYYY'
  112. }
  113. },
  114. }
  115. , window.vueValues);
  116. },
  117. mounted: function() {
  118. if($('#distribution-date').size()) {
  119. this.date = new Date($('#distribution-date').html()) ;
  120. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  121. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  122. + this.date.getFullYear() ;
  123. }
  124. this.init() ;
  125. this.loading = false ;
  126. },
  127. methods: {
  128. getDate: function() {
  129. return this.formatDate(this.date) ;
  130. },
  131. formatDate: function(date) {
  132. if(date) {
  133. return date.getFullYear() + '-'
  134. + ('0' + (date.getMonth() +1)).slice(-2) + '-'
  135. + ('0' + date.getDate()).slice(-2) ;
  136. }
  137. return false ;
  138. },
  139. init: function(idActivePointSale) {
  140. var app = this ;
  141. this.showLoading = true ;
  142. axios.get("ajax-infos",{params: {date : this.getDate()}})
  143. .then(function(response) {
  144. app.distribution = response.data.distribution ;
  145. app.producer = response.data.producer ;
  146. app.products = response.data.products ;
  147. app.initCountActiveProducts() ;
  148. app.meansPayment = response.data.means_payment ;
  149. app.oneDistributionWeekActive = response.data.one_distribution_week_active ;
  150. app.missingSubscriptions = response.data.missing_subscriptions ;
  151. app.countOrders = 0 ;
  152. if(response.data.orders) {
  153. app.orders = JSON.parse(JSON.stringify(response.data.orders)) ;
  154. app.ordersUpdate = JSON.parse(JSON.stringify(response.data.orders)) ;
  155. for(i=0 ; i < app.orders.length ; i++) {
  156. if(!app.orders[i].date_delete) {
  157. app.countOrders ++ ;
  158. }
  159. }
  160. }
  161. else {
  162. app.orders = [] ;
  163. }
  164. if(response.data.order_create) {
  165. app.orderCreate = response.data.order_create ;
  166. app.idDefaultPointSale = app.orderCreate.id_point_sale ;
  167. }
  168. if(response.data.points_sale) {
  169. app.pointsSale = response.data.points_sale ;
  170. app.initPointsSale(idActivePointSale) ;
  171. }
  172. else {
  173. app.pointsSale = [] ;
  174. }
  175. if(response.data.users) {
  176. app.users = response.data.users ;
  177. }
  178. if(response.data.delivery_notes) {
  179. app.deliveryNotes = response.data.delivery_notes ;
  180. }
  181. app.tillerIsSynchro = response.data.tiller_is_synchro ;
  182. app.calendar.attrs = [] ;
  183. var distributions = response.data.distributions ;
  184. if(distributions.length) {
  185. for(var i= 0; i < distributions.length; i++) {
  186. app.calendar.attrs.push({
  187. highlight: {
  188. backgroundColor: '#5cb85c',
  189. },
  190. contentStyle: {
  191. color: 'white',
  192. },
  193. dates: distributions[i].date
  194. }) ;
  195. }
  196. }
  197. app.showLoading = false ;
  198. app.checkboxSelectAllOrders = false ;
  199. let searchParams = new URLSearchParams(window.location.search) ;
  200. if(searchParams.has('message_generate_bl') && !app.messageGenerateDeliveryNoteDisplayed) {
  201. appAlerts.alert('info','Pour générer un bon de livraison, sélectionnez tout d\'abord un jour et un lieu de distribution.', 6000) ;
  202. app.messageGenerateDeliveryNoteDisplayed = true ;
  203. }
  204. if(app.idOrderUpdate) {
  205. app.updateOrderFromUrl();
  206. }
  207. }) ;
  208. },
  209. initCountActiveProducts: function() {
  210. this.countActiveProducts = 0 ;
  211. for(var i= 0; i < this.products.length; i++) {
  212. if(this.products[i].productDistribution[0].active == 1) {
  213. this.countActiveProducts ++ ;
  214. }
  215. }
  216. },
  217. initPointsSale: function(idActivePointSale) {
  218. this.countActivePointsSale = 0 ;
  219. this.setIdActivePointSale(0) ;
  220. for(var i= 0; i < this.pointsSale.length; i++) {
  221. if(this.pointsSale[i].pointSaleDistribution[0].delivery == 1) {
  222. this.countActivePointsSale ++ ;
  223. this.setIdActivePointSale(this.pointsSale[i].id) ;
  224. }
  225. }
  226. if(this.countActivePointsSale > 1) {
  227. this.setIdActivePointSale(0) ;
  228. }
  229. if(idActivePointSale) {
  230. this.setIdActivePointSale(idActivePointSale) ;
  231. }
  232. this.countOrdersByPointSale = [] ;
  233. for(var i = 0; i < this.pointsSale.length ; i++) {
  234. this.countOrdersByPointSale[this.pointsSale[i].id] = 0 ;
  235. }
  236. for(var i = 0; i < this.orders.length ; i++) {
  237. this.countOrdersByPointSale[this.orders[i].id_point_sale] ++ ;
  238. }
  239. },
  240. dayClicked: function(day) {
  241. this.date = day.date ;
  242. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  243. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  244. + this.date.getFullYear() ;
  245. this.init() ;
  246. },
  247. productQuantityMaxChange: function(event) {
  248. axios.get("ajax-process-product-quantity-max",{params: {
  249. idDistribution: this.distribution.id,
  250. idProduct: event.currentTarget.getAttribute('data-id-product'),
  251. quantityMax: event.currentTarget.value
  252. }})
  253. .then(function(response) {
  254. }) ;
  255. },
  256. productActiveClick: function(event) {
  257. var idProduct = event.currentTarget.getAttribute('data-id-product') ;
  258. var activeProduct = event.currentTarget.getAttribute('data-active-product') ;
  259. axios.get("ajax-process-active-product",{params: {
  260. idDistribution: this.distribution.id,
  261. idProduct: idProduct,
  262. active: activeProduct
  263. }})
  264. .then(function(response) {
  265. }) ;
  266. for(i = 0 ; i < this.products.length ; i++) {
  267. if(this.products[i].id == idProduct) {
  268. this.products[i].productDistribution[0].active = activeProduct ;
  269. }
  270. }
  271. this.initCountActiveProducts() ;
  272. },
  273. pointSaleActiveClick: function(event) {
  274. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  275. var deliveryPointSale = event.currentTarget.getAttribute('data-delivery-point-sale') ;
  276. axios.get("ajax-process-active-point-sale",{params: {
  277. idDistribution: this.distribution.id,
  278. idPointSale: idPointSale,
  279. delivery: deliveryPointSale
  280. }})
  281. .then(function(response) {
  282. }) ;
  283. for(i = 0 ; i < this.pointsSale.length ; i++) {
  284. if(this.pointsSale[i].id == idPointSale) {
  285. this.pointsSale[i].pointSaleDistribution[0].delivery = deliveryPointSale ;
  286. }
  287. }
  288. this.initPointsSale() ;
  289. },
  290. activeDistribution: function(event) {
  291. var app = this ;
  292. axios.get("ajax-process-active-distribution",{params: {
  293. idDistribution: this.distribution.id,
  294. active: event.currentTarget.getAttribute('data-active')
  295. }})
  296. .then(function(response) {
  297. app.init() ;
  298. }) ;
  299. },
  300. activeWeekDistribution: function(event) {
  301. var app = this ;
  302. axios.get("ajax-process-active-week-distribution",{params: {
  303. date: this.date.getFullYear() + '-'
  304. + ('0' + (this.date.getMonth() +1)).slice(-2) + '-'
  305. + ('0' + this.date.getDate()).slice(-2),
  306. active: event.currentTarget.getAttribute('data-active')
  307. }})
  308. .then(function(response) {
  309. app.init() ;
  310. }) ;
  311. },
  312. pointSaleClick: function(event) {
  313. this.setIdActivePointSale(event.currentTarget.getAttribute('data-id-point-sale')) ;
  314. },
  315. setIdActivePointSale: function(id) {
  316. this.idActivePointSale = id ;
  317. if(!id) {
  318. this.orderCreate.id_point_sale = this.idDefaultPointSale ;
  319. }
  320. else {
  321. this.orderCreate.id_point_sale = id ;
  322. }
  323. },
  324. orderCreatedUpdated: function() {
  325. this.showModalFormOrderCreate = false ;
  326. this.showModalFormOrderUpdate = false ;
  327. this.init(this.idActivePointSale) ;
  328. },
  329. deleteOrderClick: function(event) {
  330. var app = this ;
  331. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  332. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-delete",{params: {
  333. idOrder: idOrder
  334. }})
  335. .then(function(response) {
  336. app.init(app.idActivePointSale) ;
  337. }) ;
  338. },
  339. updateOrderFromUrl: function() {
  340. this.initModalFormOrder() ;
  341. this.updateProductOrderPrices(false);
  342. },
  343. updateOrderClick: function(event) {
  344. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  345. this.idOrderUpdate = idOrder ;
  346. this.showModalFormOrderUpdate = true ;
  347. this.initModalFormOrder() ;
  348. this.updateProductOrderPrices(false);
  349. },
  350. openModalFormOrderCreate: function() {
  351. this.showModalFormOrderCreate = true ;
  352. this.initModalFormOrder() ;
  353. this.updateProductOrderPrices(false) ;
  354. },
  355. initModalFormOrder: function() {
  356. var app = this;
  357. setTimeout(function() {
  358. $('.modal-body').css('height',$(window).height()) ;
  359. $('.modal-body').css('maxHeight','unset') ;
  360. $('.select2-order-form').select2({
  361. width: 'resolve'
  362. });
  363. $('.select2-order-form').on('select2:select', function (e) {
  364. var idUser = e.params.data.id;
  365. if(app.showModalFormOrderCreate) {
  366. Vue.set(app.orderCreate, 'id_user', idUser);
  367. }
  368. if(app.showModalFormOrderUpdate) {
  369. Vue.set(app.ordersUpdate[app.getOrderUpdateKey()], 'id_user', idUser);
  370. }
  371. });
  372. },500);
  373. },
  374. getOrderUpdateKey: function() {
  375. if (app.showModalFormOrderUpdate && app.idOrderUpdate) {
  376. for (keyOrderUpdate in app.ordersUpdate) {
  377. if (app.ordersUpdate[keyOrderUpdate].id == app.idOrderUpdate) {
  378. return keyOrderUpdate;
  379. }
  380. }
  381. }
  382. },
  383. orderPaymentModalClick: function(event) {
  384. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  385. this.idOrderPayment = idOrder ;
  386. this.showModalPayment = true ;
  387. },
  388. orderPaymentClick: function(event) {
  389. var app = this ;
  390. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  391. if(!idOrder) {
  392. idOrder = this.idOrderPayment ;
  393. }
  394. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-payment",{params: {
  395. idOrder: idOrder,
  396. type: event.currentTarget.getAttribute('data-type'),
  397. amount: event.currentTarget.getAttribute('data-amount')
  398. }})
  399. .then(function(response) {
  400. app.init(app.idActivePointSale) ;
  401. }) ;
  402. },
  403. orderViewClick: function(event) {
  404. var currentIdOrderView = event.currentTarget.getAttribute('data-id-order') ;
  405. if(this.idOrderView == currentIdOrderView) {
  406. this.showViewProduct = !this.showViewProduct ;
  407. }
  408. else {
  409. this.showViewProduct = true ;
  410. this.idOrderView = currentIdOrderView ;
  411. }
  412. },
  413. closeModalProducts: function() {
  414. this.showModalProducts = false ;
  415. this.init(this.idActivePointSale) ;
  416. },
  417. cloneOrder: function(order) {
  418. var clone = Object.assign({}, order) ;
  419. clone.productOrder = {} ;
  420. for(var key in order.productOrder) {
  421. clone.productOrder[key] = order.productOrder[key] ;
  422. }
  423. return clone ;
  424. },
  425. addSubscriptions: function() {
  426. var app = this ;
  427. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-process-add-subscriptions",{params: {
  428. date: this.getDate()
  429. }})
  430. .then(function(response) {
  431. app.init(app.idActivePointSale) ;
  432. }) ;
  433. },
  434. synchroTiller: function() {
  435. var app = this ;
  436. this.showLoading = true ;
  437. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-process-synchro-tiller",{params: {
  438. date: this.getDate()
  439. }})
  440. .then(function(response) {
  441. app.init(app.idActivePointSale) ;
  442. }) ;
  443. },
  444. totalActivePointSale: function() {
  445. var total = 0 ;
  446. for(var i = 0; i < this.orders.length ; i++) {
  447. if(this.orders[i].id_point_sale == this.idActivePointSale) {
  448. total += parseFloat(this.orders[i].amount) ;
  449. }
  450. }
  451. return total.toFixed(2);
  452. },
  453. weightActivePointSale: function() {
  454. var weight = 0 ;
  455. for(var i = 0; i < this.orders.length ; i++) {
  456. if(this.orders[i].id_point_sale == this.idActivePointSale) {
  457. weight += parseFloat(this.orders[i].weight) ;
  458. }
  459. }
  460. return weight.toFixed(2) ;
  461. },
  462. changeSynchroTiller: function(event) {
  463. var app = this ;
  464. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  465. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-change-synchro-tiller",{params: {
  466. idOrder: idOrder,
  467. boolSynchroTiller: event.currentTarget.checked ? 1 : 0
  468. }})
  469. .then(function(response) {
  470. app.init() ;
  471. }) ;
  472. },
  473. selectAllOrdersEvent: function(event) {
  474. var bool = event.currentTarget.checked ;
  475. this.selectAllOrders(bool) ;
  476. },
  477. selectAllOrders: function(bool) {
  478. for(var key in this.orders) {
  479. if(this.orders[key].id_point_sale == this.idActivePointSale) {
  480. this.orders[key].selected = bool ;
  481. }
  482. }
  483. },
  484. oneOrderSelected: function() {
  485. for(var key in this.orders) {
  486. if(this.orders[key].selected == true) {
  487. return true ;
  488. }
  489. }
  490. return false ;
  491. },
  492. generateDeliveryNote: function() {
  493. if(!this.oneOrderSelected()) {
  494. this.selectAllOrders(true) ;
  495. }
  496. if(this.oneOrderSelected()) {
  497. var app = this ;
  498. var idOrders = {} ;
  499. for(var key in this.orders) {
  500. if(this.orders[key].selected == true) {
  501. idOrders[key] = this.orders[key].id ;
  502. }
  503. }
  504. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-generate-delivery-note",{params: {
  505. idOrders: JSON.stringify(idOrders)
  506. }})
  507. .then(function(response) {
  508. appAlerts.alertResponse(response) ;
  509. app.init(app.idActivePointSale) ;
  510. }) ;
  511. }
  512. else {
  513. appAlerts.alert('danger','Veuillez sélectionner au moins une commande pour générer un bon de livraison') ;
  514. }
  515. },
  516. generateDeliveryNoteEachUser: function() {
  517. if(!this.oneOrderSelected()) {
  518. this.selectAllOrders(true) ;
  519. }
  520. if(this.oneOrderSelected()) {
  521. var app = this ;
  522. var idOrders = {} ;
  523. for(var key in this.orders) {
  524. if(this.orders[key].selected == true) {
  525. idOrders[key] = this.orders[key].id ;
  526. }
  527. }
  528. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-generate-delivery-note-each-user",{params: {
  529. idOrders: JSON.stringify(idOrders)
  530. }})
  531. .then(function(response) {
  532. appAlerts.alertResponse(response) ;
  533. app.init(app.idActivePointSale) ;
  534. }) ;
  535. }
  536. else {
  537. appAlerts.alert('danger','Veuillez sélectionner au moins une commande pour générer un bon de livraison') ;
  538. }
  539. },
  540. validateDeliveryNotes: function() {
  541. if(!this.oneOrderSelected()) {
  542. this.selectAllOrders(true) ;
  543. }
  544. if(this.oneOrderSelected()) {
  545. var app = this ;
  546. var idOrders = {} ;
  547. for(var key in this.orders) {
  548. if(this.orders[key].selected == true) {
  549. idOrders[key] = this.orders[key].id ;
  550. }
  551. }
  552. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-validate-delivery-notes",{params: {
  553. idOrders: JSON.stringify(idOrders)
  554. }})
  555. .then(function(response) {
  556. appAlerts.alertResponse(response) ;
  557. app.init(app.idActivePointSale) ;
  558. }) ;
  559. }
  560. else {
  561. appAlerts.alert('danger','Veuillez sélectionner au moins une commande pour valider un bon de livraison') ;
  562. }
  563. },
  564. deliveryNoteExist: function(idPointSale) {
  565. return typeof this.deliveryNotes[this.idActivePointSale] != 'undefined' ;
  566. },
  567. payOrders: function() {
  568. var app = this ;
  569. axios.get(UrlManager.getBaseUrlAbsolute()+"cron/pay-orders",{params: {
  570. date: app.getDate()
  571. }})
  572. .then(function(response) {
  573. appAlerts.alertResponse(response) ;
  574. app.init(app.idActivePointSale) ;
  575. }) ;
  576. },
  577. updateProductOrderPrices: function(updatePricesOnUpdateOrder) {
  578. var app = this ;
  579. app.loadingUpdateProductOrder = true;
  580. var order = null ;
  581. if(app.showModalFormOrderCreate) {
  582. order = app.orderCreate ;
  583. }
  584. if(app.showModalFormOrderUpdate && app.idOrderUpdate) {
  585. for (keyOrderUpdate in app.ordersUpdate) {
  586. if (app.ordersUpdate[keyOrderUpdate].id == app.idOrderUpdate) {
  587. order = app.ordersUpdate[keyOrderUpdate] ;
  588. }
  589. }
  590. }
  591. if(order) {
  592. axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-update-product-order", {
  593. params: {
  594. idDistribution: app.distribution.id,
  595. idUser: order.id_user,
  596. idPointSale: order.id_point_sale,
  597. idOrder: order.id
  598. }
  599. })
  600. .then(function (response) {
  601. if (response.data) {
  602. for (idProduct in response.data) {
  603. if (app.showModalFormOrderCreate) {
  604. Vue.set(app.orderCreate.productOrder[idProduct], 'prices', response.data[idProduct].prices);
  605. Vue.set(app.orderCreate.productOrder[idProduct], 'active', response.data[idProduct].active);
  606. Vue.set(app.orderCreate.productOrder[idProduct], 'unit_coefficient', response.data[idProduct].unit_coefficient);
  607. Vue.set(app.orderCreate.productOrder[idProduct], 'price', app.getBestProductPrice(app.orderCreate, idProduct, app.orderCreate.productOrder[idProduct].quantity, false));
  608. Vue.set(app.orderCreate.productOrder[idProduct], 'price_with_tax', app.getBestProductPrice(app.orderCreate, idProduct, app.orderCreate.productOrder[idProduct].quantity, true));
  609. }
  610. if (app.showModalFormOrderUpdate && app.idOrderUpdate) {
  611. for (keyOrderUpdate in app.ordersUpdate) {
  612. if (order.id == app.idOrderUpdate) {
  613. Vue.set(app.ordersUpdate[keyOrderUpdate].productOrder[idProduct], 'prices', response.data[idProduct].prices);
  614. Vue.set(app.ordersUpdate[keyOrderUpdate].productOrder[idProduct], 'active', response.data[idProduct].active);
  615. Vue.set(app.ordersUpdate[keyOrderUpdate].productOrder[idProduct], 'unit_coefficient', response.data[idProduct].unit_coefficient);
  616. Vue.set(app.ordersUpdate[keyOrderUpdate].productOrder[idProduct], 'invoice_price', response.data[idProduct].invoice_price);
  617. if(updatePricesOnUpdateOrder) {
  618. Vue.set(
  619. app.ordersUpdate[keyOrderUpdate].productOrder[idProduct],
  620. 'price',
  621. app.getBestProductPrice(app.ordersUpdate[keyOrderUpdate], idProduct, app.ordersUpdate[keyOrderUpdate].productOrder[idProduct].quantity, false));
  622. Vue.set(
  623. app.ordersUpdate[keyOrderUpdate].productOrder[idProduct],
  624. 'price_with_tax',
  625. app.getBestProductPrice(app.ordersUpdate[keyOrderUpdate], idProduct, app.ordersUpdate[keyOrderUpdate].productOrder[idProduct].quantity, true));
  626. }
  627. }
  628. }
  629. }
  630. }
  631. if(updatePricesOnUpdateOrder) {
  632. appAlerts.alert('info','Prix rechargés') ;
  633. }
  634. }
  635. app.loadingUpdateProductOrder = false;
  636. });
  637. }
  638. },
  639. updateInvoicePrices: function() {
  640. var order = null;
  641. if(app.showModalFormOrderUpdate && app.idOrderUpdate) {
  642. for (keyOrderUpdate in app.ordersUpdate) {
  643. if (app.ordersUpdate[keyOrderUpdate].id == app.idOrderUpdate) {
  644. order = app.ordersUpdate[keyOrderUpdate] ;
  645. }
  646. }
  647. }
  648. if(order) {
  649. axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-update-invoice-prices", {
  650. params: {
  651. idOrder: order.id
  652. }
  653. })
  654. .then(function (response) {
  655. app.updateProductOrderPrices(false);
  656. appAlerts.alert('info','Prix facturés réinitialisés.') ;
  657. });
  658. }
  659. },
  660. getBestProductPrice: function(order, idProduct, theQuantity, withTax) {
  661. var thePrice = 9999;
  662. var pricesArray = order.productOrder[idProduct].prices;
  663. var unitCoefficient = order.productOrder[idProduct].unit_coefficient;
  664. if(theQuantity) {
  665. theQuantity = theQuantity / unitCoefficient;
  666. }
  667. if(pricesArray) {
  668. for(var i = 0; i < pricesArray.length ; i++) {
  669. var price = pricesArray[i].price;
  670. if(withTax) {
  671. price = pricesArray[i].price_with_tax
  672. }
  673. var fromQuantity = pricesArray[i].from_quantity;
  674. if(price < thePrice && fromQuantity <= theQuantity) {
  675. thePrice = price;
  676. }
  677. }
  678. }
  679. else {
  680. var product = this.getProduct(idProduct);
  681. if(withTax) {
  682. thePrice = getPriceWithTax(product.price, product.taxRate.value);
  683. }
  684. else {
  685. thePrice = product.price;
  686. }
  687. }
  688. if(thePrice == 9999) {
  689. return 0;
  690. }
  691. else {
  692. return thePrice;
  693. }
  694. },
  695. getProduct: function(idProduct) {
  696. for(var i= 0; i < this.products.length; i++) {
  697. if(this.products[i].id == idProduct) {
  698. return this.products[i];
  699. }
  700. }
  701. return false;
  702. },
  703. },
  704. });
  705. Vue.component('modal', {
  706. template: '#modal-template'
  707. })
  708. Vue.component('order-form',{
  709. props: ['date', 'dateFormat', 'pointsSale', 'idActivePointSale', 'meansPayment', 'users', 'products', 'order', 'producer', 'loadingUpdateProductOrder'],
  710. emits: ['updateProductPrice', 'updateInvoicePrices'],
  711. data: function() {
  712. return {
  713. errors: [],
  714. idPointSale: 0,
  715. idUser: 0,
  716. username : '',
  717. comment: '',
  718. baseUrl: $('meta[name=baseurl]').attr('content'),
  719. vatMode: 'all' // 'with_tax'
  720. } ;
  721. },
  722. template: '#order-form-template',
  723. methods: {
  724. checkForm: function() {
  725. this.errors = [] ;
  726. var countProducts = 0 ;
  727. for(var key in this.order.productOrder) {
  728. if(this.order.productOrder[key].quantity > 0) {
  729. countProducts ++ ;
  730. }
  731. }
  732. if(this.order.id_point_sale
  733. && (this.order.id_user > 0 || (this.order.username && this.order.username.length))
  734. && countProducts > 0) {
  735. return true ;
  736. }
  737. if(!this.order.id_point_sale) {
  738. this.errors.push('Veuillez sélectionner un point de vente') ;
  739. }
  740. if((!this.order.id_user || this.order.id_user == 0) && !this.order.username.length) {
  741. this.errors.push('Veuillez sélectionner ou saisir un utilisateur') ;
  742. }
  743. if(!countProducts) {
  744. this.errors.push('Veuillez sélectionner au moins un produit') ;
  745. }
  746. },
  747. getProductOrderArrayRequest: function() {
  748. var productOrderArrayRequest = {};
  749. for(var key in this.order.productOrder) {
  750. productOrderArrayRequest[key] = {
  751. quantity: this.order.productOrder[key].quantity,
  752. unit: this.order.productOrder[key].unit,
  753. price: this.order.productOrder[key].price
  754. };
  755. }
  756. return JSON.stringify(productOrderArrayRequest);
  757. },
  758. submitFormCreate: function(event) {
  759. var app = this ;
  760. if(this.checkForm()) {
  761. var processCredit = event.currentTarget.getAttribute('data-process-credit') ;
  762. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-create",{params: {
  763. date: this.date.getFullYear() + '-'
  764. + ('0' + (this.date.getMonth() +1)).slice(-2) + '-'
  765. + ('0' + this.date.getDate()).slice(-2),
  766. idPointSale: this.order.id_point_sale,
  767. idUser: this.order.id_user,
  768. username: this.order.username,
  769. meanPayment: this.order.mean_payment,
  770. products: app.getProductOrderArrayRequest(),
  771. comment: this.order.comment,
  772. processCredit: processCredit
  773. }})
  774. .then(function(response) {
  775. app.order.id_point_sale = 0 ;
  776. app.order.id_user = 0 ;
  777. app.order.username = '' ;
  778. app.order.comment = '' ;
  779. for(i=0 ; i<app.order.productOrder.length ; i++) {
  780. app.order.productOrder[i] = 0 ;
  781. }
  782. app.$emit('ordercreatedupdated') ;
  783. }) ;
  784. }
  785. },
  786. submitFormUpdate: function(event) {
  787. var app = this ;
  788. if(this.checkForm()) {
  789. var processCredit = event.currentTarget.getAttribute('data-process-credit') ;
  790. if(processCredit == null) {
  791. processCredit = 0 ;
  792. }
  793. var data = new FormData();
  794. data.append('date', this.date.getFullYear() + '-'
  795. + ('0' + (this.date.getMonth() +1)).slice(-2) + '-'
  796. + ('0' + this.date.getDate()).slice(-2));
  797. data.append('idOrder', this.order.id) ;
  798. data.append('idPointSale', this.order.id_point_sale) ;
  799. data.append('meanPayment', this.order.mean_payment) ;
  800. data.append('idUser', this.order.id_user) ;
  801. data.append('username', ''+this.order.username) ;
  802. data.append('products', app.getProductOrderArrayRequest()) ;
  803. data.append('comment', this.order.comment && this.order.comment.length ? this.order.comment : '') ;
  804. data.append('processCredit', processCredit) ;
  805. axios.post(UrlManager.getBaseUrlAbsolute()+"order/ajax-update",data)
  806. .then(function(response) {
  807. app.$emit('ordercreatedupdated') ;
  808. }) ;
  809. }
  810. },
  811. productQuantityClick: function(id_product, quantity) {
  812. if(!this.order.productOrder[id_product].quantity) {
  813. this.order.productOrder[id_product].quantity = 0 ;
  814. }
  815. if(parseFloat(this.order.productOrder[id_product].quantity) + quantity >= 0) {
  816. var theQuantity = (parseFloat(this.order.productOrder[id_product].quantity) + parseFloat(quantity)).toFixed(2);
  817. var theQuantityDecimal = theQuantity % 1;
  818. if(theQuantityDecimal == 0) {
  819. theQuantity = parseInt(theQuantity);
  820. }
  821. Vue.set(this.order.productOrder[id_product], 'quantity', theQuantity);
  822. Vue.set(this.order.productOrder[id_product], 'price', app.getBestProductPrice(this.order, id_product, theQuantity, false));
  823. Vue.set(this.order.productOrder[id_product], 'price_with_tax', app.getBestProductPrice(this.order, id_product, theQuantity, true));
  824. }
  825. },
  826. getProduct: function(idProduct) {
  827. for(var i= 0; i < this.products.length; i++) {
  828. if(this.products[i].id == idProduct) {
  829. return this.products[i];
  830. }
  831. }
  832. return false;
  833. },
  834. productPriceChange: function(event) {
  835. var idProduct = event.currentTarget.getAttribute('data-id-product');
  836. var product = this.getProduct(idProduct);
  837. if(product) {
  838. var taxRateValue = parseFloat(product.taxRate.value);
  839. var withTax = event.currentTarget.getAttribute('data-with-tax');
  840. var price = 0;
  841. var priceWithTax = 0;
  842. var priceValue = parseFloat(event.currentTarget.value.replace(',', '.'));
  843. if(withTax) {
  844. priceWithTax = priceValue.toFixed(2);
  845. price = getPrice(priceWithTax, taxRateValue);
  846. }
  847. else {
  848. price = priceValue.toFixed(3);
  849. priceWithTax = getPriceWithTax(price, taxRateValue);
  850. }
  851. if(isNaN(price)) {
  852. price = 0 ;
  853. }
  854. if(isNaN(priceWithTax)) {
  855. priceWithTax = 0 ;
  856. }
  857. Vue.set(this.order.productOrder, idProduct, {
  858. active: this.order.productOrder[idProduct].active,
  859. quantity: this.order.productOrder[idProduct].quantity,
  860. unit: this.order.productOrder[idProduct].unit,
  861. price: price,
  862. price_with_tax: priceWithTax
  863. });
  864. }
  865. },
  866. userChange: function(event) {
  867. var app = this ;
  868. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-point-sale-favorite",{params: {
  869. idUser: app.order.id_user,
  870. }})
  871. .then(function(response) {
  872. if(app.idActivePointSale == 0) {
  873. app.order.id_point_sale = response.data.id_favorite_point_sale ;
  874. }
  875. app.updateProductOrderPrices(true) ;
  876. }) ;
  877. },
  878. pointSaleChange: function(event) {
  879. this.updateProductOrderPrices(true) ;
  880. },
  881. updateProductOrderPrices: function(updateProductOrderPrices) {
  882. this.$emit('updateproductorderprices', updateProductOrderPrices);
  883. },
  884. updateInvoicePrices: function() {
  885. this.$emit('updateinvoiceprices');
  886. },
  887. toggleVatMode: function() {
  888. if(this.vatMode == 'all') {
  889. this.vatMode = 'with_tax';
  890. }
  891. else {
  892. this.vatMode = 'all';
  893. }
  894. }
  895. }
  896. }) ;