~vauxoo/addons-vauxoo/6.0-trunk

« back to all changes in this revision

Viewing changes to aging_due_report/report/parser.py

  • Committer: Jose Antonio
  • Date: 2012-06-13 22:24:45 UTC
  • mto: This revision was merged to the branch mainline in revision 312.
  • Revision ID: jose@vauxoo.com-20120613222445-ybndycdaaw2puqyz

[ADD] Add wizard to add incidences from xls field

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# -*- encoding: utf-8 -*-
3
 
###########################################################################
4
 
#    Module Writen to OpenERP, Open Source Management Solution
5
 
#    Copyright (C) OpenERP Venezuela (<http://openerp.com.ve>).
6
 
#    All Rights Reserved
7
 
# Credits######################################################
8
 
#    Coded by: Humberto Arocha <hbto@vauxoo.com>
9
 
#    Planified by: Rafael Silva <rsilvam@vauxoo.com>
10
 
#    Audited by: Nhomar Hernandez <nhomar@vauxoo.com>
11
 
#############################################################################
12
 
#    This program is free software: you can redistribute it and/or modify
13
 
#    it under the terms of the GNU Affero General Public License as published by
14
 
#    the Free Software Foundation, either version 3 of the License, or
15
 
#    (at your option) any later version.
16
 
#
17
 
#    This program is distributed in the hope that it will be useful,
18
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 
#    GNU Affero General Public License for more details.
21
 
#
22
 
#    You should have received a copy of the GNU Affero General Public License
23
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 
##########################################################################
25
 
 
26
 
 
27
 
import time
28
 
from report import report_sxw
29
 
import mx.DateTime
30
 
 
31
 
 
32
 
class aging_parser(report_sxw.rml_parse):
33
 
    def __init__(self, cr, uid, name, context):
34
 
        super(aging_parser, self).__init__(cr, uid, name, context=context)
35
 
        self.localcontext.update({
36
 
            'time': time,
37
 
            'int': int,
38
 
            'get_invoice_by_partner': self._get_invoice_by_partner,
39
 
            'get_total_by_comercial': self._get_total_by_comercial,
40
 
            'get_aged_lines': self._get_aged_lines,
41
 
        })
42
 
 
43
 
    def _get_aml(self, ids):
44
 
        aml_obj = self.pool.get('account.move.line')
45
 
        res = 0.0
46
 
        if not ids:
47
 
            return res
48
 
        aml_gen = (aml_brw.debit and aml_brw.debit or aml_brw.credit *
49
 
                   -1 for aml_brw in aml_obj.browse(self.cr, self.uid, ids))
50
 
        for i in aml_gen:
51
 
            res += i
52
 
        return res
53
 
 
54
 
    def _get_total_by_comercial(self, rp_brws):
55
 
        ixp_gen = self._get_invoice_by_partner(rp_brws)
56
 
        total = 0.0
57
 
        usr_dict = {}
58
 
        for ixp in ixp_gen:
59
 
            usr_id = ixp['rp_brw'].user_id and ixp[
60
 
                'rp_brw'].user_id.id or False
61
 
 
62
 
            if usr_dict.get(usr_id):
63
 
                usr_dict[usr_id]['total'] += ixp['due_total']
64
 
            else:
65
 
                usr_dict[usr_id] = {
66
 
                    'usr_brw': ixp['rp_brw'].user_id,
67
 
                    'total': ixp['due_total']
68
 
                }
69
 
            total += ixp['due_total']
70
 
        if not total:
71
 
            return []
72
 
        return [{'total': total, 'vendor': (usr_dict[i] for i in usr_dict)}]
73
 
 
74
 
    def _get_invoice_by_partner(self, rp_brws):
75
 
        res = {}
76
 
        rp_obj = self.pool.get('res.partner')
77
 
        inv_obj = self.pool.get('account.invoice')
78
 
        mun_obj = self.pool.get('account.wh.munici.line')
79
 
        for rp_brw in rp_brws:
80
 
            inv_ids = inv_obj.search(
81
 
                self.cr, self.uid, [('partner_id', '=', rp_brw.id),
82
 
                                    ('type', '=', 'out_invoice'), ('residual', '!=', 0), ('state', 'not in', ('cancel', 'draft'))])
83
 
            if not inv_ids:
84
 
                continue
