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..c0914301f2b --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,23 @@ +{ + 'name': 'Real Estate', + 'version': '0.1', + 'sequence': 100, + 'summary': 'Real Estate Advertisement', + 'depends': [ + 'base' + ], + 'data': [ + 'security/ir.model.access.csv', + 'views/estate_property_views.xml', + 'views/estate_property_offer_views.xml', + 'views/estate_property_type_views.xml', + 'views/estate_property_tag_views.xml', + 'views/res_users_views.xml', + 'views/estate_menus.xml' + ], + 'installable': True, + 'application': True, + 'assets': {}, + 'author': 'Odoo S.A.', + 'license': 'LGPL-3', +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..fea9f441d6d --- /dev/null +++ b/estate/models/__init__.py @@ -0,0 +1,5 @@ +from . import estate_property +from . import estate_property_offer +from . import estate_property_tag +from . import estate_property_type +from . import res_users diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py new file mode 100644 index 00000000000..53c9157e5c4 --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,153 @@ +from dateutil.relativedelta import relativedelta +from odoo import models, fields, api, exceptions +from odoo.tools import float_is_zero, float_compare + + +class EstateProperty(models.Model): + _name = 'estate.property' + _description = 'Estate Property' + _order = 'id desc' + + name = fields.Char('Title', required=True, translate=True) + description = fields.Text('Description', translate=True) + postcode = fields.Char('Postcode') + date_availability = fields.Date( + string='Available From', + default=lambda _: fields.Date.today() + relativedelta(months=3), + copy=False + ) + expected_price = fields.Float('Expected price', required=True) + selling_price = fields.Float('Selling price', readonly=True, copy=False) + bedrooms = fields.Integer('Bedrooms', default=2) + living_area = fields.Integer('Living area (sqm)') + facades = fields.Integer('Facades') + garage = fields.Boolean('Garage', default=False) + garden = fields.Boolean('Garden', default=False) + garden_area = fields.Integer('Garden area (sqm)') + garden_orientation = fields.Selection( + string='Garden orientation', + selection=[('north', 'North'), ('south', 'South'), ('east', 'East'), ('west', 'West')], + help='Garden orientation is important for determining how much sunlight and warmth the outdoor space receives' + ) + active = fields.Boolean('Active', default=False) + status = fields.Selection( + string='Status', + selection=[('new', 'New'), ('offer_received', 'Offer Received'), ('offer_accepted', 'Offer Accepted'), ('sold', 'Sold'), ('cancelled', 'Cancelled')], + default='new', + required=True, + ) + property_type_id = fields.Many2one("estate.property.type", string='Type') + property_tag_ids = fields.Many2many('estate.property.tag', string='Tags') + property_offer_ids = fields.One2many('estate.property.offer', inverse_name="property_id", string='Offers', copy=False) + # Computed Field + total_area = fields.Integer('Total area (sqm)', compute='_compute_total_area', readonly=True, copy=False) + best_price = fields.Float('Best offer', compute='_compute_best_price', readonly=True, copy=False) + # Other Info + salesperson_id = fields.Many2one( + comodel_name='res.users', + string='Salesman', + default=lambda self: self.env.user) + buyer_id = fields.Many2one( + comodel_name='res.partner', + string='Buyer', + copy=False, + readonly=True, + ) + + _check_expected_price = models.Constraint( + 'check(expected_price > 0)', + 'The expected price must be a positive amount and cannot be zero!', + ) + _check_selling_price = models.Constraint( + 'check(selling_price >= 0)', + 'The selling price must be a positive amount!', + ) + + @api.ondelete(at_uninstall=False) + def _unlink_if_new_or_cancelled(self): + if any(not record.status in ['new', 'cancelled'] for record in self): + raise exceptions.UserError("Can't delete property with status different from NEW or CANCELLED.") + + @api.constrains('selling_price', 'expected_price') + def _check_selling_price(self): + for record in self: + + if float_is_zero(value=record.selling_price, precision_digits=2): + # no accepted offers yet + return + + bottom_bound = .9 * record.expected_price + if float_compare(record.selling_price, bottom_bound, precision_digits=2) < 0: + raise exceptions.ValidationError("The selling price cannot be lower than 90% of the expected price.") + + @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("property_offer_ids.price") + def _compute_best_price(self): + for record in self: + if record.property_offer_ids: + record.best_price = max(record.property_offer_ids.mapped('price')) + else: + record.best_price = 0 + + @api.onchange('garden') + def _onchange_garden_flag(self): + if self.garden: + self.garden_area = 10 + self.garden_orientation = 'north' + return + + self.garden_area = 0 + self.garden_orientation = False + + def ensure_status_is_not(self, statuses, error_message=None): + """ Ensures that the status is not equal or included in statuses """ + if not statuses: + return + + if isinstance(statuses, str): + statuses = [statuses] + + if not isinstance(statuses, (list, tuple, set)): + raise TypeError('statuses must be a string, list, tuple, or set') + + if self.status in statuses: + msg = error_message or f'Action not allowed for status: {self.status}' + raise exceptions.UserError(msg) + + def _check_or_raise(self, condition, error_message=None): + if not condition: + msg = error_message or 'Action not allowed' + raise exceptions.UserError(msg) + + def ensure_no_accepted_offers(self, error_message=None): + has_no_accepted = not any(offer.status == "accepted" for offer in self.property_offer_ids) + self._check_or_raise(has_no_accepted, error_message=error_message) + + def ensure_any_offers_with_status(self, status, error_message=None): + if not status: + raise ValueError('Status is required') + + has_at_least_one = any(offer.status == status for offer in self.property_offer_ids) + self._check_or_raise(has_at_least_one, error_message=error_message) + + # ACTIONS + + def action_set_status_cancelled(self): + for record in self: + record.ensure_status_is_not("sold", error_message="You cannot cancel a sold property") + record.status = "cancelled" + + return True + + def action_set_status_sold(self): + for record in self: + record.ensure_status_is_not("cancelled", error_message="You cannot sell a cancelled property") + record.ensure_any_offers_with_status("accepted", error_message="You cannot sell with no accepted offers") + + record.status = "sold" + + return True diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py new file mode 100644 index 00000000000..bbec1d2c426 --- /dev/null +++ b/estate/models/estate_property_offer.py @@ -0,0 +1,100 @@ +from datetime import date, timedelta +from typing import Self + +from odoo import models, fields, api +from odoo.exceptions import UserError +from odoo.tools import float_compare + + +class EstatePropertyOffer(models.Model): + _name = 'estate.property.offer' + _description = 'Estate Property Offer' + _order = 'price desc' + + price = fields.Float(string='Price') + status = fields.Selection( + string='Status', + selection=[("accepted", "Accepted"), ("refused", "Refused")], + copy=False, + readonly=True, + ) + partner_id = fields.Many2one( + comodel_name='res.partner', + string='Partner', + required=True, + ) + property_id = fields.Many2one( + comodel_name='estate.property', + string='Property', + required=True, + ) + validity = fields.Integer( + string='Validity (days)', + default=7, + required=True, + ) + # Computed Field + date_deadline = fields.Date( + string='Date of Deadline', + compute='_compute_date_deadline', + inverse='_inverse_date_deadline', + ) + # Related Field + property_type_id = fields.Many2one(related='property_id.property_type_id', string='Property Type', store=True) + + _check_price = models.Constraint( + 'check(price > 0)', + 'The offer price must be a positive amount and cannot be zero!', + ) + + @api.model + def create(self, vals) -> Self: + for val in vals: + estate_property = self.env['estate.property'].browse(val['property_id']) + + estate_property.ensure_status_is_not("sold", error_message="You cannot add an offer to a sold property.") + + # set the correct state + estate_property.status = 'offer_received' + + # offers should be higher than the ones we already have + if estate_property.property_offer_ids: + min_price = estate_property.property_offer_ids[-1].price + if float_compare(val['price'], min_price, precision_digits=2) < 0: + raise UserError(f"The offer {val['price']} cannot be lower than the other offers.") + + return super().create(vals) + + @api.depends('create_date', 'validity') + def _compute_date_deadline(self): + for record in self: + record.date_deadline = self._get_date_or_today(record.create_date) + timedelta(days=record.validity) + + def _inverse_date_deadline(self): + for record in self: + create_date_or_today = self._get_date_or_today(record.create_date) + record.validity = (record.date_deadline - create_date_or_today).days + + # ACTIONS + + def action_accept_offer(self): + for record in self: + # If an offer is already accepted we can't accept another one + record.property_id.ensure_no_accepted_offers(error_message="An offer was already accepted") + + record.status = "accepted" + record.property_id.selling_price = record.price + record.property_id.buyer_id = record.partner_id + + return True + + def action_refuse_offer(self): + for record in self: + record.status = "refused" + + return True + + @staticmethod + def _get_date_or_today(datetime_to_evaluate): + """ Returns the date part of a given datetime if present, otherwise returns today's date """ + return datetime_to_evaluate.date() if datetime_to_evaluate else date.today() diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py new file mode 100644 index 00000000000..55e28203345 --- /dev/null +++ b/estate/models/estate_property_tag.py @@ -0,0 +1,15 @@ +from odoo import models, fields + + +class EstatePropertyTag(models.Model): + _name = 'estate.property.tag' + _description = 'Estate Property Tag' + _order = 'name' + + name = fields.Char('Name', required=True, translate=True) + color = fields.Integer('Color', required=True, default=0) + + _name_uniq = models.Constraint( + 'unique (name)', + 'There is already a Property Tag with this name!.', + ) diff --git a/estate/models/estate_property_type.py b/estate/models/estate_property_type.py new file mode 100644 index 00000000000..88fc788b168 --- /dev/null +++ b/estate/models/estate_property_type.py @@ -0,0 +1,24 @@ +from odoo import models, fields, api + + +class EstatePropertyType(models.Model): + _name = 'estate.property.type' + _description = 'Estate Property Type' + _order = 'sequence, name' + + name = fields.Char('Name', required=True, translate=True) + sequence = fields.Integer('Sequence', default=1, help="Used for ordering purposes") + property_id = fields.One2many('estate.property', 'property_type_id', string='Property') + offer_ids = fields.One2many('estate.property.offer', 'property_type_id', string='Offers') + # Compute Field + offer_count = fields.Integer(string='Offer Count', compute='_compute_offer_count') + + _name_uniq = models.Constraint( + 'unique (name)', + 'There is already a Property Type with this 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_users.py b/estate/models/res_users.py new file mode 100644 index 00000000000..82ce7afa2df --- /dev/null +++ b/estate/models/res_users.py @@ -0,0 +1,8 @@ +from odoo import models, fields + + +class ResUsers(models.Model): + _name = 'res.users' + _inherit = ["res.users"] + + property_ids = fields.One2many('estate.property', 'salesperson_id', string="Real Estate Properties", domain=[('status', '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..86e6b0ffd1c --- /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,estate.property access,model_estate_property,base.group_user,1,1,1,1 +access_estate_property_type,estate.property.type access,model_estate_property_type,base.group_user,1,1,1,1 +access_estate_property_tag,estate.property.tag access,model_estate_property_tag,base.group_user,1,1,1,1 +access_estate_property_offer,estate.property.offer access,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..c0429d2f226 --- /dev/null +++ b/estate/tests/test_estate_property.py @@ -0,0 +1,102 @@ +from odoo.tests.common import TransactionCase +from odoo.exceptions import UserError +from odoo.tests import tagged, Form + + +@tagged('estate', 'post_install', '-at_install') +class EstatePropertyTestCase(TransactionCase): + + @classmethod + def setUpClass(cls): + super().setUpClass() + + # Create property type + cls.p_types = cls.env['estate.property.type'].create([ + {'name': 'House'}, + {'name': 'Studio'} + ]) + + # Create property tag + cls.p_tags = cls.env['estate.property.tag'].create([ + {'name': 'Fantastic'}, + {'name': 'Dark', 'color': 1} + ]) + + # Create property + cls.properties = cls.env['estate.property'].create([ + { + 'name': 'Test Property', + 'expected_price': 123, + 'status': 'new', + 'property_type_id': cls.p_types[0].id, + 'property_tag_ids': cls.p_tags.mapped('id') + } + ]) + + cls.test_partner = cls.env['res.partner'].create({ + 'city': 'OrigCity', + 'name': 'TestingPartner', + }) + + def test_total_area(self): + """Test that the total area is well computed""" + property_0 = self.properties[0] + + property_0.living_area = 20 + + self.assertEqual(property_0.total_area, property_0.living_area) + + property_0.garden_area = 30 + + self.assertEqual(property_0.total_area, property_0.living_area + property_0.garden_area) + + def test_create_offer_sold_property(self): + """Test that is forbidden to create an offer to a sold property""" + property_0 = self.properties[0] + + property_0.status = 'sold' + + with self.assertRaises(UserError): + self.env['estate.property.offer'].create([ + { + 'price': 123, + 'property_id': property_0.id, + 'partner_id': self.test_partner.id + } + ]) + + def test_action_sell_no_accepted_offers(self): + """Test that is forbidden to sell a property with no accepted offers""" + property_0 = self.properties[0] + + offer = self.env['estate.property.offer'].create([ + { + 'price': 123, + 'property_id': property_0.id, + 'partner_id': self.test_partner.id + } + ]) + + with self.assertRaises(UserError): + property_0.action_set_status_sold() + + # Happy path + offer.action_accept_offer() + property_0.action_set_status_sold() + + def test_reset_fields_on_uncheck_garden(self): + property_form = Form(self.env['estate.property']) + + property_form.garden = True + # Fields should be visible and filled with default values + self.assertEqual(property_form._get_modifier('garden_area', 'invisible'), False) + self.assertEqual(property_form._get_modifier('garden_orientation', 'invisible'), False) + self.assertEqual(property_form.garden_area, 10) + self.assertEqual(property_form.garden_orientation, 'north') + + property_form.garden = False + # Fields should be invisible and filled with reset values + self.assertEqual(property_form.garden_area, 0) + self.assertEqual(property_form.garden_orientation, False) + self.assertEqual(property_form._get_modifier('garden_area', 'invisible'), True) + self.assertEqual(property_form._get_modifier('garden_orientation', 'invisible'), True) diff --git a/estate/views/estate_menus.xml b/estate/views/estate_menus.xml new file mode 100644 index 00000000000..3aafa46be3a --- /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..afc4c58740d --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,44 @@ + + + + + Offers + estate.property.offer + list + [('property_type_id', '=', active_id)] + + + + estate.property.offer.form + estate.property.offer + +
+ + + + + + + + + +
+
+
+ + + estate.property.offer.list + estate.property.offer + + + + + + + + +
+

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

+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+ + + estate.property.list + estate.property + + + + + + + + + + + + + + + + estate.property.kanban + estate.property + + + + +
+
+ +
+
+ Expected Price: +
+
+ Best Offer: +
+
> + Selling Price: +
+ +
+
+
+
+
+
+
+
diff --git a/estate/views/res_users_views.xml b/estate/views/res_users_views.xml new file mode 100644 index 00000000000..f6e98a7e1bd --- /dev/null +++ b/estate/views/res_users_views.xml @@ -0,0 +1,17 @@ + + + + + res.users.view.form.inherit.estate + res.users + + + + + + + + + + + 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..f3a4956cfbf --- /dev/null +++ b/estate_account/__manifest__.py @@ -0,0 +1,17 @@ +{ + 'name': 'Real Estate Account', + 'version': '0.1', + 'sequence': 100, + 'summary': 'Real Estate Account', + 'depends': [ + 'account', + 'estate' + ], + 'data': [ + 'security/ir.model.access.csv' + ], + 'installable': True, + 'assets': {}, + '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..003c6eaff0e --- /dev/null +++ b/estate_account/models/estate_property.py @@ -0,0 +1,24 @@ +from odoo import models +from odoo.fields import Command + + +class EstateProperty(models.Model): + _name = 'estate.property' + _inherit = ["estate.property"] + + # ACTIONS + + def action_set_status_sold(self): + result = super().action_set_status_sold() + + for record in self: + self.env["account.move"].create({ + 'partner_id': record.buyer_id.id, + 'move_type': 'out_invoice', + 'line_ids': [ + Command.create({'name': '6% of the selling price', 'quantity': 1, 'price_unit': 0.06 * record.selling_price}), + Command.create({'name': 'administrative fees', 'quantity': 1, 'price_unit': 100}), + ] + }) + + return result diff --git a/estate_account/security/ir.model.access.csv b/estate_account/security/ir.model.access.csv new file mode 100644 index 00000000000..97dd8b917b8 --- /dev/null +++ b/estate_account/security/ir.model.access.csv @@ -0,0 +1 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink