actions.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import axios from 'axios'
  2. import {
  3. INIT_PAYMENTS_URL,
  4. PAYMENTS_PROCESS_URL
  5. } from '@/constants/resourcePaths'
  6. /*Action */
  7. import {
  8. PAYMENTS_NOTIFY,
  9. INIT_PAYMENTS,
  10. EXPLODE_DATA,
  11. INIT_PAYMENTS_DATE,
  12. INIT_PAYMENTS_USER,
  13. INIT_PAYMENTS_CUSTOMERS,
  14. INIT_PAYMENTS_JOURNALS,
  15. INIT_PAYMENTS_CURRENCIES,
  16. PAYMENTS_PROCESS,
  17. CHECK_CUSTOMER,
  18. CHECK_INVOICE,
  19. CHECK_MOVE,
  20. CHECK_PAYMENTS,
  21. COMPLETED_PAYMENTS,
  22. RESET_PAYMENT_CUSTOMER,
  23. RESET_DATE,
  24. RESET_USER,
  25. RESET_COMPANY,
  26. RESET_CUSTOMERS,
  27. RESET_INVOICES,
  28. RESET_MOVE_LINES,
  29. RESET_JOURNALS,
  30. RESET_CURRENCY,
  31. RESET_PAYMENTS,
  32. RESET_BANK_TYPE,
  33. RESET_BANK,
  34. SET_CHANGE_TAB_STEPS_SALES,
  35. REMOVE_MOVE_PAYMENTS_ALL
  36. } from '@/constants/actionTypes'
  37. // Mutations
  38. import {
  39. SET_PROCESSING_PAYMENTS,
  40. SET_COMPLETED_PAYMENTS
  41. } from '@/constants/mutationTypes'
  42. const actions = {
  43. /**
  44. * [PAYMENTS_NOTIFY]
  45. * @type {[type]}
  46. */
  47. [PAYMENTS_NOTIFY] ({ commit },payload) {
  48. openerp.web.notification.do_warn('Atencion', payload)
  49. return false
  50. },
  51. /**
  52. * [INIT_PAYMENTS]
  53. * @param {*} param0
  54. */
  55. [INIT_PAYMENTS] ({ dispatch }) {
  56. return axios.get(INIT_PAYMENTS_URL).then(response => {
  57. dispatch(COMPLETED_PAYMENTS, false)
  58. dispatch('explodeData', response.data)
  59. }).catch(error => {
  60. console.log(error);
  61. })
  62. },
  63. /**
  64. * [EXPLODE_DATA]
  65. * @param {*} param0
  66. * @param {*} payload
  67. */
  68. [EXPLODE_DATA] ({ dispatch }, payload) {
  69. for (let value in payload) {
  70. dispatch(`initPayments${value[0].toUpperCase()}${value.slice(1)}`,payload[value])
  71. }
  72. },
  73. /**
  74. * [CHECK_CUSTOMER]
  75. */
  76. [CHECK_CUSTOMER] ({ getters, dispatch }) {
  77. return !!getters.selectedCustomer ||dispatch(PAYMENTS_NOTIFY,'Necesitas seleccionar un cliente para continuar.')
  78. },
  79. /**
  80. * [CHECK_INVOICE]
  81. */
  82. [CHECK_INVOICE] ({ getters, dispatch }) {
  83. return !!getters.selectedInvoice ||dispatch(PAYMENTS_NOTIFY, 'Necesitas seleccionar una factura para continuar.')
  84. },
  85. /**
  86. * [data description]
  87. * @type {Object}
  88. */
  89. [CHECK_MOVE]({ getters, dispatch }) {
  90. return !!getters.movesPayments.length || dispatch(PAYMENTS_NOTIFY,'Necesitas seleccionar al menos una cuota para continuar.')
  91. },
  92. /**
  93. * [data description]
  94. * @type {Object}
  95. */
  96. [CHECK_PAYMENTS]({ getters, dispatch }) {
  97. if (!getters.selectedJournal || getters.selectedJournal.length <= 0) {
  98. return dispatch(PAYMENTS_NOTIFY, 'Necesitas seleccionar un método de pago')
  99. }
  100. if (getters.paidTotal <= 0){
  101. return dispatch(PAYMENTS_NOTIFY, 'Necesitas ingresar un monto a pagar')
  102. }
  103. if (getters.selectedJournal && getters.selectedJournal.type ==='bank') {
  104. if (!getters.selectedPaymentsBank || getters.selectedPaymentsBank.length <= 0) {
  105. return dispatch(PAYMENTS_NOTIFY, 'Necesitas seleccionar un banco')
  106. }
  107. if (!getters.selectedPaymentsBankType || getters.selectedPaymentsBankType.length <= 0) {
  108. return dispatch(PAYMENTS_NOTIFY, 'Necesitas seleccionar el tipo de transacción bancaria')
  109. }
  110. if (!getters.paymentsBankRef){
  111. return dispatch(PAYMENTS_NOTIFY, 'Necesitas ingresar el Nº cheque / boleta')
  112. }
  113. if (!getters.paymentsBankDateMaturity){
  114. return dispatch(PAYMENTS_NOTIFY, 'Necesitas ingresar una fecha de vencimiento')
  115. }
  116. }
  117. return true
  118. },
  119. /**
  120. * [PAYMENTS_PROCESS]
  121. * @param {*} param0
  122. * @param {*} payload
  123. */
  124. [PAYMENTS_PROCESS] ({ getters, commit, dispatch }, payload ) {
  125. commit(SET_PROCESSING_PAYMENTS, true)
  126. const data = {
  127. jsonrpc: '2.0',
  128. method: 'call',
  129. params: {
  130. invoiceId: getters.selectedInvoice.id,
  131. journalId: getters.selectedJournal.id,
  132. customerId: getters.selectedCustomer.id,
  133. amountPayments: getters.paidTotal <= getters.paymentCurrencyTotal ? getters.paidTotal : getters.paymentCurrencyTotal,
  134. journalType: getters.selectedJournal.type,
  135. bankId: getters.selectedJournal.type ==='bank' ? getters.selectedPaymentsBank.id : '',
  136. bankTypeId: getters.selectedJournal.type ==='bank' ? getters.selectedPaymentsBankType.id : '',
  137. bankRef: getters.selectedJournal.type ==='bank' ? getters.paymentsBankRef : '',
  138. bankDateMaturity: getters.selectedJournal.type ==='bank' ? getters.paymentsBankDateMaturity : '',
  139. paymentsBankNumber_cta: getters.selectedJournal.type ==='bank' ? getters.paymentsBankNumberCta : '',
  140. paymentsBankName_holder: getters.selectedJournal.type ==='bank' ? getters.paymentsBankNameHolder : '',
  141. moveLines: getters.movesPayments.map(item => {
  142. return item.id
  143. })
  144. }
  145. }
  146. return axios.post(PAYMENTS_PROCESS_URL, data).then(response => {
  147. commit(SET_PROCESSING_PAYMENTS, !response.data.result)
  148. dispatch(COMPLETED_PAYMENTS, response.data.result)
  149. }).catch(error => {
  150. console.log(error);
  151. commit(SET_PROCESSING_PAYMENTS, false)
  152. })
  153. },
  154. /**
  155. * [COMPLETED_PAYMENTS]
  156. *
  157. */
  158. [COMPLETED_PAYMENTS] ({ commit }, payload) {
  159. commit(SET_COMPLETED_PAYMENTS, !!payload)
  160. },
  161. /**
  162. * [RESET_PAYMENT_CUSTOMER]
  163. */
  164. [RESET_PAYMENT_CUSTOMER] ({ rootState, commit, dispatch }){
  165. for (let key in rootState) {
  166. if (!(rootState[key] instanceof Object)){
  167. continue
  168. }
  169. dispatch(`reset${key[0].toUpperCase()}${key.slice(1)}`)
  170. }
  171. dispatch(INIT_PAYMENTS)
  172. },
  173. /**
  174. * [prev description]
  175. * @type {[type]}
  176. */
  177. [SET_CHANGE_TAB_STEPS_SALES] ({ commit, dispatch, getters }, payload) {
  178. // Resetaer all
  179. if (payload.prev === 0 && payload.next === -1) {
  180. dispatch(RESET_PAYMENT_CUSTOMER)
  181. }
  182. // Reset STEPS 3-4
  183. if (payload.prev >= 2 && payload.next <= 1){
  184. dispatch(REMOVE_MOVE_PAYMENTS_ALL)
  185. }
  186. }
  187. }
  188. export default actions