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..f26f5efd8a5 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,15 @@ +{ + 'name': "Estate", + 'depends': ['base'], + 'application': True, + 'installable': True, + 'data': [ + 'security/ir.model.access.csv', + 'views/estate_property_views.xml', + 'views/estate_property_offer_views.xml', + 'views/estate_property_type_views.xml', + 'views/estate_property_tag_views.xml', + 'views/estate_menus.xml', + ], + "license": "LGPL-3", +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..6bf74e58eb2 --- /dev/null +++ b/estate/models/__init__.py @@ -0,0 +1,4 @@ +from . import estate_property +from . import estate_property_type +from . import estate_property_offer +from . import estate_property_tag diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py new file mode 100644 index 00000000000..e4b0cba2f4d --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,115 @@ +from datetime import datetime +from dateutil.relativedelta import relativedelta + +from odoo import _, api, fields, models +from odoo.exceptions import UserError, ValidationError +from odoo.tools.float_utils import float_compare, float_is_zero + + +class EstateProperty(models.Model): + _name = 'estate.property' + _description = "Real Estate Property" + + _sql_constraints = [ + ('check_expected_price', 'CHECK(expected_price > 0)', "Expected price must be strictly positive."), + ('check_selling_price', 'CHECK(selling_price >= 0)', "Selling price must be strictly positive.") + ] + + name = fields.Char(string="Title", required=True) + description = fields.Text() + postcode = fields.Char() + date_availability = fields.Date(copy=False, default=lambda self: datetime.today() + relativedelta(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(string="Facade") + 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"), + ], + string="Garden Orientation" + ) + available = fields.Boolean(default=True) + 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") + tag_ids = fields.Many2many("estate.property.tag", string="Tags") + offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers") + state = fields.Selection( + selection=[ + ('new', "New"), + ('offer_received', "Received"), + ('offer_accepted', "Accepted"), + ('sold', "Sold"), + ('cancelled', "Cancelled"), + ('refused', "Refused") + ], + required=True, + copy=False, + default='new' + ) + note = fields.Text(string="Special mention about the property.") + total_area = fields.Integer(string="Total Area (sqm)", compute="_compute_total_area") + best_offer = fields.Float(string="Best Offer", compute="_compute_best_price") + + @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: + record.best_offer = max(record.offer_ids.mapped("price"), default=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 = None + + def cancel_property_button(self): + for record in self: + if record.state != 'sold': + record.state = 'cancelled' + else: + raise UserError(_("You cannot cancel a sold property.")) + + def sold_property_button(self): + for record in self: + if record.state != 'cancelled': + record.state = 'sold' + else: + raise UserError("You cannot sell a cancelled property.") + + @api.constrains('expected_price') + def _check_expected_price_positive(self): + for record in self: + if record.expected_price <= 0: + raise ValidationError("Expected price must be strictly positive.") + + @api.constrains('selling_price', 'expected_price') + def _check_selling_price_margin(self): + for record in self: + if float_is_zero(record.selling_price, precision_digits=2): + continue + min_acceptable_price = record.expected_price * 0.9 + if float_compare(record.selling_price, min_acceptable_price, precision_digits=2) < 0: + raise ValidationError("The selling price cannot be lower than 90% of the expected price.") + + @api.ondelete(at_uninstall=False) + def unlink(self): + for record in self: + if record.state not in ['new', 'cancelled']: + raise UserError("YOU CANNOT DELETE A PROPERTY THAT IS NOT IN NEW OR CANCELLED STATUS. REVERT!") + return super().unlink() diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py new file mode 100644 index 00000000000..42de864a4d8 --- /dev/null +++ b/estate/models/estate_property_offer.py @@ -0,0 +1,65 @@ +from datetime import timedelta + +from odoo import api, fields, models, exceptions +from odoo.exceptions import UserError + + +class EstatePropertyOffer(models.Model): + _name = "estate.property.offer" + _description = "Estate property offer" + + price = fields.Float() + status = fields.Selection( + selection=[ + ('accepted', "Accepted"), + ('refused', "Refused") + ], + copy=False + ) + partner_id = fields.Many2one("res.partner", required=True) + property_id = fields.Many2one("estate.property", required=True) + validity = fields.Integer(default=7) + deadline = fields.Date(string="Deadline", compute="_compute_deadline", inverse="_inverse_deadline") + creation_date = fields.Datetime("Creation Date", readonly=True) + + @api.depends("validity") + def _compute_deadline(self): + for record in self: + if record.creation_date: + creation_date = fields.Datetime.from_string(record.creation_date) + record.deadline = creation_date.date() + timedelta(days=record.validity) + + def _inverse_deadline(self): + for record in self: + if record.deadline: + if record.creation_date: + creation_date = fields.Datetime.from_string(record.creation_date) + record.validity = (record.deadline - creation_date.date()).days + + def offer_accept(self): + if 'accepted' in self.mapped("property_id.offer_ids.status"): + raise UserError("An offer is already accepted.") + for record in self: + record.status = 'accepted' + record.property_id.state = 'offer_accepted' + record.property_id.buyer_id = record.partner_id + record.property_id.selling_price = record.price + + def offer_refuse(self): + for record in self: + record.status = 'refused' + + @api.model_create_multi + def create(self, vals_list): + for vals in vals_list: + property_id = vals.get('property_id') + price = vals.get('price', default=0.0) + + if property_id and price is not None: + property_rec = self.env['estate.property'].browse(property_id) + if property_rec.best_offer > price: + raise exceptions.UserError("Cannot add offer for a lower amount than current offer") + else: + property_rec.state = "offer_received" + + return super().create(vals_list) diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py new file mode 100644 index 00000000000..4d0a0032716 --- /dev/null +++ b/estate/models/estate_property_tag.py @@ -0,0 +1,9 @@ +from odoo import fields, models + + +class estate_property_tag(models.Model): + _name = "estate.property.tag" + _description = "Estate property tag file" + + name = fields.Char('Name', required=True) + color = fields.Integer() diff --git a/estate/models/estate_property_type.py b/estate/models/estate_property_type.py new file mode 100644 index 00000000000..73efe902063 --- /dev/null +++ b/estate/models/estate_property_type.py @@ -0,0 +1,20 @@ +from odoo import api, fields, models + + +class estate_property_type(models.Model): + _name = "estate.property.type" + _description = "Property type models file" + _order = "name" + + _sql_constraints = [ + ('unique_type_name', 'UNIQUE(name)', 'The name should be unique') + ] + + name = fields.Char("Name", required=True) + property_id = fields.One2many("estate.property", "property_type_id") + offer_counts = fields.Integer(compute="_compute_offer_count") + + @api.depends("property_id.offer_ids") + def _compute_offer_count(self): + for record in self: + record.offer_counts = len(record.property_id.offer_ids) diff --git a/estate/models/res_users.py b/estate/models/res_users.py new file mode 100644 index 00000000000..e2b69954c41 --- /dev/null +++ b/estate/models/res_users.py @@ -0,0 +1,7 @@ +from odoo import fields, models + + +class res_users(models.Model): + _inherit = 'res.users' + + property_ids = fields.One2many('estate.property', "salesman_id", domain="('state','=','new'),('state','=','offer_received')") diff --git a/estate/security/ir.model.access.csv b/estate/security/ir.model.access.csv new file mode 100644 index 00000000000..54130d3231e --- /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" +"estate.access_estate_property","access_estate_property","estate.model_estate_property","base.group_user",1,1,1,1 +"estate.access_estate_property_type","access_estate_property_type","estate.model_estate_property_type","base.group_user",1,1,1,1 +"estate.access_estate_property_offer","access_estate_property_offer","estate.model_estate_property_offer","base.group_user",1,1,1,1 +"estate.access_estate_property_tag","access_estate_property_tag","estate.model_estate_property_tag","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..177c1f4ce7e --- /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..af532492542 --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,45 @@ + + + + estate.property.offer.form + estate.property.offer + +
+ + + +

+ +

+ + + + + +
+
+
+
+
+ + estate.property.offer.list + estate.property.offer + + + + + + + + + + + + + + + + + + + + + + + + + + + + Property 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 00000000000..25c708d130d --- /dev/null +++ b/estate/views/estate_property_views.xml @@ -0,0 +1,129 @@ + + + + estate.property.list + estate.property + + + + + + + + + + + + + + + + estate.property.search + estate.property + + + + + + + + + + + + + + + + + Properties + estate.property + kanban,list,form + + + + estate.property.view.form + estate.property + +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+ + + estate.property.kanban + estate.property + + + + + +
+ Expected price:
+
+ + Best offer:
+
+ + Selling price:
+
+
+
+
+
+
+ + +
diff --git a/estate/views/res_uesrs_view.xml b/estate/views/res_uesrs_view.xml new file mode 100644 index 00000000000..f434ed8c30b --- /dev/null +++ b/estate/views/res_uesrs_view.xml @@ -0,0 +1,15 @@ + + + + res.users + 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..890f9d2291d --- /dev/null +++ b/estate_account/__manifest__.py @@ -0,0 +1,12 @@ +{ + 'name': 'Estate Account', + 'depends': [ + 'estate', + 'account' + ], + 'data': [ + 'security/ir.model.access.csv' + ], + 'application': True, + "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..1e950978353 --- /dev/null +++ b/estate_account/models/estate_property.py @@ -0,0 +1,27 @@ +from odoo import models, Command + + +class estate_property(models.Model): + _inherit = 'estate.property' + + def sold_property_button(self): + + super().sold_property_button() + + for property in self: + self.env['account.move'].create({ + 'partner_id': property.buyer_id.id, # corrected field name + 'move_type': 'out_invoice', + 'invoice_line_ids': [ + Command.create({ + "name": property.name, + "quantity": 1.0, + "price_unit": property.selling_price * 6.0 / 100.0, + }), + Command.create({ + "name": "Administrative fees", + "quantity": 1.0, + "price_unit": 100.0, + }), + ], + }) diff --git a/estate_account/security/ir.model.access.csv b/estate_account/security/ir.model.access.csv new file mode 100644 index 00000000000..78458c1cb61 --- /dev/null +++ b/estate_account/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 +estate.access_estate_property,access_estate_property,estate.model_estate_property,base.group_user,1,1,1,1 +estate.access_estate_property_type,access_estate_property_type,estate.model_estate_property_type,base.group_user,1,1,1,1 +estate.access_estate_property_tag,access_estate_property_tag,estate.model_estate_property_tag,base.group_user,1,1,1,1 +estate.access_estate_property_offer,access_estate_property_offer,estate.model_estate_property_offer,base.group_user,1,1,1,1