1
by Andhitia Rama
init |
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 osv import osv,fields |
|
23 |
from tools.translate import _ |
|
24 |
||
25 |
class wizard_bank_payment(osv.osv_memory): |
|
26 |
_name = 'account.wizard_bank_payment' |
|
27 |
_description = 'Wizard Bank Payment' |
|
28 |
||
29 |
_columns = { |
|
30 |
'journal_id' : fields.many2one(string='Journal', obj='account.journal', required=True), |
|
31 |
'name' : fields.char(string='Description', size=100, required=True), |
|
32 |
'date':fields.date(string='Date', required=True), |
|
33 |
'payment_method' : fields.selection(string='Payment Method', selection=[('bank_transfer','Bank Transfer'),('cheque','Cheque'),('giro','Giro')], required=True), |
|
34 |
'cheque_number' : fields.char(string='Cheque Number', size=50, readonly=False), |
|
35 |
'cheque_date' : fields.date(string='Cheque Date', readonly=False), |
|
36 |
'cheque_partner_bank_id' : fields.many2one(obj='res.partner.bank', string='Destination Bank Account', readonly=False), |
|
37 |
'cheque_bank_id' : fields.related('cheque_partner_bank_id', 'bank', type='many2one', relation='res.bank', string='Bank', store=True, readonly=True), |
|
38 |
'cheque_recepient' : fields.char(string='Cheque Recepient', size=100, readonly=False), |
|
39 |
'cheque_is_giro' : fields.boolean('Is Giro?'), |
|
40 |
}
|
|
41 |
||
42 |
def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False): |
|
43 |
res = super(wizard_bank_payment, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu) |
|
44 |
x = [] |
|
45 |
mod_obj = self.pool.get('ir.model.data') |
|
46 |
obj_account_voucher_type = self.pool.get('account.voucher_type') |
|
47 |
if context is None: context = {} |
|
48 |
||
49 |
voucher_type = context.get('voucher_type') |
|
50 |
||
51 |
if voucher_type and view_type == 'form': |
|
52 |
||
53 |
kriteria = [('name','=',voucher_type)] |
|
54 |
voucher_type_ids = obj_account_voucher_type.search(cr, uid, kriteria)[0] |
|
55 |
||
56 |
voucher = obj_account_voucher_type.browse(cr, uid, voucher_type_ids, context=context) |
|
57 |
||
58 |
result = mod_obj.get_object_reference(cr, uid, voucher.modul_origin, voucher.model_view_form) |
|
59 |
result = result and result[1] or False |
|
60 |
view_id = result |
|
61 |
||
62 |
if voucher.allowed_journal_ids: |
|
63 |
for journal in voucher.allowed_journal_ids: |
|
64 |
x.append(journal.id) |
|
65 |
domain_journal = list(set(x)) |
|
66 |
||
67 |
for field in res['fields']: |
|
68 |
if field == 'journal_id': |
|
69 |
res['fields'][field]['domain'] = [('id','in',domain_journal)] |
|
70 |
return res |
|
71 |
||
72 |
def view_init(self, cr, uid, fields_list, context=None): |
|
73 |
||
74 |
obj_account_invoice = self.pool.get('account.invoice') |
|
75 |
||
76 |
if context is None: |
|
77 |
context = {} |
|
78 |
||
79 |
res = super(wizard_bank_payment, self).view_init(cr, uid, fields_list, context=context) |
|
80 |
||
81 |
record_id = context and context.get('active_id', False) |
|
82 |
||
83 |
if record_id: |
|
84 |
||
85 |
invoice = obj_account_invoice.browse(cr, uid, record_id, context=context) |
|
86 |
||
87 |
if invoice.state != 'open' : |
|
88 |
raise osv.except_osv(_('Warning !'), _("You may only pay invoices that are Open !")) |
|
89 |
return res |
|
90 |
||
91 |
def get_total(self, cr, uid, context=None): |
|
92 |
obj_account_invoice = self.pool.get('account.invoice') |
|
93 |
record_id = context.get('active_ids') |
|
94 |
total = 0.00 |
|
95 |
||
96 |
for data in record_id: |
|
97 |
invoice_id = obj_account_invoice.browse(cr, uid, data) |
|
98 |
total = total + invoice_id.residual |
|
99 |
||
100 |
return total |
|
101 |
||
102 |
def bank_payment(self, cr, uid, ids, context=None): |
|
103 |
obj_account_bank_payment = self.pool.get('account.bank_payment') |
|
104 |
obj_account_voucher_line = self.pool.get('account.voucher.line') |
|
105 |
obj_account_move_line = self.pool.get('account.move.line') |
|
106 |
obj_account_invoice = self.pool.get('account.invoice') |
|
107 |
obj_account_journal = self.pool.get('account.journal') |
|
108 |
obj_account_voucher_type = self.pool.get('account.voucher_type') |
|
109 |
obj_account_period = self.pool.get('account.period') |
|
110 |
||
111 |
record_id = context.get('active_ids') |
|
112 |
||
113 |
wizard = self.read(cr, uid, ids[0], context=context) |
|
114 |
||
115 |
#raise osv.except_osv(_('Error !'), _('%s')%record_id[0])
|
|
116 |
||
117 |
journal = obj_account_journal.browse(cr, uid, wizard['journal_id'][0]) |
|
118 |
||
119 |
voucher_type_ids = obj_account_voucher_type.search(cr, uid, [('name','=','Bank Payment')])[0] |
|
120 |
||
121 |
voucher_type = obj_account_voucher_type.browse(cr, uid, voucher_type_ids) |
|
122 |
||
123 |
payment_method = wizard['payment_method'] if wizard['payment_method'] else False |
|
124 |
cheque_number = wizard['cheque_number'] if wizard['cheque_number'] else False |
|
125 |
cheque_date = wizard['cheque_date'] if wizard['cheque_date'] else False |
|
126 |
cheque_partner_bank_id = wizard['cheque_partner_bank_id'][0] if wizard['cheque_partner_bank_id'] else False |
|
127 |
cheque_bank_id = wizard['cheque_bank_id'][0] if wizard['cheque_bank_id'] else False |
|
128 |
cheque_recepient = wizard['cheque_recepient'] if wizard['cheque_recepient'] else False |
|
129 |
cheque_is_giro = wizard['cheque_is_giro'] if wizard['cheque_is_giro'] else False |
|
130 |
||
131 |
period_id = obj_account_period.find(cr, uid, wizard['date'], context) |
|
132 |
||
133 |
val_header = { |
|
134 |
'journal_id' : wizard['journal_id'][0], |
|
135 |
'name' : wizard['name'], |
|
136 |
'date' : wizard['date'], |
|
137 |
'account_id' : journal.default_debit_account_id.id, |
|
138 |
'voucher_type_id' : voucher_type.id, |
|
139 |
'type' : voucher_type.default_header_type, |
|
140 |
'amount' : self.get_total(cr, uid, context), |
|
141 |
'payment_method' : payment_method, |
|
142 |
'cheque_number' : cheque_number, |
|
143 |
'cheque_date' : cheque_date, |
|
144 |
'cheque_partner_bank_id' : cheque_partner_bank_id, |
|
145 |
'cheque_bank_id' : cheque_bank_id, |
|
146 |
'cheque_recepient' : cheque_recepient, |
|
147 |
'cheque_is_giro' : cheque_is_giro, |
|
148 |
'period_id' : period_id[0], |
|
149 |
}
|
|
150 |
||
151 |
new_account_bank_payment_id = obj_account_bank_payment.create(cr, uid, val_header, context) |
|
152 |
||
153 |
for data in record_id: |
|
154 |
invoice_id = obj_account_invoice.browse(cr, uid, data) |
|
155 |
||
156 |
move_line_ids = obj_account_move_line.search(cr, uid, [('move_id','=',invoice_id.move_id.id)]) |
|
157 |
||
158 |
for move_line in obj_account_move_line.browse(cr, uid, move_line_ids): |
|
159 |
if move_line.account_id.type == 'payable': |
|
160 |
amount = obj_account_voucher_line.compute_amount(cr, uid, move_line.id, move_line.journal_id.id, move_line.currency_id.id)['amount'] |
|
161 |
val = { |
|
162 |
'voucher_id' : new_account_bank_payment_id, |
|
163 |
'account_id' : move_line.account_id.id, |
|
164 |
'move_line_id' : move_line.id, |
|
165 |
'name' : wizard['name'], |
|
166 |
'amount' : amount, |
|
167 |
'type' : 'dr', |
|
168 |
'partner_id' : move_line.partner_id.id, |
|
169 |
}
|
|
170 |
new_account_bank_payment_detail_id = obj_account_voucher_line.create(cr, uid, val, context) |
|
171 |
||
172 |
return { |
|
173 |
'res_id' : new_account_bank_payment_id, |
|
174 |
'name' : 'Bank Payment', |
|
175 |
'view_type' : 'form', |
|
176 |
'view_mode' : 'form', |
|
177 |
'res_model' : 'account.bank_payment', #TODO |
|
178 |
'type' : 'ir.actions.act_window', |
|
179 |
'context' : context, |
|
180 |
}
|
|
181 |
||
182 |
wizard_bank_payment() |
|
183 |
||
184 |
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
185 |