moveLines.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { SELECT_MOVE_INVOICE, SELECT_MOVE, REMOVE_MOVE_PAYMENTS, ADD_MOVE_IN_INVOICE, REMOVE_MOVE_PAYMENTS_ALL } from '@/constants/actionTypes'
  2. import { SET_MOVES, SET_SELECTED_MOVE_LINE, SET_SELECTED_MOVE_PAYMENTS, REMOVE_MOVE_LINE, REMOVE_PAYMENTS_MOVE, SET_TOTAL_MOVE_PAYMENTS } from '@/constants/mutationTypes'
  3. const initialState = {
  4. moves : null,
  5. selectedMoveLine: null,
  6. movesPayments: [],
  7. movesPaymentsTotal: 0,
  8. movesPaymentsBalance: 0,
  9. }
  10. const state = {
  11. moves: initialState.moves,
  12. selectedMoveLine: initialState.selectedMoveLine,
  13. movesPayments: initialState.movesPayments,
  14. movesPaymentsTotal: initialState.movesPaymentsTotal,
  15. movesPaymentsBalance: initialState.movesPaymentsBalance,
  16. }
  17. const getters = {
  18. /**
  19. * [moves description]
  20. * @param {[type]} state [description]
  21. * @return {[type]} [description]
  22. */
  23. moves (state) {
  24. return state.moves
  25. },
  26. /**
  27. * [selectedMoveLine description]
  28. * @param {[type]} state [description]
  29. * @return {[type]} [description]
  30. */
  31. selectedMoveLine (state){
  32. return state.selectedMoveLine
  33. },
  34. /**
  35. * [movesPayments description]
  36. * @param {[type]} state [description]
  37. * @return {[type]} [description]
  38. */
  39. movesPayments (state) {
  40. return state.movesPayments
  41. },
  42. /**
  43. * [movesPaymentsTotal description]
  44. * @param {[type]} state [description]
  45. * @return {[type]} [description]
  46. */
  47. movesPaymentsTotal (state) {
  48. return state.movesPaymentsTotal
  49. },
  50. /**
  51. * [movesPaymentsBalance description]
  52. * @param {[type]} state [description]
  53. * @return {[type]} [description]
  54. */
  55. movesPaymentsBalance (state) {
  56. return state.movesPaymentsBalance
  57. }
  58. }
  59. const mutations = {
  60. /**
  61. * [moves description]
  62. * @type {[type]}
  63. * @param {[type]} state [description]
  64. * @param {[type]} payload [description]
  65. */
  66. [SET_MOVES] (state, payload) {
  67. state.moves = payload
  68. },
  69. /**
  70. * [selectedMoveLine description]
  71. * @type {[type]}
  72. * @param {[type]} state [description]
  73. * @param {[type]} payload [description]
  74. */
  75. [SET_SELECTED_MOVE_LINE] (state, payload) {
  76. state.selectedMoveLine = payload
  77. },
  78. /**
  79. * [movesPayments description]
  80. * @type {[type]}
  81. * @param {[type]} state [description]
  82. * @param {[type]} payload [description]
  83. */
  84. [SET_SELECTED_MOVE_PAYMENTS] (state, payload) {
  85. state.movesPayments = [payload, ...state.movesPayments]
  86. },
  87. /**
  88. * [actions description]
  89. * @type {Object}
  90. * @param {[type]} state [description]
  91. * @param {[type]} payload [description]
  92. */
  93. [REMOVE_MOVE_LINE] (state, payload) {
  94. let moveFoundIndex = state.moves.findIndex(item => item.id === payload.id)
  95. state.moves.splice(moveFoundIndex, 1)
  96. },
  97. /**
  98. * [actions description]
  99. * @type {Object}
  100. * @param {[type]} state [description]
  101. * @param {[type]} payload [description]
  102. */
  103. [REMOVE_PAYMENTS_MOVE]( state, payload) {
  104. if (payload.mode ==='full') {
  105. payload.moveLine.forEach(moves => {
  106. let movesFoundIndex = state.moves.find(item => item.id === moves.id)
  107. if (movesFoundIndex) return
  108. /*ADD MOVE LINE*/
  109. state.moves = [moves, ...state.moves]
  110. /* REMOVE MOVE CARD*/
  111. let movesPaymentsFoundIndex = state.movesPayments.findIndex(item => item.id === moves.id)
  112. state.movesPayments.splice(movesPaymentsFoundIndex, 1)
  113. })
  114. } else {
  115. let movesFound = state.moves.find(item => item.id === payload.moveLine.id)
  116. if (movesFound) return
  117. state.moves = [payload.moveLine, ...state.moves]
  118. let movesFoundIndex = state.movesPayments.findIndex(item => item.id === payload.moveLine.id)
  119. state.movesPayments.splice(movesFoundIndex, 1)
  120. }
  121. },
  122. /**
  123. * [total description]
  124. * @type {[type]}
  125. * @param {[type]} state [description]
  126. * @param {[type]} payload [description]
  127. */
  128. [SET_TOTAL_MOVE_PAYMENTS] (state) {
  129. let total =0
  130. state.movesPayments.forEach(item => {
  131. total = total + item.amountResidual
  132. })
  133. state.movesPaymentsTotal = total
  134. }
  135. }
  136. const actions = {
  137. /**
  138. * [SELECT_MOVE_INVOICE]
  139. * @param {*} param0
  140. * @param {*} payload
  141. */
  142. [SELECT_MOVE_INVOICE] ({ commit }, payload) {
  143. commit(SET_MOVES, payload)
  144. },
  145. /**
  146. * [SELECT_MOVE]
  147. * @param {*} param0
  148. * @param {*} payload
  149. */
  150. [SELECT_MOVE] ({ commit }, payload) {
  151. commit(SET_SELECTED_MOVE_PAYMENTS, payload)
  152. commit(SET_SELECTED_MOVE_LINE, payload)
  153. /* Remover Move Line */
  154. commit(REMOVE_MOVE_LINE, payload)
  155. commit(SET_TOTAL_MOVE_PAYMENTS)
  156. },
  157. /**
  158. *
  159. * @param {*} param0
  160. * @param {*} payload
  161. */
  162. [REMOVE_MOVE_PAYMENTS] ({ commit, dispatch }, payload) {
  163. dispatch(ADD_MOVE_IN_INVOICE, {
  164. moveLine: payload,
  165. mode: 'partial'
  166. })
  167. commit(REMOVE_PAYMENTS_MOVE, {
  168. moveLine: payload,
  169. mode: 'partial'
  170. })
  171. commit(SET_TOTAL_MOVE_PAYMENTS)
  172. },
  173. /**
  174. * [length description]
  175. * @type {[type]}
  176. */
  177. [REMOVE_MOVE_PAYMENTS_ALL] ({commit, dispatch, state}, payload) {
  178. console.log(payload);
  179. if (!payload) return
  180. if (state.movesPayments.length === 0 ) return
  181. dispatch(ADD_MOVE_IN_INVOICE,{
  182. moveLine: state.movesPayments.map(item => {
  183. return item
  184. }),
  185. mode: 'full'
  186. })
  187. commit(REMOVE_PAYMENTS_MOVE, {
  188. moveLine: state.movesPayments.map(item => {
  189. return item
  190. }),
  191. mode: 'full'
  192. })
  193. commit(SET_TOTAL_MOVE_PAYMENTS)
  194. }
  195. }
  196. export default {
  197. state,
  198. getters,
  199. mutations,
  200. actions
  201. }