Skip to content
Snippets Groups Projects
Commit 150d7730 authored by William Henrotin's avatar William Henrotin
Browse files

[FIX] delivery: free_over management


The commit 9a9cb9e refactored the way delivery carrier price
are added on sale orders. The management of free delivery price
was not taken into account past then.
This commit put the free price message directly on the sale order line
and on the wizard. The carrier price is showed on the wizard but is 0 on
the SO.

Task : 1943480

closes odoo/odoo#31315

Signed-off-by: default avatarpimodoo <pimodoo@users.noreply.github.com>
parent 0c6e0d66
No related branches found
No related tags found
No related merge requests found
......@@ -127,9 +127,11 @@ class DeliveryCarrier(models.Model):
res = getattr(self, '%s_rate_shipment' % self.delivery_type)(order)
# apply margin on computed price
res['price'] = float(res['price']) * (1.0 + (float(self.margin) / 100.0))
# save the real price in case a free_over rule overide it to 0
res['carrier_price'] = res['price']
# free when order is large enough
if res['success'] and self.free_over and order._compute_amount_total_without_delivery() >= self.amount:
res['warning_message'] = _('Info:\nThe shipping is free because the order amount exceeds %.2f.\n(The actual shipping cost is: %.2f)') % (self.amount, res['price'])
res['warning_message'] = _('The shipping is free since the order amount exceeds %.2f.') % (self.amount)
res['price'] = 0.0
return res
......
......@@ -80,11 +80,13 @@ class SaleOrder(models.Model):
self.delivery_message = res.get('warning_message', False)
else:
raise UserError(res['error_message'])
delivery_line.name = self.carrier_id.with_context(lang=self.partner_id.lang).name
if self.carrier_id.invoice_policy == 'real':
delivery_line.name = self.carrier_id.with_context(lang=self.partner_id.lang).name
delivery_line.name += _(' (Estimated Cost: %s )') % self._format_currency_amount(res['price'])
else:
delivery_line.price_unit = res['price']
if self.carrier_id.free_over and self._compute_amount_total_without_delivery() >= res['price']:
delivery_line.name += '\nFree Shipping'
self.recompute_delivery_price = False
def _create_delivery_line(self, carrier, price_unit, price_unit_in_description=False):
......@@ -114,7 +116,8 @@ class SaleOrder(models.Model):
values['name'] += _(' (Estimated Cost: %s )') % self._format_currency_amount(price_unit)
else:
values['price_unit'] = price_unit
if carrier.free_over and self._compute_amount_total_without_delivery() >= price_unit:
values['name'] += '\n' + 'Free Shipping'
if self.order_line:
values['sequence'] = self.order_line[-1].sequence + 1
sol = SaleOrderLine.sudo().create(values)
......
......@@ -295,13 +295,6 @@
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<data>
<xpath expr="//field[@name='payment_term_id']" position="after">
<div name='carrier_selection'>
<div class="alert alert-info" role="status" attrs="{'invisible': [('delivery_message','=',False)]}">
<field name='delivery_message' force_save="1"/>
</div>
</div>
</xpath>
<xpath expr="//field[@name='partner_id']" position='after'>
<field name="delivery_set" invisible="1"/>
<field name="recompute_delivery_price" invisible="1"/>
......
......@@ -18,23 +18,27 @@ class ChooseDeliveryCarrier(models.TransientModel):
required=True,
)
delivery_type = fields.Selection(related='carrier_id.delivery_type')
delivery_price = fields.Float(string='Cost', readonly=True)
delivery_price = fields.Float()
display_price = fields.Float(string='Cost', readonly=True)
currency_id = fields.Many2one('res.currency', related='order_id.currency_id')
available_carrier_ids = fields.Many2many("delivery.carrier", compute='_compute_available_carrier', string="Available Carriers")
invoicing_message = fields.Text(compute='_compute_invoicing_message')
delivery_message = fields.Text(readonly=True)
@api.onchange('carrier_id')
def _onchange_carrier_id(self):
self.delivery_message = False
if self.delivery_type in ('fixed', 'base_on_rule'):
vals = self.carrier_id.rate_shipment(self.order_id)
if vals.get('success'):
if vals['warning_message']:
self.order_id.delivery_message = vals['warning_message']
else:
self.delivery_price = vals['price']
if vals.get('warning_message'):
self.delivery_message = vals['warning_message']
self.delivery_price = vals['price']
self.display_price = vals['carrier_price']
else:
return {'error': vals['error_message']}
else:
self.display_price = 0
self.delivery_price = 0
@api.depends('carrier_id')
......@@ -55,8 +59,9 @@ class ChooseDeliveryCarrier(models.TransientModel):
vals = self.carrier_id.rate_shipment(self.order_id)
if vals.get('success'):
if vals['warning_message']:
self.order_id.delivery_message = vals['warning_message']
self.delivery_message = vals['warning_message']
self.delivery_price = vals['price']
self.display_price = vals['carrier_price']
else:
raise UserError(vals['error_message'])
return {
......@@ -70,4 +75,5 @@ class ChooseDeliveryCarrier(models.TransientModel):
def button_confirm(self):
self.order_id.carrier_id = self.carrier_id
self.order_id.delivery_message = self.delivery_message
self.order_id.set_delivery_line(self.carrier_id, self.delivery_price)
......@@ -11,7 +11,8 @@
<field name="delivery_type" invisible="1"/>
<field name="currency_id" invisible="1"/>
<field name="order_id" invisible="1"/>
<field name='delivery_price' string="Cost" widget="monetary" options="{'currency_field': 'currency_id'}" attrs="{'invisible': [('carrier_id','=', False)]}" force_save="1"/>
<field name='delivery_price' invisible="1"/>
<field name='display_price' string="Cost" widget="monetary" options="{'currency_field': 'currency_id'}" attrs="{'invisible': [('carrier_id','=', False)]}"/>
</group>
<button name="update_price" type="object" attrs="{'invisible': [('delivery_type','in', ('fixed', 'base_on_rule'))]}">
<i class="fa fa-arrow-right"/>
......@@ -20,6 +21,9 @@
<div role="alert" class="alert alert-warning" attrs="{'invisible': [('invoicing_message', '=', '')]}">
<field name="invoicing_message" nolabel="1"/>
</div>
<div role="alert" class="alert alert-info" attrs="{'invisible': [('delivery_message', '=', False)]}">
<field name="delivery_message" nolabel="1"/>
</div>
<footer>
<button name="button_confirm" type="object" string="Add" class="btn-primary"/>
<button string="Discard" special="cancel" class="btn-secondary"/>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment