From 5d1ae0f46c1dcebab12658abe85bd7b8bd565f2d Mon Sep 17 00:00:00 2001 From: "Oussama (ouou)" Date: Mon, 21 Oct 2024 14:29:10 +0200 Subject: [PATCH 1/4] [ADD] estate: creating estate module Features : - Creating properties, types of properties - Handling customers offers - Sorting your properties by tags or by type - Generating invoices when properties are sold and much more... --- estate/__init__.py | 1 + estate/__manifest__.py | 18 +++ estate/data/ir.model.access.csv | 5 + estate/demo/demo.xml | 13 ++ estate/models/__init__.py | 5 + estate/models/estate_property.py | 91 +++++++++++ estate/models/estate_property_offer.py | 58 +++++++ estate/models/estate_property_tag.py | 11 ++ estate/models/estate_property_type.py | 19 +++ estate/models/helper.py | 2 + estate/models/res_users.py | 8 + estate/views/estate_menus.xml | 8 + estate/views/estate_property_offer_views.xml | 28 ++++ estate/views/estate_property_tag_views.xml | 7 + estate/views/estate_property_type_views.xml | 51 ++++++ estate/views/estate_property_views.xml | 157 +++++++++++++++++++ estate/views/res_users.xml | 16 ++ estate_account/__init__.py | 1 + estate_account/__manifest__.py | 7 + estate_account/models/__init__.py | 1 + estate_account/models/estate_property.py | 29 ++++ 21 files changed, 536 insertions(+) create mode 100644 estate/__init__.py create mode 100644 estate/__manifest__.py create mode 100644 estate/data/ir.model.access.csv create mode 100644 estate/demo/demo.xml create mode 100644 estate/models/__init__.py create mode 100644 estate/models/estate_property.py create mode 100644 estate/models/estate_property_offer.py create mode 100644 estate/models/estate_property_tag.py create mode 100644 estate/models/estate_property_type.py create mode 100644 estate/models/helper.py create mode 100644 estate/models/res_users.py create mode 100644 estate/views/estate_menus.xml create mode 100644 estate/views/estate_property_offer_views.xml create mode 100644 estate/views/estate_property_tag_views.xml create mode 100644 estate/views/estate_property_type_views.xml create mode 100644 estate/views/estate_property_views.xml create mode 100644 estate/views/res_users.xml create mode 100644 estate_account/__init__.py create mode 100644 estate_account/__manifest__.py create mode 100644 estate_account/models/__init__.py create mode 100644 estate_account/models/estate_property.py diff --git a/estate/__init__.py b/estate/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /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 0000000000..35eacba9d9 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,18 @@ +{ + 'name': 'Real Estate', + 'depends': ['base'], + 'category': 'Tutorials', + 'application': True, + 'data': ['data/ir.model.access.csv', + 'views/res_users.xml', + 'views/estate_property_offer_views.xml', + 'views/estate_property_views.xml', + 'views/estate_property_type_views.xml', + 'views/estate_property_tag_views.xml', + 'views/estate_menus.xml', + ], + 'demo': [ + "demo/demo.xml", + ], + 'license': 'AGPL-3' +} diff --git a/estate/data/ir.model.access.csv b/estate/data/ir.model.access.csv new file mode 100644 index 0000000000..69385c87d8 --- /dev/null +++ b/estate/data/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_prop,access_estate_prop,model_estate_property,base.group_user,1,1,1,1 +access_estate_prop_type,access_estate_prop_type,model_estate_property_type,base.group_user,1,1,1,1 +access_estate_prop_offer,access_estate_prop_offer,model_estate_property_offer,base.group_user,1,1,1,1 +access_estate_prop_tag,access_estate_prop_tag,model_estate_property_tag,base.group_user,1,1,1,1 diff --git a/estate/demo/demo.xml b/estate/demo/demo.xml new file mode 100644 index 0000000000..63025fadcb --- /dev/null +++ b/estate/demo/demo.xml @@ -0,0 +1,13 @@ + + + + + Mass cancel + + + list + code + action = records.action_cancel() + + + diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 0000000000..9a2189b638 --- /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_users diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py new file mode 100644 index 0000000000..87ded8a7fc --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,91 @@ +from dateutil.relativedelta import relativedelta + +from odoo import api, fields, models +from odoo.exceptions import UserError, ValidationError +from odoo.tools import float_compare +from odoo.fields import Many2many, One2many + +from .helper import format_selection + + +class EstateProperty(models.Model): + _name = 'estate.property' + _description = 'Estate Property modelisation' + _order = 'id desc' + _sql_constraints = [ + ('expected_price_strictly_positive', 'CHECK(expected_price > 0)', 'Expected price must be stricly positive'), + ('selling_price_positive', 'CHECK(selling_price >= 0)', 'Selling price must be positive'), + ] + + name = fields.Char(required=True, string="Name") + description = fields.Text(string="Description") + postcode = fields.Char(string="Postal code") + date_availability = fields.Date(default=lambda x: fields.Date.today() + relativedelta(months=3), copy=False, + string='Availability date') + expected_price = fields.Float(required=True, string='Expected price') + selling_price = fields.Float(readonly=True, copy=False, string='Selling price') + bedrooms = fields.Integer(default=2, string='Bedrooms') + living_area = fields.Integer(string='Living area') + facades = fields.Integer(string='Facades') + garage = fields.Boolean(string='Garage') + garden = fields.Boolean(string='Garden') + garden_area = fields.Integer(string='Garden area') + garden_orientation = fields.Selection(string='Orientation', + selection=format_selection(['north', 'south', 'east', 'west']), + ) + active = fields.Boolean(default=True, string='Active') + state = fields.Selection(string='State', + selection=format_selection( + ['new', 'offer received', 'offer accepted', 'sold', 'canceled']), + default='new') + + property_type_id = fields.Many2one('estate.property.type', string='Property type') + + buyer_id = fields.Many2one('res.partner', copy=False, string='Buyer') + salesperson_id = fields.Many2one('res.users', default=lambda self: self.env.user, string='Salesperson') + + tag_ids = Many2many('estate.property.tag', string='Tags') + offer_ids = One2many('estate.property.offer', 'property_id', string='Offers') + + total_area = fields.Integer(compute='_compute_total_area', string='Total area') + best_price = fields.Float(compute='_compute_best_price', string='Best offer price') + + @api.depends('living_area', 'garden_area') + def _compute_total_area(self): + for estate in self: + estate.total_area = estate.living_area + estate.garden_area + + @api.depends('offer_ids') + def _compute_best_price(self): + for estate in self: + estate.best_price = max(estate.offer_ids.mapped('price'), default=0) + + @api.onchange('garden') + def _onchange_garden(self): + self.garden = [0, 10][self.garden] + self.garden_orientation = ['', 'north'][self.garden] + + @api.constrains('selling_price') + def _check_price_offer_reasonable(self): + if float_compare(self.selling_price, 0.9 * self.expected_price, 3) <= 0: + raise ValidationError('The selling price must be at least 90% of the expected price.') + + @api.ondelete(at_uninstall=False) + def _unlink_property_if_not_new_nor_canceled(self): + self.ensure_one() + if self.state not in ('new', 'canceled'): + raise UserError('Only new and canceled properties can be deleted.') + + def action_sold(self): + self.ensure_one() + if self.state == 'canceled': + raise UserError('Canceled properties cannot be sold.') + self.state = 'sold' + return True + + def action_cancel(self): + self.ensure_one() + if self.state == 'sold': + raise UserError('Sold properties cannot be canceled.') + self.state = 'canceled' + return True diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py new file mode 100644 index 0000000000..7adb63b0f1 --- /dev/null +++ b/estate/models/estate_property_offer.py @@ -0,0 +1,58 @@ +from dateutil.relativedelta import relativedelta + +from odoo import api, fields, models +from odoo.exceptions import UserError + +from .helper import format_selection + + +class EstatePropertyOffer(models.Model): + _name = 'estate.property.offer' + _order = 'price desc' + _description = 'Offer modelisation' + _sql_constraints = [('offer_price_strictly_positive', 'CHECK(price > 0)', 'Offer price must be stricly positive')] + + price = fields.Float(string='Price', required=True) + status = fields.Selection(copy=False, selection=format_selection(['accepted', 'refused']), string='Status') + partner_id = fields.Many2one('res.partner', required=True, string='Buyer') + property_id = fields.Many2one('estate.property', required=True, string='Property') + + validity = fields.Integer(default=7, string="Validity") # in days + date_deadline = fields.Date(compute='_compute_deadline', inverse='_inverse_deadline', string='Deadline') + + property_type_id = fields.Many2one(related='property_id.property_type_id', string='Property type') + + @api.depends('validity') + def _compute_deadline(self): + for offer in self: + offer.date_deadline = fields.Date.today() + relativedelta(days=offer.validity) + + def _inverse_deadline(self): + for offer in self: + offer.validity = (offer.date_deadline - fields.Date.today()).days + + @api.model_create_multi + def create(self, vals_list): + for vals in vals_list: + property_id = self.env['estate.property'].browse(vals['property_id']) + if any(vals['price'] < offer.price for offer in property_id.offer_ids): + raise UserError('Offer price must be higher than existing offers.') + property_id.state = 'offer received' + return super().create(vals_list) + + def action_accept(self): + self.ensure_one() + if self.property_id.state == 'sold': + raise UserError('The house was already sold.') + self.status = 'accepted' + self.property_id.state = 'offer accepted' + self.property_id.buyer_id = self.partner_id + self.property_id.selling_price = self.price + return True + + def action_refuse(self): + for offer in self: + if offer.status == 'accepted': + raise UserError('The offer was already accepted.') + offer.status = 'refused' + return True diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py new file mode 100644 index 0000000000..b7d204dbf6 --- /dev/null +++ b/estate/models/estate_property_tag.py @@ -0,0 +1,11 @@ +from odoo import fields, models + + +class EstatePropertyTag(models.Model): + _name = 'estate.property.tag' + _order = 'name asc' + _description = 'Tag modelisation' + _sql_constraints = [('unique_name', 'unique(name)', 'Tag name must be unique')] + + name = fields.Char(required=True, string='Tag Name') + color = fields.Integer(string='Color Index') diff --git a/estate/models/estate_property_type.py b/estate/models/estate_property_type.py new file mode 100644 index 0000000000..4405dca5bf --- /dev/null +++ b/estate/models/estate_property_type.py @@ -0,0 +1,19 @@ +from odoo import fields, models + + +class EstatePropertyType(models.Model): + _name = 'estate.property.type' + _description = 'Estate Property Type modelisation' + _order = 'sequence asc, name asc' + _sql_constraints = [('unique_name', 'unique(name)', 'Type name must be unique')] + + name = fields.Char(required=True, string='Name') + property_ids = fields.One2many('estate.property', 'property_type_id', string='Properties') + sequence = fields.Integer(default=1) + + offer_ids = fields.One2many('estate.property.offer', 'property_type_id', string='Offers') + offer_count = fields.Integer(compute='_compute_offer_count', string='Offer Count') + + def _compute_offer_count(self): + for record in self: + record.offer_count = len(record.offer_ids) diff --git a/estate/models/helper.py b/estate/models/helper.py new file mode 100644 index 0000000000..fb9a179c0d --- /dev/null +++ b/estate/models/helper.py @@ -0,0 +1,2 @@ +def format_selection(l: list): + return [(field, field.title()) for field in l] diff --git a/estate/models/res_users.py b/estate/models/res_users.py new file mode 100644 index 0000000000..a658f77d3e --- /dev/null +++ b/estate/models/res_users.py @@ -0,0 +1,8 @@ +from odoo import fields, models + + +class Users(models.Model): + _inherit = 'res.users' + + property_ids = fields.One2many('estate.property', 'salesperson_id', + domain=[('state', 'not in', ['sold', 'canceled'])]) diff --git a/estate/views/estate_menus.xml b/estate/views/estate_menus.xml new file mode 100644 index 0000000000..9e9f344133 --- /dev/null +++ b/estate/views/estate_menus.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/estate/views/estate_property_offer_views.xml b/estate/views/estate_property_offer_views.xml new file mode 100644 index 0000000000..c4426b8010 --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,28 @@ + + + Property Offers + estate.property.offer + [('property_type_id', '=', active_id)] + list + + + + + estate.property.offer.list + estate.property.offer + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Properties Types + estate.property.type + list,form + + diff --git a/estate/views/estate_property_views.xml b/estate/views/estate_property_views.xml new file mode 100644 index 0000000000..8b727ebe48 --- /dev/null +++ b/estate/views/estate_property_views.xml @@ -0,0 +1,157 @@ + + + + estate.property.list + estate.property + + + + + + + + + + + + + + + + + estate.property.kanban + estate.property + + + + + +
+
+ +
+
+ Expected Price: + +
+
+ Best Offer Price: + +
+
+ Selling price: + +
+
+ +
+
+
+
+
+
+
+ + + estate.property.form + estate.property + +
+ +
+
+ + + +

+ +

+ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/awesome_owl/static/src/playground.js b/awesome_owl/static/src/playground.js index 657fb8b07b..37ee641e6c 100644 --- a/awesome_owl/static/src/playground.js +++ b/awesome_owl/static/src/playground.js @@ -1,7 +1,18 @@ /** @odoo-module **/ - -import { Component } from "@odoo/owl"; - +import { Component, markup, useState } from "@odoo/owl"; +import { Counter } from "./counter/counter"; +import { Card } from "./card/card"; +import { TodoList, TodoItem } from "./todo/todo"; export class Playground extends Component { static template = "awesome_owl.playground"; + static components = { Counter, Card, TodoItem, TodoList }; + html = markup("
some content
"); + + setup() { + this.state = useState({ sum: 2 }); + } + + incrementSum() { + this.state.sum++; + } } diff --git a/awesome_owl/static/src/playground.xml b/awesome_owl/static/src/playground.xml index 4fb905d59f..8e907284fe 100644 --- a/awesome_owl/static/src/playground.xml +++ b/awesome_owl/static/src/playground.xml @@ -1,10 +1,25 @@ - -
- hello world +
+
+ + +
+

+ +

+
+ +
+
+ + +
- - +
+ +
+ + \ No newline at end of file diff --git a/awesome_owl/static/src/todo/todo.js b/awesome_owl/static/src/todo/todo.js new file mode 100644 index 0000000000..3cd838ad55 --- /dev/null +++ b/awesome_owl/static/src/todo/todo.js @@ -0,0 +1,27 @@ +import { Component, useState } from "@odoo/owl"; + +export class TodoItem extends Component { + static template = "awesome_owl.todo.item"; + static props = { + todo: { + type: Object, + shape: { + id: { type: Number }, + description: { type: String }, + isCompleted: { type: Boolean }, + }, + }, + }; +} + +export class TodoList extends Component { + static template = "awesome_owl.todo.list"; + static components = { TodoItem }; + + setup() { + this.todos = useState([ + { id: 3, description: "buy milk", isCompleted: true }, + { id: 4, description: "buy butter", isCompleted: false }, + ]); + } +} diff --git a/awesome_owl/static/src/todo/todo.xml b/awesome_owl/static/src/todo/todo.xml new file mode 100644 index 0000000000..329dc8ae90 --- /dev/null +++ b/awesome_owl/static/src/todo/todo.xml @@ -0,0 +1,18 @@ + + + + + + + + + + +
+

+ : + +

+
+
+
\ No newline at end of file From 483d6d319c98b61251f94ee25f2c772ffb331795 Mon Sep 17 00:00:00 2001 From: "Oussama (ouou)" Date: Mon, 28 Oct 2024 14:30:58 +0100 Subject: [PATCH 3/4] [ADD] Add and remove element It is now possible to add and remove elements from the todo-list, additionaly, there is an autofocus on the input field. --- awesome_owl/static/src/playground.xml | 2 +- awesome_owl/static/src/todo/todo.js | 50 ++++++++++++++++++++++++--- awesome_owl/static/src/todo/todo.xml | 5 ++- awesome_owl/static/src/utils.js | 8 +++++ 4 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 awesome_owl/static/src/utils.js diff --git a/awesome_owl/static/src/playground.xml b/awesome_owl/static/src/playground.xml index 8e907284fe..4921a005d2 100644 --- a/awesome_owl/static/src/playground.xml +++ b/awesome_owl/static/src/playground.xml @@ -18,7 +18,7 @@
-
+
diff --git a/awesome_owl/static/src/todo/todo.js b/awesome_owl/static/src/todo/todo.js index 3cd838ad55..7b9c170927 100644 --- a/awesome_owl/static/src/todo/todo.js +++ b/awesome_owl/static/src/todo/todo.js @@ -1,4 +1,5 @@ import { Component, useState } from "@odoo/owl"; +import { useAutofocus } from "../utils"; export class TodoItem extends Component { static template = "awesome_owl.todo.item"; @@ -11,7 +12,22 @@ export class TodoItem extends Component { isCompleted: { type: Boolean }, }, }, + toggle_callback: { type: Function }, + remove_callback: { type: Function }, }; + + setup() { + this.onChange = this.onChange.bind(this); + this.onClick = this.onClick.bind(this); + } + + onChange() { + this.props.toggle_callback(this.props.todo.id); + } + + onClick() { + this.props.remove_callback(this.props.todo.id); + } } export class TodoList extends Component { @@ -19,9 +35,35 @@ export class TodoList extends Component { static components = { TodoItem }; setup() { - this.todos = useState([ - { id: 3, description: "buy milk", isCompleted: true }, - { id: 4, description: "buy butter", isCompleted: false }, - ]); + this.todos = useState([]); + this.addToDo = this.addToDo.bind(this); + this.toggleState = this.toggleState.bind(this); + this.removeTodo = this.removeTodo.bind(this); + + useAutofocus("input"); + } + + addToDo(event) { + let name = event.target.value; + if (event.keyCode === 13 && name.length) { + this.todos.push({ + id: this.todos.length + 1, + description: name, + isCompleted: false, + }); + event.target.value = ""; + } + } + + removeTodo(id) { + const index = this.todos.findIndex((todo) => todo.id === id); + if (index >= 0) { + this.todos.splice(index, 1); + } + } + + toggleState(id) { + console.log(this.todos[id - 1]); + this.todos[id - 1].isCompleted = !this.todos[id - 1].isCompleted; } } diff --git a/awesome_owl/static/src/todo/todo.xml b/awesome_owl/static/src/todo/todo.xml index 329dc8ae90..40e74fe200 100644 --- a/awesome_owl/static/src/todo/todo.xml +++ b/awesome_owl/static/src/todo/todo.xml @@ -2,16 +2,19 @@ + - +

+ : +

diff --git a/awesome_owl/static/src/utils.js b/awesome_owl/static/src/utils.js new file mode 100644 index 0000000000..043b7e105e --- /dev/null +++ b/awesome_owl/static/src/utils.js @@ -0,0 +1,8 @@ +import { useRef, onMounted } from "@odoo/owl"; + +export function useAutofocus(ref) { + let inputRef = useRef(ref); + onMounted(() => { + if (inputRef) inputRef.el.focus(); + }); +} From f963dce858a3ddf12a7393e18fc09405b23e46da Mon Sep 17 00:00:00 2001 From: "Oussama (ouou)" Date: Mon, 28 Oct 2024 15:03:13 +0100 Subject: [PATCH 4/4] [ADD] awesome_owl: the first part is done Added generic cards and minimizing --- awesome_owl/static/src/card/card.js | 13 +++++++++++-- awesome_owl/static/src/card/card.xml | 5 ++--- awesome_owl/static/src/counter/counter.xml | 2 +- awesome_owl/static/src/playground.xml | 6 ++++-- awesome_owl/static/src/todo/todo.js | 1 - 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/awesome_owl/static/src/card/card.js b/awesome_owl/static/src/card/card.js index 4b93c69561..4deb72eac8 100644 --- a/awesome_owl/static/src/card/card.js +++ b/awesome_owl/static/src/card/card.js @@ -1,9 +1,18 @@ -import { Component } from "@odoo/owl"; +import { Component, useState } from "@odoo/owl"; export class Card extends Component { static template = "awesome_owl.card"; static props = { title: { type: String }, - content: { type: String }, + slots: { type: Object, optional: true }, }; + + setup() { + this.state = useState({ showContent: true }); + this.minimizeContent = this.minimizeContent.bind(this); + } + + minimizeContent() { + this.state.showContent = !this.state.showContent; + } } diff --git a/awesome_owl/static/src/card/card.xml b/awesome_owl/static/src/card/card.xml index ee374cab2f..9f2a04210e 100644 --- a/awesome_owl/static/src/card/card.xml +++ b/awesome_owl/static/src/card/card.xml @@ -5,10 +5,9 @@
+
-

- -

+
diff --git a/awesome_owl/static/src/counter/counter.xml b/awesome_owl/static/src/counter/counter.xml index b5379929f0..80ac9ed724 100644 --- a/awesome_owl/static/src/counter/counter.xml +++ b/awesome_owl/static/src/counter/counter.xml @@ -5,7 +5,7 @@

Counter:

- +
\ No newline at end of file diff --git a/awesome_owl/static/src/playground.xml b/awesome_owl/static/src/playground.xml index 4921a005d2..a7bcbab8e5 100644 --- a/awesome_owl/static/src/playground.xml +++ b/awesome_owl/static/src/playground.xml @@ -13,8 +13,10 @@
- - + + + +
diff --git a/awesome_owl/static/src/todo/todo.js b/awesome_owl/static/src/todo/todo.js index 7b9c170927..d1807bf9a2 100644 --- a/awesome_owl/static/src/todo/todo.js +++ b/awesome_owl/static/src/todo/todo.js @@ -63,7 +63,6 @@ export class TodoList extends Component { } toggleState(id) { - console.log(this.todos[id - 1]); this.todos[id - 1].isCompleted = !this.todos[id - 1].isCompleted; } }