From c030d20c68df5dfda95374f3035948f4fd77500a Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Fri, 18 Dec 2015 18:45:26 +0100 Subject: [PATCH] Add module delivery_carrier_zpl_label_print --- delivery_carrier_zpl_label_print/__init__.py | 3 + .../__openerp__.py | 46 ++++++ delivery_carrier_zpl_label_print/stock.py | 137 ++++++++++++++++++ .../stock_view.xml | 29 ++++ .../users_view.xml | 22 +++ 5 files changed, 237 insertions(+) create mode 100644 delivery_carrier_zpl_label_print/__init__.py create mode 100644 delivery_carrier_zpl_label_print/__openerp__.py create mode 100644 delivery_carrier_zpl_label_print/stock.py create mode 100644 delivery_carrier_zpl_label_print/stock_view.xml create mode 100644 delivery_carrier_zpl_label_print/users_view.xml diff --git a/delivery_carrier_zpl_label_print/__init__.py b/delivery_carrier_zpl_label_print/__init__.py new file mode 100644 index 0000000..b857415 --- /dev/null +++ b/delivery_carrier_zpl_label_print/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import stock diff --git a/delivery_carrier_zpl_label_print/__openerp__.py b/delivery_carrier_zpl_label_print/__openerp__.py new file mode 100644 index 0000000..2b2e143 --- /dev/null +++ b/delivery_carrier_zpl_label_print/__openerp__.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Delivery Carrier ZPL Label Print module for Odoo +# Copyright (C) 2015 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# +# 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 . +# +############################################################################## + + +{ + 'name': 'Delivery Carrier ZPL Label Print', + 'version': '0.1', + 'category': 'Inventory, Logistic, Storage', + 'license': 'AGPL-3', + 'summary': 'Print ZPL label from delivery order', + 'description': """ +Delivery Carrier ZPL Label Print +================================ + +Add a *Print Label* on Delivery Order that gets the ZPL attached to the picking and sends it to the printer via CUPS. + +Please contact Alexis de Lattre from Akretion for any help or question about this module. + """, + 'author': 'Akretion', + 'website': 'http://www.akretion.com', + 'depends': ['base_delivery_carrier_label', 'base_report_to_printer'], + 'data': [ + 'stock_view.xml', + 'users_view.xml', + ], + 'installable': True, +} diff --git a/delivery_carrier_zpl_label_print/stock.py b/delivery_carrier_zpl_label_print/stock.py new file mode 100644 index 0000000..0cf77b5 --- /dev/null +++ b/delivery_carrier_zpl_label_print/stock.py @@ -0,0 +1,137 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Delivery Carrier ZPL Label Print module for Odoo +# Copyright (C) 2015 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# +# 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 . +# +############################################################################## + +from openerp.osv import orm, fields +from openerp.tools.translate import _ +import logging +import base64 + +logger = logging.getLogger(__name__) + + +class ResUsers(orm.Model): + _inherit = 'res.users' + + _columns = { + 'label_printer_id': fields.many2one('printing.printer', + 'Default Label Printer'), + } + + + +class StockPicking(orm.Model): + _inherit = 'stock.picking' + + def _compute_delivery_labels(self, cr, uid, ids, name, arg, context=None): + res = {} + for picking in self.browse(cr, uid, ids, context=context): + label_ids = self.pool['shipping.label'].search( + cr, uid, [('res_id', '=', picking.id)], context=context) + print "label_ids=", label_ids + if label_ids: + res[picking.id] = True + else: + res[picking.id] = False + print "res=", res + return res + + _columns = { + 'has_delivery_labels': fields.function( + _compute_delivery_labels, type='boolean', + string='Has Delivery Labels', readonly=True), + } + + +class StockPickingOut(orm.Model): + _inherit = 'stock.picking.out' + + def _compute_delivery_labels(self, cr, uid, ids, name, arg, context=None): + res = {} + for picking in self.browse(cr, uid, ids, context=context): + label_ids = self.pool['shipping.label'].search( + cr, uid, [('res_id', '=', picking.id)], context=context) + print "label_ids=", label_ids + if label_ids: + res[picking.id] = True + else: + res[picking.id] = False + print "res=", res + return res + + _columns = { + 'has_delivery_labels': fields.function( + _compute_delivery_labels, type='boolean', + string='Has Delivery Labels', readonly=True), + } + + def print_zpl_label_picking_list( + self, cr, uid, picking_list, context=None): + zpl_files = [] # list of tuple (filename, file) + # GET ZPL files + user = self.pool['res.users'].browse(cr, uid, uid, context=context) + if not user.label_printer_id: + raise orm.except_orm( + _('Erreur:'), + _('No Default Label Printer configured for the user %s') + % user.name) + printer_id = user.label_printer_id.id + for picking in picking_list: + if picking.state != 'done': + raise orm.except_orm( + _('Error:'), + _("The delivery order %s hasn't been validated.") + % picking.name) + label_ids = self.pool['shipping.label'].search( + cr, uid, [('res_id', '=', picking.id)], context=context) + logger.info('%d labels found for picking %s', + len(label_ids), picking.name) + if not label_ids: + raise orm.except_orm( + _('Error:'), + _("No ZPL file attached to the Delivery order %s." + "You should click on the button " + "'Create Shipping Label' of the Delivery order.") + % picking.name) + labels = self.pool['shipping.label'].browse( + cr, uid, label_ids, context=context) + for label in labels: + zpl_files.append( + (label.name, base64.decodestring(label.datas))) + # NOW PRINT ! + logger.info('Starting to print %d ZPL files' % len(zpl_files)) + for zpl_fname, zpl_file in zpl_files: + logger.info('Starting to send ZPL file %s' % zpl_fname) + self.pool['printing.printer'].print_document( + cr, uid, [printer_id], zpl_fname, + zpl_file, 'raw', context=context) + # 4e arg = nom de report bidon... ça ne sert pas a priori + logger.info('ZPL file %s sent' % zpl_fname) + logger.info('Printing of ZPL files finished') + return True + + def print_zpl_delivery_label(self, cr, uid, ids, context=None): + assert len(ids) == 1, "only 1 id" + picking = self.browse(cr, uid, ids[0], context=context) + return self.print_zpl_label_picking_list( + cr, uid, [picking], context=context) + + diff --git a/delivery_carrier_zpl_label_print/stock_view.xml b/delivery_carrier_zpl_label_print/stock_view.xml new file mode 100644 index 0000000..8af96ef --- /dev/null +++ b/delivery_carrier_zpl_label_print/stock_view.xml @@ -0,0 +1,29 @@ + + + + + + + + + + delivery_carrier_zpl_label_print.picking.out.form + stock.picking.out + + + + + + + + + + + + diff --git a/delivery_carrier_zpl_label_print/users_view.xml b/delivery_carrier_zpl_label_print/users_view.xml new file mode 100644 index 0000000..8e411bf --- /dev/null +++ b/delivery_carrier_zpl_label_print/users_view.xml @@ -0,0 +1,22 @@ + + + + + + + + delivery.carrier.zpl.label.res.users.form + res.users + + + + + + + + + +