30 lines
1.2 KiB
Python

from odoo import models, fields, api, _
class SaleOrder(models.Model):
_inherit = 'sale.order'
def _add_uber_delivery_fee(self, amount):
"""Add the delivery fee as a line item if not already added or update it"""
config = self.env['uber.config'].sudo().search([('active', '=', True)], limit=1)
if config and config.delivery_product_id:
fee_product = config.delivery_product_id
# Ensure product is published so website_sale doesn't remove it from cart
if not fee_product.website_published:
fee_product.sudo().write({'website_published': True})
# Set fulfillment type on order
if hasattr(self, 'fulfilment_type'):
self.write({'fulfilment_type': 'delivery'})
fee_line = self.order_line.filtered(lambda l: l.product_id.id == fee_product.id)
if fee_line:
fee_line.write({'price_unit': amount})
else:
self.write({'order_line': [(0, 0, {
'product_id': fee_product.id,
'name': fee_product.name,
'price_unit': amount,
'product_uom_qty': 1,
})]})
return True