Skip to content

[ADD] rental: created a rental app for adding deposit for particular product #830

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 2 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
1 change: 1 addition & 0 deletions rental/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
19 changes: 19 additions & 0 deletions rental/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
'name': 'Rental App',
'category': '',
'description': """This module is rental app module""",
'depends': ['sale_renting', 'website_sale_renting'],
'data': [
"views/res_config_settings_views.xml",
"views/product_template_views.xml",
"views/templates.xml",
],
'assets': {
'web.assets_frontend': [
'rental_deposit/static/src/js/rental_deposit.js',
],
},
'application': True,
'license': 'OEEL-1',
"sequence": 1,
}
4 changes: 4 additions & 0 deletions rental/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import res_config_settings
from . import res_company
from . import product_template
from . import sale_order_line
8 changes: 8 additions & 0 deletions rental/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from odoo import fields, models


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

required_deposit = fields.Boolean(default=False)
amount_deposit = fields.Float(string="Deposit Amount")
8 changes: 8 additions & 0 deletions rental/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from odoo import fields, models


class ResCompany(models.Model):
_inherit = "res.company"

product_deposit = fields.Many2one('product.product', string="Product",
domain=[("rent_ok", "=", True)])
9 changes: 9 additions & 0 deletions rental/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from odoo import fields, models


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

product_deposit = fields.Many2one('product.product', string="Deposit",
related="company_id.product_deposit",
readonly=False, domain=[("rent_ok", "=", True)])
46 changes: 46 additions & 0 deletions rental/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from odoo import fields, models, api


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

is_deposit_line = fields.Boolean(default=False)
deposit_link_id = fields.Many2one("sale.order.line", ondelete="cascade")

@api.model_create_multi
def create(self, vals_list):
lines = super().create(vals_list)

deposit_product = self.env.company.product_deposit
deposit_lines = []
for line in lines:
if (line.product_id == deposit_product) and line.product_id.product_tmpl_id.required_deposit:
deposit_price = line.product_id.product_tmpl_id.amount_deposit * line.product_uom_qty
deposit_lines.append({
"is_deposit_line": True,
"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,
"deposit_link_id": line.id,
})
if deposit_lines:
super().create(deposit_lines)
return lines

def write(self, vals):
res = super().write(vals)
if "product_uom_qty" in vals:
for line in self:
if not line.is_deposit_line:
deposit_line = self.search([
("order_id", "=", line.order_id.id),
("deposit_link_id", "=", line.id),
])
if deposit_line:
deposit_line.write({
"product_uom_qty": vals["product_uom_qty"],
"price_unit": line.product_template_id.amount_deposit,
})
return res
26 changes: 26 additions & 0 deletions rental/static/srs/js/rental_deposit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import publicWidget from "@web/legacy/js/public/public_widget";

publicWidget.registry.WebsiteSale.include({
start: function () {
this._super.apply(this, arguments);
this.changeDepositAmount({ target: document.querySelector('input[name="add_qty"]') });
},

_onChangeAddQuantity: function (ev) {
this._super.apply(this, arguments);
this.changeDepositAmount(ev);
},

changeDepositAmount: function(ev){
const input = ev.target || document.querySelector('input[name="add_qty"]');
if (!input) return;
const qty = parseFloat(input.value || 1);
const depositSpan = document.getElementById('product_amount_deposit');
if (!depositSpan) return;

const baseDeposit = parseFloat(depositSpan.dataset.baseAmount || 0);
const currencySymbol = depositSpan.dataset.currencySymbol || '';
const totalDeposit = (baseDeposit * qty).toFixed(2);
depositSpan.textContent = `${currencySymbol} ${totalDeposit}`;
}
});
14 changes: 14 additions & 0 deletions rental/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" model="ir.ui.view">
<field name="name">product.template.form.view.rental.inherit</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="//page//group//field[@name='extra_daily']" position="after">
<field name="required_deposit"/>
<field name="amount_deposit" invisible="not required_deposit"/>
</xpath>
</field>
</record>
</odoo>
16 changes: 16 additions & 0 deletions rental/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>
<record id="res_config_settings_view_form" model="ir.ui.view">
<field name="name">res.config.settings.inherit.view.form</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="//div//div[@class='row mt8']" position="after">
<div class="row mt8">
<label string="Deposit" for="product_deposit" class="col-lg-3 o_light_label"/>
<field name="product_deposit" placeholder="Deposit on product"/>
</div>
</xpath>
</field>
</record>
</odoo>
38 changes: 38 additions & 0 deletions rental/views/templates.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>

<template id="rental_product_with_deposit_amount" inherit_id="website_sale_renting.rental_product"
name="Rental product with deposit amount">
<xpath expr="//div[@id='product_option_block']" position="after">
<div class="mt-3 d-flex justify-content-between" id="product_deposit_section">
<strong>Deposit Required:</strong>
<span id="product_amount_deposit"
t-att-data-base-amount="product.amount_deposit"
t-att-data-currency-symbol="website.company_id.currency_id.symbol">
<t t-out="website.company_id.currency_id.symbol"/>
<t t-out="product.amount_deposit"/>
</span>
</div>
</xpath>
</template>

<template id="cart_line_deposite_amount" inherit_id="website_sale.cart_lines" name="Deposite Amount in Cart">
<xpath expr="//div[@name='website_sale_cart_line_quantity']" position="before">
<t t-set="show_qty" t-value="not line.is_deposit_line"/>
</xpath>
<xpath expr="//div[@name='o_wsale_cart_line_button_container']/a[hasclass('js_delete_product')]"
position="attributes">
<attribute name="t-attf-class">#{'d-none' if line.is_deposit_line else 'js_delete_product'}</attribute>
</xpath>
<xpath expr="//div[@name='o_wsale_cart_line_button_container']" position="before">
<t t-set="deposit_for" t-value="line.deposit_link_id or line"/>
<t t-if="line.is_deposit_line">
<div class="text-muted small mt-2">
This deposit is for
<span class="text-primary fw-semibold ms-1" t-esc="deposit_for.product_id.name"/>
</div>
</t>
</xpath>

</template>
</odoo>