From 898452bce982be6c7a40ccf565033ef35daa2445 Mon Sep 17 00:00:00 2001 From: Pierrick Brun Date: Tue, 16 Oct 2018 16:57:20 +0200 Subject: [PATCH] [MIG] 10.0 connector_magento_pricing --- connector_magento_pricing/README.rst | 82 ++++ connector_magento_pricing/__init__.py | 3 + connector_magento_pricing/__manifest__.py | 19 + .../models}/__init__.py | 2 +- connector_magento_pricing/models/magento.py | 104 +++++ connector_magento_pricing/models/product.py | 130 ++++++ connector_magento_pricing/models/sale.py | 23 + .../readme/CONTRIBUTORS.rst | 2 + .../readme/DESCRIPTION.rst | 8 + .../static/description/index.html | 401 ++++++++++++++++++ .../views}/magento_model_view.xml | 13 +- magentoerpconnect_pricing/__manifest__.py | 45 -- magentoerpconnect_pricing/magento_model.py | 135 ------ magentoerpconnect_pricing/product.py | 156 ------- magentoerpconnect_pricing/sale.py | 42 -- 15 files changed, 779 insertions(+), 386 deletions(-) create mode 100644 connector_magento_pricing/README.rst create mode 100644 connector_magento_pricing/__init__.py create mode 100644 connector_magento_pricing/__manifest__.py rename {magentoerpconnect_pricing => connector_magento_pricing/models}/__init__.py (70%) create mode 100644 connector_magento_pricing/models/magento.py create mode 100644 connector_magento_pricing/models/product.py create mode 100644 connector_magento_pricing/models/sale.py create mode 100644 connector_magento_pricing/readme/CONTRIBUTORS.rst create mode 100644 connector_magento_pricing/readme/DESCRIPTION.rst create mode 100644 connector_magento_pricing/static/description/index.html rename {magentoerpconnect_pricing => connector_magento_pricing/views}/magento_model_view.xml (70%) delete mode 100644 magentoerpconnect_pricing/__manifest__.py delete mode 100644 magentoerpconnect_pricing/magento_model.py delete mode 100644 magentoerpconnect_pricing/product.py delete mode 100644 magentoerpconnect_pricing/sale.py diff --git a/connector_magento_pricing/README.rst b/connector_magento_pricing/README.rst new file mode 100644 index 000000000..50e2894f4 --- /dev/null +++ b/connector_magento_pricing/README.rst @@ -0,0 +1,82 @@ +=========================== +Connector Magento - Pricing +=========================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fconnector--magento-lightgray.png?logo=github + :target: https://github.com/OCA/connector-magento/tree/10.0/connector_magento_pricing + :alt: OCA/connector-magento +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/connector-magento-10-0/connector-magento-10-0-connector_magento_pricing + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/107/10.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Extension for **Connector Magento**. + +The prices of the products are managed in Odoo using pricelists and +are pushed to Magento. + +You can push all prices when modifying the pricelist on the backend. + +Each time a price is modified in Odoo, the price is pushed to Magento. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub 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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Camptocamp +* Akretion + +Contributors +~~~~~~~~~~~~ + +Guewen Baconnier +Pierrick Brun + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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. + +This module is part of the `OCA/connector-magento `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/connector_magento_pricing/__init__.py b/connector_magento_pricing/__init__.py new file mode 100644 index 000000000..cde864bae --- /dev/null +++ b/connector_magento_pricing/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import models diff --git a/connector_magento_pricing/__manifest__.py b/connector_magento_pricing/__manifest__.py new file mode 100644 index 000000000..b8e466b90 --- /dev/null +++ b/connector_magento_pricing/__manifest__.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Copyright 2013 Camptocamp SA +# Copyright 2018 Akretion +{ + 'name': 'Connector Magento - Pricing', + 'version': '10.0.1.0.0', + 'category': 'Connector', + 'depends': [ + 'connector_magento', + ], + 'author': "Camptocamp, Akretion, Odoo Community Association (OCA)", + 'license': 'AGPL-3', + 'website': 'http://www.odoo-magento-connector.com', + 'data': [ + 'views/magento_model_view.xml', + ], + 'installable': True, + 'application': False, + } diff --git a/magentoerpconnect_pricing/__init__.py b/connector_magento_pricing/models/__init__.py similarity index 70% rename from magentoerpconnect_pricing/__init__.py rename to connector_magento_pricing/models/__init__.py index cdd91666e..4706a4cb5 100644 --- a/magentoerpconnect_pricing/__init__.py +++ b/connector_magento_pricing/models/__init__.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from . import magento_model +from . import magento from . import product from . import sale diff --git a/connector_magento_pricing/models/magento.py b/connector_magento_pricing/models/magento.py new file mode 100644 index 000000000..7698b8fd4 --- /dev/null +++ b/connector_magento_pricing/models/magento.py @@ -0,0 +1,104 @@ +# -*- coding: utf-8 -*- +# Copyright 2013 Camptocamp SA +# Copyright 2018 Akretion + +from odoo import fields, models, api +from odoo.tools.translate import _ + + +class MagentoBackend(models.Model): + _inherit = 'magento.backend' + + def get_pricelist_id(self): + data_obj = self.env['ir.model.data'] + ref = data_obj.get_object_reference('product', 'list0') + if ref: + return ref[1] + return False + + pricelist_id = fields.Many2one( + 'product.pricelist', + 'Pricelist', + required=True, + default=get_pricelist_id, + help='The price list used to define ' + 'the prices of the products in ' + 'Magento.') + + @api.onchange('pricelist_id') + def onchange_pricelist_id(self): + if not self.id: # new record + return {} + warning = { + 'title': _('Warning'), + 'message': _('If you change the pricelist of the backend, ' + 'the price of all the products will be updated ' + 'in Magento.') + } + return {'warning': warning} + + def _update_default_prices(self): + """ Update the default prices of the products linked with + this backend. + + The default prices are linked with the 'Admin' website (id: 0). + """ + websites = self.env['magento.website'].search([ + ('backend_id', 'in'), + ('external_id', '=', '0')]) + websites._update_all_prices() + + def write(self, vals): + if 'pricelist_id' in vals: + self._update_default_prices() + return super(MagentoBackend, self).write(vals) + + +class MagentoWebsite(models.Model): + _inherit = 'magento.website' + + pricelist_id = fields.Many2one( + 'product.pricelist', + 'Pricelist', + help='The pricelist used to define ' + 'the prices of the products in ' + 'Magento for this website.\n' + 'Choose a pricelist only if the ' + 'prices are different for this ' + 'website.\n' + 'When empty, the default price ' + 'will be used.') + + def _update_all_prices(self): + """ Update the prices of all the products linked to the + website. """ + for website in self: + if website.external_id == '0': + # 'Admin' website -> default values + # Update the default prices on all the products. + binding_ids = website.backend_id.product_binding_ids + else: + binding_ids = website.product_binding_ids + for binding in binding_ids: + print binding + binding.with_delay().export_product_price( + website_id=website.id) + break + return True + + @api.onchange('pricelist_id') + def onchange_pricelist_id(self): + if not self.id: # new record + return {} + warning = { + 'title': _('Warning'), + 'message': _('If you change the pricelist of the website, ' + 'the price of all the products linked with this ' + 'website will be updated in Magento.') + } + return {'warning': warning} + + def write(self, vals): + if 'pricelist_id' in vals: + self._update_all_prices() + return super(MagentoWebsite, self).write(vals) diff --git a/connector_magento_pricing/models/product.py b/connector_magento_pricing/models/product.py new file mode 100644 index 000000000..0f8ed8b87 --- /dev/null +++ b/connector_magento_pricing/models/product.py @@ -0,0 +1,130 @@ +# -*- coding: utf-8 -*- +# Copyright 2013 Camptocamp SA +# Copyright 2018 Akretion + + +from odoo import api, models +from odoo.tools.translate import _ +from odoo.addons.queue_job.job import job, related_action +from odoo.addons.queue_job.exception import JobError +from odoo.addons.component.core import Component +from odoo.addons.component_event import skip_if +from odoo.addons.connector.components.mapper import mapping, only_create + + +# TODO: replace a price mapper only, not the full mapper +class ProductImportMapper(Component): + _inherit = 'magento.product.product.import.mapper' + + @only_create + @mapping + def price(self, record): + """ The price is imported at the creation of + the product, then it is only modified and exported + from Odoo """ + return super(ProductImportMapper, self).price(record) + + +class ProductPriceExporter(Component): + """ Export the price of a product. + + Use the pricelist configured on the backend for the + default price in Magento. + If different pricelists have been configured on the websites, + update the prices on the different websites. + """ + _name = 'magento.product.price.exporter' + _inherit = 'base.exporter' + _usage = 'price.exporter' + _apply_on = 'magento.product.product' + + def _get_price(self, pricelist_id): + """ Return the raw Odoo data for ``self.binding_id`` """ + if pricelist_id is None: + # a False value will set the 'Use default value' in Magento + return False + return self.env[self._apply_on].with_context( + {'pricelist': pricelist_id}).browse(self.binding_id.id).price + + def _update(self, data, storeview_id=None): + self.backend_adapter.write(self.binding_id.external_id, data, + storeview_id=storeview_id) + + def _run(self, binding, website_id=None): + """ Export the product inventory to Magento + + :param website_id: if None, export on all websites, + or Odoo ID for the website to update + """ + # export of products is not implemented so we just raise + # if the export was existing, we would export it + assert binding.external_id, "Record has been deleted in Magento" + pricelist = binding.backend_id.pricelist_id + if not pricelist: + name = binding.backend_id.name + raise JobError.IDMissingInBackend( + 'Configuration Error:\n' + 'No pricelist configured on the backend %s.\n\n' + 'Resolution:\n' + 'Go to Connectors > Backends > %s.\n' + 'Choose a pricelist.' % (name, name)) + pricelist_id = pricelist.id + + # export the price for websites if they have a different + # pricelist + storeview_binder = self.binder_for('magento.storeview') + for website in binding.backend_id.website_ids: + if website_id is not None and website.id != website_id: + continue + # 0 is the admin website, the update on this website + # set the default values in Magento, we use the default + # pricelist + site_pricelist_id = None + if website.external_id == '0': + site_pricelist_id = pricelist_id + elif website.pricelist_id: + site_pricelist_id = website.pricelist_id.id + + # The update of the prices in Magento is very weird: + # - The price is different per website (if the option + # is active in the config), but is shared between + # the store views of a website. + # - BUT the Magento API expects a storeview id to modify + # a price on a website (and not a website id...) + # So we take the first storeview of the website to update. + storeview_ids = self.env['magento.storeview'].search( + [('store_id.website_id.id', '=', website.id)]).ids + if not storeview_ids: + continue + magento_storeview = storeview_binder.to_internal(storeview_ids[0]) + self.binding_id = binding + price = self._get_price(site_pricelist_id) + self._update({'price': price}, storeview_id=magento_storeview.id) + self.binder.bind(binding.external_id, binding.id) + return _('Prices have been updated.') + + +class MagentoProductProduct(models.Model): + _inherit = 'magento.product.product' + + @job(default_channel='root.magento') + @related_action(action='related_action_unwrap_binding') + @api.multi + def export_product_price(self, website_id=None): + """ Export the price of a product. """ + self.ensure_one() + with self.backend_id.work_on(self._name) as work: + price_exporter = work.component(usage='price.exporter') + return price_exporter._run(self, website_id=website_id) + + +class MagentoProductPriceListener(Component): + _name = 'product.product.listener' + _inherit = 'base.event.listener' + _apply_on = ['product.product'] + + @skip_if(lambda self, record, **kwargs: self.no_connector_export(record)) + def on_product_price_changed(self, record): + """ When a product.product price has been changed """ + for binding in record.magento_bind_ids: + binding.with_delay(priority=5).export_product_price() diff --git a/connector_magento_pricing/models/sale.py b/connector_magento_pricing/models/sale.py new file mode 100644 index 000000000..4a163c45b --- /dev/null +++ b/connector_magento_pricing/models/sale.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Copyright 2013 Camptocamp SA +# Copyright 2018 Akretion + +from odoo.addons.component.core import Component +from odoo.addons.connector.components.mapper import mapping + + +class SaleOrderImportMapper(Component): + _inherit = 'magento.sale.order.mapper' + + @mapping + def pricelist_id(self, record): + """ Assign to the sale order the price list used on + the Magento Website or Backend """ + website_binder = self.binder_for('magento.website') + oe_website_id = website_binder.to_internal(record['website_id']) + website = self.session.browse('magento.website', oe_website_id) + if website.pricelist_id: + pricelist_id = website.pricelist_id.id + else: + pricelist_id = self.backend_record.pricelist_id.id + return {'pricelist_id': pricelist_id} diff --git a/connector_magento_pricing/readme/CONTRIBUTORS.rst b/connector_magento_pricing/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..3b88c836e --- /dev/null +++ b/connector_magento_pricing/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +Guewen Baconnier +Pierrick Brun diff --git a/connector_magento_pricing/readme/DESCRIPTION.rst b/connector_magento_pricing/readme/DESCRIPTION.rst new file mode 100644 index 000000000..f55989b12 --- /dev/null +++ b/connector_magento_pricing/readme/DESCRIPTION.rst @@ -0,0 +1,8 @@ +Extension for **Connector Magento**. + +The prices of the products are managed in Odoo using pricelists and +are pushed to Magento. + +You can push all prices when modifying the pricelist on the backend. + +Each time a price is modified in Odoo, the price is pushed to Magento. diff --git a/connector_magento_pricing/static/description/index.html b/connector_magento_pricing/static/description/index.html new file mode 100644 index 000000000..2fa94a819 --- /dev/null +++ b/connector_magento_pricing/static/description/index.html @@ -0,0 +1,401 @@ + + + + + + +Connector Magento - Pricing + + + +
+

Connector Magento - Pricing

+ + +

Beta License: AGPL-3 OCA/connector-magento Translate me on Weblate Try me on Runbot

+

Extension for Connector Magento.

+

The prices of the products are managed in Odoo using pricelists and +are pushed to Magento.

+

You can push all prices when modifying the pricelist on the backend.

+

Each time a price is modified in Odoo, the price is pushed to Magento.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub 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.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Camptocamp
  • +
  • Akretion
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

This module is part of the OCA/connector-magento project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/magentoerpconnect_pricing/magento_model_view.xml b/connector_magento_pricing/views/magento_model_view.xml similarity index 70% rename from magentoerpconnect_pricing/magento_model_view.xml rename to connector_magento_pricing/views/magento_model_view.xml index 066279cac..d99d98609 100644 --- a/magentoerpconnect_pricing/magento_model_view.xml +++ b/connector_magento_pricing/views/magento_model_view.xml @@ -1,14 +1,14 @@ - + magento.backend.form magento.backend - + - + @@ -16,15 +16,14 @@ magento.website.form magento.website - + - + diff --git a/magentoerpconnect_pricing/__manifest__.py b/magentoerpconnect_pricing/__manifest__.py deleted file mode 100644 index e65e66449..000000000 --- a/magentoerpconnect_pricing/__manifest__.py +++ /dev/null @@ -1,45 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# Author: Guewen Baconnier -# Copyright 2013 Camptocamp SA -# -# 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 . -# -############################################################################## - -{'name': 'Magento Connector - Pricing', - 'version': '2.0.0', - 'category': 'Connector', - 'depends': ['magentoerpconnect', - ], - 'author': "Camptocamp,Odoo Community Association (OCA)", - 'license': 'AGPL-3', - 'website': 'http://www.odoo-magento-connector.com', - 'description': """ -Magento Connector - Pricing -=========================== - -Extension for **Magento Connector**. - -The prices of the products are managed in OpenERP using pricelists and -are pushed to Magento. -""", - 'images': [], - 'demo': [], - 'data': ['magento_model_view.xml', - ], - 'installable': False, - 'application': False, - } diff --git a/magentoerpconnect_pricing/magento_model.py b/magentoerpconnect_pricing/magento_model.py deleted file mode 100644 index 62faa5e50..000000000 --- a/magentoerpconnect_pricing/magento_model.py +++ /dev/null @@ -1,135 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# Author: Guewen Baconnier -# Copyright 2013 Camptocamp SA -# -# 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 . -# -############################################################################## - -from openerp.osv import fields, orm -from openerp.tools.translate import _ -from openerp.addons.connector.session import ConnectorSession -from .product import export_product_price - - -class magento_backend(orm.Model): - _inherit = 'magento.backend' - - def _get_pricelist_id(self, cr, uid, context=None): - data_obj = self.pool.get('ir.model.data') - ref = data_obj.get_object_reference(cr, uid, 'product', 'list0') - if ref: - return ref[1] - return False - - _columns = { - 'pricelist_id': fields.many2one('product.pricelist', - 'Pricelist', - required=True, - domain="[('type', '=', 'sale')]", - help='The price list used to define ' - 'the prices of the products in ' - 'Magento.'), - } - - _defaults = { - 'pricelist_id': _get_pricelist_id, - } - - def onchange_pricelist_id(self, cr, uid, ids, pricelist_id, context=None): - if not ids: # new record - return {} - warning = { - 'title': _('Warning'), - 'message': _('If you change the pricelist of the backend, ' - 'the price of all the products will be updated ' - 'in Magento.') - } - return {'warning': warning} - - def _update_default_prices(self, cr, uid, ids, context=None): - """ Update the default prices of the products linked with - this backend. - - The default prices are linked with the 'Admin' website (id: 0). - """ - website_obj = self.pool.get('magento.website') - website_ids = website_obj.search(cr, uid, - [('backend_id', 'in', ids), - ('magento_id', '=', '0')], - context=context) - website_obj.update_all_prices(cr, uid, website_ids, context=context) - - def write(self, cr, uid, ids, vals, context=None): - if 'pricelist_id' in vals: - self._update_default_prices(cr, uid, ids, context=context) - return super(magento_backend, self).write(cr, uid, ids, - vals, context=context) - - -class magento_website(orm.Model): - _inherit = 'magento.website' - - _columns = { - 'pricelist_id': fields.many2one('product.pricelist', - 'Pricelist', - domain="[('type', '=', 'sale')]", - help='The pricelist used to define ' - 'the prices of the products in ' - 'Magento for this website.\n' - 'Choose a pricelist only if the ' - 'prices are different for this ' - 'website.\n' - 'When empty, the default price ' - 'will be used.'), - } - - def update_all_prices(self, cr, uid, ids, context=None): - """ Update the prices of all the products linked to the - website. """ - if not hasattr(ids, '__iter__'): - ids = [ids] - for website in self.browse(cr, uid, ids, context=context): - session = ConnectorSession(cr, uid, context=context) - if website.magento_id == '0': - # 'Admin' website -> default values - # Update the default prices on all the products. - binding_ids = website.backend_id.product_binding_ids - else: - binding_ids = website.product_binding_ids - for binding in binding_ids: - export_product_price.delay(session, - 'magento.product.product', - binding.id, - website_id=website.id) - return True - - def onchange_pricelist_id(self, cr, uid, ids, pricelist_id, context=None): - if not ids: # new record - return {} - warning = { - 'title': _('Warning'), - 'message': _('If you change the pricelist of the website, ' - 'the price of all the products linked with this ' - 'website will be updated in Magento.') - } - return {'warning': warning} - - def write(self, cr, uid, ids, vals, context=None): - if 'pricelist_id' in vals: - self.update_all_prices(cr, uid, ids, context=context) - return super(magento_website, self).write(cr, uid, ids, - vals, context=context) diff --git a/magentoerpconnect_pricing/product.py b/magentoerpconnect_pricing/product.py deleted file mode 100644 index 6dec905dd..000000000 --- a/magentoerpconnect_pricing/product.py +++ /dev/null @@ -1,156 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# Author: Guewen Baconnier -# Copyright 2013 Camptocamp SA -# -# 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 . -# -############################################################################## - - -from openerp.tools.translate import _ -from openerp.addons.connector.queue.job import job, related_action -from openerp.addons.connector.exception import FailedJobError -from openerp.addons.connector.unit.mapper import (mapping, - only_create - ) -from openerp.addons.connector_ecommerce.event import on_product_price_changed -from openerp.addons.magentoerpconnect.unit.export_synchronizer import ( - MagentoBaseExporter) -from openerp.addons.magentoerpconnect.backend import magento -from openerp.addons.magentoerpconnect import product -from openerp.addons.magentoerpconnect.connector import get_environment -from openerp.addons.magentoerpconnect.related_action import ( - unwrap_binding, -) - - -# TODO: replace a price mapper only, not the full mapper -@magento(replacing=product.ProductImportMapper) -class ProductImportMapper(product.ProductImportMapper): - _model_name = 'magento.product.product' - - @only_create - @mapping - def price(self, record): - """ The price is imported at the creation of - the product, then it is only modified and exported - from OpenERP """ - return super(ProductImportMapper, self).price(record) - - -@magento -class ProductPriceExporter(MagentoBaseExporter): - """ Export the price of a product. - - Use the pricelist configured on the backend for the - default price in Magento. - If different pricelists have been configured on the websites, - update the prices on the different websites. - """ - _model_name = ['magento.product.product'] - - def _get_price(self, pricelist_id): - """ Return the raw OpenERP data for ``self.binding_id`` """ - if pricelist_id is None: - # a False value will set the 'Use default value' in Magento - return False - with self.session.change_context({'pricelist': pricelist_id}): - return self.session.read(self.model._name, - self.binding_id, - ['price'])['price'] - - def _update(self, data, storeview_id=None): - self.backend_adapter.write(self.magento_id, data, - storeview_id=storeview_id) - - def _run(self, website_id=None): - """ Export the product inventory to Magento - - :param website_id: if None, export on all websites, - or OpenERP ID for the website to update - """ - # export of products is not implemented so we just raise - # if the export was existing, we would export it - assert self.magento_id, "Record has been deleted in Magento" - pricelist = self.backend_record.pricelist_id - if not pricelist: - name = self.backend_record.name - raise FailedJobError( - 'Configuration Error:\n' - 'No pricelist configured on the backend %s.\n\n' - 'Resolution:\n' - 'Go to Connectors > Backends > %s.\n' - 'Choose a pricelist.' % (name, name)) - pricelist_id = pricelist.id - - # export the price for websites if they have a different - # pricelist - storeview_binder = self.binder_for('magento.storeview') - for website in self.backend_record.website_ids: - if website_id is not None and website.id != website_id: - continue - # 0 is the admin website, the update on this website - # set the default values in Magento, we use the default - # pricelist - site_pricelist_id = None - if website.magento_id == '0': - site_pricelist_id = pricelist_id - elif website.pricelist_id: - site_pricelist_id = website.pricelist_id.id - - # The update of the prices in Magento is very weird: - # - The price is different per website (if the option - # is active in the config), but is shared between - # the store views of a website. - # - BUT the Magento API expects a storeview id to modify - # a price on a website (and not a website id...) - # So we take the first storeview of the website to update. - storeview_ids = self.session.search( - 'magento.storeview', - [('store_id.website_id', '=', website.id)]) - if not storeview_ids: - continue - magento_storeview = storeview_binder.to_backend(storeview_ids[0]) - price = self._get_price(site_pricelist_id) - self._update({'price': price}, storeview_id=magento_storeview) - self.binder.bind(self.magento_id, self.binding_id) - return _('Prices have been updated.') - - -@on_product_price_changed -def product_price_changed(session, model_name, record_id, fields=None): - """ When a product.product price has been changed """ - if session.context.get('connector_no_export'): - return - model = session.pool.get(model_name) - record = model.browse(session.cr, session.uid, - record_id, context=session.context) - for binding in record.magento_bind_ids: - export_product_price.delay(session, - binding._model._name, - binding.id, - priority=5) - - -@job -@related_action(action=unwrap_binding) -def export_product_price(session, model_name, record_id, website_id=None): - """ Export the price of a product. """ - product_bind = session.browse(model_name, record_id) - backend_id = product_bind.backend_id.id - env = get_environment(session, model_name, backend_id) - price_exporter = env.get_connector_unit(ProductPriceExporter) - return price_exporter.run(record_id, website_id=website_id) diff --git a/magentoerpconnect_pricing/sale.py b/magentoerpconnect_pricing/sale.py deleted file mode 100644 index 2624a5c3a..000000000 --- a/magentoerpconnect_pricing/sale.py +++ /dev/null @@ -1,42 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# Author: Guewen Baconnier -# Copyright 2013 Camptocamp SA -# -# 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 . -# -############################################################################## - -from openerp.addons.magentoerpconnect.backend import magento -from openerp.addons.magentoerpconnect import sale -from openerp.addons.connector.unit.mapper import mapping - - -@magento(replacing=sale.SaleOrderImportMapper) -class SaleOrderImportMapper(sale.SaleOrderImportMapper): - _model_name = 'magento.sale.order' - - @mapping - def pricelist_id(self, record): - """ Assign to the sale order the price list used on - the Magento Website or Backend """ - website_binder = self.binder_for('magento.website') - oe_website_id = website_binder.to_openerp(record['website_id']) - website = self.session.browse('magento.website', oe_website_id) - if website.pricelist_id: - pricelist_id = website.pricelist_id.id - else: - pricelist_id = self.backend_record.pricelist_id.id - return {'pricelist_id': pricelist_id}