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

[12] Solved all showstopper bugs for importing from magento 1.X #319 #322

Open
wants to merge 9 commits into
base: 12.0
Choose a base branch
from
2 changes: 1 addition & 1 deletion connector_magento/components/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def _get_magento_data(self, storeview=None):
elif self.collection.version == '2.0':
storeview_id = storeview.code
else:
storeview_id = storeview.id
storeview_id = storeview.external_id
return self.backend_adapter.read(self.external_id, storeview_id)

def run(self, external_id, binding, mapper=None):
Expand Down
5 changes: 5 additions & 0 deletions connector_magento/components/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.addons.component.core import AbstractComponent
from odoo.addons.connector.components.mapper import mapping


class MagentoImportMapper(AbstractComponent):
_name = 'magento.import.mapper'
_inherit = ['base.magento.connector', 'base.import.mapper']
_usage = 'import.mapper'

@mapping
def user_data(self, record):
return {f: record.get(f[3:]) for f in self.model._fields if f.startswith('x__')}


class MagentoExportMapper(AbstractComponent):
_name = 'magento.export.mapper'
Expand Down
2 changes: 1 addition & 1 deletion connector_magento/models/partner/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def names(self, record):
@mapping
def customer_group_id(self, record):
# import customer groups
if record['group_id'] == 0:
if not record['group_id']:
category = self.env.ref('connector_magento.category_no_account')
else:
binder = self.binder_for(model='magento.res.partner.category')
Expand Down
75 changes: 41 additions & 34 deletions connector_magento/models/sale_order/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _rule_authorized(self, record, method):
def _rule_paid(self, record, method):
""" Import the order only if it has received a payment, or if there
is nothing to pay in the first place. """
amount_paid = record.get('payment', {}).get('amount_paid') or 0
amount_paid = float(record.get('payment', {}).get('amount_paid') or 0)
if record['grand_total'] and amount_paid <= 0:
raise OrderImportRuleRetry('The order has not been paid.\n'
'The import will be retried later.')
Expand Down Expand Up @@ -127,15 +127,14 @@ def check(self, record):


class SaleOrderImportMapper(Component):

_name = 'magento.sale.order.mapper'
_inherit = 'magento.import.mapper'
_apply_on = 'magento.sale.order'

direct = [('increment_id', 'external_id'),
('order_id', 'magento_order_id'),
('grand_total', 'total_amount'),
('tax_amount', 'total_amount_tax'),
('base_grand_total', 'total_amount'),
('base_tax_amount', 'total_amount_tax'),
(normalize_datetime('created_at'), 'date_order'),
('store_id', 'storeview_id'),
]
Expand All @@ -151,7 +150,7 @@ def _add_shipping_line(self, map_record, values):
record.get('base_shipping_incl_tax') or 0.0) - discount
else:
amount = float(
record.get('shipping_amount') or 0.0) - discount
record.get('base_shipping_amount') or 0.0) - discount
line_builder = self.component(usage='order.line.builder.shipping')
# add even if the price is 0, otherwise odoo will add a shipping
# line in the order when we ship the picking
Expand Down Expand Up @@ -278,20 +277,20 @@ def customer_id(self, record):
"SaleOrderImporter._import_dependencies" % record['customer_id'])
return {'partner_id': partner.id}

@mapping
def pricelist_id(self, record):
""" Assign a pricelist in the correct currency if necessary. """
currency = record['order_currency_code']
partner = self.binder_for('magento.res.partner').to_internal(
record['customer_id'], unwrap=True)
if partner.property_product_pricelist.currency_id.name != currency:
pricelist = self.env['product.pricelist'].search(
[('currency_id.name', '=', currency)], limit=1)
if not pricelist:
raise FailedJobError(
"Missing pricelist for this order's currency: %s" %
currency)
return {'pricelist_id': pricelist.id}
# @mapping
# def pricelist_id(self, record):
# """ Assign a pricelist in the correct currency if necessary. """
# currency = record['order_currency_code']
# partner = self.binder_for('magento.res.partner').to_internal(
# record['customer_id'], unwrap=True)
# if partner.property_product_pricelist.currency_id.name != currency:
# pricelist = self.env['product.pricelist'].search(
# [('currency_id.name', '=', currency)], limit=1)
# if not pricelist:
# raise FailedJobError(
# "Missing pricelist for this order's currency: %s" %
# currency)
# return {'pricelist_id': pricelist.id}

