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

« back to all changes in this revision

Viewing changes to sale_margin/sale_margin.py

  • Committer: Fabien Pinckaers
  • Date: 2007-10-01 19:40:45 UTC
  • Revision ID: fp@tinyerp.com-15d89d735ac468ebe966d8e63cc9bc8efb592b72
Moved Document and Base Synchro to SFD

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# $Id: partner.py 1007 2005-07-25 13:18:09Z kayhman $
 
6
#
 
7
# WARNING: This program as such is intended to be used by professional
 
8
# programmers who take the whole responsability of assessing all potential
 
9
# consequences resulting from its eventual inadequacies and bugs
 
10
# End users who are looking for a ready-to-use solution with commercial
 
11
# garantees and support are strongly adviced to contract a Free Software
 
12
# Service Company
 
13
#
 
14
# This program is Free Software; you can redistribute it and/or
 
15
# modify it under the terms of the GNU General Public License
 
16
# as published by the Free Software Foundation; either version 2
 
17
# of the License, or (at your option) any later version.
 
18
#
 
19
# This program is distributed in the hope that it will be useful,
 
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
# GNU General Public License for more details.
 
23
#
 
24
# You should have received a copy of the GNU General Public License
 
25
# along with this program; if not, write to the Free Software
 
26
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
27
#
 
28
##############################################################################
 
29
 
 
30
from osv import fields,osv
 
31
import pooler
 
32
from tools import config
 
33
import time
 
34
 
 
35
class sale_order_line(osv.osv):
 
36
        _name = "sale.order.line"
 
37
        _inherit = "sale.order.line"
 
38
        def _product_margin(self, cr, uid, ids, field_name, arg, context):
 
39
                res = {}
 
40
                for line in self.browse(cr, uid, ids):
 
41
                        res[line.id] = 0
 
42
                        if line.product_id:
 
43
                                res[line.id] = round((line.price_unit*line.product_uos_qty*(100.0-line.discount)/100.0) -(line.product_id.standard_price*line.product_uos_qty),2)
 
44
                return res
 
45
 
 
46
        _columns = {
 
47
                'margin': fields.function(_product_margin, method=True, string='Margin'),
 
48
        }
 
49
sale_order_line()
 
50
 
 
51
class sale_order(osv.osv):
 
52
        _name = "sale.order"
 
53
        _inherit = "sale.order"
 
54
 
 
55
        def _product_margin(self, cr, uid, ids, field_name, arg, context):
 
56
                id_set = ",".join(map(str, ids))
 
57
                cr.execute("""
 
58
                        SELECT
 
59
                                s.id,
 
60
                                COALESCE(SUM(l.price_unit*l.product_uos_qty*(100-l.discount)/100.0 - t.standard_price * l.product_uos_qty),0)::decimal(16,2) AS amount
 
61
                        FROM
 
62
                                sale_order s
 
63
                        LEFT OUTER JOIN sale_order_line l ON (s.id=l.order_id)
 
64
                        LEFT JOIN product_product p ON (p.id=l.product_id)
 
65
                        LEFT JOIN product_template t ON (t.id=p.product_tmpl_id)
 
66
                        WHERE
 
67
                                s.id IN ("""+id_set+""") GROUP BY s.id """)
 
68
                res = dict(cr.fetchall())
 
69
                return res
 
70
 
 
71
        _columns = {
 
72
                'margin': fields.function(_product_margin, method=True, string='Margin'),
 
73
        }
 
74
sale_order()
 
75
 
 
76