85
 
            res[rp_brw.id] = {
86
 
                'rp_brw': rp_brw,
87
 
                'inv_ids': [],
88
 
                'inv_total': 0.0,
89
 
                'wh_vat': 0.0,
90
 
                'wh_islr': 0.0,
91
 
                'wh_muni': 0.0,
92
 
                'credit_note': 0.0,
93
 
                'pay_left_total': 0.0,
94
 
                'pay_total': 0.0,
95
 
                'due_total': 0.0,
96
 
            }
97
 
            for inv_brw in inv_obj.browse(self.cr, self.uid, inv_ids):
98
 
 
99
 
                pay_ids = [aml.id for aml in inv_brw.payment_ids]
100
 
                #~ VAT
101
 
                wh_lines = []
102
 
                pay_vat_ids = []
103
 
                #~ ISLR
104
 
                wh_lines = []
105
 
                pay_islr_ids = []
106
 
               #~  MUNI
107
 
                wh_lines = []
108
 
                pay_muni_ids = []
109
 
                #~  TODO: SRC
110
 
 
111
 
                #~ N/C
112
 
                #~ refund_ids = inv_obj.search(self.cr,self.uid,[('parent_id','=',inv_brw.id),('type','=','out_refund'),('state','not in',('draft','cancel')),('move_id','!=',False)])
113
 
                #~ refund_ids = inv_obj.search(self.cr,self.uid,[('parent_id','=',inv_brw.id),('type','=','out_refund'),('state','not in',('draft','cancel')),('move_id','!=',False)])
114
 
                refund_brws = []
115
 
                #~ refund_brws = refund_ids and inv_obj.browse(self.cr,self.uid,refund_ids) or []
116
 
                #~ aml_gen = (refund_brw.move_id.line_id for refund_brw in refund_brws)
117
 
                pay_refund_ids = []
118
 
                #~ for aml_brws in aml_gen:
119
 
                    #~ pay_refund_ids += [aml.id for aml in aml_brws if aml.account_id.id == inv_brw.account_id.id]
120
 
 
121
 
                #~  TODO: N/D
122
 
                #~  ACUMULACION DE LOS NOPAGOS, OBTENCION DEL PAGO
123
 
                pay_left_ids = list(set(pay_ids).difference(
124
 
                    pay_vat_ids +
125
 
                    pay_islr_ids +
126
 
                    pay_muni_ids +
127
 
                    pay_refund_ids))
128
 
                wh_vat = self._get_aml(pay_vat_ids)
129
 
                wh_islr = self._get_aml(pay_islr_ids)
130
 
                wh_muni = self._get_aml(pay_muni_ids)
131
 
                wh_src = self._get_aml([])
132
 
                debit_note = self._get_aml([])
133
 
                credit_note = self._get_aml(pay_refund_ids)
134
 
                payment_left = self._get_aml(pay_left_ids)
135
 
                payment = wh_vat + wh_islr + wh_muni + \
136
 
                    wh_src + debit_note + credit_note + payment_left
137
 
                residual = inv_brw.amount_total + payment
138
 
                date_due = mx.DateTime.strptime(
139
 
                    inv_brw.date_due or inv_brw.date_invoice, '%Y-%m-%d')
140
 
                today = mx.DateTime.now()
141
 
                due_days = (today - date_due).day
142
 
 
143
 
                #~ TODO: Si se incluye un reporte de revisiones
144
 
                #~ no se eliminara la factura
145
 
                #~ si la factura no tiene saldo entonces
146
 
                #~ no incluirla en el reporte
147
 
                if not residual:
148
 
                    continue
149
 
 
150
 
                res[rp_brw.id]['inv_ids'].append({
151
 
                    'inv_brw': inv_brw,
152
 
                    'wh_vat': wh_vat,
153
 
                    'wh_islr': wh_islr,
154
 
                    'wh_muni': wh_muni,
155
 
                    'wh_src': wh_src,
156
 
                    'debit_note': debit_note,
157
 
                    'credit_note': credit_note,
158
 
                    'refund_brws': refund_brws,
159
 
                    'payment': payment,
160
 
                    'payment_left': payment_left,
161
 
                    'residual': residual,
162
 
                    'due_days': due_days
163
 
                })
164
 
                res[rp_brw.id]['inv_total'] += inv_brw.amount_total
165
 
                res[rp_brw.id]['wh_vat'] += wh_vat
166
 
                res[rp_brw.id]['wh_islr'] += wh_islr
167
 
                res[rp_brw.id]['wh_muni'] += wh_muni
168
 
                res[rp_brw.id]['credit_note'] += credit_note
