diff --git a/magentoerpconnect/__openerp__.py b/magentoerpconnect/__openerp__.py index c8e360986..52164b97d 100644 --- a/magentoerpconnect/__openerp__.py +++ b/magentoerpconnect/__openerp__.py @@ -21,7 +21,7 @@ ############################################################################## {'name': 'Magento Connector', - 'version': '2.4.2', + 'version': '2.5.0', 'category': 'Connector', 'depends': ['account', 'product', diff --git a/magentoerpconnect/magento_model.py b/magentoerpconnect/magento_model.py index 6695ce295..2ac378d72 100644 --- a/magentoerpconnect/magento_model.py +++ b/magentoerpconnect/magento_model.py @@ -144,7 +144,6 @@ def _get_stock_field_id(self, cr, uid, context=None): 'Import products from date'), 'import_categories_from_date': fields.datetime( 'Import categories from date'), - 'catalog_price_tax_included': fields.boolean('Prices include tax'), 'product_stock_field_id': fields.many2one( 'ir.model.fields', string='Stock Field', @@ -530,6 +529,7 @@ class magento_storeview(orm.Model): 'No Sales Order Synchronization', help='Check if the storeview is active in Magento ' 'but its sales orders should not be imported.'), + 'catalog_price_tax_included': fields.boolean('Prices include tax'), } _defaults = { diff --git a/magentoerpconnect/magento_model_view.xml b/magentoerpconnect/magento_model_view.xml index bde68eb5f..8972ae66c 100644 --- a/magentoerpconnect/magento_model_view.xml +++ b/magentoerpconnect/magento_model_view.xml @@ -129,14 +129,6 @@ - -

- This option should respect the same - configuration than Magento. Pay - attention to the taxes on the products, - which should surely include prices when - this option is activated. -

@@ -314,6 +306,14 @@ + +

+ This option should respect the same + configuration as Magento. Pay + attention to the taxes on the products, + which should surely include prices when + this option is activated. +

@@ -350,6 +350,7 @@ + diff --git a/magentoerpconnect/migrations/7.0.2.5.0/post-migration.py b/magentoerpconnect/migrations/7.0.2.5.0/post-migration.py new file mode 100644 index 000000000..2eb13c1ae --- /dev/null +++ b/magentoerpconnect/migrations/7.0.2.5.0/post-migration.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Author: Ondřej Kuzník +# Copyright 2014 credativ, ltd. +# +# This program 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. +# +# This program 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 this program. If not, see . +# +############################################################################## + +import logging + +logger = logging.getLogger('upgrade') + + +def migrate(cr, version): + """ + The tax inclusion setting has moved from the backend to the storeview. + """ + if version: # do not run on a fresh DB, see lp:1259975 + logger.info("Migrating magentoerpconnect from version %s", version) + cr.execute("UPDATE magento_storeview msw " + "SET catalog_price_tax_included = " + " (SELECT mb.catalog_price_tax_included " + " FROM magento_backend mb WHERE " + " mb.id = msw.backend_id)") + cr.execute("ALTER TABLE magento_backend DROP " + " COLUMN catalog_price_tax_included") diff --git a/magentoerpconnect/sale.py b/magentoerpconnect/sale.py index 086d3899c..2f8e6a967 100644 --- a/magentoerpconnect/sale.py +++ b/magentoerpconnect/sale.py @@ -599,20 +599,23 @@ def _after_import(self, binding_id): SaleOrderMoveComment) move_comment.move(binding) + def _get_storeview(self, record): + """ Return the tax inclusion setting for the appropriate storeview """ + storeview_binder = self.get_binder_for_model('magento.storeview') + # we find storeview_id in store_id! + # (http://www.magentocommerce.com/bug-tracking/issue?issue=15886) + storeview_id = storeview_binder.to_openerp(record['store_id']) + storeview = self.session.browse('magento.storeview', storeview_id) + return storeview + def _get_magento_data(self): """ Return the raw Magento data for ``self.magento_id`` """ record = super(SaleOrderImport, self)._get_magento_data() # sometimes we don't have website_id... # we fix the record! if not record.get('website_id'): + storeview = self._get_storeview(record) # deduce it from the storeview - storeview_binder = self.get_binder_for_model('magento.storeview') - # we find storeview_id in store_id! - # (http://www.magentocommerce.com/bug-tracking/issue?issue=15886) - oe_storeview_id = storeview_binder.to_openerp(record['store_id']) - storeview = self.session.browse('magento.storeview', - oe_storeview_id) - # "fix" the record record['website_id'] = storeview.store_id.website_id.magento_id # sometimes we need to clean magento items (ex : configurable # product in a sale) @@ -770,23 +773,27 @@ def _check_special_fields(self): "in SaleOrderImport._import_addresses") def _create_data(self, map_record, **kwargs): - tax_include = self.backend_record.catalog_price_tax_included + storeview = self._get_storeview(map_record.source) self._check_special_fields() return super(SaleOrderImport, self)._create_data( - map_record, tax_include=tax_include, + map_record, + tax_include=storeview.catalog_price_tax_included, partner_id=self.partner_id, partner_invoice_id=self.partner_invoice_id, partner_shipping_id=self.partner_shipping_id, + storeview=storeview, **kwargs) def _update_data(self, map_record, **kwargs): - tax_include = self.backend_record.catalog_price_tax_included + storeview = self._get_storeview(map_record.source) self._check_special_fields() return super(SaleOrderImport, self)._update_data( - map_record, tax_include=tax_include, + map_record, + tax_include=storeview.catalog_price_tax_included, partner_id=self.partner_id, partner_invoice_id=self.partner_invoice_id, partner_shipping_id=self.partner_shipping_id, + storeview=storeview, **kwargs) def _import_dependencies(self): @@ -899,12 +906,7 @@ def name(self, record): @mapping def store_id(self, record): - binder = self.get_binder_for_model('magento.storeview') - storeview_id = binder.to_openerp(record['store_id']) - assert storeview_id is not None, ('cannot import sale orders from ' - 'non existing storeview') - storeview = self.session.browse('magento.storeview', storeview_id) - shop_id = storeview.store_id.openerp_id.id + shop_id = self.options.storeview.store_id.openerp_id.id return {'shop_id': shop_id} @mapping @@ -993,7 +995,7 @@ class SaleOrderLineImportMapper(ImportMapper): @mapping def discount_amount(self, record): discount_value = float(record.get('discount_amount') or 0) - if self.backend_record.catalog_price_tax_included: + if self.options.tax_include: row_total = float(record.get('row_total_incl_tax') or 0) else: row_total = float(record.get('row_total') or 0) diff --git a/magentoerpconnect/tests/test_synchronization.py b/magentoerpconnect/tests/test_synchronization.py index 8fe0db2c6..9dbff716b 100644 --- a/magentoerpconnect/tests/test_synchronization.py +++ b/magentoerpconnect/tests/test_synchronization.py @@ -308,8 +308,13 @@ def test_33_import_sale_order_with_configurable(self): def test_34_import_sale_order_with_taxes_included(self): """ Import sale order with taxes included """ backend_id = self.backend_id - self.backend_model.write(self.cr, self.uid, self.backend_id, - {'catalog_price_tax_included': True}) + storeview_model = self.registry('magento.storeview') + storeview_id = storeview_model.search( + self.cr, self.uid, + [('backend_id', '=', backend_id), + ('magento_id', '=', '1')]) + storeview_model.write(self.cr, self.uid, storeview_id, + {'catalog_price_tax_included': True}) with mock_api(magento_base_responses): with mock_urlopen_image(): import_record(self.session, @@ -333,14 +338,19 @@ def test_34_import_sale_order_with_taxes_included(self): # 97.5 is the amount_total if connector takes correctly included # tax prices. self.assertEqual(amount_total, 97.5000) - self.backend_model.write(self.cr, self.uid, self.backend_id, - {'catalog_price_tax_included': False}) + storeview_model.write(self.cr, self.uid, storeview_id, + {'catalog_price_tax_included': False}) def test_35_import_sale_order_with_discount(self): """ Import sale order with discounts""" backend_id = self.backend_id - self.backend_model.write(self.cr, self.uid, self.backend_id, - {'catalog_price_tax_included': True}) + storeview_model = self.registry('magento.storeview') + storeview_id = storeview_model.search( + self.cr, self.uid, + [('backend_id', '=', backend_id), + ('magento_id', '=', '2')]) + storeview_model.write(self.cr, self.uid, storeview_id, + {'catalog_price_tax_included': True}) with mock_api(magento_base_responses): with mock_urlopen_image(): import_record(self.session, @@ -371,5 +381,5 @@ def test_35_import_sale_order_with_discount(self): self.fail('encountered unexpected sale ' 'order line %s' % line.name) - self.backend_model.write(self.cr, self.uid, self.backend_id, - {'catalog_price_tax_included': False}) + storeview_model.write(self.cr, self.uid, storeview_id, + {'catalog_price_tax_included': False})