Skip to content

Commit

Permalink
Use match pattern to find journal
Browse files Browse the repository at this point in the history
Closes #11884
  • Loading branch information
cedk committed Dec 26, 2022
1 parent 2f23646 commit 468ca93
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 44 deletions.
1 change: 1 addition & 0 deletions modules/account/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Use match pattern to find journal
* Display general ledger information on account form

Version 6.6.0 - 2022-10-31
Expand Down
13 changes: 11 additions & 2 deletions modules/account/journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
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)
Expand All @@ -25,7 +26,9 @@


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)
Expand Down Expand Up @@ -153,6 +156,12 @@ def get_debit_credit_balance(cls, journals, names):
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()
Expand Down
10 changes: 7 additions & 3 deletions modules/account/view/journal_form.xml
Original file line number Diff line number Diff line change
@@ -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"/>
Expand Down
2 changes: 1 addition & 1 deletion modules/account/view/journal_tree.xml
Original file line number Diff line number Diff line change
@@ -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"/>
Expand Down
35 changes: 21 additions & 14 deletions modules/account_invoice/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
Expand Down Expand Up @@ -429,6 +433,9 @@ def __register__(cls, module_name):
# 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]
Expand Down Expand Up @@ -470,26 +477,26 @@ def on_change_with_account(self):
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':
return ['revenue']
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]
Expand Down
34 changes: 24 additions & 10 deletions modules/account_invoice_defer/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down Expand Up @@ -146,6 +149,14 @@ def __setup__(cls):
},
})

@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':
Expand All @@ -154,14 +165,17 @@ def _journal_types(cls, type):
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):
Expand Down
15 changes: 3 additions & 12 deletions modules/purchase/purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,28 +770,19 @@ def store_cache(cls, purchases):
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'
Expand Down
2 changes: 1 addition & 1 deletion modules/sale/sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ def _get_invoice_sale(self):
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

Expand Down
2 changes: 1 addition & 1 deletion modules/sale_subscription/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def _get_invoice(self):
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

Expand Down

0 comments on commit 468ca93

Please sign in to comment.