changed encoding to coding in all py files
[odoo/odoo.git] / addons / mrp / report / price.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import ir
24 import pooler
25 from report.interface import report_rml
26 from report.interface import toxml
27
28 from tools.translate import _
29
30
31 #FIXME: we should use toxml
32 class report_custom(report_rml):
33     def create_xml(self, cr, uid, ids, datas, context={}):
34         number = (datas.get('form', False) and datas['form']['number']) or 1
35         
36         def process_bom(bom, currency_id):
37             xml = '<row>'
38             sum = 0
39             sum_strd = 0
40             prod = pooler.get_pool(cr.dbname).get('product.product').browse(cr, uid, bom['product_id'])
41
42             prod_name = bom['name']
43             prod_qtty = bom['product_qty']
44             prod_uom = prod.uom_id.name 
45             level = 1
46             main_sp_price = ''
47             main_sp_name = ''
48             main_strd_price = ''
49             main_strd_price = ''
50             if prod.seller_ids and prod.seller_ids[0] :
51                 main_sp_name = '<b>' + prod.seller_ids[0].name.name + '</b>\r\n'
52                 pricelist_id = prod.seller_ids[0].name.property_product_pricelist_purchase.id
53                 if pricelist_id:
54                     pricelist_obj = pooler.get_pool(cr.dbname).get('product.pricelist')
55                     price = pricelist_obj.price_get(cr,uid,[pricelist_id], prod.id, number*prod_qtty or 1.0).setdefault(pricelist_id, 0)
56                     price = pooler.get_pool(cr.dbname).get('res.currency').compute(cr, uid, pricelist_obj.browse(cr, uid, pricelist_id).currency_id.id, currency_id, price)
57                 else:
58                     price = 0
59                 main_sp_price = '%.2f' % price + '\r\n'
60                 sum += prod_qtty*price
61
62             main_strd_price = '%.2f' % prod.standard_price + '\r\n'
63             sum_strd = prod_qtty*prod.standard_price
64
65             sellers = ''
66             sellers_price = ''
67             for seller_id in prod.seller_ids:
68                 sellers +=  '- <i>'+ seller_id.name.name +'</i>\r\n'
69                 pricelist_id = seller_id.name.property_product_pricelist_purchase.id
70                 if pricelist_id:
71                     pricelist_obj = pooler.get_pool(cr.dbname).get('product.pricelist')
72                     price = pricelist_obj.price_get(cr,uid,[pricelist_id], prod.id, number*prod_qtty or 1.0).setdefault(pricelist_id, 0)
73                     price = pooler.get_pool(cr.dbname).get('res.currency').compute(cr, uid, pricelist_obj.browse(cr, uid, pricelist_id).currency_id.id, currency_id, price)
74                 else:
75                     price = 0
76                 sellers_price += '%.2f' % price + '\r\n'
77
78             xml += "<col para='yes'>" + prod_name + '</col>'
79             xml += "<col para='no'>" + main_sp_name +  sellers + '</col>'
80             xml += "<col para='yes'>" + str(prod_qtty) + '</col>'
81             xml += "<col para='yes'>" + prod_uom + '</col>'
82             xml += "<col para='yes'>" + main_strd_price + '</col>'
83             xml += "<col para='no'>" + main_sp_price +  sellers_price + '</col>'
84
85
86             xml += '</row>'
87             return xml, sum, sum_strd
88
89         def process_workcenter(wrk):
90             xml = '<row>'
91
92             workcenter = pooler.get_pool(cr.dbname).get('mrp.workcenter').browse(cr, uid, wrk['workcenter_id'])
93
94             xml += "<col para='yes'>" + wrk['name'] + '</col>'
95             xml += "<col para='yes'>" +  '</col>'
96             xml += "<col para='no'>" + '</col>'
97             xml += "<col/>"
98             xml += "<col para='no'>" + str(wrk['cycle']*workcenter.costs_cycle) + '</col>'
99             xml += "<col para='yes'>" + str(wrk['hour']*workcenter.costs_hour) + '</col>'
100
101
102             xml += '</row>'
103             return xml, wrk['cycle']*workcenter.costs_cycle+wrk['hour']*workcenter.costs_hour
104
105
106         xml = ''
107         config_start = """
108         <config>
109             <date>09/09/2005</date>
110             <PageSize>210.00mm,297.00mm</PageSize>
111             <PageWidth>595.27</PageWidth>
112             <PageHeight>841.88</PageHeight>
113             <tableSize>60.00mm,60.00mm, 20.00mm, 20.00mm, 20.00mm, 20.00mm</tableSize>
114             """
115         config_stop = """
116             <report-footer>Generated by Open ERP</report-footer>
117         </config>
118         """
119         header = """
120             <header>
121             <field>%s</field>
122             <field>%s</field>
123             <field>%s</field>
124             <field>%s</field>
125             <field>%s</field>
126             <field>%s</field>
127             </header>
128             """ % (_('Product name'), _('Product supplier'), _('Product quantity'), _('Product uom'), _('Product Standard Price'), _('Unit Product Price'))
129         workcenter_header = """
130             <lines style='header'>
131                 <row>
132                     <col>%s</col>
133                     <col/>
134                     <col/>
135                     <col/>
136                     <col>%s</col>
137                     <col>%s</col>
138                 </row>
139             </lines>
140         """ % (_('Workcenter name'), _('Cycles Cost'), _('Hours Cost'))
141         prod_header = """
142             <lines style='header'>
143                 <row>
144                     <col para='yes'>%s</col>
145                     <col para='yes'>%s</col>
146                     <col para='yes'>%s</col>
147                     <col para='yes'>%s</col>
148                     <col para='yes'>%s</col>
149                     <col para='yes'>%s</col>
150                 </row>
151             </lines>
152         """ % (_('Product name'), _('Product supplier'), _('Product Quantity'), _('Product uom'), _('Product Standard Price'), _('Unit Product Price'))
153
154         company_currency = pooler.get_pool(cr.dbname).get('res.users').browse(cr, uid, uid).company_id.currency_id.id
155         first = True
156         for prod_id in ids:
157             bom_ids = pooler.get_pool(cr.dbname).get('mrp.bom').search(cr, uid, [('product_id','=',prod_id)])
158             prod = pooler.get_pool(cr.dbname).get('product.product').browse(cr, uid, prod_id)
159
160             for bom_id in bom_ids:
161                 bom = pooler.get_pool(cr.dbname).get('mrp.bom').browse(cr, uid, bom_id)
162
163                 sub_boms = pooler.get_pool(cr.dbname).get('mrp.bom')._bom_explode(cr, uid, bom, number, [])
164                 total = 0
165                 total_strd = 0
166                 parent_bom = {'product_qty': bom.product_qty, 'name': bom.product_id.name, 'product_uom': bom.product_id.uom_id.factor, 'product_id': bom.product_id.id}
167                 xml_tmp = ''
168                 for sub_bom in (sub_boms and sub_boms[0]) or [parent_bom]:
169                     txt, sum, sum_strd = process_bom(sub_bom, company_currency)
170                     xml_tmp +=  txt
171                     total += sum
172                     total_strd += sum_strd
173                 if not first:
174                     xml += prod_header
175                 xml += "<lines style='lines'>" + xml_tmp + '</lines>'
176                 xml += "<lines style='sub_total'><row><col>%s : </col><col>(" % (_('SUBTOTAL')) + str(number) + " %s)</col><col/><col/><col>" % (_('products')) + '%.2f' % total_strd + '</col><col>' + '%.2f' % total  + '</col></row></lines>'
177                 total2 = 0
178                 xml_tmp = ''
179                 for wrk in (sub_boms and sub_boms[1]):
180                     txt, sum = process_workcenter(wrk)
181                     xml_tmp += txt
182                     total2 += sum
183                 if xml_tmp:
184                     xml += workcenter_header
185                     xml += "<lines style='lines'>" + xml_tmp + '</lines>'
186                     xml += "<lines style='sub_total'><row><col>%s : </col><col>(" % (_('SUBTOTAL')) + str(number) + " %s)</col><col/><col/><col/><col>" % (_('products')) + '%.2f' % total2 + '</col></row></lines>'
187                 xml += "<lines style='total'><row><col>%s : </col><col>(" % (_('TOTAL')) + str(number) + " %s)</col><col/><col/><col>" % (_('products')) + '%.2f' % (total_strd+total2) + "</col><col>" + '%.2f' % (total+total2) + '</col></row></lines>'
188
189                 first = False
190
191         xml = '<?xml version="1.0" ?><report>' + config_start + '<report-header>%s\n\r' % (_('Product Cost Structure')) + prod.name  + '</report-header>'+ config_stop +  header + xml + '</report>'
192
193         return xml
194
195 report_custom('report.product.price', 'product.product', '', 'addons/mrp/report/price.xsl')
196
197 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
198