← Back to team overview

openerp-community-reviewer team mailing list archive

[Merge] lp:~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_dispatch-output-file-yvr into lp:carriers-deliveries

 

Yannick Vaucher @ Camptocamp has proposed merging lp:~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_dispatch-output-file-yvr into lp:carriers-deliveries with lp:~camptocamp/carriers-deliveries/7.0-add-delivery_carrier_label_dispatch-yvr as a prerequisite.

Requested reviews:
  Leonardo Pistone - camptocamp (lpistone): code review
  Stock and Logistic Core Editors (stock-logistic-core-editors)

For more details, see:
https://code.launchpad.net/~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_dispatch-output-file-yvr/+merge/202444
-- 
https://code.launchpad.net/~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_dispatch-output-file-yvr/+merge/202444
Your team Stock and Logistic Core Editors is requested to review the proposed merge of lp:~camptocamp/carriers-deliveries/7.0-delivery_carrier_label_dispatch-output-file-yvr into lp:carriers-deliveries.
=== modified file 'delivery_carrier_label_dispatch/__init__.py'
--- delivery_carrier_label_dispatch/__init__.py	2014-01-09 13:54:55 +0000
+++ delivery_carrier_label_dispatch/__init__.py	2014-03-28 10:38:27 +0000
@@ -18,6 +18,5 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
-from . import stock
 from . import picking_dispatch
 from . import wizard

=== modified file 'delivery_carrier_label_dispatch/__openerp__.py'
--- delivery_carrier_label_dispatch/__openerp__.py	2014-01-09 13:54:55 +0000
+++ delivery_carrier_label_dispatch/__openerp__.py	2014-03-28 10:38:27 +0000
@@ -29,8 +29,13 @@
 [Link module] Carrier labels - Picking dispatch
 ==============================
 
-This module adds a wizard on picking dispatch to generate related picking
-labels
+This module adds a wizard on picking dispatch to generate the labels
+of the packs. The labels are merged in one PDF file.
+
+If you want multiple labels for one picking, all the moves should have been
+put in a pack before the labels can be printed.
+
+If you don't define your pack it will be considered a picking is a single pack.
 
 Contributors
 ------------

=== modified file 'delivery_carrier_label_dispatch/pdf_utils.py'
--- delivery_carrier_label_dispatch/pdf_utils.py	2013-12-17 07:55:56 +0000
+++ delivery_carrier_label_dispatch/pdf_utils.py	2014-03-28 10:38:27 +0000
@@ -39,6 +39,8 @@
 
     output = PdfFileWriter()
     for pdf in pdf_list:
+        if not pdf:
+            continue
         reader = PdfFileReader(StringIO(pdf))
 
         for page in range(reader.getNumPages()):

=== removed file 'delivery_carrier_label_dispatch/stock.py'
--- delivery_carrier_label_dispatch/stock.py	2013-12-17 07:42:04 +0000
+++ delivery_carrier_label_dispatch/stock.py	1970-01-01 00:00:00 +0000
@@ -1,62 +0,0 @@
-# -*- coding: utf-8 -*-
-##############################################################################
-#
-#    Author: Yannick Vaucher
-#    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 pdf_utils import assemble_pdf
-from openerp.osv import orm
-
-
-class stock_picking(orm.Model):
-    _inherit = 'stock.picking'
-
-    def get_pdf_label(self, cr, uid, ids, context=None):
-        """ Return a single pdf of labels for a stock picking
-
-        If more than one label is found for a picking we merge one label per
-        tracking in a single pdf
-
-        :return: a list of pdf file data
-        """
-        res = dict.fromkeys(ids, False)
-        label_obj = self.pool.get('shipping.label')
-        for picking_id in ids:
-            label_ids = label_obj.search(cr, uid,
-                                         [('file_type', '=', 'pdf'),
-                                          ('res_id', '=', picking_id)],
-                                         order='create_date',
-                                         context=context)
-            if label_ids:
-                all_picking_labels = label_obj.browse(cr, uid,
-                                                      label_ids,
-                                                      context=context)
-
-                tracking_ids = [l.tracking_id for l in all_picking_labels]
-
-                # filter for newest created label for each trackings
-                label_datas = []
-                tracking_ids = set(tracking_ids)
-                while tracking_ids:
-                    tracking_id = tracking_ids.pop()
-                    for label in all_picking_labels:
-                        if label.tracking_id.id == tracking_id.id:
-                            label_datas.append(label.datas.decode('base64'))
-
-                label_pdf = assemble_pdf(label_datas)
-                res[picking_id] = label_pdf.encode('base64')
-        return res

=== modified file 'delivery_carrier_label_dispatch/wizard/generate_labels.py'
--- delivery_carrier_label_dispatch/wizard/generate_labels.py	2013-12-17 07:42:04 +0000
+++ delivery_carrier_label_dispatch/wizard/generate_labels.py	2014-03-28 10:38:27 +0000
@@ -18,11 +18,14 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
+from operator import attrgetter
+from itertools import groupby
+
+from openerp.osv import orm, fields
+from openerp.tools.translate import _
+
 from ..pdf_utils import assemble_pdf
 
-from openerp.osv import orm, fields
-from tools.translate import _
-
 
 class DeliveryCarrierLabelGenerate(orm.TransientModel):
 
@@ -40,52 +43,95 @@
     _columns = {
         'dispatch_ids': fields.many2many('picking.dispatch',
                                          string='Picking Dispatch'),
-        'label_pdf_file': fields.binary('Labels file'),
+        'generate_new_labels': fields.boolean(
+            'Generate new labels',
+            help="If this option is used, new labels will be "
+                 "generated for the packs even if they already have one.\n"
+                 "The default is to use the existing label."),
     }
 
     _defaults = {
         'dispatch_ids': _get_dispatch_ids,
+        'generate_new_labels': False,
     }
 
