|
| 1 | +from odoo import models, fields, api |
| 2 | + |
| 3 | +class WarrantyWizard(models.TransientModel): |
| 4 | + _name = 'warranty.wizard' |
| 5 | + _description = 'Warranty Wizard' |
| 6 | + |
| 7 | + sale_order_id = fields.Many2one('sale.order', string="Sale Order") |
| 8 | + warranty_line_ids = fields.One2many('warranty.line.wizard', 'wizard_id', string="Warranty Lines") |
| 9 | + |
| 10 | + @api.model |
| 11 | + def default_get(self, fields): |
| 12 | + res = super(WarrantyWizard, self).default_get(fields) |
| 13 | + active_id = self.env.context.get('active_id') |
| 14 | + if active_id: |
| 15 | + sale_order = self.env['sale.order'].browse(active_id) |
| 16 | + if not sale_order: |
| 17 | + return res |
| 18 | + |
| 19 | + warranty_lines = [] |
| 20 | + for line in sale_order.order_line: |
| 21 | + if line.product_template_id and line.product_template_id.is_warranty: |
| 22 | + warranty_lines.append((0, 0, { |
| 23 | + 'sale_order_line_id': line.id, |
| 24 | + 'product_id': line.product_template_id.id, |
| 25 | + })) |
| 26 | + |
| 27 | + res.update({ |
| 28 | + 'sale_order_id': sale_order.id, |
| 29 | + 'warranty_line_ids': warranty_lines, |
| 30 | + }) |
| 31 | + return res |
| 32 | + |
| 33 | + def action_add_warranty(self): |
| 34 | + new_order_line_list = [] |
| 35 | + for record in self: |
| 36 | + sale_order = record.sale_order_id |
| 37 | + for line in record.warranty_line_ids: |
| 38 | + if line.warranty_config_id: |
| 39 | + new_order_line_list.append({ |
| 40 | + "order_id": sale_order.id, |
| 41 | + "name": f"{line.warranty_config_id.name} / {line.end_date}", |
| 42 | + "product_id": line.warranty_config_id.product_id.product_variant_id.id, |
| 43 | + "price_unit": line.sale_order_line_id.price_subtotal |
| 44 | + * (line.warranty_config_id.percentage / 100), |
| 45 | + "warranty_id": line.sale_order_line_id.id, |
| 46 | + "sequence": line.sale_order_line_id.sequence, |
| 47 | + }) |
| 48 | + |
| 49 | + res = self.env['sale.order.line'].create(new_order_line_list) |
0 commit comments