[TEST]
[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 time
23 import pooler
24 from report.interface import report_rml
25 from tools import to_xml
26 from report import report_sxw
27 from datetime import datetime
28 from tools.translate import _
29
30 class report_custom(report_rml):
31     def create_xml(self, cr, uid, ids, datas, context=None):
32         number = (datas.get('form', False) and datas['form']['number']) or 1
33         pool = pooler.get_pool(cr.dbname)
34         product_pool = pool.get('product.product')
35         product_uom_pool = pool.get('product.uom')
36         supplier_info_pool = pool.get('product.supplierinfo')
37         workcenter_pool = pool.get('mrp.workcenter')
38         user_pool = pool.get('res.users')
39         bom_pool = pool.get('mrp.bom')
40         pricelist_pool = pool.get('product.pricelist')
41         rml_obj=report_sxw.rml_parse(cr, uid, product_pool._name,context)
42         rml_obj.localcontext.update({'lang':context.get('lang',False)})
43         company_currency = user_pool.browse(cr, uid, uid).company_id.currency_id
44         company_currency_symbol = company_currency.symbol or company_currency.name
45         def process_bom(bom, currency_id, factor=1):
46             xml = '<row>'
47             sum = 0
48             sum_strd = 0
49             prod = product_pool.browse(cr, uid, bom['product_id'])
50
51             prod_name = to_xml(bom['name'])
52             prod_qtty = factor * bom['product_qty']
53             product_uom = product_uom_pool.browse(cr, uid, bom['product_uom'], context=context)
54             product_uom_name = to_xml(product_uom.name)
55             main_sp_price, main_sp_name , main_strd_price = '','',''
56             sellers, sellers_price = '',''
57
58             if prod.seller_id:
59                 main_sp_name = '- <b>'+ to_xml(prod.seller_id.name) +'</b>\r\n'
60                 pricelist =  prod.seller_id.property_product_pricelist_purchase
61                 price = pricelist_pool.price_get(cr,uid,[pricelist.id],
62                      prod.id, number*prod_qtty or 1.0, prod.seller_id.id, {
63                         'uom': prod.uom_po_id.id,
64                         'date': time.strftime('%Y-%m-%d'),
65                         })[pricelist.id]
66                 main_sp_price = """<b>"""+rml_obj.formatLang(price)+' '+ (company_currency_symbol)+"""</b>\r\n"""
67                 sum += prod_qtty*price
68             std_price = product_uom_pool._compute_price(cr, uid, prod.uom_id.id, prod.standard_price, to_uom_id=product_uom.id)
69             main_strd_price = str(std_price) + '\r\n'
70             sum_strd = prod_qtty*std_price
71             for seller_id in prod.seller_ids:
72                 sellers +=  '- <i>'+ to_xml(seller_id.name.name) +'</i>\r\n'
73                 pricelist = seller_id.name.property_product_pricelist_purchase
74                 price = pricelist_pool.price_get(cr,uid,[pricelist.id],
75                      prod.id, number*prod_qtty or 1.0, seller_id.name.id, {
76                         'uom': prod.uom_po_id.id,
77                         'date': time.strftime('%Y-%m-%d'),
78                         })[pricelist.id]
79                 sellers_price += """<i>"""+rml_obj.formatLang(price) +' '+ (company_currency_symbol) +"""</i>\r\n"""
80             xml += """<col para='yes'> """+ prod_name +""" </col>
81                     <col para='yes'> """+ main_sp_name + sellers + """ </col>
82                     <col f='yes'>"""+ rml_obj.formatLang(prod_qtty) +' '+ product_uom_name +"""</col>
83                     <col f='yes'>"""+ rml_obj.formatLang(float(main_strd_price)) +' '+ (company_currency_symbol) +"""</col>
84                     <col f='yes'>""" + main_sp_price + sellers_price + """</col>'"""
85
86             xml += '</row>'
87             return xml, sum, sum_strd
88
89         def process_workcenter(wrk):
90             workcenter = workcenter_pool.browse(cr, uid, wrk['workcenter_id'])
91             cost_cycle = wrk['cycle']*workcenter.costs_cycle
92             cost_hour = wrk['hour']*workcenter.costs_hour
93             total = cost_cycle + cost_hour
94             xml = '<row>'
95             xml += "<col para='yes'>" + to_xml(workcenter.name) + '</col>'
96             xml += "<col/>"
97             xml += """<col f='yes'>"""+rml_obj.formatLang(cost_cycle)+' '+ (company_currency_symbol) + """</col>"""
98             xml += """<col f='yes'>"""+rml_obj.formatLang(cost_hour)+' '+ (company_currency_symbol) + """</col>"""
99             xml += """<col f='yes'>"""+rml_obj.formatLang(cost_hour + cost_cycle)+' '+ (company_currency_symbol) + """</col>"""
100             xml += '</row>'
101
102             return xml, total
103
104
105         xml = ''
106         config_start = """
107         <config>
108             <date>""" + to_xml(rml_obj.formatLang(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),date_time=True)) + """</date>
109             <company>%s</company>
110             <PageSize>210.00mm,297.00mm</PageSize>
111             <PageWidth>595.27</PageWidth>
112             <PageHeight>841.88</PageHeight>
113             <tableSize>55.00mm,58.00mm,29.00mm,29.00mm,29.00mm</tableSize>
114             """ % to_xml(user_pool.browse(cr, uid, uid).company_id.name)
115         config_stop = """
116             <report-footer>Generated by OpenERP</report-footer>
117         </config>
118         """
119
120         workcenter_header = """
121             <lines style='header'>
122                 <row>
123                     <col>%s</col>
124                     <col t='yes'/>
125                     <col t='yes'>%s</col>
126                     <col t='yes'>%s</col>
127                     <col t='yes'>%s</col>
128                 </row>
129             </lines>
130         """ % (_('Work Center name'), _('Cycles Cost'), _('Hourly Cost'),_('Work Cost'))
131         prod_header = """
132                 <row>
133                     <col>%s</col>
134                     <col>%s</col>
135                     <col t='yes'>%s</col>
136                     <col t='yes'>%s</col>
137                     <col t='yes'>%s</col>
138                 </row>
139         """ % (_('Components'), _('Components suppliers'), _('Quantity'),_('Cost Price per Unit of Measure'), _('Supplier Price per Unit of Measure'))
140
141         purchase_price_digits = rml_obj.get_digits(dp='Product Price')
142
143         for product in product_pool.browse(cr, uid, ids, context=context):
144             product_uom_name = to_xml(product.uom_id.name)
145             bom_id = bom_pool._bom_find(cr, uid, product.id, product.uom_id.id)
146             title = "<title>%s</title>" %(_("Cost Structure"))
147             title += "<title>%s</title>" % (to_xml(product.name))
148             xml += "<lines style='header'>" + title + prod_header + "</lines>"
149             if not bom_id:
150                 total_strd = number * product.standard_price
151                 total = number * product_pool.price_get(cr, uid, [product.id], 'standard_price')[product.id]
152                 xml += """<lines style='lines'><row>
153                     <col para='yes'>-</col>
154                     <col para='yes'>-</col>
155                     <col para='yes'>-</col>
156                     <col para='yes'>-</col>
157                     <col para='yes'>-</col>
158                     </row></lines>"""
159                 xml += """<lines style='total'> <row>
160                     <col> """ + _('Total Cost of %s %s') % (str(number), product_uom_name) + """: </col>
161                     <col/>
162                     <col f='yes'/>
163                     <col t='yes'>"""+ rml_obj.formatLang(total_strd, digits=purchase_price_digits) +' '+ (company_currency_symbol) + """</col>
164                     <col t='yes'>"""+ rml_obj.formatLang(total, digits=purchase_price_digits) +' '+ (company_currency_symbol) + """</col>
165                     </row></lines>'"""
166             else:
167                 bom = bom_pool.browse(cr, uid, bom_id, context=context)
168                 factor = number * product.uom_id.factor / bom.product_uom.factor
169                 sub_boms = bom_pool._bom_explode(cr, uid, bom, factor / bom.product_qty)
170                 total = 0
171                 total_strd = 0
172                 parent_bom = {
173                         'product_qty': bom.product_qty,
174                         'name': bom.product_id.name,
175                         'product_uom': bom.product_uom.id,
176                         'product_id': bom.product_id.id
177                 }
178                 xml_tmp = ''
179                 for sub_bom in (sub_boms and sub_boms[0]) or [parent_bom]:
180                     txt, sum, sum_strd = process_bom(sub_bom, company_currency.id)
181                     xml_tmp +=  txt
182                     total += sum
183                     total_strd += sum_strd
184
185                 xml += "<lines style='lines'>" + xml_tmp + '</lines>'
186                 xml += """<lines style='sub_total'> <row>
187                     <col> """ + _('Components Cost of %s %s') % (str(number), product_uom_name) + """: </col>
188                     <col/>
189                     <col t='yes'/>
190                     <col t='yes'>"""+ rml_obj.formatLang(total_strd, digits=purchase_price_digits) +' '+ (company_currency_symbol) + """</col>
191                     <col t='yes'></col>
192                     </row></lines>'"""
193
194                 total2 = 0
195                 xml_tmp = ''
196                 for wrk in (sub_boms and sub_boms[1]):
197                     txt, sum = process_workcenter(wrk)
198                     xml_tmp += txt
199                     total2 += sum
200                 if xml_tmp:
201                     xml += workcenter_header
202                     xml += "<lines style='lines'>" + xml_tmp + '</lines>'
203                     xml += """<lines style='sub_total'> <row>
204                     <col> """ + _('Work Cost of %s %s') % (str(number), product_uom_name) +""": </col>
205                     <col/>
206                     <col/>
207                     <col/>
208                     <col t='yes'>"""+ rml_obj.formatLang(total2, digits=purchase_price_digits) +' '+ (company_currency_symbol) +"""</col>
209                     </row></lines>'"""
210                 xml += """<lines style='total'> <row>
211                     <col> """ + _('Total Cost of %s %s') % (str(number), product_uom_name) + """: </col>
212                     <col/>
213                     <col t='yes'/>
214                     <col t='yes'>"""+ rml_obj.formatLang(total_strd+total2, digits=purchase_price_digits) +' '+ (company_currency_symbol) + """</col>
215                     <col t='yes'></col>
216                     </row></lines>'"""
217
218         xml = '<?xml version="1.0" ?><report>' + config_start + config_stop + xml + '</report>'
219
220         return xml
221
222 report_custom('report.product.price', 'product.product', '', 'addons/mrp/report/price.xsl')
223
224 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: