Files
tryton/line.py
T

151 lines
5.5 KiB
Python
Raw Normal View History

2010-01-20 10:58:31 +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-16 19:24:05 +02:00
import time
from decimal import Decimal
2009-02-28 16:58:26 +01:00
from trytond.model import ModelView, ModelSQL, fields
2012-01-02 18:00:00 +01:00
from trytond.wizard import Wizard, StateAction
from trytond.backend import TableHandler
2010-01-20 10:58:31 +01:00
from trytond.pyson import Eval, PYSONEncoder
2010-08-16 19:24:05 +02:00
from trytond.transaction import Transaction
2011-07-03 13:18:47 +02:00
from trytond.pool import Pool
2008-05-23 15:04:38 +02:00
2012-09-24 22:11:39 +02:00
__all__ = ['Line', 'MoveLine', 'OpenAccount']
2008-05-23 15:04:38 +02:00
2009-02-28 16:58:26 +01:00
class Line(ModelSQL, ModelView):
2008-05-23 15:04:38 +02:00
'Analytic Line'
2012-09-24 22:11:39 +02:00
__name__ = 'analytic_account.line'
2008-05-23 15:04:38 +02:00
name = fields.Char('Name', required=True)
2010-01-20 10:58:31 +01:00
debit = fields.Numeric('Debit', digits=(16, Eval('currency_digits', 2)),
required=True, depends=['currency_digits'])
2010-01-20 10:58:31 +01:00
credit = fields.Numeric('Credit', digits=(16, Eval('currency_digits', 2)),
required=True, depends=['currency_digits'])
2010-03-11 16:46:52 +01:00
currency = fields.Function(fields.Many2One('currency.currency', 'Currency',
2012-09-24 22:11:39 +02:00
on_change_with=['move_line']), 'on_change_with_currency')
2010-03-11 16:46:52 +01:00
currency_digits = fields.Function(fields.Integer('Currency Digits',
2012-09-24 22:11:39 +02:00
on_change_with=['move_line']), 'on_change_with_currency_digits')
2008-05-23 15:04:38 +02:00
account = fields.Many2One('analytic_account.account', 'Account',
2012-03-05 10:52:58 +01:00
required=True, select=True, domain=[('type', '!=', 'view')])
2008-05-23 15:04:38 +02:00
move_line = fields.Many2One('account.move.line', 'Account Move Line',
ondelete='CASCADE', required=True)
journal = fields.Many2One('account.journal', 'Journal', required=True,
2012-03-05 10:52:58 +01:00
select=True)
2008-05-23 15:04:38 +02:00
date = fields.Date('Date', required=True)
reference = fields.Char('Reference')
2008-10-22 20:09:28 +02:00
party = fields.Many2One('party.party', 'Party')
2012-03-05 10:52:58 +01:00
active = fields.Boolean('Active', select=True)
2008-05-23 15:04:38 +02:00
2012-09-24 22:11:39 +02:00
@classmethod
def __setup__(cls):
super(Line, cls).__setup__()
cls._sql_constraints += [
2008-05-23 15:04:38 +02:00
('credit_debit',
'CHECK((credit * debit = 0.0) AND (credit + debit >= 0.0))',
'Wrong credit/debit values.'),
2012-09-24 22:11:39 +02:00
]
cls._error_messages.update({
'line_on_view_account': ('You can not create a move line using '
'view account "%s".'),
'line_on_inactive_account': ('You can not create a move line '
'using inactive account "%s".'),
2013-02-17 15:30:45 +01:00
})
2012-09-24 22:11:39 +02:00
cls._order.insert(0, ('date', 'ASC'))
2008-05-23 15:04:38 +02:00
2012-09-24 22:11:39 +02:00
@classmethod
def __register__(cls, module_name):
super(Line, cls).__register__(module_name)
2010-08-16 19:24:05 +02:00
cursor = Transaction().cursor
2012-09-24 22:11:39 +02:00
table = TableHandler(cursor, cls, module_name)
# Migration from 1.2 currency has been changed in function field
table.not_null_action('currency', action='remove')
2008-05-23 15:04:38 +02:00
2012-09-24 22:11:39 +02:00
@staticmethod
def default_date():
Date = Pool().get('ir.date')
return Date.today()
2008-05-23 15:04:38 +02:00
2012-09-24 22:11:39 +02:00
@staticmethod
def default_active():
2008-05-23 15:04:38 +02:00
return True
2012-09-24 22:11:39 +02:00
@staticmethod
def default_debit():
return Decimal(0)
2012-09-24 22:11:39 +02:00
@staticmethod
def default_credit():
return Decimal(0)
2012-09-24 22:11:39 +02:00
def on_change_with_currency(self, name=None):
if self.move_line:
return self.move_line.account.company.currency.id
2012-09-24 22:11:39 +02:00
def on_change_with_currency_digits(self, name=None):
if self.move_line:
return self.move_line.account.company.currency.digits
return 2
2012-09-24 22:11:39 +02:00
@staticmethod
def query_get(obj='l'):
2008-05-23 15:04:38 +02:00
'''
Return SQL clause for analytic line depending of the context.
obj is the SQL alias of the analytic_account_line in the query.
'''
2009-04-14 19:47:51 +02:00
res = obj + '.active'
2010-08-16 19:24:05 +02:00
if Transaction().context.get('start_date'):
2009-04-14 19:47:51 +02:00
# Check start_date
2010-08-16 19:24:05 +02:00
time.strptime(str(Transaction().context['start_date']), '%Y-%m-%d')
2013-02-17 15:30:45 +01:00
res += (' AND ' + obj + '.date >= date(\''
+ str(Transaction().context['start_date']) + '\')')
2010-08-16 19:24:05 +02:00
if Transaction().context.get('end_date'):
2009-04-14 19:47:51 +02:00
# Check end_date
2010-08-16 19:24:05 +02:00
time.strptime(str(Transaction().context['end_date']), '%Y-%m-%d')
2013-02-17 15:30:45 +01:00
res += (' AND ' + obj + '.date <= date(\''
+ str(Transaction().context['end_date']) + '\')')
2008-05-23 15:04:38 +02:00
return res
@classmethod
def validate(cls, lines):
super(Line, cls).validate(lines)
for line in lines:
line.check_account()
2012-09-24 22:11:39 +02:00
def check_account(self):
if self.account.type == 'view':
self.raise_user_error('line_on_view_account',
2013-03-25 21:15:56 +00:00
(self.account.rec_name,))
2012-09-24 22:11:39 +02:00
if not self.account.active:
self.raise_user_error('line_on_inactive_account',
2013-03-25 21:15:56 +00:00
(self.account.rec_name,))
2008-05-23 15:04:38 +02:00
2009-02-28 16:58:26 +01:00
class MoveLine(ModelSQL, ModelView):
2012-09-24 22:11:39 +02:00
__name__ = 'account.move.line'
2008-05-23 15:04:38 +02:00
analytic_lines = fields.One2Many('analytic_account.line', 'move_line',
'Analytic Lines')
class OpenAccount(Wizard):
'Open Account'
2012-09-24 22:11:39 +02:00
__name__ = 'analytic_account.line.open_account'
2012-01-02 18:00:00 +01:00
start_state = 'open_'
open_ = StateAction('analytic_account.act_line_form')
2008-05-23 15:04:38 +02:00
2012-09-24 22:11:39 +02:00
def do_open_(self, action):
2012-01-02 18:00:00 +01:00
action['pyson_domain'] = [
('account', '=', Transaction().context['active_id']),
]
2010-08-16 19:24:05 +02:00
if Transaction().context.get('start_date'):
2012-01-02 18:00:00 +01:00
action['pyson_domain'].append(
('date', '>=', Transaction().context['start_date'])
)
2010-08-16 19:24:05 +02:00
if Transaction().context.get('end_date'):
2012-01-02 18:00:00 +01:00
action['pyson_domain'].append(
('date', '<=', Transaction().context['end_date'])
)
action['pyson_domain'] = PYSONEncoder().encode(action['pyson_domain'])
return action, {}
2012-09-24 22:11:39 +02:00
def transition_open_(self):
2012-01-02 18:00:00 +01:00
return 'end'