Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions estate/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import models

17 changes: 12 additions & 5 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
{
"name": "Estate", # The name that will appear in the App list
"version": "16.0.0", # Version
"application": True, # This line says the module is an App, and not a module
"depends": ["base"], # dependencies
"name": "Estate",
"version": "16.0.0",
"application": True,
"depends": ["base"],
"data": [

"security/ir.model.access.csv",
"views/estate_property_offer_views.xml",
"views/estate_property_type_views.xml",
"views/estate_property_tag_views.xml",
"views/estate_property_views.xml",
"views/estate_menus.xml",
'views/res_users_views.xml',
],
"installable": True,
"application": True,
'license': 'LGPL-3',
}
5 changes: 5 additions & 0 deletions estate/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import estate_property_type
from . import estate_property_tag
from . import estate_property_offer
from . import estate_property
from . import res_users

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a good practice we leave a blank line at the end of the file

123 changes: 123 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
from odoo import api, fields, models
from odoo.exceptions import UserError, ValidationError
from odoo.tools import float_compare, float_is_zero
from datetime import datetime, timedelta

class EstateProperty(models.Model):
_name = "estate.property"
_description = "Real Estate Property"
_order = "id desc"

# SQL Constraints
_sql_constraints = [
('check_expected_price', 'CHECK(expected_price > 0)',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to have a consistency with the quote, either we use double quote everywhere or we use single quote everywhere

'The expected price must be strictly positive.'),
('check_selling_price', 'CHECK(selling_price >= 0)',
'The selling price must be positive.'),
]

name = fields.Char(required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date()
expected_price = fields.Float(required=True)
selling_price = fields.Float()
bedrooms = fields.Integer()
living_area = fields.Integer()
facades = fields.Integer()
garage = fields.Boolean()
garden = fields.Boolean()
garden_area = fields.Integer()
total_area = fields.Integer(compute="_compute_total_area", string="Total Area")
best_price = fields.Float(compute="_compute_best_price", string="Best Offer")
garden_orientation = fields.Selection([
('north', 'North'),
('south', 'South'),
('east', 'East'),
('west', 'West')
])
active = fields.Boolean(default=True)
state = fields.Selection([
('new', 'New'),
('offer_received', 'Offer Received'),
('offer_accepted', 'Offer Accepted'),
('sold', 'Sold'),
('cancelled', 'Cancelled')
], required=True, copy=False, default='new')
property_type_id = fields.Many2one(
"estate.property.type",
string="Property Type"
)
buyer_id = fields.Many2one(
"res.partner",
string="Buyer",
copy=False
)
salesperson_id = fields.Many2one(
"res.users",
string="Salesperson",
default=lambda self: self.env.user
)
tag_ids = fields.Many2many(
"estate.property.tag",
string="Tags"
)
offer_ids = fields.One2many(
"estate.property.offer",
"property_id",
string="Offers"
)

#api.depends area
@api.depends("living_area", "garden_area")
def _compute_total_area(self):
for record in self:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for record in self:
for property in self:

We tend to use a variable that show in which model we are so it is clearer for the person reading the code

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

#action area
def action_sold(self):
for record in self:
if record.state == 'cancelled':
raise UserError("Cancelled property cannot be sold.")
record.state = 'sold'
return True

def action_cancel(self):
for record in self:
if record.state == 'sold':
raise UserError("Sold property cannot be cancelled.")
record.state = 'cancelled'
return True


#constrains area
@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 _check_property_state(self):
for record in self:
if record.state not in ['new', 'cancelled']:
raise UserError("Only new and cancelled properties can be deleted.")
70 changes: 70 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from odoo import api, models, fields
from datetime import datetime, timedelta

class EstatePropertyOffer(models.Model):
_name = "estate.property.offer"
_description = "Real Estate Property Offer"
_order = "price desc"

# SQL Constraints
_sql_constraints = [
('check_price', 'CHECK(price > 0)',
'The offer price must be strictly positive.'),
]

price = fields.Float(required=True)
validity = fields.Integer(default=7)
date_deadline = fields.Date()
status = fields.Selection(
[('accepted', 'Accepted'), ('refused', 'Refused')],
copy=False
)
partner_id = fields.Many2one(
"res.partner",
string="Buyer",
required=True
)
property_id = fields.Many2one(
"estate.property",
string="Property",
required=True,
ondelete="cascade"
)

property_type_id = fields.Many2one(
related="property_id.property_type_id",
string="Property Type",
store=True
)

def action_accept(self):
for record in self:
# Diğer tüm offer'ları refuse et
record.property_id.offer_ids.write({'status': 'refused'})
# Bu offer'ı accept et
record.status = 'accepted'
# Property'nin buyer ve selling price'ını set et
record.property_id.buyer_id = record.partner_id
record.property_id.selling_price = record.price
record.property_id.state = 'offer_accepted'
return True

def action_refuse(self):
for record in self:
record.status = 'refused'
return True

@api.model
def create(self, vals_list):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def create(self, vals_list):
def create(self, vals):

As you are not working with @api.model_create_multi, you would only have a vals (dictionary) and not a vals_list (list of vals)

property_obj = self.env['estate.property'].browse(vals_list['property_id'])

existing_offers = self.search([('property_id', '=', vals_list['property_id'])])
if existing_offers:
max_price = max(existing_offers.mapped('price'))
if vals_list['price'] <= max_price:
raise UserError("The offer must be higher than existing offers.")


property_obj.state = 'offer_received'

return super().create(vals_list)
15 changes: 15 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from odoo import models, fields

class EstatePropertyTag(models.Model):
_name = "estate.property.tag"
_description = "Real Estate Property Tag"
_order = "name"

# SQL Constraints
_sql_constraints = [
('check_name', 'UNIQUE(name)',
'The property type name must be unique.'),
]

name = fields.Char(required=True)
color = fields.Integer()
35 changes: 35 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from odoo import api, models, fields

class EstatePropertyType(models.Model):
_name = "estate.property.type"
_description = "Real Estate Property Type"
_order = "name"

# SQL Constraints
_sql_constraints = [
('check_name', 'UNIQUE(name)',
'The property type name must be unique.'),
]

name = fields.Char(required=True)
sequence = fields.Integer(default=1, help="Used to order types")
property_ids = fields.One2many(
"estate.property",
"property_type_id",
string="Properties"
)

offer_ids = fields.One2many(
"estate.property.offer",
"property_type_id",
string="Offers"
)
offer_count = fields.Integer(
string="Offers Count",
compute="_compute_offer_count"
)

@api.depends("offer_ids")
def _compute_offer_count(self):
for record in self:
record.offer_count = len(record.offer_ids)
11 changes: 11 additions & 0 deletions estate/models/res_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from odoo import fields, models

class ResUsers(models.Model):
_inherit = "res.users"

property_ids = fields.One2many(
"estate.property",
"salesperson_id",
string="Properties",
domain=[('state', 'in', ['new', 'offer_received', 'offer_accepted'])]
)
5 changes: 5 additions & 0 deletions estate/security/ir.model.access.csv
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
access_estate_property_user,access_estate_property_user,model_estate_property,base.group_user,1,1,1,1
access_estate_property_type_user,access_estate_property_type_user,model_estate_property_type,base.group_user,1,1,1,1
access_estate_property_tag_user,access_estate_property_tag_user,model_estate_property_tag,base.group_user,1,1,1,1
access_estate_property_offer_user,access_estate_property_offer_user,model_estate_property_offer,base.group_user,1,1,1,1
8 changes: 8 additions & 0 deletions estate/views/estate_menus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<menuitem id="estate_menu_root" name="Real Estate">
<menuitem id="estate_first_level_menu" name="Advertisements">
<menuitem id="estate_property_menu_action" action="estate_property_action"/>
</menuitem>
</menuitem>
</odoo>
51 changes: 51 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!-- List View -->
<record id="estate_property_offer_view_list" 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 string="Offers" editable="top"
decoration-danger="status == 'refused'"
decoration-success="status == 'accepted'">
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<field name="status" column_invisible="True"/>
<button name="action_accept" type="object" icon="fa-check" title="Accept Offer" invisible="status in ['accepted', 'refused']"/>
<button name="action_refuse" type="object" icon="fa-times" title="Refuse Offer" invisible="status in ['accepted', 'refused']"/>
</list>
</field>
</record>

<!-- Form View -->
<record id="estate_property_offer_view_form" 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 string="Offer">
<sheet>
<group>
<field name="price"/>
<field name="validity"/>
<field name="date_deadline"/>
<field name="partner_id"/>
<field name="status"/>
<field name="property_type_id"/>
<!-- property_id zorunlu ama one2many üzerinden otomatik dolacak,
formda göstermek zorunda değilsin -->
</group>
</sheet>
</form>
</field>
<!-- Action -->
</record>
<record id="estate_property_offer_action" model="ir.actions.act_window">
<field name="name">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>
</odoo>

Loading