diff --git a/estate/DO_NOT_TOUCH.txt b/estate/DO_NOT_TOUCH.txt new file mode 100644 index 00000000000..f17a1a9184d --- /dev/null +++ b/estate/DO_NOT_TOUCH.txt @@ -0,0 +1 @@ +This file should not be removed. 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..4e47d1d42f1 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,17 @@ +{ + 'name': 'Real Estate', + 'version': '1.9', + 'depends': [ + 'base', + ], + 'installable': True, + 'application': True, + 'data': [ + 'security/ir.model.access.csv', + 'views/estate_property_offer_view.xml', + 'views/estate_property_views.xml', + 'views/estate_property_type_views.xml', + 'views/estate_property_tag_views.xml', + 'views/estate_menus.xml', + ], +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..2f1821a39c1 --- /dev/null +++ b/estate/models/__init__.py @@ -0,0 +1,4 @@ +from . import estate_property +from . import estate_property_type +from . import estate_property_tag +from . import estate_property_offer diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py new file mode 100644 index 00000000000..c3df94c6bad --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,109 @@ +from odoo import api, fields, models +from odoo.exceptions import UserError, ValidationError +from odoo.tools.float_utils import float_is_zero, float_compare + + +class Property(models.Model): + _name = "estate.property" + _description = "Estate property model" + _order = "id desc" + + _check_expected_price = models.Constraint( + 'CHECK(expected_price > 0)', + 'The expected price of a property must be positive.' + ) + _check_selling_price = models.Constraint( + 'CHECK(selling_price > 0)', + 'The selling price of a property must be positive.' + ) + + name = fields.Char(required=True) + description = fields.Text() + postcode = fields.Char() + date_availability = fields.Date(copy=False, default=lambda self: fields.Date.add(fields.Date.today(), months=3)) + expected_price = fields.Float(required=True) + selling_price = fields.Float(copy=False, readonly=True) + 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='Orientation', + selection=[ + ('north', 'North'), + ('south', 'South'), + ('east', 'East'), + ('west', 'West') + ], + help="Orientation is used to define the orientation of the garden" + ) + active = fields.Boolean("Active", default=True) + state = fields.Selection( + string='State', + selection=[ + ('new', 'New'), + ('offer_received', 'Offer Received'), + ('offer_accepted', 'Offer Accepted'), + ('sold', 'Sold'), + ('canceled', 'Canceled') + ], + required=True, + default="new" + ) + property_type_id = fields.Many2one("estate.property.type", string="Property Types") + salesman = fields.Many2one('res.users', string='Salesman', default=lambda self: self.env.user) + buyer = fields.Many2one('res.partner', string='Buyer') + tag_ids = fields.Many2many("estate.property.tag", string="Property Tag") + offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers") + total_area = fields.Integer(compute="_compute_area", string="Total Area (sqm)") + best_price = fields.Float(compute="_compute_best_price", string="Best Offer") + + @api.depends('living_area', 'garden_area') + def _compute_area(self): + for record in self: + record.total_area = record.living_area + record.garden_area + + @api.depends('offer_ids') + def _compute_best_price(self): + for record in self: + if record.offer_ids: + record.best_price = max(record.offer_ids.mapped('price')) + else: + record.best_price = 0 + + @api.onchange("garden") + def _onchange_partner_id(self): + if self.garden: + self.garden_area = 10 + self.garden_orientation = "north" + else: + self.garden_area = 0 + self.garden_orientation = None + + def action_set_cancel(self): + for record in self: + if record.state == "sold": + raise UserError("Sold properties cannot be canceled") + else: + record.state = "canceled" + return True + + def action_set_sold(self): + for record in self: + if record.state == "canceled": + raise UserError("Canceled properties cannot be sold") + else: + record.state = "sold" + return True + + @api.constrains('selling_price') + def check_selling_price(self): + for record in self: + if ( + not float_is_zero(record.selling_price, 2) + and (float_compare(record.selling_price, (record.expected_price * 0.90), 2) < 0) + ): + raise ValidationError("The selling price cannot be below 90 pourcent 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..0b3958bd519 --- /dev/null +++ b/estate/models/estate_property_offer.py @@ -0,0 +1,61 @@ +from odoo import api, fields, models + + +class PropertyOffer(models.Model): + _name = "estate.property.offer" + _description = "Estate property offer" + _order = "price desc" + + + _check_price = models.Constraint( + 'CHECK(price > 0)', + 'The offer price of a property must be positive.' + ) + + price = fields.Float(string="Price") + status = fields.Selection( + string="Status", + copy=False, + selection=[ + ('accepted', 'Accepted'), + ('refused', 'Refused') + ] + ) + partner_id = fields.Many2one("res.partner", string="Partner", required=True) + property_id = fields.Many2one("estate.property", string="Property", required=True) + validity = fields.Integer(default=7, string="Validity (days)") + date_deadline = fields.Date(string="Deadline", compute="_compute_deadline", inverse="_inverse_deadline") + property_type_id = fields.Many2one(related="property_id.property_type_id", string="Type of property", store=True) + + @api.depends('validity') + def _compute_deadline(self): + for record in self: + create_date = fields.Date.to_date(record.create_date) if record.create_date else fields.Date.today() + record.date_deadline = fields.Date.add(create_date, days=record.validity) + + def _inverse_deadline(self): + for record in self: + create_date = fields.Date.to_date(record.create_date) if record.create_date else fields.Date.today() + if record.date_deadline: + record.validity = ( + record.date_deadline - fields.Date.to_date(create_date) + ).days + else: + record.validity = 0 + + def action_set_accepted(self): + for record in self: + record.status = "accepted" + record.property_id.selling_price = record.price + record.property_id.buyer = record.partner_id + + for offer in record.property_id.offer_ids: + if offer != record: + offer.status = "refused" + return True + + def action_set_refused(self): + for record in self: + record.status = "refused" + return True + diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py new file mode 100644 index 00000000000..02851c5edf3 --- /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 = "Estate property tag" + _order = "name" + + name = fields.Char(required=True) + color = fields.Integer(string="Color") + + _uniq_name = models.Constraint( + 'UNIQUE(name)', + "This tag name is already taken" + ) diff --git a/estate/models/estate_property_type.py b/estate/models/estate_property_type.py new file mode 100644 index 00000000000..ae2a2642304 --- /dev/null +++ b/estate/models/estate_property_type.py @@ -0,0 +1,24 @@ +from odoo import fields, models, api + + +class PropertyType(models.Model): + _name = "estate.property.type" + _description = "Estate property type" + _order = "sequence, name" + + name = fields.Char(required=True) + property_ids = fields.One2many("estate.property", "property_type_id", "Property") + sequence = fields.Integer("Sequence", default=1) + offer_ids = fields.One2many(related="property_ids.offer_ids") + offer_count = fields.Integer(compute="_compute_deadline") + + _uniq_name = models.Constraint( + 'UNIQUE(name)', + "This type name is already taken" + ) + + @api.depends("offer_ids") + def _compute_deadline(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..8aef9a06500 --- /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_model,access_estate_model,model_estate_property,base.group_user,1,1,1,1 +access_estate_type_model,access_estate_type_model,model_estate_property_type,base.group_user,1,1,1,1 +access_estate_tag_model,access_estate_tag_model,model_estate_property_tag,base.group_user,1,1,1,1 +access_estate_offer_model,access_estate_offer_model,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..75b9efd32f7 --- /dev/null +++ b/estate/tests/test_estate_property.py @@ -0,0 +1,57 @@ +from odoo.tests.common import TransactionCase +from odoo.tests import tagged + + +@tagged('post_install', '-at_install') +class EstateTestCase(TransactionCase): + + @classmethod + def setUpClass(cls): + super(EstateTestCase, cls).setUpClass() + + cls.buyers = cls.env['res.partner'].create({ + 'name': 'Jerome Verkyndt', + }) + + cls.property = cls.env['estate.property'].create({ + 'name': 'House Test', + 'description': 'des test test', + 'postcode': '5081', + 'expected_price': 250000.0, + 'bedrooms': 4, + 'living_area': 100, + 'facades': 4, + 'garage': True, + 'garden': True, + 'garden_area': 100, + 'garden_orientation': 'north', + 'state': 'new', + }) + + + + def test_valid_offer(self): + + offer_1 = self.env["estate.property.offer"].create({ + 'price': 1000000, + 'partner_id': self.buyers.id, + 'validity': 7, + 'property_id': self.property.id + }) + offer_2 = self.env["estate.property.offer"].create({ + 'price': 2000000, + 'partner_id': self.buyers.id, + 'validity': 7, + 'property_id': self.property.id + }) + + self.assertRecordValues( + self.property, [{"offer_ids": {offer_1.id, offer_2.id}}] + ) + + offer_2.action_set_accepted() + + + self.assertRecordValues(offer_2, [{"status": "accepted"}]) + self.assertRecordValues(offer_1, [{"status": "refused"}]) + diff --git a/estate/views/estate_menus.xml b/estate/views/estate_menus.xml new file mode 100644 index 00000000000..fd80d4de520 --- /dev/null +++ b/estate/views/estate_menus.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/estate/views/estate_property_offer_view.xml b/estate/views/estate_property_offer_view.xml new file mode 100644 index 00000000000..3e9295a6c74 --- /dev/null +++ b/estate/views/estate_property_offer_view.xml @@ -0,0 +1,46 @@ + + + + estate_property_offer.form + estate.property.offer + +
+ + + + + + + + + +
+
+
+ + + estate_property_offer.list + estate.property.offer + + + + + + +