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