|
| 1 | +from odoo import fields, models, exceptions, api |
| 2 | +from markupsafe import Markup |
| 3 | + |
| 4 | +class BudgetCreate(models.Model): |
| 5 | + _name = 'budget.budget' |
| 6 | + _inherit = ['mail.thread', 'mail.activity.mixin'] |
| 7 | + _description = "Budget" |
| 8 | + |
| 9 | + name = fields.Char(required=True) |
| 10 | + user_id = fields.Many2one("res.users") |
| 11 | + period_start_date = fields.Date(required=True) |
| 12 | + period_end_date = fields.Date(index=True) |
| 13 | + con_color = fields.Integer() |
| 14 | + over_budget = fields.Selection( |
| 15 | + [ |
| 16 | + ('warning', 'Warning'), |
| 17 | + ('restriction', 'Restriction') |
| 18 | + ], |
| 19 | + default="warning" |
| 20 | + ) |
| 21 | + budget_line_ids = fields.One2many('budget.line', 'budget_id') |
| 22 | + state = fields.Selection( |
| 23 | + [ |
| 24 | + ('draft', 'Draft'), |
| 25 | + ('conformed', 'Conformed'), |
| 26 | + ('revised', 'Revised'), |
| 27 | + ('done', 'Done') |
| 28 | + ], |
| 29 | + default='draft' |
| 30 | + ) |
| 31 | + company_id = fields.Many2one('res.company') |
| 32 | + active = fields.Boolean(default=True) |
| 33 | + message_follower_ids = fields.Many2many( |
| 34 | + 'res.partner', 'res_partner_followers_rel', 'res_id', 'partner_id', |
| 35 | + string='Followers', help="Partners following this record" |
| 36 | + ) |
| 37 | + message_ids = fields.One2many( |
| 38 | + 'mail.message', 'res_id', |
| 39 | + domain=[('model', '=', 'budget.budget'), ('model', '=', 'budget.line')], |
| 40 | + string="Messages" |
| 41 | + ) |
| 42 | + is_warning = fields.Boolean(default=False, compute="_compute_warning_message") |
| 43 | + |
| 44 | + |
| 45 | + @api.depends('budget_line_ids.archived_amount', 'budget_line_ids.budget_amount') |
| 46 | + def _compute_warning_message(self): # Compute Method : Manage the warning message |
| 47 | + for record in self: |
| 48 | + is_warning = False |
| 49 | + for line in record.budget_line_ids: |
| 50 | + if line.archived_amount and line.budget_amount and line.archived_amount > line.budget_amount: |
| 51 | + is_warning = True |
| 52 | + break |
| 53 | + record.is_warning = is_warning |
| 54 | + |
| 55 | + |
| 56 | + def action_budget_line_form(self): # Action Button : for open Budget Lines |
| 57 | + action = ( |
| 58 | + self.env["ir.actions.act_window"] |
| 59 | + .with_context({"active_id": self.id}) |
| 60 | + ._for_xml_id("budget_management.act_budget_lines_view") |
| 61 | + ) |
| 62 | + action["display_name"] = self.name |
| 63 | + return action |
| 64 | + |
| 65 | + |
| 66 | + def action_budget_form_view(self): # Action Button : for open budget Form |
| 67 | + return { |
| 68 | + "type": "ir.actions.act_window", |
| 69 | + "res_model": "budget.budget", |
| 70 | + "view_mode": "form", |
| 71 | + "res_id": self.id |
| 72 | + } |
| 73 | + |
| 74 | + def action_to_draft(self): # Button Method : for draft |
| 75 | + self.state = 'draft' |
| 76 | + |
| 77 | + def action_to_conform(self): # Button Method : for conformed |
| 78 | + self.state = 'conformed' |
| 79 | + |
| 80 | + |
| 81 | + def action_to_revised(self): # Button Method : for revised |
| 82 | + if self.state == 'conformed': |
| 83 | + self.state = 'revised' |
| 84 | + |
| 85 | + orignal_record = self.browse(self.id) |
| 86 | + |
| 87 | + new_record = orignal_record.copy(default={ |
| 88 | + 'state': 'draft', |
| 89 | + 'period_start_date': self.period_start_date, |
| 90 | + 'period_end_date': self.period_end_date, |
| 91 | + 'name': f"Revised: {self.name}" |
| 92 | + }) |
| 93 | + |
| 94 | + for line in self.budget_line_ids: |
| 95 | + line.copy({ |
| 96 | + 'budget_id': new_record.id, |
| 97 | + }) |
| 98 | + |
| 99 | + new_record.message_follower_ids = self.message_follower_ids |
| 100 | + message_body = f"Revised to: <a href='#id={new_record.id}&model=budget.budget'>{new_record.name}</a>" |
| 101 | + self.message_post(body=Markup(message_body)) |
| 102 | + |
| 103 | + message_body = f"Revised from: <a href='#id={self.id}&model=budget.budget' target='__blank'>{self.name}</a>" |
| 104 | + new_record.message_post(body=Markup(message_body)) |
| 105 | + |
| 106 | + self.active = False |
| 107 | + |
| 108 | + else: |
| 109 | + raise exceptions.ValidationError("The budget is not conformed yet...") |
| 110 | + |
| 111 | + def action_to_done(self): # Button Method : for done |
| 112 | + self.state = 'done' |
0 commit comments