|
| 1 | +from odoo import api, fields, models |
| 2 | +from odoo.exceptions import UserError |
| 3 | + |
| 4 | + |
| 5 | +class SaleOrderLine(models.Model): |
| 6 | + _inherit = 'sale.order.line' |
| 7 | + |
| 8 | + is_deposit_line = fields.Boolean(default=False) |
| 9 | + |
| 10 | + parent_rental_line_id = fields.Many2one( |
| 11 | + 'sale.order.line', |
| 12 | + string="Parent Rental Line", |
| 13 | + ondelete="cascade", |
| 14 | + index=True |
| 15 | + ) |
| 16 | + |
| 17 | + @api.model_create_multi |
| 18 | + def create(self, vals_list): |
| 19 | + lines = super().create(vals_list) |
| 20 | + rental_lines = lines.filtered( |
| 21 | + lambda l: l.product_id.rent_ok |
| 22 | + and l.product_id.requires_deposit |
| 23 | + and l.product_id.deposit_amount > 0 |
| 24 | + and not l.is_deposit_line |
| 25 | + ) |
| 26 | + if not rental_lines: |
| 27 | + return lines |
| 28 | + company_map = {} |
| 29 | + for line in rental_lines: |
| 30 | + company = line.company_id |
| 31 | + if company not in company_map: |
| 32 | + if not company.deposit_product: |
| 33 | + raise UserError("Please set deposit product in settings.") |
| 34 | + company_map[company] = company.deposit_product |
| 35 | + deposit_vals = [] |
| 36 | + for line in rental_lines: |
| 37 | + deposit_product = company_map[line.company_id] |
| 38 | + deposit_vals.append({ |
| 39 | + 'order_id': line.order_id.id, |
| 40 | + 'product_id': deposit_product.id, |
| 41 | + 'product_uom_qty': line.product_uom_qty, |
| 42 | + 'price_unit': line.product_id.deposit_amount, |
| 43 | + 'name': f"Deposit fee for product: {line.product_id.name}", |
| 44 | + 'is_deposit_line': True, |
| 45 | + 'parent_rental_line_id': line.id, |
| 46 | + }) |
| 47 | + self.create(deposit_vals) |
| 48 | + return lines |
| 49 | + |
| 50 | + @api.ondelete(at_uninstall=False) |
| 51 | + def _unlink_deposit_fee(self): |
| 52 | + if not self.env.context.get("bypass_deposit_protection_for_delete"): |
| 53 | + for record in self: |
| 54 | + if record.is_deposit_line: |
| 55 | + raise UserError("You can't delete a Deposit Product line directly.") |
| 56 | + |
| 57 | + def write(self, vals): |
| 58 | + if not self.env.context.get("bypass_deposit_protection_for_write"): |
| 59 | + for record in self: |
| 60 | + if record.is_deposit_line: |
| 61 | + raise UserError("You can't edit Deposit Product line directly.") |
| 62 | + res = super().write(vals) |
| 63 | + if 'product_uom_qty' not in vals: |
| 64 | + return res |
| 65 | + rental_lines = self.filtered( |
| 66 | + lambda l: l.product_id.rent_ok |
| 67 | + and l.product_id.requires_deposit |
| 68 | + and l.product_id.deposit_amount > 0 |
| 69 | + and not l.is_deposit_line |
| 70 | + ) |
| 71 | + for line in rental_lines: |
| 72 | + deposit_line = self.search([('parent_rental_line_id', '=', line.id)], limit=1) |
| 73 | + if deposit_line: |
| 74 | + deposit_line.with_context(bypass_deposit_protection_for_write=True).write({ |
| 75 | + 'product_uom_qty': line.product_uom_qty, |
| 76 | + 'price_unit': line.product_id.deposit_amount, |
| 77 | + }) |
| 78 | + return res |
0 commit comments