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.

807 line
33KB

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