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..645417bac8e --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,18 @@ +{ + "name": "estate", + "depends": [ + "base", + ], + "data": [ + "security/ir.model.access.csv", + "views/estate_property_offer_views.xml", + "views/estate_property_type_views.xml", + "views/estate_property_tag_views.xml", + "views/estate_property_views.xml", + "views/estate_menus.xml", + ], + "application": True, + "installable": True, + "author": "Gendi (HOELG)", + "license": "LGPL-3", +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..3683ff97b61 --- /dev/null +++ b/estate/models/__init__.py @@ -0,0 +1,6 @@ +from . import ( + estate_property, + estate_property_offer, + estate_property_tag, + estate_property_type, +) diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py new file mode 100644 index 00000000000..31213e82bf5 --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,132 @@ +import logging + +from dateutil.relativedelta import relativedelta + +from odoo import api, fields, models +from odoo.exceptions import UserError, ValidationError +from odoo.tools import float_compare, float_is_zero + +logger = logging.getLogger(__name__) + + +class EstateProperty(models.Model): + _name = "estate.property" + _description = "Real state property" + _order = "id DESC" + + name = fields.Char(required=True, string="Title") + description = fields.Text() + property_type_id = fields.Many2one( + comodel_name="estate.property.type", + string="Type", + ) + postcode = fields.Char() + date_availability = fields.Date( + copy=False, + default=fields.Date.today() + relativedelta(months=3), + string="Available From", + ) + expected_price = fields.Float(required=True) + selling_price = fields.Float(readonly=True, copy=False) + bedrooms = fields.Integer(default=2) + living_area = fields.Integer(string="Living Area (sqm)") + facades = fields.Integer() + garage = fields.Boolean() + garden = fields.Boolean() + garden_area = fields.Integer(string="Garden Area (sqm)") + garden_orientation = fields.Selection( + selection=[ + ("north", "North"), + ("south", "South"), + ("east", "East"), + ("west", "West"), + ], + ) + total_area = fields.Integer( + compute="_compute_total_area", string="Total Area (sqm)", + ) + state = fields.Selection( + selection=[ + ("new", "New"), + ("offer_received", "Offer Received"), + ("offer_accepted", "Offer Accepted"), + ("sold", "Sold"), + ("cancelled", "Cancelled"), + ], + default="new", + ) + active = fields.Boolean(default=True) + seller = fields.Many2one( + comodel_name="res.users", + string="Salesman", + default=(lambda self: self.env.user), + ) + buyer = fields.Many2one( + comodel_name="res.partner", + copy=False, + ) + tag_ids = fields.Many2many( + comodel_name="estate.property.tag", + string="Tags", + ) + offer_ids = fields.One2many( + comodel_name="estate.property.offer", + inverse_name="property_id", + string="Offers", + ) + best_offer = fields.Float(compute="_compute_best_offer") + + _check_positive_expected_price = models.Constraint( + "CHECK(expected_price > 0)", + "Expected Price must be positive!", + ) + _check_positive_selling_price = models.Constraint( + "CHECK(selling_price > 0)", + "Selling Price must be positive!", + ) + _name_uniq = models.Constraint( + "UNIQUE(name)", + "A property with this name already exists!", + ) + + @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") + def _compute_best_offer(self): + for record in self: + record.best_offer = ( + max(record.offer_ids.mapped("price")) if record.offer_ids else 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': + raise UserError("Sold properties cannot be cancelled.") + record.state = "cancelled" + + def action_sold(self): + for record in self: + if record.state == "cancelled": + raise UserError("Cancelled properties cannot be sold") + record.state = "sold" + + @api.constrains("selling_price", "expected_price") + def _check_reasonable_selling_price(self): + for record in self: + if ( + not float_is_zero(record.selling_price, precision_digits=2) + and float_compare(record.selling_price, 0.9 * record.expected_price, precision_digits=2) == -1 + ): + raise ValidationError("Selling price cannot be lower than 90% of the expected price") diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py new file mode 100644 index 00000000000..c6197ae8e63 --- /dev/null +++ b/estate/models/estate_property_offer.py @@ -0,0 +1,70 @@ +from datetime import timedelta + +from odoo import api, fields, models +from odoo.exceptions import UserError + + +class EstatePropertyOffer(models.Model): + _name = "estate.property.offer" + _description = "an amount a potential buyer offers to the seller" + _order = "price DESC" + + price = fields.Float() + status = fields.Selection( + selection=[ + ("accepted", "Accepted"), + ("refused", "Refused"), + ], + copy=False, + ) + partner_id = fields.Many2one( + comodel_name="res.partner", + required=True, + ) + property_id = fields.Many2one( + comodel_name="estate.property", + required=True, + ) + property_type_id = fields.Many2one( + related="property_id.property_type_id", + store=True, + ) + validity = fields.Integer(default=7) + date_deadline = fields.Date( + compute="_compute_date_deadline", + inverse="_inverse_date_deadline", + string="Deadline", + ) + + _check_positive_price = models.Constraint( + "CHECK(price > 0)", + "Offer price must be positive!", + ) + + @api.depends("validity", "create_date") + def _compute_date_deadline(self): + for record in self: + record.date_deadline = ( + record.create_date or fields.Date.today() + ) + timedelta(days=record.validity) + + def _inverse_date_deadline(self): + for record in self: + record.validity = ( + record.date_deadline - fields.Date.to_date(record.create_date) + ).days + + def action_refuse(self): + self.ensure_one() + self.status = "refused" + + def action_accept(self): + self.ensure_one() + # TODO: property state should be readonly and just check on state == 'offer_accepted' + if any(offer.status == 'accepted' for offer in self.property_id.offer_ids): + raise UserError("Property already has another accepted offer.") + # TODO: consider checking other property states + self.status = "accepted" + self.property_id.buyer = self.partner_id + self.property_id.selling_price = self.price + self.property_id.state = 'offer_accepted' diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py new file mode 100644 index 00000000000..91937ecfb70 --- /dev/null +++ b/estate/models/estate_property_tag.py @@ -0,0 +1,15 @@ +from odoo import fields, models + + +class PropertyTag(models.Model): + _name = "estate.property.tag" + _description = "Tag to be applied to a property" + _order = "name" + + name = fields.Char(required=True) + color = fields.Integer() + + _name_uniq = models.Constraint( + "UNIQUE(name)", + "A property tag with this name already exists!", + ) diff --git a/estate/models/estate_property_type.py b/estate/models/estate_property_type.py new file mode 100644 index 00000000000..9b990dd25d5 --- /dev/null +++ b/estate/models/estate_property_type.py @@ -0,0 +1,26 @@ +from odoo import api, fields, models + + +class EstatePropertyType(models.Model): + _name = "estate.property.type" + _description = "Type of a real estate property" + _order = "sequence, name" + + name = fields.Char(required=True) + sequence = fields.Integer("Sequence", default=1, help="Used to order property types. Lower is better.") + property_ids = fields.One2many( + comodel_name="estate.property", + inverse_name="property_type_id", + string="Properties", + ) + offer_ids = fields.One2many( + comodel_name="estate.property.offer", + inverse_name="property_type_id", + string="Offers", + ) + offer_count = fields.Integer(compute="_compute_offer_count", string="Offer count") + + @api.depends("offer_ids") + def _compute_offer_count(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..89f97c50842 --- /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,access_estate_property,model_estate_property,base.group_user,1,1,1,1 +access_estate_property_type,access_estate_property_type,model_estate_property_type,base.group_user,1,1,1,1 +access_estate_property_tag,access_estate_property_tag,model_estate_property_tag,base.group_user,1,1,1,1 +access_estate_property_offer,access_estate_property_offer,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..751e9ad96e0 --- /dev/null +++ b/estate/views/estate_menus.xml @@ -0,0 +1,12 @@ + + + + diff --git a/estate/views/estate_property_offer_views.xml b/estate/views/estate_property_offer_views.xml new file mode 100644 index 00000000000..df1a89c7d43 --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,44 @@ + + + + estate.property.offer.form + estate.property.offer + +
+ + + + + + + + + +
+
+
+ + + estate.property.offer.list + estate.property.offer + + + + + + + + +
+

+ +

+
+ + + + + + + + + + + + + +
+
+ + + estate.property.type.list + estate.property.type + + + + + + + + + + Property Types + estate.property.type + list,form + +
diff --git a/estate/views/estate_property_views.xml b/estate/views/estate_property_views.xml new file mode 100644 index 00000000000..2ae01a0036c --- /dev/null +++ b/estate/views/estate_property_views.xml @@ -0,0 +1,109 @@ + + + + estate.property.view.search + estate.property + + + + + + + + + + + + + + + + + + + estate.property.form + estate.property + +
+
+
+ + +

+ +

+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+ + + estate.property.list + estate.property + + + + + + + + + + + + + + + + + + Properties + estate.property + list,form + + {'search_default_available': True} + +
diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 00000000000..543fa0290c0 --- /dev/null +++ b/ruff.toml @@ -0,0 +1,83 @@ +# automatically generated file by the runbot nightly ruff checks, do not modify +# for ruff version 0.11.4 (or higher) +# note: 'E241', 'E272', 'E201', 'E221' are ignored on runbot in test files when formating a table like structure (more than two space) +# some rules present here are not enabled on runbot (yet) but are still advised to follow when possible : ["B904", "COM812", "E741", "EM101", "I001", "RET", "RUF021", "TRY002", "UP006", "UP007"] + + +target-version = "py310" + +[lint] +preview = true +select = [ + "BLE", # flake8-blind-except + "C", # flake8-comprehensions + "COM", # flake8-commas + "E", # pycodestyle Error + "EM", # flake8-errmsg + "EXE", # flake8-executable + "F", # Pyflakes + "FA", # flake8-future-annotations + "FLY", # flynt + "G", # flake8-logging-format + "I", # isort + "ICN", # flake8-import-conventions + "INT", # flake8-gettext + "ISC", # flake8-implicit-str-concat + "LOG", # flake8-logging + "PGH", # pygrep-hooks + "PIE", # flake8-pie + "PLC", # Pylint Convention + "PLE", # Pylint Error + "PLW", # Pylint Warning + "PYI", # flake8-pyi + "RET", # flake8-return + "RUF", # Ruff-specific rules + "SIM", # flake8-simplify + "SLOT", # flake8-slots + "T", # flake8-print + "TC", # flake8-type-checking + "TID", # flake8-tidy-imports + "TRY", # tryceratops + "UP", # pyupgrade + "W", # pycodestyle Warning + "YTT", # flake8-2020 +] +ignore = [ + "C408", # unnecessary-collection-call + "C420", # unnecessary-dict-comprehension-for-iterable + "C901", # complex-structure + "E266", # multiple-leading-hashes-for-block-comment + "E501", # line-too-long + "E713", # not-in-test + "EM102", # f-string-in-exception + "FA100", # future-rewritable-type-annotation + "PGH003", # blanket-type-ignore + "PIE790", # unnecessary-placeholder + "PIE808", # unnecessary-range-start + "PLC2701", # import-private-name + "PLW2901", # redefined-loop-name + "RUF001", # ambiguous-unicode-character-string + "RUF005", # collection-literal-concatenation + "RUF012", # mutable-class-default + "RUF100", # unused-noqa + "SIM102", # collapsible-if + "SIM108", # if-else-block-instead-of-if-exp + "SIM117", # multiple-with-statements + "TID252", # relative-imports + "TRY003", # raise-vanilla-args + "TRY300", # try-consider-else + "TRY400", # error-instead-of-exception + "UP031", # printf-string-formatting + "UP038", # non-pep604-isinstance +] + +[lint.per-file-ignores] +"**/__init__.py" = [ + "F401", # unused-import +] + +[lint.isort] +# https://www.odoo.com/documentation/master/contributing/development/coding_guidelines.html#imports +section-order = ["future", "standard-library", "third-party", "first-party", "local-folder"] +known-first-party = ["odoo"] +known-local-folder = ["odoo.addons"] diff --git a/scripts/ruff.sh b/scripts/ruff.sh new file mode 100644 index 00000000000..e45d20cd59c --- /dev/null +++ b/scripts/ruff.sh @@ -0,0 +1 @@ +ruff check ./estate --fix && ruff format ./estate