customers.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const state = {
  2. customers: [],
  3. filtered: [],
  4. selectedCustomer: null,
  5. addCustomer: false
  6. }
  7. const getters = {
  8. customers(state) {
  9. return state.filtered.length === 0 ? state.customers : state.filtered
  10. },
  11. hasSelectedCustomer(state) {
  12. return !!state.selectedCustomer
  13. },
  14. selectedCustomer(state) {
  15. return state.selectedCustomer
  16. },
  17. addCustomer(state) {
  18. return state.addCustomer
  19. }
  20. }
  21. const mutations = {
  22. pushCustomers(state, payload) {
  23. state.customers = [...payload, ...state.customers]
  24. },
  25. selectCustomer(state, payload) {
  26. state.selectedCustomer = payload.customer
  27. },
  28. applyCustomersFilter(state, payload) {
  29. state.filtered = payload
  30. },
  31. addCustomer(state) {
  32. state.addCustomer = !state.addCustomer
  33. }
  34. }
  35. const actions = {
  36. fetchCustomers({ commit, dispatch }) {
  37. return new Promise((resolve, reject) => {
  38. let ResPartner = new openerp.web.Model('res.partner')
  39. ResPartner.call('get_customers', {
  40. context: new openerp.web.CompoundContext()
  41. }).then(response => {
  42. commit('pushCustomers', response)
  43. dispatch('loaded', 'customers')
  44. resolve()
  45. }).fail(error => {
  46. console.log(error)
  47. reject(error)
  48. })
  49. })
  50. },
  51. selectCustomer({ commit }, payload) {
  52. commit('selectCustomer', {
  53. customer: payload
  54. })
  55. },
  56. filterCustomers({ commit }, payload) {
  57. commit('applyCustomersFilter', payload)
  58. },
  59. addCustomer({ commit }) {
  60. commit('addCustomer')
  61. },
  62. saveCustomer({ commit, dispatch }, payload) {
  63. if (payload) {
  64. if (!payload.name || !payload.ruc || !payload.phone) {
  65. dispatch('notify', 'Complete los datos para guardar')
  66. return
  67. }
  68. let ResPartner = new openerp.web.Model('res.partner')
  69. ResPartner.call('create_from_pos', [
  70. payload
  71. ], {
  72. context: new openerp.web.CompoundContext()
  73. }).then(id => {
  74. commit('pushCustomers', [{
  75. id,
  76. ...payload
  77. }])
  78. })
  79. }
  80. commit('addCustomer')
  81. }
  82. }
  83. export default {
  84. state,
  85. getters,
  86. mutations,
  87. actions
  88. }