actions.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import axios from 'axios'
  2. const newUrl = '/eiru_purchases/new'
  3. const createSupplierUrl = '/eiru_purchases/create_supplier'
  4. const createProductUrl = '/eiru_purchases/create_product'
  5. const createUrl = '/eiru_purchases/create'
  6. const actions = {
  7. /**
  8. * Display notification with odoo style
  9. * @param {*} param0
  10. * @param {*} payload
  11. */
  12. notify({ commit }, payload) {
  13. openerp.web.notification.do_warn('Atención', payload)
  14. return false
  15. },
  16. /**
  17. * Get all resources to init purchase app
  18. * @param {*} param0
  19. */
  20. initPurchase({ dispatch }) {
  21. return axios.get(newUrl).then(response => {
  22. dispatch('explodeData', response.data)
  23. }).catch(error => {
  24. console.log(error)
  25. })
  26. },
  27. /**
  28. *
  29. * @param {*} param0
  30. * @param {*} payload
  31. */
  32. explodeData({ commit }, payload) {
  33. Object.keys(payload).forEach(key => {
  34. commit(`set${key[0].toLocaleUpperCase()}${key.slice(1)}`, payload[key])
  35. })
  36. },
  37. /**
  38. * Send data to create a supplier
  39. * @param {*} param0
  40. * @param {*} payload
  41. */
  42. createSupplier({ commit }, payload) {
  43. const data = {
  44. jsonrpc: '2.0',
  45. method: 'call',
  46. params: {
  47. ...payload
  48. }
  49. }
  50. return axios.post(createSupplierUrl, data).then(response => {
  51. console.log(response)
  52. }).catch(error => {
  53. console.log(error)
  54. })
  55. },
  56. /**
  57. * Check if a supplier is selected or notify
  58. * @param {*} param0
  59. */
  60. checkSupplier({ getters, dispatch }) {
  61. return !!getters.supplierSelected || dispatch('notify', 'Necesitas seleccionar un proveedor para continuar')
  62. },
  63. /**
  64. * Send data to create a product
  65. * @param {*} param0
  66. * @param {*} payload
  67. */
  68. createProduct({ commit }, payload) {
  69. const data = {
  70. jsonrpc: '2.0',
  71. method: 'call',
  72. params: {
  73. ...payload
  74. }
  75. }
  76. return axios.post(createProductUrl, data).then(response => {
  77. console.log(response)
  78. }).catch(error => {
  79. console.log(error)
  80. })
  81. },
  82. /**
  83. * Check if exist product in cart or notify
  84. * @param {*} param0
  85. */
  86. checkCart({ getters, dispatch }) {
  87. return !!getters.cartItems.length || dispatch('notify', 'Necesitas agregar productos al carrito para continuar')
  88. },
  89. /**
  90. * Send data to create concrete purchase
  91. * @param {*} param0
  92. */
  93. createPurchase({ getters, dispatch }) {
  94. const data = {
  95. jsonrpc: '2.0',
  96. method: 'call',
  97. params: {
  98. supplier: getters.supplierSelected.id
  99. }
  100. }
  101. return axios.post(createUrl, data).then(response => {
  102. console.log(response)
  103. // dispatch('resetPurchase')
  104. }).catch(error => {
  105. console.log(error)
  106. })
  107. },
  108. /**
  109. * Reset all default app state
  110. * @param {*} param0
  111. */
  112. resetPurchase({ state, commit }) {
  113. Object.keys(state).forEach(value => {
  114. Object.keys(state[value]).forEach(key => {
  115. commit(`set${key[0].toLocaleUpperCase()}${key.slice(1)}`, state[value][key].default)
  116. })
  117. })
  118. }
  119. }
  120. export default actions