|
| 1 | +from odoo import api, fields, models, _ |
| 2 | +from odoo.exceptions import ValidationError |
| 3 | + |
| 4 | + |
| 5 | +class EstatePropertyMaintenance(models.Model): |
| 6 | + _name = "estate.property.maintenance" |
| 7 | + _description = "Property maintenance" |
| 8 | + |
| 9 | + property_id = fields.Many2one( |
| 10 | + 'estate.property', |
| 11 | + string="Property", |
| 12 | + required=True |
| 13 | + ) |
| 14 | + |
| 15 | + issue_type = fields.Selection( |
| 16 | + [ |
| 17 | + ('waterleakage', 'Water leakage'), |
| 18 | + ('electrical', 'Electrical'), |
| 19 | + ('plumber', 'Plumbing'), |
| 20 | + ('structure', 'Structural') |
| 21 | + ] |
| 22 | + ) |
| 23 | + description = fields.Text() |
| 24 | + priority = fields.Selection( |
| 25 | + [ |
| 26 | + ('low', 'Low'), |
| 27 | + ('medium', 'Medium'), |
| 28 | + ('high', 'High') |
| 29 | + ], |
| 30 | + string="Priority", |
| 31 | + compute="_compute_priority", |
| 32 | + store=True |
| 33 | + ) |
| 34 | + state = fields.Selection( |
| 35 | + [ |
| 36 | + ('new', 'New'), |
| 37 | + ('assign', 'Assign'), |
| 38 | + ('wip', "WIP"), |
| 39 | + ('complete', 'Complete'), |
| 40 | + ], |
| 41 | + default='new' |
| 42 | + ) |
| 43 | + date = fields.Date(default=fields.Date.today()) |
| 44 | + estimated_cost = fields.Integer(compute="_compute_estimated_cost") |
| 45 | + actual_cost = fields.Integer() |
| 46 | + technician = fields.Many2one( |
| 47 | + 'res.users', |
| 48 | + string="Assign To", |
| 49 | + ) |
| 50 | + total_actual_cost = fields.Integer( |
| 51 | + compute="_compute_total_actual_cost", |
| 52 | + ) |
| 53 | + |
| 54 | + @api.depends("issue_type") |
| 55 | + def _compute_estimated_cost(self): |
| 56 | + for record in self: |
| 57 | + if record.issue_type == 'waterleakage': |
| 58 | + record.estimated_cost = 10000 |
| 59 | + elif record.issue_type == 'electrical': |
| 60 | + record.estimated_cost = 20000 |
| 61 | + elif record.issue_type == 'structure': |
| 62 | + record.estimated_cost = 50000 |
| 63 | + elif record.issue_type == 'plumber': |
| 64 | + record.estimated_cost = 30000 |
| 65 | + else: |
| 66 | + record.estimated_cost = 0 |
| 67 | + |
| 68 | + @api.depends("issue_type") |
| 69 | + def _compute_priority(self): |
| 70 | + for record in self: |
| 71 | + if record.issue_type == 'structure': |
| 72 | + record.priority = 'high' |
| 73 | + elif record.issue_type == 'electrical': |
| 74 | + record.priority = 'medium' |
| 75 | + else: |
| 76 | + record.priority = 'low' |
| 77 | + |
| 78 | + @api.constrains('state', 'actual_cost') |
| 79 | + def _check_actual_cost(self): |
| 80 | + for record in self: |
| 81 | + if record.state == 'complete' and (not record.actual_cost or record.actual_cost <= 0): |
| 82 | + raise ValidationError(_( |
| 83 | + "Actual Cost must be fill before marking the maintenance as Done." |
| 84 | + )) |
| 85 | + |
| 86 | + @api.depends('property_id', 'actual_cost') |
| 87 | + def _compute_total_actual_cost(self): |
| 88 | + for record in self: |
| 89 | + if record.property_id: |
| 90 | + maintenance = self.search([ |
| 91 | + ('property_id', '=', record.property_id.id) |
| 92 | + ]) |
| 93 | + record.total_actual_cost = sum( |
| 94 | + maintenance.mapped('actual_cost')) |
| 95 | + else: |
| 96 | + record.total_actual_cost = 0 |
| 97 | + |
| 98 | + def action_create_issue(self): |
| 99 | + for records in self: |
| 100 | + records.state = 'assign' |
| 101 | + records.technician = self.env.user |
| 102 | + return True |
| 103 | + |
| 104 | + def action_inprogress_issue(self): |
| 105 | + for records in self: |
| 106 | + records.state = 'wip' |
| 107 | + return True |
| 108 | + |
| 109 | + def action_done_issue(self): |
| 110 | + for records in self: |
| 111 | + records.state = 'complete' |
| 112 | + return True |
0 commit comments