2
0

[FIX] cleanup

This commit is contained in:
gilles 2017-03-14 19:09:03 +01:00 committed by Zina Rasoamanana
parent d6efba2b6e
commit 383441d230
6 changed files with 88 additions and 83 deletions

View File

@ -23,7 +23,7 @@ Usage
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot :alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/92/8.0 :target: https://runbot.odoo-community.org/runbot/92/10.0
Bug Tracker Bug Tracker
=========== ===========

View File

@ -10,14 +10,6 @@
"license": "AGPL-3", "license": "AGPL-3",
"category": "Accounting", "category": "Accounting",
"depends": ["account"], "depends": ["account"],
"description": """
Account Invoice Constraint Chronology
This module helps ensuring the chronology of invoice numbers.
It prevents the validation of invoices when:
* there are draft invoices with an anterior date
* there are validated invoices with a posterior date
""",
"test": ["../account/test/account_minimal_test.xml"],
"data": ["view/account_view.xml"], "data": ["view/account_view.xml"],
'installable': True, 'installable': True,
} }

View File

@ -5,15 +5,15 @@
from odoo import models, fields, api from odoo import models, fields, api
class account_journal(models.Model): class AccountJournal(models.Model):
_inherit = ['account.journal'] _inherit = ['account.journal']
check_chronology = fields.Boolean(string='Check Chronology', default=False) check_chronology = fields.Boolean(string='Check Chronology', default=False)
@api.one @api.multi
@api.onchange('type') @api.onchange('type')
def on_change_type(self): def on_change_type(self):
if self.type not in ['sale', 'purchase', 'sale_refund', for rec in self:
'purchase_refund']: if rec.type not in ['sale', 'purchase']:
self.check_chronology = False rec.check_chronology = False
return True return True

View File

@ -6,13 +6,13 @@ from odoo import models, api, fields, _
from odoo.exceptions import UserError from odoo.exceptions import UserError
class account_invoice(models.Model): class AccountInvoice(models.Model):
_inherit = "account.invoice" _inherit = "account.invoice"
@api.multi @api.multi
def action_move_create(self): def action_move_create(self):
previously_validated = self.filtered(lambda inv: inv.move_name) previously_validated = self.filtered(lambda inv: inv.move_name)
res = super(account_invoice, self).action_move_create() res = super(AccountInvoice, self).action_move_create()
for inv in self: for inv in self:
if inv.journal_id.check_chronology: if inv.journal_id.check_chronology:
invoices = \ invoices = \

View File

@ -8,92 +8,107 @@ from datetime import datetime, timedelta
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
class TestAccountConstraintChronology(common.TransactionCase):
def setUp(self):
super(TestAccountConstraintChronology, self).setUp()
# Needed to create invoice
self.account_type1 = self.env['account.account.type'].\
create({'name': 'acc type test 1',
'type': 'receivable',
'include_initial_balance': True})
self.account_type2 = self.env['account.account.type']. \
create({'name': 'acc type test 2',
'type': 'other',
'include_initial_balance': True})
self.account_account = self.env['account.account'].\
create({'name': 'acc test',
'code': 'X2020',
'user_type_id': self.account_type1.id,
'reconcile': True})
self.account_account_line = self.env['account.account']. \
create({'name': 'acc inv line test',
'code': 'X2021',
'user_type_id': self.account_type2.id,
'reconcile': True})
self.sequence = self.env['ir.sequence'].create(
{'name': 'Journal Sale',
'prefix': 'SALE', 'padding': 6,
'company_id': self.env.ref("base.main_company").id})
self.account_journal_sale = self.env['account.journal']\
.create({'name': 'Sale journal',
'code': 'SALE',
'type': 'sale',
'sequence_id': self.sequence.id})
self.product = self.env['product.product'].create(
{'name': 'product name'})
self.analytic_account = self.env['account.analytic.account'].\
create({'name': 'test account'})
def get_journal_check(self, value): def get_journal_check(self, value):
sale_journal_obj = self.AccountJournal.env['account.journal'].\ journal = self.account_journal_sale.copy()
search([('type', '=', 'sale')], limit=1)
journal = sale_journal_obj.copy()
journal.check_chronology = value journal.check_chronology = value
return journal return journal
def create_simple_invoice(self, journal_id, date): def create_simple_invoice(self, journal_id, date):
invoice_account = self.env['account.account'].\
search([('user_type_id', '=',
self.env.ref(
'account.data_account_type_receivable'
).id)], limit=1).id
invoice_line_account = self.env['account.account'].\
search([('user_type_id', '=',
self.env.ref(
'account.data_account_type_expenses'
).id)], limit=1).id
analytic_account = self.env['account.analytic.account'].\
create({'name': 'test account'})
invoice = self.env['account.invoice'].create( invoice = self.env['account.invoice'].create(
{'partner_id': self.env.ref('base.res_partner_2').id, {'partner_id': self.env.ref('base.res_partner_2').id,
'account_id': invoice_account, 'account_id': self.account_account.id,
'type': 'in_invoice', 'type': 'in_invoice',
'journal_id': journal_id, 'journal_id': journal_id,
'date_invoice': date, 'date_invoice': date,
'state': 'draft', 'state': 'draft',
}) })
# invoice.write({'internal_number': invoice.number})
self.env['account.invoice.line'].create( self.env['account.invoice.line'].create(
{'product_id': self.env.ref('product.product_product_4').id, {'product_id': self.product.id,
'quantity': 1.0, 'quantity': 1.0,
'price_unit': 100.0, 'price_unit': 100.0,
'invoice_id': invoice.id, 'invoice_id': invoice.id,
'name': 'product that cost 100', 'name': 'product that cost 100',
'account_id': invoice_line_account, 'account_id': self.account_account_line.id,
'account_analytic_id': analytic_account.id, 'account_analytic_id': self.analytic_account.id,
}) })
return invoice return invoice
class TestAccountConstraintChronology(common.TransactionCase):
def setUp(self):
super(TestAccountConstraintChronology, self).setUp()
self.AccountJournal = self.env['account.journal']
self.Account = self.env['account.account']
def test_invoice_draft(self): def test_invoice_draft(self):
journal = get_journal_check(self, True) journal = self.get_journal_check(True)
today = datetime.now() today = datetime.now()
yesterday = today - timedelta(days=1) yesterday = today - timedelta(days=1)
date = yesterday.strftime(DEFAULT_SERVER_DATE_FORMAT) date = yesterday.strftime(DEFAULT_SERVER_DATE_FORMAT)
create_simple_invoice(self, journal.id, date) self.create_simple_invoice(journal.id, date)
date = today.strftime(DEFAULT_SERVER_DATE_FORMAT) date = today.strftime(DEFAULT_SERVER_DATE_FORMAT)
invoice_2 = create_simple_invoice(self, journal.id, date) invoice_2 = self.create_simple_invoice(journal.id, date)
self.assertTrue((invoice_2.state == 'draft'), self.assertTrue((invoice_2.state == 'draft'),
"Initial invoice state is not Draft") "Initial invoice state is not Draft")
with self.assertRaises(UserError): with self.assertRaises(UserError):
invoice_2.action_invoice_open() invoice_2.action_invoice_open()
def test_invoice_validate(self): def test_invoice_validate(self):
journal = get_journal_check(self, True) journal = self.get_journal_check(True)
today = datetime.now() today = datetime.now()
tomorrow = today + timedelta(days=1) tomorrow = today + timedelta(days=1)
date_tomorrow = tomorrow.strftime(DEFAULT_SERVER_DATE_FORMAT) date_tomorrow = tomorrow.strftime(DEFAULT_SERVER_DATE_FORMAT)
invoice_1 = create_simple_invoice(self, journal.id, date_tomorrow) invoice_1 = self.create_simple_invoice(journal.id, date_tomorrow)
self.assertTrue((invoice_1.state == 'draft'), self.assertTrue((invoice_1.state == 'draft'),
"Initial invoice state is not Draft") "Initial invoice state is not Draft")
invoice_1.action_invoice_open() invoice_1.action_invoice_open()
date = today.strftime(DEFAULT_SERVER_DATE_FORMAT) date = today.strftime(DEFAULT_SERVER_DATE_FORMAT)
invoice_2 = create_simple_invoice(self, journal.id, date) invoice_2 = self.create_simple_invoice(journal.id, date)
self.assertTrue((invoice_2.state == 'draft'), self.assertTrue((invoice_2.state == 'draft'),
"Initial invoice state is not Draft") "Initial invoice state is not Draft")
with self.assertRaises(UserError): with self.assertRaises(UserError):
invoice_2.action_invoice_open() invoice_2.action_invoice_open()
def test_invoice_without_date(self): def test_invoice_without_date(self):
journal = get_journal_check(self, True) journal = self.get_journal_check(True)
today = datetime.now() today = datetime.now()
yesterday = today - timedelta(days=1) yesterday = today - timedelta(days=1)
date = yesterday.strftime(DEFAULT_SERVER_DATE_FORMAT) date = yesterday.strftime(DEFAULT_SERVER_DATE_FORMAT)
create_simple_invoice(self, journal.id, date) self.create_simple_invoice(journal.id, date)
invoice_2 = create_simple_invoice(self, journal.id, False) invoice_2 = self.create_simple_invoice(journal.id, False)
self.assertTrue((invoice_2.state == 'draft'), self.assertTrue((invoice_2.state == 'draft'),
"Initial invoice state is not Draft") "Initial invoice state is not Draft")
with self.assertRaises(UserError): with self.assertRaises(UserError):

View File

@ -2,16 +2,14 @@
<!-- Copyright 2015-2017 ACSONE SA/NV <!-- Copyright 2015-2017 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo> <odoo>
<data>
<record id="view_account_journal_form" model="ir.ui.view"> <record id="view_account_journal_form" model="ir.ui.view">
<field name="name">account.journal.form (account_constraint_chronology)</field> <field name="name">account.journal.form (account_constraint_chronology)</field>
<field name="model">account.journal</field> <field name="model">account.journal</field>
<field name="inherit_id" ref="account.view_account_journal_form"/> <field name="inherit_id" ref="account.view_account_journal_form"/>
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="refund_sequence" position="after"> <field name="refund_sequence" position="after">
<field name="check_chronology" attrs="{'readonly': [('type', 'not in', ['sale', 'purchase', 'sale_refund', 'purchase_refund'])]}"/> <field name="check_chronology" attrs="{'readonly': [('type', 'not in', ['sale', 'purchase'])]}"/>
</field> </field>
</field> </field>
</record> </record>
</data>
</odoo> </odoo>