Skip to content

Commit

Permalink
Merge pull request #2 from eLBati/8.0-inter_company_rules_lb
Browse files Browse the repository at this point in the history
8.0 inter company rules lb
  • Loading branch information
bealdav committed Oct 3, 2016
2 parents 3ca4463 + 4485c9c commit 58a02de
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 40 deletions.
1 change: 1 addition & 0 deletions inter_company_rules/README.rst
Expand Up @@ -50,6 +50,7 @@ Contributors
* Odoo S.A.
* Chafique Delli <chafique.delli@akretion.com>
* Alexis de Lattre <alexis.delattre@akretion.com>
* Lorenzo Battistini <lorenzo.battistini@agilebg.com>

Maintainer
----------
Expand Down
12 changes: 3 additions & 9 deletions inter_company_rules/__openerp__.py
Expand Up @@ -20,17 +20,11 @@
##############################################################################
{
'name': 'Inter Company Module for Sale/Purchase Orders and Invoices',
'version': '1.1',
'version': '8.0.1.1.0',
'summary': 'Intercompany SO/PO/INV rules',
'description': ''' Module for synchronization of Documents
between several companies. For example,
this allow you to have a Sale Order created automatically
when a Purchase Order is validated with another company of the system
as supplier, and inversely.
Supported documents are SO, PO and invoices/refunds.
''',
'author': 'Odoo SA',
'author': 'Odoo SA, Odoo Community Association (OCA)',
'website': 'http://www.odoo.com',
'license': 'AGPL-3',
'depends': [
'sale',
'purchase',
Expand Down
12 changes: 7 additions & 5 deletions inter_company_rules/models/account_invoice.py
Expand Up @@ -3,7 +3,7 @@
from openerp.exceptions import Warning as UserError


class account_invoice(models.Model):
class AccountInvoice(models.Model):

_inherit = 'account.invoice'

Expand Down Expand Up @@ -40,7 +40,7 @@ def invoice_validate(self):
invoice.inter_company_create_invoice(company,
'out_refund',
'sale_refund')
return super(account_invoice, self).invoice_validate()
return super(AccountInvoice, self).invoice_validate()

@api.one
def inter_company_create_invoice(self, company, inv_type, journal_type):
Expand Down Expand Up @@ -219,10 +219,12 @@ def action_cancel(self):
for invoice in self:
company = self.env['res.company']._find_company_from_partner(
invoice.partner_id.id)
if (company and company.intercompany_user_id
and not invoice.auto_generated):
if (
company and company.intercompany_user_id and not
invoice.auto_generated
):
intercompany_uid = company.intercompany_user_id.id
for inter_invoice in self.sudo(intercompany_uid).search(
[('auto_invoice_id', '=', invoice.id)]):
inter_invoice.signal_workflow('invoice_cancel')
return super(account_invoice, self).action_cancel()
return super(AccountInvoice, self).action_cancel()
38 changes: 24 additions & 14 deletions inter_company_rules/models/purchase_order.py
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
from openerp import api, fields, models, _
from openerp.exceptions import Warning
from openerp.exceptions import Warning as UserError


class purchase_order(models.Model):
class PurchaseOrder(models.Model):

_inherit = "purchase.order"

Expand All @@ -16,7 +16,7 @@ class purchase_order(models.Model):
@api.multi
def wkf_confirm_order(self):
""" Generate inter company sale order base on conditions."""
res = super(purchase_order, self).wkf_confirm_order()
res = super(PurchaseOrder, self).wkf_confirm_order()
for order in self:
# get the company from partner then trigger action of
# intercompany relation
Expand All @@ -38,26 +38,30 @@ def inter_company_create_sale_order(self, company):
:rtype company : res.company record
"""
SaleOrder = self.env['sale.order']
company_partner = self.company_id.partner_id

# find user for creating and validation SO/PO from partner company
intercompany_uid = (company.intercompany_user_id and
company.intercompany_user_id.id or False)
if not intercompany_uid:
raise Warning(_(
raise UserError(_(
'Provide at least one user for inter company relation for % ')
% company.name)
# check intercompany user access rights
if not SaleOrder.sudo(intercompany_uid).check_access_rights(
'create', raise_exception=False):
raise Warning(_(
raise UserError(_(
"Inter company user of company %s doesn't have enough "
"access rights") % company.name)

# Accessing to selling partner with selling user, so data like
# property_account_position can be retrieved
company_partner = self.env['res.partner'].sudo(
intercompany_uid).browse(self.company_id.partner_id.id)

# check pricelist currency should be same with SO/PO document
if self.pricelist_id.currency_id.id != (
company_partner.property_product_pricelist.currency_id.id):
raise Warning(_(
raise UserError(_(
'You cannot create SO from PO because '
'sale price list currency is different than '
'purchase price list currency.'))
Expand Down Expand Up @@ -102,8 +106,10 @@ def _prepare_sale_order_data(self, name, partner, company,
'delivery',
'contact'])
return {
'name': (self.env['ir.sequence'].sudo().next_by_code('sale.order')
or '/'),
'name': (
self.env['ir.sequence'].sudo().next_by_code('sale.order') or
'/'
),
'company_id': company.id,
'client_order_ref': name,
'partner_id': partner.id,
Expand Down Expand Up @@ -131,20 +137,24 @@ def _prepare_sale_order_line_data(self, line, company, sale_id):
# it may not affected because of parallel company relation
price = line.price_unit or 0.0
taxes = line.taxes_id
product = None
if line.product_id:
taxes = line.product_id.taxes_id
# make a new browse otherwise line._uid keeps the purchasing
# company's user and can't see the selling company's taxes
product = self.env['product.product'].browse(line.product_id.id)
taxes = product.taxes_id
company_taxes = [tax_rec.id
for tax_rec in taxes
if tax_rec.company_id.id == company.id]
return {
'name': line.product_id and line.product_id.name or line.name,
'name': line.name,
'order_id': sale_id,
'product_uom_qty': line.product_qty,
'product_id': line.product_id and line.product_id.id or False,
'product_uom': (line.product_id and line.product_id.uom_id.id or
'product_id': product and product.id or False,
'product_uom': (product and product.uom_id.id or
line.product_uom.id),
'price_unit': price,
'delay': line.product_id and line.product_id.sale_delay or 0.0,
'delay': product and product.sale_delay or 0.0,
'company_id': company.id,
'tax_id': [(6, 0, company_taxes)],
}
2 changes: 1 addition & 1 deletion inter_company_rules/models/res_company.py
Expand Up @@ -3,7 +3,7 @@
from openerp.exceptions import ValidationError


class res_company(models.Model):
class ResCompany(models.Model):

_inherit = 'res.company'

Expand Down
2 changes: 1 addition & 1 deletion inter_company_rules/models/res_config.py
Expand Up @@ -2,7 +2,7 @@
from openerp import models, fields, api


class inter_company_rules_configuration(models.TransientModel):
class InterCompanyRulesConfig(models.TransientModel):

_inherit = 'base.config.settings'

Expand Down
14 changes: 7 additions & 7 deletions inter_company_rules/models/sale_order.py
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
from openerp import api, fields, models, _
from openerp.exceptions import Warning
from openerp.exceptions import Warning as UserError


class sale_order(models.Model):
class SaleOrder(models.Model):

_inherit = "sale.order"

Expand All @@ -16,7 +16,7 @@ class sale_order(models.Model):
@api.multi
def action_button_confirm(self):
""" Generate inter company purchase order based on conditions """
res = super(sale_order, self).action_button_confirm()
res = super(SaleOrder, self).action_button_confirm()
for order in self:
# if company_id not found, return to normal behavior
if not order.company_id:
Expand Down Expand Up @@ -48,21 +48,21 @@ def inter_company_create_purchase_order(self, company):
intercompany_uid = (company.intercompany_user_id and
company.intercompany_user_id.id or False)
if not intercompany_uid:
raise Warning(_(
raise UserError(_(
'Provide one user for intercompany relation for % ')
% company.name)
# check intercompany user access rights
if not PurchaseOrder.sudo(intercompany_uid).check_access_rights(
'create', raise_exception=False):
raise Warning(_(
raise UserError(_(
"Inter company user of company %s doesn't have enough "
"access rights") % company.name)

# check pricelist currency should be same with SO/PO document
if self.pricelist_id.currency_id.id != (
company_partner.property_product_pricelist_purchase.
currency_id.id):
raise Warning(_(
raise UserError(_(
'You cannot create PO from SO because '
'purchase pricelist currency is different than '
'sale pricelist currency.'))
Expand Down Expand Up @@ -103,7 +103,7 @@ def _prepare_purchase_order_data(self, company, company_partner):
company.warehouse_id.company_id.id == company.id and
company.warehouse_id or False)
if not warehouse:
raise Warning(_(
raise UserError(_(
'Configure correct warehouse for company(%s) from '
'Menu: Settings/companies/companies' % (company.name)))

Expand Down
3 changes: 2 additions & 1 deletion inter_company_rules/test/inter_company_invoice.yml
Expand Up @@ -73,6 +73,7 @@
assert supplier_invoice.invoice_line[0].account_id.company_id.id == ref("company_b"), "Applied account in created invoice line is not relevant to company."

assert supplier_invoice.state == "draft", "invoice should be in draft state."
assert supplier_invoice.amount_total == 450.0, "Total amount is incorrect."
# invoice total must include tax (in configurable CoA is 15%)
assert supplier_invoice.amount_total == 517.5, "Total amount is incorrect. Found %s" % supplier_invoice.amount_total
assert supplier_invoice.company_id.id == ref("company_b"), "Applied company in created invoice is incorrect."
assert supplier_invoice.account_id.company_id.id == ref("company_b"), "Applied account in created invoice is not relevant to company."
6 changes: 6 additions & 0 deletions inter_company_rules/test/test_intercompany_data.yml
Expand Up @@ -100,6 +100,12 @@
groups_id:
- base.group_sale_salesman
- purchase.group_purchase_user
-
!record {model: res.company, id: company_a, view: False}:
intercompany_user_id: res_users_company_a
-
!record {model: res.company, id: company_b, view: False}:
intercompany_user_id: res_users_company_b
-
Install COA for new companies.
-
Expand Down
2 changes: 1 addition & 1 deletion inter_company_rules/views/inter_company_so_po_view.xml
Expand Up @@ -20,5 +20,5 @@
</xpath>
</field>
</record>
</data>
</data>
</openerp>
2 changes: 1 addition & 1 deletion inter_company_rules/views/res_config_view.xml
Expand Up @@ -31,4 +31,4 @@
</field>
</record>
</data>
</openerp>
</openerp>

0 comments on commit 58a02de

Please sign in to comment.