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..a1df52a5f04 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,15 @@ +{ + 'name': 'Real Estate', + 'depends': ['base'], + 'author': 'Odoo S.A.', + 'license': 'LGPL-3', + 'data': [ + 'security/ir.model.access.csv', + 'views/estate_property_views.xml', + 'views/estate_property_offer_views.xml', + 'views/estate_property_tag_views.xml', + 'views/estate_property_type_views.xml', + 'views/estate_menus.xml', + 'views/res_user_views.xml', + ], +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..cfb3cd728e5 --- /dev/null +++ b/estate/models/__init__.py @@ -0,0 +1,5 @@ +from . import property +from . import property_offer +from . import property_tag +from . import property_type +from . import res_users diff --git a/estate/models/property.py b/estate/models/property.py new file mode 100644 index 00000000000..411ffa592e7 --- /dev/null +++ b/estate/models/property.py @@ -0,0 +1,118 @@ +from odoo import _, api, exceptions, fields, models +from odoo.tools.float_utils import float_compare, float_is_zero + + +class Property(models.Model): + _name = "estate.property" + _description = "Properties of our managed estates" + _order = "id desc" + + name = fields.Char(string='Title', required=True) + active = fields.Boolean(default=True) + state = fields.Selection(required=True, default='new', 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="Property Type") + salesman_id = fields.Many2one('res.users', string="Salesman", default=lambda self: self.env.user) + buyer_id = fields.Many2one('res.partner', string="Buyer", copy=False) + tag_ids = fields.Many2many("estate.property.tag") + offer_ids = fields.One2many("estate.property.offer", "property_id") + description = fields.Text(string='description') + postcode = fields.Char(string='postcode') + date_availability = fields.Date(string='Available from', default=lambda _: fields.Date.add(fields.Date.today(), months=3), copy=False) + expected_price = fields.Float(string='Expected price', required=True) + selling_price = fields.Float(string='selling price', readonly=True, copy=False) + bedrooms = fields.Integer(string='# bedrooms', default=2) + living_area = fields.Integer(string='living area size') + facades = fields.Integer(string='# facades') + garage = fields.Boolean(string='Has garage') + garden = fields.Boolean(string='Has garden') + garden_area = fields.Integer(string='garden area size') + garden_orientation = fields.Selection(string='garden orientation', + selection=[ + ('north', 'North'), + ('south', 'South'), + ('east', 'East'), + ('west', 'West'), + ]) + total_area = fields.Float(string="Total area", compute="_compute_total_area") + best_price = fields.Float(string="Best offer", compute="_compute_best_price") + + _check_positive_expected_price = models.Constraint( + 'CHECK (expected_price >= 0)', 'Expected price must be positive!') + _check_positive_selling_price = models.Constraint( + 'CHECK (selling_price >= 0)', 'Selling price must be positive!') + _check_positive_living_area = models.Constraint( + 'CHECK (living_area >= 0)', 'Living area must be positive!') + _check_positive_amounts = models.Constraint( + 'CHECK (facades >= 0)', 'Number of facades must be positive!') + _check_positive_garden_area = models.Constraint( + 'CHECK (garden_area >= 0)', 'Garden area must be positive!') + + @api.depends("living_area", "garden_area") + def _compute_total_area(self): + for record in self: + record.total_area = record.living_area + record.garden_area + + @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 = 0 + + @api.constrains('selling_price', 'expected_price') + def _check_selling_price(self): + for property in self: + if float_is_zero(property.selling_price, 2): + return + if float_compare(property.selling_price, 0.9 * property.expected_price, 2) == -1: + raise exceptions.ValidationError(_("The accepted price is less than 90% of the expected price!")) + + @api.onchange("garden") + def _onchange_garden(self): + for record in self: + record.garden_area = 10 if record.garden else 0 + record.garden_orientation = 'north' if record.garden else False + + @api.ondelete(at_uninstall=False) + def _unlink_if_draft(self): + for record in self: + if record.state not in ['new', 'cancelled']: + raise exceptions.ValidationError(_("Only properties in state New or Cancelled can be deleted!")) + + def action_sold(self): + for record in self: + if record.state == 'cancelled': + raise exceptions.UserError(_("Cancelled properties cannot be sold!")) + + record.state = "sold" + return True + + def action_cancelled(self): + for record in self: + if record.state == "sold": + raise exceptions.UserError(_("Sold properties cannot be cancelled!")) + + record.state = "cancelled" + return True + + def confirm_offer(self): + for property in self: + accepted_offer = property.offer_ids.filtered(lambda r: r.status == 'accepted') + accepted_offer.ensure_one() + property.selling_price = accepted_offer.price + property.buyer_id = accepted_offer.partner_id + property.state = 'offer_accepted' + # Refuse all other offers + (property.offer_ids - accepted_offer).action_cancel() + + def _set_offer_received(self): + self.ensure_one() + if self.state == 'new': + self.state = 'offer_received' diff --git a/estate/models/property_offer.py b/estate/models/property_offer.py new file mode 100644 index 00000000000..a7abfa4484a --- /dev/null +++ b/estate/models/property_offer.py @@ -0,0 +1,76 @@ +import datetime as dt +from math import floor + +from odoo import _, api, fields, models +from odoo.exceptions import UserError, ValidationError + + +class PropertyOffer(models.Model): + _name = "estate.property.offer" + _description = "Bids for a property" + _order = "price desc" + + price = fields.Float(string="Price", required=True) + 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(string="Validity of offer", default=7) + date_deadline = fields.Date(string="Offer expiry", compute="_compute_date_deadline", inverse="_inverse_date_deadline") + + _check_positive_price = models.Constraint('CHECK(price >= 0)', 'Price has to be positive') + + @api.depends("create_date", "validity") + def _compute_date_deadline(self): + for record in self: + record.date_deadline = fields.Date.add(record.create_date or fields.Datetime.now(), days=record.validity) + + @api.onchange('date_deadline') + def _inverse_date_deadline(self): + for record in self: + if record.date_deadline: + raw_difference = record.date_deadline - (record.create_date or fields.Datetime.now()).date() + difference = raw_difference.total_seconds() + if difference > 0: + record.validity = floor(difference / dt.timedelta(days=1).total_seconds()) + + @api.constrains('status') + def _check_maximum_one_offer_accepted(self): + accepted_offers = self.filtered(lambda r: r.status == "accepted") + properties = accepted_offers.mapped('property_id') + for property in properties: + peer_offers = property.offer_ids + accepted_peers = peer_offers.filtered(lambda r: r.status == 'accepted') + if len(accepted_peers) > 1: + raise ValidationError(_("A single offer can be accepted at a time!")) + + @api.model_create_multi + def create(self, vals_list): + property_ids = self.env["estate.property"].browse(vals["property_id"] for vals in vals_list) + # Precondition: price and property_id are required fields + for vals in vals_list: + property = property_ids.filtered(lambda p: p.id == vals["property_id"]) + property.ensure_one() + # Hook-approach (for composability) + property._set_offer_received() + if property.best_price and property.best_price > vals['price']: + raise UserError(_("New offer price must be higher than those of pre-existing offers!")) + + return super().create(vals_list) + + def action_confirm(self): + for record in self: + if record.status == "refused": + raise UserError(_("Offer is already refused!")) + + record.status = 'accepted' + record.property_id.confirm_offer() + return True + + def action_cancel(self): + for record in self: + if record.status == "accepted": + raise UserError(_("Offer is already accepted!")) + + record.status = "refused" + return True diff --git a/estate/models/property_tag.py b/estate/models/property_tag.py new file mode 100644 index 00000000000..f83a3c78cd2 --- /dev/null +++ b/estate/models/property_tag.py @@ -0,0 +1,12 @@ +from odoo import fields, models + + +class PropertyTag(models.Model): + _name = "estate.property.tag" + _description = "Tag assigned to property" + _order = "name" + + name = fields.Char(string='Name', required=True) + color = fields.Integer(string='Color') + + _name_idx = models.UniqueIndex('(name)', 'Another record already exists with the same name!') diff --git a/estate/models/property_type.py b/estate/models/property_type.py new file mode 100644 index 00000000000..e1fb5cd94ef --- /dev/null +++ b/estate/models/property_type.py @@ -0,0 +1,20 @@ +from odoo import api, fields, models + + +class PropertyType(models.Model): + _name = "estate.property.type" + _description = "Type of property" + _order = "name" + + name = fields.Char(string='Name', required=True) + sequence = fields.Integer('Sequence', default=1) + property_ids = fields.One2many("estate.property", "property_type_id") + offer_ids = fields.One2many("estate.property.offer", "property_type_id") + offer_count = fields.Integer(compute="_compute_offer_count") + + _name_idx = models.UniqueIndex('(name)', 'Another record already exists with the same name!') + + @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..7fe25d5784d --- /dev/null +++ b/estate/models/res_users.py @@ -0,0 +1,7 @@ +from odoo import fields, models + + +class ResUsers(models.Model): + _inherit = 'res.users' + + property_ids = fields.One2many("estate.property", "salesman_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..03ac00195c6 --- /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_model,access.estate.property.model,estate.model_estate_property,base.group_user,1,1,1,1 +access_estate_property_type_model,access.estate.property.type.model,estate.model_estate_property_type,base.group_user,1,1,1,1 +access_estate_property_tag_model,access.estate.property.tag.model,estate.model_estate_property_tag,base.group_user,1,1,1,1 +access_estate_property_offer_model,access.estate.property.offer.model,estate.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..9e035007a3d --- /dev/null +++ b/estate/views/estate_menus.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/estate/views/estate_property_offer_views.xml b/estate/views/estate_property_offer_views.xml new file mode 100644 index 00000000000..444f638adda --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,46 @@ + + + + Property Offers + estate.property.offer + list,form + [('property_type_id', '=', active_id)] + + + + estate.property.offer.list + estate.property.offer + + + + + + + + +
+
+
+

+ +

+
+ + + + + + + + + + + + + + + +
+
+
diff --git a/estate/views/estate_property_views.xml b/estate/views/estate_property_views.xml new file mode 100644 index 00000000000..c3434c4696a --- /dev/null +++ b/estate/views/estate_property_views.xml @@ -0,0 +1,151 @@ + + + + Properties + estate.property + list,form,kanban + {'search_default_available': True} + + + + estate.property.list + estate.property + + + + + + + + + + + + + + + + estate.property.kanban + estate.property + + + + + + +
+ +
+ Expected price: + +
+ +
+ Best offer: + +
+
+ +
+ Selling price: + +
+
+
+ +
+
+
+
+
+
+
+ + + estate.property.form + estate.property + +
+
+
+ +
+
+
+

+ +

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+ + + estate.property.search + estate.property + + + + + + + + + + + + + + + + + +
diff --git a/estate/views/res_user_views.xml b/estate/views/res_user_views.xml new file mode 100644 index 00000000000..f3af356e081 --- /dev/null +++ b/estate/views/res_user_views.xml @@ -0,0 +1,15 @@ + + + + estate.res.user.form + res.users + + + + + + + + + + diff --git a/estate_account/__init__.py b/estate_account/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/estate_account/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/estate_account/__manifest__.py b/estate_account/__manifest__.py new file mode 100644 index 00000000000..5fd8812d66d --- /dev/null +++ b/estate_account/__manifest__.py @@ -0,0 +1,7 @@ +{ + 'name': "Real Estate Accounting", + 'depends': ['estate', 'account'], + 'data': [], + 'author': 'bepro', + 'license': 'LGPL-3', +} diff --git a/estate_account/models/__init__.py b/estate_account/models/__init__.py new file mode 100644 index 00000000000..5e1963c9d2f --- /dev/null +++ b/estate_account/models/__init__.py @@ -0,0 +1 @@ +from . import estate_property diff --git a/estate_account/models/estate_property.py b/estate_account/models/estate_property.py new file mode 100644 index 00000000000..99cfe78920e --- /dev/null +++ b/estate_account/models/estate_property.py @@ -0,0 +1,37 @@ +from odoo import Command, models + + +class EstateProperty(models.Model): + _name = 'estate.property' + _inherit = "estate.property" + + def action_sold(self): + self._create_invoices() + return super().action_sold() + + def _prepare_invoice(self): + self.ensure_one() + return { + 'partner_id': self.buyer_id.id, + 'move_type': 'out_invoice', + # 'journal_id':, + 'invoice_line_ids': [ + Command.create({ + 'name': '6% of selling price', + 'quantity': 1, + 'price_unit': 0.06 * self.selling_price, + }), + Command.create({ + 'name': 'Administrative fee', + 'quantity': 1, + 'price_unit': 100.00, + }), + ], + } + + def _create_invoices(self): + self.env['account.move'].check_access('create') + + for property in self: + invoice_values = property._prepare_invoice() + self.env['account.move'].with_context(default_move_type='out_invoice').create(invoice_values)