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

10.0 account invoice merge operating unit #88

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 54 additions & 0 deletions account_invoice_merge_operating_unit/README.rst
@@ -0,0 +1,54 @@
.. image:: https://img.shields.io/badge/license-LGPLv3-blue.svg
:target: https://www.gnu.org/licenses/lgpl.html
:alt: License: LGPL-3

=====================================
Account Invoice Merge Operating Units
=====================================

This module adds Operating Unit to the new invoice ensuring that the invoices that are merged belongs to the same Operating Unit.


Usage
=====

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/213/10.0

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/operating-unit/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smashing it by providing a detailed and welcomed feedback.

Credits
=======

Images
------

* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.

Contributors
------------

* Eficent Business and IT Consulting Services S.L. <contact@eficent.com>
* Serpent Consulting Services Pvt. Ltd. <support@serpentcs.com>

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit https://odoo-community.org.
7 changes: 7 additions & 0 deletions account_invoice_merge_operating_unit/__init__.py
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from . import models
from . import wizard
23 changes: 23 additions & 0 deletions account_invoice_merge_operating_unit/__manifest__.py
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

{
'name': "Account Invoice Merge Operating Unit",
'author': "Eficent Business and IT Consulting Services S.L., "
"Serpent Consulting Services Pvt. Ltd.,"
"Odoo Community Association (OCA)",
'website': "http://www.eficent.com",
'category': 'Finance',
'version': '10.0.1.0.0',
'license': 'LGPL-3',
'depends': [
'account_invoice_merge',
'account_operating_unit',
],
'data': [
'wizard/invoice_merge_view.xml',
],
'auto_install': True,
}
6 changes: 6 additions & 0 deletions account_invoice_merge_operating_unit/models/__init__.py
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from . import account_invoice
22 changes: 22 additions & 0 deletions account_invoice_merge_operating_unit/models/account_invoice.py
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import api, models


class AccountInvoice(models.Model):
_inherit = 'account.invoice'

@api.model
def _get_invoice_key_cols(self):
res = super(AccountInvoice, self)._get_invoice_key_cols()
res.append('operating_unit_id')
return res

@api.model
def _get_first_invoice_fields(self, invoice):
res = super(AccountInvoice, self)._get_first_invoice_fields(invoice)
res.update({'operating_unit_id': invoice.operating_unit_id.id})
return res
6 changes: 6 additions & 0 deletions account_invoice_merge_operating_unit/tests/__init__.py
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from . import test_account_invoice_merge_operating_unit
@@ -0,0 +1,125 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo.tests import common
from odoo.exceptions import UserError


class TestInvoiceMergeOperatingUnit(common.TransactionCase):

def setUp(self):
super(TestInvoiceMergeOperatingUnit, self).setUp()
self.res_users_model = self.env['res.users']
self.invoice_model = self.env['account.invoice']
self.account_model = self.env['account.account']
self.inv_merge_model = self.env['invoice.merge']

# company
self.company = self.env.ref('base.main_company')
self.grp_acc_manager = self.env.ref('account.group_account_manager')
# Main Operating Unit
self.ou1 = self.env.ref('operating_unit.main_operating_unit')
# B2B Operating Unit
self.b2b = self.env.ref('operating_unit.b2b_operating_unit')
# B2C Operating Unit
self.b2c = self.env.ref('operating_unit.b2c_operating_unit')
# Partner
self.partner1 = self.env.ref('base.res_partner_1')
# Products
self.product1 = self.env.ref('product.product_product_7')

# Create user1
self.user_id = self.res_users_model.with_context({
'no_reset_password': True
}).create({'name': 'Test Account User',
'login': 'user_1',
'password': 'demo',
'email': 'example@yourcompany.com',
'company_id': self.company.id,
'company_ids': [(4, self.company.id)],
'operating_unit_ids': [(4, self.b2b.id), (4, self.b2c.id)],
'groups_id': [(6, 0, [self.grp_acc_manager.id])]
})

def _prepare_invoice(self, operating_unit_id, qty):
line_products = [(self.product1, qty)]
# Prepare invoice lines
lines = []
acc_type = self.env.ref('account.data_account_type_revenue')
account_id = self.account_model.search([('user_type_id', '=',
acc_type.id)], limit=1)
for product, qty in line_products:
line_values = {
'name': product.name,
'product_id': product.id,
'quantity': qty,
'price_unit': 50,
'account_id': account_id.id
}
lines.append((0, 0, line_values))
inv_vals = {
'partner_id': self.partner1.id,
'account_id': self.partner1.property_account_receivable_id.id,
'operating_unit_id': operating_unit_id,
'name': 'Test Supplier Invoice',
'reference_type': 'none',
'type': 'out_invoice',
'invoice_line_ids': lines,
}
return inv_vals

