1
# -*- encoding: utf-8 -*-
2
###############################################################################
4
# product_links_sync for OpenERP #
5
# Copyright (C) 2012 Akretion Sébastien BEAU <sebastien.beau@akretion.com> #
7
# This program is free software: you can redistribute it and/or modify #
8
# it under the terms of the GNU Affero General Public License as #
9
# published by the Free Software Foundation, either version 3 of the #
10
# License, or (at your option) any later version. #
12
# This program is distributed in the hope that it will be useful, #
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15
# GNU Affero General Public License for more details. #
17
# You should have received a copy of the GNU Affero General Public License #
18
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
20
###############################################################################
22
from osv import osv, fields
24
from tools import DEFAULT_SERVER_DATETIME_FORMAT
25
from datetime import datetime
26
from base_external_referentials.decorator import only_for_referential
29
class product_product(osv.osv):
30
_inherit = "product.product"
33
'product_link_write_last_date': fields.datetime('Product Link Last Write Date'),
36
def _update_product_link_last_date(self, cr, uid, vals, context=None):
37
if 'product_link_ids' in vals:
38
vals['product_link_write_last_date'] = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
41
def create(self, cr, uid, vals, context=None):
42
vals = self._update_product_link_last_date(cr, uid, vals, context=context)
43
return super(product_product, self).create(cr, uid, vals, context=context)
45
def write(self, cr, uid, ids, vals, context=None):
46
vals = self._update_product_link_last_date(cr, uid, vals, context=context)
47
return super(product_product, self).write(cr, uid, ids, vals, context=context)
49
def _get_query_and_params_for_ids_and_date(self, cr, uid, external_session, ids=None, last_exported_date=None, context=None):
50
if context.get('export_product') != 'link':
51
return super(product_product, self)._get_query_and_params_for_ids_and_date(cr, uid,
52
external_session, ids=ids, last_exported_date=last_exported_date, context=context)
54
# We have to export all product link that believe to a product which have the link modify
55
# or have been created in the external system since the last export
58
SELECT GREATEST(product_link_write_last_date, ir_model_data.create_date) as update_date,
59
product_product.id as id, ir_model_data.res_id, product_link_write_last_date
61
LEFT JOIN ir_model_data
62
ON product_product.id = ir_model_data.res_id
63
AND ir_model_data.model = 'product.product'
64
AND ir_model_data.module = 'extref/%(ref_name)s'
65
WHERE product_link_write_last_date is not NULL
67
'ref_name': external_session.referential_id.name,
70
query += " AND product_product.id in %s"
71
params += (tuple(ids),)
72
if last_exported_date:
73
query += " AND (GREATEST(product_link_write_last_date,ir_model_data.create_date) > %s)"
74
params += (last_exported_date,)
78
def get_field_to_export(self, cr, uid, ids, mapping, mapping_id, context=None):
79
fields_to_read = super(product_product, self).get_field_to_export(cr, uid, ids, mapping, mapping_id, context=context)
80
if context.get('export_product') == 'link':
81
fields_to_read = ['product_link_ids']
83
if 'product_link_ids' in fields_to_read: fields_to_read.remove('product_link_ids')
86
# def export_links_for_product(self, cr, uid, ids, context=None):
87
# """ Not implemented in this abstract module"""
90
@only_for_referential(ref_categ ='Multichannel Sale')
91
def _get_last_exported_date(self, cr, uid, external_session, context):
92
shop = external_session.sync_from_object
93
if context.get('export_product') == 'link':
94
return shop.last_products_links_export_date
96
return super(product_product, self)._get_last_exported_date(cr, uid, external_session, context)
98
@only_for_referential(ref_categ ='Multichannel Sale')
99
def _set_last_exported_date(self, cr, uid, external_session, date, context):
100
shop = external_session.sync_from_object
101
if context.get('export_product') == 'link':
102
return self.pool.get('sale.shop').write(cr, uid, shop.id, {'last_products_links_export_date': date}, context=context)
104
return super(product_product, self)._set_last_exported_date(cr, uid, external_session, date, context)