Merge lp:~sebastien.beau/e-commerce-addons/oerp6.1-stable-improve-stock-export into lp:~extra-addons-commiter/e-commerce-addons/oerp6.1-stable

Proposed by Sébastien BEAU - http://www.akretion.com
Status: Work in progress
Proposed branch: lp:~sebastien.beau/e-commerce-addons/oerp6.1-stable-improve-stock-export
Merge into: lp:~extra-addons-commiter/e-commerce-addons/oerp6.1-stable
Diff against target: 136 lines (+71/-34)
2 files modified
base_sale_multichannels/sale.py (+63/-34)
base_sale_multichannels/stock.py (+8/-0)
To merge this branch: bzr merge lp:~sebastien.beau/e-commerce-addons/oerp6.1-stable-improve-stock-export
Reviewer Review Type Date Requested Status
Maxime Chambreuil (http://www.savoirfairelinux.com) code review Needs Fixing
Guewen Baconnier @ Camptocamp Approve
Review via email: mp+168290@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Sébastien BEAU - http://www.akretion.com (sebastien.beau) wrote :

Improve the stock export.
Export in chronologic order in order to update the last export date after each export. This is a life saver when you need to export a lot of product in one time. Indeed before if the export failed, you had to start again from the beginning.

Thanks

Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) :
review: Approve
Revision history for this message
Maxime Chambreuil (http://www.savoirfairelinux.com) (max3903) wrote :

Thanks Sébastien.

Please update the translation file as you add a new field l135.

review: Needs Fixing (code review)

Unmerged revisions

286. By Sébastien BEAU - http://www.akretion.com

[REF] refactor the stock export, export the product with chronologic order and commit the last export date after each export

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'base_sale_multichannels/sale.py'
--- base_sale_multichannels/sale.py 2013-06-08 20:04:37 +0000
+++ base_sale_multichannels/sale.py 2013-06-09 15:19:31 +0000
@@ -36,6 +36,7 @@
36from base_external_referentials.external_osv import ExternalSession36from base_external_referentials.external_osv import ExternalSession
37from base_external_referentials.decorator import open_report37from base_external_referentials.decorator import open_report
38from base_external_referentials.decorator import catch_error_in_report38from base_external_referentials.decorator import catch_error_in_report
39from framework_helpers.context_managers import new_cursor, commit_now
3940
40#TODO use external_session.logger when it's posible41#TODO use external_session.logger when it's posible
41import logging42import logging
@@ -279,46 +280,74 @@
279 self._export_inventory(cr, uid, external_session, ids, context=context)280 self._export_inventory(cr, uid, external_session, ids, context=context)
280 return True281 return True
281282
282 def _get_product_ids_for_stock_to_export(self, cr, uid, shop, context=None):283 def _get_product_stock_ids_and_update_date(self, cr, uid, external_session,
283 return [product.id for product in shop.exportable_product_ids]284 product_ids, last_exported_date=None, context=None):
285 """This function will return the list of ids and the update date of each
286 product that should be updated in the external system
287
288 :param ExternalSession external_session : External_session that contain
289 all params of connection
290 :param str last_exported_date : last exported date
291 :rtype: tuple
292 :return: an tuple of ids and ids_2_dates
293 (dict with key => 'id' and val => 'last_update_date')
294 """
295 move_obj = self.pool.get('stock.move')
296
297 domain = [
298 ('product_id', 'in', product_ids),
299 ('product_id.type', '!=', 'service'),
300 ('state', '!=', 'draft'),
301 ]
302
303 if last_exported_date:
304 domain += [('write_date', '>=', last_exported_date)]
305
306 move_ids = move_obj.search(cr, uid, domain, context=context)
307
308 product_ids_to_date = {}
309 product_ids = []
310 for move in move_obj.browse(cr, uid, move_ids, context=context):
311 if product_ids_to_date.get(move.product_id.id) <= move.write_date:
312 product_ids_to_date[move.product_id.id] = move.write_date
313
314 #Now we build an ordered list (by date) of product to update
315 product_ids = product_ids_to_date.keys()
316 date_to_product = []
317 for product_id in product_ids_to_date:
318 date_to_product.append((product_ids_to_date[product_id], product_id))
319 date_to_product.sort()
320 product_ids = [product_id for date, product_id in date_to_product]
321 return product_ids, product_ids_to_date
322
284323
285 def _export_inventory(self, cr, uid, external_session, ids, context=None):324 def _export_inventory(self, cr, uid, external_session, ids, context=None):
325 shop_obj = self.pool.get('sale.shop')
286 shop = external_session.sync_from_object326 shop = external_session.sync_from_object
287 stock_move_obj = self.pool.get('stock.move')
288 for shop in self.browse(cr, uid, ids):327 for shop in self.browse(cr, uid, ids):
328 start_time = time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
289 external_session = ExternalSession(shop.referential_id, shop)329 external_session = ExternalSession(shop.referential_id, shop)
290330 product_ids = [product.id for product in shop.exportable_product_ids]
291 product_ids = self._get_product_ids_for_stock_to_export(cr, uid, shop, context=context)331 product_ids, product_ids_to_date = \
292332 self._get_product_stock_ids_and_update_date(cr, uid, external_session,
293 if shop.last_inventory_export_date:333 product_ids = product_ids,
294 # we do not exclude canceled moves because it means334 last_exported_date = shop.last_inventory_export_date,
295 # some stock levels could have increased since last export335 context=context)
296 recent_move_ids = stock_move_obj.search(
297 cr, uid,
298 [('write_date', '>', shop.last_inventory_export_date),
299 ('product_id', 'in', product_ids),
300 ('product_id.type', '!=', 'service'),
301 ('state', '!=', 'draft')],
302 context=context)
303 else:
304 recent_move_ids = stock_move_obj.search(
305 cr, uid,
306 [('product_id', 'in', product_ids)],
307 context=context)
308
309 recent_moves = stock_move_obj.browse(
310 cr, uid, recent_move_ids, context=context)
311
312 product_ids = [move.product_id.id
313 for move
314 in recent_moves
315 if move.product_id.state != 'obsolete']
316 product_ids = list(set(product_ids))
317 external_session.logger.info('Export Stock for %s products' %len(product_ids))336 external_session.logger.info('Export Stock for %s products' %len(product_ids))
318 self.pool.get('product.product').export_inventory(337 for product_id in product_ids:
319 cr, uid, external_session, product_ids, context=context)338 self.pool.get('product.product').export_inventory(
320 shop.write({'last_inventory_export_date':339 cr, uid, external_session, [product_id], context=context)
321 time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)})340 with new_cursor(cr, external_session.logger) as new_cr:
341 with commit_now(new_cr, external_session.logger) as new_cr:
342 shop_obj.write(new_cr, uid, shop.id, {
343 'last_inventory_export_date': product_ids_to_date[product_id],
344 })
345 #Update date when finish the process
346 with new_cursor(cr, external_session.logger) as new_cr:
347 with commit_now(new_cr, external_session.logger) as new_cr:
348 shop_obj.write(new_cr, uid, shop.id, {
349 'last_inventory_export_date': start_time,
350 })
322 return True351 return True
323352
324 def import_catalog(self, cr, uid, ids, context):353 def import_catalog(self, cr, uid, ids, context):
325354
=== modified file 'base_sale_multichannels/stock.py'
--- base_sale_multichannels/stock.py 2012-08-21 13:57:44 +0000
+++ base_sale_multichannels/stock.py 2013-06-09 15:19:31 +0000
@@ -44,3 +44,11 @@
44 inv_type, journal_id, context=context)44 inv_type, journal_id, context=context)
45 vals['shop_id'] = picking.shop_id.id45 vals['shop_id'] = picking.shop_id.id
46 return vals46 return vals
47
48
49class stock_move(Model):
50 _inherit="stock.move"
51
52 _columns = {
53 'write_date': fields.datetime('Write Date'),
54 }