+    def _get_packs(self, cr, uid, wizard, dispatch, context=None):
+        moves = sorted(dispatch.move_ids, key=attrgetter('tracking_id.name'))
+        for pack, moves in groupby(moves, key=attrgetter('tracking_id')):
+            pack_label = self._find_pack_label(cr, uid, wizard, pack,
+                                               context=context)
+            yield pack, list(moves), pack_label
+
+    def _find_picking_label(self, cr, uid, wizard, picking, context=None):
+        label_obj = self.pool['shipping.label']
+        domain = [('file_type', '=', 'pdf'),
+                  ('res_id', '=', picking.id),
+                  ('tracking_id', '=', False),
+                  ]
+        label_id = label_obj.search(cr, uid, domain, order='create_date DESC',
+                                    limit=1, context=context)
+        if not label_id:
+            return None
+        return label_obj.browse(cr, uid, label_id[0], context=context)
+
+    def _find_pack_label(self, cr, uid, wizard, pack, context=None):
+        label_obj = self.pool['shipping.label']
+        domain = [('file_type', '=', 'pdf'),
+                  ('tracking_id', '=', pack.id),
+                  ]
+        label_id = label_obj.search(cr, uid, domain, order='create_date DESC',
+                                    limit=1, context=context)
+        if not label_id:
+            return None
+        return label_obj.browse(cr, uid, label_id[0], context=context)
+
+    def _get_all_pdf(self, cr, uid, wizard, dispatch, context=None):
+        for pack, moves, label in self._get_packs(cr, uid, wizard, dispatch,
+                                                  context=context):
+            if not label or wizard.generate_new_labels:
+                picking_out_obj = self.pool['stock.picking.out']
+                picking = moves[0].picking_id
+                # generate the label of the pack
+                picking_out_obj.generate_labels(
+                    cr, uid, [picking.id],
+                    tracking_ids=[pack.id],
+                    context=context)
+                if pack:
+                    label = self._find_pack_label(cr, uid, wizard, pack,
+                                                  context=context)
+                else:
+                    label = self._find_picking_label(cr, uid, wizard, picking,
+                                                     context=context)
+                if not label:
+                    continue  # no label could be generated
+            yield label
+
     def action_generate_labels(self, cr, uid, ids, context=None):
         """
         Call the creation of the delivery carrier label
         of the missing labels and get the existing ones
         Then merge all of them in a single PDF
+
         """
         this = self.browse(cr, uid, ids, context=context)[0]
         if not this.dispatch_ids:
             raise orm.except_orm(_('Error'), _('No picking dispatch selected'))
 
-        picking_out_obj = self.pool.get('stock.picking.out')
-
-        # flatten all picking in one list to keep the order in case
-        # there are multiple dispatch or if pickings
-        # have been ordered to ease packaging
-        pickings = [(pick, pick.get_pdf_label()[pick.id])
-                    for dispatch in this.dispatch_ids
-                    for pick in dispatch.related_picking_ids]
-        # get picking ids for which we want to generate pdf label
-        picking_ids = [pick.id for pick, pdf in pickings
-                       if not pdf]
-        # generate missing picking labels
-        picking_out_obj.action_generate_carrier_label(cr, uid,
-                                                      picking_ids,
-                                                      #file_type='pdf',
-                                                      context=context)
-
-        # Get all pdf files adding the newly generated ones
-        data_list = [pdf or pick.get_pdf_label()[pick.id]
-                     for pick, pdf in pickings]
-        pdf_list = [data.decode('base64') for data in data_list if data]
-        pdf_file = assemble_pdf(pdf_list)
-        this.write({'label_pdf_file': pdf_file.encode('base64')})
+        attachment_obj = self.pool.get('ir.attachment')
+
+        for dispatch in this.dispatch_ids:
+            labels = self._get_all_pdf(cr, uid, this, dispatch,
+                                       context=context)
+            labels = (label.datas for label in labels)
+            labels = (label.decode('base64') for label in labels if labels)
+            data = {
+                'name': dispatch.name + '.pdf',
+                'res_id': dispatch.id,
+                'res_model': 'picking.dispatch',
+                'datas': assemble_pdf(labels).encode('base64'),
+            }
+            attachment_obj.create(cr, uid, data, context=context)
+
         return {
-            'type': 'ir.actions.act_window',
-            'res_model': 'delivery.carrier.label.generate',
-            'view_mode': 'form',
-            'view_type': 'form',
-            'res_id': this.id,
-            'views': [(False, 'form')],
-            'target': 'new',
+            'type': 'ir.actions.act_window_close',
         }

=== modified file 'delivery_carrier_label_dispatch/wizard/generate_labels_view.xml'
--- delivery_carrier_label_dispatch/wizard/generate_labels_view.xml	2013-12-06 12:51:58 +0000
+++ delivery_carrier_label_dispatch/wizard/generate_labels_view.xml	2014-03-28 10:38:27 +0000
@@ -7,13 +7,12 @@
       <field name="model">delivery.carrier.label.generate</field>
       <field name="arch" type="xml">
         <form string="Generate Carriers Labels" version="7.0">
-
+          <group>
+            <label string="This wizard creates an attachement on each selected dispatch containing picking labels"/>
+          </group>
           <group>
             <field name="dispatch_ids"/>
-          </group>
-          <separator string="Labels"/>
-          <group>
-            <field name="label_pdf_file" filename="picking_labels.pdf"/>
+            <field name="generate_new_labels"/>
           </group>
           <footer>
             <button name="action_generate_labels" string="Generate Labels" type="object" icon="gtk-execute" class="oe_highlight"/>


References