64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
from odoo import api, fields, models
|
|
|
|
|
|
class SaleOrderLine(models.Model):
|
|
_inherit = 'sale.order.line'
|
|
|
|
is_security_deposit = fields.Boolean(
|
|
string="Is Security Deposit",
|
|
default=False,
|
|
help="Marks this line as a security deposit for a rental product.",
|
|
)
|
|
rental_deposit_source_line_id = fields.Many2one(
|
|
'sale.order.line',
|
|
string="Deposit For",
|
|
ondelete='cascade',
|
|
help="The rental product line this deposit is associated with.",
|
|
)
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
lines = super().create(vals_list)
|
|
deposit_vals = []
|
|
for line in lines:
|
|
if not line.is_rental or line.is_security_deposit:
|
|
continue
|
|
if not line.order_id.is_rental_order:
|
|
continue
|
|
deposit_amount = line.order_id._compute_deposit_amount_for_line(line)
|
|
if deposit_amount <= 0:
|
|
continue
|
|
existing = line.order_id.order_line.filtered(
|
|
lambda l: (
|
|
l.is_security_deposit
|
|
and l.rental_deposit_source_line_id == line
|
|
)
|
|
)
|
|
if existing:
|
|
continue
|
|
deposit_product = line.order_id._get_deposit_product()
|
|
deposit_vals.append({
|
|
'order_id': line.order_id.id,
|
|
'product_id': deposit_product.id,
|
|
'product_uom_id': deposit_product.uom_id.id,
|
|
'name': f"SECURITY DEPOSIT - REFUNDABLE - {line.product_id.display_name}",
|
|
'product_uom_qty': 1,
|
|
'price_unit': deposit_amount,
|
|
'is_security_deposit': True,
|
|
'rental_deposit_source_line_id': line.id,
|
|
})
|
|
if deposit_vals:
|
|
super().create(deposit_vals)
|
|
return lines
|
|
|
|
def unlink(self):
|
|
deposit_lines = self.env['sale.order.line']
|
|
for line in self:
|
|
if line.is_rental and not line.is_security_deposit:
|
|
deposit_lines |= line.order_id.order_line.filtered(
|
|
lambda l: l.rental_deposit_source_line_id == line
|
|
)
|
|
if deposit_lines:
|
|
deposit_lines.unlink()
|
|
return super().unlink()
|