~gs.clearcorp/openerp-ccorp-addons/7.0_account_banking_ccorp

« back to all changes in this revision

Viewing changes to account_invoicing_report/report/account_invoicing_report.py

  • Committer: Ronald Rubi
  • Date: 2014-06-19 14:51:01 UTC
  • mfrom: (880.1.1 7.0-ven012)
  • Revision ID: ronald.rubi@clearcorp.co.cr-20140619145101-3of28ln9ye710e3v
[MRG] Add account_invoicing_report module.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Addons modules by CLEARCORP S.A.
 
6
#    Copyright (C) 2009-TODAY CLEARCORP S.A. (<http://clearcorp.co.cr>).
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU Affero General Public License as
 
10
#    published by the Free Software Foundation, either version 3 of the
 
11
#    License, or (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 Affero General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU Affero General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
import pooler
 
24
from report import report_sxw
 
25
from tools.translate import _
 
26
from openerp.addons.account_report_lib.account_report_base import accountReportbase
 
27
from numpy.ma.core import ids
 
28
 
 
29
class Parser(accountReportbase):
 
30
 
 
31
    def __init__(self, cr, uid, name, context):
 
32
        super(Parser, self).__init__(cr, uid, name, context=context)
 
33
        self.pool = pooler.get_pool(self.cr.dbname)
 
34
        self.cursor = self.cr
 
35
 
 
36
        self.localcontext.update({
 
37
           'cr' : cr,
 
38
           'uid': uid,
 
39
           'storage':{},
 
40
           'display_taxes':self.display_taxes,
 
41
           'get_order_taxes':self.get_order_taxes,
 
42
           'get_tax_name': self.get_tax_name, 
 
43
           'get_data_block_tax': self.get_data_block_tax, 
 
44
           'get_tax_type': self.get_tax_type, 
 
45
        })
 
46
    
 
47
    #---------SET AND GET DATA ----------#
 
48
    # Get taxes from wizard
 
49
    def get_taxes(self, data):
 
50
        return self._get_info(data,'account_tax_ids', 'account.tax')
 
51
    
 
52
    #Get taxes order by name
 
53
    def get_order_taxes(self, data):
 
54
        taxes_ids = []
 
55
        
 
56
        if not self.get_taxes(data):
 
57
            taxes_ids = self.pool.get('account.tax').search(self.cr, self.uid, [], order="name ASC")
 
58
            return self.pool.get('account.tax').browse(self.cr, self.uid, taxes_ids)
 
59
        else:
 
60
           return self.get_taxes(data) #browse record list
 
61
    
 
62
    #Extract ids from taxes
 
63
    def get_taxes_ids(self, data):
 
64
        ids = []
 
65
        if self.get_taxes(data):
 
66
            for tax in self.get_taxes(data):
 
67
                ids.append(tax.id)
 
68
        return ids
 
69
    
 
70
    def get_tax_name(self, tax_id):
 
71
        return self.pool.get('account.tax').browse(self.cr, self.uid, tax_id).name
 
72
    
 
73
    def get_tax_type(self, tax_id):
 
74
        name = ''
 
75
        tax = self.pool.get('account.tax').browse(self.cr, self.uid, tax_id)
 
76
        
 
77
        if tax.type == 'percent' or tax.type == 'fixed':
 
78
             name = tax.name + ' - ' + str(tax.amount) + '%'
 
79
        else:
 
80
            name = tax.name
 
81
            
 
82
        return name
 
83
    
 
84
    #================ DISPLAY DATA ===========================#
 
85
    def display_taxes(self, data):
 
86
       name = ''
 
87
       
 
88
       if not self.get_taxes(data): #It means that non-one was selected
 
89
           return _('All taxes')
 
90
       else:
 
91
           taxes = self.get_taxes(data)
 
92
           for tax in taxes:
 
93
               name += tax.name + ', '
 
94
           return name  
 
95
    
 
96
    #========= BUILD DATA
 
97
    def get_invoices(self, tax_id, data):
 
98
        account_invoice_obj = self.pool.get('account.invoice')
 
99
        result={}
 
100
        domain = []
 
101
        
 
102
        date_from = self.get_date_from(data)
 
103
        date_to = self.get_date_to(data)        
 
104
        domain.append(('date_invoice', '>=', date_from))
 
105
        domain.append(('date_invoice', '<=', date_to))
 
106
 
 
107
        tuple = ('invoice_line.invoice_line_tax_id', 'in', tax_id)
 
108
        domain.append(tuple)
 
109
        
 
110
        tuple = ('state', '!=', 'draft')
 
111
        domain.append(tuple)
 
112
        
 
113
        invoices_ids = account_invoice_obj.search(self.cr, self.uid, domain, context=None)
 
114
        invoices_obj = account_invoice_obj.browse(self.cr, self.uid, invoices_ids, context=None)
 
115
        
 
116
        return invoices_obj
 
117
    
 
118
    """
 
119
        1. Create a structure like this:
 
120
            tax_block[tax_id] = {} (invoices)
 
121
            
 
122
            Then invoices is a dictionary, where key is id invoice and each key
 
123
            has a list with lines that its tax match with tax_id that is passed
 
124
            as a parameter
 
125
            
 
126
            So, iterate dictionary like this
 
127
            
 
128
            for tax_id, invoice in tax_block.iteritems():
 
129
                tax_id ...
 
130
                for invoices in invoices:
 
131
                    for line in invoices['lines']:
 
132
                        (line as a dictionary)        
 
133
    """
 
134
    
 
135
    def get_invoices_lines_to_process(self, tax_id, data):
 
136
        line_info = {}
 
137
        invoice_dict = {}
 
138
        tax_block = {}     
 
139
        qty_lines = 0   
 
140
        list = []
 
141
        
 
142
        add_invoice = False
 
143
        
 
144
        for invoice in self.get_invoices(tax_id, data):
 
145
            line_info = {}
 
146
            invoice_dict = {}
 
147
            qty_lines = 0 #this variable is per invoice
 
148
            
 
149
            for line in invoice.invoice_line:
 
150
                added_line = False
 
151
                for tax in line.invoice_line_tax_id:
 
152
                    if tax.id == tax_id:               
 
153
                        qty_lines += 1
 
154
                        invoice_dict[invoice.id] = {'lines':[],}       
 
155
                        add_invoice = True     
 
156
                        
 
157
                        if len(invoice_dict[invoice.id]['lines']) > 0:
 
158
                            for dict in invoice_dict['lines']:
 
159
                                if line.id == dict['id']:
 
160
                                    added_line = True
 
161
                            
 
162
                            if not added_line:
 
163
                                #compute taxes
 
164
                                price = line.price_unit * (1-(line.discount or 0.0)/100.0)  
 
165
                                amount_tax = self.pool.get('account.tax').compute_all(self.cr, self.uid, [tax], price, line.quantity, product=line.product_id, partner=line.invoice_id.partner_id)
 
166
                                
 
167
                                line_info['id'] = line.id
 
168
                                line_info['price_sub_not_dis'] = line.price_subtotal_not_discounted
 
169
                                line_info['price_subtotal'] = line.price_subtotal, 
 
170
                                line_info['taxes'] = amount_tax['total_included'] - line.price_subtotal
 
171
                            
 
172
                        else:
 
173
                            #compute taxes
 
174
                            price = line.price_unit * (1-(line.discount or 0.0)/100.0)  
 
175
                            #amount_tax is a dictionary
 
176
                            amount_tax = self.pool.get('account.tax').compute_all(self.cr, self.uid, [tax], price,  line.quantity, product=line.product_id, partner=line.invoice_id.partner_id)
 
177
                            
 
178
                            line_info['id'] = line.id
 
179
                            line_info['price_sub_not_dis'] = line.price_subtotal_not_discounted
 
180
                            line_info['price_subtotal'] = line.price_subtotal,
 
181
                            line_info['taxes'] = amount_tax['total_included'] - line.price_subtotal
 
182
                            
 
183
                        invoice_dict[invoice.id]['lines'].append(line_info)                
 
184
                        
 
185
            
 
186
            if add_invoice:
 
187
                 invoice_dict[invoice.id].update({
 
188
                                   'number': invoice.number, 
 
189
                                   'client': invoice.partner_id.name or '', 
 
190
                                   'qty_lines': qty_lines,
 
191
                                   'qty_lines_total': len(invoice.invoice_line)
 
192
                                   })
 
193
                 list.append(invoice_dict)
 
194
                 add_invoice = False
 
195
                
 
196
        tax_block[tax_id] = {}
 
197
        tax_block[tax_id].update({'invoices': list})
 
198
        
 
199
        return tax_block
 
200
    
 
201
    #========== COMPUTE DATA
 
202
    def get_price_subtotal_not_discounted_per_invoice(self, invoice):
 
203
        price_subtotal_not_discount =  0.0
 
204
    
 
205
        for line in invoice['lines']:
 
206
            price_subtotal_not_discount += line['price_sub_not_dis']
 
207
    
 
208
        return price_subtotal_not_discount
 
209
    
 
210
    def get_price_subtotal_per_invoice(self, invoice):
 
211
        price_subtotal =  0.0        
 
212
        
 
213
        for line in invoice['lines']:
 
214
            price_subtotal += line['price_subtotal'][0]
 
215
        
 
216
        return price_subtotal
 
217
    
 
218
    def get_discount_per_invoice(self, invoice):
 
219
        discount = 0.0
 
220
        price_subtotal =  0.0
 
221
        price_subtotal_not_discount =  0.0
 
222
        
 
223
        price_subtotal_not_discount = self.get_price_subtotal_not_discounted_per_invoice(invoice)
 
224
        price_subtotal = self.get_price_subtotal_per_invoice(invoice)
 
225
        
 
226
        discount = price_subtotal_not_discount -  price_subtotal
 
227
        
 
228
        return discount
 
229
    
 
230
    def get_taxes_per_invoice(self, invoice):
 
231
        taxes =  0.0        
 
232
        
 
233
        for line in invoice['lines']:
 
234
            taxes += line['taxes']
 
235
        
 
236
        return taxes
 
237
    
 
238
    #============== FINAL FUNCTION
 
239
    def set_data_template(self, tax_id, data):        
 
240
        #Invoices and lines are processing in this function 
 
241
        tax_block = self.get_invoices_lines_to_process(tax_id, data)      
 
242
        
 
243
        dict_update = {'tax_block': tax_block,}       
 
244
        
 
245
        self.localcontext['storage'].update(dict_update)
 
246
        return False
 
247
    
 
248
    def get_data_block_tax(self, tax_id):        
 
249
        final_block = {}
 
250
        final_list = []  
 
251
  
 
252
        for element in self.get_data_template('tax_block')[tax_id]['invoices']:
 
253
            for id, invoice in element.iteritems():
 
254
                final_block = {}
 
255
                final_block['invoice_number'] = invoice['number']
 
256
                final_block['client'] = invoice['client']
 
257
                final_block['qty_lines'] = str(invoice['qty_lines']) + "/" + str(invoice['qty_lines_total'])                
 
258
                final_block['subtotal_without_dis'] = self.get_price_subtotal_not_discounted_per_invoice(invoice)
 
259
                final_block['discount'] = self.get_discount_per_invoice(invoice)
 
260
                final_block['subtotal_dis'] = self.get_price_subtotal_per_invoice(invoice)
 
261
                final_block['taxes'] = self.get_taxes_per_invoice(invoice)
 
262
                final_block['total'] = final_block['subtotal_dis'] + final_block['taxes']
 
263
                
 
264
                final_list.append(final_block)
 
265
        
 
266
        return final_list
 
 
b'\\ No newline at end of file'