diff --git a/README.md b/README.md
index 89e6a2b229..13524591ac 100644
--- a/README.md
+++ b/README.md
@@ -8,3 +8,5 @@ It has 3 branches for each Odoo version: one for the bases, one for
The first contains the code of the modules that serve as base for the tutorials,
and the others contains the code of each chapter with the complete
solution.
+
+First changes
diff --git a/estate/__init__.py b/estate/__init__.py
new file mode 100644
index 0000000000..9a7e03eded
--- /dev/null
+++ b/estate/__init__.py
@@ -0,0 +1 @@
+from . import models
\ No newline at end of file
diff --git a/estate/__manifest__.py b/estate/__manifest__.py
new file mode 100644
index 0000000000..af543c238b
--- /dev/null
+++ b/estate/__manifest__.py
@@ -0,0 +1,17 @@
+{
+ 'name': 'Real estate',
+ 'depends': [
+ 'base',
+ ],
+ 'data': [
+ 'security/ir.model.access.csv',
+ 'views/estate_property_views.xml',
+ 'views/estate_property_tag_views.xml',
+ 'views/estate_property_offer_views.xml',
+ 'views/estate_property_type_views.xml',
+ 'views/estate_menu.xml',
+ 'views/res_users_views.xml',
+ ],
+ 'application': True,
+ 'license': 'OEEL-1'
+}
\ No newline at end of file
diff --git a/estate/models/__init__.py b/estate/models/__init__.py
new file mode 100644
index 0000000000..4419cea280
--- /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_user
\ No newline at end of file
diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py
new file mode 100644
index 0000000000..9640b9f25d
--- /dev/null
+++ b/estate/models/estate_property.py
@@ -0,0 +1,90 @@
+from odoo import fields, models, api
+from datetime import timedelta
+from odoo.exceptions import UserError
+from odoo.tools.float_utils import float_is_zero, float_compare
+from odoo.exceptions import ValidationError
+
+class EstateProperty(models.Model):
+ _name = "estate.property"
+ _description = "Real estate property"
+ _order = "id desc"
+
+ active = fields.Boolean(default=True)
+ name = fields.Char(required=True, string="Title")
+ postcode = fields.Char()
+ date_availability = fields.Date(copy=False, default=lambda self: fields.Date.today() + timedelta(days=90))
+ 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()
+ garage = fields.Boolean()
+ garden = fields.Boolean()
+ garden_area = fields.Integer()
+ garden_orientation = fields.Selection(
+ [("north", "North"), ("south", "South"), ("east", "East"), ("west", "West")]
+ )
+ state = fields.Selection(
+ [("new", "New"), ("offer_received", "Offer Received"), ("offer_accepted", "Offer Accepted"), ("sold", "Sold"), ("canceled", "Canceled")],
+ default="new",
+ required=True,
+ copy = False,
+ )
+ 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="Property Tags")
+ offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers")
+ total_area = fields.Float(compute="_compute_total_area")
+ best_price = fields.Float(compute="_compute_best_price")
+
+ _sql_constraints = [
+ ('property_positive_expected_price', 'CHECK (expected_price > 0)', 'Expected price must be positive'),
+ ('property_positive_selling_price', 'CHECK (selling_price >= 0)', 'Selling price must be positive'),
+ ]
+
+ @api.constrains("selling_price")
+ def _check_selling_price(self):
+ for record in self:
+ # selling_price < expected_price*0.9
+ if not float_is_zero(record.selling_price, 2) and float_compare(record.selling_price, record.expected_price*0.9, 2) == -1:
+ raise ValidationError('The selling price cannot be lower than 90% of the expected 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_price = max(record.offer_ids.mapped('price'), default=0)
+
+ @api.onchange("garden")
+ def _onchange_garden(self):
+ if self.garden:
+ self.garden_area = 10
+ self.garden_orientation = "north"
+ else:
+ self.garden_area = None
+ self.garden_orientation = None
+
+ def action_sold_property(self):
+ for record in self:
+ if record.state == "canceled":
+ raise UserError("A canceled property cannot be sold")
+ record.state = "sold"
+ return True
+
+ def action_cancel_property(self):
+ for record in self:
+ if record.state == "sold":
+ raise UserError("A sold property cannot be canceled")
+ record.state = "canceled"
+ return True
+
+ @api.ondelete(at_uninstall=False)
+ def _unlink_if_new_or_canceled(self):
+ for record in self:
+ if record.state not in ('new', 'canceled'):
+ raise UserError("Can't delete active property")
\ No newline at end of file
diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py
new file mode 100644
index 0000000000..b5d24cc7c2
--- /dev/null
+++ b/estate/models/estate_property_offer.py
@@ -0,0 +1,59 @@
+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(required=True)
+ 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(default=7)
+ date_deadline = fields.Date(compute="_compute_date_deadline", inverse="_inverse_date_deadline")
+ property_type_id = fields.Many2one(related="property_id.property_type_id", store=True)
+
+ _sql_constraints = [
+ ('property_offer_positive_price', 'CHECK (price > 0)', 'Price must be positive')
+ ]
+
+ @api.depends("validity", "create_date")
+ def _compute_date_deadline(self):
+ for record in self:
+ if record.create_date:
+ record.date_deadline = fields.Datetime.add(record.create_date, days=record.validity)
+ else:
+ record.date_deadline = fields.Datetime.add(fields.Datetime.today(), days=record.validity)
+
+ def _inverse_date_deadline(self):
+ for record in self:
+ date = record.create_date.date() if record.create_date else fields.Date.today()
+ record.validity = (record.date_deadline - date).days
+
+ def action_accept_offer(self):
+ for record in self:
+ if record.property_id.state == "sold":
+ raise UserError("The property is already sold")
+ record.status = "accepted"
+ record.property_id.buyer_id = record.partner_id
+ record.property_id.selling_price = record.price
+ record.property_id.state = "sold"
+ return True
+
+ def action_refuse_offer(self):
+ for record in self:
+ record.status = "refused"
+ return True
+
+ @api.model
+ def create(self, vals):
+ property_id = self.env["estate.property"].browse(vals['property_id'])
+ if vals['price'] < property_id.best_price:
+ raise UserError("Your offer is too low")
+ property_id.state = "offer_received"
+ return super().create(vals)
diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py
new file mode 100644
index 0000000000..83951c6418
--- /dev/null
+++ b/estate/models/estate_property_tag.py
@@ -0,0 +1,14 @@
+from odoo import fields, models
+
+
+class EstatePropertyTags(models.Model):
+ _name = "estate.property.tag"
+ _description = "Real estate property tag"
+ _order = "name"
+
+ name = fields.Char(required=True)
+ color = fields.Integer("Color")
+
+ _sql_constraints = [
+ ('property_tag_name_unique', 'UNIQUE(name)', 'Property tag name must be unique')
+ ]
diff --git a/estate/models/estate_property_type.py b/estate/models/estate_property_type.py
new file mode 100644
index 0000000000..6abc7f0c84
--- /dev/null
+++ b/estate/models/estate_property_type.py
@@ -0,0 +1,22 @@
+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(string="Sequence", default=1)
+ offer_ids = fields.One2many("estate.property.offer", "property_type_id")
+ offer_count = fields.Integer(compute="_compute_offer_count")
+
+ _sql_constraints = [
+ ('property_type_name_unique', 'UNIQUE(name)', 'Property type name must be unique')
+ ]
+
+ @api.depends('offer_ids')
+ def _compute_offer_count(self):
+ for record in self:
+ record.offer_count = len(record.offer_ids)
\ No newline at end of file
diff --git a/estate/models/res_user.py b/estate/models/res_user.py
new file mode 100644
index 0000000000..7c29943314
--- /dev/null
+++ b/estate/models/res_user.py
@@ -0,0 +1,6 @@
+from odoo import models, fields
+
+class User(models.Model):
+ _inherit = ['res.users']
+
+ property_ids = fields.One2many("estate.property", "salesperson_id", string="Sales person")
\ No newline at end of file
diff --git a/estate/security/ir.model.access.csv b/estate/security/ir.model.access.csv
new file mode 100644
index 0000000000..78458c1cb6
--- /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_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
diff --git a/estate/views/estate_menu.xml b/estate/views/estate_menu.xml
new file mode 100644
index 0000000000..195ceb5036
--- /dev/null
+++ b/estate/views/estate_menu.xml
@@ -0,0 +1,12 @@
+
+
+
+
\ No newline at end of file
diff --git a/estate/views/estate_property_offer_views.xml b/estate/views/estate_property_offer_views.xml
new file mode 100644
index 0000000000..35b5dfdd17
--- /dev/null
+++ b/estate/views/estate_property_offer_views.xml
@@ -0,0 +1,41 @@
+
+
+
+ estate.property.offer.tree
+ estate.property.offer
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ estate.property.offer.form
+ estate.property.offer
+
+
+
+
+
+
+ Property offers
+ estate.property.offer
+ tree,form
+ [('property_type_id', '=',active_id)]
+
+
\ No newline at end of file
diff --git a/estate/views/estate_property_tag_views.xml b/estate/views/estate_property_tag_views.xml
new file mode 100644
index 0000000000..b7c22cce56
--- /dev/null
+++ b/estate/views/estate_property_tag_views.xml
@@ -0,0 +1,18 @@
+
+
+
+ Property tags
+ estate.property.tag
+ tree,form
+
+
+
+ estate_property_tag.view.tree
+ estate.property.tag
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/estate/views/estate_property_type_views.xml b/estate/views/estate_property_type_views.xml
new file mode 100644
index 0000000000..b8225f9850
--- /dev/null
+++ b/estate/views/estate_property_type_views.xml
@@ -0,0 +1,51 @@
+
+
+
+ Property types
+ estate.property.type
+ tree,form
+
+
+
+ estate.property.type.form
+ estate.property.type
+
+
+
+
+
+
+ estate_property_type.view.tree
+ estate.property.type
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/estate/views/estate_property_views.xml b/estate/views/estate_property_views.xml
new file mode 100644
index 0000000000..7e7cad3a41
--- /dev/null
+++ b/estate/views/estate_property_views.xml
@@ -0,0 +1,142 @@
+
+
+
+ Properties
+ estate.property
+ tree,form,kanban
+ {'search_default_available': True}
+
+
+
+ estate.property.tree
+ estate.property
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ estate.property.form
+ estate.property
+
+
+
+
+
+
+ estate.property.search
+ estate.property
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ estate_property.view.kanban
+ estate.property
+
+
+
+
+
+
+
+
+ Expected price:
+
+
+
+ Best offer:
+
+
+
+ Selling price:
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/estate/views/res_users_views.xml b/estate/views/res_users_views.xml
new file mode 100644
index 0000000000..0b92d21da8
--- /dev/null
+++ b/estate/views/res_users_views.xml
@@ -0,0 +1,19 @@
+
+
+
+
+ res.users.view.form.inherit.estate
+ res.users
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/estate_account/__init__.py b/estate_account/__init__.py
new file mode 100644
index 0000000000..9a7e03eded
--- /dev/null
+++ b/estate_account/__init__.py
@@ -0,0 +1 @@
+from . import models
\ No newline at end of file
diff --git a/estate_account/__manifest__.py b/estate_account/__manifest__.py
new file mode 100644
index 0000000000..0c02ab4da7
--- /dev/null
+++ b/estate_account/__manifest__.py
@@ -0,0 +1,8 @@
+{
+ 'name': 'Estate account',
+ 'depends': [
+ 'estate',
+ 'account',
+ ],
+ 'license': 'OEEL-1'
+}
\ No newline at end of file
diff --git a/estate_account/models/__init__.py b/estate_account/models/__init__.py
new file mode 100644
index 0000000000..f4c8fd6db6
--- /dev/null
+++ b/estate_account/models/__init__.py
@@ -0,0 +1 @@
+from . import estate_property
\ No newline at end of file
diff --git a/estate_account/models/estate_property.py b/estate_account/models/estate_property.py
new file mode 100644
index 0000000000..88a05a6c00
--- /dev/null
+++ b/estate_account/models/estate_property.py
@@ -0,0 +1,24 @@
+from odoo import models, Command
+
+class EstateProperty(models.Model):
+ _inherit = "estate.property"
+
+ def action_sold_property(self):
+ for record in self:
+ self.env["account.move"].create({
+ 'partner_id': record.buyer_id.id,
+ 'move_type': 'out_invoice',
+ "invoice_line_ids": [
+ Command.create({
+ "name": "6% of selling price",
+ "quantity": 1,
+ "price_unit": self.selling_price*0.06
+ }),
+ Command.create({
+ "name": "Administration fees",
+ "quantity": 1,
+ "price_unit": 100
+ })
+ ]
+ })
+ return super().action_sold_property()
\ No newline at end of file