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..90ff2bf1cd4 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,15 @@ +# noinspection statement-effect +{ + 'name': 'estate', + 'depends': ['base'], + 'data': [ + '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' + ], + 'author': "Odoo S.A.", + 'license': 'AGPL-3' +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..9baa1323f26 --- /dev/null +++ b/estate/models/__init__.py @@ -0,0 +1,4 @@ +from .property import Property +from .property_type import PropertyType +from .property_tag import PropertyTag +from .property_offer import PropertyOffer diff --git a/estate/models/property.py b/estate/models/property.py new file mode 100644 index 00000000000..72485a4e236 --- /dev/null +++ b/estate/models/property.py @@ -0,0 +1,106 @@ +from odoo import models, fields, api +from odoo.exceptions import UserError, ValidationError +from odoo.tools.float_utils import float_compare, float_is_zero + + +class Property(models.Model): + _name = 'estate.property' + _description = "Real estate property" + + name = fields.Char(string="Title", required=True) + description = fields.Text() + postcode = fields.Char() + date_availability = fields.Date(copy=False, default=lambda x: 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(string="Living Area (sqm)") + faces = fields.Integer(string="Facades") + has_garage = fields.Boolean(string="Garage") + has_garden = fields.Boolean(string="Garden") + garden_area = fields.Integer(string="Garden Area (sqm)") + garden_orientation = fields.Selection( + selection=[ + ('north', "North"), + ('east', "East"), + ('south', "South"), + ('west', "West") + ] + ) + active = fields.Boolean(default=True) + state = fields.Selection( + default='new', + required=True, + copy=False, + selection=[ + ('new', "New"), + ('offer_received', "Offer Received"), + ('offer_accepted', "Offer Accepted"), + ('sold', "Sold"), + ('cancelled', "Cancelled") + ] + ) + property_type_id = fields.Many2one("estate.property.type", string="Type") + buyer_id = fields.Many2one("res.partner", string="Buyer", copy=False) + salesperson_id = fields.Many2one("res.users", string="Salesman", default=lambda self: self.env.user) + tag_ids = fields.Many2many("estate.property.tag") + offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers") + + total_area = fields.Float(compute='_compute_total_area') + + @api.depends('garden_area', 'living_area') + def _compute_total_area(self): + for record in self: + record.total_area = record.garden_area + record.living_area + + best_price = fields.Float(string="Best Offer", compute="_compute_best_price") + + @api.depends("offer_ids.price") + 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 = None + + @api.onchange('has_garden') + def _onchange_has_garden(self): + if self.has_garden: + self.garden_area = 10 + self.garden_orientation = 'north' + else: + self.garden_area = 0 + self.garden_orientation = '' + + def action_do_sold(self): + for record in self: + if record.state == 'cancelled': + raise UserError('Canceled property cannot be sold') + else: + record.state = 'sold' + return True + + def action_do_cancel(self): + for record in self: + if record.state == 'sold': + raise UserError('Sold property cannot be canceled') + else: + record.state = 'cancelled' + + # Constraints + _check_expected_price = models.Constraint( + 'CHECK(expected_price > 0)', + 'Expected price must be strictly positive', + ) + _check_selling_price = models.Constraint( + 'CHECK(selling_price >= 0)', + 'Expected price must be positive', + ) + + @api.constrains('expected_price', 'selling_price') + def _check_expected_price_selling_price(self): + for record in self: + if not float_is_zero(record.selling_price, precision_digits=2): + if float_compare(record.expected_price * 0.90, record.selling_price, precision_digits=2) == 1: + raise ValidationError("Selling price must be above 90% of expected price") diff --git a/estate/models/property_offer.py b/estate/models/property_offer.py new file mode 100644 index 00000000000..2b7da30d866 --- /dev/null +++ b/estate/models/property_offer.py @@ -0,0 +1,51 @@ +from odoo import models, fields, api, exceptions + + +class PropertyOffer(models.Model): + _name = 'estate.property.offer' + _description = "Property Offer" + + price = fields.Float() + status = fields.Selection( + copy=False, + selection=[ + ('accepted', "Accepted"), + ('refused', "Refused"), + ] + ) + partner_id = fields.Many2one('res.partner', string="Partner", required=True) + property_id = fields.Many2one('estate.property', required=True) + validity = fields.Integer(default=7) + date_deadline = fields.Date(compute='_compute_deadline', inverse="_inverse_deadline") + + @api.depends("validity", "create_date") + def _compute_deadline(self): + for record in self: + base_date = record.create_date or fields.Date.today() + record.date_deadline = fields.Date.add(base_date, days=record.validity) + + def _inverse_deadline(self): + for record in self: + base_date = fields.Date.to_date(record.create_date) if record.create_date else fields.Date.today() + record.validity = (record.date_deadline - base_date).days + + def action_accept(self): + for record in self: + if record.property_id.state in ('offer_accepted', 'sold', 'cancelled'): + err_msg = f"Cannot accept offer on property that is {record.property_id.state}" + raise exceptions.UserError(err_msg) + record.status = record.status = 'accepted' + record.property_id.buyer_id = self.partner_id + record.property_id.selling_price = record.price + record.property_id.state = 'offer_accepted' + return True + + def action_refuse(self): + for record in self: + record.status = 'refused' + return True + + _check_price = models.Constraint( + 'CHECK(price > 0)', + '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..7667493d3fc --- /dev/null +++ b/estate/models/property_tag.py @@ -0,0 +1,12 @@ +from odoo import models, fields + + +class PropertyTag(models.Model): + _name = 'estate.property.tag' + _description = "Property Tag" + + name = fields.Char(string="Name", required=True) + _name_uniq = models.Constraint( + 'UNIQUE (name)', + "The name of the tag must be unique!", + ) diff --git a/estate/models/property_type.py b/estate/models/property_type.py new file mode 100644 index 00000000000..9bcdfffbf8a --- /dev/null +++ b/estate/models/property_type.py @@ -0,0 +1,12 @@ +from odoo import models, fields + + +class PropertyType(models.Model): + _name = 'estate.property.type' + _description = "Property Type" + + name = fields.Char(string="Name", required=True) + _name_uniq = models.Constraint( + 'UNIQUE (name)', + "The name of the type must be unique!", + ) \ No newline at end of file diff --git a/estate/security/ir.model.access.csv b/estate/security/ir.model.access.csv new file mode 100644 index 00000000000..be13be70d5f --- /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_user,access_estate_property,model_estate_property,base.group_user,1,1,1,1 +access_estate_property_type_user,access_estate_property_type,model_estate_property_type,base.group_user,1,1,1,1 +access_estate_property_tag_user,access_estate_property_tag,model_estate_property_tag,base.group_user,1,1,1,1 +access_estate_property_offer_user,access_estate_property_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..d2a755252a6 --- /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..d51b13c73e4 --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,35 @@ + + + + estate.property.offer.list + estate.property.offer + + + + + + +