Files
tryton/modules/account_deposit/invoice.py
T

154 lines
5.1 KiB
Python
Raw Normal View History

2015-03-05 12:48:22 +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-03-13 23:14:19 +01:00
from decimal import Decimal
2018-12-29 14:20:28 +01:00
from trytond.i18n import gettext
2021-02-27 15:23:46 +01:00
from trytond.model import ModelView, fields
2021-12-11 17:58:41 +01:00
from trytond.pool import Pool, PoolMeta
2016-07-18 14:06:56 +02:00
from trytond.pyson import Eval
2021-12-11 17:58:41 +01:00
from trytond.wizard import Button, StateTransition, StateView, Wizard
2015-03-05 12:48:22 +01:00
2018-12-29 14:20:28 +01:00
from .exceptions import DepositError
2015-03-05 12:48:22 +01:00
2018-06-26 19:11:40 +02:00
class Invoice(metaclass=PoolMeta):
2015-03-05 12:48:22 +01:00
__name__ = 'account.invoice'
@classmethod
def __setup__(cls):
super(Invoice, cls).__setup__()
cls._buttons.update({
'recall_deposit': {
'invisible': Eval('state') != 'draft',
2018-02-19 11:36:40 +01:00
'depends': ['state'],
2015-03-05 12:48:22 +01:00
},
})
@classmethod
2021-02-27 15:23:46 +01:00
def _post(cls, invoices):
super()._post(invoices)
2015-03-05 12:48:22 +01:00
cls.check_deposit(invoices)
@classmethod
@ModelView.button_action('account_deposit.wizard_recall_deposit')
def recall_deposit(cls, invoices):
pass
2021-03-13 23:14:19 +01:00
def call_deposit(self, account, description, maximum=None):
2018-03-08 15:39:53 +01:00
pool = Pool()
InvoiceLine = pool.get('account.invoice.line')
2023-03-08 16:49:14 +01:00
balance = self.party.get_deposit_balance(
account, currency=self.currency)
2021-03-13 23:14:19 +01:00
if maximum is None:
maximum = self.total_amount
total_amount = min(maximum, self.total_amount, key=abs)
2018-03-08 15:39:53 +01:00
2021-03-13 23:14:19 +01:00
amount = Decimal(0)
2018-03-08 15:39:53 +01:00
if self.type.startswith('in'):
2021-03-13 23:14:19 +01:00
if balance > 0 and total_amount > 0:
amount = -min(balance, total_amount)
2018-03-08 15:39:53 +01:00
else:
2021-03-13 23:14:19 +01:00
if balance < 0 and total_amount > 0:
amount = -min(-balance, total_amount)
2018-03-08 15:39:53 +01:00
to_delete = []
for line in self.lines:
if line.account == account:
to_delete.append(line)
if amount < 0:
line = self._get_deposit_recall_invoice_line(
amount, account, description)
2018-11-28 09:58:26 +01:00
try:
line.sequence = max(l.sequence for l in self.lines
if l.sequence is not None)
except ValueError:
pass
2018-03-08 15:39:53 +01:00
line.save()
2021-03-13 23:14:19 +01:00
else:
amount = Decimal(0)
2018-03-08 15:39:53 +01:00
if to_delete:
InvoiceLine.delete(to_delete)
2021-03-13 23:14:19 +01:00
return amount
2018-03-08 15:39:53 +01:00
def _get_deposit_recall_invoice_line(self, amount, account, description):
pool = Pool()
Line = pool.get('account.invoice.line')
line = Line(
invoice=self,
company=self.company,
type='line',
quantity=1,
account=account,
unit_price=amount,
description=description,
)
# Set taxes
line.on_change_account()
return line
2015-03-05 12:48:22 +01:00
@classmethod
def check_deposit(cls, invoices):
to_check = set()
for invoice in invoices:
for line in invoice.lines:
if line.type != 'line':
continue
2019-03-24 18:00:01 +01:00
if line.account.type.deposit:
if line.amount < 0:
2015-03-05 12:48:22 +01:00
sign = 1 if invoice.type.startswith('in') else -1
to_check.add((invoice.party, line.account, sign))
for party, account, sign in to_check:
if not party.check_deposit(account, sign):
2018-12-29 14:20:28 +01:00
raise DepositError(
gettext('account_deposit.msg_deposit_not_enough',
account=account.rec_name,
party=party.rec_name))
2015-03-05 12:48:22 +01:00
2018-06-26 19:11:40 +02:00
class InvoiceLine(metaclass=PoolMeta):
2015-03-05 12:48:22 +01:00
__name__ = 'account.invoice.line'
@classmethod
def _account_domain(cls, type_):
domain = super(InvoiceLine, cls)._account_domain(type_)
2019-03-24 18:00:01 +01:00
return domain + [('type.deposit', '=', True)]
2015-03-05 12:48:22 +01:00
class DepositRecall(Wizard):
'Recall deposit on Invoice'
__name__ = 'account.invoice.recall_deposit'
start = StateView('account.invoice.recall_deposit.start',
'account_deposit.recall_deposit_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Recall', 'recall', 'tryton-ok', default=True),
])
recall = StateTransition()
def default_start(self, fields):
return {
'company': self.record.company.id,
2023-03-08 16:49:14 +01:00
'currency': self.record.currency.id,
2015-03-05 12:48:22 +01:00
}
def transition_recall(self):
self.record.call_deposit(self.start.account, self.start.description)
2015-03-05 12:48:22 +01:00
return 'end'
class DepositRecallStart(ModelView):
'Recall deposit on Invoice'
__name__ = 'account.invoice.recall_deposit.start'
company = fields.Many2One('company.company', 'Company', readonly=True)
2023-03-08 16:49:14 +01:00
currency = fields.Many2One('currency.currency', "Currency", readonly=True)
2015-03-05 12:48:22 +01:00
account = fields.Many2One('account.account', 'Account', required=True,
domain=[
2019-03-24 18:00:01 +01:00
('type.deposit', '=', True),
2015-03-05 12:48:22 +01:00
('company', '=', Eval('company', -1)),
2023-03-08 16:49:14 +01:00
['OR',
('currency', '=', Eval('currency', -1)),
('second_currency', '=', Eval('currency', -1)),
],
])
2015-03-05 12:48:22 +01:00
description = fields.Text('Description', required=True)