diff --git a/addons/point_of_sale/point_of_sale.py b/addons/point_of_sale/point_of_sale.py index 37b5eaa4e8e..7c4740be19a 100644 --- a/addons/point_of_sale/point_of_sale.py +++ b/addons/point_of_sale/point_of_sale.py @@ -19,6 +19,8 @@ # ############################################################################## import pdb +import io +import addons import time from datetime import datetime @@ -1133,6 +1135,8 @@ class pos_order_line(osv.osv): pos_order_line() class pos_category(osv.osv): + PHOTO_SIZE_HEIGHT = 540 + PHOTO_SIZE_WIDTH = 450 _name = 'pos.category' _description = "Point of Sale Category" _order = "sequence, name" @@ -1150,6 +1154,38 @@ class pos_category(osv.osv): (_check_recursion, 'Error ! You cannot create recursive categories.', ['parent_id']) ] + def onchange_photo(self, cr, uid, ids, value, context=None): + if not value: + return {'value': {'photo_big': value, 'photo': value} } + return { + 'value': { + 'photo_big': self._photo_resize(cr, uid, value, self.PHOTO_SIZE_HEIGHT, self.PHOTO_SIZE_WIDTH, context=context), + 'photo': self._photo_resize(cr, uid, value, context=context) + } + } + + def _set_photo(self, cr, uid, id, name, value, args, context=None): + if not value: + vals = {'photo_big': value} + else: + vals = {'photo_big': self._photo_resize(cr, uid, value, self.PHOTO_SIZE_HEIGHT, self.PHOTO_SIZE_WIDTH, context=context)} + return self.write(cr, uid, [id], vals, context=context) + + def _photo_resize(self, cr, uid, photo, heigth=180, width=150, context=None): + image_stream = io.BytesIO(photo.decode('base64')) + img = Image.open(image_stream) + img.thumbnail((heigth, width), Image.ANTIALIAS) + img_stream = StringIO.StringIO() + img.save(img_stream, "JPEG") + return img_stream.getvalue().encode('base64') + + def _get_photo(self, cr, uid, ids, name, args, context=None): + result = dict.fromkeys(ids, False) + for category in self.browse(cr, uid, ids, context=context): + if category.photo_big: + result[category.id] = self._photo_resize(cr, uid, category.photo_big, context=context) + return result + def name_get(self, cr, uid, ids, context=None): if not len(ids): return [] @@ -1172,7 +1208,22 @@ class pos_category(osv.osv): 'parent_id': fields.many2one('pos.category','Parent Category', select=True), 'child_id': fields.one2many('pos.category', 'parent_id', string='Children Categories'), 'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of product categories."), + 'photo_big': fields.binary('Big-sized employee photo', help="This field holds the photo of the employee. The photo field is used as an interface to access this field. The image is base64 encoded, and PIL-supported. Full-sized photo are however resized to 540x450 px."), + 'photo': fields.function(_get_photo, fnct_inv=_set_photo, string='Employee photo', type="binary", + store = { + 'hr.employee': (lambda self, cr, uid, ids, c={}: ids, ['photo_big'], 10), + }, help="Image used as photo for the employee. It is automatically resized as a 180x150 px image. A larger photo is stored inside the photo_big field."), } + + def _default_photo(self, cr, uid, context=None): + photo_path = addons.get_module_resource('point_of_sale','images','default_category_photo.png') + return self._photo_resize(cr, uid, open(photo_path, 'rb').read().encode('base64'), context=context) + + + _defaults = { + 'photo' : _default_photo, + } + pos_category() import io, StringIO diff --git a/addons/point_of_sale/point_of_sale_view.xml b/addons/point_of_sale/point_of_sale_view.xml index a320c547353..527635f2452 100644 --- a/addons/point_of_sale/point_of_sale_view.xml +++ b/addons/point_of_sale/point_of_sale_view.xml @@ -648,6 +648,9 @@ + + +