~openerp-commiter/openobject-addons/extra-6.0

« back to all changes in this revision

Viewing changes to product_catalog_report/report/product_catalog.py

  • Committer: Harshad Modi
  • Date: 2008-03-03 06:15:54 UTC
  • Revision ID: hmo@tinyerp.com-3b4316a56d0263e4a15ea4323cf77539512c9b87
summary:
* This module use to print report of product catalog with product image, list price

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2005-2007 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# WARNING: This program as such is intended to be used by professional
 
6
# programmers who take the whole responsability of assessing all potential
 
7
# consequences resulting from its eventual inadequacies and bugs
 
8
# End users who are looking for a ready-to-use solution with commercial
 
9
# garantees and support are strongly adviced to contract a Free Software
 
10
# Service Company
 
11
#
 
12
# This program is Free Software; you can redistribute it and/or
 
13
# modify it under the terms of the GNU General Public License
 
14
# as published by the Free Software Foundation; either version 2
 
15
# of the License, or (at your option) any later version.
 
16
#
 
17
# This program is distributed in the hope that it will be useful,
 
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
# GNU General Public License for more details.
 
21
#
 
22
# You should have received a copy of the GNU General Public License
 
23
# along with this program; if not, write to the Free Software
 
24
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
25
#
 
26
##############################################################################
 
27
 
 
28
import time
 
29
import reportlab
 
30
import reportlab.lib.units
 
31
import urllib
 
32
import base64
 
33
from report import report_sxw
 
34
 
 
35
class product_catalog(report_sxw.rml_parse):
 
36
    def __init__(self, cr, uid, name, context):
 
37
        super(product_catalog, self).__init__(cr, uid, name, context)
 
38
 
 
39
        self.localcontext.update({
 
40
            'time': time,
 
41
            'image_url' : self._get_imagepath,
 
42
            'currency_code': self._get_currency,
 
43
#            'Carton': self._get_carton,
 
44
#            'SCondt': self._get_SCondt,
 
45
#            'UV': self._get_UV,
 
46
            'categories':self._getCategories,
 
47
            'products':self._getProducts,
 
48
            'description':self._get_desc,
 
49
            'packaging_title':self._get_packaging_title,
 
50
            'packaging_value':self._get_packaging_value,
 
51
            'Price':self._get_price,
 
52
 
 
53
        })
 
54
    def _get_imagepath(self,product):
 
55
        attach_ids = self.pool.get('ir.attachment').search(self.cr, self.uid, [('res_model','=','product.product'), ('res_id', '=',product)])
 
56
        datas = self.pool.get('ir.attachment').read(self.cr, self.uid, attach_ids)
 
57
        if len(datas):
 
58
            # if there are several, pick first
 
59
            try:
 
60
                if datas[0]['link']:
 
61
                    try:
 
62
                        img_data =  base64.encodestring(urllib.urlopen(datas[0]['link']).read())
 
63
                        return img_data
 
64
                    except Exception,innerEx:
 
65
                        print innerEx
 
66
                elif datas[0]['datas']:
 
67
                    return datas[0]['datas']
 
68
            except Exception,e:
 
69
                print e
 
70
        return None
 
71
 
 
72
    def setCat(self,cats):
 
73
        lst = []
 
74
        for cat in cats:
 
75
            if cat not in lst:
 
76
                lst.append(cat)
 
77
                category = self.pool.get('product.category').read(self.cr,self.uid,[cat])
 
78
                if category[0]['child_id']:
 
79
                    lst.extend(self.setCat(category[0]['child_id']))
 
80
        return lst
 
81
 
 
82
 
 
83
    def _getCategories(self,cat):
 
84
        lst =  self.setCat(cat[0][2])
 
85
        cat_ids = self.pool.get('product.category').search(self.cr,self.uid,[('id','in',lst)])
 
86
        tmpCat_ids = []
 
87
        for cat in cat_ids:
 
88
            prod_ids = self.pool.get('product.template').search(self.cr,self.uid,[('categ_id','=',cat)])
 
89
            if len(prod_ids):
 
90
                tmpCat_ids.append(cat)
 
91
        cats = self.pool.get('product.category').read(self.cr,self.uid,tmpCat_ids)
 
92
        return cats
 
93
    def _getProducts(self,category,lang):
 
94
        prod_tmpIDs = self.pool.get('product.template').search(self.cr,self.uid,[('categ_id','=',category)])
 
95
        prod_ids = self.pool.get('product.product').search(self.cr,self.uid,[('product_tmpl_id','in',prod_tmpIDs)])
 
96
        prods = self.pool.get('product.product').read(self.cr,self.uid,prod_ids,context={'lang':lang})
 
97
        return prods
 
98
 
 
99
    def _get_currency(self):
 
100
        return self.pool.get('res.users').browse(self.cr, self.uid, [self.uid])[0].company_id.currency_id.name
 
101
 
 
102
 
 
103
    def _get_packaging_title(self,product,index):
 
104
        packaging_ids = self.pool.get('product.packaging').search(self.cr,self.uid,[('product_id','=',product)],limit=4)
 
105
        packs = self.pool.get('product.packaging').read(self.cr,self.uid,packaging_ids)
 
106
        if len(packs) > index:
 
107
            s = str(packs[index]['name'])
 
108
            if len(s)>9:
 
109
                p = str(s[0:9]) + "..."
 
110
                return p
 
111
            elif not s == "False":
 
112
                return s
 
113
        return " "
 
114
 
 
115
    def _get_packaging_value(self,product,index):
 
116
        packaging_ids = self.pool.get('product.packaging').search(self.cr,self.uid,[('product_id','=',product)],limit=4)
 
117
        packs = self.pool.get('product.packaging').read(self.cr,self.uid,packaging_ids)
 
118
        if len(packs) > index:
 
119
            return str(packs[index]['qty'])
 
120
        return False
 
121
 
 
122
    def _get_price(self,product,pricelist):
 
123
        price = self.pool.get('product.pricelist').price_get(self.cr,self.uid,[pricelist], product, 1.0, None,{'uom': False})[pricelist]
 
124
        if not price:
 
125
            price = 0.0
 
126
        return price
 
127
 
 
128
    def _get_desc(self,tempate_id):
 
129
        if tempate_id:
 
130
            prodtmpl = self.pool.get('product.template').read(self.cr,self.uid,[tempate_id])[0]
 
131
 
 
132
            if prodtmpl['description_sale']:
 
133
                return prodtmpl['description_sale']
 
134
            else:
 
135
                return "no Description Specified"
 
136
        else:
 
137
            return "This is Test Description"
 
138
 
 
139
 
 
140
report_sxw.report_sxw('report.product_catalog', 'res.partner', 'addons/product_catalog_report/report/product_catalog.rml', parser=product_catalog,header=False)
 
141
 
 
142