123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- import axios from 'axios'
- import { Db } from '../utils'
- const actions = {
- notify(_, message) {
- openerp.web.notification.do_warn('Atención', message)
- return false
- },
- initProcess({ getters, commit, dispatch }, payload) {
- commit('setMode', payload || getters.mode)
- commit('setResult', '')
-
- commit('setLoading', true)
- commit('setCompleted', false)
- return axios.get('/eiru_sales/init', {
- params: {
- mode: getters.mode
- }
- }).then(({data}) => {
- commit('setLoading', false)
- commit('toggleFooterButtonsVisibility')
- dispatch('explodeData', data)
- }).catch(error => {
- console.error(error)
- })
- },
- explodeData({ dispatch, commit }, data) {
- Db.create(data)
- for (let value in data) {
- if (value === 'settings') {
- commit('updateSettings', data[value])
- continue
- }
- dispatch(`init${value[0].toUpperCase()}${value.slice(1)}`, data[value])
- }
- },
- createProduct({ dispatch }, payload) {
- return axios.post('createProductUrl', {
- jsonrpc: '2.0',
- method: 'call',
- params: {
- ...payload
- }
- }).then(({data}) => {
- dispatch('receiveProduct', data.result)
- }).catch(error => {
- console.error(error)
- })
- },
- createCustomer({ dispatch }, payload) {
- return axios.post('/eiru_sales/create_customer', {
- jsonrpc: '2.0',
- method: 'call',
- params: {
- ...payload
- }
- }).then(({data}) => {
- dispatch('receiveCustomer', data.result)
- }).catch(error => {
- console.error(error)
- })
- },
- checkCart({ getters, dispatch }) {
- return !!getters.cartItems.length || dispatch('notify', 'Necesitas agregar productos al carrito para continuar')
- },
- checkCustomer({ getters, dispatch }) {
- if (getters.processing) {
- return dispatch('notify', 'Espere mientras se está procesando')
- }
- return !!getters.selectedCustomer || dispatch('notify', 'Necesitas seleccionar un cliente para continuar')
- },
- checkPaymentMethod({ getters }) {
- if (getters.processing) {
- return dispatch('notify', 'Espere mientras se está procesando')
- }
- return true
- },
- checkAmountReceived({ getters, dispatch }) {
- if (getters.paymentType === 'cash') {
- return getters.initialPayment >= getters.amountToPay || dispatch('notify', 'El monto recibido no puede ser menor al monto a pagar')
- } else {
- return getters.initialPayment < getters.amountToPay || dispatch('notify', 'El monto recibido no puede ser igual o mayor al monto a pagar')
- }
- },
- toggleSettingsVisibility({ commit }) {
- commit('setSettingsVisibility')
- },
- changeSetting({dispatch, commit}, setting) {
- commit('setLoading', true)
- return axios.post('/eiru_sales/save_settings', {
- jsonrpc: '2.0',
- method: 'call',
- params: {
- ...setting
- }
- }).then(({data}) => {
- dispatch('updateImages', data.result)
- commit('updateSettings', data.result)
- commit('setLoading', false)
- }).catch(error => {
- console.log(error)
- })
- },
- updateImages({ commit, getters }, data) {
- const imageType = getters.settings.imageType ? 'small' : 'big'
-
- if (imageType === data.imageType) {
- return
- }
- return axios.get('/eiru_sales/get_images').then(({data}) => {
- commit('setProducts', data.products)
- commit('setCustomers', data.customers)
- }).catch(error => {
- console.error(error)
- })
- },
- endProcess({ getters, commit, dispatch }) {
- const mode = getters.mode
- // Check variables
- if (mode == 'sale') {
- if (getters.paymentType === 'cash' && getters.initialPayment < getters.amountToPay) {
- return dispatch('notify', 'El monto recibido no puede ser menor al monto a pagar')
- }
-
- if (getters.paymentType !== 'cash' && getters.initialPayment >= getters.amountToPay) {
- return dispatch('notify', 'El monto recibido no puede ser igual o mayor al monto a pagar')
- }
- }
- commit('setLoading', true)
- // Collect data
- const data = {
- mode,
- items: getters.cartItems.map(item => {
- return {
- id: item.id,
- quantity: item.quantity,
- price: item.price
- }
- }),
- total: getters.cartTotal,
- customerId: getters.selectedCustomer.id,
- paymentTermId: getters.paymentTerm.id,
- journalId: getters.selectedJournal.id,
- payment: getters.initialPayment > getters.amountToPay ? getters.amountToPay : getters.initialPayment,
- currencyId: getters.selectedCurrency.id,
- paymentMethod: getters.paymentMethod,
- bankPaymentData: {
- ...getters.bankPaymentData
- }
- }
- // Send data to server endpoint
- axios.post('/eiru_sales/finish', {
- jsonrpc: '2.0',
- method: 'call',
- params: {
- ...data
- }
- }).catch(() => {
- dispatch('notify', 'La transacción no terminó correctamente')
- })
- // Print document
- dispatch('printDocument').then(() => {
- commit('setLoading', false)
- commit('setCompleted', true)
- })
- },
- printDocument({ getters, dispatch }) {
- if (getters.mode === 'sale') {
- return dispatch('printTicket')
- }
- },
- printTicket({ getters }) {
- const data = {
- company: getters.companyName,
- street: getters.user.company.street,
- city: getters.user.company.city,
- country: getters.user.company.country,
- customer: getters.selectedCustomerName,
- date: openerp.web.date_to_str(new Date()),
- user: getters.user.name,
- items: getters.cartItems.map(item => {
- return {
- name: item.name.toUpperCase(),
- quantity: item.quantity,
- price: item.price,
- subtotal: item.quantity * item.price
- }
- }),
- paymentMethod: getters.paymentMethod,
- total: getters.amountToPay,
- received: getters.amountResidual + getters.amountToPay,
- residual: getters.amountResidual,
- currencyPosition: getters.selectedCurrency.position,
- currencyDecimalPlaces: getters.selectedCurrency.decimalPlaces,
- currencyDecimalSeparator: getters.selectedCurrency.decimalSeparator,
- currencySymbol: getters.currencySymbol
- }
- const wrapper = document.createElement('div')
- wrapper.innerHTML = openerp.web.qweb.render('EiruPosTicket', {...data})
- wrapper.setAttribute('id', 'ticket_wrapper')
- var childElement = document.body.appendChild(wrapper)
- const ticket_el = document.querySelector('.eiru_pos_ticket')
- const ticket_width = 70
- const measure_factor = 3.7793
- const dpi_factor = 3.125
- return new Promise(resolve => {
- openerp.html2canvas(ticket_el, {
- logging: false,
- width: ticket_width * measure_factor,
- scale: dpi_factor
- }).then(function (canvas) {
- childElement.remove()
-
- const dataURL = canvas.toDataURL('image/png')
-
- const width = (canvas.width / measure_factor) / dpi_factor
- const height = (canvas.height / measure_factor) / dpi_factor
-
- const doc = new jsPDF({
- unit: 'mm',
- format: [height, ticket_width]
- })
-
- doc.addImage(dataURL, 'PNG', 0, 0, width, height)
- const data = doc.output('datauristring')
-
- openerp.printer_bridge.print(data)
- resolve();
- });
- })
- },
- resetSettings() {
- // Ignore this
- },
- resetProcess({rootState, dispatch}) {
- for (let key in rootState) {
- if (!(rootState[key] instanceof Object)) {
- continue
- }
-
- key = key.replace('Module', '')
- dispatch(`reset${key[0].toUpperCase()}${key.slice(1)}`)
- }
- dispatch('initProcess')
- }
- }
- export default actions
|