From afed7db30042e2ea91cdae292dadb7b6f742e33d Mon Sep 17 00:00:00 2001
From: "Touati Djamel (otd)" <otd@odoo.com>
Date: Tue, 7 Feb 2023 10:33:18 +0000
Subject: [PATCH] [FIX] delivery: compute total amount only if a sale order
 exists

Steps to reproduce the bug:
- Create a delivery transfer:
- Add any product
- Select any carrier in the additional info tab
- try to validate the transfer

Problem:
An error is triggered: `ValueError: Expected singleton: sale.order()`
The `_compute_amount_total_without_delivery` function is called with an
empty recordset whereas we need to trigger it with a sale_order

opw-3164490
opw-3168878
opw-3166074

closes odoo/odoo#112069

Signed-off-by: Djamel Touati (otd) <otd@odoo.com>
---
 addons/delivery/models/stock_picking.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/addons/delivery/models/stock_picking.py b/addons/delivery/models/stock_picking.py
index b1f5697429bc..ac68b3df5f1c 100644
--- a/addons/delivery/models/stock_picking.py
+++ b/addons/delivery/models/stock_picking.py
@@ -219,9 +219,10 @@ class StockPicking(models.Model):
     def send_to_shipper(self):
         self.ensure_one()
         res = self.carrier_id.send_shipping(self)[0]
-        amount_without_delivery = self.sale_id._compute_amount_total_without_delivery()
-        if self.carrier_id.free_over and self.sale_id and self.carrier_id._compute_currency(self.sale_id, amount_without_delivery, 'pricelist_to_company') >= self.carrier_id.amount:
-            res['exact_price'] = 0.0
+        if self.carrier_id.free_over and self.sale_id:
+            amount_without_delivery = self.sale_id._compute_amount_total_without_delivery()
+            if self.carrier_id._compute_currency(self.sale_id, amount_without_delivery, 'pricelist_to_company') >= self.carrier_id.amount:
+                res['exact_price'] = 0.0
         self.carrier_price = res['exact_price'] * (1.0 + (self.carrier_id.margin / 100.0))
         if res['tracking_number']:
             previous_pickings = self.env['stock.picking']
-- 
GitLab