forked from OCA/connector-magento
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magento_model.py
135 lines (119 loc) · 5.74 KB
/
magento_model.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
# -*- 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.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)