|
| 1 | +from odoo import api, fields, models |
| 2 | + |
| 3 | + |
| 4 | +class SaleOrderLine(models.Model): |
| 5 | + _inherit = "sale.order.line" |
| 6 | + |
| 7 | + is_deposit_line = fields.Boolean(string="Is Deposit Line", default=False) |
| 8 | + |
| 9 | + def _add_deposit_line(self): |
| 10 | + if not self.product_id: |
| 11 | + return |
| 12 | + |
| 13 | + if self.order_id and self.product_id.rent_ok and self.product_id.require_deposit: |
| 14 | + deposit_product_id = int(self.env['ir.config_parameter'].get_param('deposit_product_id')) |
| 15 | + deposit_product = self.env['product.product'].browse(deposit_product_id) |
| 16 | + if deposit_product: |
| 17 | + deposit_line = self.order_id.order_line.filtered(lambda line: line.product_id == deposit_product and line.name.endswith(f"For {self.product_id.name}")) |
| 18 | + if deposit_line: |
| 19 | + deposit_line.product_uom_qty = self.product_uom_qty |
| 20 | + deposit_line.price_unit = self.product_id.deposit_amount |
| 21 | + else: |
| 22 | + self.env["sale.order.line"].create({ |
| 23 | + 'order_id': self.order_id.id, |
| 24 | + 'product_id': deposit_product.id, |
| 25 | + 'name': f"Deposit For {self.product_id.name}", |
| 26 | + 'product_uom_qty': self.product_uom_qty, |
| 27 | + 'price_unit': self.product_id.deposit_amount, |
| 28 | + 'linked_line_id': self.id, |
| 29 | + 'price_subtotal': self.product_uom_qty * self.product_id.deposit_amount, |
| 30 | + 'is_deposit_line': True, |
| 31 | + }) |
| 32 | + |
| 33 | + @api.model_create_multi |
| 34 | + def create(self, vals_list): |
| 35 | + res = super().create(vals_list) |
| 36 | + res._add_deposit_line() |
| 37 | + return res |
| 38 | + |
| 39 | + def write(self, vals): |
| 40 | + res = super().write(vals) |
| 41 | + if 'product_uom_qty' in vals: |
| 42 | + for line in self: |
| 43 | + linked_lines = self.search([('linked_line_id', '=', line.id)]) |
| 44 | + for linked_line in linked_lines: |
| 45 | + linked_line.product_uom_qty = line.product_uom_qty |
| 46 | + linked_line.price_subtotal = line.product_uom_qty * line.product_id.deposit_amount |
| 47 | + |
| 48 | + return res |
0 commit comments