2012-11-15 19:33:08 +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.
|
2021-12-11 17:58:41 +01:00
|
|
|
from trytond.model import Unique, fields
|
2012-11-15 19:33:08 +01:00
|
|
|
from trytond.pool import Pool, PoolMeta
|
2021-12-11 17:58:41 +01:00
|
|
|
from trytond.pyson import Eval
|
2018-08-31 14:58:05 +02:00
|
|
|
from trytond.transaction import Transaction
|
2012-11-15 19:33:08 +01:00
|
|
|
|
|
|
|
|
|
2018-06-26 19:11:40 +02:00
|
|
|
class InvoiceLine(metaclass=PoolMeta):
|
2012-11-15 19:33:08 +01:00
|
|
|
__name__ = 'account.invoice.line'
|
|
|
|
|
asset = fields.Many2One('account.asset', 'Asset', domain=[
|
|
|
|
|
('state', '=', 'running'),
|
|
|
|
|
('product', '=', Eval('product')),
|
|
|
|
|
],
|
|
|
|
|
states={
|
|
|
|
|
'invisible': (~Eval('is_assets_depreciable', False)
|
2022-01-03 22:39:35 +01:00
|
|
|
| (Eval('invoice_type') != 'out')),
|
|
|
|
|
'readonly': Eval('invoice_state') != 'draft',
|
2022-04-08 19:07:12 +02:00
|
|
|
})
|
2012-11-15 19:33:08 +01:00
|
|
|
is_assets_depreciable = fields.Function(fields.Boolean(
|
2014-01-26 14:28:47 +01:00
|
|
|
'Is Assets depreciable'),
|
2012-11-15 19:33:08 +01:00
|
|
|
'on_change_with_is_assets_depreciable')
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def __setup__(cls):
|
|
|
|
|
super(InvoiceLine, cls).__setup__()
|
2015-06-22 16:58:27 +02:00
|
|
|
table = cls.__table__()
|
2012-11-15 19:33:08 +01:00
|
|
|
cls._sql_constraints += [
|
2015-06-22 16:58:27 +02:00
|
|
|
('asset_uniq', Unique(table, table.asset),
|
2018-12-29 14:20:28 +01:00
|
|
|
'account_asset.msg_invoice_line_asset_unique'),
|
2012-11-15 19:33:08 +01:00
|
|
|
]
|
|
|
|
|
|
2019-03-24 18:00:01 +01:00
|
|
|
@classmethod
|
|
|
|
|
def _account_domain(cls, type_):
|
|
|
|
|
domain = super()._account_domain(type_)
|
|
|
|
|
if type_ == 'in':
|
|
|
|
|
domain.append(('type.fixed_asset', '=', True))
|
|
|
|
|
return domain
|
|
|
|
|
|
2018-08-31 14:58:05 +02:00
|
|
|
@fields.depends('product', 'invoice', 'invoice_type',
|
|
|
|
|
'_parent_invoice.invoice_date', '_parent_invoice.accounting_date')
|
2012-11-15 19:33:08 +01:00
|
|
|
def on_change_product(self):
|
2014-10-29 20:39:04 +01:00
|
|
|
super(InvoiceLine, self).on_change_product()
|
2014-10-06 22:28:08 +02:00
|
|
|
if self.invoice and self.invoice.type:
|
|
|
|
|
type_ = self.invoice.type
|
|
|
|
|
else:
|
|
|
|
|
type_ = self.invoice_type
|
2012-11-15 19:33:08 +01:00
|
|
|
|
2016-03-20 22:58:04 +01:00
|
|
|
if (self.product and type_ == 'in'
|
2014-10-06 22:28:08 +02:00
|
|
|
and self.product.type == 'assets'
|
|
|
|
|
and self.product.depreciable):
|
2018-08-31 14:58:05 +02:00
|
|
|
date = (self.invoice.accounting_date or self.invoice.invoice_date
|
|
|
|
|
if self.invoice else None)
|
|
|
|
|
with Transaction().set_context(date=date):
|
|
|
|
|
self.account = self.product.account_asset_used
|
2012-11-15 19:33:08 +01:00
|
|
|
|
2014-01-26 14:28:47 +01:00
|
|
|
@fields.depends('asset', 'unit')
|
2012-11-15 19:33:08 +01:00
|
|
|
def on_change_asset(self):
|
|
|
|
|
Uom = Pool().get('product.uom')
|
|
|
|
|
|
|
|
|
|
if self.asset:
|
|
|
|
|
quantity = self.asset.quantity
|
|
|
|
|
if self.unit:
|
|
|
|
|
quantity = Uom.compute_qty(self.asset.unit, quantity,
|
|
|
|
|
self.unit)
|
2014-10-29 20:39:04 +01:00
|
|
|
self.quantity = quantity
|
2012-11-15 19:33:08 +01:00
|
|
|
else:
|
2014-10-29 20:39:04 +01:00
|
|
|
self.quantity = quantity
|
|
|
|
|
self.unit = self.unit
|
2012-11-15 19:33:08 +01:00
|
|
|
|
2014-01-26 14:28:47 +01:00
|
|
|
@fields.depends('product')
|
2012-11-15 19:33:08 +01:00
|
|
|
def on_change_with_is_assets_depreciable(self, name=None):
|
|
|
|
|
if self.product:
|
|
|
|
|
return self.product.type == 'assets' and self.product.depreciable
|
|
|
|
|
return False
|
|
|
|
|
|
2016-08-10 22:59:00 +02:00
|
|
|
def get_move_lines(self):
|
2012-11-15 19:33:08 +01:00
|
|
|
Asset = Pool().get('account.asset')
|
|
|
|
|
if self.asset:
|
2021-05-02 10:54:50 +02:00
|
|
|
date = self.invoice.accounting_date or self.invoice.invoice_date
|
|
|
|
|
Asset.close([self.asset], account=self.account, date=date)
|
2016-08-10 22:59:00 +02:00
|
|
|
return super(InvoiceLine, self).get_move_lines()
|