@mapping
def payment(self, record):
Expand Down Expand Up @@ -720,7 +719,6 @@ def _import_dependencies(self):


class SaleOrderLineImportMapper(Component):

_name = 'magento.sale.order.line.mapper'
_inherit = 'magento.import.mapper'
_apply_on = 'magento.sale.order.line'
Expand All @@ -739,8 +737,11 @@ def discount_amount(self, record):
else:
row_total = float(record.get('row_total') or 0)
discount = 0
if discount_value > 0:
discount = 100 * discount_value / (discount_value + row_total)
if discount_value > 0 and row_total > 0:
if self.collection.version == '1.7':
discount = 100 * discount_value / row_total
else:
discount = 100 * discount_value / (row_total + discount_value)
result = {'discount': discount}
return result

Expand Down Expand Up @@ -772,8 +773,8 @@ def product_options(self, record):
# }
# }
if record.get('product_option', {}).get(
'extension_attributes', {}).get(
'configurable_item_options'):
'extension_attributes', {}).get(
'configurable_item_options'):
_logger.debug(
'Magento order#%s contains a product with configurable '
'options but their import is not supported yet for '
Expand All @@ -799,14 +800,20 @@ def product_options(self, record):
def price(self, record):
""" In Magento 2, base_row_total_incl_tax may not be present
if no taxes apply """
discount_amount = float(record['base_discount_amount'] or 0)
base_row_total = float(record['base_row_total'] or 0.)
base_row_total_incl_tax = (
float(record['base_row_total_incl_tax'] or 0)
if 'base_row_total_incl_tax' in record else base_row_total)
qty_ordered = float(record['qty_ordered'])
if self.options.tax_include:
total = base_row_total_incl_tax
if self.collection.version == '1.7':
discount_amount = float(record['base_discount_amount'] or 0)
base_row_total = float(record['base_row_total'] or 0.)
base_row_total_incl_tax = (
float(record['base_row_total_incl_tax'] or 0)
if 'base_row_total_incl_tax' in record else base_row_total)
qty_ordered = float(record['qty_ordered'])
if self.options.tax_include:
total = base_row_total_incl_tax
else:
total = base_row_total
return {'price_unit': total / qty_ordered}
else:
total = base_row_total
return {'price_unit': (total + discount_amount) / qty_ordered}
if self.options.tax_include:
return {'price_unit': record.get('base_price_incl_tax', 0.0)}
else:
return {'price_unit': record.get('base_price', 0.0)}
52 changes: 52 additions & 0 deletions connector_magento_firstname/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

====================================================
Magento Connector - Partner first name and last name
====================================================

Glue module between Magento Connector and Partner first name / last name.

Installation
============

This module is automatically installed when ``connector_magento`` and
``partner_firstname`` are installed.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/connector-magento/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smashing it by providing a detailed and welcomed feedback.

Credits
=======

Images
------

* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.

Contributors
------------

* Guewen Baconnier <[email protected]>

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit https://odoo-community.org.

2 changes: 2 additions & 0 deletions connector_magento_firstname/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import models
17 changes: 17 additions & 0 deletions connector_magento_firstname/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

{'name': 'Magento Connector - Partner first name and last name',
'version': '12.0.0.0',
'author': 'Camptocamp,Odoo Community Association (OCA)',
'license': 'AGPL-3',
'category': 'Hidden',
'depends': ['connector_magento',
'partner_firstname',
],
'website': 'https://www.camptocamp.com',
'data': [],
'installable': True,
'auto_install': True,
}
14 changes: 14 additions & 0 deletions connector_magento_firstname/i18n/connector_magento_firstname.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 10.0\n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

2 changes: 2 additions & 0 deletions connector_magento_firstname/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import partner
30 changes: 30 additions & 0 deletions connector_magento_firstname/models/partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from odoo.addons.component.core import Component
from odoo.addons.connector.components.mapper import mapping


class PartnerImportMapper(Component):
_inherit = 'magento.partner.import.mapper'

@mapping
def names(self, record):
parts = [part for part in (record['firstname'],
record.get('middlename')) if part]
values = {'firstname': ' '.join(parts),
'lastname': record['lastname']}
return values


class AddressImportMapper(Component):
_inherit = 'magento.address.import.mapper'

@mapping
def names(self, record):
parts = [part for part in (record['firstname'],
record.get('middlename')) if part]
values = {'firstname': ' '.join(parts),
'lastname': record['lastname']}
return values