[IMP] Inter-company pricing: put price on move when only partner and not connected...
authorJosse Colpaert <jco@odoo.com>
Wed, 3 Sep 2014 17:15:10 +0000 (19:15 +0200)
committerJosse Colpaert <jco@odoo.com>
Fri, 12 Sep 2014 11:49:41 +0000 (13:49 +0200)
addons/purchase/purchase.py
addons/purchase/stock.py
addons/stock/stock.py
addons/stock_account/stock.py

index 2cd492e..02296ce 100644 (file)
@@ -727,7 +727,7 @@ class purchase_order(osv.osv):
             'origin': order.name,
             'route_ids': order.picking_type_id.warehouse_id and [(6, 0, [x.id for x in order.picking_type_id.warehouse_id.route_ids])] or [],
             'warehouse_id':order.picking_type_id.warehouse_id.id,
-            'invoice_state': order.invoice_method == 'picking' and '2binvoiced' or 'none'
+            'invoice_state': order.invoice_method == 'picking' and '2binvoiced' or 'none',
         }
 
         diff_quantity = order_line.product_qty
index 508b9a0..75fabe4 100644 (file)
@@ -53,7 +53,6 @@ class stock_move(osv.osv):
             default['purchase_line_id'] = False
         return super(stock_move, self).copy(cr, uid, id, default, context)
 
-
     def _create_invoice_line_from_vals(self, cr, uid, move, invoice_line_vals, context=None):
         invoice_line_id = super(stock_move, self)._create_invoice_line_from_vals(cr, uid, move, invoice_line_vals, context=context)
         if move.purchase_line_id:
@@ -69,7 +68,7 @@ class stock_move(osv.osv):
     def _get_master_data(self, cr, uid, move, company, context=None):
         if move.purchase_line_id:
             purchase_order = move.purchase_line_id.order_id
-            return purchase_order.partner_id, purchase_order.create_uid.id, purchase_order.pricelist_id.currency_id.id
+            return purchase_order.partner_id, purchase_order.create_uid.id, purchase_order.currency_id.id
         return super(stock_move, self)._get_master_data(cr, uid, move, company, context=context)
 
     def _get_invoice_line_vals(self, cr, uid, move, partner, inv_type, context=None):
index acbc9b6..531b2fa 100644 (file)
@@ -1525,12 +1525,6 @@ class stock_move(osv.osv):
             res.append((line.id, name))
         return res
 
-    def create(self, cr, uid, vals, context=None):
-        if vals.get('product_id') and not vals.get('price_unit'):
-            prod_obj = self.pool.get('product.product')
-            vals['price_unit'] = prod_obj.browse(cr, uid, vals['product_id'], context=context).standard_price
-        return super(stock_move, self).create(cr, uid, vals, context=context)
-
     def _quantity_normalize(self, cr, uid, ids, name, args, context=None):
         uom_obj = self.pool.get('product.uom')
         res = {}
@@ -2020,6 +2014,26 @@ class stock_move(osv.osv):
             date_expected = time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
         return {'value': {'date': date_expected}}
 
+    def attribute_price(self, cr, uid, move, context=None):
+        """
+            Attribute price to move, important in multi-company
+        """
+        if move.location_id.usage != 'internal' and move.location_dest_id.usage == 'internal' and not move.price_unit:
+            partner = move.partner_id or (move.picking_id and move.picking_id.partner_id)
+            price = False
+            # If partner given, search price in its purchase pricelist
+            if partner and partner.property_product_pricelist_purchase:
+                pricelist_obj = self.pool.get("product.pricelist")
+                pricelist = partner.property_product_pricelist.id
+                price = pricelist_obj.price_get(cr, uid, [pricelist],
+                                    move.product_id.id, move.product_uom_qty, partner, {
+                                                                                'uom': move.product_uom.id,
+                                                                                'date': move.date,
+                                                                                })[pricelist]
+            if not price:
+                price = move.product_id.standard_price
+            self.write(cr, uid, [move.id], {'price_unit': price})
+
 
     def action_confirm(self, cr, uid, ids, context=None):
         """ Confirms stock move or put it in waiting if it's linked to another move.
@@ -2033,6 +2047,7 @@ class stock_move(osv.osv):
         }
         to_assign = {}
         for move in self.browse(cr, uid, ids, context=context):
+            self.attribute_price(cr, uid, move, context=context)
             state = 'confirmed'
             #if the move is preceeded, then it's waiting (if preceeding move is done, then action_assign has been called already and its state is already available)
             if move.move_orig_ids:
index 7a0d5cc..e4d58e3 100644 (file)
@@ -96,7 +96,13 @@ class stock_move(osv.osv):
 
     def _get_master_data(self, cr, uid, move, company, context=None):
         ''' returns a tuple (browse_record(res.partner), ID(res.users), ID(res.currency)'''
-        return move.picking_id.partner_id, uid, company.currency_id.id
+        currency = company.currency_id.id
+        if move.partner_id:
+            if move.partner_id.property_product_pricelist_purchase and move.location_id.usage != 'internal' and move.location_dest_id.usage == 'internal':
+                currency = move.partner_id.property_product_pricelist_purchase.currency_id.id
+            elif move.partner_id.property_product_pricelist and move.location_id.usage == 'internal' and move.location_dest_id.usage != 'internal':
+                currency = move.partner_id.property_product_pricelist.currency_id.id
+        return move.picking_id.partner_id, uid, currency
 
     def _create_invoice_line_from_vals(self, cr, uid, move, invoice_line_vals, context=None):
         return self.pool.get('account.invoice.line').create(cr, uid, invoice_line_vals, context=context)
@@ -110,10 +116,26 @@ class stock_move(osv.osv):
         if context is None:
             context = {}
         if type in ('in_invoice', 'in_refund'):
-            # Take the user company and pricetype
-            product = move_line.product_id.with_context(currency_id=move_line.company_id.currency_id.id)
-            amount_unit = product.price_get('standard_price')[move_line.product_id.id]
-            return amount_unit
+            if move_line.partner_id:
+                return move_line.price_unit
+            else:
+                # Take the user company and pricetype
+                # TODO: This intercompany still needed?
+                product = move_line.product_id.with_context(currency_id=move_line.company_id.currency_id.id)
+                amount_unit = product.price_get('standard_price')[move_line.product_id.id]
+                return amount_unit
+        else:
+            # If partner given, search price in its sale pricelist
+            if move_line.partner_id and move_line.partner_id.property_product_pricelist:
+                pricelist_obj = self.pool.get("product.pricelist")
+                pricelist = move_line.partner_id.property_product_pricelist.id
+                price = pricelist_obj.price_get(cr, uid, [pricelist],
+                        product, move_line.product_uom_qty, move_line.partner_id.id, {
+                            'uom': move_line.product_uom.id,
+                            'date': move_line.date,
+                            })[pricelist]
+                if price:
+                    return price
         return move_line.product_id.list_price
 
     def _get_invoice_line_vals(self, cr, uid, move, partner, inv_type, context=None):
@@ -172,13 +194,20 @@ class stock_picking(osv.osv):
                 res.append(move.picking_id.id)
         return res
 
+    def _set_inv_state(self, cr, uid, picking_id, name, value, arg, context=None):
+        pick = self.browse(cr, uid, picking_id, context=context)
+        moves = [x.id for x in pick.move_lines]
+        move_obj= self.pool.get("stock.move")
+        move_obj.write(cr, uid, moves, {'invoice_state': pick.invoice_state})
+
+
     _columns = {
         'invoice_state': fields.function(__get_invoice_state, type='selection', selection=[
             ("invoiced", "Invoiced"),
             ("2binvoiced", "To Be Invoiced"),
             ("none", "Not Applicable")
           ], string="Invoice Control", required=True,
-
+        fnct_inv = _set_inv_state,
         store={
             'stock.picking': (lambda self, cr, uid, ids, c={}: ids, ['state'], 10),
             'stock.move': (__get_picking_move, ['picking_id', 'invoice_state'], 10),