summaryrefslogtreecommitdiff
path: root/addons/sale_coupon_delivery/models
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/sale_coupon_delivery/models
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/sale_coupon_delivery/models')
-rw-r--r--addons/sale_coupon_delivery/models/__init__.py6
-rw-r--r--addons/sale_coupon_delivery/models/sale_coupon.py14
-rw-r--r--addons/sale_coupon_delivery/models/sale_coupon_program.py24
-rw-r--r--addons/sale_coupon_delivery/models/sale_coupon_reward.py19
-rw-r--r--addons/sale_coupon_delivery/models/sale_order.py65
5 files changed, 128 insertions, 0 deletions
diff --git a/addons/sale_coupon_delivery/models/__init__.py b/addons/sale_coupon_delivery/models/__init__.py
new file mode 100644
index 00000000..9c652c3a
--- /dev/null
+++ b/addons/sale_coupon_delivery/models/__init__.py
@@ -0,0 +1,6 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from . import sale_order
+from . import sale_coupon_program
+from . import sale_coupon_reward \ No newline at end of file
diff --git a/addons/sale_coupon_delivery/models/sale_coupon.py b/addons/sale_coupon_delivery/models/sale_coupon.py
new file mode 100644
index 00000000..71700866
--- /dev/null
+++ b/addons/sale_coupon_delivery/models/sale_coupon.py
@@ -0,0 +1,14 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+
+from odoo import models, _
+
+
+class Coupon(models.Model):
+ _inherit = "coupon.coupon"
+
+ def _check_coupon_code(self, order):
+ if self.program_id.reward_type == 'free_shipping' and not order.order_line.filtered(lambda line: line.is_delivery):
+ return {'error': _('The shipping costs are not in the order lines.')}
+ return super(Coupon, self)._check_coupon_code(order)
diff --git a/addons/sale_coupon_delivery/models/sale_coupon_program.py b/addons/sale_coupon_delivery/models/sale_coupon_program.py
new file mode 100644
index 00000000..aefde1c2
--- /dev/null
+++ b/addons/sale_coupon_delivery/models/sale_coupon_program.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+
+from odoo import models, _, api
+
+
+class CouponProgram(models.Model):
+ _inherit = "coupon.program"
+
+ def _filter_not_ordered_reward_programs(self, order):
+ """
+ Returns the programs when the reward is actually in the order lines
+ """
+ programs = super(CouponProgram, self)._filter_not_ordered_reward_programs(order)
+ # Do not filter on free delivery programs. As delivery_unset is called everywhere (which is
+ # rather stupid), the delivery line is unliked to be created again instead of writing on it to
+ # modify the price_unit. That way, the reward is unlink and is not set back again.
+ return programs
+
+ def _check_promo_code(self, order, coupon_code):
+ if self.reward_type == 'free_shipping' and not any(line.is_delivery for line in order.order_line):
+ return {'error': _('The shipping costs are not in the order lines.')}
+ return super(CouponProgram, self)._check_promo_code(order, coupon_code)
diff --git a/addons/sale_coupon_delivery/models/sale_coupon_reward.py b/addons/sale_coupon_delivery/models/sale_coupon_reward.py
new file mode 100644
index 00000000..589db2a2
--- /dev/null
+++ b/addons/sale_coupon_delivery/models/sale_coupon_reward.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import fields, models, _
+
+
+class CouponReward(models.Model):
+ _inherit = 'coupon.reward'
+ _description = "Coupon Reward"
+
+ reward_type = fields.Selection(selection_add=[('free_shipping', 'Free Shipping')])
+
+ def name_get(self):
+ result = []
+ reward_names = super(CouponReward, self).name_get()
+ free_shipping_reward_ids = self.filtered(lambda reward: reward.reward_type == 'free_shipping').ids
+ for res in reward_names:
+ result.append((res[0], res[0] in free_shipping_reward_ids and _("Free Shipping") or res[1]))
+ return result
diff --git a/addons/sale_coupon_delivery/models/sale_order.py b/addons/sale_coupon_delivery/models/sale_order.py
new file mode 100644
index 00000000..79c0e603
--- /dev/null
+++ b/addons/sale_coupon_delivery/models/sale_order.py
@@ -0,0 +1,65 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import models, _
+
+class SaleOrder(models.Model):
+ _inherit = "sale.order"
+
+ def _get_no_effect_on_threshold_lines(self):
+ self.ensure_one()
+ # Do not count shipping and free shipping
+ free_delivery_product = self.env['coupon.program'].search([('reward_type', '=', 'free_shipping')]).mapped('discount_line_product_id')
+ lines = self.order_line.filtered(lambda line: line.is_delivery or line.product_id in free_delivery_product)
+ return lines + super(SaleOrder, self)._get_no_effect_on_threshold_lines()
+
+ def _get_paid_order_lines(self):
+ """ Returns the taxes included sale order total amount without the rewards amount"""
+ free_reward_product = self.env['coupon.program'].search([('reward_type', '=', 'product')]).mapped('discount_line_product_id')
+ return self.order_line.filtered(lambda x: not (x.is_reward_line or x.is_delivery) or x.product_id in free_reward_product)
+
+ def _get_reward_line_values(self, program):
+ if program.reward_type == 'free_shipping':
+ return [self._get_reward_values_free_shipping(program)]
+ else:
+ return super(SaleOrder, self)._get_reward_line_values(program)
+
+ def _get_reward_values_free_shipping(self, program):
+ delivery_line = self.order_line.filtered(lambda x: x.is_delivery)
+ taxes = delivery_line.product_id.taxes_id.filtered(lambda t: t.company_id.id == self.company_id.id)
+ taxes = self.fiscal_position_id.map_tax(taxes)
+ return {
+ 'name': _("Discount: %s", program.name),
+ 'product_id': program.discount_line_product_id.id,
+ 'price_unit': delivery_line and - delivery_line.price_unit or 0.0,
+ 'product_uom_qty': 1.0,
+ 'product_uom': program.discount_line_product_id.uom_id.id,
+ 'order_id': self.id,
+ 'is_reward_line': True,
+ 'tax_id': [(4, tax.id, False) for tax in taxes],
+ }
+
+ def _get_cheapest_line(self):
+ # Unit prices tax included
+ return min(self.order_line.filtered(lambda x: not x.is_reward_line and not x.is_delivery and x.price_reduce > 0), key=lambda x: x['price_reduce'])
+
+class SalesOrderLine(models.Model):
+ _inherit = "sale.order.line"
+
+ def unlink(self):
+ # Due to delivery_set and delivery_unset methods that are called everywhere, don't unlink
+ # reward lines if it's a free shipping
+ self = self.exists()
+ orders = self.mapped('order_id')
+ applied_programs = orders.mapped('no_code_promo_program_ids') + \
+ orders.mapped('code_promo_program_id') + \
+ orders.mapped('applied_coupon_ids').mapped('program_id')
+ free_shipping_products = applied_programs.filtered(
+ lambda program: program.reward_type == 'free_shipping'
+ ).mapped('discount_line_product_id')
+ lines_to_unlink = self.filtered(lambda line: line.product_id not in free_shipping_products)
+ # Unless these lines are the last ones
+ res = super(SalesOrderLine, lines_to_unlink).unlink()
+ only_free_shipping_line_orders = orders.filtered(lambda order: len(order.order_line.ids) == 1 and order.order_line.is_reward_line)
+ super(SalesOrderLine, only_free_shipping_line_orders.mapped('order_line')).unlink()
+ return res