diff --git a/estate/__init__.py b/estate/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/estate/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/estate/__manifest__.py b/estate/__manifest__.py new file mode 100644 index 00000000000..a979afd6fc3 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,21 @@ +{ + "name": "Real Estate", + "version": "1.0.0", + "depends": ["base"], + "author": "Odoo S.A.", + "license": "LGPL-3", + "category": "Real Estate", + "description": """ + This is a real estate management module that allows users to manage properties, agents, and clients. + """, + "application": True, + "data": [ + "security/ir.model.access.csv", + "views/estate_property_offer_views.xml", + "views/estate_property_tag_views.xml", + "views/estate_property_type_views.xml", + "views/estate_property_views.xml", + "views/estate_property_menus.xml", + "views/res_users_views.xml", + ], +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..1274e6d5223 --- /dev/null +++ b/estate/models/__init__.py @@ -0,0 +1,5 @@ +from . import estate +from . import estate_offer +from . import estate_tag +from . import estate_type +from . import res_users diff --git a/estate/models/estate.py b/estate/models/estate.py new file mode 100644 index 00000000000..e5fc321cf23 --- /dev/null +++ b/estate/models/estate.py @@ -0,0 +1,130 @@ +from dateutil.relativedelta import relativedelta + +from odoo import api, fields, models +from odoo.exceptions import UserError, ValidationError +from odoo.tools.float_utils import float_compare, float_is_zero + + +class Estate(models.Model): + _name = "estate.estate" + _description = "Real Estate" + _order = "id desc" + + name = fields.Char(string="Name", required=True) + description = fields.Text(string="Description") + postcode = fields.Char(string="Postcode") + date_availability = fields.Date( + string="Available From", + default=fields.Date.today() + relativedelta(months=3), + copy=False, + ) + expected_price = fields.Float(string="Expected Price", required=True) + selling_price = fields.Float(string="Selling price", readonly=True, copy=False) + bedrooms = fields.Integer(string="Bedrooms", default=2) + living_area = fields.Integer(string="Living Area") + facades = fields.Integer(string="Facades") + garage = fields.Boolean(string="Garage") + garden = fields.Boolean(string="Garden") + garden_area = fields.Integer(string="Garden Area") + garden_orientation = fields.Selection( + string="Garden Orientation", + selection=[ + ("north", "North"), + ("east", "East"), + ("west", "West"), + ("south", "South"), + ], + ) + active = fields.Boolean(string="Active", default=True) + state = fields.Selection( + string="Status", + selection=[ + ("new", "New"), + ("offer_received", "Offer Received"), + ("offer_accepted", "Offer Accepted"), + ("sold", "Sold"), + ("cancelled", "Cancelled"), + ], + required=True, + copy=False, + default="new", + ) + type_id = fields.Many2one( + string="Property Type", + comodel_name="estate.type", + ) + salesman_id = fields.Many2one( + string="Salesman", + comodel_name="res.users", + default=lambda self: self.env.user, + ) + buyer_id = fields.Many2one(string="Buyer", comodel_name="res.partner", copy=False) + tag_ids = fields.Many2many(string="Tags", comodel_name="estate.tag") + offer_ids = fields.One2many( + string="Offers", + comodel_name="estate.offer", + inverse_name="property_id", + ) + total_area = fields.Float(string="Total Area", compute="_compute_total_area") + best_offer = fields.Float(string="Best Offer", compute="_compute_best_offer") + + _positive_expected_price = models.Constraint( + "CHECK(expected_price > 0)", + "Expected price should be strictly positive", + ) + _positive_selling_price = models.Constraint( + "CHECK(selling_price >= 0)", + "Selling price should be positive", + ) + + @api.depends("living_area", "garden_area") + def _compute_total_area(self): + for record in self: + record.total_area = record.living_area + record.garden_area + + @api.depends("offer_ids.price") + def _compute_best_offer(self): + for record in self: + record.best_offer = ( + max(record.offer_ids.mapped("price")) if len(record.offer_ids) else 0.0 + ) + + @api.onchange("garden") + def _on_change_garden(self): + self.garden_area = 10 if self.garden else 0 + self.garden_orientation = "north" if self.garden else None + + def action_sell_property(self): + for property in self: + if property.state == "cancelled": + raise UserError(self.env._("Cancelled properties cannot be sold")) + property.state = "sold" + + def action_cancel_property(self): + for property in self: + if property.state == "sold": + raise UserError(self.env._("Sold properties cannot be cancelled")) + property.state = "cancelled" + + @api.constrains("expected_price", "selling_price") + def _check_selling_price_limit(self): + for property in self: + if not float_is_zero(property.selling_price, precision_digits=5): + limit = 0.9 * property.expected_price + if ( + float_compare(property.selling_price, limit, precision_digits=5) + == -1 + ): + raise ValidationError( + self.env._( + "The selling price should not be less than 90% of the expected price of the property", + ), + ) + + @api.ondelete(at_uninstall=False) + def _unlink_if_new_or_cancelled(self): + for state in self.mapped("state"): + if state not in ["new", "cancelled"]: + raise UserError( + self.env._("New or cancelled estates only can be deleted"), + ) diff --git a/estate/models/estate_offer.py b/estate/models/estate_offer.py new file mode 100644 index 00000000000..0a9eee99cf6 --- /dev/null +++ b/estate/models/estate_offer.py @@ -0,0 +1,88 @@ +from dateutil.relativedelta import relativedelta + +from odoo import api, fields, models +from odoo.exceptions import UserError + + +class EstateOFfer(models.Model): + _name = "estate.offer" + _description = "Offer made to some estate (property)" + _order = "price desc" + + price = fields.Float(string="Price") + status = fields.Selection( + string="Status", + selection=[("accepted", "Accepted"), ("refused", "Refused")], + copy=False, + ) + property_id = fields.Many2one( + string="Property", + comodel_name="estate.estate", + required=True, + ) + partner_id = fields.Many2one( + string="Partner", + comodel_name="res.partner", + required=True, + ) + validity = fields.Integer(string="Validity", default=7) + date_deadline = fields.Date( + string="Deadline", + compute="_compute_deadline", + inverse="_inverse_deadline", + ) + property_type_id = fields.Many2one( + string="Property Type", + comodel_name="estate.type", + related="property_id.type_id", + store=True, + ) + + _positive_offer_price = models.Constraint( + "CHECK(price > 0)", + "Offer price should be strictly positive", + ) + + @api.depends("create_date", "validity") + def _compute_deadline(self): + for offer in self: + curr_date = offer.create_date if offer.create_date else fields.Date.today() + offer.date_deadline = curr_date + relativedelta(days=offer.validity) + + def _inverse_deadline(self): + for offer in self: + date_diff = offer.date_deadline - (offer.create_date.date()) + offer.validity = date_diff.days + + def action_accept_offer(self): + # check if there is no other accepted offers + if "accepted" in self.property_id.offer_ids.mapped("status"): + raise UserError( + self.env._("Only one offer can be accepted for a given property!"), + ) + + # update status, selling price, buyer + self.status = "accepted" + self.property_id.selling_price = self.price + self.property_id.buyer_id = self.partner_id + self.property_id.state = "offer_accepted" + + def action_refuse_offer(self): + self.status = "refused" + + @api.model_create_multi + def create(self, vals_list): + # check before saving if the offer price >= existing offer prices + for record in vals_list: + property = self.env["estate.estate"].browse(record["property_id"]) + max_offer_price = max(property.offer_ids.mapped("price"), default=0) + if record["price"] < max_offer_price: + raise UserError( + self.env._( + "Cannot create an offer with price lower than an existing offer", + ), + ) + + # update state and save to the database + property.state = "offer_received" + return super().create(vals_list) diff --git a/estate/models/estate_tag.py b/estate/models/estate_tag.py new file mode 100644 index 00000000000..65715107a09 --- /dev/null +++ b/estate/models/estate_tag.py @@ -0,0 +1,15 @@ +from odoo import fields, models + + +class EstateTag(models.Model): + _name = "estate.tag" + _description = "A list of tags that categorize the properities" + _order = "name" + + name = fields.Char(string="Name", required=True) + color = fields.Integer(string="Color") + + _unique_name = models.Constraint( + "UNIQUE(name)", + "Property tag names must be unique!", + ) diff --git a/estate/models/estate_type.py b/estate/models/estate_type.py new file mode 100644 index 00000000000..d03ce3b0129 --- /dev/null +++ b/estate/models/estate_type.py @@ -0,0 +1,32 @@ +from odoo import api, fields, models + + +class EstateType(models.Model): + _name = "estate.type" + _description = "The type of the property to be sold such as House, apartment, ..." + _order = "sequence, name" + + name = fields.Char(string="Name", required=True) + property_ids = fields.One2many( + string="Properties", + comodel_name="estate.estate", + inverse_name="type_id", + ) + sequence = fields.Integer(string="Sequence", default=1) + + offer_ids = fields.One2many( + string="Property type offers", + comodel_name="estate.offer", + inverse_name="property_type_id", + ) + offer_count = fields.Integer(string="Offers count", compute="_compute_offers_count") + + _unique_name = models.Constraint( + "UNIQUE(name)", + "Property type names must be unique!", + ) + + @api.depends("offer_ids") + def _compute_offers_count(self): + for record in self: + record.offer_count = len(record.offer_ids) diff --git a/estate/models/res_users.py b/estate/models/res_users.py new file mode 100644 index 00000000000..5011d9c1e40 --- /dev/null +++ b/estate/models/res_users.py @@ -0,0 +1,12 @@ +from odoo import fields, models + + +class Users(models.Model): + _inherit = "res.users" + + property_ids = fields.One2many( + string="Properities", + comodel_name="estate.estate", + inverse_name="salesman_id", + domain=[("state", "in", ["new", "offer_received"])], + ) diff --git a/estate/security/ir.model.access.csv b/estate/security/ir.model.access.csv new file mode 100644 index 00000000000..84833910702 --- /dev/null +++ b/estate/security/ir.model.access.csv @@ -0,0 +1,5 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_estate_estate,access.estate.estate,model_estate_estate,base.group_user,1,1,1,1 +access_estate_offer,access.estate.offer,model_estate_offer,base.group_user,1,1,1,1 +access_estate_type,access.estate.type,model_estate_type,base.group_user,1,1,1,1 +access_estate_tag,access.estate.tag,model_estate_tag,base.group_user,1,1,1,1 diff --git a/estate/views/estate_property_menus.xml b/estate/views/estate_property_menus.xml new file mode 100644 index 00000000000..ec7c08d31b1 --- /dev/null +++ b/estate/views/estate_property_menus.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/estate/views/estate_property_offer_views.xml b/estate/views/estate_property_offer_views.xml new file mode 100644 index 00000000000..20b04d5d969 --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,38 @@ + + + + estate.property.offer.view.form + estate.offer + form + +
+ + + + + + + + + + + +
+
+
+ + + estate.property.offer.view.list + estate.offer + list + + + + + + + + + + +
diff --git a/estate/views/estate_property_tag_views.xml b/estate/views/estate_property_tag_views.xml new file mode 100644 index 00000000000..742da5f270e --- /dev/null +++ b/estate/views/estate_property_tag_views.xml @@ -0,0 +1,18 @@ + + + + Property Tags + estate.tag + list,form + + + + estate.property.tag.view.list + estate.tag + + + + + + + diff --git a/estate/views/estate_property_type_views.xml b/estate/views/estate_property_type_views.xml new file mode 100644 index 00000000000..5cb82c81562 --- /dev/null +++ b/estate/views/estate_property_type_views.xml @@ -0,0 +1,66 @@ + + + + Property Types + estate.type + list,form + + + + Offers + estate.offer + list,form + [('property_type_id', '=', active_id)] + + + + estate.property.type.view.list + estate.type + + + + + + + + + + estate.property.type.view.form + estate.type + form + +
+ +
+ +
+ +

+ +

+
+ + + + + + + + + + + + + + + +
+
+
+
+
diff --git a/estate/views/estate_property_views.xml b/estate/views/estate_property_views.xml new file mode 100644 index 00000000000..481bc696910 --- /dev/null +++ b/estate/views/estate_property_views.xml @@ -0,0 +1,150 @@ + + + + Properties + estate.estate + list,form,kanban + {'search_default_available': True} + + + + estate.property.view.kanban + estate.estate + + + + + +
+ +
+
+ Expected Price: +
+
+ Best Offer: +
+
+ Selling Price: +
+
+ +
+
+
+
+
+
+ + + estate.property.view.list + estate.estate + + + + + + + + + + + + + + + + estate.property.view.form + estate.estate + form + +
+
+
+ + +

+ +

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +