~koi-accounting-modules-maintainer/koi-accounting-modules/7.0-fixed-asset

« back to all changes in this revision

Viewing changes to ar_account/wizard/wizard_cash_payment.py

  • Committer: Andhitia Rama
  • Date: 2014-04-06 22:04:29 UTC
  • Revision ID: andhitia.r@gmail.com-20140406220429-5yler527mgqcm5l8
init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
from lxml import etree
 
23
 
 
24
import netsvc
 
25
import time
 
26
 
 
27
from osv import osv,fields
 
28
from tools.translate import _
 
29
import decimal_precision as dp
 
30
 
 
31
class wizard_cash_payment(osv.osv_memory):
 
32
    _name = 'account.wizard_cash_payment'
 
33
    _description = 'Wizard Cash Payment'
 
34
 
 
35
    _columns = {
 
36
                'journal_id' : fields.many2one(string='Journal', obj='account.journal', required=True),
 
37
                'name' : fields.char(string='Description', size=100, required=True),
 
38
                'date':fields.date(string='Date', required=True),
 
39
                }
 
40
 
 
41
    def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
 
42
        res = super(wizard_cash_payment, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
 
43
        x = []
 
44
        mod_obj = self.pool.get('ir.model.data')
 
45
        obj_account_voucher_type = self.pool.get('account.voucher_type')
 
46
        if context is None: context = {}
 
47
 
 
48
        voucher_type = context.get('voucher_type')
 
49
 
 
50
        if voucher_type and view_type == 'form':
 
51
 
 
52
                kriteria = [('name','=',voucher_type)]
 
53
                voucher_type_ids = obj_account_voucher_type.search(cr, uid, kriteria)[0]
 
54
 
 
55
                voucher = obj_account_voucher_type.browse(cr, uid, voucher_type_ids, context=context)
 
56
 
 
57
                result = mod_obj.get_object_reference(cr, uid, voucher.modul_origin, voucher.model_view_form)
 
58
                result = result and result[1] or False
 
59
                view_id = result
 
60
                
 
61
                if voucher.allowed_journal_ids:
 
62
                        for journal in voucher.allowed_journal_ids:
 
63
                                x.append(journal.id)
 
64
                domain_journal = list(set(x))
 
65
 
 
66
                for field in res['fields']:
 
67
                    if field == 'journal_id':
 
68
                        res['fields'][field]['domain'] = [('id','in',domain_journal)]
 
69
        return res
 
70
 
 
71
    def view_init(self, cr, uid, fields_list, context=None):
 
72
 
 
73
        obj_account_invoice = self.pool.get('account.invoice')
 
74
 
 
75
        if context is None:
 
76
            context = {}
 
77
            
 
78
        res = super(wizard_cash_payment, self).view_init(cr, uid, fields_list, context=context)
 
79
        
 
80
        record_id = context and context.get('active_id', False)
 
81
        
 
82
        if record_id:
 
83
 
 
84
            invoice = obj_account_invoice.browse(cr, uid, record_id, context=context)
 
85
 
 
86
            if invoice.state != 'open' :
 
87
                raise osv.except_osv(_('Warning !'), _("You may only pay invoices that are Open !"))
 
88
        return res
 
89
 
 
90
    def get_total(self, cr, uid, context=None):
 
91
        obj_account_invoice = self.pool.get('account.invoice')
 
92
        record_id = context.get('active_ids')
 
93
        total = 0.00
 
94
 
 
95
        for data in record_id:
 
96
            invoice_id = obj_account_invoice.browse(cr, uid, data)
 
97
            total = total + invoice_id.residual
 
98
        
 
99
        return total
 
100
                            
 
101
    def cash_payment(self, cr, uid, ids, context=None):
 
102
        obj_account_cash_payment = self.pool.get('account.cash_payment')
 
103
        obj_account_voucher_line = self.pool.get('account.voucher.line')
 
104
        obj_account_move_line = self.pool.get('account.move.line')
 
105
        obj_account_invoice = self.pool.get('account.invoice')
 
106
        obj_account_journal = self.pool.get('account.journal')
 
107
        obj_account_voucher_type = self.pool.get('account.voucher_type')
 
108
        obj_account_period = self.pool.get('account.period')
 
109
 
 
110
        record_id = context.get('active_ids')
 
111
 
 
112
        wizard = self.read(cr, uid, ids[0], context=context)
 
113
 
 
114
        #raise osv.except_osv(_('Error !'), _('%s')%record_id[0])
 
115
 
 
116
        journal = obj_account_journal.browse(cr, uid, wizard['journal_id'][0])
 
117
        voucher_currency_id = journal.currency and journal.currency.id or journal.company_id.currency_id.id
 
118
 
 
119
        voucher_type_ids = obj_account_voucher_type.search(cr, uid, [('name','=','Cash Payment')])[0]
 
120
 
 
121
        voucher_type = obj_account_voucher_type.browse(cr, uid, voucher_type_ids)
 
122
        
 
123
        period_id = obj_account_period.find(cr, uid, wizard['date'], context)
 
124
 
 
125
        val_header = {
 
126
                        'journal_id' : wizard['journal_id'][0],
 
127
                        'name' : wizard['name'],
 
128
                        'date' : wizard['date'],
 
129
                        'account_id' : journal.default_debit_account_id.id,
 
130
                        'voucher_type_id' : voucher_type.id,
 
131
                        'type' : voucher_type.default_header_type,
 
132
                        'amount' : self.get_total(cr, uid, context),
 
133
                        'period_id' : period_id[0],
 
134
                        }
 
135
 
 
136
        new_account_cash_payment_id = obj_account_cash_payment.create(cr, uid, val_header, context)
 
137
 
 
138
        for data in record_id:
 
139
 
 
140
            invoice_id = obj_account_invoice.browse(cr, uid, data)
 
141
 
 
142
            move_line_ids = obj_account_move_line.search(cr, uid, [('move_id','=',invoice_id.move_id.id)])
 
143
 
 
144
            for move_line in obj_account_move_line.browse(cr, uid, move_line_ids):
 
145
                if move_line.account_id.type == 'payable':
 
146
                    amount = obj_account_voucher_line.compute_amount(cr, uid, move_line.id, move_line.journal_id.id, voucher_currency_id)['amount']
 
147
                    val = {
 
148
                            'voucher_id' : new_account_cash_payment_id,
 
149
                            'account_id' : move_line.account_id.id,
 
150
                            'move_line_id' : move_line.id,
 
151
                            'name' : wizard['name'],
 
152
                            'amount' : amount,
 
153
                            'type' : 'dr',
 
154
                            'partner_id' : move_line.partner_id.id,
 
155
                            }
 
156
                    new_account_cash_payment_detail_id = obj_account_voucher_line.create(cr, uid, val, context)
 
157
 
 
158
        return  {
 
159
                        'res_id' : new_account_cash_payment_id,
 
160
                        'name' : 'Cash Payment',
 
161
                        'view_type' : 'form',
 
162
                        'view_mode' : 'form',
 
163
                        'res_model' : 'account.cash_payment', #TODO
 
164
                        'type' : 'ir.actions.act_window',
 
165
                        'context' : context,
 
166
                        }
 
167
        
 
168
wizard_cash_payment()
 
169
 
 
170
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
171