Files
tryton/party.py
T

182 lines
7.1 KiB
Python
Raw Normal View History

#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
2013-09-06 22:33:47 +02:00
from sql import Literal
from sql.aggregate import Sum
from sql.conditionals import Coalesce
2012-09-24 22:17:29 +02:00
from trytond.model import fields
2011-08-21 11:20:29 +02:00
from trytond.pyson import Eval, Bool
2010-08-19 22:02:00 +02:00
from trytond.transaction import Transaction
2012-09-24 22:17:29 +02:00
from trytond.pool import Pool, PoolMeta
__all__ = ['Party']
__metaclass__ = PoolMeta
2012-09-24 22:17:29 +02:00
class Party:
__name__ = 'party.party'
2010-03-11 16:45:27 +01:00
account_payable = fields.Property(fields.Many2One('account.account',
2011-08-04 16:57:14 +02:00
'Account Payable', domain=[
('kind', '=', 'payable'),
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
'required': Bool(Eval('context', {}).get('company')),
'invisible': ~Eval('context', {}).get('company'),
2011-08-04 16:57:14 +02:00
}))
2010-03-11 16:45:27 +01:00
account_receivable = fields.Property(fields.Many2One('account.account',
2011-08-04 16:57:14 +02:00
'Account Receivable', domain=[
('kind', '=', 'receivable'),
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
'required': Bool(Eval('context', {}).get('company')),
'invisible': ~Eval('context', {}).get('company'),
2011-08-04 16:57:14 +02:00
}))
2010-03-11 16:45:27 +01:00
customer_tax_rule = fields.Property(fields.Many2One('account.tax.rule',
2011-08-04 16:57:14 +02:00
'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.'))
2010-03-11 16:45:27 +01:00
supplier_tax_rule = fields.Property(fields.Many2One('account.tax.rule',
2011-08-04 16:57:14 +02:00
'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.'))
2010-03-11 16:45:27 +01:00
receivable = fields.Function(fields.Numeric('Receivable'),
'get_receivable_payable', searcher='search_receivable_payable')
payable = fields.Function(fields.Numeric('Payable'),
'get_receivable_payable', searcher='search_receivable_payable')
receivable_today = fields.Function(fields.Numeric('Receivable Today'),
'get_receivable_payable', searcher='search_receivable_payable')
payable_today = fields.Function(fields.Numeric('Payable Today'),
'get_receivable_payable', searcher='search_receivable_payable')
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.
'''
res = {}
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')
2012-09-24 22:17:29 +02:00
User = pool.get('res.user')
Date = pool.get('ir.date')
2010-08-19 22:02:00 +02:00
cursor = Transaction().cursor
2013-09-06 22:33:47 +02:00
line = MoveLine.__table__()
account = Account.__table__()
for name in names:
if name not in ('receivable', 'payable',
'receivable_today', 'payable_today'):
raise Exception('Bad argument')
2012-09-24 22:17:29 +02:00
res[name] = dict((p.id, Decimal('0.0')) for p in parties)
2011-07-20 16:58:39 +02:00
user_id = Transaction().user
if user_id == 0 and 'user' in Transaction().context:
user_id = Transaction().context['user']
2012-09-24 22:17:29 +02:00
user = User(user_id)
2012-12-05 13:43:06 +01:00
if not user.company:
return res
2012-12-05 13:43:06 +01:00
company_id = user.company.id
2013-09-06 22:33:47 +02:00
line_query, _ = MoveLine.query_get(line)
for name in names:
code = name
2013-09-06 22:33:47 +02:00
today_query = Literal(True)
if name in ('receivable_today', 'payable_today'):
code = name[:-6]
2013-09-06 22:33:47 +02:00
today_query = ((line.maturity_date <= Date.today())
| (line.maturity_date == None))
2013-09-06 22:33:47 +02:00
cursor.execute(*line.join(account,
condition=account.id == line.account
).select(line.party,
Sum(Coalesce(line.debit, 0) - Coalesce(line.credit, 0)),
where=account.active
& (account.kind == code)
& line.party.in_([p.id for p in parties])
& (line.reconciliation == None)
& (account.company == company_id)
& line_query & today_query,
group_by=line.party))
for party_id, sum in cursor.fetchall():
2009-09-22 16:08:11 +02:00
# SQLite uses float for SUM
if not isinstance(sum, Decimal):
sum = Decimal(str(sum))
res[name][party_id] = sum
return res
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')
2012-09-24 22:17:29 +02:00
Company = pool.get('company.company')
User = pool.get('res.user')
Date = pool.get('ir.date')
2013-09-06 22:33:47 +02:00
line = MoveLine.__table__()
account = Account.__table__()
if name not in ('receivable', 'payable',
'receivable_today', 'payable_today'):
raise Exception('Bad argument')
company_id = None
2011-07-20 16:58:39 +02:00
user_id = Transaction().user
if user_id == 0 and 'user' in Transaction().context:
user_id = Transaction().context['user']
2012-09-24 22:17:29 +02:00
user = User(user_id)
2010-08-19 22:02:00 +02:00
if Transaction().context.get('company'):
2012-09-24 22:17:29 +02:00
child_companies = Company.search([
('parent', 'child_of', [user.main_company.id]),
])
if Transaction().context['company'] in child_companies:
2010-08-19 22:02:00 +02:00
company_id = Transaction().context['company']
if not company_id:
2012-12-05 13:43:06 +01:00
if user.company:
company_id = user.company.id
elif user.main_company:
company_id = user.main_company.id
if not company_id:
return []
code = name
2013-09-06 22:33:47 +02:00
today_query = Literal(True)
if name in ('receivable_today', 'payable_today'):
code = name[:-6]
2013-09-06 22:33:47 +02:00
today_query = ((line.maturity_date <= Date.today())
| (line.maturity_date == None))
2013-09-06 22:33:47 +02:00
line_query, _ = MoveLine.query_get(line)
Operator = fields.SQL_OPERATORS[clause[1]]
2013-09-06 22:33:47 +02:00
query = line.join(account, condition=account.id == line.account
).select(line.party,
where=account.active
& (account.kind == code)
& (line.party != None)
& (line.reconciliation == None)
& (account.company == company_id)
& today_query,
group_by=line.party,
having=Operator(Sum(Coalesce(line.debit, 0)
- Coalesce(line.credit, 0)),
Decimal(clause[2] or 0)))
return [('id', 'in', query)]