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..40b9b08d226 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,28 @@ +{ + "name": "Estate", + "summary": """ + test + """, + "description": """ + test + """, + "author": "covey", + "website": "", + "category": "Tutorials", + "version": "19.0.0.1.0", + "application": True, + "installable": True, + "depends": [ + "base", + ], + "data": [ + "security/ir.model.access.csv", + "views/res_users_views.xml", + "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" + ], + "license": "AGPL-3", +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..9a2189b6382 --- /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_users diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py new file mode 100644 index 00000000000..08fea508486 --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,113 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo import fields, models, api +from odoo.exceptions import ValidationError +from odoo.tools.float_utils import float_compare, float_is_zero + + +class EstateProperty(models.Model): + _name = "estate.property" + _description = "Property" + _order = "id desc" + + name = fields.Char(required=True) + description = fields.Text() + postcode = fields.Char() + date_availability = fields.Date( + copy=False, default=fields.Date.add(fields.Date.today(), months=3) + ) + expected_price = fields.Float(required=True) + selling_price = fields.Float(readonly=True, copy=False) + 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="Garden orientation", + selection=[ + ("north", "North"), + ("south", "South"), + ("east", "East"), + ("west", "West"), + ], + help="this is used to indicated the garden orientation", + ) + active = fields.Boolean(default=True) + state = fields.Selection( + string="State", + selection=[ + ("new", "New"), + ("offer_received", "Offer Received"), + ("offer_accepted", "Offer Accepted"), + ("sold", "Sold"), + ("cancelled", "Cancelled"), + ], + help="indicates the state of the property ad", + ) + estate_type_id = fields.Many2one("estate.property.type", string="Property Type") + buyer_id = fields.Many2one("res.partner", string="Buyer", copy=False) + salesperson_id = fields.Many2one("res.users", string="Sales Person") + tag_ids = fields.Many2many("estate.property.tag") + offer_ids = fields.One2many("estate.property.offer", "property_id") + total_area = fields.Float(compute="_compute_total") + + _check_expected_price_positive = models.Constraint( + "CHECK(expected_price >= 0)", "The expected price must be positive." + ) + _check_selling_price_positive = models.Constraint( + "CHECK(selling_price >= 0)", "The selling price must be positive." + ) + + @api.depends("living_area", "garden_area") + def _compute_total(self): + for record in self: + record.total_area = record.living_area + record.garden_area + + @api.onchange("garden") + def _onchange_garden(self): + for record in self: + if record.garden: + record.garden_area = 10 + record.garden_orientation = "north" + else: + record.garden_area = 0 + record.garden_orientation = None + + def action_cancel_sale(self): + for record in self: + record.state = "cancelled" + + def action_mark_as_sold(self): + for record in self: + if record.state == "cancelled": + raise ValidationError("Cancelled properties cannot be sold.") + record.state = "sold" + + @api.constrains("expected_price") + def _check_expected_price(self): + for record in self: + if float_is_zero(record.expected_price, precision_digits=2): + raise ValidationError("Expected price must be greater than zero.") + + @api.constrains("selling_price") + def _check_selling_price(self): + for record in self: + if ( + float_compare( + record.selling_price, + record.expected_price * 0.9, + precision_digits=2, + ) + < 0 + ): + raise ValidationError( + "Selling price cannot be lower than 90%of expected price!" + ) + + @api.ondelete(at_uninstall=False) + def _unlink_if_user_inactive(self): + if any(record.state not in ("new", "cancelled") for record in self): + raise ValidationError( + "Can't delete an opportunity that is neither 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..dd144aaba86 --- /dev/null +++ b/estate/models/estate_property_offer.py @@ -0,0 +1,90 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo import fields, models, api +from odoo.exceptions import ValidationError + + +class EstatePropertyOffer(models.Model): + _name = "estate.property.offer" + _description = "Property Offer" + _order = "price desc" + + price = fields.Float() + state = fields.Selection( + string="State", + selection=[("accepted", "Accepted"), ("refused", "Refused")], + help="State of the offer", + copy=False, + ) + partner_id = fields.Many2one("res.partner", required=True) + property_id = fields.Many2one("estate.property", required=True) + property_type_id = fields.Many2one( + "estate.property.type", related="property_id.estate_type_id", store=True + ) + validity = fields.Integer(default=7) + date_deadline = fields.Date( + compute="_compute_deadline", inverse="_inverse_deadline" + ) + + @api.depends("validity") + def _compute_deadline(self): + for record in self: + if record.create_date: + record.date_deadline = fields.Date.add( + record.create_date, days=record.validity + ) + else: + record.date_deadline = fields.Date.add( + fields.Date.today(), days=record.validity + ) + + def _inverse_deadline(self): + for record in self: + diff = int((record.date_deadline - fields.Date.today()).days) + record.validity = diff + + _check_offer_price_positive = models.Constraint( + "CHECK(price > 0)", "The offer price must be positive." + ) + + def action_accept_offer(self): + for record in self: + record.state = "accepted" + record.property_id.selling_price = record.price + record.property_id.buyer_id = record.partner_id + record.property_id.state = "sold" + + for offer in record.property_id.offer_ids: + if offer.id != record.id: + offer.state = "refused" + return True + + def action_refuse_offer(self): + for record in self: + record.state = "refused" + return True + + @api.model + def create(self, vals_list): + current_property = self.env["estate.property"].browse( + vals_list[0]["property_id"] + ) + if current_property.state not in {"new", "offer_received"}: + error_message = ( + "The sale is closed and no offer can be added for this property" + ) + raise ValidationError(error_message) + current_offers = current_property.offer_ids + if len(current_offers) > 0: + best_offer_price = max(offer.price for offer in current_offers) + feedback_buffer = [] + for new_offer in vals_list: + if new_offer["price"] < best_offer_price: + feedback_buffer.append(str(new_offer["price"])) + if len(feedback_buffer) > 0: + error_message = ( + f"The following offer prices are lower than the current best price ({best_offer_price}) and can't therefor not be added:\n - " + + "\n - ".join(feedback_buffer) + ) + raise ValidationError(error_message) + current_property.state = "offer_received" + return super().create(vals_list) diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py new file mode 100644 index 00000000000..91b57547718 --- /dev/null +++ b/estate/models/estate_property_tag.py @@ -0,0 +1,15 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo import fields, models + + +class EstatePropertyTag(models.Model): + _name = "estate.property.tag" + _description = "Property Tag" + _order = "name" + + _check_name_unique = models.Constraint( + "UNIQUE(name)", "The tag name must be unique." + ) + + name = fields.Char(required=True) + color = fields.Integer("Color") diff --git a/estate/models/estate_property_type.py b/estate/models/estate_property_type.py new file mode 100644 index 00000000000..e0636da89d2 --- /dev/null +++ b/estate/models/estate_property_type.py @@ -0,0 +1,27 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo import fields, models, api + + +class EstatePropertyType(models.Model): + _name = "estate.property.type" + _description = "Property Type" + _order = "name" + + _check_name_unique = models.Constraint( + "UNIQUE(name)", "The property type name must be unique." + ) + + name = fields.Char(required=True) + property_ids = fields.One2many( + "estate.property", "estate_type_id", string="Properties" + ) + offer_ids = fields.One2many("estate.property.offer", "property_type_id") + offer_count = fields.Integer(compute="_compute_offer_count") + sequence = fields.Integer( + "Sequence", default=1, help="Used to order stages. Lower is better." + ) + + @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..0386275971d --- /dev/null +++ b/estate/models/res_users.py @@ -0,0 +1,11 @@ +from odoo import fields, models + + +class ResUsers(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..e20ec4dd90b --- /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/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..7202f8b67a0 --- /dev/null +++ b/estate/tests/test_estate_property.py @@ -0,0 +1,42 @@ +from odoo.exceptions import ValidationError +from odoo.tests import TransactionCase +from odoo import Command + + +class TestEstateProperty(TransactionCase): + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.estate = cls.env['estate.property'].create({ + 'name': 'Super test estate', + 'expected_price': 100000.0, + 'state': 'new', + }) + cls.test_partner = cls.env['res.partner'].create({ + 'name': 'Maman ours', + }) + + def test_estate_best_price(self): + ''' + Ensure best price is correctly updated when an offer is received. + ''' + self.assertEqual(self.estate.best_price, 0.0) + self.estate.offer_ids = [Command.create({ + 'price': 125000.0, + 'partner_id': self.test_partner.id, + })] + self.assertEqual(self.estate.best_price, 125000.0) + + def test_accept_offer_south_facing_garden(self): + ''' + Ensure offers for estates with south-facing gardens can only be accepted if above expected + price. + ''' + self.estate.expected_price = 500000 + self.estate.offer_ids = [Command.create({ + 'price': 475000.0, + 'partner_id': self.test_partner.id, + })] + with self.assertRaises(ValidationError): + self.estate.offer_ids.accept_offer() diff --git a/estate/views/estate_menus.xml b/estate/views/estate_menus.xml new file mode 100644 index 00000000000..e1ef9d4b493 --- /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..77a8ce0d4d4 --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,26 @@ + + + + estate.property.offer.view.list + estate.property.offer + + + + + + + + + + + Property Offers + estate.property.offer + list,form + [('property_type_id', '=', active_id)] + + + Property view offer + estate.property.offer + list,form + + diff --git a/estate/views/estate_property_tag_views.xml b/estate/views/estate_property_tag_views.xml new file mode 100644 index 00000000000..edca2d9a42d --- /dev/null +++ b/estate/views/estate_property_tag_views.xml @@ -0,0 +1,43 @@ + + + + Property tags + estate.property.tag + list,form + + + estate.property.tag.view.list + estate.property.tag + + + + + + + + + estate.property.tag.view.form + estate.property.tag + +
+ +
+

+ +

+
+
+
+
+
+ + + estate.property.tag.search + estate.property.tag + + + + + + +
diff --git a/estate/views/estate_property_type_views.xml b/estate/views/estate_property_type_views.xml new file mode 100644 index 00000000000..08edefea3ac --- /dev/null +++ b/estate/views/estate_property_type_views.xml @@ -0,0 +1,66 @@ + + + + Property types + estate.property.type + list,form + + + estate.property.type.view.list + estate.property.type + + + + + + + + + + + + estate.property.type.view.form + estate.property.type + + +
+ +
+ +
+
+

+ +

+
+ + + + + + + + + + + +
+
+
+
+ + + 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..0f2dc92f727 --- /dev/null +++ b/estate/views/estate_property_views.xml @@ -0,0 +1,152 @@ + + + + Properties + estate.property + list,form,kanban + {'search_default_available': True} + + + estate.property.view.list + estate.property + + + + + + + + + + + + + + + + estate.property.view.form + estate.property + +
+
+
+ +
+

+ +

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +