Skip to content
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
18 changes: 18 additions & 0 deletions rental_deposit/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
'author': 'Aditi (adpaw)',
'name': 'Deposit Rental App',
'license': 'LGPL-3',
'depends': ['sale_renting', 'website_sale'],
'data': [
'views/res_config_settings_view.xml',
'views/product_template_view.xml',
'views/template_view.xml'
],
'assets': {
'web.assets_frontend': [
'rental_deposit/static/src/deposit_amount.js',
],
},
'installable': True,
'auto_install': 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 res_config_settings
from . import product_template
from . import sale_order_line
from . import res_company
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 fields, models


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

requires_deposit = fields.Boolean(string="Requires Deposit")
deposit_amount = fields.Float()
7 changes: 7 additions & 0 deletions rental_deposit/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from odoo import fields, models


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

deposit_product = fields.Many2one("product.product")
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'

deposit_product = fields.Many2one(
"product.product",
related="company_id.deposit_product",
string="Deposit",
readonly=False
)
78 changes: 78 additions & 0 deletions rental_deposit/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from odoo import api, fields, models
from odoo.exceptions import UserError


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

is_deposit_line = fields.Boolean(default=False)

parent_rental_line_id = fields.Many2one(
'sale.order.line',
string="Parent Rental Line",
ondelete="cascade",
index=True
)

@api.model_create_multi
def create(self, vals_list):
lines = super().create(vals_list)
rental_lines = lines.filtered(
lambda l: l.product_id.rent_ok
and l.product_id.requires_deposit
and l.product_id.deposit_amount > 0
and not l.is_deposit_line
)
if not rental_lines:
return lines
company_map = {}
for line in rental_lines:
company = line.company_id
if company not in company_map:
if not company.deposit_product:
raise UserError("Please set deposit product in settings.")
company_map[company] = company.deposit_product
deposit_vals = []
for line in rental_lines:
deposit_product = company_map[line.company_id]
deposit_vals.append({
'order_id': line.order_id.id,
'product_id': deposit_product.id,
'product_uom_qty': line.product_uom_qty,
'price_unit': line.product_id.deposit_amount,
'name': f"Deposit fee for product: {line.product_id.name}",
'is_deposit_line': True,
'parent_rental_line_id': line.id,
})
self.create(deposit_vals)
return lines

@api.ondelete(at_uninstall=False)
def _unlink_deposit_fee(self):
if not self.env.context.get("bypass_deposit_protection_for_delete"):
for record in self:
if record.is_deposit_line:
raise UserError("You can't delete a Deposit Product line directly.")

def write(self, vals):
if not self.env.context.get("bypass_deposit_protection_for_write"):
for record in self:
if record.is_deposit_line:
raise UserError("You can't edit Deposit Product line directly.")
res = super().write(vals)
if 'product_uom_qty' not in vals:
return res
rental_lines = self.filtered(
lambda l: l.product_id.rent_ok
and l.product_id.requires_deposit
and l.product_id.deposit_amount > 0
and not l.is_deposit_line
)
for line in rental_lines:
deposit_line = self.search([('parent_rental_line_id', '=', line.id)], limit=1)
if deposit_line:
deposit_line.with_context(bypass_deposit_protection_for_write=True).write({
'product_uom_qty': line.product_uom_qty,
'price_unit': line.product_id.deposit_amount,
})
return res
24 changes: 24 additions & 0 deletions rental_deposit/static/src/deposit_amount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import publicWidget from "@web/legacy/js/public/public_widget";

publicWidget.registry.DepositRental = publicWidget.Widget.extend({
selector: "#product_detail",
events: {
'change input[name="add_qty"]': '_updateDepositAmount',
},

start: function () {
this._super.apply(this, arguments);
if ($("#deposit_amount").length) {
this._updateDepositAmount();
} else {
this.$el.off('change input[name="add_qty"]');
}
},

_updateDepositAmount: function () {
const qty = parseFloat($("#o_wsale_cta_wrapper").find("input[name='add_qty']").val());
const depositAmount = parseFloat($("#deposit_amount").attr("data-base-amount")) || 0;
const currencySymbol = $("#deposit_amount").attr("data-currency-symbol") || "";
$("#deposit_amount").text(currencySymbol + (depositAmount * qty).toFixed(2));
},
})
13 changes: 13 additions & 0 deletions rental_deposit/views/product_template_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<odoo>
<record id="product_template_form_view_rental_deposit" model="ir.ui.view">
<field name="name">product.template.view.form.inherit.rental.deposit</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="//group[@name='extra_rental']" position="inside">
<field name="requires_deposit"/>
<field name="deposit_amount" invisible="not requires_deposit" widget="monetary"/>
</xpath>
</field>
</record>
</odoo>
13 changes: 13 additions & 0 deletions rental_deposit/views/res_config_settings_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<odoo>
<record id="res_config_settings_view_form_deposit_inherit" model="ir.ui.view">
<field name="name">res.config.settings.view.form.inherit.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">
<xpath expr="//field[@name='extra_product']" position="after">
<label for="deposit_product"/>
<field name="deposit_product"/>
</xpath>
</field>
</record>
</odoo>
12 changes: 12 additions & 0 deletions rental_deposit/views/template_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<odoo>
<template id="rental_product_details_inherited" inherit_id="website_sale.product">
<xpath expr="//t[@t-call='website_sale.cta_wrapper']" position="after">
<t t-if="product.rent_ok and product.requires_deposit">
<div class="mt-2">
<strong>Deposit Required: </strong>
<span id="deposit_amount" t-att-data-base-amount="product.deposit_amount"/>
</div>
</t>
</xpath>
</template>
</odoo>