Bugfix:partner_ledger report when no records exists
[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 parents = {
27     'tr':1,
28     'li':1,
29     'story': 0,
30     'section': 0
31     }
32
33 class account_invoice_1(report_sxw.rml_parse):
34     def __init__(self, cr, uid, name, context):
35         super(account_invoice_1, self).__init__(cr, uid, name, context)
36         self.localcontext.update({
37             'time': time,
38             'invoice_lines': self.invoice_lines,
39             'repeat_In':self.repeat_In,
40         })
41         self.context = context
42
43     def repeat_In(self, lst, name, nodes_parent=False,td=False,width=[],value=[],type=[]):
44         self._node.data = ''
45         node = self._find_parent(self._node, nodes_parent or parents)
46         ns = node.nextSibling
47
48         value=['tax_types','quantity','uos','price_unit','discount','price_subtotal','currency']
49         type=['string','string','string','string','string','string','string']
50         width=[62,38,29,67,53,53,27]
51         td=7
52
53         tableFlag=0
54
55         if not lst:
56             lst.append(1)
57         for ns in node.childNodes :
58             if tableFlag==1:
59                 break
60             if ns and ns.nodeName!='#text' and ns.tagName=='blockTable' and td :
61                 tableFlag=1
62
63                 width_str = ns._attrs['colWidths'].nodeValue
64                 ns.removeAttribute('colWidths')
65                 total_td = td * len(value)
66
67                 if not width:
68                     for v in value:
69                         width.append(30)
70                 for v in range(len(value)):
71                     width_str +=',%d'%width[v]
72
73                 ns.setAttribute('colWidths',width_str)
74
75                 child_list =  ns.childNodes
76
77                 for child in child_list:
78                     if child.nodeName=='tr':
79                         lc = child.childNodes[1]
80 #                        for t in range(td):
81                         i=0
82                         for v in value:
83                             t2="[[%s['type']=='text' and removeParentNode('tr')]]"%(name)
84                             t1= "[[ %s['%s'] ]]"%(name,v)
85                             t3="[[ %s['type']=='subtotal' and ( setTag('para','para',{'fontName':'Times-bold'})) ]]"%name
86                             newnode = lc.cloneNode(1)
87
88                             newnode.childNodes[1].lastChild.data = t1 + t2 +t3
89 #                           newnode.childNodes[1].lastChild.data=[[ a['status']==1 and ( setTag('para','para',{'fontName':'Times-bold'})) ]]
90                             child.appendChild(newnode)
91                             newnode=False
92                             i+=1
93
94
95         return super(account_invoice_1,self).repeatIn(lst, name, nodes_parent=False)
96
97     def invoice_lines(self,invoice):
98
99         result =[]
100         sub_total={}
101         info=[]
102         invoice_list=[]
103         res={}
104         list_in_seq={}
105         ids = self.pool.get('account.invoice.line').search(self.cr, self.uid, [('invoice_id', '=', invoice.id)])
106         ids.sort()
107         for id in range(0,len(ids)):
108             info = self.pool.get('account.invoice.line').browse(self.cr, self.uid,ids[id], self.context.copy())
109             list_in_seq[info]=info.sequence
110 #            invoice_list +=[info]
111         i=1
112         j=0
113         final=sorted(list_in_seq.items(), lambda x, y: cmp(x[1], y[1]))
114         invoice_list=[x[0] for x in final]
115         sum_flag={}
116         sum_flag[j]=-1
117         for entry in invoice_list:
118             res={}
119
120             if entry.state=='article':
121                 self.cr.execute('select tax_id from account_invoice_line_tax where invoice_line_id=%s', (entry.id,))
122                 tax_ids=self.cr.fetchall()
123
124                 if tax_ids==[]:
125                     res['tax_types']=''
126                 else:
127                     tax_names_dict={}
128                     for item in range(0,len(tax_ids))    :
129                         self.cr.execute('select name from account_tax where id=%s', (tax_ids[item][0],))
130                         type=self.cr.fetchone()
131                         tax_names_dict[item] =type[0]
132                     tax_names = ','.join([tax_names_dict[x] for x in range(0,len(tax_names_dict))])
133                     res['tax_types']=tax_names
134                 res['name']=entry.name
135                 res['quantity']="%.2f"%(entry.quantity)
136                 res['price_unit']="%.2f"%(entry.price_unit)
137                 res['discount']="%.2f"%(entry.discount)
138                 res['price_subtotal']="%.2f"%(entry.price_subtotal)
139                 sub_total[i]=entry.price_subtotal
140                 i=i+1
141                 res['note']=entry.note
142                 res['currency']=invoice.currency_id.code
143                 res['type']=entry.state
144
145                 if entry.uos_id.id==False:
146                     res['uos']=''
147                 else:
148                     uos_name = self.pool.get('product.uom').read(self.cr,self.uid,entry.uos_id.id,['name'],self.context.copy())
149                     res['uos'] = uos_name['name']
150             else:
151
152                 res['quantity']=''
153                 res['price_unit']=''
154                 res['discount']=''
155                 res['tax_types']=''
156                 res['type']=entry.state
157                 res['note']=entry.note
158                 res['uos']=''
159
160                 if entry.state=='subtotal':
161                     res['name']=entry.name
162                     sum=0
163                     sum_id=0
164                     if sum_flag[j]==-1:
165                         temp=1
166                     else:
167                         temp=sum_flag[j]
168
169                     for sum_id in range(temp,len(sub_total)+1):
170                         sum+=sub_total[sum_id]
171                     sum_flag[j+1]= sum_id +1
172
173                     j=j+1
174                     res['price_subtotal']="%.2f"%(sum)
175                     res['currency']=invoice.currency_id.code
176                     res['quantity']=''
177                     res['price_unit']=''
178                     res['discount']=''
179                     res['tax_types']=''
180                     res['uos']=''
181                 elif entry.state=='title':
182                     res['name']=entry.name
183                     res['price_subtotal']=''
184                     res['currency']=''
185                 elif entry.state=='text':
186                     res['name']=entry.name
187                     res['price_subtotal']=''
188                     res['currency']=''
189                 elif entry.state=='line':
190                     res['quantity']='____________'
191                     res['price_unit']='______________'
192                     res['discount']='____________'
193                     res['tax_types']='_________________'
194                     res['uos']='_____'
195                     res['name']='________________________________________'
196                     res['price_subtotal']='_______________________'
197                     res['currency']='_______'
198                 elif entry.state=='break':
199                     res['type']=entry.state
200                     res['name']=entry.name
201                     res['price_subtotal']=''
202                     res['currency']=''
203                 else:
204                     res['name']=entry.name
205                     res['price_subtotal']=''
206                     res['currency']=invoice.currency_id.code
207
208             result.append(res)
209         return result
210 report_sxw.report_sxw('report.account.invoice.layout', 'account.invoice', 'addons/account_invoice_layout/report/report_account_invoice_layout.rml', parser=account_invoice_1)
211 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
212