From 316efb28de1169fe5e40a59d4d4c28bc5f3f0a25 Mon Sep 17 00:00:00 2001 From: ppch-odoo Date: Wed, 26 Feb 2025 19:11:01 +0530 Subject: [PATCH 1/6] [IMP] rental_deposit: added deposit in rental config settings added deposit product in config settings by inheriting res_config_settings model added require deposit and deposit amount in product template by inhering product_template model added inherited view for template as well as config settings also inherited view for product template task - 4605761 --- rental_deposit/__init__.py | 1 + rental_deposit/__manifest__.py | 14 ++++++++++++++ rental_deposit/models/__init__.py | 2 ++ rental_deposit/models/product_template.py | 8 ++++++++ rental_deposit/models/res_config_settings.py | 11 +++++++++++ rental_deposit/views/product_template_views.xml | 14 ++++++++++++++ .../views/res_config_settings_views.xml | 16 ++++++++++++++++ 7 files changed, 66 insertions(+) create mode 100644 rental_deposit/__init__.py create mode 100644 rental_deposit/__manifest__.py create mode 100644 rental_deposit/models/__init__.py create mode 100644 rental_deposit/models/product_template.py create mode 100644 rental_deposit/models/res_config_settings.py create mode 100644 rental_deposit/views/product_template_views.xml create mode 100644 rental_deposit/views/res_config_settings_views.xml diff --git a/rental_deposit/__init__.py b/rental_deposit/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/rental_deposit/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/rental_deposit/__manifest__.py b/rental_deposit/__manifest__.py new file mode 100644 index 00000000000..0f463e6443b --- /dev/null +++ b/rental_deposit/__manifest__.py @@ -0,0 +1,14 @@ +{ + 'name': "Rental Deposit", + 'version': '1.0', + 'depends': ['website_sale_renting'], + 'author': "ppch", + 'category': 'Category', + 'description': """ + """,'license': "LGPL-3", + 'data': [ + 'views/res_config_settings_views.xml', + 'views/product_template_views.xml', + ], + 'installable': True, +} diff --git a/rental_deposit/models/__init__.py b/rental_deposit/models/__init__.py new file mode 100644 index 00000000000..5f10144de79 --- /dev/null +++ b/rental_deposit/models/__init__.py @@ -0,0 +1,2 @@ +from . import res_config_settings +from . import product_template diff --git a/rental_deposit/models/product_template.py b/rental_deposit/models/product_template.py new file mode 100644 index 00000000000..cf666ff190e --- /dev/null +++ b/rental_deposit/models/product_template.py @@ -0,0 +1,8 @@ +from odoo import fields, models + + +class ProductTemplate(models.Model): + _inherit = 'product.template' + + require_deposit = fields.Boolean(string="Require Deposit") + deposit_amount = fields.Float(string="Deposit Amount") diff --git a/rental_deposit/models/res_config_settings.py b/rental_deposit/models/res_config_settings.py new file mode 100644 index 00000000000..80860ae3a63 --- /dev/null +++ b/rental_deposit/models/res_config_settings.py @@ -0,0 +1,11 @@ +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = 'res.config.settings' + + deposit_product_id = fields.Many2one( + 'product.product', + string="Deposit Product", + config_parameter='rental_deposit.deposit_product_id' + ) diff --git a/rental_deposit/views/product_template_views.xml b/rental_deposit/views/product_template_views.xml new file mode 100644 index 00000000000..6059c5f8015 --- /dev/null +++ b/rental_deposit/views/product_template_views.xml @@ -0,0 +1,14 @@ + + + + product.template.form.inherit.rental.deposit + product.template + + + + + + + + + diff --git a/rental_deposit/views/res_config_settings_views.xml b/rental_deposit/views/res_config_settings_views.xml new file mode 100644 index 00000000000..c242ad3aac3 --- /dev/null +++ b/rental_deposit/views/res_config_settings_views.xml @@ -0,0 +1,16 @@ + + + + + res.config.settings.view.form.inherit.rental.deposit + res.config.settings + + + + + + + + + + From 2e0f893059389131b513fe7c5f3c8855bcca9d96 Mon Sep 17 00:00:00 2001 From: ppch-odoo Date: Thu, 27 Feb 2025 19:00:28 +0530 Subject: [PATCH 2/6] [IMP] rental_deposit: added deposit in sale order line - Deposit will be added in sale order line when any product is selected in which deposit is configured - Also added write override, if product or quantity is changed then it reflects to deposit also - Added unlink override if product which has deposit and removed it from sale order line then remove deposit also - In website module on rental product, required deposit will be displayed task - 4605761 --- rental_deposit/__manifest__.py | 1 + rental_deposit/models/__init__.py | 3 +- rental_deposit/models/sale_order_line.py | 72 +++++++++++++++++++ .../views/website_rent_template.xml | 12 ++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 rental_deposit/models/sale_order_line.py create mode 100644 rental_deposit/views/website_rent_template.xml diff --git a/rental_deposit/__manifest__.py b/rental_deposit/__manifest__.py index 0f463e6443b..4c4287c9cb3 100644 --- a/rental_deposit/__manifest__.py +++ b/rental_deposit/__manifest__.py @@ -9,6 +9,7 @@ 'data': [ 'views/res_config_settings_views.xml', 'views/product_template_views.xml', + 'views/website_rent_template.xml' ], 'installable': True, } diff --git a/rental_deposit/models/__init__.py b/rental_deposit/models/__init__.py index 5f10144de79..96a66425976 100644 --- a/rental_deposit/models/__init__.py +++ b/rental_deposit/models/__init__.py @@ -1,2 +1,3 @@ -from . import res_config_settings from . import product_template +from . import res_config_settings +from . import sale_order_line diff --git a/rental_deposit/models/sale_order_line.py b/rental_deposit/models/sale_order_line.py new file mode 100644 index 00000000000..c88f75cb00f --- /dev/null +++ b/rental_deposit/models/sale_order_line.py @@ -0,0 +1,72 @@ +from odoo import _, api, fields, models +from odoo.exceptions import UserError + + +class SaleOrderLine(models.Model): + _inherit = "sale.order.line" + + @api.model_create_multi + def create(self, vals_list): + lines = super(SaleOrderLine, self).create(vals_list) + deposit_product_id = self.env['ir.config_parameter'].sudo().get_param('rental_deposit.deposit_product_id') + deposit_product = self.env["product.product"].browse(int(deposit_product_id)) + deposit_lines = [] + for line in lines: + if line.product_id and line.product_id.product_tmpl_id.require_deposit: + if not deposit_product_id: + raise UserError(_("Please select deposit product from configuration")) + deposit_price = line.product_id.product_tmpl_id.deposit_amount if line.product_id.product_tmpl_id.deposit_amount else deposit_product.lst_price + existing_deposit_lines = self.env["sale.order.line"].search([ + ("order_id", "=", line.order_id.id), + ("product_id", "=", deposit_product.id), + ("linked_line_id", "=", line.id) + ], limit=1) + if not existing_deposit_lines: + deposit_lines.append({ + "order_id": line.order_id.id, + "product_id": deposit_product.id, + "name": f"Deposit for {line.product_id.name}", + "product_uom_qty": line.product_uom_qty, + "price_unit": deposit_price, + "linked_line_id": line.id, + }) + if deposit_lines: + self.create(deposit_lines) + return lines + + def write(self, vals_list): + lines = super(SaleOrderLine, self).write(vals_list) + + deposit_product_id = self.env["ir.config_parameter"].sudo().get_param("rental_deposit.deposit_product_id") + deposit_product = self.env["product.product"].browse(int(deposit_product_id)) + + for line in self: + deposit_line = self.env["sale.order.line"].search([ + ("order_id", "=", line.order_id.id), + ("linked_line_id", "=", line.id), + ("product_id", "=", deposit_product.id) + ], limit=1) + + if line.product_id and line.product_id.product_tmpl_id.require_deposit: + if not deposit_product_id: + raise UserError(_("Please select a deposit product from configuration.")) + deposit_price = line.product_id.product_tmpl_id.deposit_amount or deposit_product.lst_price + if deposit_line: + deposit_line.write({ + "product_uom_qty": line.product_uom_qty, + "price_unit": deposit_price, + }) + else: + self.create({ + "order_id": line.order_id.id, + "product_id": deposit_product.id, + "name": f"Deposit for {line.product_id.name}", + "product_uom_qty": line.product_uom_qty, + "price_unit": deposit_price, + "linked_line_id": line.id, + }) + + elif deposit_line: + deposit_line.unlink() + + return lines diff --git a/rental_deposit/views/website_rent_template.xml b/rental_deposit/views/website_rent_template.xml new file mode 100644 index 00000000000..f86a5863173 --- /dev/null +++ b/rental_deposit/views/website_rent_template.xml @@ -0,0 +1,12 @@ + + + + From 08a1350926f928ca5f458381cb50d783e468eeaa Mon Sep 17 00:00:00 2001 From: ppch-odoo Date: Fri, 28 Feb 2025 19:08:09 +0530 Subject: [PATCH 3/6] [IMP] rental_deposit: Improved by adding below changes - Ensured that only one deposit added when duplicating sale order - Controller added to track qty from website. - improved sale order line creation logic task - 4605761 --- rental_deposit/__init__.py | 1 + rental_deposit/controllers/__init__.py | 1 + rental_deposit/controllers/main.py | 14 ++++++++ rental_deposit/models/__init__.py | 1 + rental_deposit/models/sale_order.py | 17 +++++++++ rental_deposit/models/sale_order_line.py | 35 ++++++++----------- .../views/website_rent_template.xml | 2 +- 7 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 rental_deposit/controllers/__init__.py create mode 100644 rental_deposit/controllers/main.py create mode 100644 rental_deposit/models/sale_order.py diff --git a/rental_deposit/__init__.py b/rental_deposit/__init__.py index 0650744f6bc..91c5580fed3 100644 --- a/rental_deposit/__init__.py +++ b/rental_deposit/__init__.py @@ -1 +1,2 @@ +from . import controllers from . import models diff --git a/rental_deposit/controllers/__init__.py b/rental_deposit/controllers/__init__.py new file mode 100644 index 00000000000..12a7e529b67 --- /dev/null +++ b/rental_deposit/controllers/__init__.py @@ -0,0 +1 @@ +from . import main diff --git a/rental_deposit/controllers/main.py b/rental_deposit/controllers/main.py new file mode 100644 index 00000000000..c974949bcb4 --- /dev/null +++ b/rental_deposit/controllers/main.py @@ -0,0 +1,14 @@ +from odoo.http import request, route +from odoo.addons.website_sale.controllers.variant import WebsiteSaleVariantController + +class WebsiteSaleRentingVariantController(WebsiteSaleVariantController): + + @route('/website_sale/get_combination_info', type='json', auth='public', methods=['POST'], website=True) + def get_combination_info_website( + self, product_template_id, product_id, combination, add_qty, parent_combination=None, + **kwargs + ): + combination_info = super().get_combination_info_website(product_template_id, product_id, combination, add_qty, parent_combination=None, **kwargs) + combination_info['add_qty'] = add_qty + print(combination_info) + return combination_info diff --git a/rental_deposit/models/__init__.py b/rental_deposit/models/__init__.py index 96a66425976..3b8e35bd3bc 100644 --- a/rental_deposit/models/__init__.py +++ b/rental_deposit/models/__init__.py @@ -1,3 +1,4 @@ from . import product_template from . import res_config_settings +from . import sale_order from . import sale_order_line diff --git a/rental_deposit/models/sale_order.py b/rental_deposit/models/sale_order.py new file mode 100644 index 00000000000..02d31c58200 --- /dev/null +++ b/rental_deposit/models/sale_order.py @@ -0,0 +1,17 @@ +from odoo import models + + +class SaleOrder(models.Model): + _inherit = "sale.order" + + def copy(self, default=None): + """Override copy to prevent deposit lines from being duplicated.""" + default = dict(default or {}) + + deposit_product_id = self.env["ir.config_parameter"].sudo().get_param("rental_deposit.deposit_product_id") + + filtered_lines = self.order_line.filtered(lambda line: not line.product_id.id == int(deposit_product_id)) + + default["order_line"] = [(0, 0, line.copy_data()[0]) for line in filtered_lines] + + return super().copy(default) diff --git a/rental_deposit/models/sale_order_line.py b/rental_deposit/models/sale_order_line.py index c88f75cb00f..d2471b1f342 100644 --- a/rental_deposit/models/sale_order_line.py +++ b/rental_deposit/models/sale_order_line.py @@ -8,30 +8,22 @@ class SaleOrderLine(models.Model): @api.model_create_multi def create(self, vals_list): lines = super(SaleOrderLine, self).create(vals_list) + deposit_product_id = self.env['ir.config_parameter'].sudo().get_param('rental_deposit.deposit_product_id') deposit_product = self.env["product.product"].browse(int(deposit_product_id)) - deposit_lines = [] + if not deposit_product_id: + raise UserError(_("Please select deposit product from configuration")) for line in lines: if line.product_id and line.product_id.product_tmpl_id.require_deposit: - if not deposit_product_id: - raise UserError(_("Please select deposit product from configuration")) deposit_price = line.product_id.product_tmpl_id.deposit_amount if line.product_id.product_tmpl_id.deposit_amount else deposit_product.lst_price - existing_deposit_lines = self.env["sale.order.line"].search([ - ("order_id", "=", line.order_id.id), - ("product_id", "=", deposit_product.id), - ("linked_line_id", "=", line.id) - ], limit=1) - if not existing_deposit_lines: - deposit_lines.append({ - "order_id": line.order_id.id, - "product_id": deposit_product.id, - "name": f"Deposit for {line.product_id.name}", - "product_uom_qty": line.product_uom_qty, - "price_unit": deposit_price, - "linked_line_id": line.id, - }) - if deposit_lines: - self.create(deposit_lines) + self.create([{ + "order_id": line.order_id.id, + "product_id": deposit_product.id, + "name": f"Deposit for {line.product_id.name}", + "product_uom_qty": line.product_uom_qty, + "price_unit": deposit_price, + "linked_line_id": line.id, + }]) return lines def write(self, vals_list): @@ -40,6 +32,9 @@ def write(self, vals_list): deposit_product_id = self.env["ir.config_parameter"].sudo().get_param("rental_deposit.deposit_product_id") deposit_product = self.env["product.product"].browse(int(deposit_product_id)) + if not deposit_product_id: + raise UserError(_("Please select a deposit product from configuration.")) + for line in self: deposit_line = self.env["sale.order.line"].search([ ("order_id", "=", line.order_id.id), @@ -48,8 +43,6 @@ def write(self, vals_list): ], limit=1) if line.product_id and line.product_id.product_tmpl_id.require_deposit: - if not deposit_product_id: - raise UserError(_("Please select a deposit product from configuration.")) deposit_price = line.product_id.product_tmpl_id.deposit_amount or deposit_product.lst_price if deposit_line: deposit_line.write({ diff --git a/rental_deposit/views/website_rent_template.xml b/rental_deposit/views/website_rent_template.xml index f86a5863173..5a589fd8a63 100644 --- a/rental_deposit/views/website_rent_template.xml +++ b/rental_deposit/views/website_rent_template.xml @@ -4,7 +4,7 @@
- Required Deposit: + Required Deposit for 1 Quantity:
From 3c41b97a10180d3d27fb9686faa290f5d74d79a0 Mon Sep 17 00:00:00 2001 From: ppch-odoo Date: Mon, 3 Mar 2025 10:08:24 +0530 Subject: [PATCH 4/6] [IMP] rental_deposit: Improved by overriding copy_data - Ensured that on duplicating more than one sale order ensure that it works perfectly - Onchange method is defined in product_template task - 4605761 --- rental_deposit/__init__.py | 1 - rental_deposit/__manifest__.py | 5 ++++- rental_deposit/controllers/__init__.py | 1 - rental_deposit/controllers/main.py | 14 ------------ rental_deposit/models/product_template.py | 8 ++++++- rental_deposit/models/sale_order.py | 27 ++++++++++++++--------- rental_deposit/models/sale_order_line.py | 14 +++++++----- 7 files changed, 36 insertions(+), 34 deletions(-) delete mode 100644 rental_deposit/controllers/__init__.py delete mode 100644 rental_deposit/controllers/main.py diff --git a/rental_deposit/__init__.py b/rental_deposit/__init__.py index 91c5580fed3..0650744f6bc 100644 --- a/rental_deposit/__init__.py +++ b/rental_deposit/__init__.py @@ -1,2 +1 @@ -from . import controllers from . import models diff --git a/rental_deposit/__manifest__.py b/rental_deposit/__manifest__.py index 4c4287c9cb3..b7940786ca2 100644 --- a/rental_deposit/__manifest__.py +++ b/rental_deposit/__manifest__.py @@ -5,7 +5,10 @@ 'author': "ppch", 'category': 'Category', 'description': """ - """,'license': "LGPL-3", + Rental Deposit is configured and it will be added as deposit product whenever any product + which has deposit required will be true and it will work in both frontend and backend + """, + 'license': "LGPL-3", 'data': [ 'views/res_config_settings_views.xml', 'views/product_template_views.xml', diff --git a/rental_deposit/controllers/__init__.py b/rental_deposit/controllers/__init__.py deleted file mode 100644 index 12a7e529b67..00000000000 --- a/rental_deposit/controllers/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from . import main diff --git a/rental_deposit/controllers/main.py b/rental_deposit/controllers/main.py deleted file mode 100644 index c974949bcb4..00000000000 --- a/rental_deposit/controllers/main.py +++ /dev/null @@ -1,14 +0,0 @@ -from odoo.http import request, route -from odoo.addons.website_sale.controllers.variant import WebsiteSaleVariantController - -class WebsiteSaleRentingVariantController(WebsiteSaleVariantController): - - @route('/website_sale/get_combination_info', type='json', auth='public', methods=['POST'], website=True) - def get_combination_info_website( - self, product_template_id, product_id, combination, add_qty, parent_combination=None, - **kwargs - ): - combination_info = super().get_combination_info_website(product_template_id, product_id, combination, add_qty, parent_combination=None, **kwargs) - combination_info['add_qty'] = add_qty - print(combination_info) - return combination_info diff --git a/rental_deposit/models/product_template.py b/rental_deposit/models/product_template.py index cf666ff190e..9d082b2eb72 100644 --- a/rental_deposit/models/product_template.py +++ b/rental_deposit/models/product_template.py @@ -1,4 +1,4 @@ -from odoo import fields, models +from odoo import api, fields, models class ProductTemplate(models.Model): @@ -6,3 +6,9 @@ class ProductTemplate(models.Model): require_deposit = fields.Boolean(string="Require Deposit") deposit_amount = fields.Float(string="Deposit Amount") + + @api.onchange('require_deposit') + def _onchange_require_deposit(self): + self.write({ + 'deposit_amount': 0 if self.require_deposit else 0 + }) diff --git a/rental_deposit/models/sale_order.py b/rental_deposit/models/sale_order.py index 02d31c58200..fd20d2fb9e0 100644 --- a/rental_deposit/models/sale_order.py +++ b/rental_deposit/models/sale_order.py @@ -1,17 +1,22 @@ -from odoo import models +from odoo import Command, models class SaleOrder(models.Model): _inherit = "sale.order" - def copy(self, default=None): - """Override copy to prevent deposit lines from being duplicated.""" - default = dict(default or {}) - - deposit_product_id = self.env["ir.config_parameter"].sudo().get_param("rental_deposit.deposit_product_id") - - filtered_lines = self.order_line.filtered(lambda line: not line.product_id.id == int(deposit_product_id)) + def _get_copiable_order_lines_without_deposit(self): + """Returns the order lines that can be copied to a new order without copying deposit order line.""" + deposit_product_id = self.env['ir.config_parameter'].get_param('rental_deposit.deposit_product_id') + return self.order_line.filtered(lambda line: line.product_id.id != int(deposit_product_id)) - default["order_line"] = [(0, 0, line.copy_data()[0]) for line in filtered_lines] - - return super().copy(default) + def copy_data(self, default=None): + """Override copy_data to prevent deposit lines from being duplicated.""" + default = dict(default or {}) + default.setdefault('order_line', []) + vals_list = super().copy_data(default=default) + for order, vals in zip(self, vals_list): + vals['order_line'] = [ + Command.create(line) + for line in order._get_copiable_order_lines_without_deposit().copy_data() + ] + return vals_list diff --git a/rental_deposit/models/sale_order_line.py b/rental_deposit/models/sale_order_line.py index d2471b1f342..bb5e04017dc 100644 --- a/rental_deposit/models/sale_order_line.py +++ b/rental_deposit/models/sale_order_line.py @@ -7,12 +7,15 @@ class SaleOrderLine(models.Model): @api.model_create_multi def create(self, vals_list): - lines = super(SaleOrderLine, self).create(vals_list) + """Create deposit lines automatically for products that require a deposit.""" + lines = super().create(vals_list) deposit_product_id = self.env['ir.config_parameter'].sudo().get_param('rental_deposit.deposit_product_id') deposit_product = self.env["product.product"].browse(int(deposit_product_id)) + if not deposit_product_id: raise UserError(_("Please select deposit product from configuration")) + for line in lines: if line.product_id and line.product_id.product_tmpl_id.require_deposit: deposit_price = line.product_id.product_tmpl_id.deposit_amount if line.product_id.product_tmpl_id.deposit_amount else deposit_product.lst_price @@ -27,16 +30,17 @@ def create(self, vals_list): return lines def write(self, vals_list): - lines = super(SaleOrderLine, self).write(vals_list) + """Update deposit lines when the main product quantity changes.""" + lines = super().write(vals_list) - deposit_product_id = self.env["ir.config_parameter"].sudo().get_param("rental_deposit.deposit_product_id") - deposit_product = self.env["product.product"].browse(int(deposit_product_id)) + deposit_product_id = self.env['ir.config_parameter'].sudo().get_param('rental_deposit.deposit_product_id') + deposit_product = self.env['product.product'].browse(int(deposit_product_id)) if not deposit_product_id: raise UserError(_("Please select a deposit product from configuration.")) for line in self: - deposit_line = self.env["sale.order.line"].search([ + deposit_line = self.env['sale.order.line'].search([ ("order_id", "=", line.order_id.id), ("linked_line_id", "=", line.id), ("product_id", "=", deposit_product.id) From ea8e25918ee0c0973b32797ffb32fe1e88f17d2e Mon Sep 17 00:00:00 2001 From: ppch-odoo Date: Thu, 6 Mar 2025 18:32:29 +0530 Subject: [PATCH 5/6] [IMP] rental_deposit: improved Rental Deposit by some changes - Removed onchange_require_deposit as not needed - Instead of direct creating deposit line used list to create it - Extended controller class to get qty in website - Extended Widget to display deposit which change based on required quantity --- rental_deposit/__init__.py | 1 + rental_deposit/__manifest__.py | 5 +++ rental_deposit/controllers/__init__.py | 1 + rental_deposit/controllers/main.py | 13 ++++++++ rental_deposit/models/product_template.py | 6 ---- rental_deposit/models/sale_order_line.py | 32 ++++++++++++------- .../static/src/js/rental_deposit_quantity.js | 10 ++++++ .../views/website_rent_template.xml | 7 ++-- 8 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 rental_deposit/controllers/__init__.py create mode 100644 rental_deposit/controllers/main.py create mode 100644 rental_deposit/static/src/js/rental_deposit_quantity.js diff --git a/rental_deposit/__init__.py b/rental_deposit/__init__.py index 0650744f6bc..91c5580fed3 100644 --- a/rental_deposit/__init__.py +++ b/rental_deposit/__init__.py @@ -1 +1,2 @@ +from . import controllers from . import models diff --git a/rental_deposit/__manifest__.py b/rental_deposit/__manifest__.py index b7940786ca2..5b35102fd2f 100644 --- a/rental_deposit/__manifest__.py +++ b/rental_deposit/__manifest__.py @@ -14,5 +14,10 @@ 'views/product_template_views.xml', 'views/website_rent_template.xml' ], + 'assets':{ + 'web.assets_frontend': [ + 'rental_deposit/static/src/js/rental_deposit_quantity.js', + ], + }, 'installable': True, } diff --git a/rental_deposit/controllers/__init__.py b/rental_deposit/controllers/__init__.py new file mode 100644 index 00000000000..12a7e529b67 --- /dev/null +++ b/rental_deposit/controllers/__init__.py @@ -0,0 +1 @@ +from . import main diff --git a/rental_deposit/controllers/main.py b/rental_deposit/controllers/main.py new file mode 100644 index 00000000000..913482bc033 --- /dev/null +++ b/rental_deposit/controllers/main.py @@ -0,0 +1,13 @@ +from odoo.http import request, route +from odoo.addons.website_sale.controllers.variant import WebsiteSaleVariantController + +class WebsiteSaleRentingVariantController(WebsiteSaleVariantController): + + @route('/website_sale/get_combination_info', type='json', auth='public', methods=['POST'], website=True) + def get_combination_info_website( + self, product_template_id, product_id, combination, add_qty, parent_combination=None, + **kwargs + ): + combination_info = super().get_combination_info_website(product_template_id, product_id, combination, add_qty, parent_combination=None, **kwargs) + combination_info['add_qty'] = add_qty + return combination_info diff --git a/rental_deposit/models/product_template.py b/rental_deposit/models/product_template.py index 9d082b2eb72..6d6d5e2323d 100644 --- a/rental_deposit/models/product_template.py +++ b/rental_deposit/models/product_template.py @@ -6,9 +6,3 @@ class ProductTemplate(models.Model): require_deposit = fields.Boolean(string="Require Deposit") deposit_amount = fields.Float(string="Deposit Amount") - - @api.onchange('require_deposit') - def _onchange_require_deposit(self): - self.write({ - 'deposit_amount': 0 if self.require_deposit else 0 - }) diff --git a/rental_deposit/models/sale_order_line.py b/rental_deposit/models/sale_order_line.py index bb5e04017dc..fc12e564746 100644 --- a/rental_deposit/models/sale_order_line.py +++ b/rental_deposit/models/sale_order_line.py @@ -11,22 +11,27 @@ def create(self, vals_list): lines = super().create(vals_list) deposit_product_id = self.env['ir.config_parameter'].sudo().get_param('rental_deposit.deposit_product_id') - deposit_product = self.env["product.product"].browse(int(deposit_product_id)) - if not deposit_product_id: raise UserError(_("Please select deposit product from configuration")) + deposit_product = self.env["product.product"].browse(int(deposit_product_id)) + + deposit_lines = [] for line in lines: if line.product_id and line.product_id.product_tmpl_id.require_deposit: deposit_price = line.product_id.product_tmpl_id.deposit_amount if line.product_id.product_tmpl_id.deposit_amount else deposit_product.lst_price - self.create([{ + deposit_lines.append({ "order_id": line.order_id.id, "product_id": deposit_product.id, "name": f"Deposit for {line.product_id.name}", "product_uom_qty": line.product_uom_qty, "price_unit": deposit_price, "linked_line_id": line.id, - }]) + }) + + if deposit_lines: + self.create(deposit_lines) + return lines def write(self, vals_list): @@ -34,13 +39,14 @@ def write(self, vals_list): lines = super().write(vals_list) deposit_product_id = self.env['ir.config_parameter'].sudo().get_param('rental_deposit.deposit_product_id') - deposit_product = self.env['product.product'].browse(int(deposit_product_id)) - if not deposit_product_id: raise UserError(_("Please select a deposit product from configuration.")) + deposit_product = self.env['product.product'].browse(int(deposit_product_id)) + + deposit_lines = [] for line in self: - deposit_line = self.env['sale.order.line'].search([ + existing_deposit_line = self.env['sale.order.line'].search([ ("order_id", "=", line.order_id.id), ("linked_line_id", "=", line.id), ("product_id", "=", deposit_product.id) @@ -48,13 +54,13 @@ def write(self, vals_list): if line.product_id and line.product_id.product_tmpl_id.require_deposit: deposit_price = line.product_id.product_tmpl_id.deposit_amount or deposit_product.lst_price - if deposit_line: - deposit_line.write({ + if existing_deposit_line: + existing_deposit_line.write({ "product_uom_qty": line.product_uom_qty, "price_unit": deposit_price, }) else: - self.create({ + deposit_lines.append({ "order_id": line.order_id.id, "product_id": deposit_product.id, "name": f"Deposit for {line.product_id.name}", @@ -62,8 +68,10 @@ def write(self, vals_list): "price_unit": deposit_price, "linked_line_id": line.id, }) + elif existing_deposit_line: + existing_deposit_line.unlink() - elif deposit_line: - deposit_line.unlink() + if deposit_lines: + self.create(deposit_lines) return lines diff --git a/rental_deposit/static/src/js/rental_deposit_quantity.js b/rental_deposit/static/src/js/rental_deposit_quantity.js new file mode 100644 index 00000000000..a21a4940055 --- /dev/null +++ b/rental_deposit/static/src/js/rental_deposit_quantity.js @@ -0,0 +1,10 @@ +import publicWidget from '@web/legacy/js/public/public_widget'; + +publicWidget.registry.WebsiteSale.include({ + _onChangeCombination(ev, $parent, combination) { + let qtyDisplay = this.$el.find("#add_qty"); + let depositAmount = qtyDisplay.data("deposit"); + let totalDeposit = combination.add_qty * depositAmount; + qtyDisplay.text(totalDeposit.toFixed(2)); + }, +}); diff --git a/rental_deposit/views/website_rent_template.xml b/rental_deposit/views/website_rent_template.xml index 5a589fd8a63..af354548f89 100644 --- a/rental_deposit/views/website_rent_template.xml +++ b/rental_deposit/views/website_rent_template.xml @@ -2,10 +2,9 @@ From 908ad13cdbde6352dad98d43b43f808c40eea91c Mon Sep 17 00:00:00 2001 From: ppch-odoo Date: Thu, 20 Mar 2025 14:31:42 +0530 Subject: [PATCH 6/6] [IMP] rental_deposit: sudo() removed - sudo() removed as without bypassing access rights we can get deposit product --- rental_deposit/models/sale_order_line.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rental_deposit/models/sale_order_line.py b/rental_deposit/models/sale_order_line.py index fc12e564746..b490ff73193 100644 --- a/rental_deposit/models/sale_order_line.py +++ b/rental_deposit/models/sale_order_line.py @@ -10,7 +10,7 @@ def create(self, vals_list): """Create deposit lines automatically for products that require a deposit.""" lines = super().create(vals_list) - deposit_product_id = self.env['ir.config_parameter'].sudo().get_param('rental_deposit.deposit_product_id') + deposit_product_id = self.env['ir.config_parameter'].get_param('rental_deposit.deposit_product_id') if not deposit_product_id: raise UserError(_("Please select deposit product from configuration")) @@ -38,7 +38,7 @@ def write(self, vals_list): """Update deposit lines when the main product quantity changes.""" lines = super().write(vals_list) - deposit_product_id = self.env['ir.config_parameter'].sudo().get_param('rental_deposit.deposit_product_id') + deposit_product_id = self.env['ir.config_parameter'].get_param('rental_deposit.deposit_product_id') if not deposit_product_id: raise UserError(_("Please select a deposit product from configuration."))