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 4 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
1 change: 1 addition & 0 deletions rental_deposit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
18 changes: 18 additions & 0 deletions rental_deposit/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
'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'
],
'installable': True,
}
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
14 changes: 14 additions & 0 deletions rental_deposit/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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")

@api.onchange('require_deposit')
def _onchange_require_deposit(self):
self.write({
'deposit_amount': 0 if self.require_deposit else 0

Choose a reason for hiding this comment

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

why?

Copy link
Author

Choose a reason for hiding this comment

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

This is not needed
I was checking that if someone after setting a deposit untick then retick on that time it should be zero.
But it i forgot to remove it
so i will remove it

})
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
69 changes: 69 additions & 0 deletions rental_deposit/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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'].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
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):
"""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))

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),
("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 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
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>
12 changes: 12 additions & 0 deletions rental_deposit/views/website_rent_template.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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">
<t t-set="info" t-value="product"/>
<div t-if="info.require_deposit" class="mb-4">
<strong class="attribute_name">Required Deposit for 1 Quantity:</strong>
<span t-out="info.deposit_amount"/>
</div>
</xpath>
</template>
</odoo>