Use match pattern to find journal

Closes #11884
This commit is contained in:
Cédric Krier
2022-12-26 15:08:29 +01:00
parent 2f2364672e
commit 468ca93e07
9 changed files with 70 additions and 44 deletions
+1
View File
@@ -1,3 +1,4 @@
* Use match pattern to find journal
* Display general ledger information on account form
Version 6.6.0 - 2022-10-31
+11 -2
View File
@@ -7,7 +7,8 @@ from sql.aggregate import Sum
from trytond import backend
from trytond.i18n import gettext
from trytond.model import (
DeactivableMixin, ModelSQL, ModelView, Unique, Workflow, fields)
DeactivableMixin, MatchMixin, ModelSQL, ModelView, Unique, Workflow,
fields, sequence_ordered)
from trytond.model.exceptions import AccessError
from trytond.modules.company.model import (
CompanyMultiValueMixin, CompanyValueMixin)
@@ -25,7 +26,9 @@ STATES = {
class Journal(
DeactivableMixin, ModelSQL, ModelView, CompanyMultiValueMixin):
DeactivableMixin, MatchMixin,
sequence_ordered('matching_sequence', "Matching Sequence"),
ModelSQL, ModelView, CompanyMultiValueMixin):
'Journal'
__name__ = 'account.journal'
name = fields.Char('Name', size=None, required=True, translate=True)
@@ -153,6 +156,12 @@ class Journal(
debit - credit)
return result
@classmethod
def find(cls, pattern):
for journal in cls.search([]):
if journal.match(pattern):
return journal
@classmethod
def write(cls, *args):
pool = Pool()
+7 -3
View File
@@ -1,13 +1,17 @@
<?xml version="1.0"?>
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<form col="6">
<form>
<label name="name"/>
<field name="name"/>
<label name="code"/>
<field name="code"/>
<label name="active"/>
<field name="active" xexpand="0" width="100"/>
<label name="code"/>
<field name="code"/>
<label name="matching_sequence"/>
<field name="matching_sequence"/>
<label name="type"/>
<field name="type"/>
<label name="sequence"/>
+1 -1
View File
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<tree>
<tree sequence="matching_sequence">
<field name="code"/>
<field name="name" expand="1"/>
<field name="type"/>
+21 -14
View File
@@ -127,7 +127,11 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin):
currency_date = fields.Function(fields.Date('Currency Date'),
'on_change_with_currency_date')
journal = fields.Many2One(
'account.journal', 'Journal', required=True, states=_states,
'account.journal', 'Journal',
states={
'readonly': _states['readonly'],
'required': Eval('state') != 'draft',
},
context={
'company': Eval('company', -1),
}, depends={'company'})
@@ -429,6 +433,9 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin):
# Migration from 5.8: drop foreign key for sequence
table.drop_fk('sequence')
# Migration from 6.6: drop not null on journal
table.not_null_action('journal', 'remove')
@classmethod
def order_number(cls, tables):
table, _ = tables[None]
@@ -470,19 +477,6 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin):
account = self.party.account_payable_used
return account.id if account else None
@fields.depends('type')
def on_change_type(self):
Journal = Pool().get('account.journal')
journal_type = {
'out': 'revenue',
'in': 'expense',
}.get(self.type or 'out', 'revenue')
journals = Journal.search([
('type', '=', journal_type),
], limit=1)
if journals:
self.journal, = journals
@classmethod
def _journal_types(cls, invoice_type):
if invoice_type == 'out':
@@ -490,6 +484,19 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin):
else:
return ['expense']
@fields.depends('type')
def on_change_with_journal(self, pattern=None):
pool = Pool()
Journal = pool.get('account.journal')
pattern = pattern.copy() if pattern is not None else {}
pattern.setdefault('type', {
'out': 'revenue',
'in': 'expense',
}.get(self.type))
journal = Journal.find(pattern)
if journal:
return journal.id
@classmethod
def order_accounting_date(cls, tables):
table, _ = tables[None]
+24 -10
View File
@@ -72,8 +72,11 @@ class InvoiceDeferred(Workflow, ModelSQL, ModelView):
], "Type", required=True,
states=_states)
journal = fields.Many2One(
'account.journal', "Journal", required=True,
states=_states,
'account.journal', "Journal",
states={
'readonly': _states['readonly'],
'required': Eval('state') != 'draft',
},
context={
'company': Eval('company', -1),
},
@@ -146,6 +149,14 @@ class InvoiceDeferred(Workflow, ModelSQL, ModelView):
},
})
@classmethod
def __register__(cls, module_name):
table_h = cls.__table_handler__(module_name)
super().__register__(module_name)
# Migration from 6.6: drop not null on journal
table_h.not_null_action('journal', 'remove')
@classmethod
def _journal_types(cls, type):
if type == 'out':
@@ -154,14 +165,17 @@ class InvoiceDeferred(Workflow, ModelSQL, ModelView):
return ['expense']
@fields.depends('type')
def on_change_type(self):
Journal = self.__class__.journal.get_target()
journal_types = self.__class__._journal_types(self.type)
journals = Journal.search([
('type', 'in', journal_types),
], limit=1)
if journals:
self.journal, = journals
def on_change_with_journal(self, pattern=None):
pool = Pool()
Journal = pool.get('account.journal')
pattern = pattern.copy() if pattern is not None else {}
pattern.setdefault('type', {
'out': 'revenue',
'in': 'expense',
}.get(self.type))
journal = Journal.find(pattern)
if journal:
return journal.id
@fields.depends('invoice_line', 'start_date', 'company')
def on_change_invoice_line(self):
+3 -12
View File
@@ -770,28 +770,19 @@ class Purchase(
def _get_invoice_purchase(self):
'Return invoice'
pool = Pool()
Journal = pool.get('account.journal')
Invoice = pool.get('account.invoice')
journals = Journal.search([
('type', '=', 'expense'),
], limit=1)
if journals:
journal, = journals
else:
journal = None
party = self.invoice_party or self.party
return Invoice(
invoice = Invoice(
company=self.company,
type='in',
journal=journal,
party=party,
invoice_address=self.invoice_address,
currency=self.currency,
account=party.account_payable_used,
payment_term=self.payment_term,
)
invoice.journal = invoice.on_change_with_journal()
return invoice
def create_invoice(self):
'Create an invoice for the purchase and return it'
+1 -1
View File
@@ -831,7 +831,7 @@ class Sale(
currency=self.currency,
account=party.account_receivable_used,
)
invoice.on_change_type()
invoice.journal = invoice.on_change_with_journal()
invoice.payment_term = self.payment_term
return invoice
+1 -1
View File
@@ -467,7 +467,7 @@ class Subscription(Workflow, ModelSQL, ModelView):
account=party.account_receivable_used,
)
invoice.invoice_date = self.next_invoice_date
invoice.on_change_type()
invoice.journal = invoice.on_change_with_journal()
invoice.payment_term = self.payment_term
return invoice