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 deposit_rental_app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
21 changes: 21 additions & 0 deletions deposit_rental_app/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "Deposit Rental App",
"description": """
Adds a deposit to a rental product on the webshop
""",
"category": "Sales/Sales",
"depends": ["account", "sale_renting", "website_sale"],
"data": [
"views/res_config_settings_views.xml",
"views/product_template_views.xml",
"views/template.xml",
],
"assets": {
"web.assets_frontend": [
"deposit_rental_app/static/src/js/sale_product_field.js",
]
},
"application": True,
"installable": True,
"license": "LGPL-3",
}
4 changes: 4 additions & 0 deletions deposit_rental_app/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
32 changes: 32 additions & 0 deletions deposit_rental_app/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from odoo import fields, models


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

required_deposit = fields.Boolean(default=False, string="Required Deposit")
amount = fields.Monetary(string="Amount", default="0.0")

def _get_combination_info(
self,
combination=False,
product_id=False,
add_qty=1.0,
parent_combination=False,
only_template=False,
):
combination_info = super()._get_combination_info(
combination=combination,
product_id=product_id,
add_qty=add_qty,
parent_combination=parent_combination,
only_template=only_template,
)

combination_info.update(
{
"required_deposit": self.required_deposit,
"amount": self.amount,
}
)
return combination_info
14 changes: 14 additions & 0 deletions deposit_rental_app/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from odoo import fields, models


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

# RENTAL company defaults :

# Deposit product configured in settings
deposit_product_id = fields.Many2one(
"product.product",
string="Deposit",
help="This product will be used to add deposit in the Rental Order.",
)
13 changes: 13 additions & 0 deletions deposit_rental_app/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from odoo import fields, models


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

deposit_product_id = fields.Many2one(
string="Deposit",
help="This product will be used to add deposit in the Rental Order.",
comodel_name="product.product",
related="company_id.deposit_product_id",
readonly=False,
)
76 changes: 76 additions & 0 deletions deposit_rental_app/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from odoo import api, fields, models


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

is_deposit = fields.Boolean(string="Is Deposit", default=False)

def _handle_deposit_product(self):
for line in self:
order = line.order_id

deposit_product = order.company_id.deposit_product_id

if not deposit_product:
continue

if line.is_deposit or not line.product_id.required_deposit:
continue

deposit_amount = line.product_id.amount * line.product_uom_qty

deposit_line = order.order_line.filtered(
lambda l: l.is_deposit
and l.name == f"Deposit for {line.product_id.name}"
)

if deposit_amount > 0:
if deposit_line:
deposit_line.with_context(no_update_deposit=True).write(
{
"price_unit": deposit_amount,
"product_uom_qty": 1,
}
)
else:
order.order_line.with_context(no_update_deposit=True).create(
{
"order_id": order.id,
"product_id": deposit_product.id,
"name": f"Deposit for {line.product_id.name}",
"price_unit": deposit_amount,
"product_uom_qty": 1,
"is_deposit": True,
"product_uom": deposit_product.uom_id.id,
}
)
elif deposit_line:
deposit_line.unlink()

@api.model_create_multi
def create(self, vals_list):
"""Create a product line along with its deposit line"""
records = super().create(vals_list)
if not self.env.context.get("no_update_deposit"):
records._handle_deposit_product()
return records

def write(self, vals):
"""Update the product with its deposit line"""
res = super().write(vals)
if not self.env.context.get("no_update_deposit"):
self._handle_deposit_product()
return res

@api.ondelete(at_uninstall=False)
def _unlink_related_deposits(self):
"""Remove deposit lines when the product is removed from recordset"""
for line in self:
if line.is_deposit or not line.product_id.required_deposit:
continue
deposit_lines = line.order_id.order_line.filtered(
lambda l: l.is_deposit
and l.name == f"Deposit for {line.product_id.name}"
)
deposit_lines.unlink()
19 changes: 19 additions & 0 deletions deposit_rental_app/static/src/js/sale_product_field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { WebsiteSale } from '@website_sale/js/website_sale';


const oldOnChangeCombination = WebsiteSale.prototype._onChangeCombination;

WebsiteSale.include({
_onChangeCombination(ev, $parent, combination) {
oldOnChangeCombination.call(this, ev, $parent, combination);


if (combination && combination.amount !== undefined) {
const $amount = $parent.find("#total_amount");
const quantity = parseFloat($parent.find("input[name='add_qty']").val()) || 1;
console.log("amount and quantity", $amount, quantity);
$amount.text(this._priceToStr(combination.amount * quantity));
console.log("combitnation", combination, $amount.text());
}
},
});
23 changes: 23 additions & 0 deletions deposit_rental_app/views/product_template_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>

<record id="product_template_form_view_rental_inherit" 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="//group[@name='extra_rental']" position="inside">
<field name="required_deposit" widget="checkbox" />
<field name="amount" widget="monetary" invisible="not required_deposit" help="Amount here specifies deposit for 1 unit of this product" />
</xpath>
</field>
</record>

<record id="product_template_action_rental_inherit" model="ir.actions.act_window">
<field name="name">product.template.form.view.rental.extended</field>
<field name="res_model">product.template</field>
<field name="view_id" ref="product_template_form_view_rental_inherit"/>
<field name="view_mode">form</field>
</record>

</odoo>
27 changes: 27 additions & 0 deletions deposit_rental_app/views/res_config_settings_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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.view.form.inherit.rental</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="//div[contains(@class, 'row mt8')]" position="after">
<div class="row mt2">
<label string="Deposit" for="deposit_product_id" class="col-lg-3 o_light_label"/>
<field name="deposit_product_id" placeholder="Product to charge deposit fee" help="Product to charge deposit fee" />
</div>
</xpath>
</field>
</record>

<record id="action_rental_config_settings" model="ir.actions.act_window">
<field name="name">Deposit Settings Extended</field>
<field name="res_model">res.config.settings</field>
<field name="view_id" ref="res_config_settings_view_form"/>
<field name="view_mode">form</field>
<field name="target">inline</field>
<field name="context">{'module' : 'sale_renting', 'bin_size': False}</field>
</record>

</odoo>
12 changes: 12 additions & 0 deletions deposit_rental_app/views/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="rental_product_details_inherited" inherit_id="website_sale.product">
<xpath expr="//div[@id='o_wsale_cta_wrapper']" position="after">
<t t-if="combination_info['required_deposit']">
<p class="fs-5 pt-2 fw-bold">Deposit Required : $ <span id="total_amount" t-out="combination_info['amount']"/></p>
</t>
</xpath>

</template>
</odoo>