trytonpsk-sale_pos/invoice.py

69 lines
2.6 KiB
Python
Raw Normal View History

2020-04-15 21:47:31 +02: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-01-13 18:29:40 +01:00
from trytond.pool import PoolMeta
2022-02-18 18:28:04 +01:00
from trytond.model import fields, Workflow, ModelView
2020-04-15 21:47:31 +02:00
from trytond.pyson import Eval, Bool
2022-02-18 18:28:04 +01:00
from trytond.i18n import gettext
from trytond.model.exceptions import AccessError
from trytond.pool import Pool
2020-04-15 21:47:31 +02:00
class Invoice(metaclass=PoolMeta):
__name__ = 'account.invoice'
2022-02-07 18:50:54 +01:00
position = fields.Char('Position', states={'invisible': Eval('type') != 'out'})
turn = fields.Integer('Turn', states={'invisible': Eval('type') != 'out'})
2020-04-15 21:47:31 +02:00
@classmethod
def __setup__(cls):
super(Invoice, cls).__setup__()
cls.party.states['readonly'] = Bool(Eval('number'))
2022-10-18 18:21:25 +02:00
if 'turn' not in cls._check_modify_exclude:
cls._check_modify_exclude.add('turn')
2022-02-18 18:28:04 +01:00
@classmethod
@ModelView.button
@Workflow.transition('cancelled')
def cancel(cls, invoices):
pool = Pool()
Move = pool.get('account.move')
Line = pool.get('account.move.line')
cancel_moves = []
delete_moves = []
to_save = []
for invoice in invoices:
if invoice.move:
if invoice.move.state == 'draft':
delete_moves.append(invoice.move)
elif not invoice.cancel_move:
if (invoice.type == 'out'
and not invoice.company.cancel_invoice_out):
raise AccessError(
2023-10-21 01:54:42 +02:00
gettext('account_invoice.msg_invoice_customer_cancel_move',
2022-02-18 18:28:04 +01:00
invoice=invoice.rec_name))
invoice.cancel_move = invoice.move.cancel()
to_save.append(invoice)
cancel_moves.append(invoice.cancel_move)
if cancel_moves:
Move.save(cancel_moves)
cls.save(to_save)
if delete_moves:
Move.delete(delete_moves)
if cancel_moves:
Move.post(cancel_moves)
# Write state before reconcile to prevent invoice to go to paid state
cls.write(invoices, {
'state': 'cancelled',
})
for invoice in invoices:
if not invoice.move or not invoice.cancel_move:
continue
to_reconcile = []
for line in invoice.move.lines + invoice.cancel_move.lines:
if line.account == invoice.account:
to_reconcile.append(line)
Line.reconcile(to_reconcile)
cls._clean_payments(invoices)