diff --git a/real_estate/__init__.py b/real_estate/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/real_estate/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/real_estate/__manifest__.py b/real_estate/__manifest__.py new file mode 100644 index 00000000000..388c00baab4 --- /dev/null +++ b/real_estate/__manifest__.py @@ -0,0 +1,21 @@ +{ + "name": "Real Estate Application", + "summary": "This is a custom real estate application for understanding Odoo", + "description": "This is a custom real estate application for understanding Odoo", + "author": "Odoo S.A.", + "website": "https://www.odoo.com", + "category": "Tutorials", + "version": "0.1", + "application": True, + "data": [ + "data/res_estate_data.xml", + "security/ir.model.access.csv", + "views/estate_property_templates.xml", + "views/estate_property_offers_templates.xml", + "views/estate_property_type_templates.xml", + "views/estate_property_tags_templates.xml", + "views/estate_menu.xml", + ], + "assets": {}, + "license": "AGPL-3", +} diff --git a/real_estate/data/res_estate_data.xml b/real_estate/data/res_estate_data.xml new file mode 100644 index 00000000000..e785f0f1312 --- /dev/null +++ b/real_estate/data/res_estate_data.xml @@ -0,0 +1,103 @@ + + + + + House + + + Apartment + + + Castle + + + Property 1 + Description 1 + 1000 + 10000 + 0 + True + 2 + 2 + 2 + True + True + 22 + new + north + + + + + + + + + + + + 20000.00 + + + + 10000.00 + + + + + + Property 2 + Description 2 + 2000 + 10000 + 0 + True + 2 + 2 + 2 + True + True + 22 + new + north + + + + + + + + + 20000.00 + + + + 10000.00 + + + + + + Property 3 + Description 3 + 1040 + 10000 + 0 + True + 2 + 2 + 2 + True + True + 22 + new + north + + + + + + + + + diff --git a/real_estate/models/__init__.py b/real_estate/models/__init__.py new file mode 100644 index 00000000000..3ed85a6943c --- /dev/null +++ b/real_estate/models/__init__.py @@ -0,0 +1,4 @@ +from . import estate_property +from . import estate_property_type +from . import estate_property_tags +from . import estate_property_offers diff --git a/real_estate/models/estate_property.py b/real_estate/models/estate_property.py new file mode 100644 index 00000000000..503d31bfc12 --- /dev/null +++ b/real_estate/models/estate_property.py @@ -0,0 +1,116 @@ +from datetime import date + +from dateutil.relativedelta import relativedelta +from odoo import api, fields, models +from odoo.exceptions import UserError, ValidationError +from odoo.tools import float_compare, float_is_zero + + +class Estate(models.Model): + _name = "estate.property" + _description = "Real_Estate_Property" + _order = "id desc" + + name = fields.Char(string="Name", required=True) + description = fields.Text(string="Description") + postcode = fields.Char(string="Postcode") + date_availability = fields.Date( + string="Date Availability", + default=lambda self: date.today() + relativedelta(months=3), + copy=False, + ) + expected_price = fields.Float(string="Expected Price", required=True) + selling_price = fields.Float(string="Selling Price", readonly=True, copy=False) + active = fields.Boolean(string="Active", default=True) + bedrooms = fields.Integer(string="Bedrooms", default=2) + living_area = fields.Integer(string="Living Area") + facades = fields.Integer(string="Facades") + garage = fields.Boolean(string="Garage") + garden = fields.Boolean(string="Garden") + garden_area = fields.Integer(string="Garden Area") + state = fields.Selection( + string="State", + selection=[ + ('new', "New"), + ('offered', "Offer Received"), + ('accepted', "Offer Accepted"), + ('sold', "Sold"), + ('cancelled', "Cancelled"), + ], + required=True, + copy=False, + ) + garden_orientation = fields.Selection( + [ + ('north', "North"), + ('south', "South"), + ('east', "East"), + ('west', "West"), + ] + ) + sales_person = fields.Many2one( + 'res.users', + string="Sales Person", + copy=False, + default=lambda self: self.env.user, + ) + buyer = fields.Many2one('res.partner', string="Buyer") + property_type = fields.Many2one( + comodel_name="estate.property.type", + string="Property Type", + ) + tags = fields.Many2many('estate.property.tags', string="Tags") + offer_ids = fields.One2many('estate.property.offers', 'property_id', string="Offers") + total_area = fields.Integer(compute='_compute_total_area') + best_offer = fields.Float(compute='_compute_best_offer') + + @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') + def _compute_best_offer(self): + for record in self: + offers = record.offer_ids.mapped('price') + if offers: + record.best_offer = max(offers) + else: + record.best_offer = 0.0 + + @api.onchange('garden') + def _onchange_garden(self): + if self.garden: + self.garden_area = 10 + self.garden_orientation = 'north' + else: + self.garden_area = 0 + self.garden_orientation = 0 + + def action_set_sold(self): + for record in self: + if record.state == 'cancelled': + raise UserError("This property cannot be sold.") + record.state = 'sold' + return True + + def action_set_cancel(self): + for record in self: + if record.state == 'sold': + raise UserError("This property is sold.") + record.state = 'cancelled' + return True + + @api.constrains('expected_price') + def _check_expected_price(self): + for record in self: + if record.expected_price <= 0: + raise ValidationError("Expected price cannot be negative.") + + @api.constrains('selling_price', 'expected_price') + def _check_selling_price(self): + for record in self: + if record.selling_price < 0: + raise ValidationError("Selling price cannot be negative.") + if not float_is_zero(record.selling_price, precision_digits=2) and float_compare(record.selling_price, record.expected_price * 0.9, precision_digits=2) == -1: + raise ValidationError("The selling price cannot be lower than 90% of the expected price!") diff --git a/real_estate/models/estate_property_offers.py b/real_estate/models/estate_property_offers.py new file mode 100644 index 00000000000..c13c8954da4 --- /dev/null +++ b/real_estate/models/estate_property_offers.py @@ -0,0 +1,49 @@ +from datetime import date + +from dateutil.relativedelta import relativedelta +from odoo import api, fields, models +from odoo.exceptions import ValidationError + + +class EstatePropertyOffers(models.Model): + _name = "estate.property.offers" + _description = "Real Estate Property Offers" + _order = "price desc" + + price = fields.Float(string="Price") + status = fields.Selection( + string="Status", + selection=[('accepted', "Accepted"), ('rejected', "Rejected")], + copy=False, + readonly=True, + ) + partner_id = fields.Many2one(comodel_name='res.partner', required=True) + property_id = fields.Many2one(comodel_name='estate.property', required=True) + property_type_id = fields.Many2one(related='property_id.property_type', store=True) + validity = fields.Integer(string="Validity", default=7) + date_deadline = fields.Date(compute='_compute_date_deadline') + + @api.depends('create_date', 'validity') + def _compute_date_deadline(self): + for record in self: + start_date = record.create_date if record.create_date else date.today() + record.date_deadline = start_date + relativedelta(days=record.validity) + + def action_accept_offer(self): + for record in self: + record.status = 'accepted' + record.property_id.state = 'accepted' + record.property_id.buyer = record.partner_id + record.property_id.selling_price = record.price + return True + + def action_reject_offer(self): + for record in self: + record.status = 'rejected' + return True + + @api.constrains('price') + def _check_price(self): + for record in self: + if record.price <= 0: + raise ValidationError("Offer price cannot be negative.") diff --git a/real_estate/models/estate_property_tags.py b/real_estate/models/estate_property_tags.py new file mode 100644 index 00000000000..bf93150cae6 --- /dev/null +++ b/real_estate/models/estate_property_tags.py @@ -0,0 +1,15 @@ +from odoo import fields, models + + +class EstatePropertyTags(models.Model): + _name = "estate.property.tags" + _description = "Real Estate Property Tags" + _order = "name asc" + + name = fields.Char(string="Name", required=True) + color = fields.Integer(string="Color") + + _name_uniq = models.Constraint( + 'unique (name)', + "This tag is already available", + ) diff --git a/real_estate/models/estate_property_type.py b/real_estate/models/estate_property_type.py new file mode 100644 index 00000000000..2b0e4726b3e --- /dev/null +++ b/real_estate/models/estate_property_type.py @@ -0,0 +1,16 @@ +from odoo import api, fields, models + + +class EstatePropertyType(models.Model): + _name = "estate.property.type" + _description = "Real Estate Property Type" + + name = fields.Char(string="Name", required=True) + property_ids = fields.One2many('estate.property', 'property_type') + offer_ids = fields.One2many(comodel_name="estate.property.offers", inverse_name="property_type_id", string="Offers") + 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/real_estate/security/ir.model.access.csv b/real_estate/security/ir.model.access.csv new file mode 100644 index 00000000000..58710e125d0 --- /dev/null +++ b/real_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_tags,access.estate.property.tags,model_estate_property_tags,base.group_user,1,1,1,1 +access_estate_property_offers,access.estate.property.offers,model_estate_property_offers,base.group_user,1,1,1,1 diff --git a/real_estate/views/estate_menu.xml b/real_estate/views/estate_menu.xml new file mode 100644 index 00000000000..925907a4c07 --- /dev/null +++ b/real_estate/views/estate_menu.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/real_estate/views/estate_property_offers_templates.xml b/real_estate/views/estate_property_offers_templates.xml new file mode 100644 index 00000000000..856094faf2e --- /dev/null +++ b/real_estate/views/estate_property_offers_templates.xml @@ -0,0 +1,22 @@ + + + + Property Offers + estate.property.offers + list,form + [('property_type_id', '=', active_id)] + + + estate.property.offers.list + estate.property.offers + + + + + +