actions.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const actions = {
  2. notify({ commit }, payload) {
  3. openerp.web.notification.do_warn('Atención', payload)
  4. return false
  5. },
  6. initSale({ commit, dispatch }, payload) {
  7. commit('setPosInstance', payload)
  8. let promises = [
  9. dispatch('fetchCompany'),
  10. dispatch('fetchCurrencies'),
  11. dispatch('fetchProducts'),
  12. dispatch('fetchCustomers'),
  13. dispatch('fetchJournals'),
  14. dispatch('fetchPaymentTerms')
  15. ]
  16. Promise.all(promises).then(() => {
  17. console.info('loaded')
  18. }).catch(error => {
  19. console.error(error)
  20. })
  21. },
  22. checkCart({ getters, dispatch }) {
  23. return getters.cartIsEmpty || dispatch('notify', 'Necesitas agregar productos al carrito para continuar')
  24. },
  25. checkCustomer({ getters, dispatch }) {
  26. return getters.hasSelectedCustomer || dispatch('notify', 'Necesitas seleccionar un cliente para continuar')
  27. },
  28. checkAmountPaid({ getters, dispatch }) {
  29. return (getters.payment === 'cash' && getters.amountPaid >= getters.total) || dispatch('notify', 'El monto recibido no puede ser menor al monto a pagar')
  30. },
  31. completeSale({ getters, dispatch }) {
  32. return new Promise((resolve, reject) => {
  33. let AccountVoucher = new openerp.web.Model('account.voucher')
  34. AccountVoucher.call('create_from_pos', [
  35. {
  36. customer_id: getters.selectedCustomer.id,
  37. payment_term_id: getters.selectedPaymentTerm.id,
  38. journal_id: getters.selectedJournal.id,
  39. account_id: getters.selectedJournal.default_credit_account.id,
  40. cart_items: getters.cartItems.map(item => {
  41. return {
  42. id: item.id,
  43. qty: item.qty,
  44. price: item.list_price,
  45. discount: item.price >= item.list_price ? 1.0 : item.price / item.list_price
  46. }
  47. }),
  48. cart_total: getters.total,
  49. amount_paid: getters.amountPaid > getters.total ? getters.total : getters.amountPaid
  50. }
  51. ], {
  52. context: new openerp.web.CompoundContext()
  53. }).then(response => {
  54. window.location.reload()
  55. dispatch('printInvoice', response)
  56. resolve(response)
  57. }).fail(error => {
  58. reject(error)
  59. })
  60. })
  61. },
  62. printInvoice({ getters }, payload) {
  63. let self = getters.posInstance
  64. let active_ids = {
  65. active_id: payload.invoice_id,
  66. active_ids: [payload.invoice_id],
  67. active_model: 'account.invoice'
  68. }
  69. self.rpc('/web/action/load', {
  70. action_id: payload.action_id,
  71. context: new openerp.web.CompoundContext(active_ids).eval()
  72. }).then(result => {
  73. result.context = new openerp.web.CompoundContext(result.context || {}, active_ids).eval();
  74. result.flags = result.flags || {};
  75. result.flags.inline = true;
  76. self.do_action(result, {
  77. on_close: function () {
  78. self.getParent().reload();
  79. }
  80. })
  81. })
  82. }
  83. }
  84. export default actions