diff --git a/.gitignore b/.gitignore index b6e47617de1..fbc0c865e38 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +# Not functionnal +estate/security/security.xml 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..12a16cd1fb7 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,17 @@ +{ + 'name': 'Real estate', + 'author': 'anden', + 'license': 'LGPL-3', + 'depends': ['base'], + 'category': 'Real Estate/Brokerage', + 'application': True, + 'data': [ + # 'security/security.xml', + 'security/ir.model.access.csv', + 'views/estate_property_views.xml', + 'views/estate_property_type_views.xml', + 'views/estate_property_tag_views.xml', + 'views/estate_property_offer_views.xml', + 'views/estate_menus.xml', + ] +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..9d31a3a6bcb --- /dev/null +++ b/estate/models/__init__.py @@ -0,0 +1,4 @@ +from . import property +from . import property_type +from . import property_tag +from . import property_offer diff --git a/estate/models/property.py b/estate/models/property.py new file mode 100644 index 00000000000..ec8c333ca31 --- /dev/null +++ b/estate/models/property.py @@ -0,0 +1,99 @@ +from odoo import api, fields, models, exceptions +from odoo.tools.float_utils import float_compare, float_is_zero + + +class EstateProperty(models.Model): + _name = 'estate_property' + _description = 'estate property' + _order = "id desc" + + name = fields.Char(string='Title', required=True) + description = fields.Text() + postcode = fields.Char() + date_availability = fields.Date(string='Available From', 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(string='Living Area (sqm)') + facades = fields.Integer() + garage = fields.Boolean() + garden = fields.Boolean() + garden_area = fields.Integer(string='Garden Area (sqm)') + garden_orientation = fields.Selection(selection=[('north', 'North'), ('south', 'South'), ('east', 'East'), ('west', 'West')]) + active = fields.Boolean(default=True) + state = fields.Selection( + string='Status', + selection=[ + ('new', 'New'), + ('offer received', 'Offer Received'), + ('offer accepted', 'Offer Accepted'), + ('sold', 'Sold'), + ('cancelled', 'Cancelled') + ], + required=True, + copy=False, + default='new', + ) + property_type_id = fields.Many2one('estate_property_type', string='Property Type') + salesperson = fields.Many2one('res.users', string='Salesman', default=lambda self: self.env.user) + buyer = fields.Many2one('res.partner', copy=False) + property_tag_id = fields.Many2many('estate_property_tag', string='Property Tag') + property_offer_id = fields.One2many('estate_property_offer', 'property_id', string='Property Offer') + total_area = fields.Float(string='Total Area (sqm)', compute='_compute_area') + best_price = fields.Float(string='Best offer', compute='_compute_best_price') + + @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('property_offer_id.price') + def _compute_best_price(self): + for record in self: + record.best_price = max(record.property_offer_id.mapped('price'), default=False) + + @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 = False + + def action_property_sold(self): + if self.state == 'cancelled': + raise exceptions.UserError('Cancelled properties cannot be sold') + else: + self.state = 'sold' + + def action_property_cancel(self): + if self.state == 'sold': + raise exceptions.UserError('Sold properties cannot be cancelled') + else: + self.state = 'cancelled' + + _check_expected_price = models.Constraint( + 'CHECK(0 < expected_price)', + 'A property expected price must be strictly positive') + + _check_selling_price = models.Constraint( + 'CHECK(0 <= selling_price)', + 'A property selling price must be positive') + + @api.constrains('selling_price', 'expected_price') + def _check_selling_price(self): + for record in self: + if not float_is_zero(record.selling_price, 2): + if float_compare(record.selling_price, 0.9 * record.expected_price, 2) == -1: + raise exceptions.ValidationError("The selling price cannot be lower than 90% of the expected price.") + + @api.onchange('property_offer_id') + def _onchange_property_offer_id(self): + for record in self: + if len(record.property_offer_id) > 0: + if record.state == 'new': + record.state = 'offer received' + elif record.state != 'cancelled': + record.state = 'new' diff --git a/estate/models/property_offer.py b/estate/models/property_offer.py new file mode 100644 index 00000000000..1467775f2cc --- /dev/null +++ b/estate/models/property_offer.py @@ -0,0 +1,54 @@ +from odoo import api, fields, models, exceptions + + +class EstatePropertyOffer(models.Model): + _name = 'estate_property_offer' + _description = 'estate property offer' + _order = "price desc" + + price = fields.Float() + status = fields.Selection(selection=[('accepted', 'Accepted'), ('refused', 'Refused')], copy=False, readonly=True) + partner_id = fields.Many2one('res.partner', required=True, string='Partner') + property_id = fields.Many2one('estate_property', required=True) + validity = fields.Integer(string='Validity (days)', default=7) + date_deadline = fields.Date(string='Deadline', compute='_compute_deadline', inverse='_inverse_deadline') + + @api.depends('create_date', 'validity') + def _compute_deadline(self): + for record in self: + if record.create_date: + record.date_deadline = fields.Date.add(record.create_date.date(), days=record.validity) + else: + record.date_deadline = fields.Date.add(fields.Date.today(), days=record.validity) # If no create_date we take the date of today + + def _inverse_deadline(self): + for record in self: + if record.create_date: + record.validity = (record.date_deadline - record.create_date.date()).days + else: + record.validity = (record.date_deadline - fields.Date.today()).days # If no create_date we take the date of today + + def action_status_accepted(self): + + if len(self) > 1: + raise exceptions.UserError('Only one offer can be accepted') + + if self.status != 'accepted' and 'accepted' in self.mapped('property_id.property_offer_id.status'): + raise exceptions.UserError('Another offer is already accepted') + + self.status = 'accepted' + self.property_id.selling_price = self.price + self.property_id.buyer = self.partner_id + self.property_id.state = 'offer accepted' + + def action_status_refused(self): + for record in self: + if record.status == 'accepted': + record.property_id.selling_price = False + record.property_id.buyer = False + record.property_id.state = 'offer received' + record.status = 'refused' + + _check_offer_price = models.Constraint( + 'CHECK(0 < price)', + 'An offer price must be strictly positive') diff --git a/estate/models/property_tag.py b/estate/models/property_tag.py new file mode 100644 index 00000000000..1574df8ecb3 --- /dev/null +++ b/estate/models/property_tag.py @@ -0,0 +1,15 @@ +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("Color Index") + + _name_uniq = models.Constraint( + 'unique (name)', + "A property tag name must be unique", + ) diff --git a/estate/models/property_type.py b/estate/models/property_type.py new file mode 100644 index 00000000000..3c1840118e5 --- /dev/null +++ b/estate/models/property_type.py @@ -0,0 +1,16 @@ +from odoo import fields, models + + +class EstatePropertyType(models.Model): + _name = 'estate_property_type' + _description = 'estate property type' + _order = "sequence, name" + + name = fields.Char(string='Type', required=True) + sequence = fields.Integer('Sequence', default=1) + property_ids = fields.One2many('estate_property', 'property_type_id') + + _name_uniq = models.Constraint( + 'unique (name)', + "A property type name must be unique", + ) diff --git a/estate/security/ir.model.access.csv b/estate/security/ir.model.access.csv new file mode 100644 index 00000000000..ca416c8057c --- /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_ir_group_user,ir_group_user,model_estate_property,base.group_user,1,1,1,1 +access_ir_group_user_type,ir_group_user_type,model_estate_property_type,base.group_user,1,1,1,1 +access_ir_group_user_tag,ir_group_user_tag,model_estate_property_tag,base.group_user,1,1,1,1 +access_ir_group_user_offer,ir_group_user_offer,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..28b1018038b --- /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..2ac8cad15ed --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,37 @@ + + + + + estate_property_offer_list + estate_property_offer + + + + + + +