actions.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import axios from 'axios'
  2. import { Db } from '../utils'
  3. const actions = {
  4. notify(_, message) {
  5. openerp.web.notification.do_warn('Atención', message)
  6. return false
  7. },
  8. initProcess({ getters, commit, dispatch }, payload) {
  9. commit('setMode', payload || getters.mode)
  10. commit('setResult', '')
  11. commit('setLoading', true)
  12. commit('setCompleted', false)
  13. return axios.get('/eiru_sales/init', {
  14. params: {
  15. mode: getters.mode
  16. }
  17. }).then(({data}) => {
  18. commit('setLoading', false)
  19. commit('toggleFooterButtonsVisibility')
  20. dispatch('explodeData', data)
  21. }).catch(error => {
  22. console.error(error)
  23. })
  24. },
  25. explodeData({ dispatch, commit }, data) {
  26. Db.create(data)
  27. for (let value in data) {
  28. if (value === 'settings') {
  29. commit('updateSettings', data[value])
  30. continue
  31. }
  32. dispatch(`init${value[0].toUpperCase()}${value.slice(1)}`, data[value])
  33. }
  34. },
  35. createProduct({ dispatch }, payload) {
  36. return axios.post('createProductUrl', {
  37. jsonrpc: '2.0',
  38. method: 'call',
  39. params: {
  40. ...payload
  41. }
  42. }).then(({data}) => {
  43. dispatch('receiveProduct', data.result)
  44. }).catch(error => {
  45. console.error(error)
  46. })
  47. },
  48. createCustomer({ dispatch }, payload) {
  49. return axios.post('/eiru_sales/create_customer', {
  50. jsonrpc: '2.0',
  51. method: 'call',
  52. params: {
  53. ...payload
  54. }
  55. }).then(({data}) => {
  56. dispatch('receiveCustomer', data.result)
  57. }).catch(error => {
  58. console.error(error)
  59. })
  60. },
  61. checkCart({ getters, dispatch }) {
  62. return !!getters.cartItems.length || dispatch('notify', 'Necesitas agregar productos al carrito para continuar')
  63. },
  64. checkCustomer({ getters, dispatch }) {
  65. if (getters.processing) {
  66. return dispatch('notify', 'Espere mientras se está procesando')
  67. }
  68. return !!getters.selectedCustomer || dispatch('notify', 'Necesitas seleccionar un cliente para continuar')
  69. },
  70. checkPaymentMethod({ getters }) {
  71. if (getters.processing) {
  72. return dispatch('notify', 'Espere mientras se está procesando')
  73. }
  74. return true
  75. },
  76. checkAmountReceived({ getters, dispatch }) {
  77. if (getters.paymentType === 'cash') {
  78. return getters.initialPayment >= getters.amountToPay || dispatch('notify', 'El monto recibido no puede ser menor al monto a pagar')
  79. } else {
  80. return getters.initialPayment < getters.amountToPay || dispatch('notify', 'El monto recibido no puede ser igual o mayor al monto a pagar')
  81. }
  82. },
  83. toggleSettingsVisibility({ commit }) {
  84. commit('setSettingsVisibility')
  85. },
  86. changeSetting({dispatch, commit}, setting) {
  87. commit('setLoading', true)
  88. return axios.post('/eiru_sales/save_settings', {
  89. jsonrpc: '2.0',
  90. method: 'call',
  91. params: {
  92. ...setting
  93. }
  94. }).then(({data}) => {
  95. dispatch('updateImages', data.result)
  96. commit('updateSettings', data.result)
  97. commit('setLoading', false)
  98. }).catch(error => {
  99. console.log(error)
  100. })
  101. },
  102. updateImages({ commit, getters }, data) {
  103. const imageType = getters.settings.imageType ? 'small' : 'big'
  104. if (imageType === data.imageType) {
  105. return
  106. }
  107. return axios.get('/eiru_sales/get_images').then(({data}) => {
  108. commit('setProducts', data.products)
  109. commit('setCustomers', data.customers)
  110. }).catch(error => {
  111. console.error(error)
  112. })
  113. },
  114. endProcess({ getters, commit, dispatch }) {
  115. const mode = getters.mode
  116. // Check variables
  117. if (mode == 'sale') {
  118. if (getters.paymentType === 'cash' && getters.initialPayment < getters.amountToPay) {
  119. return dispatch('notify', 'El monto recibido no puede ser menor al monto a pagar')
  120. }
  121. if (getters.paymentType !== 'cash' && getters.initialPayment >= getters.amountToPay) {
  122. return dispatch('notify', 'El monto recibido no puede ser igual o mayor al monto a pagar')
  123. }
  124. }
  125. commit('setLoading', true)
  126. // Collect data
  127. const data = {
  128. mode,
  129. items: getters.cartItems.map(item => {
  130. return {
  131. id: item.id,
  132. quantity: item.quantity,
  133. price: item.price
  134. }
  135. }),
  136. total: getters.cartTotal,
  137. customerId: getters.selectedCustomer.id,
  138. paymentTermId: getters.paymentTerm.id,
  139. journalId: getters.selectedJournal.id,
  140. payment: getters.initialPayment > getters.amountToPay ? getters.amountToPay : getters.initialPayment,
  141. currencyId: getters.selectedCurrency.id,
  142. paymentMethod: getters.paymentMethod,
  143. bankPaymentData: {
  144. ...getters.bankPaymentData
  145. }
  146. }
  147. // Send data to server endpoint
  148. axios.post('/eiru_sales/finish', {
  149. jsonrpc: '2.0',
  150. method: 'call',
  151. params: {
  152. ...data
  153. }
  154. }).catch(() => {
  155. dispatch('notify', 'La transacción no terminó correctamente')
  156. })
  157. // Print document
  158. dispatch('printDocument').then(() => {
  159. commit('setLoading', false)
  160. commit('setCompleted', true)
  161. })
  162. },
  163. printDocument({ getters, dispatch }) {
  164. if (getters.mode === 'sale') {
  165. return dispatch('printTicket')
  166. }
  167. },
  168. printTicket({ getters }) {
  169. const data = {
  170. company: getters.companyName,
  171. street: getters.user.company.street,
  172. city: getters.user.company.city,
  173. country: getters.user.company.country,
  174. customer: getters.selectedCustomerName,
  175. date: openerp.web.date_to_str(new Date()),
  176. user: getters.user.name,
  177. items: getters.cartItems.map(item => {
  178. return {
  179. name: item.name.toUpperCase(),
  180. quantity: item.quantity,
  181. price: item.price,
  182. subtotal: item.quantity * item.price
  183. }
  184. }),
  185. paymentMethod: getters.paymentMethod,
  186. total: getters.amountToPay,
  187. received: getters.amountResidual + getters.amountToPay,
  188. residual: getters.amountResidual,
  189. currencyPosition: getters.selectedCurrency.position,
  190. currencyDecimalPlaces: getters.selectedCurrency.decimalPlaces,
  191. currencyDecimalSeparator: getters.selectedCurrency.decimalSeparator,
  192. currencySymbol: getters.currencySymbol
  193. }
  194. const wrapper = document.createElement('div')
  195. wrapper.innerHTML = openerp.web.qweb.render('EiruPosTicket', {...data})
  196. wrapper.setAttribute('id', 'ticket_wrapper')
  197. var childElement = document.body.appendChild(wrapper)
  198. const ticket_el = document.querySelector('.eiru_pos_ticket')
  199. const ticket_width = 70
  200. const measure_factor = 3.7793
  201. const dpi_factor = 3.125
  202. return new Promise(resolve => {
  203. openerp.html2canvas(ticket_el, {
  204. logging: false,
  205. width: ticket_width * measure_factor,
  206. scale: dpi_factor
  207. }).then(function (canvas) {
  208. childElement.remove()
  209. const dataURL = canvas.toDataURL('image/png')
  210. const width = (canvas.width / measure_factor) / dpi_factor
  211. const height = (canvas.height / measure_factor) / dpi_factor
  212. const doc = new jsPDF({
  213. unit: 'mm',
  214. format: [height, ticket_width]
  215. })
  216. doc.addImage(dataURL, 'PNG', 0, 0, width, height)
  217. const data = doc.output('datauristring')
  218. openerp.printer_bridge.print(data)
  219. resolve();
  220. });
  221. })
  222. },
  223. resetSettings() {
  224. // Ignore this
  225. },
  226. resetProcess({rootState, dispatch}) {
  227. for (let key in rootState) {
  228. if (!(rootState[key] instanceof Object)) {
  229. continue
  230. }
  231. key = key.replace('Module', '')
  232. dispatch(`reset${key[0].toUpperCase()}${key.slice(1)}`)
  233. }
  234. dispatch('initProcess')
  235. }
  236. }
  237. export default actions