diff --git a/estate/.vscode/settings.json b/estate/.vscode/settings.json new file mode 100644 index 00000000000..13bb971d627 --- /dev/null +++ b/estate/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "xml.symbols.enabled": false, + "python.languageServer": "None" +} \ No newline at end of file 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..0df472bdec3 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,17 @@ +{ + 'name': 'Estate', + 'depends': ['base'], + 'application': True, + 'author': 'toher', + 'license': 'LGPL-3', + + 'data': [ + 'security/ir.model.access.csv', + 'view/estate_property_offer_view.xml', + 'view/estate_property_views.xml', + 'view/estate_property_type_view.xml', + 'view/estate_property_tag_view.xml', + 'view/res_users.xml', + 'view/estate_menus.xml', + ], +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..12614b21242 --- /dev/null +++ b/estate/models/__init__.py @@ -0,0 +1,5 @@ +from . import estate_property +from . import estate_type +from . import estate_tag +from . import estate_offer +from . import res_user diff --git a/estate/models/estate_offer.py b/estate/models/estate_offer.py new file mode 100644 index 00000000000..af0c02acd09 --- /dev/null +++ b/estate/models/estate_offer.py @@ -0,0 +1,70 @@ +from datetime import timedelta + +from odoo.exceptions import UserError + +from odoo import api, fields, models + + +class EstatePropertyOffer(models.Model): + _name = "estate.property.offer" + _description = "Offer" + _order = "price desc" + + price = fields.Float() + status = fields.Selection( + string="Status", + selection=[ + ("accepted", "Accepted"), + ("refused", "Refused"), + ("new", "New") + ], + copy=False, + default="new" + ) + partner_id = fields.Many2one("res.partner", string="Buyer", required=True) + property_id = fields.Many2one("estate.property", string="Property", required=True) + validity = fields.Integer(string="Validity", default=7) + date_deadline = fields.Date(string="Deadline", compute="_compute_dead_line", inverse="_inverse_validity") + + _check_positif_price = models.Constraint( + 'CHECK(price > 0)', + 'The price must be positive' + ) + + property_type_id = fields.Many2one(related="property_id.property_type_id", store=True) + + @api.model + def create(self, vals_list): + for vals in vals_list: + property = self.env["estate.property"].browse(vals["property_id"]) + property.set_offer_received() + all_prices = property.proterty_offer_ids.mapped('price') + max_price = all_prices[0] if all_prices else 0 + if max_price and max_price >= vals['price']: + raise UserError(self.env._("New offer can't be lower than an other offer")) + return super().create(vals_list) + + @api.depends("validity") + def _compute_dead_line(self): + for offer in self: + offer.date_deadline = fields.Date.today() + timedelta(days=offer.validity) + + def _inverse_validity(self): + for offer in self: + difference = (offer.create_date.date() - fields.Date.today()) + offer.validity = difference.days + + def action_accept_offer(self): + for offer in self: + if offer.status == "refused": + raise UserError(self.env._("Can't accept an refused offer")) + offer.status = "accepted" + offer.property_id.accept_offer() + return True + + def action_refuse_offer(self): + for offer in self: + if offer.status == "accepted": + raise UserError(self.env._("Can't refuse an accepted offer")) + offer.status = "refused" + return True diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py new file mode 100644 index 00000000000..33133af0b06 --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,133 @@ +from datetime import timedelta + +from odoo.tools import float_utils + +from odoo import api, exceptions, fields, models + + +class EstateProperty(models.Model): + _name = "estate.property" + _description = "Estate Property Module" + _order = "id desc" + + _check_positif_expected_price = models.Constraint( + 'CHECK(expected_price > 0)', + 'The expected price must be strictly positive' + ) + _check_positif_selling_price = models.Constraint( + 'CHECK(selling_price >= 0)', + 'The selling price must be positive' + ) + + # Base Fields + name = fields.Char(string="Title", required=True) + description = fields.Text(string="Description") + postcode = fields.Char(string="Postcode") + expected_price = fields.Float(string="Expected Price", required=True) + selling_price = fields.Float(string="Selling Price", copy=False, readonly=True) + best_price = fields.Integer(string="Best Price", compute="_compute_best_price") + expected_date = fields.Date( + string="Expected Date", + required=True, + copy=False, + default=lambda _: (fields.Date.today() + timedelta(days=90)), + ) + bedroom = fields.Integer(string="Number of Bedroom", default=2) + living_area = fields.Integer(string="Living area (square metter)") + facades = fields.Integer(string="Number of facades") + garage = fields.Boolean(string="Have a garage?") + garden = fields.Boolean(string="Have a garden?") + garden_area = fields.Integer(string="Garden area (square metter)") + garden_orientation = fields.Selection( + string="Garden's orientation", + selection=[ + ("north", "North"), + ("south", "South"), + ("east", " East"), + ("west", "West"), + ], + ) + total_area = fields.Integer(string="Total Area", compute="_compute_total_area") + state = fields.Selection( + string="Estate's state", + selection=[ + ("new", "New"), + ("offer_received", "Offer Received"), + ("offer_accepted", " Offer Accepted"), + ("sold", "Sold"), + ("cancelled", "Cancelled"), + ], + default="new", + copy=False + ) + salesman_id = fields.Many2one( + "res.users", + string="Salesman", + default=lambda self: self.env.user + ) + buyer_id = fields.Many2one("res.partner", string="Buyer", copy=False) + property_type_id = fields.Many2one("estate.property.type", string="Type") + property_tag_ids = fields.Many2many("estate.property.tag", string="Tag") + proterty_offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers") + active = fields.Boolean(default=True) + + @api.depends('garden_area', 'living_area') + def _compute_total_area(self): + for record in self: + record.total_area = record.living_area + record.garden_area + + @api.depends('proterty_offer_ids.price') + def _compute_best_price(self): + for record in self: + all_prices = record.proterty_offer_ids.mapped('price') + record.best_price = all_prices[0] if all_prices else 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 = '' + + @api.constrains("selling_price", "expected_price") + def _check_price_expectation(self): + for record in self: + if float_utils.float_is_zero(record.selling_price, 2): + return True + if float_utils.float_compare(record.selling_price, record.expected_price * 0.9, 2) == -1: + raise exceptions.ValidationError(self.env._("Selling price can't be smaller than 90% of the expected price")) + return True + + @api.ondelete(at_uninstall=False) + def delete(self): + for record in self: + if record.state not in {"new", "cancelled"}: + raise exceptions.UserError(self.env._(f"Can't delete an advertise in {record.state} state")) + + def action_sold_adv(self): + for advertisements in self: + if advertisements.state == "cancelled": + raise exceptions.UserError(self.env._("Can't sold an canceled advertise")) + advertisements.state = "sold" + return True + + def action_cancel_adv(self): + for advertisements in self: + if advertisements.state == "sold": + raise exceptions.UserError(self.env._("Can't cancel an sold advertise")) + advertisements.state = "cancelled" + return True + + def accept_offer(self): + for advertise in self: + accepted_offer = advertise.proterty_offer_ids.filtered(lambda r: r.status == 'accepted') + advertise.buyer_id = accepted_offer.partner_id + advertise.selling_price = accepted_offer.price + advertise.state = 'offer_accepted' + (advertise.proterty_offer_ids - accepted_offer).action_refuse_offer() + + def set_offer_received(self): + for advertise in self: + advertise.state = "offer_received" diff --git a/estate/models/estate_tag.py b/estate/models/estate_tag.py new file mode 100644 index 00000000000..77005a9f844 --- /dev/null +++ b/estate/models/estate_tag.py @@ -0,0 +1,15 @@ +from odoo import fields, models + + +class PropertyTag(models.Model): + _name = "estate.property.tag" + _description = "Estate Tag" + _order = "name" + + _check_unique_name = models.Constraint( + 'UNIQUE(name)', + "Another tag already exists with the same name!" + ) + + name = fields.Char(string="Tag") + color = fields.Integer() diff --git a/estate/models/estate_type.py b/estate/models/estate_type.py new file mode 100644 index 00000000000..05169b02b8e --- /dev/null +++ b/estate/models/estate_type.py @@ -0,0 +1,23 @@ +from odoo import api, fields, models + + +class EstateType(models.Model): + _name = "estate.property.type" + _description = "Estate Type" + _order = "sequence" + + _check_unique_name = models.Constraint( + 'UNIQUE(name)', + "Another type already exists with the same name!" + ) + + name = fields.Char(string="Type", required=True) + property_ids = fields.One2many('estate.property', 'property_type_id') + sequence = fields.Integer('Sequence', default=1, help="Used to order stages. Lower is better.") + offer_ids = fields.One2many('estate.property.offer', 'property_type_id') + 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/estate/models/res_user.py b/estate/models/res_user.py new file mode 100644 index 00000000000..320617c0ce6 --- /dev/null +++ b/estate/models/res_user.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") diff --git a/estate/security/ir.model.access.csv b/estate/security/ir.model.access.csv new file mode 100644 index 00000000000..dc51e32485d --- /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_model,access_estate_model,model_estate_property,base.group_user,1,1,1,1 +access_estate_type_model,access_state_type_model,model_estate_property_type,base.group_user,1,1,1,1 +access_estate_tag_model,access_state_tag_model,model_estate_property_tag,base.group_user,1,1,1,1 +access_estate_offer_model,access_state_offer_model,model_estate_property_offer,base.group_user,1,1,1,1 diff --git a/estate/view/estate_menus.xml b/estate/view/estate_menus.xml new file mode 100644 index 00000000000..6cdd588fa80 --- /dev/null +++ b/estate/view/estate_menus.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/estate/view/estate_property_offer_view.xml b/estate/view/estate_property_offer_view.xml new file mode 100644 index 00000000000..3d233f2b583 --- /dev/null +++ b/estate/view/estate_property_offer_view.xml @@ -0,0 +1,27 @@ + + + + Property Offers + estate.property.offer + list + [('property_type_id', '=', active_id)] + + + + estate.property.offer.list + estate.property.offer + + + + + + + + +

+ +

+ + + + + + + + + + + + + +
+
+ +
diff --git a/estate/view/estate_property_views.xml b/estate/view/estate_property_views.xml new file mode 100644 index 00000000000..2d4481e7122 --- /dev/null +++ b/estate/view/estate_property_views.xml @@ -0,0 +1,156 @@ + + + + + Properties + estate.property + list,form,kanban + {'search_default_available': True} + + + + + estate.property.list + estate.property + + + + + + + + + + + + + + + + estate.property.kanban + estate.property + + + + + + +
+ +
+ +
+ +
+ +
+
+ +
+ +
+
+
+ +
+
+
+
+
+
+
+ + + + estate.property.form + estate.property + +
+
+
+ +

+ +

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