Files
tryton/modules/account_eu/account.py
T

144 lines
5.3 KiB
Python
Raw Normal View History

2017-09-28 09:06:04 +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.
from sql import Literal, Null
2020-10-12 00:25:22 +02:00
from sql.aggregate import Min, Sum
from sql.functions import CurrentTimestamp
2017-09-28 09:06:04 +02:00
from trytond.model import ModelSQL, ModelView, fields
from trytond.modules.account.exceptions import FiscalYearNotFoundError
2021-08-21 09:09:55 +02:00
from trytond.modules.currency.fields import Monetary
2021-12-11 17:58:41 +01:00
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Bool, Eval
from trytond.transaction import Transaction
2021-08-21 09:09:55 +02:00
2017-09-28 09:06:04 +02:00
2018-06-26 19:11:41 +02:00
class TaxTemplate(metaclass=PoolMeta):
2017-09-28 09:06:04 +02:00
__name__ = 'account.tax.template'
ec_sales_list_code = fields.Char("EC Sales List Code")
def _get_tax_value(self, tax=None):
value = super(TaxTemplate, self)._get_tax_value(tax=tax)
if not tax or tax.ec_sales_list_code != self.ec_sales_list_code:
value['ec_sales_list_code'] = self.ec_sales_list_code
2017-09-28 09:06:04 +02:00
return value
2018-06-26 19:11:41 +02:00
class Tax(metaclass=PoolMeta):
2017-09-28 09:06:04 +02:00
__name__ = 'account.tax'
2018-03-09 11:41:24 +01:00
ec_sales_list_code = fields.Char("EC Sales List Code",
states={
'readonly': (Bool(Eval('template', -1))
& ~Eval('template_override', False)),
})
2017-09-28 09:06:04 +02:00
class ECSalesList(ModelSQL, ModelView):
"EC Sales List"
__name__ = 'account.ec_sales_list'
company_tax_identifier = fields.Many2One(
'party.identifier', "Company Tax Identifier")
2019-04-14 14:20:51 +02:00
party = fields.Many2One('party.party', "Party")
2017-09-28 09:06:04 +02:00
party_tax_identifier = fields.Many2One(
'party.identifier', "Party Tax Identifier")
code = fields.Char("Code")
2021-08-21 09:09:55 +02:00
amount = Monetary(
"Amount", currency='currency', digits='currency')
2017-09-28 09:06:04 +02:00
currency = fields.Many2One('currency.currency', "Currency")
@classmethod
def table_query(cls):
pool = Pool()
2020-10-12 00:25:22 +02:00
Company = pool.get('company.company')
2017-09-28 09:06:04 +02:00
Invoice = pool.get('account.invoice')
Move = pool.get('account.move')
2020-10-12 00:25:22 +02:00
Line = pool.get('account.move.line')
TaxLine = pool.get('account.tax.line')
2017-09-28 09:06:04 +02:00
Period = pool.get('account.period')
Tax = pool.get('account.tax')
context = Transaction().context
2020-10-12 00:25:22 +02:00
company = Company.__table__()
2017-09-28 09:06:04 +02:00
invoice = Invoice.__table__()
move = Move.__table__()
2020-10-12 00:25:22 +02:00
line = Line.__table__()
tax_line = TaxLine.__table__()
2017-09-28 09:06:04 +02:00
period = Period.__table__()
tax = Tax.__table__()
where = invoice.company == context.get('company')
if context.get('fiscalyear'):
where &= (period.fiscalyear == context.get('fiscalyear'))
2017-09-28 09:06:04 +02:00
if context.get('period'):
where &= (period.id == context.get('period'))
if context.get('start_date'):
where &= (move.date >= context.get('start_date'))
if context.get('end_date'):
where &= (move.date <= context.get('end_date'))
2017-09-28 09:06:04 +02:00
where &= ((tax.ec_sales_list_code != Null)
& (tax.ec_sales_list_code != ''))
2020-10-12 00:25:22 +02:00
where &= tax_line.type == 'base'
2017-09-28 09:06:04 +02:00
where &= invoice.type == 'out'
2020-10-12 00:25:22 +02:00
return (tax_line
.join(tax, condition=tax_line.tax == tax.id)
.join(line, condition=tax_line.move_line == line.id)
.join(move, condition=line.move == move.id)
2017-09-28 09:06:04 +02:00
.join(period, condition=move.period == period.id)
2020-10-12 00:25:22 +02:00
.join(invoice, condition=invoice.move == move.id)
.join(company, condition=company.id == invoice.company)
2017-09-28 09:06:04 +02:00
.select(
2020-10-12 00:25:22 +02:00
Min(tax_line.id).as_('id'),
2017-09-28 09:06:04 +02:00
Literal(0).as_('create_uid'),
2020-10-12 00:25:22 +02:00
CurrentTimestamp().as_('create_date'),
cls.write_uid.sql_cast(Literal(Null)).as_('write_uid'),
cls.write_date.sql_cast(Literal(Null)).as_('write_date'),
2017-09-28 09:06:04 +02:00
invoice.tax_identifier.as_('company_tax_identifier'),
2019-04-14 14:20:51 +02:00
invoice.party.as_('party'),
2017-09-28 09:06:04 +02:00
invoice.party_tax_identifier.as_('party_tax_identifier'),
tax.ec_sales_list_code.as_('code'),
2020-10-12 00:25:22 +02:00
Sum(tax_line.amount).as_('amount'),
company.currency.as_('currency'),
2017-09-28 09:06:04 +02:00
where=where,
group_by=[
invoice.tax_identifier,
2019-04-14 14:20:51 +02:00
invoice.party,
2017-09-28 09:06:04 +02:00
invoice.party_tax_identifier,
tax.ec_sales_list_code,
2020-10-12 00:25:22 +02:00
company.currency,
2017-09-28 09:06:04 +02:00
]))
class ECSalesListContext(ModelView):
"EC Sales List Context"
__name__ = 'account.ec_sales_list.context'
company = fields.Many2One('company.company', "Company", required=True)
fiscalyear = fields.Many2One(
'account.fiscalyear', "Fiscal Year", required=True,
domain=[
('company', '=', Eval('company')),
])
2017-09-28 09:06:04 +02:00
period = fields.Many2One(
'account.period', "Period",
domain=[
('fiscalyear', '=', Eval('fiscalyear')),
])
2017-09-28 09:06:04 +02:00
@classmethod
def default_company(cls):
return Transaction().context.get('company')
@classmethod
def default_fiscalyear(cls):
pool = Pool()
FiscalYear = pool.get('account.fiscalyear')
context = Transaction().context
if 'fiscalyear' not in context:
try:
fiscalyear = FiscalYear.find(
cls.default_company(), test_state=False)
except FiscalYearNotFoundError:
return None
return fiscalyear.id
return context['fiscalyear']