|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +############################################################################## |
| 3 | +# For copyright and license notices, see __openerp__.py file in module root |
| 4 | +# directory |
| 5 | +############################################################################## |
| 6 | +from openerp import models, fields, api |
| 7 | + |
| 8 | + |
| 9 | +class account_invoice_refund(models.TransientModel): |
| 10 | + |
| 11 | + # we inherit refund creation to choose a company journal and also try to |
| 12 | + # choose a journal of same point of sale |
| 13 | + |
| 14 | + _inherit = 'account.invoice.refund' |
| 15 | + |
| 16 | + @api.model |
| 17 | + def _get_invoice_id(self): |
| 18 | + return self._context.get('active_id', False) |
| 19 | + |
| 20 | + # @api.one |
| 21 | + @api.multi |
| 22 | + @api.onchange('invoice_id') |
| 23 | + def _onchange_invoice(self): |
| 24 | + journal_type = False |
| 25 | + if self.invoice_id.type == 'out_refund' or 'out_invoice': |
| 26 | + journal_type = 'sale_refund' |
| 27 | + elif self.invoice_id.type == 'in_refund' or 'in_invoice': |
| 28 | + journal_type = 'purchase_refund' |
| 29 | + # return {'domain': {'journal_id': price}} |
| 30 | + journals = self.env['account.journal'].search( |
| 31 | + [('type', '=', journal_type), |
| 32 | + ('company_id', '=', self.invoice_id.company_id.id)]) |
| 33 | + periods = self.env['account.period'].search( |
| 34 | + [('company_id', '=', self.invoice_id.company_id.id)]) |
| 35 | + # self.journal_type = journal_type |
| 36 | + |
| 37 | + point_of_sale = self.invoice_id.journal_id.point_of_sale_id |
| 38 | + print 'point_of_sale', point_of_sale |
| 39 | + if point_of_sale: |
| 40 | + journal = self.env['account.journal'].search( |
| 41 | + [('type', '=', journal_type), |
| 42 | + ('company_id', '=', self.invoice_id.company_id.id), |
| 43 | + ('point_of_sale_id', '=', point_of_sale.id), |
| 44 | + ], limit=1) |
| 45 | + print 'journal', journal |
| 46 | + if not journal and journals: |
| 47 | + journal = journals[0] |
| 48 | + if journal: |
| 49 | + self.journal_id = journal.id |
| 50 | + return {'domain': { |
| 51 | + 'journal_id': [('id', 'in', journals.ids)], |
| 52 | + 'period': [('id', 'in', periods.ids)] |
| 53 | + }} |
| 54 | + |
| 55 | + invoice_id = fields.Many2one( |
| 56 | + 'account.invoice', |
| 57 | + 'Invoice', |
| 58 | + default=_get_invoice_id, |
| 59 | + store=True) |
| 60 | + |
| 61 | + def compute_refund(self, cr, uid, ids, data_refund, context=None): |
| 62 | + res = super(account_invoice_refund, self).compute_refund( |
| 63 | + cr, uid, ids, data_refund, context=context) |
| 64 | + domain = res.get('domain', []) |
| 65 | + invoice_ids = context.get('active_ids', []) |
| 66 | + if not invoice_ids: |
| 67 | + return res |
| 68 | + sale_order_ids = self.pool['sale.order'].search( |
| 69 | + cr, uid, [('invoice_ids', 'in', invoice_ids)]) |
| 70 | + invoice_obj = self.pool['account.invoice'] |
| 71 | + refund_invoice_ids = invoice_obj.search(cr, uid, domain) |
| 72 | + origin = ', '.join([x.number for x in invoice_obj.browse( |
| 73 | + cr, uid, invoice_ids) if x.number]) |
| 74 | + invoice_obj.write(cr, uid, refund_invoice_ids, { |
| 75 | + 'origin': origin, |
| 76 | + }) |
| 77 | + if not self.browse(cr, uid, ids, context=context)[0].period: |
| 78 | + invoice_obj.write(cr, uid, refund_invoice_ids, { |
| 79 | + 'period_id': invoice_obj.browse( |
| 80 | + cr, uid, invoice_ids)[0].period_id.id |
| 81 | + }) |
| 82 | + for invoice_id in refund_invoice_ids: |
| 83 | + self.pool['sale.order'].write( |
| 84 | + cr, uid, sale_order_ids, {'invoice_ids': [(4, invoice_id)]}) |
| 85 | + return res |
| 86 | + |
| 87 | +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: |
0 commit comments