~openobject-italia-core-devs/openobject-italia/italian-addons

« back to all changes in this revision

Viewing changes to l10n_it_ricevute_bancarie/wizard/wizard_emissione_riba.py

  • Committer: Lorenzo Battistini
  • Date: 2012-05-21 08:20:57 UTC
  • mto: This revision was merged to the branch mainline in revision 181.
  • Revision ID: lorenzo.battistini@agilebg.com-20120521082057-ljhjzbzh5q53pyte
[ADD] l10n_it_ricevute_bancarie
moving from https://code.launchpad.net/~openobject-italia-core-devs/openobject-italia/l10n_it_ricevute_bancarie

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#    
 
4
#    Copyright (C) 2012 Andrea Cometa.
 
5
#    Email: info@andreacometa.it
 
6
#    Web site: http://www.andreacometa.it
 
7
#    Copyright (C) 2012 Agile Business Group sagl (<http://www.agilebg.com>)
 
8
#    Copyright (C) 2012 Domsense srl (<http://www.domsense.com>)
 
9
#    Copyright (C) 2012 Associazione OpenERP Italia
 
10
#    (<http://www.openerp-italia.org>).
 
11
#    All Rights Reserved
 
12
#
 
13
#    This program is free software: you can redistribute it and/or modify
 
14
#    it under the terms of the GNU Affero General Public License as published
 
15
#    by the Free Software Foundation, either version 3 of the License, or
 
16
#    (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 Affero General Public License for more details.
 
22
#
 
23
#    You should have received a copy of the GNU Affero General Public License
 
24
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
25
#
 
26
##############################################################################
 
27
 
 
28
from osv import fields,osv
 
29
 
 
30
# -------------------------------------------------------
 
31
#        EMISSIONE RIBA
 
32
# -------------------------------------------------------
 
33
class emissione_riba(osv.osv_memory):
 
34
    _name = "riba.emissione"
 
35
    _description = "Emissione Ricevute Bancarie"
 
36
    _columns = {
 
37
 
 
38
        'configurazione' : fields.many2one('riba.configurazione', 'Configurazione', required=True),
 
39
        }
 
40
    
 
41
    def crea_distinta(self, cr, uid, ids, context=None):
 
42
        if context is None:
 
43
            context = {}
 
44
        def create_rdl(conta, bank_id, rd_id, date_maturity, partner_id, acceptance_account_id):
 
45
            rdl = {
 
46
                'sequence' : conta,
 
47
                'bank_id' : bank_id,
 
48
                'distinta_id': rd_id,
 
49
                'due_date' : date_maturity,
 
50
                'partner_id' : partner_id,
 
51
                'state': 'draft',
 
52
                'acceptance_account_id': acceptance_account_id,
 
53
            }
 
54
            return riba_distinta_line.create(cr, uid, rdl, context=context)
 
55
            
 
56
        """
 
57
        Qui creiamo la distinta
 
58
        """
 
59
        wizard_obj = self.browse(cr,uid,ids)[0]
 
60
        active_ids = context and context.get('active_ids', [])
 
61
        riba_distinta = self.pool.get('riba.distinta')
 
62
        riba_distinta_line = self.pool.get('riba.distinta.line')
 
63
        riba_distinta_move_line = self.pool.get('riba.distinta.move.line')
 
64
        move_line_obj = self.pool.get('account.move.line')
 
65
 
 
66
        # create distinta
 
67
        rd = {
 
68
            'name': self.pool.get('ir.sequence').get(cr, uid, 'seq.riba.distinta'),
 
69
            'config': wizard_obj.configurazione.id,
 
70
            'user_id': uid,
 
71
            'date_created': fields.date.context_today(cr,uid,context),
 
72
        }
 
73
        rd_id = riba_distinta.create(cr, uid, rd)
 
74
        
 
75
        # group by partner and due date
 
76
        grouped_lines = {}
 
77
        move_line_ids = move_line_obj.search(cr, uid, [('id', 'in', active_ids)], context=context)
 
78
        for move_line in move_line_obj.browse(cr, uid, move_line_ids, context=context):
 
79
            if move_line.partner_id.group_riba:
 
80
                if not grouped_lines.get(
 
81
                    (move_line.partner_id.id, move_line.date_maturity), False):
 
82
                    grouped_lines[(move_line.partner_id.id, move_line.date_maturity)] = []
 
83
                grouped_lines[(move_line.partner_id.id, move_line.date_maturity)].append(
 
84
                    move_line)
 
85
        
 
86
        # create lines
 
87
        conta = 1
 
88
        
 
89
        for move_line in move_line_obj.browse(cr, uid, move_line_ids, context=context):
 
90
            if move_line.partner_id.bank_ids:
 
91
                bank_id = move_line.partner_id.bank_ids[0]
 
92
            else:
 
93
                raise osv.except_osv('Attenzione!', 'Il cliente %s non ha la banca!!!' % move_line.partner_id.name)
 
94
            if move_line.partner_id.group_riba:
 
95
                for key in grouped_lines:
 
96
                    if key[0] == move_line.partner_id.id and key[1] == move_line.date_maturity:
 
97
                        rdl_id = create_rdl(conta, bank_id.id, rd_id, move_line.date_maturity, move_line.partner_id.id, wizard_obj.configurazione.acceptance_account_id.id)
 
98
                        total = 0.0
 
99
                        invoice_date_group = ''
 
100
                        for grouped_line in grouped_lines[key]:
 
101
                            riba_distinta_move_line.create(cr, uid, {
 
102
                                'riba_line_id': rdl_id,
 
103
                                'amount': grouped_line.debit,
 
104
                                'move_line_id': grouped_line.id,
 
105
                                }, context=context)
 
106
                        del grouped_lines[key]
 
107
                        break
 
108
            else:
 
109
                rdl_id = create_rdl(conta, bank_id.id, rd_id, move_line.date_maturity, move_line.partner_id.id, wizard_obj.configurazione.acceptance_account_id.id)
 
110
                riba_distinta_move_line.create(cr, uid, {
 
111
                    'riba_line_id': rdl_id,
 
112
                    'amount': move_line.debit,
 
113
                    'move_line_id': move_line.id,
 
114
                    }, context=context)
 
115
            
 
116
            conta+=1
 
117
        
 
118
        # ----- show distinta form
 
119
        mod_obj = self.pool.get('ir.model.data')
 
120
        res = mod_obj.get_object_reference(cr, uid, 'l10n_it_ricevute_bancarie', 'view_distinta_riba_form')
 
121
        res_id = res and res[1] or False,
 
122
        return {
 
123
            'name': 'Distinta',
 
124
            'view_type': 'form',
 
125
            'view_mode': 'form',
 
126
            'view_id': res_id,
 
127
            'res_model': 'riba.distinta',
 
128
            'type': 'ir.actions.act_window',
 
129
            #'nodestroy': True,
 
130
            'target': 'current',
 
131
            'res_id': rd_id or False,
 
132
        }
 
133
 
 
134
emissione_riba()