cart.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. const state = {
  2. cartItems: [],
  3. cartTotal: 0,
  4. itemToDiscount: null
  5. }
  6. const getters = {
  7. cartItems(state, getters) {
  8. if (state.cartItems.length > 0) {
  9. for (let item of state.cartItems) {
  10. if (!!item.packs) {
  11. continue
  12. }
  13. item.packs = []
  14. for (let pack of getters.packs) {
  15. if (item.id !== pack.productId) {
  16. continue
  17. }
  18. item.packs.push(pack)
  19. }
  20. }
  21. }
  22. return state.cartItems
  23. },
  24. cartTotal(state) {
  25. return state.cartTotal
  26. },
  27. itemToDiscount(state) {
  28. return state.itemToDiscount
  29. }
  30. }
  31. const mutations = {
  32. setCart (state, payload) {
  33. state.cartItems = payload
  34. },
  35. pushToCart (state, payload) {
  36. let productFound = state.cartItems.find(item => item.id === payload.id)
  37. if (productFound) {
  38. if (payload.quantity > 1) {
  39. if (productFound.quantity === payload.quantity) {
  40. productFound.quantity = productFound.quantity + 1
  41. } else {
  42. productFound.quantity = payload.quantity
  43. }
  44. } else {
  45. productFound.quantity = productFound.quantity + payload.quantity
  46. }
  47. return
  48. }
  49. // console.log(getters)
  50. state.cartItems = [payload, ...state.cartItems]
  51. },
  52. pullFromCart (state, payload) {
  53. let productFoundIndex = state.cartItems.findIndex(item => item.id === payload.item.id)
  54. if (state.cartItems[productFoundIndex].quantity === 1) {
  55. payload.mode = 'full'
  56. }
  57. if (payload.mode === 'partial') {
  58. if (payload.item.quantity !== -1) {
  59. state.cartItems[productFoundIndex].quantity = payload.item.quantity
  60. } else {
  61. state.cartItems[productFoundIndex].quantity = state.cartItems[productFoundIndex].quantity - 1
  62. }
  63. } else {
  64. state.cartItems.splice(productFoundIndex, 1)
  65. }
  66. },
  67. setCartTotal (state, payload) {
  68. state.cartTotal = payload
  69. },
  70. setItemToDiscount (state, payload) {
  71. state.itemToDiscount = payload
  72. },
  73. setItemPrice (state, payload) {
  74. let foundProduct = state.cartItems.find(item => item.id === state.itemToDiscount.id)
  75. foundProduct.price = payload
  76. },
  77. resetPrice (state, payload) {
  78. let foundProduct = state.cartItems.find(item => item.id === payload.id)
  79. foundProduct.price = foundProduct.listPrice
  80. }
  81. }
  82. const actions = {
  83. addToCart ({ commit }, payload) {
  84. commit('pushToCart', payload)
  85. },
  86. decreaseFromCart ({ commit }, payload) {
  87. commit('pullFromCart', {
  88. item: payload,
  89. mode: 'partial'
  90. })
  91. },
  92. undoPrice ({ commit }, payload) {
  93. commit('resetPrice', payload)
  94. },
  95. changePrice ({ commit, getters, dispatch }, payload) {
  96. commit('setItemToDiscount', payload)
  97. if (getters.pricelistsForProduct.length === 0) {
  98. dispatch('notify', 'Este producto no posee lista de precios')
  99. return
  100. }
  101. dispatch('togglePricelists')
  102. },
  103. applyPrice ({ commit, dispatch }, payload) {
  104. if (payload) {
  105. commit('setItemPrice', payload)
  106. }
  107. commit('setItemToDiscount', null)
  108. dispatch('togglePricelists')
  109. },
  110. removeFromCart ({ commit }, payload) {
  111. commit('pullFromCart', {
  112. item: payload,
  113. mode: 'full'
  114. })
  115. },
  116. changeCartTotal ({ commit, dispatch }, payload) {
  117. commit('setCartTotal', payload)
  118. dispatch('changeAmountToPay', payload)
  119. },
  120. updateFromSaleOrder({ commit, dispatch }, saleOrder) {
  121. dispatch('resetCart', [])
  122. if (!saleOrder) {
  123. return
  124. }
  125. let total = 0
  126. for (let line of saleOrder.lines) {
  127. total = total + (line.price * line.quantity)
  128. commit('pushToCart', line)
  129. }
  130. dispatch('changeCartTotal', total)
  131. },
  132. resetCart ({ commit }) {
  133. commit('setCart', [])
  134. commit('setCartTotal', 0)
  135. }
  136. }
  137. export default {
  138. state,
  139. getters,
  140. mutations,
  141. actions
  142. }