Skip to content

[IMP] rental_deposit: added deposit in rental #422

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

Draft
wants to merge 6 commits into
base: 18.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rental_deposit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import controllers
from . import models
23 changes: 23 additions & 0 deletions rental_deposit/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
'name': "Rental Deposit",
'version': '1.0',
'depends': ['website_sale_renting'],
'author': "ppch",
'category': 'Category',
'description': """
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',
'views/website_rent_template.xml'
],
'assets':{
'web.assets_frontend': [
'rental_deposit/static/src/js/rental_deposit_quantity.js',
],
},
'installable': True,
}
1 change: 1 addition & 0 deletions rental_deposit/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import main
13 changes: 13 additions & 0 deletions rental_deposit/controllers/main.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions rental_deposit/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import product_template
from . import res_config_settings
from . import sale_order
from . import sale_order_line
8 changes: 8 additions & 0 deletions rental_deposit/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from odoo import api, fields, models


class ProductTemplate(models.Model):
_inherit = 'product.template'

require_deposit = fields.Boolean(string="Require Deposit")
deposit_amount = fields.Float(string="Deposit Amount")
11 changes: 11 additions & 0 deletions rental_deposit/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -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'
)
22 changes: 22 additions & 0 deletions rental_deposit/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from odoo import Command, models


class SaleOrder(models.Model):
_inherit = "sale.order"

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))

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where have you define line?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

below line 19, in for loop

Command.create(line)
for line in order._get_copiable_order_lines_without_deposit().copy_data()

for line in order._get_copiable_order_lines_without_deposit().copy_data()
]
return vals_list
77 changes: 77 additions & 0 deletions rental_deposit/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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):
"""Create deposit lines automatically for products that require a deposit."""
lines = super().create(vals_list)

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"))

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
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):
"""Update deposit lines when the main product quantity changes."""
lines = super().write(vals_list)

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."))

deposit_product = self.env['product.product'].browse(int(deposit_product_id))

deposit_lines = []
for line in self:
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)
], limit=1)

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 existing_deposit_line:
existing_deposit_line.write({
"product_uom_qty": line.product_uom_qty,
"price_unit": deposit_price,
})
else:
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,
})
elif existing_deposit_line:
existing_deposit_line.unlink()

if deposit_lines:
self.create(deposit_lines)

return lines
10 changes: 10 additions & 0 deletions rental_deposit/static/src/js/rental_deposit_quantity.js
Original file line number Diff line number Diff line change
@@ -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));
},
});
14 changes: 14 additions & 0 deletions rental_deposit/views/product_template_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="product_template_form_view_rental_deposit" model="ir.ui.view">
<field name="name">product.template.form.inherit.rental.deposit</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="sale_renting.product_template_form_view_rental"/>
<field name="arch" type="xml">
<xpath expr="//group[@name='extra_rental']" position="inside">
<field name="require_deposit"/>
<field name="deposit_amount" invisible="require_deposit == False"/>
</xpath>
</field>
</record>
</odoo>
16 changes: 16 additions & 0 deletions rental_deposit/views/res_config_settings_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!-- Inherited View For Rental Settings -->
<record id="res_config_settings_view_form_inherit_rental_deposit" model="ir.ui.view">
<field name="name">res.config.settings.view.form.inherit.rental.deposit</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="sale_renting.res_config_settings_view_form"/>
<field name="arch" type="xml">
<xpath expr="//setting[@name='rental_delay_costs']" position="after">
<setting string="Deposit Product" help="Set a product to be used for deposit on sale order">
<field name="deposit_product_id" placeholder="Select a product"/>
</setting>
</xpath>
</field>
</record>
</odoo>
11 changes: 11 additions & 0 deletions rental_deposit/views/website_rent_template.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="website_sale_renting_deposit" inherit_id="website_sale_renting.rental_product" name="Deposit">
<xpath expr="//div[@id='product_attributes_simple']" position="after">
<div t-if="product.require_deposit" class="mb-4 o_qty_rental">
<strong>Required Deposit</strong>
<span id="add_qty" t-att-data-deposit="product.deposit_amount">0.00</span>
</div>
</xpath>
</template>
</odoo>