Skip to content

Commit

Permalink
Merge pull request #93 from acsone/8.0-fix-invoice-merge-purchase-ape
Browse files Browse the repository at this point in the history
[8.0][FIX] Merge invoices from purchase order
  • Loading branch information
pedrobaeza committed Sep 17, 2015
2 parents 6676bf7 + 4387ad8 commit 53a08fb
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 6 deletions.
6 changes: 0 additions & 6 deletions account_invoice_merge/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,6 @@ def make_key(br, fields):
if 'sale.order' in self.env.registry else False
invoice_line_obj = self.env['account.invoice.line']
# None if purchase is not installed
po_obj = self.env['purchase.order']\
if 'purchase.order' in self.env.registry else False
for new_invoice_id in invoices_info:
if so_obj:
todos = so_obj.search(
Expand All @@ -211,10 +209,6 @@ def make_key(br, fields):
if invoice_line_ids:
so_line.write(
{'invoice_lines': [(6, 0, invoice_line_ids)]})
if po_obj:
todos = po_obj.search(
[('invoice_ids', 'in', invoices_info[new_invoice_id])])
todos.write({'invoice_ids': [(4, new_invoice_id)]})
# recreate link (if any) between original analytic account line
# (invoice time sheet for example) and this new invoice
anal_line_obj = self.env['account.analytic.line']
Expand Down
44 changes: 44 additions & 0 deletions account_invoice_merge_purchase/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:alt: License: AGPL-3

Merge invoices from purchase order
==================================

This module provides a compatibility between purchase order workflow and account invoice merge module


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/95/8.0

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/account-invoicing/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
`here <https://github.com/OCA/account-invoicing/issues/new?body=module:%20account_invoice_merge_purchase%0Aversion:%208.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Credits
=======

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

* Adrien Peiffer <adrien.peiffer@acsone.eu>

Maintainer
----------

.. image:: http://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: http://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 http://odoo-community.org.
2 changes: 2 additions & 0 deletions account_invoice_merge_purchase/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import models
39 changes: 39 additions & 0 deletions account_invoice_merge_purchase/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This file is part of account_invoice_merge_purchase,
# an Odoo module.
#
# Copyright (c) 2015 ACSONE SA/NV (<http://acsone.eu>)
#
# account_invoice_merge_purchase is free software:
# you can redistribute it and/or modify it under the terms of the GNU
# Affero General Public License as published by the Free Software
# Foundation,either version 3 of the License, or (at your option) any
# later version.
#
# account_invoice_merge_purchase is distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with account_invoice_merge_purchase.
# If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
{
'name': "Account Invoice Merge Purchase",

'summary': """Compatibility between purchase and account invoice merge""",
'author': "ACSONE SA/NV,Odoo Community Association (OCA)",
'website': "http://acsone.eu",
'category': 'Finance',
'version': '8.0.1.0.0',
'license': 'AGPL-3',
'depends': [
'account_invoice_merge',
'purchase',
],
'auto_install': True,
}
2 changes: 2 additions & 0 deletions account_invoice_merge_purchase/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import account_invoice
57 changes: 57 additions & 0 deletions account_invoice_merge_purchase/models/account_invoice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This file is part of account_invoice_merge_purchase,
# an Odoo module.
#
# Copyright (c) 2015 ACSONE SA/NV (<http://acsone.eu>)
#
# account_invoice_merge_purchase is free software:
# you can redistribute it and/or modify it under the terms of the GNU
# Affero General Public License as published by the Free Software
# Foundation,either version 3 of the License, or (at your option) any
# later version.
#
# account_invoice_merge_purchase is distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with account_invoice_merge_purchase.
# If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import models, api


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

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

@api.multi
def do_merge(self, keep_references=True, date_invoice=False):
invoices_info = super(AccountInvoice, self).do_merge(
keep_references=keep_references, date_invoice=date_invoice)
po_obj = self.env['purchase.order']
invoice_line_obj = self.env['account.invoice.line']
for new_invoice_id in invoices_info:
todos = po_obj.search(
[('invoice_ids', 'in', invoices_info[new_invoice_id])])
todos.write({'invoice_ids': [(4, new_invoice_id)]})
for org_po in todos:
for po_line in org_po.order_line:
invoice_line_ids = invoice_line_obj.search(
[('invoice_id.state', '!=', 'cancel'),
('purchase_line_id', '=', po_line.id)])
if invoice_line_ids.ids:
po_line.write(
{'invoice_lines': [(6, 0,
invoice_line_ids.ids)]})
return invoices_info
2 changes: 2 additions & 0 deletions account_invoice_merge_purchase/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import test_account_invoice_merge_purchase
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This file is part of account_invoice_merge_purchase,
# an Odoo module.
#
# Copyright (c) 2015 ACSONE SA/NV (<http://acsone.eu>)
#
# account_invoice_merge_purchase is free software:
# you can redistribute it and/or modify it under the terms of the GNU
# Affero General Public License as published by the Free Software
# Foundation,either version 3 of the License, or (at your option) any
# later version.
#
# account_invoice_merge_purchase is distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with account_invoice_merge_purchase.
# If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.tests import common
from openerp import workflow
from datetime import datetime


def create_simple_po(self, partner, invoice_method):
vals = {'partner_id': partner.id,
'invoice_method': invoice_method,
'location_id': self.ref('stock.stock_location_stock'),
'pricelist_id': self.ref('purchase.list0'),
'order_line': [(0, 0, {'name': 'test',
'date_planned': datetime.today(),
'price_unit': 10,
})]
}
return self.po_obj.create(vals)


def pay_invoice(self, invoice):
aml = self.aml_obj.search(
[('account_id.type', 'in', ['payable', 'receivable']),
('invoice.id', '=', invoice.id)])
ctx = self.context.copy()
ctx.update({'active_ids': [aml.id]})
writeoff = self.writeoff_obj.with_context(active_ids=[aml.id]).create(
{'journal_id': self.journal01.id,
'writeoff_acc_id': self.account01.id})
writeoff.trans_rec_reconcile()


def invoice_picking(self, picking):
wizard = self.invoice_picking_wiz.with_context(active_ids=[picking.id])\
.create({})
return wizard.with_context(active_ids=[picking.id]).create_invoice()


class TestAccountInvoiceMergePurchase(common.TransactionCase):

def setUp(self):
super(TestAccountInvoiceMergePurchase, self).setUp()
self.partner01 = self.env.ref('base.res_partner_1')
self.context = self.env['res.users'].context_get()
self.po_obj = self.env['purchase.order']
self.inv_obj = self.env['account.invoice']
self.aml_obj = self.env['account.move.line']
self.journal01 = self.env.ref('account.miscellaneous_journal')
self.account01 = self.env.ref('account.a_pay')
self.writeoff_obj = self.env['account.move.line.reconcile.writeoff']
self.picking_obj = self.env['stock.picking']
self.invoice_picking_wiz = self.env['stock.invoice.onshipping']

def test_order_multi_purchase_order(self):
purchase_order01 = create_simple_po(self, self.partner01, 'order')
# I confirm the purchase order
workflow.trg_validate(self.uid, 'purchase.order',
purchase_order01.id, 'purchase_confirm',
self.cr)
# I check if the purchase order is confirmed
purchase_order01.invalidate_cache()
self.assertEqual(purchase_order01.state, 'approved',
"Purchase order's state isn't correct")
invoice_ids = purchase_order01.invoice_ids.ids
purchase_order02 = purchase_order01.copy()
# I confirm the second purchase order
workflow.trg_validate(self.uid, 'purchase.order',
purchase_order02.id, 'purchase_confirm',
self.cr)
# I check if the purchase order is confirmed
purchase_order02.invalidate_cache()
self.assertEqual(purchase_order02.state, 'approved',
"Purchase order's state isn't correct")
invoice_ids.extend(purchase_order02.invoice_ids.ids)
invoices = self.inv_obj.browse(invoice_ids)
invoices_info = invoices.do_merge()
new_invoice_ids = invoices_info.keys()
# Ensure there is only one new invoice
self.assertEqual(len(new_invoice_ids), 1)
# I post the created invoice
workflow.trg_validate(self.uid, 'account.invoice', new_invoice_ids[0],
'invoice_open', self.cr)
# I pay the merged invoice
invoice = self.inv_obj.browse(new_invoice_ids)[0]
pay_invoice(self, invoice)
# I check if merge invoice is paid
self.assertEqual(invoice.state, 'paid')
purchase_order01.invalidate_cache()
# I check if purchase order are done
self.assertEqual(purchase_order01.state, 'done')
self.assertEqual(purchase_order02.state, 'done')

def test_picking_multi_purchase_order(self):
purchase_order01 = self.env.ref('purchase.purchase_order_1')
# I set the invoice method to picking
purchase_order01.invoice_method = 'picking'
# I confirm the purchase order
workflow.trg_validate(self.uid, 'purchase.order',
purchase_order01.id, 'purchase_confirm',
self.cr)
# I check if the purchase order is confirmed
purchase_order01.invalidate_cache()
self.assertEqual(purchase_order01.state, 'approved',
"Purchase order's state isn't correct")
# I check if there is a picking
self.assertEqual(len(purchase_order01.picking_ids.ids), 1)
picking01 = purchase_order01.picking_ids[0]
# I transfer the picking
picking01.do_transfer()
invoice_ids = invoice_picking(self, picking01)

purchase_order02 = purchase_order01.copy()
# I set the invoice method to picking
purchase_order02.invoice_method = 'picking'
# I confirm the second purchase order
workflow.trg_validate(self.uid, 'purchase.order',
purchase_order02.id, 'purchase_confirm',
self.cr)
# I check if the purchase order is confirmed
purchase_order02.invalidate_cache()
self.assertEqual(purchase_order02.state, 'approved',
"Purchase order's state isn't correct")
# I check if there is a picking
self.assertEqual(len(purchase_order02.picking_ids.ids), 1)
picking02 = purchase_order02.picking_ids[0]
# I transfer the picking
picking02.do_transfer()
invoice_ids.extend(invoice_picking(self, picking02))
invoices = self.inv_obj.browse(invoice_ids)
invoices_info = invoices.do_merge()
new_invoice_ids = invoices_info.keys()
# Ensure there is only one new invoice
self.assertEqual(len(new_invoice_ids), 1)
# I post the created invoice
workflow.trg_validate(self.uid, 'account.invoice', new_invoice_ids[0],
'invoice_open', self.cr)
# I pay the merged invoice
invoice = self.inv_obj.browse(new_invoice_ids)[0]
pay_invoice(self, invoice)
# I check if merge invoice is paid
self.assertEqual(invoice.state, 'paid')
purchase_order01.invalidate_cache()
# I check if purchase order are done
self.assertEqual(purchase_order01.state, 'done')
self.assertEqual(purchase_order02.state, 'done')

0 comments on commit 53a08fb

Please sign in to comment.