diff --git a/awesome_dashboard/__manifest__.py b/awesome_dashboard/__manifest__.py index a1cd72893d7..adb7542effb 100644 --- a/awesome_dashboard/__manifest__.py +++ b/awesome_dashboard/__manifest__.py @@ -22,6 +22,9 @@ 'views/views.xml', ], 'assets': { + 'awesome_dashboard.dashboard': [ + 'awesome_dashboard/static/src/dashboard/**/*', + ], 'web.assets_backend': [ 'awesome_dashboard/static/src/**/*', ], diff --git a/awesome_dashboard/controllers/controllers.py b/awesome_dashboard/controllers/controllers.py index 56d4a051287..05977d3bd7f 100644 --- a/awesome_dashboard/controllers/controllers.py +++ b/awesome_dashboard/controllers/controllers.py @@ -9,7 +9,7 @@ logger = logging.getLogger(__name__) class AwesomeDashboard(http.Controller): - @http.route('/awesome_dashboard/statistics', type='json', auth='user') + @http.route('/awesome_dashboard/statistics', type='jsonrpc', auth='user') def get_statistics(self): """ Returns a dict of statistics about the orders: diff --git a/awesome_dashboard/static/src/dashboard.js b/awesome_dashboard/static/src/dashboard.js deleted file mode 100644 index c4fb245621b..00000000000 --- a/awesome_dashboard/static/src/dashboard.js +++ /dev/null @@ -1,8 +0,0 @@ -import { Component } from "@odoo/owl"; -import { registry } from "@web/core/registry"; - -class AwesomeDashboard extends Component { - static template = "awesome_dashboard.AwesomeDashboard"; -} - -registry.category("actions").add("awesome_dashboard.dashboard", AwesomeDashboard); diff --git a/awesome_dashboard/static/src/dashboard.xml b/awesome_dashboard/static/src/dashboard.xml deleted file mode 100644 index 1a2ac9a2fed..00000000000 --- a/awesome_dashboard/static/src/dashboard.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - hello dashboard - - - diff --git a/awesome_dashboard/static/src/dashboard/configuration_dialog/configuration_dialog.js b/awesome_dashboard/static/src/dashboard/configuration_dialog/configuration_dialog.js new file mode 100644 index 00000000000..cacc3f548f3 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/configuration_dialog/configuration_dialog.js @@ -0,0 +1,33 @@ +import { Component } from "@odoo/owl"; +import { CheckBox } from "@web/core/checkbox/checkbox"; +import { Dialog } from "@web/core/dialog/dialog"; +import { registry } from "@web/core/registry"; +import dashboard_items from "../dashboard_items"; + +export class ConfigurationDialog extends Component { + static template = "awesome_dashboard.ConfigurationDialog"; + static components = { CheckBox, Dialog }; + static props = { + items: Array, + close: Function, + enabled_items: Array, + setEnabledItems: Function, + }; + + setup() { + this.all_items = this.props.enabled_items; + } + + toggleValue(item) { + if (this.all_items.includes(item)) { + this.all_items = this.all_items.filter(name => name !== item); + } else { + this.all_items = [...this.all_items, item]; + } + } + + onApply() { + this.props.setEnabledItems(this.all_items); + this.props.close(); + } +} diff --git a/awesome_dashboard/static/src/dashboard/configuration_dialog/configuration_dialog.xml b/awesome_dashboard/static/src/dashboard/configuration_dialog/configuration_dialog.xml new file mode 100644 index 00000000000..ecec5472aad --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/configuration_dialog/configuration_dialog.xml @@ -0,0 +1,27 @@ + + + + + +

Which cards do you wish to see?

+ + + + + + + + +
+
+ +
diff --git a/awesome_dashboard/static/src/dashboard/dashboard.js b/awesome_dashboard/static/src/dashboard/dashboard.js new file mode 100644 index 00000000000..00104198bee --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard.js @@ -0,0 +1,68 @@ +import { Component, onWillStart, reactive, useState } from "@odoo/owl"; +import { registry } from "@web/core/registry"; +import { useService } from "@web/core/utils/hooks"; +import { Layout } from "@web/search/layout"; +import { DashboardItem } from "./dashboard_item/dashboard_item"; +import { PieChart } from "./pie_chart/pie_chart"; +import { ConfigurationDialog } from "./configuration_dialog/configuration_dialog"; +import dashboard_items from "./dashboard_items"; + +class AwesomeDashboard extends Component { + static template = "awesome_dashboard.AwesomeDashboard"; + static components = { DashboardItem, Layout, PieChart }; + + setup() { + this.action = useService("action"); + this.state = reactive({ statistics: useService("statistics") }); + this.items = registry.category("awesome_dashboard").getAll(); + this.dialog = useService("dialog"); + this.context = useState({ enabled_items: this.getEnabledItems() }); + } + + getEnabledItems() { + // Open all items by default + const stored_items = JSON.parse( + localStorage.getItem("enabled_items") + ); + if (!stored_items) { + const all_items = this.items.map((item) => item.id); + this.setEnabledItems(all_items); + return all_items; + } + return stored_items || []; + } + + setEnabledItems(values) { + localStorage.setItem("enabled_items", JSON.stringify(values)); + this.context.enabled_items = values; + } + + openLeads() { + this.action.doAction({ + type: "ir.actions.act_window", + name: "Leads", + res_model: "crm.lead", + views: [ + [false, 'list'], + [false, 'form'], + ], + }); + } + + openCustomers() { + this.action.doAction("base.action_partner_form"); + } + + openConfiguration() { + this.dialog.add( + ConfigurationDialog, + { + items: this.items, + enabled_items: this.context.enabled_items, + setEnabledItems: this.setEnabledItems.bind(this), + }, + ); + } +} + +registry.category("lazy_components").add("AwesomeDashboard", AwesomeDashboard); diff --git a/awesome_dashboard/static/src/dashboard/dashboard.scss b/awesome_dashboard/static/src/dashboard/dashboard.scss new file mode 100644 index 00000000000..32862ec0d82 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard.scss @@ -0,0 +1,3 @@ +.o_dashboard { + background-color: gray; +} diff --git a/awesome_dashboard/static/src/dashboard/dashboard.xml b/awesome_dashboard/static/src/dashboard/dashboard.xml new file mode 100644 index 00000000000..d055c5a99ba --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + +
+ + + + + + +
+
+
+ +
diff --git a/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_item.js b/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_item.js new file mode 100644 index 00000000000..68be9b5d6f3 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_item.js @@ -0,0 +1,8 @@ +import { Component } from "@odoo/owl"; + +export class DashboardItem extends Component { + static template = "awesome_dashboard.DashboardItem"; + static props = { + size: { type: Number, default: 1 }, + } +} diff --git a/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_item.xml b/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_item.xml new file mode 100644 index 00000000000..51456739b59 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_item.xml @@ -0,0 +1,22 @@ + + + + +
+
+
+
+ + Some content + +
+

+ +

+
+
+
+
+ +
+ diff --git a/awesome_dashboard/static/src/dashboard/dashboard_items.js b/awesome_dashboard/static/src/dashboard/dashboard_items.js new file mode 100644 index 00000000000..2f55839c8e0 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard_items.js @@ -0,0 +1,70 @@ +import { registry } from "@web/core/registry"; +import { PieChartCard } from "./pie_chart_card/pie_chart_card"; +import { NumberCard } from "./number_card/number_card"; + +const items = [ + { + id: "average_quantity", + description: "Average amount of t-shirt", + size: 1, + Component: NumberCard, + props: (data) => ({ + title: "Average amount of t-shirt by order this month", + value: data.average_quantity, + }), + }, + { + id: "average_time", + description: "Average order time", + size: 2, + Component: NumberCard, + props: (data) => ({ + title: "Average time for an order to go from 'new' to 'sent' or 'cancelled'", + value: data.average_time, + }), + }, + { + id: "nb_new_orders", + description: "New orders", + size: 2, + Component: NumberCard, + props: (data) => ({ + title: "Number of new orders this month", + value: data.nb_new_orders, + }), + }, + { + id: "nb_cancelled_orders", + description: "Average amount of t-shirt", + size: 1, + Component: NumberCard, + props: (data) => ({ + title: "Number of cancelled orders this month", + value: data.nb_cancelled_orders, + }), + }, + { + id: "total_amount", + description: "Total new orders", + size: 1, + Component: NumberCard, + props: (data) => ({ + title: "Total amount of new orders this month", + value: data.total_amount, + }), + }, + { + id: "orders_by_size", + description: "Shirt orders by size", + size: 2, + Component: PieChartCard, + props: (data) => ({ + title: "Shirt orders by size", + value: data.orders_by_size, + }), + }, +] + +items.forEach(item => { + registry.category("awesome_dashboard").add(item.id, item); +}); diff --git a/awesome_dashboard/static/src/dashboard/number_card/number_card.js b/awesome_dashboard/static/src/dashboard/number_card/number_card.js new file mode 100644 index 00000000000..3296d42cb9b --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/number_card/number_card.js @@ -0,0 +1,9 @@ +import { Component } from "@odoo/owl"; + +export class NumberCard extends Component { + static template = "awesome_dashboard.NumberCard"; + static props = { + title: String, + value: Number, + } +} diff --git a/awesome_dashboard/static/src/dashboard/number_card/number_card.xml b/awesome_dashboard/static/src/dashboard/number_card/number_card.xml new file mode 100644 index 00000000000..cd78cac70cb --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/number_card/number_card.xml @@ -0,0 +1,16 @@ + + + + +
+
+ +
+

+ +

+
+
+ +
+ diff --git a/awesome_dashboard/static/src/dashboard/pie_chart/pie_chart.js b/awesome_dashboard/static/src/dashboard/pie_chart/pie_chart.js new file mode 100644 index 00000000000..3fd8d3148f8 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/pie_chart/pie_chart.js @@ -0,0 +1,63 @@ +import { + Component, + onWillStart, + onWillUnmount, + useRef, + useEffect, +} from "@odoo/owl"; +import { loadJS } from "@web/core/assets"; + +export class PieChart extends Component { + static template = "awesome_dashboard.PieChart"; + static props = { + data: { default: {}, type: Object }, + } + + setup() { + this.rootRef = useRef("root"); + this.canvasRef = useRef("canvas"); + this.containerRef = useRef("container"); + + this.chart = null; + this.tooltip = null; + this.legendTooltip = null; + + onWillStart(async () => { + await loadJS("/web/static/lib/Chart/Chart.js") + }); + + useEffect(() => { + this.renderChart(); + }); + + onWillUnmount(this.onWillUnmount); + } + + onWillUnmount() { + if (this.chart) { + this.chart.destroy(); + } + } + + renderChart() { + if (this.chart) { + this.chart.destroy(); + } + + const labels = Object.keys(this.props.data); + const values = Object.values(this.props.data); + + const config = { + type: 'pie', + data: { + labels: labels, + datasets: [{ + label: "Orders by size", + data: values, + }], + }, + } + + this.chart = new Chart(this.canvasRef.el, config); + } +} diff --git a/awesome_dashboard/static/src/dashboard/pie_chart/pie_chart.xml b/awesome_dashboard/static/src/dashboard/pie_chart/pie_chart.xml new file mode 100644 index 00000000000..c55ce92b62c --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/pie_chart/pie_chart.xml @@ -0,0 +1,11 @@ + + + + +
+ +
+
+ +
+ diff --git a/awesome_dashboard/static/src/dashboard/pie_chart_card/pie_chart_card.js b/awesome_dashboard/static/src/dashboard/pie_chart_card/pie_chart_card.js new file mode 100644 index 00000000000..8ae3e7b8d15 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/pie_chart_card/pie_chart_card.js @@ -0,0 +1,11 @@ +import { Component } from "@odoo/owl"; +import { PieChart } from "../pie_chart/pie_chart"; + +export class PieChartCard extends Component { + static template = "awesome_dashboard.PieChartCard"; + static components = { PieChart }; + static props = { + title: String, + value: Object, + } +} diff --git a/awesome_dashboard/static/src/dashboard/pie_chart_card/pie_chart_card.xml b/awesome_dashboard/static/src/dashboard/pie_chart_card/pie_chart_card.xml new file mode 100644 index 00000000000..31c745b689e --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/pie_chart_card/pie_chart_card.xml @@ -0,0 +1,16 @@ + + + + +
+
+ +
+
+ +
+
+
+ +
+ diff --git a/awesome_dashboard/static/src/dashboard/statistics_service.js b/awesome_dashboard/static/src/dashboard/statistics_service.js new file mode 100644 index 00000000000..7e88b3595cf --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/statistics_service.js @@ -0,0 +1,21 @@ +import { rpc } from "@web/core/network/rpc"; +import { registry } from "@web/core/registry"; +import { memoize } from "@web/core/utils/functions"; + +export async function loadStatistic() { + return await rpc("/awesome_dashboard/statistics"); +} + +export const statisticsService = { + async start() { + let data = await loadStatistic(); + + setInterval(async () => { + data = await loadStatistic(); + }, 10 * 60 * 1000); + + return data; + } +} + +registry.category("services").add("statistics", statisticsService); diff --git a/awesome_dashboard/static/src/dashboard_action.js b/awesome_dashboard/static/src/dashboard_action.js new file mode 100644 index 00000000000..27a8fa94cb2 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard_action.js @@ -0,0 +1,12 @@ +import { Component, xml } from "@odoo/owl"; +import { registry } from "@web/core/registry"; +import { LazyComponent } from "@web/core/assets"; + +export class DashboardLoader extends Component { + static components = { LazyComponent }; + static template = xml` + + `; +} + +registry.category("actions").add("awesome_dashboard.dashboard", DashboardLoader); diff --git a/awesome_owl/__manifest__.py b/awesome_owl/__manifest__.py index e8ac1cda552..55002ab81de 100644 --- a/awesome_owl/__manifest__.py +++ b/awesome_owl/__manifest__.py @@ -29,8 +29,10 @@ 'assets': { 'awesome_owl.assets_playground': [ ('include', 'web._assets_helpers'), + ('include', 'web._assets_backend_helpers'), 'web/static/src/scss/pre_variables.scss', 'web/static/lib/bootstrap/scss/_variables.scss', + 'web/static/lib/bootstrap/scss/_maps.scss', ('include', 'web._assets_bootstrap'), ('include', 'web._assets_core'), 'web/static/src/libs/fontawesome/css/font-awesome.css', diff --git a/awesome_owl/static/src/card/card.js b/awesome_owl/static/src/card/card.js new file mode 100644 index 00000000000..e3ae539f3fd --- /dev/null +++ b/awesome_owl/static/src/card/card.js @@ -0,0 +1,20 @@ +import { Component, useState } from "@odoo/owl"; + +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({ open: true }); + } + + toggleOpen() { + this.state.open = !this.state.open; + } +} + +export default Card; diff --git a/awesome_owl/static/src/card/card.xml b/awesome_owl/static/src/card/card.xml new file mode 100644 index 00000000000..3a2c66b06e2 --- /dev/null +++ b/awesome_owl/static/src/card/card.xml @@ -0,0 +1,21 @@ + + + + +
+
+
+
+ +
+
+

+ Default content +

+
+
+
+ +
diff --git a/awesome_owl/static/src/counter/counter.js b/awesome_owl/static/src/counter/counter.js new file mode 100644 index 00000000000..aa46aac1151 --- /dev/null +++ b/awesome_owl/static/src/counter/counter.js @@ -0,0 +1,19 @@ +import { Component, useState } from "@odoo/owl"; + +class Counter extends Component { + static template = "awesome_owl.counter"; + static props = { + onChange: {type: Function, optional: true}, + } + + setup() { + this.state = useState({ value: 0 }); + } + + increment() { + this.state.value++; + this.props.onChange?.(); + } +} + +export default Counter; diff --git a/awesome_owl/static/src/counter/counter.xml b/awesome_owl/static/src/counter/counter.xml new file mode 100644 index 00000000000..73adf71f83a --- /dev/null +++ b/awesome_owl/static/src/counter/counter.xml @@ -0,0 +1,16 @@ + + + + +
+
Counter:
+ +
+
+ +
diff --git a/awesome_owl/static/src/playground.js b/awesome_owl/static/src/playground.js index 4ac769b0aa5..7c1473e6e6f 100644 --- a/awesome_owl/static/src/playground.js +++ b/awesome_owl/static/src/playground.js @@ -1,5 +1,20 @@ -import { Component } from "@odoo/owl"; +import { Component, markup, useState } from "@odoo/owl"; +import Counter from "./counter/counter"; +import Card from "./card/card"; +import TodoList from "./todo_list/todo_list"; export class Playground extends Component { static template = "awesome_owl.playground"; + static components = { Counter, Card, TodoList }; + + setup() { + this.state = useState({ sum: 0 }); + } + + incrementSum() { + this.state.sum++; + } + + card_content = "
content
" + card2_content = markup("
content
") } diff --git a/awesome_owl/static/src/playground.xml b/awesome_owl/static/src/playground.xml index 4fb905d59f9..1432b34aa30 100644 --- a/awesome_owl/static/src/playground.xml +++ b/awesome_owl/static/src/playground.xml @@ -3,7 +3,30 @@
- hello world + Hello World + +
+ + +

The sum is:

+
+ +
+ + + Hello + + + + +
+ +
> + +
diff --git a/awesome_owl/static/src/todo_list/todo_item.js b/awesome_owl/static/src/todo_list/todo_item.js new file mode 100644 index 00000000000..e7119aa36c6 --- /dev/null +++ b/awesome_owl/static/src/todo_list/todo_item.js @@ -0,0 +1,12 @@ +import { Component } from "@odoo/owl"; + +class TodoItem extends Component { + static template = "awesome_owl.todo_item" + static props = { + item: { optional: true }, + toggleState: { type: Function }, + removeTodo: { type: Function }, + } +} + +export default TodoItem; diff --git a/awesome_owl/static/src/todo_list/todo_item.xml b/awesome_owl/static/src/todo_list/todo_item.xml new file mode 100644 index 00000000000..034bc9025e8 --- /dev/null +++ b/awesome_owl/static/src/todo_list/todo_item.xml @@ -0,0 +1,27 @@ + + + + +
+ + . + + +
+
+ +
diff --git a/awesome_owl/static/src/todo_list/todo_list.js b/awesome_owl/static/src/todo_list/todo_list.js new file mode 100644 index 00000000000..cc62a703c36 --- /dev/null +++ b/awesome_owl/static/src/todo_list/todo_list.js @@ -0,0 +1,48 @@ +import { Component, useState } from "@odoo/owl"; +import TodoItem from "./todo_item"; +import { useAutofocus } from "../utils"; + +class TodoList extends Component { + static template = "awesome_owl.todo_list" + static components = { TodoItem }; + static props = {} + + setup() { + this.state = useState({ todos: [], counter: 0 }); + this.inputRef = useAutofocus({ refName: "input" }); + this.removeTodo = this.removeTodo.bind(this); + this.toggleState = this.toggleState.bind(this); + } + + addTodo(event) { + if (event.keyCode === 13) { + const value = event.target.value; + if (value.length) { + this.state.counter++; + this.state.todos = [ + ...this.state.todos, + { + id: this.state.counter, + description: event.target.value, + isCompleted: false, + }, + ]; + event.target.value = ""; + } + } + } + + toggleState(targetId) { + this.state.todos = this.state.todos.map(todo => ( + todo.id === targetId ? { ...todo, isCompleted: !todo.isCompleted } : todo + )); + } + + removeTodo(targetId) { + this.state.todos = this.state.todos.filter( + ({ id }) => id !== targetId + ); + } +} + +export default TodoList; diff --git a/awesome_owl/static/src/todo_list/todo_list.xml b/awesome_owl/static/src/todo_list/todo_list.xml new file mode 100644 index 00000000000..2e1cfa48e56 --- /dev/null +++ b/awesome_owl/static/src/todo_list/todo_list.xml @@ -0,0 +1,22 @@ + + + + +
+

ToDo List ()

+ + + + +
+
+ +
diff --git a/awesome_owl/static/src/utils.js b/awesome_owl/static/src/utils.js new file mode 100644 index 00000000000..d1c886c763d --- /dev/null +++ b/awesome_owl/static/src/utils.js @@ -0,0 +1,11 @@ +import { useEffect, useRef } from "@odoo/owl"; + +export function useAutofocus({ refName } = {}) { + const ref = useRef(refName || "autofocus"); + + useEffect((el) => { + el?.focus(); + }, () => [ref.el]); + + return ref; +} diff --git a/estate/__init__.py b/estate/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /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 00000000000..ceb98e83436 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,21 @@ +{ + 'name': 'Real Estate', + 'license': 'LGPL-3', + 'version': '1.0', + 'depends': ['base'], + 'author': 'Odoo S.A.', + 'category': 'Category', + 'description': """ + Real Estate Advertisement module + """, + 'application': True, + '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_user_views.xml', + 'views/estate_menus.xml', + ], +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..089ce4cd63e --- /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 estate_user diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py new file mode 100644 index 00000000000..2cca25ef41a --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,120 @@ +from odoo import api, fields, models +from odoo.exceptions import UserError, ValidationError +from odoo.tools.float_utils import float_is_zero, float_compare + + +class EstateProperty(models.Model): + _name = 'estate.property' + _description = 'Estate Properties' + _order = 'id desc' + + name = fields.Char(required=True) + description = fields.Text() + postcode = fields.Char() + date_availability = fields.Date( + copy=False, + default=fields.Date.add(fields.Date.today(), months=3), + ) + expected_price = fields.Float(required=True) + selling_price = fields.Float(readonly=True, copy=False) + best_price = fields.Float(compute='_compute_best_price') + bedrooms = fields.Integer(default=2) + living_area = fields.Integer() + garden_area = fields.Integer() + total_area = fields.Integer(compute='_compute_total_area') + facades = fields.Integer() + garage = fields.Boolean() + garden = fields.Boolean() + garden_orientation = fields.Selection( + string='Garden orientation', + selection=[ + ('north', 'North'), + ('south', 'South'), + ('east', 'East'), + ('west', 'West'), + ], + ) + active = fields.Boolean(default=True) + state = fields.Selection( + string='State', + required=True, + default='new', + selection=[ + ('new', 'New'), + ('offer_received', 'Offer Received'), + ('offer_accepted', 'Offer Accepted'), + ('sold', 'Sold'), + ('cancelled', 'Cancelled'), + ], + ) + 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') + + _check_expected_price = models.Constraint( + 'CHECK(expected_price > 0)', + 'The expected price of a property should be strictly positive.', + ) + _check_selling_price = models.Constraint( + 'CHECK(selling_price >= 0)', + 'The selling price of a property should be positive.', + ) + + @api.depends('offer_ids') + def _compute_best_price(self): + for record in self: + record.best_price = max(record.offer_ids.mapped('price'), default=0.0) + + @api.depends('living_area', 'garden_area') + def _compute_total_area(self): + for record in self: + living_area = record.living_area or 0 + garden_area = record.garden_area or 0 + record.total_area = living_area + garden_area + + @api.constrains('selling_price', 'expected_price') + def _check_selling_price(self): + for record in self: + if ( + not float_is_zero(record.selling_price, 2) + and float_compare(record.selling_price, record.expected_price * 0.90, 2) < 0 + ): + raise ValidationError("Selling price cannot be lower than 90% of expected price") + + @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 = None + + @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(f"Can't delete property in {record.state} state") + + def action_mark_as_sold(self): + for record in self: + if record.state == 'cancelled': + raise UserError('Cannot sell cancelled properties') + + record.state = 'sold' + + def action_mark_as_cancelled(self): + for record in self: + if record.state == 'sold': + raise UserError('Cannot cancel sold properties') + + record.state = 'cancelled' diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py new file mode 100644 index 00000000000..9401c961cc6 --- /dev/null +++ b/estate/models/estate_property_offer.py @@ -0,0 +1,70 @@ +from odoo import api, fields, models +from odoo.exceptions import UserError + + +class EstatePropertyOffer(models.Model): + _name = 'estate.property.offer' + _description = 'Estate Offers' + _order = 'price desc' + + price = fields.Float(required=True) + status = fields.Selection( + string='Status', + selection=[ + ('accepted', 'Accepted'), + ('refused', 'Refused'), + ], + ) + partner_id = fields.Many2one('res.partner', string='Partner') + property_id = fields.Many2one('estate.property', string='Property') + 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) + + _check_price = models.Constraint( + 'CHECK(price > 0)', + 'The price of an offer should be strictly positive.', + ) + + @api.depends('create_date', 'validity') + def _compute_date_deadline(self): + for record in self: + created_date = record.create_date or fields.Date.today() + record.date_deadline = fields.Date.add( + created_date, days=record.validity, + ) + + def _inverse_date_deadline(self): + for record in self: + created_date = record.create_date.date() or fields.Date.today() + record.validity = (record.date_deadline - created_date).days + + @api.model + def create(self, val_lists): + for vals in val_lists: + linked_property = self.env['estate.property'].browse(vals['property_id']) + if vals['price'] < linked_property.best_price: + raise UserError('Offer price cannot be lower than existing offers') + + linked_property.state = 'offer_received' + return super().create(val_lists) + + def action_mark_as_accepted(self): + for record in self: + if ( + record.property_id.state not in ('new', 'offer_received') + ): + raise UserError('Cannot accept offer in this state') + + record.status = 'accepted' + record.property_id.selling_price = record.price + record.property_id.buyer_id = record.partner_id + record.property_id.state = 'offer_accepted' + + def action_mark_as_refused(self): + for record in self: + if record.status == 'accepted': + record.property_id.state = 'offer_received' + record.property_id.selling_price = 0.0 + record.property_id.buyer_id = None + record.status = 'refused' diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py new file mode 100644 index 00000000000..68fac921654 --- /dev/null +++ b/estate/models/estate_property_tag.py @@ -0,0 +1,15 @@ +from odoo import fields, models + + +class EstatePropertyTag(models.Model): + _name = 'estate.property.tag' + _description = 'Estate Property Tags' + _order = 'name asc' + + name = fields.Char(required=True) + color = fields.Integer() + + _check_unique_name = models.Constraint( + 'unique (name)', + 'The name of a tag should be unique.', + ) diff --git a/estate/models/estate_property_type.py b/estate/models/estate_property_type.py new file mode 100644 index 00000000000..bc1506ae342 --- /dev/null +++ b/estate/models/estate_property_type.py @@ -0,0 +1,22 @@ +from odoo import api, fields, models + + +class EstatePropertyType(models.Model): + _name = 'estate.property.type' + _description = 'Estate Property Types' + _order = 'name asc' + + name = fields.Char(required=True) + property_ids = fields.One2many( + 'estate.property', 'property_type_id', string="Properties", + ) + sequence = fields.Integer('Sequence', default=1, help="Used to order property types.") + offer_ids = fields.One2many( + 'estate.property.offer', 'property_type_id', string="Offers", + ) + offer_count = fields.Integer(compute="_compute_offer_count") + + @api.depends('offer_ids') + def _compute_offer_count(self): + for record in self: + record.offer_count = len(record.offer_ids) diff --git a/estate/models/estate_user.py b/estate/models/estate_user.py new file mode 100644 index 00000000000..fc02e1990a2 --- /dev/null +++ b/estate/models/estate_user.py @@ -0,0 +1,9 @@ +from odoo import fields, models + + +class EstateUser(models.Model): + _inherit = 'res.users' + + property_ids = fields.One2many( + 'estate.property', 'salesperson_id', string='Real Estate Properties', + ) diff --git a/estate/security/ir.model.access.csv b/estate/security/ir.model.access.csv new file mode 100644 index 00000000000..89f97c50842 --- /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 +access_estate_property,access_estate_property,model_estate_property,base.group_user,1,1,1,1 +access_estate_property_type,access_estate_property_type,model_estate_property_type,base.group_user,1,1,1,1 +access_estate_property_tag,access_estate_property_tag,model_estate_property_tag,base.group_user,1,1,1,1 +access_estate_property_offer,access_estate_property_offer,model_estate_property_offer,base.group_user,1,1,1,1 diff --git a/estate/views/estate_menus.xml b/estate/views/estate_menus.xml new file mode 100644 index 00000000000..9be236faae9 --- /dev/null +++ b/estate/views/estate_menus.xml @@ -0,0 +1,11 @@ + + + diff --git a/estate/views/estate_property_offer_views.xml b/estate/views/estate_property_offer_views.xml new file mode 100644 index 00000000000..4d5573e76a7 --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,59 @@ + + + Property Offers + estate.property.offer + list,form + [('property_type_id', '=', active_id)] + + + + estate.property.offer.list + estate.property.offer + + + + + + +