Files
tryton/modules/account/party.py
T

350 lines
13 KiB
Python
Raw Normal View History

2014-12-02 12:54:29 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2010-08-19 22:02:00 +02:00
from decimal import Decimal
2018-04-30 15:26:03 +02:00
from sql import Literal, Null
2013-09-06 22:33:47 +02:00
from sql.aggregate import Sum
from sql.conditionals import Coalesce
2018-12-29 14:20:28 +01:00
from trytond.i18n import gettext
2017-04-01 00:25:49 +02:00
from trytond.model import ModelSQL, fields
from trytond.modules.company.model import (
CompanyMultiValueMixin, CompanyValueMixin)
2021-08-21 09:09:55 +02:00
from trytond.modules.currency.fields import Monetary
2021-12-11 17:58:41 +01:00
from trytond.modules.party.exceptions import EraseError
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval, If
from trytond.tools import grouped_slice, reduce_ids
from trytond.transaction import Transaction
2018-12-29 14:20:28 +01:00
from .exceptions import AccountMissing
2012-09-24 22:17:29 +02:00
2017-04-01 00:25:49 +02:00
account_names = [
'account_payable', 'account_receivable',
'customer_tax_rule', 'supplier_tax_rule']
2018-06-26 19:11:40 +02:00
class Party(CompanyMultiValueMixin, metaclass=PoolMeta):
2012-09-24 22:17:29 +02:00
__name__ = 'party.party'
2017-04-01 00:25:49 +02:00
accounts = fields.One2Many('party.party.account', 'party', "Accounts")
account_payable = fields.MultiValue(fields.Many2One(
'account.account', "Account Payable",
domain=[
2019-12-28 17:48:50 +01:00
('closed', '!=', True),
2019-03-24 18:00:01 +01:00
('type.payable', '=', True),
('party_required', '=', True),
2013-10-09 18:25:04 +02:00
('company', '=', Eval('context', {}).get('company', -1)),
2011-08-04 16:57:14 +02:00
],
states={
2011-08-21 11:20:29 +02:00
'invisible': ~Eval('context', {}).get('company'),
2011-08-04 16:57:14 +02:00
}))
2017-04-01 00:25:49 +02:00
account_receivable = fields.MultiValue(fields.Many2One(
'account.account', "Account Receivable",
domain=[
2019-12-28 17:48:50 +01:00
('closed', '!=', True),
2019-03-24 18:00:01 +01:00
('type.receivable', '=', True),
('party_required', '=', True),
2013-10-09 18:25:04 +02:00
('company', '=', Eval('context', {}).get('company', -1)),
2011-08-04 16:57:14 +02:00
],
states={
2011-08-21 11:20:29 +02:00
'invisible': ~Eval('context', {}).get('company'),
2011-08-04 16:57:14 +02:00
}))
2017-04-01 00:25:49 +02:00
customer_tax_rule = fields.MultiValue(fields.Many2One(
'account.tax.rule', "Customer Tax Rule",
domain=[
2013-10-09 18:25:04 +02:00
('company', '=', Eval('context', {}).get('company', -1)),
('kind', 'in', ['sale', 'both']),
],
2011-08-04 16:57:14 +02:00
states={
2011-08-21 11:20:29 +02:00
'invisible': ~Eval('context', {}).get('company'),
2011-08-04 16:57:14 +02:00
}, help='Apply this rule on taxes when party is customer.'))
2017-04-01 00:25:49 +02:00
supplier_tax_rule = fields.MultiValue(fields.Many2One(
'account.tax.rule', "Supplier Tax Rule",
domain=[
2013-10-09 18:25:04 +02:00
('company', '=', Eval('context', {}).get('company', -1)),
('kind', 'in', ['purchase', 'both']),
],
2011-08-04 16:57:14 +02:00
states={
2011-08-21 11:20:29 +02:00
'invisible': ~Eval('context', {}).get('company'),
2011-08-04 16:57:14 +02:00
}, help='Apply this rule on taxes when party is supplier.'))
2021-08-21 09:09:55 +02:00
currency = fields.Function(fields.Many2One(
'currency.currency', "Currency"), 'get_currency')
receivable = fields.Function(Monetary(
"Receivable", currency='currency', digits='currency'),
'get_receivable_payable', searcher='search_receivable_payable')
payable = fields.Function(Monetary(
"Payable", currency='currency', digits='currency'),
'get_receivable_payable', searcher='search_receivable_payable')
receivable_today = fields.Function(Monetary(
"Receivable Today", currency='currency', digits='currency'),
'get_receivable_payable', searcher='search_receivable_payable')
payable_today = fields.Function(Monetary(
"Payable Today", currency='currency', digits='currency'),
'get_receivable_payable', searcher='search_receivable_payable')
2017-04-01 00:25:49 +02:00
@classmethod
def multivalue_model(cls, field):
pool = Pool()
if field in account_names:
return pool.get('party.party.account')
return super(Party, cls).multivalue_model(field)
2019-08-18 19:06:19 +02:00
@classmethod
def _default_tax_rule(cls, type_, **pattern):
pool = Pool()
Configuration = pool.get('account.configuration')
config = Configuration(1)
assert type_ in {'customer', 'supplier'}
tax_rule = config.get_multivalue(
'default_%s_tax_rule' % type_, **pattern)
return tax_rule.id if tax_rule else None
@classmethod
def default_customer_tax_rule(cls, **pattern):
return cls._default_tax_rule('customer', **pattern)
@classmethod
def default_supplier_tax_rule(cls, **pattern):
return cls._default_tax_rule('supplier', **pattern)
2021-08-21 09:09:55 +02:00
def get_currency(self, name):
pool = Pool()
Company = pool.get('company.company')
company_id = Transaction().context.get('company')
if company_id is not None and company_id >= 0:
company = Company(company_id)
2021-08-21 09:09:55 +02:00
return company.currency.id
2012-09-24 22:17:29 +02:00
@classmethod
def get_receivable_payable(cls, parties, names):
'''
Function to compute receivable, payable (today or not) for party ids.
'''
result = {}
2011-07-03 13:18:40 +02:00
pool = Pool()
2012-09-24 22:17:29 +02:00
MoveLine = pool.get('account.move.line')
2013-09-06 22:33:47 +02:00
Account = pool.get('account.account')
2019-03-24 18:00:01 +01:00
AccountType = pool.get('account.account.type')
2012-09-24 22:17:29 +02:00
User = pool.get('res.user')
Date = pool.get('ir.date')
cursor = Transaction().connection.cursor()
2013-09-06 22:33:47 +02:00
line = MoveLine.__table__()
account = Account.__table__()
2019-03-24 18:00:01 +01:00
account_type = AccountType.__table__()
2013-09-06 22:33:47 +02:00
for name in names:
if name not in ('receivable', 'payable',
'receivable_today', 'payable_today'):
raise Exception('Bad argument')
result[name] = dict((p.id, Decimal('0.0')) for p in parties)
2014-12-20 19:12:19 +01:00
user = User(Transaction().user)
2012-12-05 13:43:06 +01:00
if not user.company:
return result
2012-12-05 13:43:06 +01:00
company_id = user.company.id
exp = Decimal(str(10.0 ** -user.company.currency.digits))
with Transaction().set_context(company=company_id):
today = Date.today()
amount = Sum(Coalesce(line.debit, 0) - Coalesce(line.credit, 0))
for name in names:
code = name
today_where = Literal(True)
if name in ('receivable_today', 'payable_today'):
code = name[:-6]
today_where = ((line.maturity_date <= today)
| (line.maturity_date == Null))
for sub_parties in grouped_slice(parties):
sub_ids = [p.id for p in sub_parties]
party_where = reduce_ids(line.party, sub_ids)
cursor.execute(*line.join(account,
condition=account.id == line.account
2019-03-24 18:00:01 +01:00
).join(account_type,
condition=account.type == account_type.id
).select(line.party, amount,
2019-03-24 18:00:01 +01:00
where=(getattr(account_type, code)
& (line.reconciliation == Null)
& (account.company == company_id)
& party_where
& today_where),
group_by=line.party))
for party, value in cursor:
# SQLite uses float for SUM
if not isinstance(value, Decimal):
value = Decimal(str(value))
result[name][party] = value.quantize(exp)
return result
2012-09-24 22:17:29 +02:00
@classmethod
def search_receivable_payable(cls, name, clause):
2011-07-03 13:18:40 +02:00
pool = Pool()
2012-09-24 22:17:29 +02:00
MoveLine = pool.get('account.move.line')
2013-09-06 22:33:47 +02:00
Account = pool.get('account.account')
2019-03-24 18:00:01 +01:00
AccountType = pool.get('account.account.type')
2012-09-24 22:17:29 +02:00
User = pool.get('res.user')
Date = pool.get('ir.date')
2013-09-06 22:33:47 +02:00
line = MoveLine.__table__()
account = Account.__table__()
2019-03-24 18:00:01 +01:00
account_type = AccountType.__table__()
2013-09-06 22:33:47 +02:00
if name not in ('receivable', 'payable',
'receivable_today', 'payable_today'):
raise Exception('Bad argument')
_, operator, value = clause
2014-12-20 19:12:19 +01:00
user = User(Transaction().user)
if not user.company:
return []
2014-12-20 19:12:19 +01:00
company_id = user.company.id
with Transaction().set_context(company=company_id):
today = Date.today()
code = name
2013-09-06 22:33:47 +02:00
today_query = Literal(True)
if name in ('receivable_today', 'payable_today'):
code = name[:-6]
today_query = ((line.maturity_date <= today)
| (line.maturity_date == Null))
Operator = fields.SQL_OPERATORS[operator]
# Need to cast numeric for sqlite
2018-04-30 15:26:03 +02:00
cast_ = MoveLine.debit.sql_cast
amount = cast_(Sum(Coalesce(line.debit, 0) - Coalesce(line.credit, 0)))
if operator in {'in', 'not in'}:
2018-04-30 15:26:03 +02:00
value = [cast_(Literal(Decimal(v or 0))) for v in value]
else:
2018-04-30 15:26:03 +02:00
value = cast_(Literal(Decimal(value or 0)))
2019-03-24 18:00:01 +01:00
query = (line.join(account, condition=account.id == line.account
).join(account_type, condition=account.type == account_type.id
2013-09-06 22:33:47 +02:00
).select(line.party,
2019-03-24 18:00:01 +01:00
where=(getattr(account_type, code)
& (line.party != Null)
& (line.reconciliation == Null)
2013-09-06 22:33:47 +02:00
& (account.company == company_id)
2019-03-24 18:00:01 +01:00
& today_query),
group_by=line.party,
having=Operator(amount, value)))
2013-09-06 22:33:47 +02:00
return [('id', 'in', query)]
2016-10-02 15:19:34 +02:00
@property
def account_payable_used(self):
pool = Pool()
Configuration = pool.get('account.configuration')
account = self.account_payable
if not account:
2021-04-03 14:27:09 +02:00
with Transaction().set_context(self._context):
config = Configuration(1)
account = config.get_multivalue('default_account_payable')
# Allow empty values on on_change
if not account and not Transaction().readonly:
2018-12-29 14:20:28 +01:00
raise AccountMissing(
gettext('account.msg_party_missing_payable_account',
party=self.rec_name))
2018-08-31 14:58:05 +02:00
if account:
return account.current()
@property
def account_receivable_used(self):
pool = Pool()
Configuration = pool.get('account.configuration')
account = self.account_receivable
if not account:
2021-04-03 14:27:09 +02:00
with Transaction().set_context(self._context):
config = Configuration(1)
account = config.get_multivalue('default_account_receivable')
# Allow empty values on on_change
if not account and not Transaction().readonly:
2018-12-29 14:20:28 +01:00
raise AccountMissing(
gettext('account.msg_party_missing_receivable_account',
party=self.rec_name))
2018-08-31 14:58:05 +02:00
if account:
return account.current()
2019-09-20 11:47:15 +02:00
@classmethod
def view_attributes(cls):
2021-04-09 10:52:03 +02:00
return super().view_attributes() + [
2019-09-20 11:47:15 +02:00
('/tree/field[@name="receivable_today"]',
'visual', If(Eval('receivable_today', 0) > 0, 'danger', '')),
('/tree/field[@name="payable_today"]',
'visual', If(Eval('payable_today', 0) < 0, 'warning', '')),
]
2021-10-11 15:35:46 +02:00
@classmethod
def copy(cls, parties, default=None):
default = default.copy() if default else {}
if Transaction().check_access:
2021-10-11 15:35:46 +02:00
fields = [
'accounts',
'account_payable', 'account_receivable',
'customer_tax_rule', 'supplier_tax_rule']
default_values = cls.default_get(fields, with_rec_name=False)
for fname in fields:
default.setdefault(fname, default_values.get(fname))
return super().copy(parties, default=default)
2016-10-02 15:19:34 +02:00
2017-04-01 00:25:49 +02:00
class PartyAccount(ModelSQL, CompanyValueMixin):
"Party Account"
__name__ = 'party.party.account'
party = fields.Many2One(
'party.party', "Party", ondelete='CASCADE',
context={
'company': Eval('company', -1),
},
depends={'company'})
2017-04-01 00:25:49 +02:00
account_payable = fields.Many2One(
'account.account', "Account Payable",
domain=[
2019-03-24 18:00:01 +01:00
('type.payable', '=', True),
('party_required', '=', True),
2017-04-01 00:25:49 +02:00
('company', '=', Eval('company', -1)),
],
ondelete='RESTRICT')
2017-04-01 00:25:49 +02:00
account_receivable = fields.Many2One(
'account.account', "Account Receivable",
domain=[
2019-03-24 18:00:01 +01:00
('type.receivable', '=', True),
('party_required', '=', True),
2017-04-01 00:25:49 +02:00
('company', '=', Eval('company', -1)),
],
ondelete='RESTRICT')
2017-04-01 00:25:49 +02:00
customer_tax_rule = fields.Many2One(
'account.tax.rule', "Customer Tax Rule",
domain=[
('company', '=', Eval('company', -1)),
('kind', 'in', ['sale', 'both']),
],
ondelete='RESTRICT')
2017-04-01 00:25:49 +02:00
supplier_tax_rule = fields.Many2One(
'account.tax.rule', "Supplier Tax Rule",
domain=[
('company', '=', Eval('company', -1)),
('kind', 'in', ['purchase', 'both']),
],
ondelete='RESTRICT')
2017-04-01 00:25:49 +02:00
2018-06-26 19:11:40 +02:00
class PartyReplace(metaclass=PoolMeta):
2016-10-02 15:19:34 +02:00
__name__ = 'party.replace'
@classmethod
def fields_to_replace(cls):
return super(PartyReplace, cls).fields_to_replace() + [
('account.move.line', 'party'),
]
2018-03-24 00:04:07 +01:00
2018-06-26 19:11:40 +02:00
class PartyErase(metaclass=PoolMeta):
2018-03-24 00:04:07 +01:00
__name__ = 'party.erase'
def check_erase_company(self, party, company):
super(PartyErase, self).check_erase_company(party, company)
if party.receivable or party.payable:
2018-12-29 14:20:28 +01:00
raise EraseError(
gettext('account.msg_erase_party_receivable_payable',
party=party.rec_name,
company=company.rec_name))