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

8.0 Add support for multi company #212

Open
wants to merge 7 commits into
base: 8.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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 magentoerpconnect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
from . import stock_picking
from . import stock_tracking
from . import payment_method
from . import res_company

from . import consumer
1 change: 1 addition & 0 deletions magentoerpconnect/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
'stock_view.xml',
'security/ir.model.access.csv',
'payment_method_view.xml',
'res_company_view.xml',
],
'installable': True,
'application': True,
Expand Down
36 changes: 34 additions & 2 deletions magentoerpconnect/magento_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ def _get_stock_field_id(self):
'The value can also be specified on website or the store or the '
'store view.'
)
is_multi_company = fields.Boolean(
string='Is Backend Multi-Company',
help="If this flag is set, it is possible to choose warehouse at each "
"level. "
"When import partner, ignore company_id if this flag is set.",
)

_sql_constraints = [
('sale_prefix_uniq', 'unique(sale_prefix)',
Expand Down Expand Up @@ -364,6 +370,15 @@ class MagentoConfigSpecializer(models.AbstractModel):
'The value can also be specified on website or the store or the '
'store view.'
)
specific_warehouse_id = fields.Many2one(
comodel_name='stock.warehouse',
string='Specific warehouse',
help='If specified, this warehouse will be used to load fill the '
'field warehouse (and company) on the sale order created by the '
'connector.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not working in my setup. We get another warehouse from the same company. In fact, I cannot seem to find any place (in this patch or elsewhere) where the Magento warehouse setting is used to assign a warehouse to the sale orders imported from Magento. The Magento documentation does not even claim that the warehouse setting is used as such, but says the warehouse is used to gather the available product quantities instead (http://odoo-magento-connector.com/howto/configure_warehouse.html). Can you confirm that this setting is actually ignored for sale orders created from the Magento import?

'The value can also be specified on website or the store or the '
'store view.'
)
account_analytic_id = fields.Many2one(
comodel_name='account.analytic.account',
string='Analytic account',
Expand All @@ -374,6 +389,10 @@ class MagentoConfigSpecializer(models.AbstractModel):
string='Fiscal position',
compute='_get_fiscal_position_id',
)
warehouse_id = fields.Many2one(
comodel_name='stock.warehouse',
string='warehouse',
compute='_get_warehouse_id')

@property
def _parent(self):
Expand All @@ -393,6 +412,13 @@ def _get_fiscal_position_id(self):
this.specific_fiscal_position_id or
this._parent.fiscal_position_id)

@api.multi
def _get_warehouse_id(self):
for this in self:
this.warehouse_id = (
this.specific_warehouse_id or
this._parent.warehouse_id)


class MagentoWebsite(models.Model):
_name = 'magento.website'
Expand All @@ -419,6 +445,7 @@ class MagentoWebsite(models.Model):
string='Magento Products',
readonly=True,
)
is_multi_company = fields.Boolean(related="backend_id.is_multi_company")

@api.multi
def import_partners(self):
Expand Down Expand Up @@ -503,6 +530,7 @@ class MagentoStore(models.Model):
"payment method is not giving an option for this by "
"itself. (See Payment Methods)",
)
is_multi_company = fields.Boolean(related="backend_id.is_multi_company")


class MagentoStoreview(models.Model):
Expand Down Expand Up @@ -544,13 +572,17 @@ class MagentoStoreview(models.Model):
'but its sales orders should not be imported.',
)
catalog_price_tax_included = fields.Boolean(string='Prices include tax')
is_multi_company = fields.Boolean(related="backend_id.is_multi_company")

@api.multi
def import_sale_orders(self):
session = ConnectorSession(self.env.cr, self.env.uid,
context=self.env.context)
import_start_time = datetime.now()
for storeview in self:
user_id = storeview.sudo().warehouse_id.company_id.\
magento_company_user_id.id or self.env.uid
session = ConnectorSession(self.env.cr, user_id,
context=self.env.context)

if storeview.no_sales_order_sync:
_logger.debug("The storeview '%s' is active in Magento "
"but is configured not to import the "
Expand Down
10 changes: 10 additions & 0 deletions magentoerpconnect/magento_model_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
domain="[('model', 'in', ['product.product', 'product.template']), ('ttype', '=', 'float')]"/>
<field name="account_analytic_id" groups="sale.group_analytic_accounting" />
<field name="fiscal_position_id"/>
<field name="is_multi_company"/>
</group>
</page>

Expand Down Expand Up @@ -188,6 +189,9 @@
<field name="specific_account_analytic_id" groups="sale.group_analytic_accounting" class="oe_edit_only" />
<field name="fiscal_position_id" class="oe_read_only"/>
<field name="specific_fiscal_position_id" class="oe_edit_only" />
<field name="is_multi_company" invisible="1"/>
<field name="warehouse_id" class="oe_read_only" attrs="{'invisible': [('is_multi_company', '=', False)]}"/>
<field name="specific_warehouse_id" class="oe_edit_only" attrs="{'invisible': [('is_multi_company', '=', False)]}"/>
</group>
<notebook>
<page name="import" string="Imports">
Expand Down Expand Up @@ -258,6 +262,9 @@
<field name="specific_account_analytic_id" groups="sale.group_analytic_accounting" class="oe_edit_only" />
<field name="fiscal_position_id" class="oe_read_only"/>
<field name="specific_fiscal_position_id" class="oe_edit_only" />
<field name="is_multi_company" invisible="1"/>
<field name="warehouse_id" class="oe_read_only" attrs="{'invisible': [('is_multi_company', '=', False)]}"/>
<field name="specific_warehouse_id" class="oe_edit_only" attrs="{'invisible': [('is_multi_company', '=', False)]}"/>
<field name="send_picking_done_mail"/>
<field name="send_invoice_paid_mail"/>
<field name="create_invoice_on"/>
Expand Down Expand Up @@ -318,6 +325,9 @@
<field name="specific_account_analytic_id" groups="sale.group_analytic_accounting" class="oe_edit_only" />
<field name="fiscal_position_id" class="oe_read_only"/>
<field name="specific_fiscal_position_id" class="oe_edit_only" />
<field name="is_multi_company" invisible="1"/>
<field name="warehouse_id" class="oe_read_only" attrs="{'invisible': [('is_multi_company', '=', False)]}"/>
<field name="specific_warehouse_id" class="oe_edit_only" attrs="{'invisible': [('is_multi_company', '=', False)]}"/>
<field name="section_id" options="{'no_create': True}" groups="base.group_multi_salesteams"/>
<field name="lang_id" widget="selection"/>
<field name="catalog_price_tax_included"/>
Expand Down
4 changes: 4 additions & 0 deletions magentoerpconnect/partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ def website_id(self, record):
@only_create
@mapping
def company_id(self, record):
if self.backend_record.is_multi_company:
return {'company_id': False}
binder = self.binder_for(model='magento.storeview')
storeview = binder.to_openerp(record['store_id'], browse=True)
if storeview:
Expand Down Expand Up @@ -497,6 +499,8 @@ def title(self, record):
@only_create
@mapping
def company_id(self, record):
if self.backend_record.is_multi_company:
return {'company_id': False}
parent = self.options.parent_partner
if parent:
if parent.company_id:
Expand Down
6 changes: 5 additions & 1 deletion magentoerpconnect/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ def _recompute_magento_qty_backend(self, backend, products,
else:
stock_field = 'virtual_available'

location = backend.warehouse_id.lot_stock_id
location = self.env['stock.location']
if self.env.context.get('location'):
location = location.browse(self.env.context['location'])
else:
location = backend.warehouse_id.lot_stock_id

product_fields = ['magento_qty', stock_field]
if read_fields:
Expand Down
16 changes: 16 additions & 0 deletions magentoerpconnect/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
# © 2016 Laetitia Gangloff, Acsone SA/NV (http://www.acsone.eu)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import fields, models


class ResCompany(models.Model):
_inherit = "res.company"

magento_company_user_id = fields.Many2one(
comodel_name="res.users",
string="Magento Company User",
help="The user attached to the company use to import sale order",
domain="[('company_id', '=', id)]"
)
22 changes: 22 additions & 0 deletions magentoerpconnect/res_company_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>

<record id="res_company_view_form_inherit_magentoerpconnect" model="ir.ui.view">
<field name="name">res.company.form (magentoerpconnect)</field>
<field name="model">res.company</field>
<field name="inherit_id" ref="base.view_company_form"/>
<field name="priority">20</field>
<field name="arch" type="xml">
<page string="Configuration" position="inside">
<group>
<group string="Magento Parameters">
<field name="magento_company_user_id"/>
</group>
</group>
</page>
</field>
</record>

</data>
</openerp>
38 changes: 36 additions & 2 deletions magentoerpconnect/tests/test_address_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ def setUp(self):
self.model = self.env['magento.res.partner']
self.address_model = self.env['magento.address']
warehouse_id = self.env.ref('stock.warehouse0').id
self.backend_id = self.backend_model.create({
self.backend = self.backend_model.create({
'name': 'Test Magento Address book',
'version': '1.7',
'location': 'http://anyurl',
'username': 'guewen',
'warehouse_id': warehouse_id,
'password': '42'}).id
'password': '42'})
self.backend_id = self.backend.id
with mock_api(magento_base_responses):
import_batch(self.session, 'magento.website', self.backend_id)
import_batch(self.session, 'magento.store', self.backend_id)
Expand Down Expand Up @@ -93,6 +94,8 @@ def test_individual_1_address(self):
self.assertEqual(address_bind.magento_id, '9999253',
msg="The merged address should be the "
"billing address")
self.assertEqual(partner.company_id.id,
self.backend.warehouse_id.company_id.id)

def test_individual_2_addresses(self):
""" Import an individual (b2c) with 2 addresses """
Expand All @@ -117,6 +120,10 @@ def test_individual_2_addresses(self):
self.assertEqual(partner.child_ids[0].type, 'delivery',
msg="The shipping address should be of "
"type 'delivery'")
self.assertEqual(partner.company_id.id,
self.backend.company_id.id)
self.assertEqual(partner.child_ids[0].company_id.id,
self.backend.company_id.id)

def test_company_1_address(self):
""" Import an company (b2b) with 1 address """
Expand Down Expand Up @@ -169,3 +176,30 @@ def get_address(magento_id):
self.assertEqual(address.type, 'delivery',
msg="The shipping address should be of "
"type 'delivery'")

def test_multi_company_2_addresses(self):
"""Import an invidual on multi backend company"""
self.backend.is_multi_company = True
with mock_api(individual_2_addresses):
import_record(self.session, 'magento.res.partner',
self.backend_id, '9999255')
partner = self.model.search([('magento_id', '=', '9999255'),
('backend_id', '=', self.backend_id)])
self.assertEqual(len(partner), 1)
# Name of the billing address
self.assertEqual(partner.name, u'Mace Sébastien')
self.assertEqual(partner.type, 'default')
# billing address merged with the partner,
# second address as a contact
self.assertEqual(len(partner.child_ids), 1)
self.assertEqual(len(partner.magento_bind_ids), 1)
self.assertEqual(len(partner.magento_address_bind_ids), 1)
address_bind = partner.magento_address_bind_ids[0]
self.assertEqual(address_bind.magento_id, '9999254',
msg="The merged address should be the "
"billing address")
self.assertEqual(partner.child_ids[0].type, 'delivery',
msg="The shipping address should be of "
"type 'delivery'")
self.assertFalse(partner.company_id.id)
self.assertFalse(partner.child_ids[0].company_id.id)
43 changes: 40 additions & 3 deletions magentoerpconnect/tests/test_update_product_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
class TestUpdateStockQty(SetUpMagentoSynchronized):
""" Test the export of pickings to Magento """

def _product_change_qty(self, product, new_qty):
def _product_change_qty(self, product, new_qty, location_id=False):
wizard_model = self.env['stock.change.product.qty']
wizard = wizard_model.create({'product_id': product.id,
'new_quantity': new_qty})
data = {'product_id': product.id,
'new_quantity': new_qty}
if location_id:
data['location_id'] = location_id
wizard = wizard_model.create(data)
wizard.change_product_qty()

def setUp(self):
Expand Down Expand Up @@ -199,3 +202,37 @@ def test_export_qty_api_on_write(self):
'backorders': 2,
'use_config_backorders': 0,
})

def test_compute_new_qty_on_location(self):
product = self.binding_product.openerp_id
binding = self.binding_product
# start with 0
self.assertEqual(product.virtual_available, 0.0)
self.assertEqual(binding.magento_qty, 0.0)

my_location_id = self.env.ref("stock.stock_location_components").id

# change to 30
self._product_change_qty(product, 30)
self._product_change_qty(product, 5, my_location_id)

# the virtual available is 30, the magento qty has not been
# updated yet
self.assertEqual(product.virtual_available, 35.0)
self.assertEqual(binding.magento_qty, 0.0)

# search for the new quantities to push to Magento
# we mock the job so we can check it .delay() is called on it
# when the quantity is changed
patched = ('openerp.addons.magentoerpconnect.'
'product.export_product_inventory')
with mock.patch(patched) as export_product_inventory:
binding.with_context(location=my_location_id
).recompute_magento_qty()
self.assertEqual(binding.magento_qty, 5)
export_product_inventory.delay.assert_called_with(
mock.ANY,
'magento.product.product',
binding.id,
priority=20,
fields=['magento_qty'])