Set sii_operation_key when reset SII keys

This commit is contained in:
Raimon Esteve 2017-07-17 11:14:11 +02:00
parent db10a68229
commit 7ad168a3b1
3 changed files with 300 additions and 7 deletions

View file

@ -213,6 +213,8 @@ class Invoice:
to_write = []
for record in records:
sii_keys = record._set_sii_keys()
sii_keys['sii_operation_key'] = ('R1' if record.type in [
'in_credit_note', 'out_credit_note'] else 'F1')
to_write.extend(([record], sii_keys))
if to_write:

293
tests/scenario_aeat_sii.rst Normal file
View file

@ -0,0 +1,293 @@
=================
AEAT SII Scenario
=================
Imports::
>>> import datetime
>>> from dateutil.relativedelta import relativedelta
>>> from decimal import Decimal
>>> from operator import attrgetter
>>> from proteus import config, Model, Wizard
>>> today = datetime.date.today()
Create database::
>>> config = config.set_trytond()
>>> config.pool.test = True
Install account_sii::
>>> Module = Model.get('ir.module.module')
>>> aeat_sii_module, = Module.find(
... [('name', '=', 'aeat_sii')])
>>> Module.install([aeat_sii_module.id], config.context)
>>> Wizard('ir.module.module.install_upgrade').execute('upgrade')
Create company::
>>> Currency = Model.get('currency.currency')
>>> CurrencyRate = Model.get('currency.currency.rate')
>>> currencies = Currency.find([('code', '=', 'USD')])
>>> if not currencies:
... currency = Currency(name='US Dollar', symbol=u'$', code='USD',
... rounding=Decimal('0.01'), mon_grouping='[]',
... mon_decimal_point='.')
... currency.save()
... CurrencyRate(date=today + relativedelta(month=1, day=1),
... rate=Decimal('1.0'), currency=currency).save()
... else:
... currency, = currencies
>>> Company = Model.get('company.company')
>>> Party = Model.get('party.party')
>>> company_config = Wizard('company.company.config')
>>> company_config.execute('company')
>>> company = company_config.form
>>> party = Party(name='Dunder Mifflin')
>>> party.save()
>>> company.party = party
>>> company.currency = currency
>>> company_config.execute('add')
>>> company, = Company.find([])
Reload the context::
>>> User = Model.get('res.user')
>>> config._context = User.get_preferences(True, config.context)
Create fiscal year::
>>> FiscalYear = Model.get('account.fiscalyear')
>>> Sequence = Model.get('ir.sequence')
>>> SequenceStrict = Model.get('ir.sequence.strict')
>>> fiscalyear = FiscalYear(name=str(today.year))
>>> fiscalyear.start_date = today + relativedelta(month=1, day=1)
>>> fiscalyear.end_date = today + relativedelta(month=12, day=31)
>>> fiscalyear.company = company
>>> post_move_seq = Sequence(name=str(today.year), code='account.move',
... company=company)
>>> post_move_seq.save()
>>> fiscalyear.post_move_sequence = post_move_seq
>>> invoice_seq = SequenceStrict(name=str(today.year),
... code='account.invoice', company=company)
>>> invoice_seq.save()
>>> fiscalyear.out_invoice_sequence = invoice_seq
>>> fiscalyear.in_invoice_sequence = invoice_seq
>>> fiscalyear.out_credit_note_sequence = invoice_seq
>>> fiscalyear.in_credit_note_sequence = invoice_seq
>>> fiscalyear.save()
>>> FiscalYear.create_period([fiscalyear.id], config.context)
>>> Period = Model.get('account.period')
>>> period, = Period.find([
... ('start_date', '>=', today.replace(day=1)),
... ('end_date', '<=', today.replace(day=1) + relativedelta(months=+1)),
... ], limit=1)
Create chart of accounts::
>>> AccountTemplate = Model.get('account.account.template')
>>> Account = Model.get('account.account')
>>> account_template, = AccountTemplate.find([('parent', '=', None)])
>>> create_chart = Wizard('account.create_chart')
>>> create_chart.execute('account')
>>> create_chart.form.account_template = account_template
>>> create_chart.form.company = company
>>> create_chart.execute('create_account')
>>> receivable, = Account.find([
... ('kind', '=', 'receivable'),
... ('company', '=', company.id),
... ])
>>> payable, = Account.find([
... ('kind', '=', 'payable'),
... ('company', '=', company.id),
... ])
>>> revenue, = Account.find([
... ('kind', '=', 'revenue'),
... ('company', '=', company.id),
... ])
>>> expense, = Account.find([
... ('kind', '=', 'expense'),
... ('company', '=', company.id),
... ])
>>> account_tax, = Account.find([
... ('kind', '=', 'other'),
... ('company', '=', company.id),
... ('name', '=', 'Main Tax'),
... ])
>>> create_chart.form.account_receivable = receivable
>>> create_chart.form.account_payable = payable
>>> create_chart.execute('create_properties')
Create tax::
>>> TaxCode = Model.get('account.tax.code')
>>> Tax = Model.get('account.tax')
>>> tax = Tax()
>>> tax.name = 'Tax'
>>> tax.description = 'Tax'
>>> tax.type = 'percentage'
>>> tax.rate = Decimal('.10')
>>> tax.invoice_account = account_tax
>>> tax.credit_note_account = account_tax
>>> invoice_base_code = TaxCode(name='invoice base')
>>> invoice_base_code.save()
>>> tax.invoice_base_code = invoice_base_code
>>> invoice_tax_code = TaxCode(name='invoice tax')
>>> invoice_tax_code.save()
>>> tax.invoice_tax_code = invoice_tax_code
>>> credit_note_base_code = TaxCode(name='credit note base')
>>> credit_note_base_code.save()
>>> tax.credit_note_base_code = credit_note_base_code
>>> credit_note_tax_code = TaxCode(name='credit note tax')
>>> credit_note_tax_code.save()
>>> tax.credit_note_tax_code = credit_note_tax_code
>>> tax.sii_book_key = 'E'
>>> tax.sii_issued_key = '01'
>>> tax.sii_subjected_key = 'S1'
>>> tax.save()
>>> invoice_base_code = tax.invoice_base_code
>>> invoice_tax_code = tax.invoice_tax_code
>>> credit_note_base_code = tax.credit_note_base_code
>>> credit_note_tax_code = tax.credit_note_tax_code
Create party::
>>> Party = Model.get('party.party')
>>> party = Party(name='Party')
>>> party.save()
Create product::
>>> ProductUom = Model.get('product.uom')
>>> unit, = ProductUom.find([('name', '=', 'Unit')])
>>> ProductTemplate = Model.get('product.template')
>>> Product = Model.get('product.product')
>>> product = Product()
>>> template = ProductTemplate()
>>> template.name = 'product'
>>> template.default_uom = unit
>>> template.type = 'service'
>>> template.list_price = Decimal('40')
>>> template.cost_price = Decimal('25')
>>> template.account_expense = expense
>>> template.account_revenue = revenue
>>> template.customer_taxes.append(tax)
>>> template.save()
>>> product.template = template
>>> product.save()
Create payment term::
>>> PaymentTerm = Model.get('account.invoice.payment_term')
>>> PaymentTermLine = Model.get('account.invoice.payment_term.line')
>>> payment_term = PaymentTerm(name='Term')
>>> payment_term_line = PaymentTermLine(type='percent', days=20,
... percentage=Decimal(50))
>>> payment_term.lines.append(payment_term_line)
>>> payment_term_line = PaymentTermLine(type='remainder', days=40)
>>> payment_term.lines.append(payment_term_line)
>>> payment_term.save()
Create invoice::
>>> Invoice = Model.get('account.invoice')
>>> InvoiceLine = Model.get('account.invoice.line')
>>> invoice = Invoice()
>>> invoice.party = party
>>> invoice.payment_term = payment_term
>>> line = InvoiceLine()
>>> invoice.lines.append(line)
>>> line.product = product
>>> line.quantity = 5
>>> line.unit_price = Decimal('40')
>>> line = InvoiceLine()
>>> invoice.lines.append(line)
>>> line.account = revenue
>>> line.description = 'Test'
>>> line.quantity = 1
>>> line.unit_price = Decimal(20)
>>> invoice.save()
>>> invoice.sii_book_key
u'E'
>>> invoice.sii_operation_key
u'F1'
>>> invoice.sii_issued_key
u'01'
>>> invoice.sii_book_key = 'I'
>>> invoice.sii_operation_key = 'F2'
>>> invoice.sii_issued_key = '02'
>>> invoice.save()
>>> invoice.reload()
>>> invoice.sii_book_key == 'I'
True
>>> invoice.click('reset_sii_keys')
>>> invoice.reload()
>>> invoice.sii_book_key == 'E'
True
>>> invoice.sii_operation_key == 'F1'
True
>>> invoice.click('post')
>>> invoice.state
u'posted'
Create Credit invoice::
>>> invoice = Invoice()
>>> invoice.type = 'out_credit_note'
>>> invoice.party = party
>>> invoice.payment_term = payment_term
>>> line = InvoiceLine()
>>> invoice.lines.append(line)
>>> line.product = product
>>> line.quantity = 5
>>> line.unit_price = Decimal('40')
>>> line = InvoiceLine()
>>> invoice.lines.append(line)
>>> line.account = revenue
>>> line.description = 'Test'
>>> line.quantity = 1
>>> line.unit_price = Decimal(20)
>>> invoice.sii_operation_key = 'R1'
>>> invoice.save()
>>> invoice.sii_book_key
u'E'
>>> invoice.sii_operation_key
u'R1'
>>> invoice.sii_issued_key
u'01'
>>> invoice.sii_book_key = 'I'
>>> invoice.sii_operation_key = 'F2'
>>> invoice.sii_issued_key = '02'
>>> invoice.save()
>>> invoice.reload()
>>> invoice.sii_book_key == 'I'
True
>>> invoice.click('reset_sii_keys')
>>> invoice.reload()
>>> invoice.sii_book_key == 'E'
True
>>> invoice.sii_operation_key == 'R1'
True
>>> invoice.click('post')
>>> invoice.state
u'posted'
Create AEAT Report::
>>> AEATReport = Model.get('aeat.sii.report')
>>> report = AEATReport()
>>> report.fiscalyear = fiscalyear
>>> report.period = period
>>> report.operation_type = 'A0'
>>> report.book = 'E'
>>> report.save()
>>> report.state
u'draft'
>>> report.click('load_invoices')
>>> len(report.lines)
2

View file

@ -1,11 +1,10 @@
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
import unittest
# import doctest
import doctest
import trytond.tests.test_tryton
from trytond.tests.test_tryton import test_view, test_depends
# TODO: Remove if no sceneario needed.
# from trytond.tests.test_tryton import doctest_setup, doctest_teardown
from trytond.tests.test_tryton import doctest_setup, doctest_teardown
class AeatSIITestCase(unittest.TestCase):
@ -27,8 +26,7 @@ def suite():
suite = trytond.tests.test_tryton.suite()
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(
AeatSIITestCase))
# TODO: remove if no scenario needed.
#suite.addTests(doctest.DocFileSuite('scenario_aeat_sii.rst',
# setUp=doctest_setup, tearDown=doctest_teardown, encoding='utf-8',
# optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
suite.addTests(doctest.DocFileSuite('scenario_aeat_sii.rst',
setUp=doctest_setup, tearDown=doctest_teardown, encoding='utf-8',
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
return suite