~qtheuret/unifield-addons/ref_addons

« back to all changes in this revision

Viewing changes to mrp/report/price.py

  • Committer: Quentin THEURET
  • Date: 2014-03-05 12:14:13 UTC
  • mfrom: (4612.2.2 unifield-addons)
  • Revision ID: qt@tempo-consulting.fr-20140305121413-qgiq1n9ujvojbtmv
Merge latest trunk

Show diffs side-by-side

added added

removed removed

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