-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[ADD] estate: created a new module #1013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 19.0
Are you sure you want to change the base?
Changes from all commits
f6b793d
c86ae0a
3e951c1
17de643
ff2dc3a
6f8f97b
50a5546
7e6d80c
6032334
8d96247
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import models |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| 'name': 'Real Estate', | ||
| 'depends': ['base'], | ||
| '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/res_users_views.xml', | ||
| 'views/estate_menus.xml' | ||
| ], | ||
| 'application': True, | ||
| 'installable': True, | ||
| 'author': 'Odoo S.A.', | ||
| 'license': 'LGPL-3' | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| from dateutil.relativedelta import relativedelta | ||
|
|
||
| from odoo import models, fields, api | ||
| from odoo.exceptions import UserError | ||
| from odoo.exceptions import ValidationError | ||
| from odoo.tools.float_utils import float_compare, float_is_zero | ||
|
|
||
|
|
||
| class EstateProperty(models.Model): | ||
| _name = "estate.property" | ||
| _description = "Real Estate Property" | ||
| _order = "id desc" | ||
|
|
||
| name = fields.Char(required=True) | ||
| description = fields.Text() | ||
| postcode = fields.Char() | ||
| date_availability = fields.Date("Availability Date", default=fields.Date.today() + relativedelta(months=3)) | ||
| expected_price = fields.Float("Expected Price", required=True) | ||
| selling_price = fields.Float("Selling Price", readonly=True) | ||
| bedrooms = fields.Integer(default=2) | ||
| living_area = fields.Integer("Living Area(sqm)") | ||
| facades = fields.Integer() | ||
| garage = fields.Boolean() | ||
| garden = fields.Boolean() | ||
| garden_area = fields.Integer("Garden Area(sqm)") | ||
| garden_orientation = fields.Selection( | ||
| selection=[ | ||
| ("north", "North"), | ||
| ("south", "South"), | ||
| ("east", "East"), | ||
| ("west", "West"), | ||
| ], | ||
| string="Garden Orientation", | ||
| ) | ||
| state = fields.Selection( | ||
| [ | ||
| ("new", "New"), | ||
| ("offer_received", "Offer Received"), | ||
| ("offer_accepted", "Offer Accepted"), | ||
| ("sold", "Sold"), | ||
| ("cancelled", "Cancelled"), | ||
| ], | ||
| "Status", | ||
| required=True, | ||
| copy=False, | ||
| default="new", | ||
| ) | ||
| active = fields.Boolean(default=True) | ||
| property_type_id = fields.Many2one("estate.property.type", "Property Type") | ||
| buyer_id = fields.Many2one("res.partner", "Buyer", copy=False) | ||
| salesperson_id = fields.Many2one("res.users", string="Salesperson") | ||
| tag_ids = fields.Many2many("estate.property.tag", "Tags") | ||
| offer_ids = fields.One2many("estate.property.offer", "property_id", "Offers") | ||
| total_area = fields.Integer("Total Area(sqm)", compute="_compute_total_area") | ||
| best_price = fields.Float("Best Offer", compute="_compute_best_price") | ||
|
|
||
| _check_expected_price = models.Constraint( | ||
| 'CHECK(expected_price > 0)', | ||
| 'The expected price of a property must be strictly positive.' | ||
| ) | ||
|
|
||
| _check_selling_price = models.Constraint( | ||
| 'CHECK(selling_price > 0)', | ||
| 'The selling price of a property 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.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 = False | ||
|
|
||
| def action_cancel(self): | ||
| for record in self: | ||
| if record.state == 'sold': | ||
| raise UserError("A sold property cannot be cancelled") | ||
| else: | ||
| self.state = 'cancelled' | ||
|
|
||
| def action_sold(self): | ||
| for record in self: | ||
| if record.state == 'cancelled': | ||
| raise UserError("A cancelled property cannot be set as sold") | ||
| else: | ||
| self.state = 'sold' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here also |
||
|
|
||
| @api.constrains('selling_price', 'expected_price') | ||
| def _check_selling_price(self): | ||
| for record in self: | ||
| if not float_is_zero(record.selling_price, precision_digits=2): | ||
| if float_compare(record.selling_price, record.expected_price * 0.9, 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_if_new_or_cancelled(self): | ||
| for record in self: | ||
| if record.state not in ('new', 'cancelled'): | ||
| raise UserError("Only 'New' or 'Cancelled' property can be deleted.") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| from dateutil.relativedelta import relativedelta | ||
|
|
||
| from odoo import fields, models, api | ||
| from odoo.exceptions import UserError | ||
|
|
||
|
|
||
| class EstatePropertyOffer(models.Model): | ||
| _name = "estate.property.offer" | ||
| _description = "Real Estate Property Offer" | ||
| _order = "price desc" | ||
|
|
||
| price = fields.Float() | ||
| status = fields.Selection( | ||
| [("accepted", "Accepted"), ("refused", "Refused")], | ||
| copy=False, | ||
| ) | ||
| partner_id = fields.Many2one("res.partner", string="Partner", required=True) | ||
| property_id = fields.Many2one("estate.property", string="Property", required=True) | ||
| validity = fields.Integer(string="Validity(days)", default=7) | ||
| date_deadline = fields.Date(string="Deadline Date", compute="_compute_date_deadline", inverse="_inverse_date_deadline") | ||
| property_type_id = fields.Many2one(related="property_id.property_type_id", store=True) | ||
|
|
||
| @api.depends('validity') | ||
| def _compute_date_deadline(self): | ||
| for record in self: | ||
| creation_date = record.create_date or fields.Date.today() | ||
| record.date_deadline = relativedelta(days=record.validity) + creation_date | ||
|
|
||
| def _inverse_date_deadline(self): | ||
| for record in self: | ||
| creation_date = record.create_date or fields.Date.today() | ||
| record.validity = (record.date_deadline - fields.Date.to_date(creation_date)).days | ||
|
|
||
| def action_accept(self): | ||
| for record in self: | ||
| if record.property_id.buyer_id: | ||
| raise UserError("Property already accepted") | ||
| else: | ||
| record.status = 'accepted' | ||
| record.property_id.selling_price = record.price | ||
| record.property_id.state = 'offer_accepted' | ||
| record.property_id.buyer_id = record.partner_id | ||
|
|
||
| def action_refuse(self): | ||
| for record in self: | ||
| record.status = 'refused' | ||
| return True | ||
|
|
||
| @api.model | ||
| def create(self, value): | ||
| for record in value: | ||
| property_id = record.get('property_id') | ||
| offer_price = record.get('price') | ||
| if property_id and offer_price is not None: | ||
| estate_property = self.env['estate.property'].browse(property_id) | ||
| estate_property.state = 'offer_received' | ||
| existing_offers = self.search([('property_id', '=', property_id), ('price', '>=', offer_price)]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not good way to use search() inside a loop. |
||
| if existing_offers: | ||
| raise UserError("You cannot create an offer with a lower amount than an existing offer for this property.") | ||
| return super().create(value) | ||
|
|
||
| _check_offer_price = models.Constraint( | ||
| 'CHECK(price > 0)', | ||
| 'The price of an offer must be strictly positive.' | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from odoo import fields, models | ||
|
|
||
|
|
||
| class EstatePropertyTag(models.Model): | ||
| _name = "estate.property.tag" | ||
| _description = "Real Estate Property Tag" | ||
| _order = "name" | ||
|
|
||
| name = fields.Char(required=True) | ||
| color = fields.Integer() | ||
|
|
||
| _check_tag_name_unique = models.Constraint( | ||
| 'UNIQUE(name)', | ||
| 'The name of the property tag must be unique.' | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| from odoo import fields, models, api | ||
|
|
||
|
|
||
| class EstatePropertyType(models.Model): | ||
| _name = "estate.property.type" | ||
| _description = "Real Estate Property Type" | ||
| _order = "sequence, name" | ||
|
|
||
| name = fields.Char(required=True) | ||
| property_ids = fields.One2many("estate.property", "property_type_id", string="Properties") | ||
| sequence = fields.Integer("Sequence", default=1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I suggested to you in previous reviews, Odoo always generates a string if it same as the technical name, then no need to add. |
||
| offer_ids = fields.One2many("estate.property.offer", "property_type_id", string="Offers") | ||
| offer_count = fields.Integer(string="Offer Count", compute="_compute_offer_count") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
|
|
||
| @api.depends("offer_ids") | ||
| def _compute_offer_count(self): | ||
| for record in self: | ||
| record.offer_count = len(record.offer_ids) | ||
|
|
||
| _check_type_name_unique = models.Constraint( | ||
| 'UNIQUE(name)', | ||
| 'The name of the property type must be unique.' | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from odoo import fields, models | ||
|
|
||
| class InheritedModel(models.Model): | ||
| _inherit = "res.users" | ||
|
|
||
| property_ids=fields.One2many("estate.property", "salesperson_id", domain=[("state", "!=", "sold")], string="Properties") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| access_estate_property_tag,estate.property.tag.access,model_estate_property_tag,base.group_user,1,1,1,1 | ||
| access_estate_property_offer,estate.property.offer.access,model_estate_property_offer,base.group_user,1,1,1,1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <odoo> | ||
| <menuitem id="estate_menu_root" name="Real Estate" /> | ||
|
|
||
| <menuitem id="menu_estate_advertisements" | ||
| name="Advertisements" | ||
| parent="estate_menu_root" /> | ||
|
|
||
| <menuitem id="menu_estate_property_action" | ||
| name="Properties" | ||
| parent="menu_estate_advertisements" | ||
| action="action_estate_property" /> | ||
|
|
||
| <menuitem id="menu_estate_settings" | ||
| name="Settings" | ||
| parent="estate_menu_root" /> | ||
|
|
||
| <menuitem id="menu_estate_property_type_action_settings" | ||
| name="Property Types" | ||
| parent="menu_estate_settings" | ||
| action="estate_property_type_action" /> | ||
|
|
||
| <menuitem id="menu_estate_property_tag_action_settings" | ||
| name="Property Tags" | ||
| parent="menu_estate_settings" | ||
| action="action_estate_property_tag" /> | ||
| </odoo> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <odoo> | ||
| <record id="estate_property_offer_action" model="ir.actions.act_window"> | ||
| <field name="name">Property Offers</field> | ||
| <field name="res_model">estate.property.offer</field> | ||
| <field name="view_mode">list,form</field> | ||
| <field name="domain">[('property_type_id', '=', active_id)]</field> | ||
| </record> | ||
|
|
||
| <record id="estate_property_offer_list_view" model="ir.ui.view"> | ||
| <field name="name">estate.property.offer.list</field> | ||
| <field name="model">estate.property.offer</field> | ||
| <field name="arch" type="xml"> | ||
| <list decoration-danger="status == 'refused'" | ||
| decoration-success="status == 'accepted'" editable="bottom"> | ||
| <field name="price" /> | ||
| <field name="partner_id" /> | ||
| <field name="status" /> | ||
| <field name="validity" /> | ||
| <field name="date_deadline" /> | ||
| <button name="action_accept" type="object" string="Accept" icon="fa-check" | ||
| invisible="status in ['accepted', 'refused']" /> | ||
| <button name="action_refuse" type="object" string="Refuse" icon="fa-times" | ||
| invisible="status in ['accepted', 'refused']" /> | ||
| <field name="property_type_id" /> | ||
| </list> | ||
| </field> | ||
| </record> | ||
|
|
||
| <record id="estate_property_offer_form_view" model="ir.ui.view"> | ||
| <field name="name">estate.property.offer.form</field> | ||
| <field name="model">estate.property.offer</field> | ||
| <field name="arch" type="xml"> | ||
| <form> | ||
| <sheet> | ||
| <group> | ||
| <field name="price" /> | ||
| <field name="partner_id" /> | ||
| <field name="status" /> | ||
| <field name="validity" /> | ||
| <field name="date_deadline" /> | ||
| </group> | ||
| </sheet> | ||
| </form> | ||
| </field> | ||
| </record> | ||
| </odoo> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <odoo> | ||
| <record id="action_estate_property_tag" model="ir.actions.act_window"> | ||
| <field name="name">Property Tags</field> | ||
| <field name="res_model">estate.property.tag</field> | ||
| <field name="view_mode">list,form</field> | ||
| </record> | ||
|
|
||
| <record id="estate_property_tag_tree" model="ir.ui.view"> | ||
| <field name="name">estate.property.tag.list</field> | ||
| <field name="model">estate.property.tag</field> | ||
| <field name="arch" type="xml"> | ||
| <list string="Property Tags" editable="top"> | ||
| <field name="name"/> | ||
| </list> | ||
| </field> | ||
| </record> | ||
| </odoo> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will throw an error if multiple records are in self.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about these changes?