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ů.

distribution-index.js 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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.countOrdersByPointSale = [] ;
  219. for(var i = 0; i < this.pointsSale.length ; i++) {
  220. this.countOrdersByPointSale[this.pointsSale[i].id] = 0 ;
  221. }
  222. for(var i = 0; i < this.orders.length ; i++) {
  223. this.countOrdersByPointSale[this.orders[i].id_point_sale] ++ ;
  224. }
  225. this.countActivePointsSale = 0 ;
  226. this.setIdActivePointSale(0) ;
  227. for(var i= 0; i < this.pointsSale.length; i++) {
  228. if(this.pointsSale[i].pointSaleDistribution[0].delivery == 1 && (this.pointsSale[i].status == 1 || this.countOrdersByPointSale[this.pointsSale[i].id] > 0)) {
  229. this.countActivePointsSale ++ ;
  230. this.setIdActivePointSale(this.pointsSale[i].id) ;
  231. }
  232. }
  233. if(this.countActivePointsSale > 1) {
  234. this.setIdActivePointSale(0) ;
  235. }
  236. if(idActivePointSale) {
  237. this.setIdActivePointSale(idActivePointSale) ;
  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. }})
  773. .then(function(response) {
  774. app.order.id_point_sale = 0 ;
  775. app.order.id_user = 0 ;
  776. app.order.username = '' ;
  777. app.order.comment = '' ;
  778. for(i=0 ; i<app.order.productOrder.length ; i++) {
  779. app.order.productOrder[i] = 0 ;
  780. }
  781. app.$emit('ordercreatedupdated') ;
  782. }) ;
  783. }
  784. },
  785. submitFormUpdate: function(event) {
  786. var app = this ;
  787. if(this.checkForm()) {
  788. var processCredit = event.currentTarget.getAttribute('data-process-credit') ;
  789. if(processCredit == null) {
  790. processCredit = 0 ;
  791. }
  792. var data = new FormData();
  793. data.append('date', this.date.getFullYear() + '-'
  794. + ('0' + (this.date.getMonth() +1)).slice(-2) + '-'
  795. + ('0' + this.date.getDate()).slice(-2));
  796. data.append('idOrder', this.order.id) ;
  797. data.append('idPointSale', this.order.id_point_sale) ;
  798. data.append('meanPayment', this.order.mean_payment) ;
  799. data.append('idUser', this.order.id_user) ;
  800. data.append('username', ''+this.order.username) ;
  801. data.append('products', app.getProductOrderArrayRequest()) ;
  802. data.append('comment', this.order.comment && this.order.comment.length ? this.order.comment : '') ;
  803. data.append('processCredit', processCredit) ;
  804. axios.post(UrlManager.getBaseUrlAbsolute()+"order/ajax-update",data)
  805. .then(function(response) {
  806. app.$emit('ordercreatedupdated') ;
  807. }) ;
  808. }
  809. },
  810. productQuantityClick: function(id_product, quantity) {
  811. if(!this.order.productOrder[id_product].quantity) {
  812. this.order.productOrder[id_product].quantity = 0 ;
  813. }
  814. if(parseFloat(this.order.productOrder[id_product].quantity) + quantity >= 0) {
  815. var theQuantity = (parseFloat(this.order.productOrder[id_product].quantity) + parseFloat(quantity)).toFixed(2);
  816. var theQuantityDecimal = theQuantity % 1;
  817. if(theQuantityDecimal == 0) {
  818. theQuantity = parseInt(theQuantity);
  819. }
  820. Vue.set(this.order.productOrder[id_product], 'quantity', theQuantity);
  821. Vue.set(this.order.productOrder[id_product], 'price', app.getBestProductPrice(this.order, id_product, theQuantity, false));
  822. Vue.set(this.order.productOrder[id_product], 'price_with_tax', app.getBestProductPrice(this.order, id_product, theQuantity, true));
  823. }
  824. },
  825. getProduct: function(idProduct) {
  826. for(var i= 0; i < this.products.length; i++) {
  827. if(this.products[i].id == idProduct) {
  828. return this.products[i];
  829. }
  830. }
  831. return false;
  832. },
  833. productPriceChange: function(event) {
  834. var idProduct = event.currentTarget.getAttribute('data-id-product');
  835. var product = this.getProduct(idProduct);
  836. if(product) {
  837. var taxRateValue = parseFloat(product.taxRate.value);
  838. var withTax = event.currentTarget.getAttribute('data-with-tax');
  839. var price = 0;
  840. var priceWithTax = 0;
  841. var priceValue = parseFloat(event.currentTarget.value.replace(',', '.'));
  842. if(withTax) {
  843. priceWithTax = priceValue.toFixed(2);
  844. price = getPrice(priceWithTax, taxRateValue);
  845. }
  846. else {
  847. price = priceValue.toFixed(3);
  848. priceWithTax = getPriceWithTax(price, taxRateValue);
  849. }
  850. if(isNaN(price)) {
  851. price = 0 ;
  852. }
  853. if(isNaN(priceWithTax)) {
  854. priceWithTax = 0 ;
  855. }
  856. Vue.set(this.order.productOrder, idProduct, {
  857. active: this.order.productOrder[idProduct].active,
  858. quantity: this.order.productOrder[idProduct].quantity,
  859. unit: this.order.productOrder[idProduct].unit,
  860. price: price,
  861. price_with_tax: priceWithTax
  862. });
  863. }
  864. },
  865. userChange: function(event) {
  866. var app = this ;
  867. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-point-sale-favorite",{params: {
  868. idUser: app.order.id_user,
  869. }})
  870. .then(function(response) {
  871. if(app.idActivePointSale == 0) {
  872. app.order.id_point_sale = response.data.id_favorite_point_sale ;
  873. }
  874. app.updateProductOrderPrices(true) ;
  875. }) ;
  876. },
  877. pointSaleChange: function(event) {
  878. this.updateProductOrderPrices(true) ;
  879. },
  880. updateProductOrderPrices: function(updateProductOrderPrices) {
  881. this.$emit('updateproductorderprices', updateProductOrderPrices);
  882. },
  883. updateInvoicePrices: function() {
  884. this.$emit('updateinvoiceprices');
  885. },
  886. toggleVatMode: function() {
  887. if(this.vatMode == 'all') {
  888. this.vatMode = 'with_tax';
  889. }
  890. else {
  891. this.vatMode = 'all';
  892. }
  893. }
  894. }
  895. }) ;