Files
tryton/modules/account_invoice_history/account.py
T

98 lines
3.5 KiB
Python
Raw Normal View History

2014-12-02 12:54:40 +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.
2026-03-22 21:05:26 +01:00
from collections import defaultdict
2021-06-24 09:21:14 +02:00
from sql import Null
2021-01-30 20:41:29 +01:00
from sql.conditionals import Greatest
from sql.functions import CurrentTimestamp
2012-09-24 22:19:48 +02:00
from trytond import backend
2013-04-08 23:45:02 +02:00
from trytond.model import ModelView, Workflow, fields
2021-12-11 17:58:41 +01:00
from trytond.pool import Pool, PoolMeta
2026-03-22 21:05:26 +01:00
from trytond.tools import sqlite_apply_types
from trytond.transaction import Transaction
2012-09-24 22:19:48 +02:00
2018-06-26 19:11:42 +02:00
class Invoice(metaclass=PoolMeta):
2012-09-24 22:19:48 +02:00
__name__ = 'account.invoice'
numbered_at = fields.Timestamp("Numbered At")
history_datetime = fields.Function(
fields.Timestamp("History DateTime"),
'get_history_datetime')
2012-09-24 22:19:48 +02:00
@classmethod
def __setup__(cls):
2025-04-04 00:45:18 +02:00
super().__setup__()
2021-02-27 15:23:46 +01:00
cls._check_modify_exclude.add('numbered_at')
cls.party.datetime_field = 'history_datetime'
2023-02-20 16:56:51 +01:00
cls.party_tax_identifier.datetime_field = 'history_datetime'
cls.invoice_address.datetime_field = 'history_datetime'
cls.payment_term.datetime_field = 'history_datetime'
2021-01-30 20:41:29 +01:00
@classmethod
def get_history_datetime(cls, invoices, name):
pool = Pool()
Party = pool.get('party.party')
Address = pool.get('party.address')
2023-02-20 16:56:51 +01:00
Identifier = pool.get('party.identifier')
2021-01-30 20:41:29 +01:00
PaymentTerm = pool.get('account.invoice.payment_term')
table = cls.__table__()
party = Party.__table__()
address = Address.__table__()
2023-02-20 16:56:51 +01:00
identifier = Identifier.__table__()
2021-01-30 20:41:29 +01:00
payment_term = PaymentTerm.__table__()
cursor = Transaction().connection.cursor()
2026-03-22 21:05:26 +01:00
datetimes = defaultdict(lambda: None)
query = (table
.join(party, condition=table.party == party.id)
.join(address, condition=table.invoice_address == address.id)
.join(identifier, 'LEFT',
condition=table.party_tax_identifier == identifier.id)
.join(payment_term, 'LEFT',
condition=table.payment_term == payment_term.id)
.select(table.id,
Greatest(table.numbered_at, party.create_date,
address.create_date, identifier.create_date,
payment_term.create_date).as_('history_datetime'),
where=fields.SQL_OPERATORS['in'](table.id, map(int, invoices))
& (table.numbered_at != Null)
& (table.state.in_(cls._history_states()))))
if backend.name == 'sqlite':
sqlite_apply_types(query, [None, 'DATETIME'])
cursor.execute(*query)
datetimes.update(cursor)
2021-01-30 20:41:29 +01:00
return datetimes
@classmethod
def _history_states(cls):
return ['posted', 'paid', 'cancelled']
2016-08-10 23:00:16 +02:00
@classmethod
def set_number(cls, invoices):
numbered = [i for i in invoices if not i.number or not i.numbered_at]
2025-04-04 00:45:18 +02:00
super().set_number(invoices)
if numbered:
cls.write(numbered, {
'numbered_at': CurrentTimestamp(),
2012-09-24 22:19:48 +02:00
})
2013-04-08 23:45:02 +02:00
@classmethod
@ModelView.button
@Workflow.transition('draft')
def draft(cls, invoices):
2025-04-04 00:45:18 +02:00
super().draft(invoices)
2013-04-08 23:45:02 +02:00
cls.write(invoices, {
'numbered_at': None,
2013-04-08 23:45:02 +02:00
})
2012-09-24 22:19:48 +02:00
@classmethod
def copy(cls, invoices, default=None):
2009-03-28 19:49:20 +01:00
if default is None:
default = {}
2018-09-01 01:02:03 +02:00
else:
default = default.copy()
default.setdefault('numbered_at', None)
2025-04-04 00:45:18 +02:00
return super().copy(invoices, default=default)