forked from OCA/connector-magento
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.py
156 lines (138 loc) · 6.61 KB
/
product.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
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)