Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] delivery: free_over management #31315

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion addons/delivery/models/delivery_carrier.py
Expand Up @@ -127,9 +127,11 @@ def rate_shipment(self, order):
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

Expand Down
7 changes: 5 additions & 2 deletions addons/delivery/models/sale_order.py
Expand Up @@ -80,11 +80,13 @@ def recompute_delivery_cost(self):
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):
Expand Down Expand Up @@ -114,7 +116,8 @@ def _create_delivery_line(self, carrier, price_unit, price_unit_in_description=F
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)
Expand Down
7 changes: 0 additions & 7 deletions addons/delivery/views/delivery_view.xml
Expand Up @@ -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"/>
Expand Down
18 changes: 12 additions & 6 deletions addons/delivery/wizard/choose_delivery_carrier.py
Expand Up @@ -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
pimodoo marked this conversation as resolved.
Show resolved Hide resolved
self.delivery_price = 0

@api.depends('carrier_id')
Expand All @@ -55,8 +59,9 @@ def update_price(self):
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']
pimodoo marked this conversation as resolved.
Show resolved Hide resolved
self.delivery_price = vals['price']
self.display_price = vals['carrier_price']
else:
raise UserError(vals['error_message'])
return {
Expand All @@ -70,4 +75,5 @@ def update_price(self):

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)
6 changes: 5 additions & 1 deletion addons/delivery/wizard/choose_delivery_carrier_views.xml
Expand Up @@ -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"/>
Expand All @@ -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"/>
Expand Down