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..e119d078f69 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,15 @@ +{ + 'name': 'Estate', + 'author': 'empir', + 'depends': ['base'], + 'application': True, + 'license': 'LGPL-3', + 'data': [ + 'security/ir.model.access.csv', + 'views/property.xml', + 'views/offer.xml', + 'views/tag.xml', + 'views/type.xml', + 'views/estate_menus.xml', + ], +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..fc7779d569a --- /dev/null +++ b/estate/models/__init__.py @@ -0,0 +1,4 @@ +from . import estate_property +from . import property_type +from . import property_tag +from . import property_offer diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py new file mode 100644 index 00000000000..3a513e7e428 --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,131 @@ +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 + + +class EstateProperty(models.Model): + _name = "estate.property" + _description = "Real estate properties" + _order = "id desc" + + name = fields.Char(required=True, default="Unknown") + active = fields.Boolean(default=True) + state = fields.Selection( + string="Status", + selection=[ + ("new", "New"), + ("offer_received", "Offer Received"), + ("offer_accepted", "Offer Accepted"), + ("sold", "Sold"), + ("cancelled", "Cancelled"), + ], + required=True, + default="new", + copy=False, + ) + description = fields.Text() + postcode = fields.Char() + date_availability = fields.Date( + copy=False, + default=fields.Date.today() + relativedelta(months=3), + ) + expected_price = fields.Float(required=True) + selling_price = fields.Float(readonly=True, copy=False) + bedrooms = fields.Integer(default=2) + living_area = fields.Integer() + facades = fields.Integer() + garage = fields.Boolean() + garden = fields.Boolean() + garden_area = fields.Integer() + garden_orientation = fields.Selection( + string="Garden orientation", + selection=[ + ("north", "North"), + ("south", "South"), + ("east", "East"), + ("west", "West"), + ], + ) + property_type_id = fields.Many2one("estate.property.type", string="Property type") + buyer_id = fields.Many2one("res.partner", string="Buyer", copy=False) + salesperson_id = fields.Many2one( + "res.users", + string="Salesperson", + default=lambda self: self.env.user, + ) + tag_ids = fields.Many2many("estate.property.tag", string="Tags") + offer_ids = fields.One2many( + "estate.property.offer", + inverse_name="property_id", + string="Offers", + ) + total_area = fields.Integer(compute="_compute_total_area") + best_price = fields.Float(compute="_compute_best_price") + + _check_expected_price_positive = models.Constraint( + "CHECK(expected_price > 0)", + "Expected price must always be positive", + ) + + _check_selling_price_positive = models.Constraint( + "CHECK(selling_price >= 0)", + "Selling price must be positive", + ) + + @api.constrains("selling_price", "expected_price") + def _check_selling_price_minimum(self): + for record in self: + comp = float_compare( + record.selling_price, + record.expected_price * 0.9, + precision_digits=2, + ) + if record.selling_price > 0 and comp < 0: + msg = "Selling price must be at least 90% of expected price" + raise ValidationError(msg) + + @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_price(self): + for record in self: + if len(record.offer_ids) > 0: + record.best_price = max(record.offer_ids.mapped("price")) + else: + record.best_price = 0 + + @api.onchange("garden") + def _onchange_garden(self): + if self.garden: + self.garden_area = 10 + self.garden_orientation = "north" + else: + self.garden_area = 0 + self.garden_orientation = None + + def action_cancel(self): + for record in self: + if record.state == "sold": + msg = "Sold properties can not be cancelled" + raise UserError(msg) + record.state = "cancelled" + return True + + def action_sold(self): + for record in self: + if record.state == "cancelled": + msg = "Cancelled properties can not be sold" + raise UserError(msg) + record.state = "sold" + return True + + @api.onchange("offer_ids") + def _onchange_offer_ids(self): + for record in self: + if record.state == "new" and len(record.offer_ids) == 1: + record.state = "offer_received" diff --git a/estate/models/property_offer.py b/estate/models/property_offer.py new file mode 100644 index 00000000000..f9da672f278 --- /dev/null +++ b/estate/models/property_offer.py @@ -0,0 +1,89 @@ +from dateutil.relativedelta import relativedelta + +from odoo import api, fields, models +from odoo.exceptions import UserError + + +class PropertyOffer(models.Model): + _name = "estate.property.offer" + _description = "Real estate property offers" + _order = "price desc" + + price = fields.Float() + status = fields.Selection( + string="Status", + selection=[ + ("accepted", "Accepted"), + ("refused", "Refused"), + ], + copy=False, + ) + partner_id = fields.Many2one("res.partner", required=True) + property_id = fields.Many2one("estate.property", required=True) + validity = fields.Integer(default=7) + date_deadline = fields.Date( + compute="_compute_date_deadline", + inverse="_inverse_date_deadline", + ) + property_type_id = fields.Many2one(related="property_id.property_type_id") + + _check_expected_price_positive = models.Constraint( + "CHECK(price > 0)", + "Offer price must always be positive", + ) + + @api.depends("validity") + def _compute_date_deadline(self): + for record in self: + if record.validity: + crdate = record.create_date or fields.Date.today() + record.date_deadline = crdate + relativedelta( + days=record.validity, + ) + + def _inverse_date_deadline(self): + for record in self: + if record.date_deadline: + record.validity = record._compute_validity() + + # inverse doesn't update the UI when date_deadline changes, + # so we define an additional onchange to not confuse users + @api.onchange("date_deadline") + def _onchange_date_deadline(self): + self.validity = self._compute_validity() + + def _compute_validity(self): + crdate = fields.Date.today() + if self.create_date: + crdate = self.create_date.date() + delta = self.date_deadline - crdate + return delta.days + + def action_accept(self): + # prevent accepting multiple offers + if len(self) > 1: + msg = "Only one offer can be accepted at a time" + raise UserError(msg) + + # no need to process an already accepted offer + if self.status == "accepted": + return True + + # ensure no other offer is already accepted + accepted_offers = [ + offer for offer in self.property_id.offer_ids if offer.status == "accepted" + ] + if len(accepted_offers) > 0: + msg = "Another offer was already accepted" + raise UserError(msg) + + self.status = "accepted" + self.property_id.buyer_id = self.partner_id + self.property_id.selling_price = self.price + self.property_id.state = "offer_accepted" + return True + + def action_refuse(self): + for record in self: + record.status = "refused" + return True diff --git a/estate/models/property_tag.py b/estate/models/property_tag.py new file mode 100644 index 00000000000..981da3a72a6 --- /dev/null +++ b/estate/models/property_tag.py @@ -0,0 +1,15 @@ +from odoo import fields, models + + +class PropertyTag(models.Model): + _name = "estate.property.tag" + _description = "Real estate property tags" + _order = "name" + + name = fields.Char(required=True) + color = fields.Integer() + + _name_uniq = models.Constraint( + "unique(name)", + "A tag with the same name already exists", + ) diff --git a/estate/models/property_type.py b/estate/models/property_type.py new file mode 100644 index 00000000000..1f9eedf726c --- /dev/null +++ b/estate/models/property_type.py @@ -0,0 +1,21 @@ +from odoo import api, fields, models + + +class PropertyType(models.Model): + _name = "estate.property.type" + _description = "Real estate property types" + _order = "sequence, name" + + sequence = fields.Integer(string="Sequence", default=1) + name = fields.Char(required=True, string="Name") + property_ids = fields.One2many("estate.property", inverse_name="property_type_id") + offer_ids = fields.One2many( + "estate.property.offer", + inverse_name="property_type_id", + ) + offer_count = fields.Integer(compute="_compute_offer_counts") + + @api.depends("offer_ids") + def _compute_offer_counts(self): + for record in self: + record.offer_count = len(record.offer_ids) diff --git a/estate/security/ir.model.access.csv b/estate/security/ir.model.access.csv new file mode 100644 index 00000000000..0aa97cf897c --- /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_property_user,estate.estate_property.user,model_estate_property,base.group_user,1,1,1,1 +access_estate_property_type_user,estate.estate_property_type.user,model_estate_property_type,base.group_user,1,1,1,1 +access_estate_property_tag_user,estate.estate_property_tag.user,model_estate_property_tag,base.group_user,1,1,1,1 +access_estate_property_offer_user,estate.estate_property_offer.user,model_estate_property_offer,base.group_user,1,1,1,1 diff --git a/estate/views/estate_menus.xml b/estate/views/estate_menus.xml new file mode 100644 index 00000000000..50089a9365d --- /dev/null +++ b/estate/views/estate_menus.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/estate/views/offer.xml b/estate/views/offer.xml new file mode 100644 index 00000000000..807e0dfad1d --- /dev/null +++ b/estate/views/offer.xml @@ -0,0 +1,42 @@ + + + + Offers + estate.property.offer + list,form + [('property_type_id','=',active_id)] + + + + estate.property.offer.list + estate.property.offer + + + + + + + + + + + + estate.property.offer.form + estate.property.offer + +
+ + + + + + + + + + + +
+
+
+
diff --git a/estate/views/property.xml b/estate/views/property.xml new file mode 100644 index 00000000000..745968c6265 --- /dev/null +++ b/estate/views/property.xml @@ -0,0 +1,139 @@ + + + + Properties + estate.property + list,form + {'search_default_available':True} + + + + estate.property.list + estate.property + + + + + + + + + + + + + + + + + estate.property.form + estate.property + +
+
+
+ +
+

+ +

+ Tags: + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ +

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