Forráskód Böngészése

[ADD] views data and server data to correct payment and voucher creation

Gogs 7 éve
szülő
commit
fba628e482

+ 2 - 1
.gitignore

@@ -1,3 +1,4 @@
 *.pyc
 node_modules
-static/src
+static/src
+controllers

+ 1 - 4
__init__.py

@@ -1,12 +1,9 @@
 # -*- coding: utf-8 -*
-from models import account_invoice
 from models import account_journal
 from models import account_payment_term
-from models import account_period
 from models import account_voucher
 from models import eiru_pos
 from models import product_template
 from models import res_company
 from models import res_currency
-from models import res_partner
-from models import sale_order
+from models import res_partner

+ 0 - 4
models/account_invoice.py

@@ -1,4 +0,0 @@
-from openerp import api, fields, models
-
-class AccountInvoice(models.Model):
-    _inherit = 'account.invoice'

+ 16 - 1
models/account_journal.py

@@ -1,4 +1,5 @@
 from openerp import api, fields, models
+import json
 
 class AccountJournal(models.Model):
     _inherit = 'account.journal'
@@ -13,7 +14,21 @@ class AccountJournal(models.Model):
                 'id': journal.id,
                 'name': journal.name,
                 'display_name': journal.display_name,
-                'code': journal.code
+                'code': journal.code,
+                'cash_control': journal.cash_control,
+                'type': journal.type,
+                'default_credit_account': {
+                    'id': journal.default_credit_account_id.id,
+                    'name': journal.default_credit_account_id.name,
+                    'display_name': journal.default_credit_account_id.display_name,
+                    'code': journal.default_credit_account_id.code
+                },
+                'default_debit_account': {
+                    'id': journal.default_debit_account_id.id,
+                    'name': journal.default_debit_account_id.name,
+                    'display_name': journal.default_debit_account_id.display_name,
+                    'code': journal.default_debit_account_id.code
+                }
             })
 
         return journals

+ 2 - 1
models/account_payment_term.py

@@ -14,8 +14,9 @@ class AccountPaymentTerm(models.Model):
                 for line in term.line_ids:
                     lines.append({
                         'id': line.id,
-                        'display_name': line.display_name,
                         'days': line.days,
+                        'days2': line.days2,
+                        'value': line.value,
                         'value_amount': line.value_amount
                     })
 

+ 0 - 19
models/account_period.py

@@ -1,19 +0,0 @@
-from openerp import api, fields, models
-
-class AccountPeriod(models.Model):
-    _inherit = 'account.period'
-
-    @api.model
-    def get_periods(self):
-        domain = []
-        periods = []
-
-        for period in self.env['account.period'].search(domain):
-            periods.append({
-                'id': period.id,
-                'name': period.name,
-                'display_name': period.display_name,
-                'state': period.state
-            })
-
-        return periods

+ 8 - 10
models/account_voucher.py

@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-from openerp import api, fields, models, cli
+from openerp import api, fields, models
 from openerp.exceptions import except_orm
 
 from datetime import datetime
@@ -26,13 +26,11 @@ class AccountVoucher(models.Model):
             'picking_policy': 'direct',
             'state': 'manual',
             'date_confirm': date_now,
-            'payment_term': 4
+            'payment_term': int(values['payment_term_id'])
         })
 
         # Step 2: Create invoice and calculate due_date
-        invoice_id = sale_order.action_invoice_create()
-        
-        invoice = self.env['account.invoice'].browse(invoice_id)
+        invoice = self.env['account.invoice'].browse(sale_order.action_invoice_create())
 
         due_date = parse(date_now) + rd(days=max(invoice.payment_term.line_ids.mapped(lambda x: x.days + x.days2)))
 
@@ -45,7 +43,7 @@ class AccountVoucher(models.Model):
         amount_paid = float(values['amount_paid'])
         decimal_precision = self.env['decimal.precision'].precision_get('Account')
 
-        invoice_move_lines = invoice._get_analytic_lines() + self.env['account.invoice.tax'].move_line_get(invoice_id)
+        invoice_move_lines = invoice._get_analytic_lines() + self.env['account.invoice.tax'].move_line_get(invoice.id)
 
         compute_taxes = self.env['account.invoice.tax'].compute(invoice)
         invoice.check_tax_lines(compute_taxes)
@@ -124,18 +122,18 @@ class AccountVoucher(models.Model):
             'state': 'open'
         })
 
-        # Step 6: Pay Invoice
+        # Step 6: Create voucher
         account_voucher = self.create({
             'reference': account_move.name,
             'type': 'receipt',
-            'journal_id': 7,
+            'journal_id': int(values['journal_id']),
             'company_id': account_move.company_id.id,
             'pre_line': True,
-            'amount': 35000.0,
+            'amount': float(values['amount_paid']),
             'period_id': account_move.period_id.id,
             'date': account_move.date,
             'partner_id': account_move.partner_id.id,
-            'account_id': 160,
+            'account_id': int(values['account_id']),
             'line_cr_ids': [[0, False, {
                 'date_due': l.date_maturity,
                 'account_id': l.account_id.id,

+ 0 - 55
models/sale_order.py

@@ -1,55 +0,0 @@
-from openerp import api, fields, models
-
-class SaleOrder(models.Model):
-    _inherit = 'sale.order'
-
-    @api.model
-    def process_pos_order(self, cart_items, customer_id):
-        order_line_values = []
-        for item in cart_items:
-            order_line_values.append([0, False, {
-                'product_id': int(item['id']),
-                'product_uom_qty': float(item['qty']),
-                'price_unit': float(item['list_price']),
-                'discount': 0
-            }])
-
-        order_values = {
-            'partner_id': customer_id,
-            'partner_invoice_id': customer_id,
-            'partner_shipping_id': customer_id,
-            'order_line': order_line_values,
-            'picking_policy': 'direct',
-            'order_policy': 'manual'
-        }
-
-        order = self.env['sale.order'].create(order_values)
-
-        # Process order now
-        order.action_button_confirm()
-        invoice_id = order.action_invoice_create()
-        invoice = {}
-
-        if invoice_id:
-            invoice = self.env['account.invoice'].browse(invoice_id)
-            invoice.signal_workflow('invoice_open')
-
-        for picking in order.picking_ids:
-            picking.force_assign()
-            picking.action_done()
-
-        dummy, view_id = self.env['ir.model.data'].get_object_reference('account_voucher', 'view_vendor_receipt_dialog_form')
-
-        return {
-                'id': invoice.id,
-                'view_id': view_id,
-                'name': invoice.name,
-                'display_name': invoice.display_name,
-                'currency_id': invoice.currency_id.id,
-                'partner_account_id': self.env['res.partner']._find_accounting_partner(invoice.partner_id).id,
-                'amount': invoice.type in ('out_refund', 'in_refund') and -invoice.residual or invoice.residual,
-                'reference': invoice.name,
-                'invoice_type': invoice.type,
-                'default_type': invoice.type in ('out_invoice', 'out_refund') and 'receipt' or 'payment',
-                'type': invoice.type in ('out_invoice','out_refund') and 'receipt' or 'payment'
-        } if invoice_id else {}

+ 23 - 5
src/components/payment/method/PaymentDetails.vue

@@ -23,12 +23,12 @@
                 label(for="credit") Crédito
         transition(name="fade")
             .form-item(v-if="payment === 'credit'")
-                select.form-input.input-only
-                    option(v-for="term in paymentTerms" v-bind:value="term.id" v-if="term.lines.length > 0 && term.lines[0].days !== 0") {{ term.display_name }}
+                select.form-input.input-only(v-model="term")
+                    option(v-for="term in paymentTerms" :value="term" v-if="term.lines.length > 0 && term.lines[0].days !== 0") {{ term.display_name }}
             .form-item(v-else)
                 label.form-label Método de Pago
-                select.form-input
-                    option(v-for="journal in journals" v-bind:value="journal.id") {{ journal.display_name }}
+                select.form-input(v-model="journal")
+                    option(v-for="journal in journals" :value="journal") {{ journal.display_name }}
 </template>
 
 <script>
@@ -44,6 +44,22 @@
                     this.togglePayment(value)
                 }
             },
+            term: {
+                get() {
+                    return this.selectedPaymentTerm
+                },
+                set(value) {
+                    this.selectPaymentTerm(value)
+                }
+            },
+            journal: {
+                get() {
+                    return this.selectedJournal
+                },
+                set(value) {
+                    this.selectJournal(value)
+                }
+            },
             ...mapGetters([
                 'hasSelectedCustomer',
                 'selectedCustomer',
@@ -62,7 +78,9 @@
                 return this.hasSelectedCustomer && this.selectedCustomer.credit_limit >= this.selectedCustomer.credit ? this.selectedCustomer.credit_limit - this.selectedCustomer.credit : 0
             },
             ...mapActions([
-                'togglePayment'
+                'togglePayment',
+                'selectJournal',
+                'selectPaymentTerm'
             ]) 
         }
     }

+ 6 - 4
src/store/actions.js

@@ -11,14 +11,13 @@ const actions = {
             dispatch('fetchProducts'),
             dispatch('fetchCustomers'),
             dispatch('fetchJournals'),
-            dispatch('fetchPeriods'),
             dispatch('fetchPaymentTerms')
         ]
 
         Promise.all(promises).then(() => {
-            console.log('loaded')
+            console.info('loaded')
         }).catch(error => {
-            console.log(error)
+            console.error(error)
         })
     },
     completeSale({ getters }) {
@@ -28,6 +27,9 @@ const actions = {
             AccountVoucher.call('create_from_pos', [
                 {
                     customer_id: getters.selectedCustomer.id,
+                    payment_term_id: getters.selectedPaymentTerm.id,
+                    journal_id: getters.selectedJournal.id,
+                    account_id: getters.selectedJournal.default_credit_account.id,
                     cart_items: getters.cartItems.map(item => {
                         return {
                             id: item.id,
@@ -36,7 +38,7 @@ const actions = {
                         }
                     }),
                     cart_total: getters.total,
-                    amount_paid: getters.amountPaid
+                    amount_paid: getters.amountPaid > getters.total ? getters.total : getters.amountPaid
                 }
             ], {
                 context: new openerp.web.CompoundContext()

+ 18 - 24
src/store/modules/account.js

@@ -1,6 +1,5 @@
 const state = {
     journals: [],
-    periods: [],
     payment: 'cash',
     paymentTerms: [],
     selectedJournal: null,
@@ -12,9 +11,6 @@ const getters = {
     journals(state) {
         return state.journals
     },
-    periods(state) {
-        return state.periods
-    },
     payment(state) {
         return state.payment
     },
@@ -36,9 +32,6 @@ const mutations = {
     setJournals(state, payload) {
         state.journals = payload
     },
-    setPeriods(state, payload) {
-        state.periods = payload
-    },
     setPaymentTerms(state, payload) {
         state.paymentTerms = payload
     },
@@ -65,6 +58,8 @@ const actions = {
                 context: new openerp.web.CompoundContext()
             }).then(response => {
                 commit('setJournals', response)
+
+                dispatch('autoSelectJournal', response)
                 dispatch('loaded', 'journals')
 
                 resolve()
@@ -74,22 +69,11 @@ const actions = {
             })
         })
     },
-    fetchPeriods({ commit, dispatch }) {
-        return new Promise((resolve, reject) => {
-            let AccountPeriod = new openerp.web.Model('account.period')
-
-            AccountPeriod.call('get_periods', {
-                context: new openerp.web.CompoundContext()
-            }).then(response => {
-                commit('setPeriods', response)
-                dispatch('loaded', 'periods')
-
-                resolve()
-            }).fail(error => {
-                console.log(error)
-                reject(error)
-            })
-        })
+    autoSelectJournal({ dispatch }, payload) {
+        dispatch('selectJournal', payload.find(j => j.type === 'cash'))
+    },
+    selectJournal({ commit }, payload) {
+        commit('setSelectedJournal', payload)
     },
     fetchPaymentTerms({ commit, dispatch }) {
         return new Promise((resolve, reject) => {
@@ -98,7 +82,10 @@ const actions = {
             AccountPaymentTerm.call('get_account_payment_terms', {
                 context: new openerp.web.CompoundContext()
             }).then(response => {
+                console.table(response)
                 commit('setPaymentTerms', response)
+
+                dispatch('autoSelectPaymentTerm', response)
                 dispatch('loaded', 'paymentTerms')
 
                 resolve()
@@ -108,7 +95,14 @@ const actions = {
             })
         })
     },
-    togglePayment({ commit }, payload) {
+    autoSelectPaymentTerm({ dispatch }, payload) {
+        dispatch('selectPaymentTerm', payload.find(t => t.lines.length === 1 && t.lines[0].days === 0) || payload.find(t => t.lines.length === 1 && t.lines[0].days >= 0))
+    },
+    selectPaymentTerm({ commit }, payload) {
+        commit('setSelectedPaymentTerm', payload)
+    },
+    togglePayment({ commit, dispatch, getters }, payload) {
+        dispatch('autoSelectPaymentTerm', payload === 'cash' ? getters.paymentTerms : getters.paymentTerms.filter(t => t.id !== getters.selectedPaymentTerm.id))
         commit('togglePayment', payload)
     },
     changeAmountPaid({ commit }, payload) {

+ 0 - 2
src/store/modules/loader.js

@@ -5,7 +5,6 @@ const state = {
         customers: false,
         products: false,
         journals: false,
-        periods: false,
         paymentTerms: false
     },
     messages: [
@@ -26,7 +25,6 @@ const getters = {
                state.loaded.customers && 
                state.loaded.products &&
                state.loaded.journals &&
-               state.loaded.periods &&
                state.loaded.paymentTerms
     }
 }