~almacom/openobject-addons/ac_addons

« back to all changes in this revision

Viewing changes to purchase/report/order.py

  • Committer: David Janssens
  • Date: 2009-05-15 10:50:47 UTC
  • Revision ID: david.j@almacom.co.th-20090515105047-3fefsk2io2bloi0f
import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution   
 
5
#    Copyright (C) 2004-2009 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/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
import time
 
24
from report import report_sxw
 
25
from osv import osv
 
26
import pooler
 
27
 
 
28
class order(report_sxw.rml_parse):
 
29
    def __init__(self, cr, uid, name, context):
 
30
        super(order, self).__init__(cr, uid, name, context)
 
31
        self.localcontext.update({
 
32
            'time': time,
 
33
            'get_line_tax': self._get_line_tax,
 
34
            'get_tax': self._get_tax,
 
35
            'get_product_code': self._get_product_code,
 
36
        })
 
37
    def _get_line_tax(self, line_obj):
 
38
        self.cr.execute("SELECT tax_id FROM purchase_order_taxe WHERE order_line_id=%s", (line_obj.id))
 
39
        res = self.cr.fetchall() or None
 
40
        if not res:
 
41
            return ""
 
42
        if isinstance(res, list):
 
43
            tax_ids = [t[0] for t in res]
 
44
        else:
 
45
            tax_ids = res[0]
 
46
        res = [tax.name for tax in pooler.get_pool(cr.dbname).get('account.tax').browse(self.cr, self.uid, tax_ids)]
 
47
        return ",\n ".join(res)
 
48
    
 
49
    def _get_tax(self, order_obj):
 
50
        self.cr.execute("SELECT DISTINCT tax_id FROM purchase_order_taxe, purchase_order_line, purchase_order \
 
51
            WHERE (purchase_order_line.order_id=purchase_order.id) AND (purchase_order.id=%s)", (order_obj.id))
 
52
        res = self.cr.fetchall() or None
 
53
        if not res:
 
54
            return []
 
55
        if isinstance(res, list):
 
56
            tax_ids = [t[0] for t in res]
 
57
        else:
 
58
            tax_ids = res[0]
 
59
        tax_obj = pooler.get_pool(cr.dbname).get('account.tax')
 
60
        res = []
 
61
        for tax in tax_obj.browse(self.cr, self.uid, tax_ids):
 
62
            self.cr.execute("SELECT DISTINCT order_line_id FROM purchase_order_line, purchase_order_taxe \
 
63
                WHERE (purchase_order_taxe.tax_id=%s) AND (purchase_order_line.order_id=%s)", (tax.id, order_obj.id))
 
64
            lines = self.cr.fetchall() or None
 
65
            if lines:
 
66
                if isinstance(lines, list):
 
67
                    line_ids = [l[0] for l in lines]
 
68
                else:
 
69
                    line_ids = lines[0]
 
70
                base = 0
 
71
                for line in pooler.get_pool(cr.dbname).get('purchase.order.line').browse(self.cr, self.uid, line_ids):
 
72
                    base += line.price_subtotal
 
73
                res.append({'code':tax.name,
 
74
                    'base':base,
 
75
                    'amount':base*tax.amount})
 
76
        return res
 
77
    def _get_product_code(self, product_id, partner_id):
 
78
        product_obj=pooler.get_pool(self.cr.dbname).get('product.product')
 
79
        return product_obj._product_code(self.cr, self.uid, [product_id], name=None, arg=None, context={'partner_id': partner_id})[product_id]
 
80
 
 
81
report_sxw.report_sxw('report.purchase.order','purchase.order','addons/purchase/report/order.rml',parser=order)
 
82
 
 
83
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
84