~zaber/openobject-addons/stable_5.0-extra-addons

« back to all changes in this revision

Viewing changes to c2c_cash_based_vat_report/report/vat_report_payment_report_full.py

  • Committer: Vincent Renaville
  • Date: 2011-02-07 10:26:27 UTC
  • Revision ID: vincent.renaville@camptocamp.com-20110207102627-cco62a62mp40l85u
[ADD] c2c_cash_based_vat_report

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
#
 
3
#  Copyright (c) 2010 CamptoCamp. All rights reserved.
 
4
##############################################################################
 
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 tools import config
 
32
from tools.translate import _
 
33
 
 
34
class VATReportPaymentReportFull(report_sxw.rml_parse):
 
35
        _name = 'report.account.vat.declaration.full'
 
36
        def __init__(self, cr, uid, name, context):
 
37
                super(VATReportPaymentReportFull, self).__init__(cr, uid, name, context=context)
 
38
                self.localcontext.update({
 
39
                        'time': time,
 
40
                        'get_codes': self._get_codes,
 
41
                        'get_company': self._get_company,
 
42
                        'get_currency': self._get_currency,
 
43
                        'get_lines' : self._get_lines,
 
44
                })
 
45
        def _create_tax_code_dict_full(self,cr,uid,context):
 
46
            tax_list = {}
 
47
            if self.pool.get('account.tax.code')._get_company(cr, uid, context):
 
48
                tax_code_obj_list = self.pool.get('account.tax.code').search(cr, uid, ['company_id','=',context['company_id']])
 
49
            else:
 
50
                tax_code_obj_list = self.pool.get('account.tax.code').search(cr, uid, [])
 
51
 
 
52
            for tax_id in tax_code_obj_list:
 
53
                full_obj = { 'amount' : 0.0,
 
54
                    'invoices_link' : [],
 
55
                    }
 
56
                tax_list[tax_id] = full_obj
 
57
            ## Add Specific id to 0 for untaxes amount
 
58
            full_obj = { 'amount' : 0.0,
 
59
                'invoices_link' : [],
 
60
                }
 
61
            tax_list[0] = full_obj
 
62
            return tax_list
 
63
        ## This function compute tax based on invoice for an range of date
 
64
 
 
65
        def _construct_full_line(self,form,datee):
 
66
            ### Get all line concerned
 
67
            context = {'date_from': form['date_from'],'date_to':form['date_to'],'company_id':form['company_id']}
 
68
            tax_code_obj = self.pool.get('account.tax.code')
 
69
            ids = tax_code_obj.search(self.cr, self.uid, [('company_id','=',context['company_id'])])
 
70
            tax_obj = self.pool.get('account.tax.code')
 
71
            thier_lines = tax_obj.getthirdpartylines(self.cr,self.uid,ids,datee,context)
 
72
            tax_dict = self._create_tax_code_dict_full(self.cr, self.uid, context)
 
73
            for line in thier_lines:
 
74
                ## Compute payment for this line
 
75
                if line['debit'] != 0.0:
 
76
                    tmp_base_amount = abs(line['debit'])
 
77
                else:
 
78
                    tmp_base_amount = abs(line['credit'])
 
79
 
 
80
                payment_total = tax_obj.getpaymentforaline(self.cr, self.uid, line,datee)
 
81
                prorata = tax_obj.compute_prorate(tmp_base_amount,payment_total)
 
82
                for prodline  in tax_obj.getlinefortax(self.cr,self.uid,context,line):
 
83
                    if prodline['debit']  != 0.0:
 
84
                        amount_tax = 0 - abs(prodline['debit'])
 
85
                    else:
 
86
                        amount_tax = abs(prodline['credit'])
 
87
                    ## Get invoice for move_if one
 
88
                    invoice_obj = self.pool.get('account.invoice')
 
89
                    invoice_ids = invoice_obj.search(self.cr,self.uid,[('move_id','=',prodline['move_id'])])
 
90
                    if invoice_ids:
 
91
                        invoice_number = invoice_obj.browse(self.cr,self.uid,invoice_ids)[0].number
 
92
                    else:
 
93
                        invoice_number = ''
 
94
                    ##
 
95
                    ## Get partner name for move_if one
 
96
                    partner_obj = self.pool.get('res.partner')
 
97
                    if prodline['partner_id']:
 
98
                        partner_name = partner_obj.browse(self.cr,self.uid,[prodline['partner_id']])[0].name
 
99
                    else:
 
100
                        partner_name = ''
 
101
                    ##
 
102
                    ## Get move name for move_if one
 
103
                    move_obj = self.pool.get('account.move')
 
104
                    if prodline['move_id']:
 
105
                        move_name = move_obj.browse(self.cr,self.uid,[prodline['move_id']])[0].name
 
106
                    else:
 
107
                        move_name = ''
 
108
                    ##
 
109
                    prod_val = { 'name' : prodline['name'],
 
110
                    'move_name' : move_name,
 
111
                    'prorat_amount' : round(amount_tax * prorata, int(config['price_accuracy'])),
 
112
                    'prorata': str(round(prorata * 100, int(config['price_accuracy']))) + '%',
 
113
                    'invoice' : invoice_number,
 
114
                    'partner' : partner_name
 
115
                    }
 
116
                    prodline['vat_prorata_amount'] =  amount_tax * prorata
 
117
                    if tax_dict.has_key(prodline['tax_code_id']) :
 
118
                        tax_dict[prodline['tax_code_id']]['amount'] = tax_dict[prodline['tax_code_id']]['amount'] + amount_tax * prorata
 
119
                        tax_dict[prodline['tax_code_id']]['invoices_link'].append(prod_val)
 
120
                    else:
 
121
                        tax_dict[0]['amount'] = tax_dict[0]['amount'] + amount_tax * prorata
 
122
                        tax_dict[0]['invoices_link'].append(prod_val)
 
123
            for record in tax_code_obj.browse(self.cr, self.uid, ids, context):
 
124
                def _rec_get(record):
 
125
                    if tax_dict.has_key(record.id):
 
126
                        amount =  tax_dict[record.id]['amount']
 
127
                    else:
 
128
                        amount = 0.0
 
129
                    for rec in record.child_ids:
 
130
                        amount += _rec_get(rec) * rec.sign
 
131
                    return amount
 
132
                tax_dict[record.id]['amount'] = round(_rec_get(record), int(config['price_accuracy']))
 
133
 
 
134
            return tax_dict
 
135
        def _get_lines(self,form,company_id=False, parent=False, level=0):
 
136
            ### Get Result
 
137
            list_code = self._get_codes(company_id, parent, level)
 
138
            tc = self.pool.get('account.tax.code')
 
139
            if form['date_to_from'] == 'date_to':
 
140
                ThirdParty = self._construct_full_line(form,form['date_to'])
 
141
            else:
 
142
                ThirdParty = self._construct_full_line(form,form['date_from'])
 
143
            result = []
 
144
            for id_tab in list_code:
 
145
                ##
 
146
                tax_info = tc.browse(self.cr, self.uid, id_tab[1])
 
147
                #print str(list_code[id])
 
148
                res_pot = {
 
149
                   'name':tax_info.name,
 
150
                   'level' : id_tab[0],
 
151
                   'amount':ThirdParty[id_tab[1]]['amount'],
 
152
                   'invoice_link' :ThirdParty[id_tab[1]]['invoices_link']
 
153
                }
 
154
                result.append(res_pot)
 
155
                ##
 
156
            ## Add amount without tax
 
157
            res_pot_without_tax = {
 
158
                   'name':_('Amount without VAT'),
 
159
                   'level' : id_tab[0],
 
160
                   'amount': ThirdParty[0]['amount'],
 
161
                   'invoice_link' :ThirdParty[0]['invoices_link']
 
162
                }
 
163
            result.append(res_pot_without_tax)
 
164
 
 
165
            return result
 
166
 
 
167
 
 
168
        def _get_codes(self, company_id, parent=False, level=0):
 
169
                tc = self.pool.get('account.tax.code')
 
170
                ids = tc.search(self.cr, self.uid, [('parent_id','=',parent),('company_id','=',company_id)])
 
171
 
 
172
                res = []
 
173
                for code in tc.browse(self.cr, self.uid, ids):
 
174
                        res.append(('.'*2*level,code.id))
 
175
 
 
176
                        res += self._get_codes(company_id, code.id, level+1)
 
177
                return res
 
178
 
 
179
 
 
180
        def _get_company(self, form):
 
181
                return pooler.get_pool(self.cr.dbname).get('res.company').browse(self.cr, self.uid, form['company_id']).name
 
182
 
 
183
        def _get_currency(self, form):
 
184
                return pooler.get_pool(self.cr.dbname).get('res.company').browse(self.cr, self.uid, form['company_id']).currency_id.name
 
185
 
 
186
 
 
187
 
 
188
 
 
189
report_sxw.report_sxw('report.account.vat.payment.declaration.full', 'account.tax.code',
 
190
        'addons/c2c_vat_report_payment/report/vat_report_payment_full.rml', parser=VATReportPaymentReportFull, header=False)
 
191