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..109a18aa1e1 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,25 @@ +{ + 'name': 'Estate', + 'depends': [ + 'base' + ], + 'data': [ + 'security/ir.model.access.csv', + 'data/ir_config_parameter_data.xml', + 'data/estate_types.xml', + 'views/estate_property_views.xml', + 'views/estate_property_offer_views.xml', + 'views/estate_property_type_views.xml', + 'views/estate_property_tag_views.xml', + 'views/estate_res_user_views.xml', + 'views/res_config_settings_views.xml', + 'views/estate_menu_views.xml' + ], + 'demo': [ + 'demo/demo_property_data.xml', + 'demo/demo_offer_data.xml' + ], + 'application': True, + 'author': 'Odoo S.A.', + 'license': 'LGPL-3' +} diff --git a/estate/data/estate_types.xml b/estate/data/estate_types.xml new file mode 100644 index 00000000000..00bf748622b --- /dev/null +++ b/estate/data/estate_types.xml @@ -0,0 +1,14 @@ + + + Residential + + + Commercial + + + Industrial + + + Land + + diff --git a/estate/data/ir_config_parameter_data.xml b/estate/data/ir_config_parameter_data.xml new file mode 100644 index 00000000000..b6ed55c7c43 --- /dev/null +++ b/estate/data/ir_config_parameter_data.xml @@ -0,0 +1,12 @@ + + + + estate.default_garden_area + 10 + + + estate.default_garden_orientation + north + + + diff --git a/estate/demo/demo_offer_data.xml b/estate/demo/demo_offer_data.xml new file mode 100644 index 00000000000..6e0aff85c27 --- /dev/null +++ b/estate/demo/demo_offer_data.xml @@ -0,0 +1,26 @@ + + + + + 10000 + 14 + + + + + + 1500000 + 14 + + + + + + 1500001 + 14 + + + + + + diff --git a/estate/demo/demo_property_data.xml b/estate/demo/demo_property_data.xml new file mode 100644 index 00000000000..62dd3152857 --- /dev/null +++ b/estate/demo/demo_property_data.xml @@ -0,0 +1,55 @@ + + + Big Villa + new + + A nice and big villa + 12345 + 2020-02-02 + 1600000 + 6 + 100 + 4 + True + True + 100000 + south + + + + Trailer home + cancelled + + Home in trailer park + 54321 + 1970-01-01 + 100000 + 120000 + 1 + 10 + 4 + False + + + + Appartment + new + + Nice appartment + 13579 + 2025-01-01 + 350000 + 2 + 80 + 4 + True + + + diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..afa462190a9 --- /dev/null +++ b/estate/models/__init__.py @@ -0,0 +1,6 @@ +from . import estate_property +from . import estate_property_offer +from . import estate_property_tag +from . import estate_property_type +from . import res_config_settings +from . import res_user diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py new file mode 100644 index 00000000000..e94d2740883 --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,101 @@ +from odoo import api, fields, models, _ +from odoo.exceptions import UserError, ValidationError +from odoo.tools import float_compare + + +class PropertyModel(models.Model): + _name = "estate.property" + _description = "Estate Property model" + _order = "id desc" + + name = fields.Char("Title", required=True) + description = fields.Text() + postcode = fields.Char() + date_availability = fields.Date(default=fields.Date.add(fields.Date.today(), months=3), copy=False) + expected_price = fields.Float(required=True) + best_offer = fields.Float(compute="_compute_highest_price") + selling_price = fields.Float(readonly=True, copy=False) + bedrooms = fields.Integer(default=2) + living_area = fields.Integer("Living Area (sqm)") + facades = fields.Integer() + garage = fields.Boolean() + garden = fields.Boolean() + garden_area = fields.Integer("Garden Area (sqm)") + garden_orientation = fields.Selection( + selection=[('north', 'North'), ('south', 'South'), ('east', 'East'), ('west', 'West')] + ) + total_living_area = fields.Integer("Total Area (sqm)", compute="_compute_total_area") + active = fields.Boolean(default=True) + state = fields.Selection( + selection=[ + ("new", "New"), + ("received", "Offer Received"), + ("accepted", "Offer Accepted"), + ("sold", "Sold"), + ("cancelled", "Cancelled") + ], + string="Status", + required=True, + copy=False, + default="new" + ) + property_type_id = fields.Many2one("estate.property.type") + buyer_id = fields.Many2one("res.partner", copy=False) + salesperson_id = fields.Many2one("res.users", default=lambda self: self.env.user) + tag_ids = fields.Many2many("estate.property.tag") + offer_ids = fields.One2many("estate.property.offer", "property_id") + + _check_positive_expected_price = models.Constraint( + "CHECK(expected_price >= 0)", + "The expected price must be positive." + ) + _check_positive_selling_price = models.Constraint( + "CHECK(selling_price >= 0)", + "The selling price must be positive" + ) + + @api.depends("living_area", "garden_area") + def _compute_total_area(self): + for record in self: + record.total_living_area = record.living_area + record.garden_area + + @api.depends("offer_ids") + def _compute_highest_price(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 = self.env["ir.config_parameter"].get_param("estate.default_garden_area") + self.garden_orientation = self.env["ir.config_parameter"].get_param("estate.default_garden_orientation") + else: + self.garden_area = 0 + self.garden_orientation = None + + @api.constrains("selling_price", "expected_price") + def _check_selling_price(self): + for record in self: + if record.selling_price and float_compare(record.selling_price, record.expected_price * .9, 0) == -1: + raise ValidationError("The selling price cannot be lower than 90% of the expected price.") + + @api.ondelete(at_uninstall=False) + def _unlink_if_new_or_cancelled(self): + if any(record.state not in ('new', 'cancelled') for record in self): + raise UserError(_("Only 'New' and 'Cancelled' properties can be deleted.")) + + def action_mark_as_sold(self): + self.ensure_one() + if self.state == "cancelled": + raise UserError(_("A cancelled property cannot be set as sold.")) + if not any(offer.status == "accepted" for offer in self.offer_ids): + raise UserError(_("A property must have an accepted offer to be marked as sold.")) + self.state = "sold" + return True + + def action_mark_as_cancelled(self): + self.ensure_one() + if self.state == "sold": + raise UserError(_("A sold property cannot be set as cancelled.")) + self.state = "cancelled" + return True diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py new file mode 100644 index 00000000000..5c2908d6a07 --- /dev/null +++ b/estate/models/estate_property_offer.py @@ -0,0 +1,68 @@ +from odoo import api, fields, models, _ +from odoo.exceptions import UserError + + +class PropertyOfferModel(models.Model): + _name = "estate.property.offer" + _description = "Estate Property Offer model" + _order = "price desc" + + price = fields.Float() + status = fields.Selection( + selection=[ + ("accepted", "Accepted"), + ("refused", "Refused") + ], + copy=False + ) + partner_id = fields.Many2one("res.partner", required=True) + property_id = fields.Many2one("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_deadline", inverse="_inverse_deadline") + + _check_price = models.Constraint( + "CHECK(price >= 0)", + "The price of the offer must be positive." + ) + + @api.depends("validity") + def _compute_deadline(self): + for record in self: + record.date_deadline = fields.Date.add((record.create_date or fields.Datetime.now()), days=record.validity) + + def _inverse_deadline(self): + for record in self: + record.validity = (record.date_deadline - fields.Date.to_date(record.create_date)).days if record.date_deadline else record.validity + + @api.model + def create(self, vals_list: list[dict]): + EstateProperties = self.env["estate.property"].with_prefetch( + [vals["property_id"] for vals in vals_list] + ) + for val in vals_list: + estate_property = EstateProperties.browse(val["property_id"]) + if estate_property.state == "sold": + raise UserError(_("Cannot create a new offer for a sold property.")) + if estate_property.offer_ids and val["price"] < min(estate_property.offer_ids.mapped("price")): + raise UserError(_("Cannot create a new offer with a lower price than an existing offer.")) + if estate_property.state == 'new': + estate_property.state = 'received' + return super().create(vals_list) + + def action_accept_offer(self): + self.ensure_one() + self.status = "accepted" + self.property_id.selling_price = self.price + self.property_id.buyer_id = self.partner_id + self.property_id.state = "accepted" + self.refuse_all_other_offers() + + def refuse_all_other_offers(self): + for offer in self.property_id.offer_ids: + if offer != self: + offer.status = "refused" + + def action_refuse_offer(self): + self.ensure_one() + self.status = "refused" diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py new file mode 100644 index 00000000000..7764bebd6e4 --- /dev/null +++ b/estate/models/estate_property_tag.py @@ -0,0 +1,15 @@ +from odoo import fields, models + + +class PropertyTagModel(models.Model): + _name = "estate.property.tag" + _description = "Estate Property Tag model" + _order = "name" + + name = fields.Char(required=True) + color = fields.Integer() + + _check_tag_uniqueness = models.Constraint( + "UNIQUE(name)", + "Each tag should have a unique name." + ) diff --git a/estate/models/estate_property_type.py b/estate/models/estate_property_type.py new file mode 100644 index 00000000000..1ac10368cc4 --- /dev/null +++ b/estate/models/estate_property_type.py @@ -0,0 +1,23 @@ +from odoo import api, fields, models + + +class PropertyTypeModel(models.Model): + _name = "estate.property.type" + _description = "Estate Property Type model" + _order = "sequence" + + name = fields.Char(required=True) + property_ids = fields.One2many("estate.property", "property_type_id") + offer_ids = fields.One2many("estate.property.offer", "property_type_id") + offer_count = fields.Integer("Offer Count", compute="_compute_offer_count") + sequence = fields.Integer("Sequence") + + _check_type_uniqueness = models.Constraint( + "UNIQUE(name)", + "Each type should have a unique name." + ) + + @api.depends("offer_ids") + def _compute_offer_count(self): + for record in self: + record.offer_count = len(record.offer_ids) diff --git a/estate/models/res_config_settings.py b/estate/models/res_config_settings.py new file mode 100644 index 00000000000..bc8c7cea325 --- /dev/null +++ b/estate/models/res_config_settings.py @@ -0,0 +1,11 @@ +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + garden_area = fields.Integer(default=10, config_parameter="estate.default_garden_area") + garden_orientation = fields.Selection( + selection=[('north', 'North'), ('south', 'South'), ('east', 'East'), ('west', 'West')], + config_parameter="estate.default_garden_orientation" + ) diff --git a/estate/models/res_user.py b/estate/models/res_user.py new file mode 100644 index 00000000000..095bfdcd4b4 --- /dev/null +++ b/estate/models/res_user.py @@ -0,0 +1,9 @@ +from odoo import fields, models + + +class ResUser(models.Model): + _inherit = "res.users" + + property_ids = fields.One2many( + "estate.property", "salesperson_id", domain=['|', ('state', '=', 'new'), ('state', '=', 'received')] + ) diff --git a/estate/security/ir.model.access.csv b/estate/security/ir.model.access.csv new file mode 100644 index 00000000000..0c0b62b7fee --- /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 +estate.access_estate_property,access_estate_property,estate.model_estate_property,base.group_user,1,1,1,1 +estate.access_estate_property_type,access_estate_property_type,estate.model_estate_property_type,base.group_user,1,1,1,1 +estate.access_estate_property_tag,access_estate_property_tag,estate.model_estate_property_tag,base.group_user,1,1,1,1 +estate.access_estate_property_offer,access_estate_property_offer,estate.model_estate_property_offer,base.group_user,1,1,1,1 diff --git a/estate/tests/__init__.py b/estate/tests/__init__.py new file mode 100644 index 00000000000..576617cccff --- /dev/null +++ b/estate/tests/__init__.py @@ -0,0 +1 @@ +from . import test_estate_property diff --git a/estate/tests/test_estate_property.py b/estate/tests/test_estate_property.py new file mode 100644 index 00000000000..d00dbcd74b0 --- /dev/null +++ b/estate/tests/test_estate_property.py @@ -0,0 +1,107 @@ +from odoo.tests.common import TransactionCase +from odoo.exceptions import UserError +from odoo.tests import tagged, Form + + +@tagged("post_install", "-at_install") +class EstateTestCase(TransactionCase): + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.ADMINISTRATOR_ID = cls.env.ref("base.partner_admin").id + cls.new_property = cls.env["estate.property"].create([{ + "name": "New Property", + "state": "new", + "expected_price": 1000.0 + }]) + cls.received_property = cls.env["estate.property"].create([{ + "name": "Received offer Property", + "state": "received", + "expected_price": 2000.0 + }]) + cls.accepted_property = cls.env["estate.property"].create([{ + "name": "Accepeted offer Property", + "state": "accepted", + "expected_price": 3000.0 + }]) + cls.sold_property = cls.env["estate.property"].create([{ + "name": "Sold Property", + "state": "new", + "expected_price": 4000.0 + }]) + cls.env["estate.property.offer"].create([{ + "partner_id": cls.ADMINISTRATOR_ID, + "property_id": cls.sold_property.id, + "status": "accepted", + "price": 4000.0 + }]) + cls.sold_property.state = "sold" + cls.cancelled_property = cls.env["estate.property"].create([{ + "name": "Cancelled Property", + "state": "cancelled", + "expected_price": 5000.0 + }]) + cls.env["estate.property.offer"].create([ + { + "partner_id": cls.ADMINISTRATOR_ID, + "property_id": cls.accepted_property.id, + "status": "accepted", + "price": 3000.0 + }, + { + "partner_id": cls.ADMINISTRATOR_ID, + "property_id": cls.received_property.id, + "status": "refused", + "price": 1999.0 + }, + { + "partner_id": cls.ADMINISTRATOR_ID, + "property_id": cls.received_property.id, + "price": 2000.0 + } + ]) + + def test_offer_for_sold_property(self): + offer_for_sold_property = { + "partner_id": EstateTestCase.ADMINISTRATOR_ID, + "property_id": self.sold_property.id + } + correct_offers = [ + {"partner_id": EstateTestCase.ADMINISTRATOR_ID, "property_id": self.new_property.id, "price": 1000}, + {"partner_id": EstateTestCase.ADMINISTRATOR_ID, "property_id": self.received_property.id, "price": 2000}, + {"partner_id": EstateTestCase.ADMINISTRATOR_ID, "property_id": self.accepted_property.id, "price": 3000}, + {"partner_id": EstateTestCase.ADMINISTRATOR_ID, "property_id": self.cancelled_property.id, "price": 5000}, + ] + with self.assertRaises(UserError, msg="Creating an offer for a sold property should raise a UserError but it did not."): + self.env["estate.property.offer"].create([offer_for_sold_property]) + for offer in correct_offers: + self.env["estate.property.offer"].create([offer]) + + def test_sell_property_with_no_accepted_offer(self): + non_sellable_properties = [ + self.new_property, + self.received_property + ] + for property in non_sellable_properties: + with self.assertRaises(UserError): + property.action_mark_as_sold() + self.accepted_property.action_mark_as_sold() + + def test_sold_state_correctly_set(self): + self.accepted_property.action_mark_as_sold() + self.assertEqual(self.accepted_property.state, "sold", "When a property is sold, its state should be set to 'sold'.") + + def test_garden_area_orientation_reset(self): + property_form = Form(self.env["estate.property"]) + self.assertFalse(property_form.garden) + self.assertEqual(property_form.garden_area, 0) + self.assertFalse(property_form.garden_orientation) + property_form.garden = True + self.assertTrue(property_form.garden) + self.assertEqual(property_form.garden_area, 10) + self.assertEqual(property_form.garden_orientation, "north") + property_form.garden = False + self.assertFalse(property_form.garden) + self.assertEqual(property_form.garden_area, 0) + self.assertFalse(property_form.garden_orientation) diff --git a/estate/views/estate_menu_views.xml b/estate/views/estate_menu_views.xml new file mode 100644 index 00000000000..69042027848 --- /dev/null +++ b/estate/views/estate_menu_views.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..4333fe0c7b1 --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,45 @@ + + + Property Offers + estate.property.offer + list,form + [('property_type_id', '=', active_id)] + + + + estate.property.offer.list + estate.property.offer + + + + + + + + +

+ +

+ + + + + + + + + + + + + + + + + +
+
+ + + estate.property.type.list + estate.property.type + + + + + + + +
diff --git a/estate/views/estate_property_views.xml b/estate/views/estate_property_views.xml new file mode 100644 index 00000000000..c83a9b9d7fe --- /dev/null +++ b/estate/views/estate_property_views.xml @@ -0,0 +1,135 @@ + + + Properties + estate.property + list,form,kanban + {'search_default_available': True} + + + + estate.property.list + estate.property + + + + + + + + + + + + + + + estate.property.form + estate.property + +
+
+
+ + +

+ +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+
+ + + estate.property.search + estate.property + + + + + + + + + + + + + + + + estate.property.kanban + estate.property + + + + + +
+ + () +
+
+ Expected Price: + +
+
+ Best Offer: + +
+
+ Selling Price: + +
+ +
+
+
+
+
+
diff --git a/estate/views/estate_res_user_views.xml b/estate/views/estate_res_user_views.xml new file mode 100644 index 00000000000..fde66af8cfd --- /dev/null +++ b/estate/views/estate_res_user_views.xml @@ -0,0 +1,21 @@ + + + res.users.form + res.users + + + + + + + + + + + + + + + + + diff --git a/estate/views/res_config_settings_views.xml b/estate/views/res_config_settings_views.xml new file mode 100644 index 00000000000..a3851b741dd --- /dev/null +++ b/estate/views/res_config_settings_views.xml @@ -0,0 +1,28 @@ + + + Settings + res.config.settings + form + + + + res.config.settings.view.form.inherit.estate + res.config.settings + + + + + + + + sqm + + + + + + + + + + diff --git a/estate_account/__init__.py b/estate_account/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/estate_account/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/estate_account/__manifest__.py b/estate_account/__manifest__.py new file mode 100644 index 00000000000..a5cc3b3cc3f --- /dev/null +++ b/estate_account/__manifest__.py @@ -0,0 +1,11 @@ +{ + 'name': 'Estate Account', + 'depends': [ + 'base', + 'account', + 'estate' + ], + 'data': [], + 'author': 'Odoo S.A.', + 'license': 'LGPL-3' +} diff --git a/estate_account/models/__init__.py b/estate_account/models/__init__.py new file mode 100644 index 00000000000..5e1963c9d2f --- /dev/null +++ b/estate_account/models/__init__.py @@ -0,0 +1 @@ +from . import estate_property diff --git a/estate_account/models/estate_property.py b/estate_account/models/estate_property.py new file mode 100644 index 00000000000..78ec297815f --- /dev/null +++ b/estate_account/models/estate_property.py @@ -0,0 +1,39 @@ +import re + +from odoo import models, Command + + +class EstateAccountPropertyModel(models.Model): + _inherit = "estate.property" + + def action_mark_as_sold(self): + self.ensure_one() + self.env["account.move"].create({ + "name": self._generate_invoice_name(), + "partner_id": self.salesperson_id.id, + "move_type": "out_invoice", + "line_ids": [ + Command.create({ + "name": self.name, + "quantity": 1, + "price_unit": self.selling_price + }), + Command.create({ + "name": "VAT", + "quantity": 1, + "price_unit": self.selling_price * .06 + }), + Command.create({ + "name": "Administrative Fees", + "quantity": 1, + "price_unit": 100.00 + }) + ] + }) + return super().action_mark_as_sold() + + def _generate_invoice_name(self): + invoice = self.env["account.move"].search([("name", "like", "Invoice %")], order="id desc", limit=1) + if invoice and (match := re.match(r"Invoice\s+(\d+)", invoice.name)): + return f"Invoice {int(match.group(1)) + 1}" + return "Invoice 1"