Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14.0 mig account invoice change #16

Open
wants to merge 4 commits into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions account_invoice_change/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import wizard
31 changes: 31 additions & 0 deletions account_invoice_change/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
'name': 'Account Invoice Change',
'author': 'Hibou Corp. <[email protected]>',
'version': '14.0.1.0.0',
'category': 'Accounting',
'sequence': 95,
'summary': 'Technical foundation for changing invoices.',
'description': """
Technical foundation for changing invoices.

Creates wizard and permissions for making invoice changes that can be
handled by other individual modules.

This module implements, as examples, how to change the Salesperson and Date fields.

Abstractly, individual 'changes' should come from specific 'fields' or capability
modules that handle the consequences of changing that field in whatever state the
the invoice is currently in.

""",
'website': 'https://hibou.io/',
'depends': [
'account',
],
'data': [
'wizard/invoice_change_views.xml',
'security/ir.model.access.csv',
],
'installable': True,
'application': False,
}
3 changes: 3 additions & 0 deletions account_invoice_change/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_invoice_change,access_invoice_change,model_account_invoice_change,base.group_user,1,0,0,0
manage_invoice_change,manage_invoice_change,model_account_invoice_change,base.group_system,1,1,1,1
1 change: 1 addition & 0 deletions account_invoice_change/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_invoice_change
51 changes: 51 additions & 0 deletions account_invoice_change/tests/test_invoice_change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from odoo.addons.account.tests.account_test_users import AccountTestUsers
from odoo import fields


class TestInvoiceChange(AccountTestUsers):

def test_invoice_change_basic(self):
self.account_invoice_obj = self.env['account.move']
self.payment_term = self.env.ref('account.account_payment_term_advance')
self.journalrec = self.env['account.journal'].search([('type', '=', 'sale')])[0]
self.partner3 = self.env.ref('base.res_partner_3')
self.invoice_basic = self.account_invoice_obj.with_user(self.account_user).create({
'type': 'out_invoice',
'name': "Test Customer Invoice",
'invoice_payment_term_id': self.payment_term.id,
'journal_id': self.journalrec.id,
'partner_id': self.partner3.id,
# account_id=self.account_rec1_id.id,
'line_ids': [(0, 0, {
'product_id': self.env.ref('product.product_product_5').id,
'quantity': 10.0,
'account_id': self.env['account.account'].search(
[('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id)], limit=1).id,
'name': 'product test 5',
'price_unit': 100.00,
})],
})
self.assertEqual(self.invoice_basic.state, 'draft')
self.invoice_basic.action_post()
self.assertEqual(self.invoice_basic.state, 'posted')
self.assertEqual(self.invoice_basic.date, fields.Date.today())
self.assertEqual(self.invoice_basic.invoice_date, fields.Date.today())
self.assertEqual(self.invoice_basic.invoice_user_id, self.account_user)
self.assertEqual(self.invoice_basic.line_ids[0].date, fields.Date.today())

ctx = {'active_model': 'account.move', 'active_ids': [self.invoice_basic.id]}
change = self.env['account.invoice.change'].with_context(ctx).create({})
self.assertEqual(change.date, self.invoice_basic.date)
self.assertEqual(change.invoice_date, self.invoice_basic.invoice_date)
self.assertEqual(change.invoice_user_id, self.invoice_basic.invoice_user_id)

change_date = '2018-01-01'
change_invoice_date = '2019-01-01'
change_user = self.env.user
change.write({'invoice_user_id': change_user.id, 'date': change_date, 'invoice_date': change_invoice_date})

change.affect_change()
self.assertEqual(str(self.invoice_basic.date), change_date)
self.assertEqual(str(self.invoice_basic.invoice_date), change_invoice_date)
self.assertEqual(self.invoice_basic.invoice_user_id, change_user)
self.assertEqual(str(self.invoice_basic.line_ids[0].date), change_date)
1 change: 1 addition & 0 deletions account_invoice_change/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import invoice_change
57 changes: 57 additions & 0 deletions account_invoice_change/wizard/invoice_change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from odoo import api, fields, models, _
from odoo.exceptions import UserError


class InvoiceChangeWizard(models.TransientModel):
_name = 'account.invoice.change'
_description = 'Invoice Change'

move_id = fields.Many2one('account.move', string='Invoice', readonly=True, required=True)
move_company_id = fields.Many2one('res.company', readonly=True, related='move_id.company_id')
invoice_user_id = fields.Many2one('res.users', string='Salesperson')
date = fields.Date(string='Accounting Date')
invoice_date = fields.Date(string='Invoice Date')

@api.model
def default_get(self, fields):
rec = super(InvoiceChangeWizard, self).default_get(fields)
context = dict(self._context or {})
active_model = context.get('active_model')
active_ids = context.get('active_ids')

# Checks on context parameters
if not active_model or not active_ids:
raise UserError(
_("Programmation error: wizard action executed without active_model or active_ids in context."))
if active_model != 'account.move':
raise UserError(_(
"Programmation error: the expected model for this action is 'account.move'. The provided one is '%d'.") % active_model)

# Checks on received invoice records
invoice = self.env[active_model].browse(active_ids)
if len(invoice) != 1:
raise UserError(_("Invoice Change expects only one invoice."))
rec.update({
'move_id': invoice.id,
'invoice_user_id': invoice.invoice_user_id.id,
'date': invoice.date,
'invoice_date': invoice.invoice_date,
})
return rec

def _new_move_vals(self):
vals = {}
if self.move_id.invoice_user_id != self.invoice_user_id:
vals['invoice_user_id'] = self.invoice_user_id.id
if self.move_id.date != self.date:
vals['date'] = self.date
if self.move_id.invoice_date != self.invoice_date:
vals['invoice_date'] = self.invoice_date
return vals

def affect_change(self):
self.ensure_one()
vals = self._new_move_vals()
if vals:
self.move_id.write(vals)
return True
48 changes: 48 additions & 0 deletions account_invoice_change/wizard/invoice_change_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="account_invoice_change_form" model="ir.ui.view">
<field name="name">Invoice Change</field>
<field name="model">account.invoice.change</field>
<field name="arch" type="xml">
<form string="Invoice Change">
<group>
<group name="group_left">
<field name="move_id" invisible="1"/>
<field name="move_company_id" invisible="1"/>
<field name="invoice_user_id"/>
<field name="invoice_date"/>
<field name="date"/>
</group>
<group name="group_right"/>
</group>
<footer>
<button name="affect_change" string="Change" type="object" class="btn-primary"/>
<button string="Cancel" class="btn-default" special="cancel"/>
</footer>
</form>
</field>
</record>

<record id="action_view_account_invoice_change" model="ir.actions.act_window">
<field name="name">Invoice Change Wizard</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">account.invoice.change</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>

<record id="view_move_form_inherit" model="ir.ui.view">
<field name="name">account.move.form.inherit.invoice.change</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='state']" position="before">
<button name="%(action_view_account_invoice_change)d" string="Change"
type="action" class="btn-secondary"
attrs="{'invisible': ['|', ('state', 'in', ('sale', 'done', 'cancel')), ('type', 'not in', ('out_invoice', 'out_refund'))]}"
context="{'default_move_id': id}"
groups="account.group_account_manager" />
</xpath>
</field>
</record>
</odoo>