def test_invoice_merge_with_opearting_unit(self):
"""Create the invoice and assert the value of
Operating Unit in the new Invoice.
"""
# Create invoice
self.invoice = self.invoice_model.sudo(self.user_id.id).\
create(self._prepare_invoice(self.b2b.id, 10))

# Create second invoice with different quantity
self.invoice2 = self.invoice_model.sudo(self.user_id.id).\
create(self._prepare_invoice(self.b2b.id, 20))

wiz_invoice_merge = self.inv_merge_model.with_context({
'active_ids': [self.invoice.id, self.invoice2.id],
'active_model': 'account.invoice'
})

action = wiz_invoice_merge.create({
'keep_references': True
}).merge_invoices()
invoices = self.invoice_model.browse(action['domain'][0][2])
self.assertEqual(invoices[1].operating_unit_id,
invoices[2].operating_unit_id,
'Invoice should have Operating Unit')

def test_invoice_dirty_check_on_opearting_unit(self):
"""Creates the invoice and asserts that Operating Unit
of the invoice getting merged are same otherwise
raise an exception.
"""
# Create invoice
self.invoice = self.invoice_model.sudo(self.user_id.id).\
create(self._prepare_invoice(self.b2b.id, 10))
# Create second invoice with different quantity
self.invoice2 = self.invoice_model.sudo(self.user_id.id).\
create(self._prepare_invoice(self.b2c.id, 20))

# dirty check to assert similar Operating Unit in invoices

wiz_invoice_merge = self.inv_merge_model.with_context({
'active_ids': [self.invoice.id, self.invoice2.id],
'active_model': 'account.invoice'
})

with self.assertRaises(UserError):

wiz_invoice_merge.create({'keep_references': True})._dirty_check()

wiz_invoice_merge_dif = self.inv_merge_model.with_context({
'active_ids': [self.invoice.id, self.invoice2.id],
'active_model': 'invoice.merge'
})
wiz_invoice_merge_dif.create({'keep_references': True})._dirty_check()
6 changes: 6 additions & 0 deletions account_invoice_merge_operating_unit/wizard/__init__.py
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from . import invoice_merge
23 changes: 23 additions & 0 deletions account_invoice_merge_operating_unit/wizard/invoice_merge.py
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import api, models, _
from odoo.exceptions import UserError


class InvoiceMerge(models.TransientModel):
_inherit = "invoice.merge"

@api.model
def _dirty_check(self):
res = super(InvoiceMerge, self)._dirty_check()
if self.env.context.get('active_model', '') == 'account.invoice':
ids = self.env.context['active_ids']
invs = self.env['account.invoice'].browse(ids)
for d in invs:
if d['operating_unit_id'] != invs[0]['operating_unit_id']:
raise UserError(_('Not all invoices are at the same '
'Operating Unit!'))
return res
37 changes: 37 additions & 0 deletions account_invoice_merge_operating_unit/wizard/invoice_merge_view.xml
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2017 Eficent Business and IT Consulting Services S.L.
Copyright 2017 Serpent Consulting Services Pvt. Ltd.
License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -->
<odoo>

<record id="view_invoice_merge_ou" model="ir.ui.view">
<field name="name">Merger Partner Invoice OU</field>
<field name="model">invoice.merge</field>
<field name="inherit_id" ref="account_invoice_merge.view_invoice_merge"/>
<field name="priority" eval="16"/>
<field name="arch" type="xml">
<xpath expr="//form/group/p[1]" position="attributes">
<attribute name="invisible">True</attribute>
</xpath>
<xpath expr="//form/group/separator" position="after">
<p>
Please note that:
<br/><br/>
Invoices will only be merged if:
<br/>
* Invoices are in draft
<br/>
* Invoices belong to the same partner
<br/>
* Invoices are have same company, operating unit, partner, address, currency, journal, salesman, account, type
<br/><br/>
Lines will only be merged if:
<br/>
* Invoice lines are exactly the same except for the product,quantity and unit
<br/>
</p>
</xpath>
</field>
</record>

</odoo>