~jamesj/openobject-addons/c2c_budget_fixes

« back to all changes in this revision

Viewing changes to bookstore/report/purchase_order.py

  • Committer: Fabien Pinckaers
  • Date: 2008-04-08 18:09:25 UTC
  • Revision ID: fp@tinyerp.com-75a1f91ae9874435f54acf8495ef157f9f9cdbad
New module: bookstore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
# Copyright (c) 2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
5
#
 
6
# WARNING: This program as such is intended to be used by professional
 
7
# programmers who take the whole responsability of assessing all potential
 
8
# consequences resulting from its eventual inadequacies and bugs
 
9
# End users who are looking for a ready-to-use solution with commercial
 
10
# garantees and support are strongly adviced to contract a Free Software
 
11
# Service Company
 
12
#
 
13
# This program is Free Software; you can redistribute it and/or
 
14
# modify it under the terms of the GNU General Public License
 
15
# as published by the Free Software Foundation; either version 2
 
16
# of the License, or (at your option) any later version.
 
17
#
 
18
# This program is distributed in the hope that it will be useful,
 
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
# GNU General Public License for more details.
 
22
#
 
23
# You should have received a copy of the GNU General Public License
 
24
# along with this program; if not, write to the Free Software
 
25
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
26
#
 
27
##############################################################################
 
28
 
 
29
import time
 
30
from report import report_sxw
 
31
from osv import osv
 
32
import pooler
 
33
 
 
34
 
 
35
class purchase_order(report_sxw.rml_parse):
 
36
        def __init__(self, cr, uid, name, context):
 
37
                super(purchase_order, self).__init__(cr, uid, name, context)
 
38
                self.localcontext.update({
 
39
                        'time': time,
 
40
                        'get_line_tax': self._get_line_tax,
 
41
                        'get_tax': self._get_tax,
 
42
                        'editor' : self.editor,
 
43
                        'purchase' : self.purchase,
 
44
                        'author':self.author,
 
45
                })
 
46
 
 
47
        def editor(self,o_id):
 
48
                self.cr.execute("select distinct p.editor from purchase_order_line pl left join product_product p on (pl.product_id=p.id) where pl.order_id=%d", (o_id,))
 
49
                res = map(lambda x: x[0], self.cr.fetchall())
 
50
                editor = []
 
51
                for r in res:
 
52
                        if r:
 
53
                                p = self.pool.get('res.partner').browse(self.cr, self.uid, r)
 
54
                                editor.append({'id':p.id,'name':p.name})
 
55
                        else:
 
56
                                editor.append({'id':False,'name':''})
 
57
                return editor
 
58
 
 
59
        def purchase(self, editor_id, o_id):
 
60
                if editor_id:
 
61
                        self.cr.execute("SELECT PL.id  FROM purchase_order_line PL,product_product P   where P.product_tmpl_id=PL.product_id and PL.order_id=%d and P.editor=%d", (o_id,editor_id or None))
 
62
                else:
 
63
                        self.cr.execute("SELECT PL.id  FROM purchase_order_line PL,product_product P   where P.product_tmpl_id=PL.product_id and PL.order_id=%d and P.editor is NULL", (o_id,))
 
64
                res = self.cr.fetchall()
 
65
                line_id = map(lambda x: x[0], res)
 
66
                line = self.pool.get('purchase.order.line').browse(self.cr,self.uid,line_id)
 
67
                return line
 
68
 
 
69
        def author(self,product_id):
 
70
 
 
71
                author_id = self.pool.get('product.product').read(self.cr,self.uid,[product_id])[0];
 
72
 
 
73
                author_name = self.pool.get('library.author').read(self.cr,self.uid,author_id['author_ids']);
 
74
 
 
75
                name_list=[];
 
76
 
 
77
                for a_name in author_name:
 
78
                        name_list.append(a_name['name']);
 
79
 
 
80
                return ', '.join(name_list)
 
81
        # end def
 
82
 
 
83
        def _get_line_tax(self, line_obj):
 
84
                self.cr.execute("SELECT tax_id FROM purchase_order_taxe WHERE order_line_id=%d" % (line_obj.id))
 
85
                res = self.cr.fetchall() or None
 
86
                if not res:
 
87
                        return ""
 
88
                if isinstance(res, list):
 
89
                        tax_ids = [t[0] for t in res]
 
90
                else:
 
91
                        tax_ids = res[0]
 
92
                res = [tax.name for tax in pooler.get_pool(cr.dbname).get('account.tax').browse(self.cr, self.uid, tax_ids)]
 
93
                return ",\n ".join(res)
 
94
 
 
95
        def _get_tax(self, order_obj):
 
96
                self.cr.execute("SELECT DISTINCT tax_id FROM purchase_order_taxe, purchase_order_line, purchase_order \
 
97
                        WHERE (purchase_order_line.order_id=purchase_order.id) AND (purchase_order.id=%d)" % (order_obj.id))
 
98
                res = self.cr.fetchall() or None
 
99
                if not res:
 
100
                        return []
 
101
                if isinstance(res, list):
 
102
                        tax_ids = [t[0] for t in res]
 
103
                else:
 
104
                        tax_ids = res[0]
 
105
                tax_obj = pooler.get_pool(cr.dbname).get('account.tax')
 
106
                res = []
 
107
                for tax in tax_obj.browse(self.cr, self.uid, tax_ids):
 
108
                        self.cr.execute("SELECT DISTINCT order_line_id FROM purchase_order_line, purchase_order_taxe \
 
109
                                WHERE (purchase_order_taxe.tax_id=%d) AND (purchase_order_line.order_id=%d)" % (tax.id, order_obj.id))
 
110
                        lines = self.cr.fetchall() or None
 
111
                        if lines:
 
112
                                if isinstance(lines, list):
 
113
                                        line_ids = [l[0] for l in lines]
 
114
                                else:
 
115
                                        line_ids = lines[0]
 
116
                                base = 0
 
117
                                for line in pooler.get_pool(cr.dbname).get('purchase.order.line').browse(self.cr, self.uid, line_ids):
 
118
                                        base += line.price_subtotal
 
119
                                res.append({'code':tax.name,
 
120
                                        'base':base,
 
121
                                        'amount':base*tax.amount})
 
122
                return res
 
123
 
 
124
report_sxw.report_sxw('report.purchase.order.bookstore','purchase.order','addons/bookstore/report/purchase_order.rml',parser=purchase_order)