|
| 1 | +from odoo import api, models, fields |
| 2 | + |
| 3 | + |
| 4 | +class SaleOrderLineRentalInherit(models.Model): |
| 5 | + _inherit = "sale.order.line" |
| 6 | + main_rental_id = fields.Many2one("sale.order.line", ondelete="cascade") |
| 7 | + child_rental_id = fields.Many2one("sale.order.line", ondelete="cascade") |
| 8 | + |
| 9 | + @api.model_create_multi |
| 10 | + def create(self, vals_list): |
| 11 | + res = super().create(vals_list) |
| 12 | + deposit_product_id = ( |
| 13 | + self.env["ir.config_parameter"].sudo().get_param("rental.deposite_product") |
| 14 | + ) |
| 15 | + for line in res: |
| 16 | + if ( |
| 17 | + deposit_product_id |
| 18 | + and line.product_template_id.require_deposite |
| 19 | + and line.product_template_id.deposite_amount |
| 20 | + ): |
| 21 | + child_rental_ol = self.env["sale.order.line"].create( |
| 22 | + { |
| 23 | + "order_id": line.order_id.id, |
| 24 | + "product_id": (int)(deposit_product_id), |
| 25 | + "name": f"Deposit for {line.product_id.name}", |
| 26 | + "product_uom_qty": line.product_uom_qty, |
| 27 | + "price_unit": line.product_template_id.deposite_amount, |
| 28 | + "main_rental_id": line.id, |
| 29 | + } |
| 30 | + ) |
| 31 | + line.child_rental_id = child_rental_ol.id |
| 32 | + return res |
| 33 | + |
| 34 | + def write(self, vals): |
| 35 | + for line in self: |
| 36 | + if line.child_rental_id and "product_uom_qty" in vals: |
| 37 | + qty = vals["product_uom_qty"] |
| 38 | + line.child_rental_id.write( |
| 39 | + { |
| 40 | + "product_uom_qty": qty, |
| 41 | + "price_unit": line.product_template_id.deposite_amount, |
| 42 | + } |
| 43 | + ) |
| 44 | + return super().write(vals) |
0 commit comments