cart.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. state.cartItems = [payload, ...state.cartItems]
  36. },
  37. pullFromCart (state, payload) {
  38. let productFoundIndex = state.cartItems.findIndex(item => item.id === payload.item.id)
  39. if (state.cartItems[productFoundIndex].quantity === 1) {
  40. payload.mode = 'full'
  41. }
  42. if (payload.mode === 'partial') {
  43. if (payload.item.quantity !== -1) {
  44. state.cartItems[productFoundIndex].quantity = payload.item.quantity
  45. } else {
  46. state.cartItems[productFoundIndex].quantity = state.cartItems[productFoundIndex].quantity - 1
  47. }
  48. } else {
  49. state.cartItems.splice(productFoundIndex, 1)
  50. }
  51. },
  52. setCartTotal (state, payload) {
  53. state.cartTotal = payload
  54. },
  55. setItemToDiscount (state, payload) {
  56. state.itemToDiscount = payload
  57. },
  58. setItemPrice (state, payload) {
  59. let foundProduct = state.cartItems.find(item => item.id === state.itemToDiscount.id)
  60. foundProduct.price = payload
  61. },
  62. resetPrice (state, payload) {
  63. let foundProduct = state.cartItems.find(item => item.id === payload.id)
  64. foundProduct.price = foundProduct.listPrice
  65. }
  66. }
  67. const actions = {
  68. addToCart ({ commit }, payload) {
  69. commit('pushToCart', payload)
  70. },
  71. decreaseFromCart ({ commit }, payload) {
  72. commit('pullFromCart', {
  73. item: payload,
  74. mode: 'partial'
  75. })
  76. },
  77. undoPrice ({ commit }, payload) {
  78. commit('resetPrice', payload)
  79. },
  80. changePrice ({ commit }, payload) {
  81. commit('setItemToDiscount', payload)
  82. },
  83. applyPrice ({ commit }, payload) {
  84. if (payload) {
  85. commit('setItemPrice', payload)
  86. }
  87. commit('setItemToDiscount', null)
  88. },
  89. removeFromCart ({ commit }, payload) {
  90. commit('pullFromCart', {
  91. item: payload,
  92. mode: 'full'
  93. })
  94. },
  95. changeCartTotal ({ commit, dispatch }, payload) {
  96. commit('setCartTotal', payload)
  97. dispatch('changeAmountToPay', payload)
  98. },
  99. updateFromSaleOrder({ commit, dispatch }, saleOrder) {
  100. dispatch('resetCart', [])
  101. if (!saleOrder) {
  102. return
  103. }
  104. let total = 0
  105. for (let line of saleOrder.lines) {
  106. total = total + (line.price * line.quantity)
  107. commit('pushToCart', line)
  108. }
  109. dispatch('changeCartTotal', total)
  110. },
  111. resetCart ({ commit }) {
  112. commit('setCart', [])
  113. commit('setCartTotal', 0)
  114. }
  115. }
  116. export default {
  117. state,
  118. getters,
  119. mutations,
  120. actions
  121. }