summaryrefslogtreecommitdiff
path: root/addons
diff options
context:
space:
mode:
Diffstat (limited to 'addons')
-rw-r--r--addons/account/account_invoice.py143
-rw-r--r--addons/account/wizard/account_invoice_refund.py2
-rw-r--r--addons/point_of_sale/wizard/pos_return.py2
3 files changed, 83 insertions, 64 deletions
diff --git a/addons/account/account_invoice.py b/addons/account/account_invoice.py
index 856b88ab99d..ed6a21eb950 100644
--- a/addons/account/account_invoice.py
+++ b/addons/account/account_invoice.py
@@ -1150,73 +1150,92 @@ class account_invoice(osv.osv):
ids = self.search(cr, user, [('name',operator,name)] + args, limit=limit, context=context)
return self.name_get(cr, user, ids, context)
- def _refund_cleanup_lines(self, cr, uid, lines):
+ def _refund_cleanup_lines(self, cr, uid, lines, context=None):
+ clean_lines = []
for line in lines:
- del line['id']
- del line['invoice_id']
- for field in ('company_id', 'partner_id', 'account_id', 'product_id',
- 'uos_id', 'account_analytic_id', 'tax_code_id', 'base_code_id'):
- if line.get(field):
- line[field] = line[field][0]
- if 'invoice_line_tax_id' in line:
- line['invoice_line_tax_id'] = [(6,0, line.get('invoice_line_tax_id', [])) ]
- return map(lambda x: (0,0,x), lines)
-
- def refund(self, cr, uid, ids, date=None, period_id=None, description=None, journal_id=None):
- invoices = self.read(cr, uid, ids, ['name', 'type', 'number', 'reference', 'comment', 'date_due', 'partner_id', 'partner_contact', 'partner_insite', 'partner_ref', 'payment_term', 'account_id', 'currency_id', 'invoice_line', 'tax_line', 'journal_id', 'company_id', 'user_id', 'fiscal_position'])
- obj_invoice_line = self.pool.get('account.invoice.line')
- obj_invoice_tax = self.pool.get('account.invoice.tax')
+ clean_line = {}
+ for field in line._all_columns.keys():
+ if line._all_columns[field].column._type == 'many2one':
+ clean_line[field] = line[field].id
+ elif line._all_columns[field].column._type not in ['many2many','one2many']:
+ clean_line[field] = line[field]
+ elif field == 'invoice_line_tax_id':
+ tax_list = []
+ for tax in line[field]:
+ tax_list.append(tax.id)
+ clean_line[field] = [(6,0, tax_list)]
+ clean_lines.append(clean_line)
+ return map(lambda x: (0,0,x), clean_lines)
+
+ def _prepare_refund(self, cr, uid, invoice, date=None, period_id=None, description=None, journal_id=None, context=None):
+ """Prepare the dict of values to create the new refund from the invoice.
+ This method may be overridden to implement custom
+ refund generation (making sure to call super() to establish
+ a clean extension chain).
+
+ :param integer invoice_id: id of the invoice to refund
+ :param dict invoice: read of the invoice to refund
+ :param string date: refund creation date from the wizard
+ :param integer period_id: force account.period from the wizard
+ :param string description: description of the refund from the wizard
+ :param integer journal_id: account.journal from the wizard
+ :return: dict of value to create() the refund
+ """
obj_journal = self.pool.get('account.journal')
- new_ids = []
- for invoice in invoices:
- del invoice['id']
-
- type_dict = {
- 'out_invoice': 'out_refund', # Customer Invoice
- 'in_invoice': 'in_refund', # Supplier Invoice
- 'out_refund': 'out_invoice', # Customer Refund
- 'in_refund': 'in_invoice', # Supplier Refund
- }
-
- invoice_lines = obj_invoice_line.read(cr, uid, invoice['invoice_line'])
- invoice_lines = self._refund_cleanup_lines(cr, uid, invoice_lines)
- tax_lines = obj_invoice_tax.read(cr, uid, invoice['tax_line'])
- tax_lines = filter(lambda l: l['manual'], tax_lines)
- tax_lines = self._refund_cleanup_lines(cr, uid, tax_lines)
- if journal_id:
- refund_journal_ids = [journal_id]
- elif invoice['type'] == 'in_invoice':
- refund_journal_ids = obj_journal.search(cr, uid, [('type','=','purchase_refund')])
+ type_dict = {
+ 'out_invoice': 'out_refund', # Customer Invoice
+ 'in_invoice': 'in_refund', # Supplier Invoice
+ 'out_refund': 'out_invoice', # Customer Refund
+ 'in_refund': 'in_invoice', # Supplier Refund
+ }
+ invoice_data = {}
+ for field in ['name', 'reference', 'comment', 'date_due', 'partner_id', 'company_id',
+ 'account_id', 'currency_id', 'payment_term', 'user_id', 'fiscal_position']:
+ if invoice._all_columns[field].column._type == 'many2one':
+ invoice_data[field] = invoice[field].id
else:
- refund_journal_ids = obj_journal.search(cr, uid, [('type','=','sale_refund')])
-
- if not date:
- date = time.strftime('%Y-%m-%d')
- invoice.update({
- 'type': type_dict[invoice['type']],
- 'date_invoice': date,
- 'state': 'draft',
- 'number': False,
- 'invoice_line': invoice_lines,
- 'tax_line': tax_lines,
- 'journal_id': refund_journal_ids
- })
- if period_id:
- invoice.update({
- 'period_id': period_id,
- })
- if description:
- invoice.update({
- 'name': description,
- })
- # take the id part of the tuple returned for many2one fields
- for field in ('partner_id', 'company_id',
- 'account_id', 'currency_id', 'payment_term', 'journal_id',
- 'user_id', 'fiscal_position'):
- invoice[field] = invoice[field] and invoice[field][0]
+ invoice_data[field] = invoice[field] if invoice[field] else False
+
+ invoice_lines = self._refund_cleanup_lines(cr, uid, invoice.invoice_line, context=context)
+
+ tax_lines = filter(lambda l: l['manual'], invoice.tax_line)
+ tax_lines = self._refund_cleanup_lines(cr, uid, tax_lines, context=context)
+ if journal_id:
+ refund_journal_ids = [journal_id]
+ elif invoice['type'] == 'in_invoice':
+ refund_journal_ids = obj_journal.search(cr, uid, [('type','=','purchase_refund')], context=context)
+ else:
+ refund_journal_ids = obj_journal.search(cr, uid, [('type','=','sale_refund')], context=context)
+
+ if not date:
+ date = time.strftime('%Y-%m-%d')
+ invoice_data.update({
+ 'type': type_dict[invoice['type']],
+ 'date_invoice': date,
+ 'state': 'draft',
+ 'number': False,
+ 'invoice_line': invoice_lines,
+ 'tax_line': tax_lines,
+ 'journal_id': refund_journal_ids and refund_journal_ids[0] or False,
+ })
+ if period_id:
+ invoice_data['period_id'] = period_id
+ if description:
+ invoice_data['name'] = description
+ return invoice_data
+
+ def refund(self, cr, uid, ids, date=None, period_id=None, description=None, journal_id=None, context=None):
+ new_ids = []
+ for invoice in self.browse(cr, uid, ids, context=context):
+ invoice = self._prepare_refund(cr, uid, invoice,
+ date=date,
+ period_id=period_id,
+ description=description,
+ journal_id=journal_id,
+ context=context)
# create the new invoice
- new_ids.append(self.create(cr, uid, invoice))
+ new_ids.append(self.create(cr, uid, invoice, context=context))
return new_ids
diff --git a/addons/account/wizard/account_invoice_refund.py b/addons/account/wizard/account_invoice_refund.py
index 533170cf4a4..54d6c38a353 100644
--- a/addons/account/wizard/account_invoice_refund.py
+++ b/addons/account/wizard/account_invoice_refund.py
@@ -146,7 +146,7 @@ class account_invoice_refund(osv.osv_memory):
raise osv.except_osv(_('Insufficient Data!'), \
_('No period found on the invoice.'))
- refund_id = inv_obj.refund(cr, uid, [inv.id], date, period, description, journal_id)
+ refund_id = inv_obj.refund(cr, uid, [inv.id], date, period, description, journal_id, context=context)
refund = inv_obj.browse(cr, uid, refund_id[0], context=context)
inv_obj.write(cr, uid, [refund.id], {'date_due': date,
'check_total': inv.check_total})
diff --git a/addons/point_of_sale/wizard/pos_return.py b/addons/point_of_sale/wizard/pos_return.py
index 294cc531ae2..d6dfdd1e183 100644
--- a/addons/point_of_sale/wizard/pos_return.py
+++ b/addons/point_of_sale/wizard/pos_return.py
@@ -278,7 +278,7 @@ class add_product(osv.osv_memory):
location_id=res and res[0] or None
if order_id.invoice_id:
- invoice_obj.refund(cr, uid, [order_id.invoice_id.id], time.strftime('%Y-%m-%d'), False, order_id.name)
+ invoice_obj.refund(cr, uid, [order_id.invoice_id.id], time.strftime('%Y-%m-%d'), False, order_id.name, context=context)
new_picking=picking_obj.create(cr, uid, {
'name':'%s (return)' %order_id.name,
'move_lines':[], 'state':'draft',