169
 
                res[rp_brw.id]['pay_left_total'] += payment_left
170
 
                res[rp_brw.id]['pay_total'] += payment
171
 
                res[rp_brw.id]['due_total'] += residual
172
 
            #~ TODO: Report donde no se elimine esta clave del diccionario
173
 
            #~ y se use para revisiones internas
174
 
            #~ Si no tiene saldo, sacarlo del reporte
175
 
            not res[rp_brw.id]['due_total'] and res.pop(rp_brw.id)
176
 
 
177
 
        #~ ordenando los registros en orden alfabetico
178
 
        #~ si llegaran a existir
179
 
        res2 = []
180
 
        if res.keys():
181
 
            rp_ids = rp_obj.search(self.cr, self.uid, [(
182
 
                'id', 'in', res.keys())], order='name asc')
183
 
            for rp_id in rp_ids:
184
 
                res2.append(res[rp_id])
185
 
        return res2
186
 
 
187
 
    def _get_aged_lines(self, rp_brws, span=30, date_from=time.strftime('%Y-%m-%d')):
188
 
 
189
 
        if not rp_brws:
190
 
            return []
191
 
 
192
 
        ixp_gen = self._get_invoice_by_partner(rp_brws)
193
 
 
194
 
        if not ixp_gen:
195
 
            return []
196
 
 
197
 
        '''
198
 
        [
199
 
            {
200
 
            'rp_brw':rp_brw,
201
 
            'inv_ids':[{
202
 
                    'residual':residual,
203
 
                    'due_days':due_days
204
 
                }],
205
 
            'due_total':due_total,
206
 
            }
207
 
        ]
208
 
        '''
209
 
 
210
 
        '''
211
 
            [{
212
 
                'id'        : ixp['rp_brw'].id,
213
 
                'rp_brw'    : ixp['rp_brw'],
214
 
                'not_due'   : 0.00,
215
 
                '1to30'     : 0.00,
216
 
                '31to60'    : 0.00,
217
 
                '61to90'    : 0.00,
218
 
                '91to120'   : 0.00,
219
 
                '120+'      : 0.00,
220
 
            }]
221
 
        '''
222
 
        result = []
223
 
        for ixp in ixp_gen:
224
 
            res = {
225
 
                'id': ixp['rp_brw'].id,
226
 
                'rp_brw': ixp['rp_brw'],
227
 
                'not_due': 0.00,
228
 
                '1to30': 0.00,
229
 
                '31to60': 0.00,
230
 
                '61to90': 0.00,
231
 
                '91to120': 0.00,
232
 
                '120+': 0.00,
233
 
                'total': 0.00,
234
 
            }
235
 
 
236
 
            for inv in ixp['inv_ids']:
237
 
                res['total'] += inv['residual']
238
 
                if inv['due_days'] <= 0:
239
 
                    res['not_due'] += inv['residual']
240
 
 
241
 
                elif inv['due_days'] > 0 and inv['due_days'] <= 30:
242
 
                    res['1to30'] += inv['residual']
243
 
 
244
 
                elif inv['due_days'] > 30 and inv['due_days'] <= 60:
245
 
                    res['31to60'] += inv['residual']
246
 
 
247
 
                elif inv['due_days'] > 60 and inv['due_days'] <= 90:
248
 
                    res['61to90'] += inv['residual']
249
 
 
250
 
                elif inv['due_days'] > 90 and inv['due_days'] <= 120:
251
 
                    res['91to120'] += inv['residual']
252
 
 
253
 
                else:
254
 
                    res['120+'] += inv['residual']
255
 
 
256
 
            result.append(res)
257
 
        print 'THIS IS THE RESULT ', result
258
 
        return result
259
 
 
260
 
report_sxw.report_sxw(
261
 
    'report.formal_due_report',
262
 
    'res.partner',
263
 
    'addons/aging_due_report/report/formal_due_report.rml',
264
 
    parser=aging_parser,
265
 
    header=False
266
 
)
267
 
report_sxw.report_sxw(
268
 
    'report.detail_due_report',
269
 
    'res.partner',
270
 
    'addons/aging_due_report/report/detail_due_report.rml',
271
 
    parser=aging_parser,
272
 
    header=False
273
 
)
274
 
report_sxw.report_sxw(
275
 
    'report.aging_due_report',
276
 
    'res.partner',
277
 
    'addons/aging_due_report/report/aging_due_report.rml',
278
 
    parser=aging_parser,
279
 
    header=False
280
 
)
281
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: