Bugfix[375034]:account_invoice_layout
[odoo/odoo.git] / addons / account_invoice_layout / report / report_account_invoice_layout.py
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 import time
23 import pooler
24 from report import report_sxw
25
26 class account_invoice_1(report_sxw.rml_parse):
27     def __init__(self, cr, uid, name, context):
28         super(account_invoice_1, self).__init__(cr, uid, name, context)
29         self.localcontext.update({
30             'time': time,
31             'invoice_lines': self.invoice_lines,
32         })
33         self.context = context
34         self._node = None
35
36     def invoice_lines(self,invoice):
37         result =[]
38         sub_total={}
39         info=[]
40         invoice_list=[]
41         res={}
42         list_in_seq={}
43         ids = self.pool.get('account.invoice.line').search(self.cr, self.uid, [('invoice_id', '=', invoice.id)])
44         ids.sort()
45         for id in range(0,len(ids)):
46             info = self.pool.get('account.invoice.line').browse(self.cr, self.uid,ids[id], self.context.copy())
47             list_in_seq[info]=info.sequence
48         i=1
49         j=0
50         final=sorted(list_in_seq.items(), lambda x, y: cmp(x[1], y[1]))
51         invoice_list=[x[0] for x in final]
52         sum_flag={}
53         sum_flag[j]=-1
54         for entry in invoice_list:
55             res={}
56             if entry.state=='article':
57                 self.cr.execute('select tax_id from account_invoice_line_tax where invoice_line_id=%s', (entry.id,))
58                 tax_ids=self.cr.fetchall()
59                 if tax_ids==[]:
60                     res['tax_types']=''
61                 else:
62                     tax_names_dict={}
63                     for item in range(0,len(tax_ids))    :
64                         self.cr.execute('select name from account_tax where id=%s', (tax_ids[item][0],))
65                         type=self.cr.fetchone()
66                         tax_names_dict[item] =type[0]
67                     tax_names = ','.join([tax_names_dict[x] for x in range(0,len(tax_names_dict))])
68                     res['tax_types']=tax_names
69                 res['name']=entry.name
70                 res['quantity']="%.2f"%(entry.quantity)
71                 res['price_unit']="%.2f"%(entry.price_unit)
72                 res['discount']="%.2f"%(entry.discount)
73                 res['price_subtotal']="%.2f"%(entry.price_subtotal)
74                 sub_total[i]=entry.price_subtotal
75                 i=i+1
76                 res['note']=entry.note
77                 res['currency']=invoice.currency_id.code
78                 res['type']=entry.state
79
80                 if entry.uos_id.id==False:
81                     res['uos']=''
82                 else:
83                     uos_name = self.pool.get('product.uom').read(self.cr,self.uid,entry.uos_id.id,['name'],self.context.copy())
84                     res['uos'] = uos_name['name']
85             else:
86
87                 res['quantity']=''
88                 res['price_unit']=''
89                 res['discount']=''
90                 res['tax_types']=''
91                 res['type']=entry.state
92                 res['note']=entry.note
93                 res['uos']=''
94
95                 if entry.state=='subtotal':
96                     res['name']=entry.name
97                     sum=0
98                     sum_id=0
99                     if sum_flag[j]==-1:
100                         temp=1
101                     else:
102                         temp=sum_flag[j]
103
104                     for sum_id in range(temp,len(sub_total)+1):
105                         sum+=sub_total[sum_id]
106                     sum_flag[j+1]= sum_id +1
107
108                     j=j+1
109                     res['price_subtotal']="%.2f"%(sum)
110                     res['currency']=invoice.currency_id.code
111                     res['quantity']=''
112                     res['price_unit']=''
113                     res['discount']=''
114                     res['tax_types']=''
115                     res['uos']=''
116                 elif entry.state=='title':
117                     res['name']=entry.name
118                     res['price_subtotal']=''
119                     res['currency']=''
120                 elif entry.state=='text':
121                     res['name']=entry.name
122                     res['price_subtotal']=''
123                     res['currency']=''
124                 elif entry.state=='line':
125                     res['quantity']='_______________'
126                     res['price_unit']='______________'
127                     res['discount']='____________'
128                     res['tax_types']='____________________'
129                     res['uos']='_____'
130                     res['name']='_______________________________________________'
131                     res['price_subtotal']='____________'
132                     res['currency']='____'
133                 elif entry.state=='break':
134                     res['type']=entry.state
135                     res['name']=entry.name
136                     res['price_subtotal']=''
137                     res['currency']=''
138                 else:
139                     res['name']=entry.name
140                     res['price_subtotal']=''
141                     res['currency']=invoice.currency_id.code
142
143             result.append(res)
144         return result
145 report_sxw.report_sxw('report.account.invoice.layout', 'account.invoice', 'addons/account_invoice_layout/report/report_account_invoice_layout.rml', parser=account_invoice_1)
146 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
147