Skip to content

[ADD] rental_deposit: implement deposit amount feature for rental products #898

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 1 commit 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
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
17 changes: 17 additions & 0 deletions rental_deposit/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
'name': 'Rental Deposit',
'version': '1.0',
'category': 'Rental',
'author': 'rodh',
'license': "LGPL-3",
'summary': 'Add deposit product functionality for rentals',
'depends': ['sale_renting', 'website_sale'],
'data': [
'views/res_config_settings_views.xml',
'views/product_template_views.xml',
'views/website_sale_product.xml',
],
'installable': True,
'auto_install': False,
'application': False,
}
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
31 changes: 31 additions & 0 deletions rental_deposit/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from odoo import api, fields, models


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

require_deposit = fields.Boolean(string="Require Deposit")
deposit_amount = fields.Monetary(string="Deposit Amount", currency_field='currency_id')

@api.onchange('require_deposit')
def _onchange_requires_deposit(self):
if self.require_deposit:
param_obj = self.env['ir.config_parameter'].sudo()
deposit_product_id = param_obj.get_param('rental_deposit.deposit_product_id')

Choose a reason for hiding this comment

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

Your res_config_settings uses config_parameter='rental_deposit.product_id' but here you read param_obj.get_param('rental_deposit.deposit_product_id'). Shouldn't this key match? Am I missing anything?

if deposit_product_id:
deposit_product = self.env['product.product'].browse(int(deposit_product_id))
if deposit_product:
self.deposit_amount = deposit_product.list_price
else:
self.deposit_amount = 0.0
else:
self.deposit_amount = 0.0
else:
self.deposit_amount = 0.0


class ProductProduct(models.Model):
_inherit = 'product.product'

require_deposit = fields.Boolean(related='product_tmpl_id.require_deposit', readonly=False)
deposit_amount = fields.Monetary(related='product_tmpl_id.deposit_amount', currency_field='currency_id', readonly=False)
12 changes: 12 additions & 0 deletions rental_deposit/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'

rental_deposit_product_id = fields.Many2one(
'product.product',
string='Deposit Product',
domain=[('sale_ok', '=', True)],
config_parameter='rental_deposit.product_id'
)
35 changes: 35 additions & 0 deletions rental_deposit/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from odoo import api, models


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

@api.model_create_multi
def create(self, vals_list):
orders = super().create(vals_list)
for order in orders:
order._add_deposit_lines()
return orders

def _add_deposit_lines(self):
for line in self.order_line:
product = line.product_id
if product.require_deposit:
exists = self.order_line.filtered(
lambda l: l.is_deposit_line and l.product_id == product
)
if not exists:
self.env['sale.order.line'].create({
'order_id': self.id,
'product_id': product.id,
'name': f"Deposit for {product.display_name}",
'product_uom_qty': line.product_uom_qty,
'price_unit': product.deposit_amount,
'is_deposit_line': True,
})

def _cart_update(self, product_id=None, line_id=None, add_qty=0, set_qty=0, **kwargs):
res = super()._cart_update(product_id=product_id, line_id=line_id, add_qty=add_qty, set_qty=set_qty, **kwargs)
self._add_deposit_lines()

return res
28 changes: 28 additions & 0 deletions rental_deposit/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from odoo import api, fields, models


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

deposit_amount = fields.Monetary(
string="Deposit Amount",
compute="_compute_deposit_amount",
currency_field="currency_id"
)
is_deposit_line = fields.Boolean(
string="Is Deposit Line",
help="Indicates whether this line is for a rental deposit",
default=False
)

@api.depends('product_id')
def _compute_is_deposit_line(self):
for line in self:
line.is_deposit_line = line.product_id and line.product_id.is_deposit_product
Comment on lines +12 to +21

Choose a reason for hiding this comment

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

You define _compute_is_deposit_line but never decorate it or assign it to is_deposit_line either remove it or hook it properly.


def _compute_deposit_amount(self):

Choose a reason for hiding this comment

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

depends on?

for line in self:
if line.product_id.rent_ok:
line.deposit_amount = line.product_id.deposit_amount or (line.price_unit * 0.5)
else:
line.deposit_amount = 0.0
2 changes: 2 additions & 0 deletions rental_deposit/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_sale_order_line_deposit,sale.order.line.deposit,model_sale_order_line,,1,1,1,1
16 changes: 16 additions & 0 deletions rental_deposit/views/product_template_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="product_template_inherit" model="ir.ui.view">
<field name="name">product.template.inherit</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_only_form_view"/>
<field name="arch" type="xml">
<xpath expr="//page[@name='pricing']" position="inside">
<group name="deposit" string="Deposit">
<field name="require_deposit"/>
<field name="deposit_amount" invisible="not require_deposit"/>
</group>
</xpath>
</field>
</record>
</odoo>
13 changes: 13 additions & 0 deletions rental_deposit/views/res_config_settings_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<odoo>
<record id="rental_res_config_settings_view_form_inherit" model="ir.ui.view">
<field name="name">res.config.settings.view.form.deposit</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="base.res_config_settings_view_form"/>
<field name="arch" type="xml">
<setting name="rental_delay_costs" position="inside">
<label for="rental_deposit_product_id" class="col-lg-3"/>
<field name="rental_deposit_product_id" string="Deposit" widget="many2one" options="{'no_create': True}"/>
</setting>
</field>
</record>
</odoo>
29 changes: 29 additions & 0 deletions rental_deposit/views/website_sale_product.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="rental_deposit_product" inherit_id="website_sale.product">
<xpath expr="//div[@id='o_wsale_cta_wrapper']" position="after">
<t t-if="product.rent_ok and product.require_deposit">
<div class="mt16 alert alert-info">
<strong>Deposit Required: </strong>
<span t-esc="product.deposit_amount" t-options="{'widget': 'monetary', 'display_currency': website.currency_id}"/>
</div>
</t>
</xpath>
</template>
<template id="cart_lines_deposit_control" inherit_id="website_sale.cart_lines">
<xpath expr="//div[@name='website_sale_cart_line_quantity']" position="replace">
<div name="website_sale_cart_line_quantity" class="css_quantity input-group">
<a href="#" class="js_add_cart_json btn btn-link border-end-0" aria-label="Remove one">
<i class="fa fa-minus"/>
</a>
<input type="text" class="js_quantity quantity form-control border-start-0 border-end-0" t-att-data-line-id="line.id" t-att-data-product-id="line.product_id.id" t-att-value="line.product_uom_qty"/>
<a href="#" class="js_add_cart_json btn btn-link border-start-0" aria-label="Add one">
<i class="fa fa-plus"/>
</a>
</div>
</xpath>
<xpath expr="//a[hasclass('js_delete_product')]" position="attributes">
<attribute name="t-if">not line.is_deposit_line</attribute>
</xpath>
</template>
</odoo>