1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20
##############################################################################
24
from osv import fields, osv
25
from tools.translate import _
29
class account_invoice_refund(osv.osv_memory):
32
_inherit = 'account.invoice.refund'
34
def _get_journal(self, cr, uid, context=None):
35
obj_journal = self.pool.get('account.journal')
36
user_obj = self.pool.get('res.users')
39
inv_type = context.get('type', 'out_invoice')
40
company_id = user_obj.browse(
41
cr, uid, uid, context=context).company_id.id
42
type = (inv_type == 'out_invoice') and 'sale_refund' or \
43
(inv_type == 'out_refund') and 'sale' or \
44
(inv_type == 'in_invoice') and 'purchase_refund' or \
45
(inv_type == 'in_refund') and 'purchase'
46
journal = obj_journal.search(cr, uid, [('type', '=', type), (
47
'company_id', '=', company_id)], limit=1, context=context)
48
return journal and journal[0] or False
50
def fields_view_get(self, cr, uid, view_id=None, view_type=False,
51
context=None, toolbar=False, submenu=False):
54
journal_obj = self.pool.get('account.journal')
55
user_obj = self.pool.get('res.users')
56
# remove the entry with key 'form_view_ref', otherwise fields_view_get
58
context.pop('form_view_ref', None)
59
res = super(account_invoice_refund, self).\
60
fields_view_get(cr, uid,
64
toolbar=toolbar, submenu=submenu)
65
type = context.get('type', 'out_invoice')
66
company_id = user_obj.browse(
67
cr, uid, uid, context=context).company_id.id
68
journal_type = (type == 'out_invoice') and 'sale_refund' or \
69
(type == 'out_refund') and 'sale' or \
70
(type == 'in_invoice') and 'purchase_refund' or \
71
(type == 'in_refund') and 'purchase'
72
for field in res['fields']:
73
if field == 'journal_id':
74
journal_select = journal_obj._name_search(cr, uid, '',
81
res['fields'][field]['selection'] = journal_select
84
def _get_period(self, cr, uid, context={}):
86
Return default account period value
88
account_period_obj = self.pool.get('account.period')
89
ids = account_period_obj.find(cr, uid, context=context)
95
def _get_orig(self, cr, uid, inv, ref, context={}):
97
Return default origin value
100
if inv.type == 'out_invoice':
102
orig = _('INV REFUND:') + (nro_ref or '') + _('- DATE:') + (
103
inv.date_invoice or '') + (' TOTAL:' + str(inv.amount_total) or '')
106
def compute_refund(self, cr, uid, ids, mode='refund', context=None):
108
@param cr: the current row, from the database cursor,
109
@param uid: the current user’s ID for security checks,
110
@param ids: the account invoice refund’s ID or list of IDs
113
inv_obj = self.pool.get('account.invoice')
114
reconcile_obj = self.pool.get('account.move.reconcile')
115
account_m_line_obj = self.pool.get('account.move.line')
116
mod_obj = self.pool.get('ir.model.data')
117
act_obj = self.pool.get('ir.actions.act_window')
118
wf_service = netsvc.LocalService('workflow')
119
inv_tax_obj = self.pool.get('account.invoice.tax')
120
inv_line_obj = self.pool.get('account.invoice.line')
121
res_users_obj = self.pool.get('res.users')
125
for form in self.browse(cr, uid, ids, context=context):
130
company = res_users_obj.browse(
131
cr, uid, uid, context=context).company_id
132
journal_id = form.journal_id.id
133
for inv in inv_obj.browse(cr, uid, context.get('active_ids'),
135
if inv.state in ['draft', 'proforma2', 'cancel']:
136
raise osv.except_osv(_('Error!'), _(
137
'Cannot %s draft/proforma/cancel invoice.') % (mode))
138
if inv.reconciled and mode in ('cancel', 'modify'):
139
raise osv.except_osv(_('Error!'), _(
140
'Cannot %s invoice which is already reconciled, '
141
'invoice should be unreconciled first. You can only '
142
'refund this invoice.') % (mode))
144
period = form.period.id
146
period = inv.period_id and inv.period_id.id or False
149
journal_id = inv.journal_id.id
153
if not form.period.id:
154
cr.execute("select name from ir_model_fields \
155
where model = 'account.period' \
156
and name = 'company_id'")
157
result_query = cr.fetchone()
159
cr.execute("""select p.id from account_fiscalyear y
161
where y.id=p.fiscalyear_id \
162
and date(%s) between p.date_start AND
163
p.date_stop and y.company_id = %s limit 1""",
166
cr.execute("""SELECT id
167
from account_period where date(%s)
168
between date_start AND date_stop \
169
limit 1 """, (date,))
174
date = inv.date_invoice
176
description = form.description
178
description = inv.name
181
raise osv.except_osv(_('Insufficient Data!'),
182
_('No period found on the invoice.'))
184
refund_id = inv_obj.refund(cr, uid, [
185
inv.id], date, period,
186
description, journal_id,
188
refund = inv_obj.browse(cr, uid, refund_id[0], context=context)
190
inv_obj.write(cr, uid, [refund.id],
192
'check_total': inv.check_total,
193
'parent_id': inv.id})
194
inv_obj.button_compute(cr, uid, refund_id)
196
created_inv.append(refund_id[0])
197
if mode in ('cancel', 'modify'):
198
movelines = inv.move_id.line_id
199
to_reconcile_ids = {}
200
for line in movelines:
201
if line.account_id.id == inv.account_id.id:
202
to_reconcile_ids[line.account_id.id] = [line.id]
203
if type(line.reconcile_id) != osv.orm.browse_null:
204
reconcile_obj.unlink(cr, uid, line.reconcile_id.id)
205
wf_service.trg_validate(uid, 'account.invoice',
206
refund.id, 'invoice_open', cr)
207
refund = inv_obj.browse(
208
cr, uid, refund_id[0], context=context)
209
for tmpline in refund.move_id.line_id:
210
if tmpline.account_id.id == inv.account_id.id:
212
tmpline.account_id.id].append(tmpline.id)
213
for account in to_reconcile_ids:
214
account_m_line_obj.reconcile(
215
cr, uid, to_reconcile_ids[account],
216
writeoff_period_id=period,
217
writeoff_journal_id=inv.journal_id.id,
218
writeoff_acc_id=inv.account_id.id
221
invoice = inv_obj.read(cr, uid, [inv.id],
222
['name', 'type', 'number',
223
'reference', 'comment',
224
'date_due', 'partner_id',
227
'partner_ref', 'payment_term',
228
'account_id', 'currency_id',
229
'invoice_line', 'tax_line',
230
'journal_id', 'period_id'],
234
invoice_lines = inv_line_obj.browse(
235
cr, uid, invoice['invoice_line'], context=context)
236
invoice_lines = inv_obj._refund_cleanup_lines(
237
cr, uid, invoice_lines, context=context)
238
tax_lines = inv_tax_obj.browse(
239
cr, uid, invoice['tax_line'], context=context)
240
tax_lines = inv_obj._refund_cleanup_lines(
241
cr, uid, tax_lines, context=context)
244
'date_invoice': date,
247
'invoice_line': invoice_lines,
248
'tax_line': tax_lines,
254
'partner_id', 'account_id', 'currency_id',
255
'payment_term', 'journal_id'):
256
invoice[field] = invoice[
257
field] and invoice[field][0]
258
inv_id = inv_obj.create(cr, uid, invoice, {})
259
if inv.payment_term.id:
260
data = inv_obj.onchange_payment_term_date_invoice(
261
cr, uid, [inv_id], inv.payment_term.id, date)
262
if 'value' in data and data['value']:
263
inv_obj.write(cr, uid, [inv_id], data['value'])
264
created_inv.append(inv_id)
265
xml_id = (inv.type == 'out_refund') and 'action_invoice_tree1' or \
266
(inv.type == 'in_refund') and 'action_invoice_tree2' or \
267
(inv.type == 'out_invoice') and 'action_invoice_tree3' or \
268
(inv.type == 'in_invoice') and 'action_invoice_tree4'
269
result = mod_obj.get_object_reference(cr, uid, 'account', xml_id)
270
id = result and result[1] or False
271
result = act_obj.read(cr, uid, id, context=context)
272
invoice_domain = eval(result['domain'])
273
invoice_domain.append(('id', 'in', created_inv))
274
result['domain'] = invoice_domain
277
def invoice_refund(self, cr, uid, ids, context=None):
278
data_refund = self.read(cr, uid, ids, [
280
context=context)[0]['filter_refund']
281
return self.compute_refund(cr, uid, ids, data_refund, context=context)
284
account_invoice_refund()
286
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: