Skip to content

Commit

Permalink
Merge ad0f47f into 74a29bc
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanRijnhart committed Jun 7, 2018
2 parents 74a29bc + ad0f47f commit 4b042cc
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 22 deletions.
1 change: 1 addition & 0 deletions purchase_sale_inter_company/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Configuration
* Go to the tab *Inter-Company* then the group *Purchase To Sale*.
* Select the *Warehouse For Sale Orders*, it is the warehouse that will be used to automatically generate the sale order in the other company.
* If you check the option *Sale Auto Validation* in the configuration of company B, then when you validate a *Purchase Order* in company A with company B as supplier, then the *Sale Order* will be automatically validated in company B with company A as customer.
* If you check the option *Update Intercompany Purchase Price* in the configuration of company B, then when you validate a *Purchase Order* in company A with company B as supplier, the prices on the *Purchase Order* will automatically be updated with the prices of the sales order in company B. Price updates will be logged when the sale order is created. If this option is not checked, then the sale order cannot be validated if there are price differences. The option is checked by default.

Usage
=====
Expand Down
2 changes: 1 addition & 1 deletion purchase_sale_inter_company/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
'name': 'Inter Company Module for Purchase to Sale Order',
'summary': 'Intercompany PO/SO rules',
'version': '8.0.1.0.0',
'version': '8.0.1.1.0',
'category': 'Purchase Management',
'website': 'http://www.odoo.com',
'author': 'Odoo SA, Akretion, Odoo Community Association (OCA)',
Expand Down
8 changes: 8 additions & 0 deletions purchase_sale_inter_company/models/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ def _inter_company_create_sale_order(self, dest_company_id):
# Validation of sale order
if dest_company.sale_auto_validation:
sale_order.signal_workflow('order_confirm')
else:
diffs = sale_order.multi_company_check_prices()
if diffs:
prices = '\n'.join(
_('%s: %s vs %s') % diff for diff in diffs)
self.message_post(_(
'Sale order %s was created with price differences: %s)' % (
sale_order.name, prices)))

@api.multi
def _prepare_sale_order_data(self, name, partner, dest_company,
Expand Down
7 changes: 7 additions & 0 deletions purchase_sale_inter_company/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ class ResCompany(models.Model):
'stock.warehouse', string='Warehouse For Sale Orders',
help="Default value to set on Sale Orders that "
"will be created based on Purchase Orders made to this company")
update_intercompany_purchase_price = fields.Boolean(
help=("If an intercompany sale order item has a different price than "
"the originating purchase item, update the purchase item "
"automatically. If disabled, just log the price differences on "
"the purchase order (and don't allow to confirm the sales "
"order)."),
default=True)
47 changes: 41 additions & 6 deletions purchase_sale_inter_company/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import fields, models, api
from openerp.exceptions import Warning as UserError
from openerp.tools.translate import _


class SaleOrder(models.Model):
Expand All @@ -14,14 +16,47 @@ class SaleOrder(models.Model):
string='Source Purchase Order',
readonly=True, copy=False)

@api.multi
def multi_company_check_prices(self, update=False):
""" Compare prices of multi company orders with the originating
purchase orders. """
self.ensure_one()
cur = self.pricelist_id.currency_id
diffs = []
for line in self.order_line.filtered('auto_purchase_line_id'):
po_line = line.auto_purchase_line_id.sudo()
if not cur.is_zero(po_line.price_unit - line.price_unit):
diffs.append(
(po_line.name, po_line.price_unit, line.price_unit))
if update:
po_line.write({'price_unit': line.price_unit})
return diffs

@api.multi
def signal_workflow(self, signal):
for order in self:
if signal == 'order_confirm' and order.auto_purchase_order_id:
for line in order.order_line:
if line.auto_purchase_line_id:
line.auto_purchase_line_id.sudo().write({
'price_unit': line.price_unit})
""" Check price differences with the origin purchase order and either
report on the po that they were updated or raise an error (depending
on the sale's company settings) """
if signal == 'order_confirm':
for order in self.filtered('auto_purchase_order_id'):
po = order.auto_purchase_order_id.sudo()
update = order.company_id.update_intercompany_purchase_price
diffs = order.multi_company_check_prices(update=update)
if not diffs:
continue
if not update:
prices = '\n'.join(
_('%s: %s (PO) vs. %s (SO)') % diff for diff in diffs)
raise UserError(_(
'Could not confim sale order %s because there '
'were price differences with purchase order %s: %s'
) % (order.name, po.name, prices))
prices = '\n'.join(
_('%s: %s -> %s') % diff for diff in diffs)
po.message_post(_(
'Sale order %s was confirmed. The following prices '
'were updated on this purchase order: %s') % (
order.name, prices))
return super(SaleOrder, self).signal_workflow(signal=signal)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,78 @@
# © 2013-Today Odoo SA
# © 2016 Chafique DELLI @ Akretion
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp.exceptions import Warning as UserError
from openerp.tests.common import TransactionCase


class TestPurchaseSaleInterCompany(TransactionCase):
def setUp(self):
super(TestPurchaseSaleInterCompany, self).setUp()
self.purchase_company_a = self.env.ref(
self.purchase = self.env.ref(
'purchase_sale_inter_company.purchase_company_a')
self.sales_company = self.env.ref(
'account_invoice_inter_company.company_b')

def test_purchase_sale_inter_company(self):
def test_01_purchase_sale_inter_company_validated(self):
purchase_price = self.purchase.order_line.price_unit
self.sales_company.write({
'sale_auto_validation': True,
'update_intercompany_purchase_price': True,
})
# Confirm the purchase of company A
self.purchase_company_a.sudo(self.env.ref(
self.purchase.sudo(self.env.ref(
'account_invoice_inter_company.user_company_a')).signal_workflow(
'purchase_confirm')

# Check sale order created in company B
sales = self.env['sale.order'].sudo(self.env.ref(
'account_invoice_inter_company.user_company_b')).search([
('auto_purchase_order_id', '=', self.purchase_company_a.id)
('auto_purchase_order_id', '=', self.purchase.id)
])
self.assertNotEquals(sales, False)
self.assertTrue(sales)
self.assertEquals(len(sales), 1)
if sales.company_id.sale_auto_validation:
self.assertEquals(sales.state, 'manual')
else:
self.assertEquals(sales.state, 'draft')
self.assertEquals(sales.state, 'manual')
self.assertEquals(sales.partner_id,
self.purchase_company_a.company_id.partner_id)
self.purchase.company_id.partner_id)
self.assertEquals(sales.company_id.partner_id,
self.purchase_company_a.partner_id)
self.purchase.partner_id)
self.assertEquals(len(sales.order_line),
len(self.purchase_company_a.order_line))
len(self.purchase.order_line))
self.assertEquals(sales.order_line.product_id,
self.purchase_company_a.order_line.product_id)
self.purchase.order_line.product_id)
# Price has been updated
self.assertNotEqual(
purchase_price, self.purchase.order_line.price_unit)
self.assertEqual(
sales.order_line.price_unit, self.purchase.order_line.price_unit)
# Price update is logged on the purchase order
term = "%s -> %s" % (
purchase_price, sales.order_line.price_unit)
self.assertIn(term, ''.join(self.purchase.message_ids.mapped('body')))

def test_02_purchase_sale_inter_company_draft(self):
self.sales_company.write({
'sale_auto_validation': False,
'update_intercompany_purchase_price': True,
})
# Confirm the purchase of company A
self.purchase.sudo(self.env.ref(
'account_invoice_inter_company.user_company_a')).signal_workflow(
'purchase_confirm')
# Check sale order created in company B
sales = self.env['sale.order'].sudo(self.env.ref(
'account_invoice_inter_company.user_company_b')).search([
('auto_purchase_order_id', '=', self.purchase.id)
])
self.assertTrue(sales)
self.assertEquals(sales.state, 'draft')

def test_03_purchase_sale_inter_company_no_price_update(self):
self.sales_company.write({
'sale_auto_validation': True,
'update_intercompany_purchase_price': False,
})
# Confirm the purchase of company A
with self.assertRaisesRegexp(UserError, 'price difference'):
self.purchase.sudo(self.env.ref(
'account_invoice_inter_company.user_company_a')
).signal_workflow('purchase_confirm')
1 change: 1 addition & 0 deletions purchase_sale_inter_company/views/res_company_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<group string="Purchase to Sale">
<field name="warehouse_id" attrs="{'required': True}" domain="[('company_id', '=', active_id)]"/>
<field name="sale_auto_validation"/>
<field name="update_intercompany_purchase_price"/>
</group>
</xpath>
</field>
Expand Down

0 comments on commit 4b042cc

Please sign in to comment.