diff --git a/.gitignore b/.gitignore index b6e47617de1..61a0ca00289 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + + +.vscode/ 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..2bc44e1e3ff --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,19 @@ +{ + 'name': 'Estate', + 'version': '1.0', + 'depends': ['base'], + 'author': 'Odoo S.A.', + 'application': True, + 'installable': True, + 'category': '', + 'description': '', + 'license': 'LGPL-3', + '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/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..673cc3c764d --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,108 @@ +from odoo import api, fields, models +from odoo.exceptions import UserError, ValidationError +from odoo.tools.float_utils import float_compare + + +class EstateProperty(models.Model): + _name = "estate.property" + _description = "Estate Property" + _order = "id desc" + _check_expected_price = models.Constraint( + 'CHECK(expected_price > 0)', + 'The expected price should be stricly positive' + ) + _check_selling_price = models.Constraint( + 'CHECK(selling_price >= 0)', + 'The selling price should be positive' + ) + + 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), string="Available From") + 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='Orientation', + selection=[ + ('north', 'North'), + ('west', 'West'), + ('south', 'South'), + ('east', 'East') + ], + help="Choose the appropriate orientation of the garden" + ) + active = fields.Boolean(default=True) + state = fields.Selection( + string="Estate status", + selection=[('new', 'New'), ('offer_received', 'Offer Received'), ('offer_accepted', 'Offer Accepted'), ('sold', 'Sold'), ('cancelled', 'Cancelled')], + help='This field explain the estate status.', + required=True, + copy=False, + default='new' + ) + property_type_id = fields.Many2one("estate.property.type", string="Type") + seller_id = fields.Many2one("res.users", string="Salesman", default=lambda self: self.env.user, domain="[('type', '=', 'internal')]") + buyer_id = fields.Many2one("res.partner", string="Buyer", domain="[('type', '=', 'portal')]") + tag_ids = fields.Many2many("estate.property.tag") + offer_ids = fields.One2many("estate.property.offer", "property_id") + total_area = fields.Float(compute="_compute_total_area", string="Total Area (sqm)") + best_price = fields.Float(compute="_compute_best_offer", string="Best Offer") + + @api.depends("garden_area", "living_area") + def _compute_total_area(self): + for record in self: + record.total_area = record.garden_area + record.living_area + + @api.depends("offer_ids") + def _compute_best_offer(self): + for record in self: + record.best_price = max(record.offer_ids.mapped("price")) if len(record.offer_ids) > 0 else 0.0 + + @api.onchange("garden") + def _onchange_garden(self): + for record in self: + if not record.garden: + record.garden_area = 0 + record.garden_orientation = '' + else: + record.garden_area = 10 + record.garden_orientation = 'north' + + def estate_property_action_sold(self): + self.__estate_property_action_sold_cancel('sold', "A cancelled property cannot be sold!", "This property is already sold!") + + def estate_property_action_cancel(self): + self.__estate_property_action_sold_cancel('cancelled', "A sold property cannot be cancelled!", "This property is already cancelled!") + + def __estate_property_action_sold_cancel(self, target, error_message, error_same_target_message): + for record in self: + # exclude target from next condition + if record.state == target: + raise UserError(error_same_target_message) + # easiest way to exclude the other state + elif record.state in ('sold', 'cancelled'): + raise UserError(error_message) + # the property can be sold/cancelled + else: + record.state = target + + @api.constrains('selling_price') + def _check_price_constraint(self): + for record in self: + if record.selling_price and float_compare(record.selling_price, record.expected_price * 0.9, precision_digits=4) < 0: + raise ValidationError("The price cannot be les than 90% of the expected price") + + @api.onchange("offer_ids") + def _onchange_offer_ids(self): + for record in self: + if len(record.offer_ids) > 0: + record.state = "offer_received" + else: + record.state = "new" diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py new file mode 100644 index 00000000000..c5a65b2fdfe --- /dev/null +++ b/estate/models/estate_property_offer.py @@ -0,0 +1,58 @@ +from odoo import api, fields, models + + +class EstatePropertyOffer(models.Model): + _name = "estate.property.offer" + _description = "Estate Property Offer" + _order = "price desc" + _check_offer_price = models.Constraint( + 'CHECK(price > 0)', + 'The offer price should be stricly positive' + ) + + price = fields.Float() + status = fields.Selection( + string="Status", + copy=False, + selection=[ + ('accepted', 'Accepted'), + ('refused', 'Refused') + ] + ) + 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_date_deadline", inverse="_inverse_date_deadline") + + @api.depends("validity") + def _compute_date_deadline(self): + for record in self: + record.date_deadline = fields.Date.add(fields.Date.today(), days=record.validity) + + @api.onchange("date_deadline") + def _inverse_date_deadline(self): + for record in self: + record.validity = (record.date_deadline - fields.Date.today()).days + + def action_accept_offer(self): + for record in self: + if record.status == 'accepted': + return False + record.property_id.selling_price = record.price + record.property_id.buyer_id = record.partner_id + for offer in record.property_id.offer_ids: + offer.status = "refused" + record.status = 'accepted' + record.property_id.state = 'offer_accepted' + return True + + def action_refuse_offer(self): + for record in self: + record.status = "refused" + return True + + @api.ondelete(at_uninstall=False) + def _unlink_if_deleted(self): + for record in self: + record.property_id.selling_price = 0.0 diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py new file mode 100644 index 00000000000..59cbe8b8f99 --- /dev/null +++ b/estate/models/estate_property_tag.py @@ -0,0 +1,10 @@ +from odoo import fields, models + + +class EstatePropertyTag(models.Model): + _name = "estate.property.tag" + _description = "Estate Property Tag" + _order = "name" + + name = fields.Char(required=True) + color = fields.Integer() diff --git a/estate/models/estate_property_type.py b/estate/models/estate_property_type.py new file mode 100644 index 00000000000..977fba2d1be --- /dev/null +++ b/estate/models/estate_property_type.py @@ -0,0 +1,18 @@ +from odoo import api, fields, models + + +class EstatePropertyType(models.Model): + _name = "estate.property.type" + _description = "Estate Property Types" + _order = "sequence, name" + + name = fields.Char(required=True) + property_ids = fields.One2many("estate.property", "property_type_id") + sequence = fields.Integer('Sequence', default=1, help="Used to order estate property types.") + offer_ids = fields.One2many("estate.property.offer", "property_type_id") + offer_count = fields.Integer(compute="_compute_offer_count") + + @api.depends('offer_ids') + def _compute_offer_count(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..d375e4a2ac5 --- /dev/null +++ b/estate/security/ir.model.access.csv @@ -0,0 +1,6 @@ +id,name,model_id/id,group_id/id,perm_read,perm_write,perm_create,perm_unlink +access_estate_model,estate_property_users,model_estate_property,base.group_user,1,1,1,1 +access_estate_model_type,estate_property_type_users,model_estate_property_type,base.group_user,1,1,1,1 +portal_access_estate_model,estate_property_portal,model_estate_property,base.group_portal,1,0,0,0 +access_estate_model_tag,estate_property_tag_users,model_estate_property_tag,base.group_user,1,1,1,1 +access_estate_model_offer,estate_property_offer_users,model_estate_property_offer,base.group_user,1,1,1,1 diff --git a/estate/views/estate_menus.xml b/estate/views/estate_menus.xml new file mode 100644 index 00000000000..ba6a2f8f00a --- /dev/null +++ b/estate/views/estate_menus.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/estate/views/estate_property_offer_views.xml b/estate/views/estate_property_offer_views.xml new file mode 100644 index 00000000000..cef3c4baa73 --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,24 @@ + + + + estate.property.offer.list + estate.property.offer + + + + + + + + + + + + + + + + + + + + + + + + + + + + estate.property.type.list + estate.property.type + + + + + + + + + + Property Types + estate.property.type + list,form + + diff --git a/estate/views/estate_property_views.xml b/estate/views/estate_property_views.xml new file mode 100644 index 00000000000..6036828434b --- /dev/null +++ b/estate/views/estate_property_views.xml @@ -0,0 +1,107 @@ + + + + estate.property.list + estate.property + + + + + + + + + + + + + + + estate.property.form + estate.property + +
+
+
+ + +

+
+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+ + + estate.property.search + estate.property + + + + + + + + + + + + + + + + + + + Properties + estate.property + list,form + {'search_default_available':1} + +