diff --git a/estate/__manifest__.py b/estate/__manifest__.py index 7635a8a533..159f6902bd 100644 --- a/estate/__manifest__.py +++ b/estate/__manifest__.py @@ -12,9 +12,10 @@ "data": [ "security/ir.model.access.csv", "views/estate_property_views.xml", + "views/res_user_model_views.xml", + "views/estate_property_offer_views.xml", "views/estate_property_type_views.xml", "views/estate_property_tag_views.xml", - "views/estate_property_offer_views.xml", "views/estate_menus.xml", ], } diff --git a/estate/models/__init__.py b/estate/models/__init__.py index 2f1821a39c..4419cea280 100644 --- a/estate/models/__init__.py +++ b/estate/models/__init__.py @@ -2,3 +2,4 @@ from . import estate_property_type from . import estate_property_tag from . import estate_property_offer +from . import res_user \ No newline at end of file diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py index 8899c865c5..353bf077cd 100644 --- a/estate/models/estate_property.py +++ b/estate/models/estate_property.py @@ -123,3 +123,9 @@ def action_sold_property(self): else: self.write({"state": "sold"}) return True + + @api.ondelete(at_uninstall=False) + def _unlink_except_certain_state(self): + for record in self: + if record.state not in ['new', 'canceled']: + raise UserError(_('Only new or canceled property can be deleted.')) diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py index 74c5f5ba38..3fa295fd4e 100644 --- a/estate/models/estate_property_offer.py +++ b/estate/models/estate_property_offer.py @@ -1,6 +1,6 @@ -from odoo import api, fields, models +from odoo import api, fields, models, _ from datetime import timedelta, date - +from odoo.exceptions import UserError class EstatePropertyOffer(models.Model): _name = "estate.property.offer" @@ -18,7 +18,9 @@ class EstatePropertyOffer(models.Model): compute="_compute_date_deadline", inverse="_inverse_date_deadline" ) - property_type_id = fields.Many2one(related="property_id.property_type_id", store=True) + property_type_id = fields.Many2one( + related="property_id.property_type_id", store=True + ) _sql_constraints = [ ("positive_offer_price", "CHECK(price > 0)", "Offer Price Must be Positive") ] @@ -52,3 +54,11 @@ def action_accept_offer(self): def action_refuse_offer(self): self.write({"status": "refused"}) return True + + @api.model + def create(self, vals): + property_id = self.env["estate.property"].browse(vals["property_id"]) + property_id.state = "offer received" + if any(offer.price > vals.get("price", 0) for offer in property_id.offer_ids): + raise UserError(_("You cannot create an offer with a lower price than an existing offer.")) + return super().create(vals) \ No newline at end of file diff --git a/estate/models/res_user.py b/estate/models/res_user.py new file mode 100644 index 0000000000..0fe4671e6d --- /dev/null +++ b/estate/models/res_user.py @@ -0,0 +1,11 @@ +from odoo import models, fields + + +class ResUser(models.Model): + _inherit = "res.users" + + property_ids = fields.One2many( + "estate.property", + "salesman_id", + domain="['|', ('state', '=', 'new'), ('state', '=', 'offer received')]" + ) diff --git a/estate/views/res_user_model_views.xml b/estate/views/res_user_model_views.xml new file mode 100644 index 0000000000..34bc410c9b --- /dev/null +++ b/estate/views/res_user_model_views.xml @@ -0,0 +1,27 @@ + + + + res.users.form.inherit.properties + res.users + + + + + + + + + + + + + + + + + + + + + +