|
| 1 | +from odoo import api, fields, models |
| 2 | +from odoo.exceptions import UserError, MissingError |
| 3 | +from odoo.tools import _ |
| 4 | + |
| 5 | + |
| 6 | +class SaleOrderLine(models.Model): |
| 7 | + _inherit = 'sale.order.line' |
| 8 | + |
| 9 | + is_deposit_line = fields.Boolean(default=False) |
| 10 | + |
| 11 | + @api.model |
| 12 | + def create(self, vals_list): |
| 13 | + lines = super().create(vals_list) |
| 14 | + rental_lines = lines.filtered( |
| 15 | + lambda l: l.product_id.rent_ok |
| 16 | + and l.product_id.requires_deposit |
| 17 | + and l.product_id.deposit_amount > 0 |
| 18 | + ) |
| 19 | + if not rental_lines: |
| 20 | + return lines |
| 21 | + company_map = {} |
| 22 | + for line in rental_lines: |
| 23 | + company = line.company_id |
| 24 | + if company not in company_map: |
| 25 | + if not company.deposit_product: |
| 26 | + raise UserError(_("Please set deposit product in settings.")) |
| 27 | + company_map[company] = company.deposit_product |
| 28 | + deposit_vals = [] |
| 29 | + for line in rental_lines: |
| 30 | + deposit_product = company_map[line.company_id] |
| 31 | + deposit_vals.append({ |
| 32 | + 'order_id': line.order_id.id, |
| 33 | + 'product_id': deposit_product.id, |
| 34 | + 'product_uom_qty': line.product_uom_qty, |
| 35 | + 'price_unit': line.product_id.deposit_amount, |
| 36 | + 'name': f"This amount is deposit for {line.product_id.name} product", |
| 37 | + 'is_deposit_line': True, |
| 38 | + }) |
| 39 | + self.create(deposit_vals) |
| 40 | + return lines |
| 41 | + |
| 42 | + @api.ondelete(at_uninstall=False) |
| 43 | + def _unlink_deposit_fee(self): |
| 44 | + if not self.env.context.get("bypass_deposit_protection_for_delete"): |
| 45 | + for record in self: |
| 46 | + if record.is_deposit_line: |
| 47 | + raise UserError(_("You can't delete a Deposit Product line directly.")) |
| 48 | + rental_lines = self.filtered( |
| 49 | + lambda l: l.product_id.rent_ok |
| 50 | + and l.product_id.requires_deposit |
| 51 | + and l.product_id.deposit_amount > 0 |
| 52 | + ) |
| 53 | + if not rental_lines: |
| 54 | + return |
| 55 | + deposit_lines = self.search([ |
| 56 | + ('order_id', 'in', rental_lines.mapped('order_id').ids), |
| 57 | + ('is_deposit_line', '=', True), |
| 58 | + ]) |
| 59 | + deposit_map = {} |
| 60 | + for line in deposit_lines: |
| 61 | + deposit_map[line.name] = line |
| 62 | + for line in rental_lines: |
| 63 | + deposit_line = deposit_map.get( |
| 64 | + f"This amount is deposit for {line.product_id.name} product" |
| 65 | + ) |
| 66 | + if not deposit_line: |
| 67 | + raise MissingError(_("Deposit fee is not present.")) |
| 68 | + deposit_line.with_context(bypass_deposit_protection_for_delete=True).unlink() |
| 69 | + |
| 70 | + def write(self, vals): |
| 71 | + if not self.env.context.get("bypass_deposit_protection_for_write"): |
| 72 | + for record in self: |
| 73 | + if record.is_deposit_line: |
| 74 | + raise UserError(_("You can't edit Deposit Product line directly.")) |
| 75 | + res = super().write(vals) |
| 76 | + if 'product_uom_qty' not in vals: |
| 77 | + return res |
| 78 | + rental_lines = self.filtered( |
| 79 | + lambda l: l.product_id.rent_ok |
| 80 | + and l.product_id.requires_deposit |
| 81 | + and l.product_id.deposit_amount > 0 |
| 82 | + ) |
| 83 | + if not rental_lines: |
| 84 | + return res |
| 85 | + deposit_lines = self.search([ |
| 86 | + ('order_id', 'in', rental_lines.mapped('order_id').ids), |
| 87 | + ('is_deposit_line', '=', True), |
| 88 | + ]) |
| 89 | + deposit_map = {} |
| 90 | + for line in deposit_lines: |
| 91 | + deposit_map[line.name] = line |
| 92 | + for line in rental_lines: |
| 93 | + deposit_line = deposit_map.get( |
| 94 | + f"This amount is deposit for {line.product_id.name} product" |
| 95 | + ) |
| 96 | + if not deposit_line: |
| 97 | + raise MissingError(_("Deposit product line not found.")) |
| 98 | + deposit_line.with_context(bypass_deposit_protection_for_write=True).write({ |
| 99 | + 'product_uom_qty': line.product_uom_qty, |
| 100 | + 'price_unit': line.product_id.deposit_amount, |
| 101 | + }) |
| 102 | + return res |
0 commit comments