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..86f41074884 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,20 @@ +{ + 'name': 'Real Estate', + 'version': '0.0', + '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, + 'author': 'juica', + 'license': 'LGPL-3', +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..8f914bbb526 --- /dev/null +++ b/estate/models/__init__.py @@ -0,0 +1,5 @@ +from . import estate_property +from . import estate_property_type +from . import estate_property_tag +from . import estate_property_offer +from . import res_user diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py new file mode 100644 index 00000000000..b9040563abc --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,150 @@ +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 EstateProperty(models.Model): + _name = "estate.property" + _description = "Real Estate Property" + _order = "id desc" + + name = fields.Char('Property Name', required=True, translate=True) + description = fields.Text('Description', translate=True) + postcode = fields.Char('Post Code', required=True) + date_availability = fields.Date( + 'Availability Date', + required=True, + copy=False, + default=fields.Date.today() + relativedelta(months=3), + ) + type_id = fields.Many2one("estate.property.type", string="Type", required=True) + offer_ids = fields.One2many("estate.property.offer", "property_id") + tag_ids = fields.Many2many("estate.property.tag", string="Tags") + salesperson_id = fields.Many2one( + "res.users", + string="Salesperson", + default=lambda self: self.env.user, + ) + buyer_id = fields.Many2one( + "res.partner", + string="Buyer", + copy=False, + ) + expected_price = fields.Float('Expected Price') + selling_price = fields.Float( + 'Selling Price', + readonly=True, + copy=False, + ) + bedrooms = fields.Integer( + '# Bedrooms', + default=2, + ) + facades = fields.Integer('# Facades') + garage = fields.Boolean('Garage') + garden = fields.Boolean('Garden') + living_area = fields.Integer('Living Area mt²') + garden_area = fields.Integer('Garden mt²') + garden_orientation = fields.Selection( + string='Garden Orientation', + selection=[ + ('north', 'North'), + ('south', 'South'), + ('east', 'East'), + ('west', 'West'), + ], + ) + active = fields.Boolean('Active', default=True) + state = fields.Selection( + string='State', + selection=[ + ('new', 'New'), + ('offer_received', 'Offer Received'), + ('offer_accepted', 'Offer Accepted'), + ('sold', 'Sold'), + ('cancelled', 'Cancelled'), + ], + default="new", + copy=False, + required=True, + readonly=True, + # group_expand=True + ) + total_area = fields.Integer( + "Total Area m²", + compute="_compute_total_area", + ) + best_price = fields.Float( + "Best Price", + compute="_compute_best_price", + ) + + @api.depends("living_area", "garden_area") + def _compute_total_area(self): + for property in self: + property.total_area = property.living_area + property.garden_area + + @api.depends("offer_ids.price") + def _compute_best_price(self): + for property in self: + property.best_price = max(property.offer_ids.mapped("price"), default=0) + + @api.onchange("garden") + def _onchange_garden(self): + if self.garden: + self.garden_area = 10 + # Nasty magic string. I should turn the option into a variable and then reference it + self.garden_orientation = "north" + return + + self.garden_area = 0 + self.garden_orientation = None + + def action_sold(self): + for property in self: + if not property.selling_price: + raise UserError("You can not sell a property without accepted offers") + + if property.state == "cancelled": + raise UserError("You can not sell a cancelled property") + + property.state = "sold" + + return True + + def action_cancel(self): + for property in self: + if property.state == "sold": + raise UserError("You can not cancel a sold property") + + property.state = "cancelled" + + return True + + _exp_price_positive = models.Constraint( + 'CHECK(expected_price > 0)', + 'The property expected price must be strictly positive', + ) + + _sell_price_positive = models.Constraint( + 'CHECK(selling_price >= 0)', + 'The property selling price must be positive', + ) + + @api.constrains("selling_price", "expected_price") + def _check_selling_price(self): + for property in self: + if float_is_zero(property.selling_price, precision_digits=2): + continue + + limit = 0.9 * property.expected_price + if float_compare(property.selling_price, limit, precision_digits=2) == -1: + raise ValidationError("The property selling price must be at least 90% of the expected price") + + @api.ondelete(at_uninstall=False) + def _unlink_except_not_new_or_cancelled(self): + for property in self: + if property.state not in ('new', 'cancelled'): + raise UserError("You can not delete a property that is not either 'new' or 'cancelled'") diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py new file mode 100644 index 00000000000..412422f5740 --- /dev/null +++ b/estate/models/estate_property_offer.py @@ -0,0 +1,96 @@ +from dateutil.relativedelta import relativedelta + +from odoo import api, fields, models +from odoo.exceptions import UserError +from odoo.tools.float_utils import float_compare, float_is_zero + + +class EstatePropertyOffer(models.Model): + _name = "estate.property.offer" + _description = "Real Estate Property Offer" + _order = "price desc" + + price = fields.Float('Price', required=True) + property_id = fields.Many2one('estate.property', 'property_id', required=True) + partner_id = fields.Many2one('res.partner', required=True) + status = fields.Selection( + string='Status', + copy=False, + selection=[ + ('accepted', 'Accepted'), + ('refused', 'Refused'), + ], + ) + property_type_id = fields.Many2one(related="property_id.type_id", store=True) + validity = fields.Integer( + "Validity (days)", + default=7, + required=True, + ) + date_deadline = fields.Date( + "Deadline", + required=True, + compute="_compute_deadline", + inverse="_inverse_deadline", + ) + + @api.depends("validity") + def _compute_deadline(self): + for offer in self: + offer.date_deadline = fields.Date.today() + relativedelta(days=offer.validity) + + def _inverse_deadline(self): + for offer in self: + delta = offer.date_deadline - fields.Date.today() + offer.validity = delta.days + + def action_offer_accept(self): + for offer in self: + if offer.property_id.state in ('offer_accepted', 'sold', 'cancelled'): + raise UserError("You can not accept more offers for this property") + + offer.status = "accepted" + offer.property_id.state = "offer_accepted" + offer.property_id.buyer_id = offer.partner_id + offer.property_id.selling_price = offer.price + + return True + + def action_offer_refuse(self): + for offer in self: + offer.status = "refused" + + return True + + _price_positive = models.Constraint( + 'CHECK(price > 0)', + 'The offer price must be strictly positive', + ) + + @api.model_create_multi + def create(self, values): + # Get the lowest new offer per property + prop_min_offer = {} + for v in values: + pid = v['property_id'] + prop_min_offer[pid] = min(prop_min_offer.get(pid, float('inf')), v.get('price', 0)) + + # Browse the properties referenced by the new offers + properties = self.env['estate.property'].browse(prop_min_offer.keys()) + + for prop in properties: + if prop.state in ('sold', 'cancelled'): + raise UserError("You can not make an offer on a sold or cancelled property") + + # No new offer may be lower than the best existing one + best_existing = prop.offer_ids[0].price if len(prop.offer_ids) else 0.0 + + if float_is_zero(best_existing, precision_digits=2): + continue + + if float_compare(prop_min_offer[prop.id], best_existing, precision_digits=2) == -1: + raise UserError("You can not offer less than the biggest offer") + + properties.filtered(lambda p: p.state == 'new').state = 'offer_received' + + return super().create(values) diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py new file mode 100644 index 00000000000..28c549e1195 --- /dev/null +++ b/estate/models/estate_property_tag.py @@ -0,0 +1,15 @@ +from odoo import fields, models + + +class EstatePropertyTag(models.Model): + _name = "estate.property.tag" + _description = "Real Estate Property Tag" + _order = "name" + + name = fields.Char('Tag Name', required=True, translate=True) + color = fields.Integer(string="Color Index") + + _uniq_name = models.Constraint( + 'UNIQUE(name)', + 'The tag name must be unique', + ) diff --git a/estate/models/estate_property_type.py b/estate/models/estate_property_type.py new file mode 100644 index 00000000000..9b69f669d51 --- /dev/null +++ b/estate/models/estate_property_type.py @@ -0,0 +1,24 @@ +from odoo import api, fields, models + + +class EstatePropertyType(models.Model): + _name = "estate.property.type" + _description = "Real Estate Property Type" + _order = "sequence" + + name = fields.Char('Type Name', required=True, translate=True) + sequence = fields.Integer('Sequence', default=1) + + property_ids = fields.One2many("estate.property", "type_id") + offer_ids = fields.One2many("estate.property.offer", "property_type_id") + offer_count = fields.Integer(compute="_compute_offer_count", default=0) + + @api.depends("offer_ids") + def _compute_offer_count(self): + for offer in self: + offer.offer_count = len(offer.offer_ids) + + _uniq_name = models.Constraint( + 'UNIQUE(name)', + 'The type name must be unique', + ) diff --git a/estate/models/res_user.py b/estate/models/res_user.py new file mode 100644 index 00000000000..80838e2454e --- /dev/null +++ b/estate/models/res_user.py @@ -0,0 +1,10 @@ +from odoo import fields, models + + +class ResUser(models.Model): + _inherit = "res.users" + + property_ids = fields.One2many( + "estate.property", "salesperson_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..cc31a1ef9aa --- /dev/null +++ b/estate/security/ir.model.access.csv @@ -0,0 +1,8 @@ +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_user,access_estate_property_type_user,model_estate_property_type,base.group_user,1,0,0,0 +access_estate_property_type_admin,access_estate_property_type_admin,model_estate_property_type,base.group_system,1,1,1,1 +access_estate_property_tag_user,access_estate_property_tag_user,model_estate_property_tag,base.group_user,1,0,0,0 +access_estate_property_tag_admin,access_estate_property_tag_admin,model_estate_property_tag,base.group_system,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/tests/__init__.py b/estate/tests/__init__.py new file mode 100644 index 00000000000..c613c60ae1a --- /dev/null +++ b/estate/tests/__init__.py @@ -0,0 +1 @@ +from . import test_property diff --git a/estate/tests/test_property.py b/estate/tests/test_property.py new file mode 100644 index 00000000000..82efc0ece8c --- /dev/null +++ b/estate/tests/test_property.py @@ -0,0 +1,72 @@ +from odoo.exceptions import UserError +from odoo.tests import tagged, Form +from odoo.tests.common import TransactionCase + + +# The CI will run these tests after all the modules are installed, +# not right after installing the one defining it. +@tagged('post_install', '-at_install') +class EstateTestCase(TransactionCase): + + @classmethod + def setUpClass(cls): + # add env on cls and many other things + super().setUpClass() + + # create the data for each tests. By doing it in the setUpClass instead + # of in a setUp or in each test case, we reduce the testing time and + # the duplication of code. + cls.property_type = cls.env['estate.property.type'].create({'name': 'House'}) + cls.buyer = cls.env['res.partner'].create({'name': 'Test Buyer'}) + cls.properties = cls.env['estate.property'].create([ + { + 'name': 'Property A', + 'postcode': '1000', + 'type_id': cls.property_type.id, + 'expected_price': 100000, + 'garden': True, + 'garden_area': 10, + 'garden_orientation': 'north', + + }, + { + 'name': 'Property B', + 'postcode': '2000', + 'type_id': cls.property_type.id, + 'expected_price': 200000, + 'garden': True, + 'garden_area': 30, + 'garden_orientation': 'north', + }, + ]) + + def test_sell_without_accepted_offer(self): + """Selling a property with no accepted offers must fail.""" + with self.assertRaises(UserError): + self.properties.action_sold() + + def test_offer_on_sold_property(self): + """Creating an offer for a sold property must fail.""" + property = self.properties[0] + offer = self.env['estate.property.offer'].create({ + 'property_id': property.id, + 'partner_id': self.buyer.id, + 'price': property.expected_price, + }) + offer.action_offer_accept() + property.action_sold() + + with self.assertRaises(UserError): + self.env['estate.property.offer'].create({ + 'property_id': property.id, + 'partner_id': self.buyer.id, + 'price': property.expected_price * 2, + }) + + def test_garden_uncheck_reset(self): + prop = self.properties[0] + with Form(prop) as p: + p.garden = True + p.garden = False + + self.assertEqual(prop.garden_area, 0) diff --git a/estate/views/estate_menus.xml b/estate/views/estate_menus.xml new file mode 100644 index 00000000000..73ab08ea383 --- /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..21ce52a4fb6 --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,58 @@ + + + + Property Offers + estate.property.offer + list,form + [('property_type_id', '=', active_id)] + + + + estate.property.offer.list + estate.property.offer + + + + + + + + + +
+

+ +

+
+ + + + + + + + + + +
+
+ + + estate.property.type.search + 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..3031fa237f8 --- /dev/null +++ b/estate/views/estate_property_views.xml @@ -0,0 +1,169 @@ + + + + Properties + estate.property + list,form,kanban + {'search_default_available': 1} + + + + 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/res_users_views.xml b/estate/views/res_users_views.xml new file mode 100644 index 00000000000..a9ddc43d538 --- /dev/null +++ b/estate/views/res_users_views.xml @@ -0,0 +1,19 @@ + + + + + + res.users.view.form.inherit.properties_ids + 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..f091eaedb0c --- /dev/null +++ b/estate_account/__manifest__.py @@ -0,0 +1,15 @@ +{ + 'name': 'Real Estate Accounting', + 'version': '0.0', + 'depends': [ + 'base', + 'estate', + 'account', + ], + 'data': [ + ], + 'installable': True, + 'application': True, + 'author': 'juica', + '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..dd6aa325b73 --- /dev/null +++ b/estate_account/models/estate_property.py @@ -0,0 +1,26 @@ +from odoo import Command, models + + +class InheritedModel(models.Model): + _inherit = "estate.property" + + def action_sold(self): + for property in self: + self.env['account.move'].create({ + 'partner_id': property.buyer_id.id, + 'move_type': 'out_invoice', + 'line_ids': [ + Command.create({ + 'name': f"Property: {property.name}", # Prob should use _(%d) for translating, but its just a demo ¯\_(ツ)_/¯ + 'quantity': 1, + 'price_unit': .06 * property.selling_price, + }), + Command.create({ + 'name': "Administrative fees", + 'quantity': 1, + 'price_unit': 100, + }), + ], + }) + + return super().action_sold()