From 5d1ae0f46c1dcebab12658abe85bd7b8bd565f2d Mon Sep 17 00:00:00 2001 From: "Oussama (ouou)" Date: Mon, 21 Oct 2024 14:29:10 +0200 Subject: [PATCH 1/2] [ADD] estate: creating estate module Features : - Creating properties, types of properties - Handling customers offers - Sorting your properties by tags or by type - Generating invoices when properties are sold and much more... --- estate/__init__.py | 1 + estate/__manifest__.py | 18 +++ estate/data/ir.model.access.csv | 5 + estate/demo/demo.xml | 13 ++ estate/models/__init__.py | 5 + estate/models/estate_property.py | 91 +++++++++++ estate/models/estate_property_offer.py | 58 +++++++ estate/models/estate_property_tag.py | 11 ++ estate/models/estate_property_type.py | 19 +++ estate/models/helper.py | 2 + estate/models/res_users.py | 8 + estate/views/estate_menus.xml | 8 + estate/views/estate_property_offer_views.xml | 28 ++++ estate/views/estate_property_tag_views.xml | 7 + estate/views/estate_property_type_views.xml | 51 ++++++ estate/views/estate_property_views.xml | 157 +++++++++++++++++++ estate/views/res_users.xml | 16 ++ estate_account/__init__.py | 1 + estate_account/__manifest__.py | 7 + estate_account/models/__init__.py | 1 + estate_account/models/estate_property.py | 29 ++++ 21 files changed, 536 insertions(+) create mode 100644 estate/__init__.py create mode 100644 estate/__manifest__.py create mode 100644 estate/data/ir.model.access.csv create mode 100644 estate/demo/demo.xml create mode 100644 estate/models/__init__.py create mode 100644 estate/models/estate_property.py create mode 100644 estate/models/estate_property_offer.py create mode 100644 estate/models/estate_property_tag.py create mode 100644 estate/models/estate_property_type.py create mode 100644 estate/models/helper.py create mode 100644 estate/models/res_users.py create mode 100644 estate/views/estate_menus.xml create mode 100644 estate/views/estate_property_offer_views.xml create mode 100644 estate/views/estate_property_tag_views.xml create mode 100644 estate/views/estate_property_type_views.xml create mode 100644 estate/views/estate_property_views.xml create mode 100644 estate/views/res_users.xml create mode 100644 estate_account/__init__.py create mode 100644 estate_account/__manifest__.py create mode 100644 estate_account/models/__init__.py create mode 100644 estate_account/models/estate_property.py diff --git a/estate/__init__.py b/estate/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /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 0000000000..35eacba9d9 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,18 @@ +{ + 'name': 'Real Estate', + 'depends': ['base'], + 'category': 'Tutorials', + 'application': True, + 'data': ['data/ir.model.access.csv', + 'views/res_users.xml', + 'views/estate_property_offer_views.xml', + 'views/estate_property_views.xml', + 'views/estate_property_type_views.xml', + 'views/estate_property_tag_views.xml', + 'views/estate_menus.xml', + ], + 'demo': [ + "demo/demo.xml", + ], + 'license': 'AGPL-3' +} diff --git a/estate/data/ir.model.access.csv b/estate/data/ir.model.access.csv new file mode 100644 index 0000000000..69385c87d8 --- /dev/null +++ b/estate/data/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_prop,access_estate_prop,model_estate_property,base.group_user,1,1,1,1 +access_estate_prop_type,access_estate_prop_type,model_estate_property_type,base.group_user,1,1,1,1 +access_estate_prop_offer,access_estate_prop_offer,model_estate_property_offer,base.group_user,1,1,1,1 +access_estate_prop_tag,access_estate_prop_tag,model_estate_property_tag,base.group_user,1,1,1,1 diff --git a/estate/demo/demo.xml b/estate/demo/demo.xml new file mode 100644 index 0000000000..63025fadcb --- /dev/null +++ b/estate/demo/demo.xml @@ -0,0 +1,13 @@ + + + + + Mass cancel + + + list + code + action = records.action_cancel() + + + diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 0000000000..9a2189b638 --- /dev/null +++ b/estate/models/__init__.py @@ -0,0 +1,5 @@ +from . import estate_property +from . import estate_property_type +from . import estate_property_tag +from . import estate_property_offer +from . import res_users diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py new file mode 100644 index 0000000000..87ded8a7fc --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,91 @@ +from dateutil.relativedelta import relativedelta + +from odoo import api, fields, models +from odoo.exceptions import UserError, ValidationError +from odoo.tools import float_compare +from odoo.fields import Many2many, One2many + +from .helper import format_selection + + +class EstateProperty(models.Model): + _name = 'estate.property' + _description = 'Estate Property modelisation' + _order = 'id desc' + _sql_constraints = [ + ('expected_price_strictly_positive', 'CHECK(expected_price > 0)', 'Expected price must be stricly positive'), + ('selling_price_positive', 'CHECK(selling_price >= 0)', 'Selling price must be positive'), + ] + + name = fields.Char(required=True, string="Name") + description = fields.Text(string="Description") + postcode = fields.Char(string="Postal code") + date_availability = fields.Date(default=lambda x: fields.Date.today() + relativedelta(months=3), copy=False, + string='Availability date') + expected_price = fields.Float(required=True, string='Expected price') + selling_price = fields.Float(readonly=True, copy=False, string='Selling price') + bedrooms = fields.Integer(default=2, string='Bedrooms') + 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') + garden_orientation = fields.Selection(string='Orientation', + selection=format_selection(['north', 'south', 'east', 'west']), + ) + active = fields.Boolean(default=True, string='Active') + state = fields.Selection(string='State', + selection=format_selection( + ['new', 'offer received', 'offer accepted', 'sold', 'canceled']), + default='new') + + property_type_id = fields.Many2one('estate.property.type', string='Property type') + + buyer_id = fields.Many2one('res.partner', copy=False, string='Buyer') + salesperson_id = fields.Many2one('res.users', default=lambda self: self.env.user, string='Salesperson') + + tag_ids = Many2many('estate.property.tag', string='Tags') + offer_ids = One2many('estate.property.offer', 'property_id', string='Offers') + + total_area = fields.Integer(compute='_compute_total_area', string='Total area') + best_price = fields.Float(compute='_compute_best_price', string='Best offer price') + + @api.depends('living_area', 'garden_area') + def _compute_total_area(self): + for estate in self: + estate.total_area = estate.living_area + estate.garden_area + + @api.depends('offer_ids') + def _compute_best_price(self): + for estate in self: + estate.best_price = max(estate.offer_ids.mapped('price'), default=0) + + @api.onchange('garden') + def _onchange_garden(self): + self.garden = [0, 10][self.garden] + self.garden_orientation = ['', 'north'][self.garden] + + @api.constrains('selling_price') + def _check_price_offer_reasonable(self): + if float_compare(self.selling_price, 0.9 * self.expected_price, 3) <= 0: + raise ValidationError('The selling price must be at least 90% of the expected price.') + + @api.ondelete(at_uninstall=False) + def _unlink_property_if_not_new_nor_canceled(self): + self.ensure_one() + if self.state not in ('new', 'canceled'): + raise UserError('Only new and canceled properties can be deleted.') + + def action_sold(self): + self.ensure_one() + if self.state == 'canceled': + raise UserError('Canceled properties cannot be sold.') + self.state = 'sold' + return True + + def action_cancel(self): + self.ensure_one() + if self.state == 'sold': + raise UserError('Sold properties cannot be canceled.') + self.state = 'canceled' + return True diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py new file mode 100644 index 0000000000..7adb63b0f1 --- /dev/null +++ b/estate/models/estate_property_offer.py @@ -0,0 +1,58 @@ +from dateutil.relativedelta import relativedelta + +from odoo import api, fields, models +from odoo.exceptions import UserError + +from .helper import format_selection + + +class EstatePropertyOffer(models.Model): + _name = 'estate.property.offer' + _order = 'price desc' + _description = 'Offer modelisation' + _sql_constraints = [('offer_price_strictly_positive', 'CHECK(price > 0)', 'Offer price must be stricly positive')] + + price = fields.Float(string='Price', required=True) + status = fields.Selection(copy=False, selection=format_selection(['accepted', 'refused']), string='Status') + partner_id = fields.Many2one('res.partner', required=True, string='Buyer') + property_id = fields.Many2one('estate.property', required=True, string='Property') + + validity = fields.Integer(default=7, string="Validity") # in days + date_deadline = fields.Date(compute='_compute_deadline', inverse='_inverse_deadline', string='Deadline') + + property_type_id = fields.Many2one(related='property_id.property_type_id', string='Property type') + + @api.depends('validity') + def _compute_deadline(self): + for offer in self: + offer.date_deadline = fields.Date.today() + relativedelta(days=offer.validity) + + def _inverse_deadline(self): + for offer in self: + offer.validity = (offer.date_deadline - fields.Date.today()).days + + @api.model_create_multi + def create(self, vals_list): + for vals in vals_list: + property_id = self.env['estate.property'].browse(vals['property_id']) + if any(vals['price'] < offer.price for offer in property_id.offer_ids): + raise UserError('Offer price must be higher than existing offers.') + property_id.state = 'offer received' + return super().create(vals_list) + + def action_accept(self): + self.ensure_one() + if self.property_id.state == 'sold': + raise UserError('The house was already sold.') + self.status = 'accepted' + self.property_id.state = 'offer accepted' + self.property_id.buyer_id = self.partner_id + self.property_id.selling_price = self.price + return True + + def action_refuse(self): + for offer in self: + if offer.status == 'accepted': + raise UserError('The offer was already accepted.') + offer.status = 'refused' + return True diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py new file mode 100644 index 0000000000..b7d204dbf6 --- /dev/null +++ b/estate/models/estate_property_tag.py @@ -0,0 +1,11 @@ +from odoo import fields, models + + +class EstatePropertyTag(models.Model): + _name = 'estate.property.tag' + _order = 'name asc' + _description = 'Tag modelisation' + _sql_constraints = [('unique_name', 'unique(name)', 'Tag name must be unique')] + + name = fields.Char(required=True, string='Tag Name') + color = fields.Integer(string='Color Index') diff --git a/estate/models/estate_property_type.py b/estate/models/estate_property_type.py new file mode 100644 index 0000000000..4405dca5bf --- /dev/null +++ b/estate/models/estate_property_type.py @@ -0,0 +1,19 @@ +from odoo import fields, models + + +class EstatePropertyType(models.Model): + _name = 'estate.property.type' + _description = 'Estate Property Type modelisation' + _order = 'sequence asc, name asc' + _sql_constraints = [('unique_name', 'unique(name)', 'Type name must be unique')] + + name = fields.Char(required=True, string='Name') + property_ids = fields.One2many('estate.property', 'property_type_id', string='Properties') + sequence = fields.Integer(default=1) + + offer_ids = fields.One2many('estate.property.offer', 'property_type_id', string='Offers') + offer_count = fields.Integer(compute='_compute_offer_count', string='Offer Count') + + def _compute_offer_count(self): + for record in self: + record.offer_count = len(record.offer_ids) diff --git a/estate/models/helper.py b/estate/models/helper.py new file mode 100644 index 0000000000..fb9a179c0d --- /dev/null +++ b/estate/models/helper.py @@ -0,0 +1,2 @@ +def format_selection(l: list): + return [(field, field.title()) for field in l] diff --git a/estate/models/res_users.py b/estate/models/res_users.py new file mode 100644 index 0000000000..a658f77d3e --- /dev/null +++ b/estate/models/res_users.py @@ -0,0 +1,8 @@ +from odoo import fields, models + + +class Users(models.Model): + _inherit = 'res.users' + + property_ids = fields.One2many('estate.property', 'salesperson_id', + domain=[('state', 'not in', ['sold', 'canceled'])]) diff --git a/estate/views/estate_menus.xml b/estate/views/estate_menus.xml new file mode 100644 index 0000000000..9e9f344133 --- /dev/null +++ b/estate/views/estate_menus.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/estate/views/estate_property_offer_views.xml b/estate/views/estate_property_offer_views.xml new file mode 100644 index 0000000000..c4426b8010 --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,28 @@ + + + Property Offers + estate.property.offer + [('property_type_id', '=', active_id)] + list + + + + + estate.property.offer.list + estate.property.offer + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Properties 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 0000000000..8b727ebe48 --- /dev/null +++ b/estate/views/estate_property_views.xml @@ -0,0 +1,157 @@ + + + + estate.property.list + estate.property + + + + + + + + + + + + + + + + + estate.property.kanban + estate.property + + + + + +
+
+ +
+
+ Expected Price: + +
+
+ Best Offer Price: + +
+
+ Selling price: + +
+
+ +
+
+
+
+
+
+
+ + + estate.property.form + estate.property + +
+ +
+
+ + + +

+ +

+ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +