Commit 74cad8cf authored by Cédric Krier's avatar Cédric Krier
Browse files

Use match pattern to find journal

Closes #11884
parent bd7b1e764e0b
Pipeline #60089 passed with stages
in 1 minute and 32 seconds
* Use match pattern to find journal
* Display general ledger information on account form
Version 6.6.0 - 2022-10-31
......
......@@ -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)
......@@ -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)
......@@ -154,6 +157,12 @@
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()
Move = pool.get('account.move')
......
<?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"/>
......@@ -5,4 +5,7 @@
<label name="name"/>
<field name="name"/>
<label name="active"/>
<field name="active" xexpand="0" width="100"/>
<label name="code"/>
<field name="code"/>
......@@ -7,7 +10,8 @@
<label name="code"/>
<field name="code"/>
<label name="active"/>
<field name="active" xexpand="0" width="100"/>
<label name="matching_sequence"/>
<field name="matching_sequence"/>
<label name="type"/>
<field name="type"/>
<label name="sequence"/>
......
<?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"/>
......
......@@ -127,7 +127,11 @@
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 @@
# 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 @@
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 @@
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]
......
......@@ -72,8 +72,11 @@
], "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),
},
......@@ -147,6 +150,14 @@
})
@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':
return ['revenue']
......@@ -154,14 +165,17 @@
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):
......
......@@ -770,5 +770,4 @@
def _get_invoice_purchase(self):
'Return invoice'
pool = Pool()
Journal = pool.get('account.journal')
Invoice = pool.get('account.invoice')
......@@ -774,10 +773,2 @@
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
......@@ -783,5 +774,4 @@
party = self.invoice_party or self.party
return Invoice(
invoice = Invoice(
company=self.company,
type='in',
......@@ -786,9 +776,8 @@
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,
)
......@@ -789,9 +778,11 @@
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'
......
......@@ -831,7 +831,7 @@
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
......
......@@ -467,7 +467,7 @@
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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment