123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- import axios from 'axios'
- const actions = {
- notify(_, message) {
- openerp.web.notification.do_warn('Atención', message)
- return false
- },
- initProcess({ getters, commit, dispatch }, mode) {
- commit('setMode', mode || getters.mode)
- commit('setResult', '')
-
- commit('setLoading', true)
- commit('setCompleted', false)
- if (!getters.isWired) {
- commit('setLoading', false)
- return
- }
-
- 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) {
- 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')
- },
- checkSaleOrder({ getters, dispatch }) {
- return !!getters.selectedSaleOrder || dispatch('notify', 'Necesitas seleccionar un presupuesto para continuar')
- },
- checkStockPicking({ getters, dispatch }) {
- return !!getters.selectedStockPicking || dispatch('notify', 'Necesitas seleccionar una entrega para confirmar')
- },
- 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
- 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)
- let data = {
- mode
- }
- if (['sale', 'budget', 'product_picking'].includes(mode)) {
- data = {
- ...data,
- items: getters.cartItems.map(item => {
- return {
- id: item.id,
- quantity: item.quantity,
- price: item.price
- }
- }),
- customerId: getters.selectedCustomer.id,
- currencyId: getters.selectedCurrency.id,
- warehouseId: getters.selectedWarehouse.id
- }
- }
- if (['sale', 'budget', 'payment'].includes(mode)) {
- data = {
- ...data,
- total: getters.cartTotal,
- saleOrderId: (getters.selectedSaleOrder && getters.selectedSaleOrder.id) || null,
- paymentTermId: getters.paymentTerm.id,
- journalId: getters.selectedJournal.id,
- payment: getters.initialPayment > getters.amountToPay ? getters.amountToPay : getters.initialPayment,
- paymentMethod: getters.paymentMethod,
- customerId: getters.selectedCustomer.id,
- currencyId: getters.selectedCurrency.id,
- warehouseId: getters.selectedWarehouse.id,
- bankPaymentData: {
- ...getters.bankPaymentData
- }
- }
- }
- if (mode === 'product_delivery') {
- data = {
- ...data,
- stockPickingId: getters.selectedStockPicking.id
- }
- }
- if (['sale', 'budget'].includes(mode)) {
- dispatch('storeDataAsync', data)
- dispatch('printDocument').then(() => {
- commit('setLoading', false)
- commit('setCompleted', true)
- })
- return
- }
-
- dispatch('storeDataSync', data)
- },
- storeDataSync({ dispatch, commit }, data) {
- commit('storeData', data)
- dispatch('syncData').then(() => {
- commit('resetData')
- commit('setLoading', false)
- commit('setCompleted', true)
- })
- },
- storeDataAsync({ getters, dispatch, commit }, data) {
- commit('storeData', data)
- if (getters.isWired) {
- dispatch('syncData')
- commit('resetData')
- }
- },
- syncData({ getters, dispatch }) {
- if (getters.data.length == 0) {
- return
- }
- if (getters.isSynchronizable) {
- dispatch('notify', 'Estás conectado nuevamente. Sincronizaremos sus datos en segundo plano')
- }
- return axios.post('/eiru_sales/finish', {
- jsonrpc: '2.0',
- method: 'call',
- params: {
- data: getters.data
- }
- }).then(() => {
- if (getters.isSynchronizable) {
- dispatch('notify', 'Los datos fueron sincronizados con éxito')
- }
- }).catch(() => {
- if (getters.isSynchronizable) {
- dispatch('notify', 'Los datos no fueron sincronizados correctamente')
- }
- })
- },
- printDocument({ getters, dispatch }) {
- if (getters.mode === 'sale') {
- return dispatch('printTicket')
- }
- },
- printTicket({ getters, dispatch }) {
- if (!openerp.printer_bridge) {
- dispatch('notify', 'Impresión no disponible')
- return
- }
- 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', '')
- if (key === 'data') {
- continue
- }
- dispatch(`reset${key[0].toUpperCase()}${key.slice(1)}`)
- }
- dispatch('initProcess')
- },
- changeNetStatus({ commit, dispatch }, isWired) {
- dispatch('hideTopbar', isWired)
- commit('setNetStatus', isWired)
- if (isWired) {
- dispatch('syncData')
- return
- }
- dispatch('notify', 'Estás sin conexión. Ocultaremos el menú superior mientras sigas trabajando')
- },
- hideTopbar(_, isWired) {
- if (!openerp.eiru_topbar_toggler) {
- return
- }
- setTimeout(() => {
- if (isWired) {
- openerp.eiru_topbar_toggler.show()
- } else {
- openerp.eiru_topbar_toggler.hide()
- }
- }, 3000)
- }
- }
- export default actions
|