Remove the "ids" argument from the function _prepare_order_line_invoice_line because...
[odoo/odoo.git] / addons / sale / stock.py
index f8e10a5..f13e512 100644 (file)
@@ -27,12 +27,11 @@ class stock_move(osv.osv):
         'sale_line_id': fields.many2one('sale.order.line', 'Sales Order Line', ondelete='set null', select=True, readonly=True),
     }
 
-    def _create_chained_picking(self, cr, uid, pick_name, picking, ptype, move, context=None):
-        res = super(stock_move, self)._create_chained_picking(cr, uid, pick_name, picking, ptype, move, context=context)
+    def _prepare_chained_picking(self, cr, uid, picking_name, picking, picking_type, moves_todo, context=None):
+        values = super(stock_move, self)._prepare_chained_picking(cr, uid, picking_name, picking, picking_type, moves_todo, context=context)
         if picking.sale_id:
-            self.pool.get('stock.picking').write(cr, uid, [res], {'sale_id': picking.sale_id.id})
-        return res
-stock_move()
+            values['sale_id'] = picking.sale_id.id
+        return values
 
 class stock_picking(osv.osv):
     _inherit = 'stock.picking'
@@ -49,24 +48,32 @@ class stock_picking(osv.osv):
         else:
             return super(stock_picking, self).get_currency_id(cursor, user, picking)
 
-    def _get_payment_term(self, cursor, user, picking):
-        if picking.sale_id and picking.sale_id.payment_term:
-            return picking.sale_id.payment_term.id
-        return super(stock_picking, self)._get_payment_term(cursor, user, picking)
-
-    def _get_address_invoice(self, cursor, user, picking):
-        res = {}
+    def _get_partner_to_invoice(self, cr, uid, picking, context=None):
+        """ Inherit the original function of the 'stock' module
+            We select the partner of the sale order as the partner of the customer invoice
+        """
         if picking.sale_id:
-            res['contact'] = picking.sale_id.partner_order_id.id
-            res['invoice'] = picking.sale_id.partner_invoice_id.id
-            return res
-        return super(stock_picking, self)._get_address_invoice(cursor, user, picking)
+            return picking.sale_id.partner_id
+        return super(stock_picking, self)._get_partner_to_invoice(cr, uid, picking, context=context)
 
     def _get_comment_invoice(self, cursor, user, picking):
         if picking.note or (picking.sale_id and picking.sale_id.note):
             return picking.note or picking.sale_id.note
         return super(stock_picking, self)._get_comment_invoice(cursor, user, picking)
 
+    def _prepare_invoice(self, cr, uid, picking, partner, inv_type, journal_id, context=None):
+        """ Inherit the original function of the 'stock' module in order to override some
+            values if the picking has been generated by a sale order
+        """
+        invoice_vals = super(stock_picking, self)._prepare_invoice(cr, uid, picking, partner, inv_type, journal_id, context=context)
+        if picking.sale_id:
+            invoice_vals['address_contact_id'] = picking.sale_id.partner_order_id.id
+            invoice_vals['address_invoice_id'] = picking.sale_id.partner_invoice_id.id
+            invoice_vals['fiscal_position'] = picking.sale_id.fiscal_position.id
+            invoice_vals['payment_term'] = picking.sale_id.payment_term.id
+            invoice_vals['user_id'] = picking.sale_id.user_id.id
+        return invoice_vals
+
     def _get_price_unit_invoice(self, cursor, user, move_line, type):
         if move_line.sale_line_id and move_line.sale_line_id.product_id.id == move_line.product_id.id:
             uom_id = move_line.product_id.uom_id.id
@@ -136,7 +143,7 @@ class stock_picking(osv.osv):
 
         for picking in picking_obj.browse(cursor, user, picking_ids,
                 context=context):
-            if not picking.sale_id:
+            if not picking.sale_id or picking.backorder_id:
                 continue
             sale_lines = picking.sale_id.order_line
             invoice_created = invoices[result[picking.id]]
@@ -168,32 +175,17 @@ class stock_picking(osv.osv):
                         if not account_id:
                             account_id = sale_line.product_id.categ_id.\
                                     property_account_expense_categ.id
-                    price_unit = sale_line.price_unit
-                    discount = sale_line.discount
-                    tax_ids = sale_line.tax_id
-                    tax_ids = map(lambda x: x.id, tax_ids)
-
-                    account_analytic_id = self._get_account_analytic_invoice(cursor,
-                            user, picking, sale_line)
-
-                    account_id = fiscal_position_obj.map_account(cursor, user, picking.sale_id.partner_id.property_account_position, account_id)
-                    invoice = invoices[result[picking.id]]
-                    invoice_line_id = invoice_line_obj.create(cursor, user, {
-                        'name': name,
-                        'invoice_id': invoice.id,
-                        'uos_id': sale_line.product_uos.id or sale_line.product_uom.id,
-                        'product_id': sale_line.product_id.id,
-                        'account_id': account_id,
-                        'price_unit': price_unit,
-                        'discount': discount,
-                        'quantity': sale_line.product_uos_qty,
-                        'invoice_line_tax_id': [(6, 0, tax_ids)],
-                        'account_analytic_id': account_analytic_id,
-                        'notes':sale_line.notes
-                    }, context=context)
-                    order_line_obj.write(cursor, user, [sale_line.id], {'invoiced': True,
-                        'invoice_lines': [(6, 0, [invoice_line_id])],
-                    })
+
+                    vals = order_line_obj._prepare_order_line_invoice_line(cursor, user, sale_line, account_id, context)
+                    if vals: #note: in some cases we may not want to include all service lines as invoice lines
+                        vals['name'] = name
+                        vals['account_analytic_id'] = self._get_account_analytic_invoice(cursor, user, picking, sale_line)
+                        vals['invoice_id'] = invoices[result[picking.id]].id
+                        invoice_line_id = invoice_line_obj.create(cursor, user, vals, context=context)
+                        order_line_obj.write(cursor, user, [sale_line.id], {
+                            'invoiced': True,
+                            'invoice_lines': [(6, 0, [invoice_line_id])],
+                        })
         return result
 
-stock_picking()
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: