currency.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const state = {
  2. currencies: [],
  3. selectedCurrency: null,
  4. loadingCurrencies: false,
  5. }
  6. const getters = {
  7. currencies(state) {
  8. return state.currencies
  9. },
  10. currencySymbols(state, { settings }) {
  11. if (!state.currencies || !state.selectedCurrency) {
  12. return []
  13. }
  14. if (settings && !settings.allowCurrencyExchange) {
  15. return []
  16. }
  17. return state.currencies.filter(c => state.selectedCurrency.id !== c.id).map(c => c.symbol)
  18. },
  19. baseCurrency(state) {
  20. return state.currencies.find(c => c.base === true)
  21. },
  22. selectedCurrency(state) {
  23. return state.selectedCurrency
  24. },
  25. currencySymbol(state) {
  26. return state.selectedCurrency && state.selectedCurrency.symbol
  27. },
  28. loadingCurrencies(state) {
  29. return state.loadingCurrencies
  30. }
  31. }
  32. const mutations = {
  33. setSelectedCurrency(state, currencySymbol) {
  34. if (currencySymbol) {
  35. state.selectedCurrency = state.currencies.find(c => c.symbol === currencySymbol)
  36. return
  37. }
  38. state.selectedCurrency = state.currencies.find(c => c.base === true)
  39. },
  40. setCurrencies(state, payload) {
  41. state.currencies = payload
  42. },
  43. setLoadingCurrencies(state, payload) {
  44. state.loadingCurrencies = !!payload
  45. }
  46. }
  47. const actions = {
  48. initCurrencies({ commit }, payload) {
  49. commit('setCurrencies', payload)
  50. commit('setSelectedCurrency')
  51. commit('setLoadingCurrencies')
  52. },
  53. resetCurrency({ commit }) {
  54. commit('setLoadingCurrencies', true)
  55. commit('setCurrencies', [])
  56. },
  57. changeCurrency({ commit, dispatch, getters }, currencySymbol) {
  58. commit('computePaymentInCurrency', {
  59. fromSymbol: getters.currencySymbol,
  60. toSymbol: currencySymbol
  61. });
  62. commit('setSelectedCurrency', currencySymbol)
  63. // commit('setInitialPayment', 0)
  64. // commit('setAmountResidual')
  65. if (getters.paymentType === 'credit') {
  66. dispatch('computePaymentLines')
  67. }
  68. dispatch('autoSelectJournal')
  69. }
  70. }
  71. export default {
  72. state,
  73. getters,
  74. mutations,
  75. actions
  76. }