Skip to content

Commit

Permalink
[IMP] estate: add the sprinkles! (ch12)
Browse files Browse the repository at this point in the history
  • Loading branch information
mizosoft committed Mar 22, 2024
1 parent fe593c2 commit 15c80f9
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 39 deletions.
2 changes: 1 addition & 1 deletion estate/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
'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_property_offer_views.xml',
'views/estate_menus.xml',
],
'application': True,
Expand Down
17 changes: 10 additions & 7 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class EsateProperty(models.Model):
_name = 'estate.property'
_description = 'Defines a real estate property'
_order = 'id desc'

name = fields.Char(string='Title', required=True, index=True)
description = fields.Text(string='Description')
Expand All @@ -30,12 +31,13 @@ class EsateProperty(models.Model):
],
'Garden Orientation')
active = fields.Boolean(string='Active', default=True)
status = fields.Selection([
state = fields.Selection([
('new', 'New'),
('offer_received', 'Offer Received'),
('offer_accepted', 'Offer Accepted'),
('sold', 'Sold'),
('cancelled', 'Cancelled')
], 'Status', default='new')
('cancelled', 'Cancelled'),
], 'State', default='new')
property_type_id = fields.Many2one('estate.property.type', string='Property Type')
buyer_id = fields.Many2one('res.partner', string='Buyer')
salesperson_id = fields.Many2one('res.users', string='Salesperson', default=lambda self: self.env.user)
Expand Down Expand Up @@ -78,23 +80,24 @@ def _check_selling_price(self):

def action_mark_sold(self):
for record in self:
if record.status == 'cancelled':
if record.state == 'cancelled':
raise UserError('A cancelled property cannot be sold')

record.status = 'sold'
record.state = 'sold'
return True

def action_mark_cancelled(self):
for record in self:
if record.status == 'sold':
if record.state == 'sold':
raise UserError('A sold property cannot be cancelled')

record.status = 'cancelled'
record.state = 'cancelled'
return True

def _accept_offer(self, offer):
self.buyer_id = offer.buyer_id
self.selling_price = offer.price
self.state = 'offer_accepted'

def _refuse_accepted_offer(self):
self.buyer_id = None
Expand Down
16 changes: 9 additions & 7 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
class EstatePropertyOffer(models.Model):
_name = 'estate.property.offer'
_description = 'An offer for a property'
_order = 'price desc'

price = fields.Float(default=0.0)
status = fields.Selection([
state = fields.Selection([
('new', 'New'),
('accepted', 'Accepted'),
('refused', 'Refused')
], default='new', copy=False)

buyer_id = fields.Many2one('res.partner', string='Buyer', required=True)
property_id = fields.Many2one('estate.property', string='Property', required=True)
property_type_id = fields.Many2one('estate.property.type', string='Property Type', related='property_id.property_type_id', stored=True)

validity = fields.Integer('Validity (days)', default=7)
date_deadline = fields.Date('Deadline', compute='_compute_deadline', inverse='_inverse_deadline')
validity = fields.Integer(string='Validity (days)', default=7)
date_deadline = fields.Date(string='Deadline', compute='_compute_deadline', inverse='_inverse_deadline')

create_date = fields.Date('Date Created')

Expand All @@ -43,20 +45,20 @@ def _inverse_deadline(self):

def action_accept(self):
for record in self:
if record.status == 'accepted':
if record.state == 'accepted':
continue

if record.property_id.selling_price:
raise UserError('The property already has an accepted offer!')

record.property_id._accept_offer(record)
record.status = 'accepted'
record.state = 'accepted'
return True

def action_refuse(self):
for record in self:
if record.status == 'accepted':
if record.state == 'accepted':
record.property_id._refuse_accepted_offer()

record.status = 'refused'
record.state = 'refused'
return True
4 changes: 3 additions & 1 deletion estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
class EstatePropertyTag(models.Model):
_name = 'estate.property.tag'
_description = 'A property tag'
_order = 'name'

name = fields.Char('Name', required=True)
name = fields.Char(string='Name', required=True)
color = fields.Integer(string='Color')

_sql_constraints = [
('unique_name',
Expand Down
15 changes: 13 additions & 2 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
from odoo import fields, models
from odoo import fields, models, api


class EstatePropertyType(models.Model):
_name = 'estate.property.type'
_description = 'A property type'
_order = 'sequence, name'

name = fields.Char('Name', required=True)
name = fields.Char(string='Name', required=True)
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(compute='_compute_offer_count')

sequence = fields.Integer(string='Sequence', default=1)

_sql_constraints = [
('unique_name',
'UNIQUE(name)',
'Property types must be unique.')
]

@api.depends('offer_ids')
def _compute_offer_count(self):
for record in self:
record.offer_count = len(record.offer_ids)
24 changes: 19 additions & 5 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@
<odoo>
<!--Tree views-->

<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">tree,form</field>
<field name="domain">[('property_type_id', '=', active_id)]</field>
<field name="context">{'default_property_type_id': active_id}</field>
</record>

<record id="estate_property_offer_view_tree" model="ir.ui.view">
<field name="name">estate.property.offer.tree</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<tree string="Offers">
<tree string="Offers"
editable="bottom"
decoration-danger="state == 'refused'"
decoration-success="state == 'accepted'"
decoration-muted="state == 'sold'">
<field name="price"/>
<field name="buyer_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<field name="status"/>
<button name="action_accept" type="object" title="Accept" icon="fa-check"/>
<button name="action_refuse" type="object" title="Refuse" icon="fa-times"/>
<field name="state" column_invisible="1"/>
<button name="action_accept" type="object"
title="Accept" icon="fa-check" invisible="state != 'new'"/>
<button name="action_refuse" type="object"
title="Refuse" icon="fa-times" invisible="state != 'new'"/>
</tree>
</field>
</record>
Expand All @@ -31,7 +45,7 @@
<field name="buyer_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<field name="status"/>
<field name="state"/>
</group>
</sheet>
</form>
Expand Down
2 changes: 1 addition & 1 deletion estate/views/estate_property_tag_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<field name="name">estate.property.tag.tree</field>
<field name="model">estate.property.tag</field>
<field name="arch" type="xml">
<tree string="Property Tags">
<tree string="Property Tags" editable="bottom">
<field name="name"/>
</tree>
</field>
Expand Down
18 changes: 18 additions & 0 deletions estate/views/estate_property_type_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<field name="model">estate.property.type</field>
<field name="arch" type="xml">
<tree string="Property Types">
<field name="sequence" widget="handle"/>
<field name="name"/>
</tree>
</field>
Expand All @@ -28,11 +29,28 @@
<field name="arch" type="xml">
<form string="Property Type">
<sheet>
<div class="oe_button_box" name="button_box">
<button name="%(estate.estate_property_offer_action)d"
type="action"
class="oe_stat_button"
icon="fa-money">
<field name="offer_count" widget="statinfo" string="Offers"/>
</button>
</div>

<div class="oe_title">
<h1 class="mb32">
<field name="name" class="mb16"/>
</h1>
</div>

<field name="property_ids">
<tree>
<field name="name"/>
<field name="expected_price"/>
<field name="state"/>
</tree>
</field>
</sheet>
</form>
</field>
Expand Down
36 changes: 21 additions & 15 deletions estate/views/estate_property_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<field name="name">Properties</field>
<field name="res_model">estate.property</field>
<field name="view_mode">tree,form</field>
<field name="context">{'search_default_available': True}</field>
</record>

<!--Tree views-->
Expand All @@ -14,15 +15,19 @@
<field name="name">estate.property.tree</field>
<field name="model">estate.property</field>
<field name="arch" type="xml">
<tree string="Properties">
<tree string="Properties"
decoration-success="state in ('offer_received', 'offer_accepted')"
decoration-bf="state == 'offer_accepted'"
decoration-muted="state == 'sold'">
<field name="name"/>
<field name="postcode"/>
<field name="bedrooms"/>
<field name="living_area"/>
<field name="expected_price"/>
<field name="selling_price"/>
<field name="date_availability"/>
<field name="property_type_id"/>
<field name="date_availability" optional="1"/>
<field name="state" invisible="1"/>
</tree>
</field>
</record>
Expand All @@ -35,17 +40,19 @@
<field name="arch" type="xml">
<form string="Property">
<header>
<button name="action_mark_sold" type="object" string="Sold"/>
<button name="action_mark_cancelled" type="object" string="Cancel"/>
<div invisible="state != 'new'">
<button name="action_mark_sold" type="object" string="Sold"/>
<button name="action_mark_cancelled" type="object" string="Cancel"/>
</div>
<field name="state" widget="statusbar" statusbar_visible="new,offer_received,offer_accepted,sold" options="{'clickable': '1'}"/>
</header>

<sheet>
<div class="oe_title">
<h1 class="mb32">
<field name="name" class="mb16"/>

<h3>
<field name="property_tag_ids" widget="many2many_tags"/>
<field name="property_tag_ids" widget="many2many_tags" options="{'color_field': 'color'}"/>
</h3>

<!--Allow archiving/unarchiving through gear menu.-->
Expand All @@ -55,10 +62,11 @@

<group>
<group>
<field name="status"/>
<field name="property_type_id"/>
<field name="state"/>
<field name="property_type_id" options="{'no_create': true}"/>
<field name="postcode"/>
</group>

<group>
<field name="date_availability"/>
<field name="expected_price"/>
Expand All @@ -76,14 +84,14 @@
<field name="facades"/>
<field name="garage"/>
<field name="garden"/>
<field name="garden_area"/>
<field name="garden_orientation"/>
<field name="garden_area" invisible="not garden"/>
<field name="garden_orientation" invisible="not garden"/>
<field name="total_area"/>
</group>
</page>

<page name="offers" string="Offers">
<field name="offer_ids"/>
<field name="offer_ids" readonly="state in ('offer_accepted', 'sold', 'cancelled')"/>
</page>

<page name="extra_info" string="Extra Info">
Expand All @@ -109,13 +117,11 @@
<field name="postcode"/>
<field name="expected_price"/>
<field name="bedrooms"/>
<field name="living_area"/>
<field name="living_area" filter_domain="[('living_area', '&gt;=', self)]"/>
<field name="facades"/>
<field name="property_type_id"/>

<filter string="Available" name="available" domain="[('status', 'in', ('new', 'offer_received'))]"/>
<filter string="Available" name="available" domain="[('state', 'in', ('new', 'offer_received'))]"/>
<filter string="Archived" name="inactive" domain="[('active', '=', False)]"/>

<filter string="Postcode" name="postcode" context="{'group_by': 'postcode'}"/>
</search>
</field>
Expand Down

0 comments on commit 15c80f9

Please sign in to comment.