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

« back to all changes in this revision

Viewing changes to l10n_it_ricevute_bancarie/wizard/wizard_accreditation.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 Agile Business Group sagl (<http://www.agilebg.com>)
 
5
#    Copyright (C) 2012 Domsense srl (<http://www.domsense.com>)
 
6
#    Copyright (C) 2012 Associazione OpenERP Italia
 
7
#    (<http://www.openerp-italia.org>).
 
8
#    All Rights Reserved
 
9
#
 
10
#    This program is free software: you can redistribute it and/or modify
 
11
#    it under the terms of the GNU Affero General Public License as published
 
12
#    by the Free Software Foundation, either version 3 of the License, or
 
13
#    (at your option) any later version.
 
14
#
 
15
#    This program is distributed in the hope that it will be useful,
 
16
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
#    GNU Affero General Public License for more details.
 
19
#
 
20
#    You should have received a copy of the GNU Affero General Public License
 
21
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
22
#
 
23
##############################################################################
 
24
 
 
25
from osv import fields,osv
 
26
from tools.translate import _
 
27
import netsvc
 
28
 
 
29
class riba_accreditation(osv.osv_memory):
 
30
    
 
31
    def _get_accreditation_journal_id(self, cr, uid, context=None):
 
32
        return self.pool.get('riba.configurazione').get_default_value_by_distinta( cr, uid, 'accreditation_journal_id', context=context)
 
33
    
 
34
    def _get_accreditation_account_id(self, cr, uid, context=None):
 
35
        return self.pool.get('riba.configurazione').get_default_value_by_distinta( cr, uid, 'accreditation_account_id', context=context)
 
36
    
 
37
    def _get_bank_account_id(self, cr, uid, context=None):
 
38
        return self.pool.get('riba.configurazione').get_default_value_by_distinta( cr, uid, 'bank_account_id', context=context)
 
39
    
 
40
    def _get_bank_expense_account_id(self, cr, uid, context=None):
 
41
        return self.pool.get('riba.configurazione').get_default_value_by_distinta( cr, uid, 'bank_expense_account_id', context=context)
 
42
    
 
43
    def _get_accreditation_amount(self, cr, uid, context=None):
 
44
        if context is None:
 
45
            context = {}
 
46
        if not context.get('active_id', False):
 
47
            return False
 
48
        distinta_pool = self.pool.get('riba.distinta')
 
49
        distinta = distinta_pool.browse(cr, uid, context['active_id'], context=context)
 
50
        amount = 0.0
 
51
        for line in distinta.line_ids:
 
52
            amount += line.amount
 
53
        return amount
 
54
    
 
55
    _name = "riba.accreditation"
 
56
    _description = "Bank accreditation"
 
57
    _columns = {
 
58
        'accreditation_journal_id' : fields.many2one('account.journal', "Accreditation journal", 
 
59
            domain=[('type', '=', 'bank')]),
 
60
        'accreditation_account_id' : fields.many2one('account.account', "Ri.Ba. bank account"),
 
61
        'accreditation_amount': fields.float('Credit amount'),
 
62
        'bank_account_id' : fields.many2one('account.account', "Bank account", 
 
63
            domain=[('type', '=', 'liquidity')]),
 
64
        'bank_amount': fields.float('Versed amount'),
 
65
        'bank_expense_account_id' : fields.many2one('account.account', "Bank Expenses account"),
 
66
        'expense_amount': fields.float('Expenses amount'),
 
67
        }
 
68
 
 
69
    _defaults = {
 
70
        'accreditation_journal_id': _get_accreditation_journal_id,
 
71
        'accreditation_account_id': _get_accreditation_account_id,
 
72
        'bank_account_id': _get_bank_account_id,
 
73
        'bank_expense_account_id': _get_bank_expense_account_id,
 
74
        'accreditation_amount': _get_accreditation_amount,
 
75
        }
 
76
        
 
77
    def skip(self, cr, uid, ids, context=None):
 
78
        if context is None:
 
79
            context = {}
 
80
        wf_service = netsvc.LocalService("workflow")
 
81
        active_id = context and context.get('active_id', False) or False
 
82
        if not active_id:
 
83
            raise osv.except_osv(_('Error'), _('No active ID found'))
 
84
        wf_service.trg_validate(
 
85
            uid, 'riba.distinta', active_id, 'accredited', cr)
 
86
        return {'type': 'ir.actions.act_window_close'}
 
87
        
 
88
    def create_move(self, cr, uid, ids, context=None):
 
89
        if context is None:
 
90
            context = {}
 
91
        wf_service = netsvc.LocalService("workflow")
 
92
        active_id = context and context.get('active_id', False) or False
 
93
        if not active_id:
 
94
            raise osv.except_osv(_('Error'), _('No active ID found'))
 
95
        move_pool = self.pool.get('account.move')
 
96
        move_line_pool = self.pool.get('account.move.line')
 
97
        distinta_pool = self.pool.get('riba.distinta')
 
98
        distinta = distinta_pool.browse(cr, uid, active_id, context=context)
 
99
        wizard = self.browse(cr,uid,ids)[0]
 
100
        if not wizard.accreditation_journal_id or not wizard.accreditation_account_id or not wizard.bank_account_id or not wizard.bank_expense_account_id:
 
101
            raise osv.except_osv(_('Error'), _('Every account is mandatory'))
 
102
        move_vals = {
 
103
            'ref': _('Accreditation Ri.Ba. %s') % distinta.name,
 
104
            'journal_id': wizard.accreditation_journal_id.id,
 
105
            'line_id': [
 
106
                (0,0, {
 
107
                    'name':  _('Credit'),
 
108
                    'account_id': wizard.accreditation_account_id.id,
 
109
                    'credit': wizard.accreditation_amount,
 
110
                    'debit': 0.0,
 
111
                    }),
 
112
                (0,0, {
 
113
                    'name':  _('Bank'),
 
114
                    'account_id': wizard.bank_account_id.id,
 
115
                    'debit': wizard.bank_amount,
 
116
                    'credit': 0.0,
 
117
                    }),
 
118
                (0,0, {
 
119
                    'name':  _('Bank'),
 
120
                    'account_id': wizard.bank_expense_account_id.id,
 
121
                    'debit': wizard.expense_amount,
 
122
                    'credit': 0.0,
 
123
                    }),
 
124
                ]
 
125
            }
 
126
        move_id = move_pool.create(cr, uid, move_vals, context=context)
 
127
        distinta.write({'accreditation_move_id': move_id})
 
128
        wf_service.trg_validate(
 
129
            uid, 'riba.distinta', active_id, 'accredited', cr)
 
130
        return {
 
131
            'name': _('Accreditation Entry'),
 
132
            'view_type': 'form',
 
133
            'view_mode': 'form',
 
134
            'res_model': 'account.move',
 
135
            'type': 'ir.actions.act_window',
 
136
            'target': 'current',
 
137
            'res_id': move_id or False,
 
138
        }
 
139
        
 
140
 
 
141
riba_accreditation()