← Back to team overview

savoirfairelinux-openerp team mailing list archive

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

 

Sébastien BEAU - http://www.akretion.com has proposed merging lp:~sebastien.beau/e-commerce-addons/oerp6.1-stable-improve-stock-export into lp:e-commerce-addons/oerp6.1-stable.

Requested reviews:
  extra-addons-commiter (extra-addons-commiter)

For more details, see:
https://code.launchpad.net/~sebastien.beau/e-commerce-addons/oerp6.1-stable-improve-stock-export/+merge/168290
-- 
https://code.launchpad.net/~sebastien.beau/e-commerce-addons/oerp6.1-stable-improve-stock-export/+merge/168290
Your team extra-addons-commiter is requested to review the proposed merge of lp:~sebastien.beau/e-commerce-addons/oerp6.1-stable-improve-stock-export into lp:e-commerce-addons/oerp6.1-stable.
=== 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:13:37 +0000
@@ -36,6 +36,7 @@
 from base_external_referentials.external_osv import ExternalSession
 from base_external_referentials.decorator import open_report
 from base_external_referentials.decorator import catch_error_in_report
+from framework_helpers.context_managers import new_cursor, commit_now
 
 #TODO use external_session.logger when it's posible
 import logging
@@ -279,46 +280,74 @@
             self._export_inventory(cr, uid, external_session, ids, context=context)
         return True
 
-    def _get_product_ids_for_stock_to_export(self, cr, uid, shop, context=None):
-        return [product.id for product in shop.exportable_product_ids]
+    def _get_product_stock_ids_and_update_date(self, cr, uid, external_session,
+        product_ids, last_exported_date=None, context=None):
+        """This function will return the list of ids and the update date of each
+            product that should be updated in the external system
+
+        :param ExternalSession external_session : External_session that contain
+            all params of connection
+        :param str last_exported_date : last exported date
+        :rtype: tuple
+        :return: an tuple of ids and ids_2_dates
+            (dict with key => 'id' and val => 'last_update_date')
+        """
+        move_obj = self.pool.get('stock.move')
+
+        domain =  [
+            ('product_id', 'in', product_ids),
+            ('product_id.type', '!=', 'service'),
+            ('state', '!=', 'draft'),
+            ]
+
+        if last_exported_date:
+            domain += [('write_date', '>=', last_exported_date)]
+
+        move_ids = move_obj.search(cr, uid, domain, context=context)
+
+        product_ids_to_date = {}
+        product_ids = []
+        for move in move_obj.browse(cr, uid, move_ids, context=context):
+            if product_ids_to_date.get(move.product_id.id) <= move.write_date:
+                product_ids_to_date[move.product_id.id] = move.write_date
+
+        #Now we build an ordered list (by date) of product to update
+        product_ids = product_ids_to_date.keys()
+        date_to_product = []
+        for product_id in product_ids_to_date:
+            date_to_product.append((product_ids_to_date[product_id], product_id))
+        date_to_product.sort()
+        product_ids = [product_id for date, product_id in date_to_product]
+        return product_ids, product_ids_to_date
+
 
     def _export_inventory(self, cr, uid, external_session, ids, context=None):
+        shop_obj = self.pool.get('sale.shop')
         shop = external_session.sync_from_object
-        stock_move_obj = self.pool.get('stock.move')
         for shop in self.browse(cr, uid, ids):
+            start_time = time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
             external_session = ExternalSession(shop.referential_id, shop)
-
-            product_ids = self._get_product_ids_for_stock_to_export(cr, uid, shop, context=context)
-
-            if shop.last_inventory_export_date:
-                # we do not exclude canceled moves because it means
-                # some stock levels could have increased since last export
-                recent_move_ids = stock_move_obj.search(
-                    cr, uid,
-                    [('write_date', '>', shop.last_inventory_export_date),
-                     ('product_id', 'in', product_ids),
-                     ('product_id.type', '!=', 'service'),
-                     ('state', '!=', 'draft')],
-                    context=context)
-            else:
-                recent_move_ids = stock_move_obj.search(
-                    cr, uid,
-                    [('product_id', 'in', product_ids)],
-                    context=context)
-
-            recent_moves = stock_move_obj.browse(
-                cr, uid, recent_move_ids, context=context)
-
-            product_ids = [move.product_id.id
-                           for move
-                           in recent_moves
-                           if move.product_id.state != 'obsolete']
-            product_ids = list(set(product_ids))
+            product_ids = [product.id for product in shop.exportable_product_ids]
+            product_ids, product_ids_to_date = \
+                self._get_product_stock_ids_and_update_date(cr, uid, external_session,
+                        product_ids = product_ids,
+                        last_exported_date = shop.last_inventory_export_date,
+                        context=context)
             external_session.logger.info('Export Stock for %s products' %len(product_ids))
-            self.pool.get('product.product').export_inventory(
-                    cr, uid, external_session, product_ids, context=context)
-            shop.write({'last_inventory_export_date':
-                            time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)})
+            for product_id in product_ids:
+                self.pool.get('product.product').export_inventory(
+                    cr, uid, external_session, [product_id], context=context)
+                with new_cursor(cr, external_session.logger) as new_cr:
+                    with commit_now(new_cr, external_session.logger) as new_cr:
+                        shop_obj.write(new_cr, uid, shop.id, {
+                            'last_inventory_export_date': product_ids_to_date[product_id],
+                            })
+            #Update date when finish the process
+            with new_cursor(cr, external_session.logger) as new_cr:
+                with commit_now(new_cr, external_session.logger) as new_cr:
+                    shop_obj.write(new_cr, uid, shop.id, {
+                        'last_inventory_export_date': start_time,
+                        })
         return True
 
     def import_catalog(self, cr, uid, ids, context):
@@ -410,7 +439,8 @@
               AND stock_picking.type = 'out'
               AND NOT stock_picking.do_not_export
               AND (NOT delivery_carrier.export_needs_tracking
-                   OR stock_picking.carrier_tracking_ref IS NOT NULL)
+                   OR stock_picking.carrier_tracking_ref IS NOT NULL
+                   OR delivery_carrier IS NULL)
         GROUP BY stock_picking.id,
                  sale_order.id,
                  delivery_carrier.export_needs_tracking,

=== 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:13:37 +0000
@@ -44,3 +44,11 @@
                                                             inv_type, journal_id, context=context)
         vals['shop_id'] = picking.shop_id.id
         return vals
+
+
+class stock_move(Model):
+    _inherit="stock.move"
+
+    _columns = {
+        'write_date': fields.datetime('Write Date'),
+    }


Follow ups