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

« back to all changes in this revision

Viewing changes to purchase_discount/purchase_discount.py

  • Committer: Fabien Pinckaers
  • Date: 2008-11-12 06:43:12 UTC
  • Revision ID: fp@tinyerp.com-20081112064312-fp85io97i1e95tuz
moved

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
1
2
##############################################################################
2
3
#
3
 
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
4
 
#
5
 
# WARNING: This program as such is intended to be used by professional
6
 
# programmers who take the whole responsability of assessing all potential
7
 
# consequences resulting from its eventual inadequacies and bugs
8
 
# End users who are looking for a ready-to-use solution with commercial
9
 
# garantees and support are strongly adviced to contract a Free Software
10
 
# Service Company
11
 
#
12
 
# This program is Free Software; you can redistribute it and/or
13
 
# modify it under the terms of the GNU General Public License
14
 
# as published by the Free Software Foundation; either version 2
15
 
# of the License, or (at your option) any later version.
16
 
#
17
 
# This program is distributed in the hope that it will be useful,
18
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 
# GNU General Public License for more details.
21
 
#
22
 
# You should have received a copy of the GNU General Public License
23
 
# along with this program; if not, write to the Free Software
24
 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
4
#    OpenERP, Open Source Management Solution   
 
5
#    Copyright (C) 2004-2008 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/>.
25
20
#
26
21
##############################################################################
27
22
 
35
30
import pooler
36
31
 
37
32
class purchase_order_line(osv.osv):
38
 
        _name = "purchase.order.line"
39
 
        _inherit = "purchase.order.line"
40
 
 
41
 
        def _amount_line(self, cr, uid, ids, prop, unknow_none,unknow_dict):
42
 
                res = {}
43
 
                cur_obj=self.pool.get('res.currency')
44
 
                for line in self.browse(cr, uid, ids):
45
 
                        cur = line.order_id.pricelist_id.currency_id
46
 
                        res[line.id] = cur_obj.round(cr, uid, cur, line.price_unit * line.product_qty * (1 - (line.discount or 0.0) /100.0))
47
 
                return res
48
 
 
49
 
        _columns = {
50
 
                'discount': fields.float('Discount (%)', digits=(16,2)),
51
 
                'price_subtotal': fields.function(_amount_line, method=True, string='Subtotal'),
52
 
        }
53
 
        _defaults = {
54
 
                'discount': lambda *a: 0.0,
55
 
        }
 
33
    _name = "purchase.order.line"
 
34
    _inherit = "purchase.order.line"
 
35
 
 
36
    def _amount_line(self, cr, uid, ids, prop, unknow_none,unknow_dict):
 
37
        res = {}
 
38
        cur_obj=self.pool.get('res.currency')
 
39
        for line in self.browse(cr, uid, ids):
 
40
            cur = line.order_id.pricelist_id.currency_id
 
41
            res[line.id] = cur_obj.round(cr, uid, cur, line.price_unit * line.product_qty * (1 - (line.discount or 0.0) /100.0))
 
42
        return res
 
43
 
 
44
    _columns = {
 
45
        'discount': fields.float('Discount (%)', digits=(16,2)),
 
46
        'price_subtotal': fields.function(_amount_line, method=True, string='Subtotal'),
 
47
    }
 
48
    _defaults = {
 
49
        'discount': lambda *a: 0.0,
 
50
    }
56
51
purchase_order_line()
57
52
 
58
53
class purchase_order(osv.osv):
59
 
        _name = "purchase.order"
60
 
        _inherit = "purchase.order"
61
 
        
62
 
        def _amount_untaxed(self, cr, uid, ids, field_name, arg, context):
63
 
                id_set = ",".join(map(str, ids))
64
 
                sql_req="SELECT s.id,COALESCE(SUM(l.price_unit*l.product_qty*(100.0-l.discount))/100.0,0) AS amount FROM purchase_order s LEFT OUTER JOIN purchase_order_line l ON (s.id=l.order_id) WHERE s.id IN ("+id_set+") GROUP BY s.id"
65
 
                cr.execute(sql_req)
66
 
                
67
 
                res = dict(cr.fetchall())
68
 
 
69
 
                cur_obj=self.pool.get('res.currency')
70
 
                for id in res.keys():
71
 
                        order=self.browse(cr, uid, [id])[0]
72
 
                        cur=order.pricelist_id.currency_id
73
 
                        res[id]=cur_obj.round(cr, uid, cur, res[id])
74
 
                return res
75
 
 
76
 
 
77
 
        def _amount_tax(self, cr, uid, ids, field_name, arg, context):
78
 
                res = {}
79
 
                cur_obj=self.pool.get('res.currency')
80
 
                for order in self.browse(cr, uid, ids):
81
 
                        val = 0.0
82
 
                        cur=order.pricelist_id.currency_id
83
 
                        for line in order.order_line:
84
 
                                for c in self.pool.get('account.tax').compute(cr, uid, line.taxes_id, line.price_unit * (1-(line.discount or 0.0)/100.0), line.product_qty, order.partner_address_id.id, line.product_id):
85
 
                                        val+= cur_obj.round(cr, uid, cur, c['amount'])
86
 
                        res[order.id]=cur_obj.round(cr, uid, cur, val)
87
 
                return res
88
 
 
89
 
        _columns = {
90
 
                'amount_untaxed': fields.function(_amount_untaxed, method=True, string='Untaxed Amount'),
91
 
                'amount_tax': fields.function(_amount_tax, method=True, string='Taxes'),
92
 
        }               
93
 
        
 
54
    _name = "purchase.order"
 
55
    _inherit = "purchase.order"
 
56
    
 
57
    def _amount_untaxed(self, cr, uid, ids, field_name, arg, context):
 
58
        id_set = ",".join(map(str, ids))
 
59
        sql_req="SELECT s.id,COALESCE(SUM(l.price_unit*l.product_qty*(100.0-l.discount))/100.0,0) AS amount FROM purchase_order s LEFT OUTER JOIN purchase_order_line l ON (s.id=l.order_id) WHERE s.id IN ("+id_set+") GROUP BY s.id"
 
60
        cr.execute(sql_req)
 
61
        
 
62
        res = dict(cr.fetchall())
 
63
 
 
64
        cur_obj=self.pool.get('res.currency')
 
65
        for id in res.keys():
 
66
            order=self.browse(cr, uid, [id])[0]
 
67
            cur=order.pricelist_id.currency_id
 
68
            res[id]=cur_obj.round(cr, uid, cur, res[id])
 
69
        return res
 
70
 
 
71
 
 
72
    def _amount_tax(self, cr, uid, ids, field_name, arg, context):
 
73
        res = {}
 
74
        cur_obj=self.pool.get('res.currency')
 
75
        for order in self.browse(cr, uid, ids):
 
76
            val = 0.0
 
77
            cur=order.pricelist_id.currency_id
 
78
            for line in order.order_line:
 
79
                for c in self.pool.get('account.tax').compute(cr, uid, line.taxes_id, line.price_unit * (1-(line.discount or 0.0)/100.0), line.product_qty, order.partner_address_id.id, line.product_id):
 
80
                    val+= cur_obj.round(cr, uid, cur, c['amount'])
 
81
            res[order.id]=cur_obj.round(cr, uid, cur, val)
 
82
        return res
 
83
 
 
84
    _columns = {
 
85
        'amount_untaxed': fields.function(_amount_untaxed, method=True, string='Untaxed Amount'),
 
86
        'amount_tax': fields.function(_amount_tax, method=True, string='Taxes'),
 
87
    }       
 
88
    
94
89
purchase_order()
 
90
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
91