cart.js 3.8 KB

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