~vauxoo/addons-vauxoo/6.0-trunk

« back to all changes in this revision

Viewing changes to debit_credit_note/wizard/account_invoice_refund.py

  • Committer: Sabrina Romero
  • Date: 2013-08-20 21:10:39 UTC
  • mto: (543.7.272 vaddddddd)
  • mto: This revision was merged to the branch mainline in revision 840.
  • Revision ID: sabrina@vauxoo.com-20130820211039-9jqrffvg2nz8q3vx

[ADD] Module product_do_merge added

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
import time
 
23
 
 
24
from osv import fields, osv
 
25
from tools.translate import _
 
26
import netsvc
 
27
 
 
28
 
 
29
class account_invoice_refund(osv.osv_memory):
 
30
 
 
31
    """Refunds invoice"""
 
32
    _inherit = 'account.invoice.refund'
 
33
 
 
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')
 
37
        if context is None:
 
38
            context = {}
 
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
 
49
 
 
50
    def fields_view_get(self, cr, uid, view_id=None, view_type=False,
 
51
                        context=None, toolbar=False, submenu=False):
 
52
        if context is None:
 
53
            context = {}
 
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
 
57
        # crashes
 
58
        context.pop('form_view_ref', None)
 
59
        res = super(account_invoice_refund, self).\
 
60
            fields_view_get(cr, uid,
 
61
                            view_id=view_id,
 
62
                            view_type=view_type,
 
63
                            context=context,
 
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, '',
 
75
                                                          [('type', '=',
 
76
                                                            journal_type),
 
77
                                                           ('company_id',
 
78
                                                               'child_of',
 
79
                                                               [company_id])],
 
80
                                                          context=context)
 
81
                res['fields'][field]['selection'] = journal_select
 
82
        return res
 
83
 
 
84
    def _get_period(self, cr, uid, context={}):
 
85
        """
 
86
        Return  default account period value
 
87
        """
 
88
        account_period_obj = self.pool.get('account.period')
 
89
        ids = account_period_obj.find(cr, uid, context=context)
 
90
        period_id = False
 
91
        if ids:
 
92
            period_id = ids[0]
 
93
        return period_id
 
94
 
 
95
    def _get_orig(self, cr, uid, inv, ref, context={}):
 
96
        """
 
97
        Return  default origin value
 
98
        """
 
99
        nro_ref = ref
 
100
        if inv.type == 'out_invoice':
 
101
            nro_ref = inv.number
 
102
        orig = _('INV REFUND:') + (nro_ref or '') + _('- DATE:') + (
 
103
            inv.date_invoice or '') + (' TOTAL:' + str(inv.amount_total) or '')
 
104
        return orig
 
105
 
 
106
    def compute_refund(self, cr, uid, ids, mode='refund', context=None):
 
107
        """
 
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
 
111
 
 
112
        """
 
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')
 
122
        if context is None:
 
123
            context = {}
 
124
 
 
125
        for form in self.browse(cr, uid, ids, context=context):
 
126
            created_inv = []
 
127
            date = False
 
128
            period = False
 
129
            description = False
 
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'),
 
134
                                      context=context):
 
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))
 
143
                if form.period.id:
 
144
                    period = form.period.id
 
145
                else:
 
146
                    period = inv.period_id and inv.period_id.id or False
 
147
 
 
148
                if not journal_id:
 
149
                    journal_id = inv.journal_id.id
 
150
 
 
151
                if form.date:
 
152
                    date = form.date
 
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()
 
158
                        if result_query:
 
159
                            cr.execute("""select p.id from account_fiscalyear y
 
160
                                            , account_period p
 
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""",
 
164
                                      (date, company.id,))
 
165
                        else:
 
166
                            cr.execute("""SELECT id
 
167
                                    from account_period where date(%s)
 
168
                                    between date_start AND  date_stop  \
 
169
                                    limit 1 """, (date,))
 
170
                        res = cr.fetchone()
 
171
                        if res:
 
172
                            period = res[0]
 
173
                else:
 
174
                    date = inv.date_invoice
 
175
                if form.description:
 
176
                    description = form.description
 
177
                else:
 
178
                    description = inv.name
 
179
 
 
180
                if not period:
 
181
                    raise osv.except_osv(_('Insufficient Data!'),
 
182
                                         _('No period found on the invoice.'))
 
183
 
 
184
                refund_id = inv_obj.refund(cr, uid, [
 
185
                                           inv.id], date, period,
 
186
                                           description, journal_id,
 
187
                                           context=context)
 
188
                refund = inv_obj.browse(cr, uid, refund_id[0], context=context)
 
189
                # Add parent invoice
 
190
                inv_obj.write(cr, uid, [refund.id],
 
191
                              {'date_due': date,
 
192
                               'check_total': inv.check_total,
 
193
                               'parent_id': inv.id})
 
194
                inv_obj.button_compute(cr, uid, refund_id)
 
195
 
 
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:
 
211
                            to_reconcile_ids[
 
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
 
219
                        )
 
220
                    if mode == 'modify':
 
221
                        invoice = inv_obj.read(cr, uid, [inv.id],
 
222
                                               ['name', 'type', 'number',
 
223
                                                'reference', 'comment',
 
224
                                                'date_due', 'partner_id',
 
225
                                                'partner_insite',
 
226
                                                'partner_contact',
 
227
                                                'partner_ref', 'payment_term',
 
228
                                                'account_id', 'currency_id',
 
229
                                                'invoice_line', 'tax_line',
 
230
                                                'journal_id', 'period_id'],
 
231
                                               context=context)
 
232
                        invoice = invoice[0]
 
233
                        del invoice['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)
 
242
                        invoice.update({
 
243
                            'type': inv.type,
 
244
                            'date_invoice': date,
 
245
                            'state': 'draft',
 
246
                            'number': False,
 
247
                            'invoice_line': invoice_lines,
 
248
                            'tax_line': tax_lines,
 
249
                            'period_id': period,
 
250
                            'name': description,
 
251
                            'origin': orig,
 
252
                        })
 
253
                        for field in (
 
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
 
275
            return result
 
276
 
 
277
    def invoice_refund(self, cr, uid, ids, context=None):
 
278
        data_refund = self.read(cr, uid, ids, [
 
279
                                'filter_refund'],
 
280
                                context=context)[0]['filter_refund']
 
281
        return self.compute_refund(cr, uid, ids, data_refund, context=context)
 
282
 
 
283
 
 
284
account_invoice_refund()
 
285
 
 
286
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: