Skip to content

Commit

Permalink
Merge pull request #74 from credativUK/7.0-tax-inclusion-per-storeview
Browse files Browse the repository at this point in the history
Tax inclusion settings per storeview
  • Loading branch information
guewen committed Jan 6, 2015
2 parents 0df0279 + f5e8969 commit b02928b
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 36 deletions.
2 changes: 1 addition & 1 deletion magentoerpconnect/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
##############################################################################

{'name': 'Magento Connector',
'version': '2.4.2',
'version': '2.5.0',
'category': 'Connector',
'depends': ['account',
'product',
Expand Down
2 changes: 1 addition & 1 deletion magentoerpconnect/magento_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 = {
Expand Down
17 changes: 9 additions & 8 deletions magentoerpconnect/magento_model_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,6 @@
<field name="sale_prefix" placeholder="mag-" />
<field name="product_stock_field_id" widget="selection"
domain="[('model', 'in', ['product.product', 'product.template']), ('ttype', '=', 'float')]"/>
<field name="catalog_price_tax_included"/>
<p attrs="{'invisible': [('catalog_price_tax_included', '=', False)]}">
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.
</p>
</group>
</page>

Expand Down Expand Up @@ -314,6 +306,14 @@
</group>
<group string="Options">
<field name="lang_id" widget="selection"/>
<field name="catalog_price_tax_included"/>
<p attrs="{'invisible': [('catalog_price_tax_included', '=', False)]}">
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.
</p>
</group>
<notebook>
<page name="import" string="Imports">
Expand Down Expand Up @@ -350,6 +350,7 @@
<field name="code"/>
<field name="enabled"/>
<field name="no_sales_order_sync"/>
<field name="catalog_price_tax_included"/>
<field name="sort_order"/>
<field name="store_id"/>
</tree>
Expand Down
39 changes: 39 additions & 0 deletions magentoerpconnect/migrations/7.0.2.5.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
#
##############################################################################

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")
38 changes: 20 additions & 18 deletions magentoerpconnect/sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 18 additions & 8 deletions magentoerpconnect/tests/test_synchronization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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})

0 comments on commit b02928b

Please sign in to comment.