diff --git a/addons/sale/wizard/sale_make_invoice_advance.py b/addons/sale/wizard/sale_make_invoice_advance.py index 78a04645e08..4e6d6414ddf 100644 --- a/addons/sale/wizard/sale_make_invoice_advance.py +++ b/addons/sale/wizard/sale_make_invoice_advance.py @@ -65,45 +65,18 @@ class sale_advance_payment_inv(osv.osv_memory): return {'value': {'amount': product.list_price}} return {'value': {'amount': 0}} - def create_invoices(self, cr, uid, ids, context=None): - """ create invoices for the active sale orders """ + def _prepare_advance_invoice_vals(self, cr, uid, ids, context=None): if context is None: context = {} sale_obj = self.pool.get('sale.order') - inv_obj = self.pool.get('account.invoice') - inv_line_obj = self.pool.get('account.invoice.line') ir_property_obj = self.pool.get('ir.property') - act_window = self.pool.get('ir.actions.act_window') fiscal_obj = self.pool.get('account.fiscal.position') + inv_line_obj = self.pool.get('account.invoice.line') wizard = self.browse(cr, uid, ids[0], context) sale_ids = context.get('active_ids', []) - if wizard.advance_payment_method == 'all': - # create the final invoices of the active sale orders - res = sale_obj.manual_invoice(cr, uid, sale_ids, context) - if context.get('open_invoices', False): - return res - return {'type': 'ir.actions.act_window_close'} - - if wizard.advance_payment_method == 'lines': - # open the list view of sale order lines to invoice - res = act_window.for_xml_id(cr, uid, 'sale', 'action_order_line_tree2', context) - res['context'] = { - 'search_default_uninvoiced': 1, - 'search_default_order_id': sale_ids and sale_ids[0] or False, - } - return res - assert wizard.advance_payment_method in ('fixed', 'percentage') - - inv_ids = [] - + result = [] for sale in sale_obj.browse(cr, uid, sale_ids, context=context): - if sale.order_policy == 'postpaid': - raise osv.except_osv( - _('Error!'), - _("You cannot make an advance on a sales order \ - that is defined as 'Automatic Invoice after delivery'.")) - val = inv_line_obj.product_id_change(cr, uid, [], wizard.product_id.id, uom=False, partner_id=sale.partner_id.id, fposition_id=sale.fiscal_position.id) res = val['value'] @@ -172,12 +145,45 @@ class sale_advance_payment_inv(osv.osv_memory): 'payment_term': sale.payment_term.id, 'fiscal_position': sale.fiscal_position.id or sale.partner_id.property_account_position.id } - inv_id = inv_obj.create(cr, uid, inv_values, context=context) - inv_obj.button_reset_taxes(cr, uid, [inv_id], context=context) - inv_ids.append(inv_id) + result.append((sale.id, inv_values)) + return result - # add the invoice to the sale order's invoices - sale.write({'invoice_ids': [(4, inv_id)]}) + def _create_invoices(self, cr, uid, inv_values, sale_id, context=None): + inv_obj = self.pool.get('account.invoice') + sale_obj = self.pool.get('sale.order') + inv_id = inv_obj.create(cr, uid, inv_values, context=context) + inv_obj.button_reset_taxes(cr, uid, [inv_id], context=context) + # add the invoice to the sale order's invoices + sale_obj.write(cr, uid, sale_id, {'invoice_ids': [(4, inv_id)]}, context=context) + return inv_id + + + def create_invoices(self, cr, uid, ids, context=None): + """ create invoices for the active sale orders """ + sale_obj = self.pool.get('sale.order') + act_window = self.pool.get('ir.actions.act_window') + wizard = self.browse(cr, uid, ids[0], context) + sale_ids = context.get('active_ids', []) + if wizard.advance_payment_method == 'all': + # create the final invoices of the active sale orders + res = sale_obj.manual_invoice(cr, uid, sale_ids, context) + if context.get('open_invoices', False): + return res + return {'type': 'ir.actions.act_window_close'} + + if wizard.advance_payment_method == 'lines': + # open the list view of sale order lines to invoice + res = act_window.for_xml_id(cr, uid, 'sale', 'action_order_line_tree2', context) + res['context'] = { + 'search_default_uninvoiced': 1, + 'search_default_order_id': sale_ids and sale_ids[0] or False, + } + return res + assert wizard.advance_payment_method in ('fixed', 'percentage') + + inv_ids = [] + for sale_id, inv_values in self._prepare_advance_invoice_vals(cr, uid, ids, context=context): + inv_ids.append(self._create_invoices(cr, uid, inv_values, sale_id, context=context)) if context.get('open_invoices', False): return self.open_invoices( cr, uid, ids, inv_ids, context=context) diff --git a/addons/sale_stock/sale_stock.py b/addons/sale_stock/sale_stock.py index 8dd7014f6f5..b6a3dfc1143 100644 --- a/addons/sale_stock/sale_stock.py +++ b/addons/sale_stock/sale_stock.py @@ -646,60 +646,35 @@ class sale_order_line(osv.osv): class sale_advance_payment_inv(osv.osv_memory): _inherit = "sale.advance.payment.inv" - - def create_invoices(self, cr, uid, ids, context=None): - """ create invoices for the active sale orders """ - result = super(sale_advance_payment_inv, self).create_invoices(cr, uid, ids, context=context) + + def _create_invoices(self, cr, uid, inv_values, sale_id, context=None): + result = super(sale_advance_payment_inv, self)._create_invoices(cr, uid, inv_values, sale_id, context=context) sale_obj = self.pool.get('sale.order') - inv_line_obj = self.pool.get('account.invoice.line') sale_line_obj = self.pool.get('sale.order.line') wizard = self.browse(cr, uid, ids[0], context) - sale_ids = context.get('active_ids', []) - inv_ids = [] - for sale in sale_obj.browse(cr, uid, sale_ids, context=context): - if sale.order_policy == 'postpaid': - raise osv.except_osv( - _('Error!'), - _("You cannot make an advance on a sales order \ - that is defined as 'Automatic Invoice after delivery'.")) + sale = sale_obj.browse(cr, uid, sale_id, context=context) + if sale.order_policy == 'postpaid': + raise osv.except_osv( + _('Error!'), + _("You cannot make an advance on a sales order \ + that is defined as 'Automatic Invoice after delivery'.")) - val = inv_line_obj.product_id_change(cr, uid, [], wizard.product_id.id, - uom=False, partner_id=sale.partner_id.id, fposition_id=sale.fiscal_position.id) - res = val['value'] - # determine invoice amount - if wizard.advance_payment_method == 'percentage': - inv_amount = sale.amount_total * wizard.amount / 100 - if not res.get('name'): - res['name'] = _("Advance of %s %%") % (wizard.amount) - else: - inv_amount = wizard.amount - if not res.get('name'): - #TODO: should find a way to call formatLang() from rml_parse - symbol = sale.pricelist_id.currency_id.symbol - if sale.pricelist_id.currency_id.position == 'after': - res['name'] = _("Advance of %s %s") % (inv_amount, symbol) - else: - res['name'] = _("Advance of %s %s") % (symbol, inv_amount) - - # determine taxes - if res.get('invoice_line_tax_id'): - res['invoice_line_tax_id'] = [(6, 0, res.get('invoice_line_tax_id'))] - else: - res['invoice_line_tax_id'] = False - # If invoice on picking: add the cost on the SO - # If not, the advance will be deduced when generating the final invoice - if sale.order_policy == 'picking': - vals = { - 'order_id': sale.id, - 'name': res.get('name'), - 'price_unit': -inv_amount, - 'product_uom_qty': wizard.qtty or 1.0, - 'product_uos_qty': wizard.qtty or 1.0, - 'product_uos': res.get('uos_id', False), - 'product_uom': res.get('uom_id', False), - 'product_id': wizard.product_id.id or False, - 'discount': False, - 'tax_id': res.get('invoice_line_tax_id'), - } - sale_line_obj.create(cr, uid, vals, context=context) + # If invoice on picking: add the cost on the SO + # If not, the advance will be deduced when generating the final invoice + line_name = inv_values.get('invoice_line') and inv_values.get('invoice_line')[2].get('name') or '' + line_tax = inv_values.get('invoice_line') and inv_values.get('invoice_line')[2].get('invoice_line_tax_id') or False + if sale.order_policy == 'picking': + vals = { + 'order_id': sale.id, + 'name': line_name, + 'price_unit': -inv_amount, + 'product_uom_qty': wizard.qtty or 1.0, + 'product_uos_qty': wizard.qtty or 1.0, + 'product_uos': res.get('uos_id', False), + 'product_uom': res.get('uom_id', False), + 'product_id': wizard.product_id.id or False, + 'discount': False, + 'tax_id': line_tax, + } + sale_line_obj.create(cr, uid, vals, context=context) return result