1
# -*- coding: utf-8 -*-
2
##############################################################################
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>).
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.
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.
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/>.
21
##############################################################################
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
29
class Parser(accountReportbase):
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)
36
self.localcontext.update({
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,
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')
52
#Get taxes order by name
53
def get_order_taxes(self, data):
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)
60
return self.get_taxes(data) #browse record list
62
#Extract ids from taxes
63
def get_taxes_ids(self, data):
65
if self.get_taxes(data):
66
for tax in self.get_taxes(data):
70
def get_tax_name(self, tax_id):
71
return self.pool.get('account.tax').browse(self.cr, self.uid, tax_id).name
73
def get_tax_type(self, tax_id):
75
tax = self.pool.get('account.tax').browse(self.cr, self.uid, tax_id)
77
if tax.type == 'percent' or tax.type == 'fixed':
78
name = tax.name + ' - ' + str(tax.amount) + '%'
84
#================ DISPLAY DATA ===========================#
85
def display_taxes(self, data):
88
if not self.get_taxes(data): #It means that non-one was selected
91
taxes = self.get_taxes(data)
93
name += tax.name + ', '
97
def get_invoices(self, tax_id, data):
98
account_invoice_obj = self.pool.get('account.invoice')
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))
107
tuple = ('invoice_line.invoice_line_tax_id', 'in', tax_id)
110
tuple = ('state', '!=', 'draft')
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)
119
1. Create a structure like this:
120
tax_block[tax_id] = {} (invoices)
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
126
So, iterate dictionary like this
128
for tax_id, invoice in tax_block.iteritems():
130
for invoices in invoices:
131
for line in invoices['lines']:
132
(line as a dictionary)
135
def get_invoices_lines_to_process(self, tax_id, data):
144
for invoice in self.get_invoices(tax_id, data):
147
qty_lines = 0 #this variable is per invoice
149
for line in invoice.invoice_line:
151
for tax in line.invoice_line_tax_id:
154
invoice_dict[invoice.id] = {'lines':[],}
157
if len(invoice_dict[invoice.id]['lines']) > 0:
158
for dict in invoice_dict['lines']:
159
if line.id == dict['id']:
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)
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
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)
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
183
invoice_dict[invoice.id]['lines'].append(line_info)
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)
193
list.append(invoice_dict)
196
tax_block[tax_id] = {}
197
tax_block[tax_id].update({'invoices': list})
201
#========== COMPUTE DATA
202
def get_price_subtotal_not_discounted_per_invoice(self, invoice):
203
price_subtotal_not_discount = 0.0
205
for line in invoice['lines']:
206
price_subtotal_not_discount += line['price_sub_not_dis']
208
return price_subtotal_not_discount
210
def get_price_subtotal_per_invoice(self, invoice):
213
for line in invoice['lines']:
214
price_subtotal += line['price_subtotal'][0]
216
return price_subtotal
218
def get_discount_per_invoice(self, invoice):
221
price_subtotal_not_discount = 0.0
223
price_subtotal_not_discount = self.get_price_subtotal_not_discounted_per_invoice(invoice)
224
price_subtotal = self.get_price_subtotal_per_invoice(invoice)
226
discount = price_subtotal_not_discount - price_subtotal
230
def get_taxes_per_invoice(self, invoice):
233
for line in invoice['lines']:
234
taxes += line['taxes']
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)
243
dict_update = {'tax_block': tax_block,}
245
self.localcontext['storage'].update(dict_update)
248
def get_data_block_tax(self, tax_id):
252
for element in self.get_data_template('tax_block')[tax_id]['invoices']:
253
for id, invoice in element.iteritems():
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']
264
final_list.append(final_block)
b'\\ No newline at end of file'