diff --git a/awesome_dashboard/__manifest__.py b/awesome_dashboard/__manifest__.py index a1cd72893d7..6c152c3bad0 100644 --- a/awesome_dashboard/__manifest__.py +++ b/awesome_dashboard/__manifest__.py @@ -24,7 +24,11 @@ 'assets': { 'web.assets_backend': [ 'awesome_dashboard/static/src/**/*', + ('remove', 'awesome_dashboard/static/src/dashboard/*'), ], + 'awesome_dashboard.dashboard': [ + 'awesome_dashboard/static/src/dashboard/**/*', + ] }, 'license': 'AGPL-3' } 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/dashboard.js b/awesome_dashboard/static/src/dashboard/dashboard.js new file mode 100644 index 00000000000..f1ac1809295 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard.js @@ -0,0 +1,89 @@ +import { Component, useState } from "@odoo/owl"; +import { CheckBox } from "@web/core/checkbox/checkbox"; +import { Dialog } from "@web/core/dialog/dialog"; +import { browser } from "@web/core/browser/browser"; +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 { NumberCard } from "./number_card/number_card"; +import { PieChartCard } from "./pie_chart_card/pie_chart_card"; + +class AwesomeDashboard extends Component { + static template = "awesome_dashboard.AwesomeDashboard"; + static components = { Layout, DashboardItem, NumberCard, PieChartCard }; + + setup() { + this.items = registry.category("awesome_dashboard").getAll(); + this.action = useService("action"); + this.stats = useState(useService("awesome_dashboard.statistics")); + this.dialog = useService("dialog"); + this.hiddenItemIds = useState( + browser.localStorage.getItem("hiddenDashboardItemIds")?.split(",") || [], + ); + } + + openCustomers() { + this.action.doAction("base.action_partner_form"); + } + + async openLeads() { + this.action.doAction({ + type: "ir.actions.act_window", + name: "All Leads", + res_model: "crm.lead", + views: [ + [false, "list"], + [false, "form"], + ], + }); + } + + openConfig() { + this.dialog.add(ConfigDialog, { + items: this.items, + hiddenItemIds: this.hiddenItemIds, + onUpdateConfig: this.updateConfig.bind(this), + }); + } + + updateConfig(newHiddenItemIds) { + this.hiddenItemIds = newHiddenItemIds; + } +} + +class ConfigDialog extends Component { + static template = "awesome_dashboard.ConfigDialog"; + static components = { Dialog, CheckBox }; + static props = { + close: Function, + items: { type: Array, elements: Object }, + hiddenItemIds: { type: Array, elements: String }, + onUpdateConfig: { type: Function, optional: true }, + }; + + setup() { + this.items = useState( + this.props.items.map((item) => ({ + ...item, + displayed: !this.props.hiddenItemIds.includes(item.id), + })), + ); + } + + onChange(checked, itemToChange) { + itemToChange.displayed = checked; + const newHiddenItemIds = this.items + .filter((item) => !item.displayed) + .map((item) => item.id); + + browser.localStorage.setItem("hiddenDashboardItemIds", newHiddenItemIds); + this.props.onUpdateConfig?.(newHiddenItemIds); + } + + done() { + this.props.close(); + } +} + +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..fedf54c0b9d --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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..1b0f3997e15 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_item.js @@ -0,0 +1,16 @@ +import { Component } from "@odoo/owl"; + +export class DashboardItem extends Component { + static template = "awesome_dashboard.DashboardItem"; + static props = { + size: { type: Number, optional: true }, + slots: { + type: Object, + shape: { default: Object }, + }, + }; + + static defaultProps = { + size: 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..1a0790200ab --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_item.xml @@ -0,0 +1,11 @@ + + + +
+
+ +
+
+
+ +
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..18239f8da3b --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard_items.js @@ -0,0 +1,65 @@ +import { registry } from "@web/core/registry"; +import { NumberCard } from "./number_card/number_card"; +import { PieChartCard } from "./pie_chart_card/pie_chart_card"; + +const items = [ + { + id: "nb_new_orders", + description: "Number of new orders", + Component: NumberCard, + props: (data) => ({ + title: "Number of new orders this month", + value: data.nb_new_orders, + }), + }, + { + id: "total_amount", + description: "Total amount of new orders", + Component: NumberCard, + props: (data) => ({ + title: "Total amount of new orders this month", + value: data.total_amount, + }), + }, + { + id: "average_quantity", + description: "Average amount of T-shirt", + Component: NumberCard, + props: (data) => ({ + title: "Average amount of T-shirt per order this month", + value: data.average_quantity, + }), + }, + { + id: "nb_cancelled_orders", + description: "Number of canceled orders", + Component: NumberCard, + props: (data) => ({ + title: "Number of canceled orders this month", + value: data.nb_cancelled_orders, + }), + }, + { + id: "average_time", + description: "Average order time", + Component: NumberCard, + props: (data) => ({ + title: "Average time for an order to go from 'new' to 'sent' or 'canceled'", + value: data.average_time, + }), + }, + { + id: "orders_by_size", + description: "Orders by size", + Component: PieChartCard, + size: 2, + props: (data) => ({ + title: "T-shirt orders by size", + stats: 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..0d1ae8deadf --- /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..fbdd626bc7c --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/number_card/number_card.xml @@ -0,0 +1,10 @@ + + + +
+
+ +
+
+ +
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..164c6c9e41a --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/pie_chart/pie_chart.js @@ -0,0 +1,28 @@ +import { Component, onWillStart, onWillUnmount, useEffect, useRef } from "@odoo/owl"; +import { loadJS } from "@web/core/assets"; + +export class PieChart extends Component { + static template = "awesome_dashboard.PieChart"; + static props = { + data: Object, + }; + + setup() { + this.canvasRef = useRef("canvas"); + onWillStart(() => loadJS("/web/static/lib/Chart/Chart.js")); + useEffect(() => this.renderChart()); + onWillUnmount(() => this.chart?.destroy()); + } + + renderChart() { + this.chart?.destroy(); + const ctx = this.canvasRef.el; + this.chart = new Chart(ctx, { + type: "doughnut", + data: { + datasets: [{ data: Object.values(this.props.data) }], + labels: Object.keys(this.props.data), + }, + }); + } +} 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..697b3287b6e --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/pie_chart/pie_chart.xml @@ -0,0 +1,7 @@ + + + + + + + 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..bdbdd26ea9a --- /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, + stats: 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..e9224b20db2 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/pie_chart_card/pie_chart_card.xml @@ -0,0 +1,8 @@ + + + +
+ +
+ +
diff --git a/awesome_dashboard/static/src/dashboard_action.js b/awesome_dashboard/static/src/dashboard_action.js new file mode 100644 index 00000000000..21c4e8cd52c --- /dev/null +++ b/awesome_dashboard/static/src/dashboard_action.js @@ -0,0 +1,10 @@ +import { Component } from "@odoo/owl"; +import { registry } from "@web/core/registry"; +import { LazyComponent } from "@web/core/assets"; + +class DashboardLoader extends Component { + static template = "awesome_dashboard.DashboardLoader"; + static components = { LazyComponent }; +} + +registry.category("actions").add("awesome_dashboard.dashboard", DashboardLoader); diff --git a/awesome_dashboard/static/src/dashboard_action.xml b/awesome_dashboard/static/src/dashboard_action.xml new file mode 100644 index 00000000000..b5502cdefc7 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard_action.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/awesome_dashboard/static/src/statistics_service.js b/awesome_dashboard/static/src/statistics_service.js new file mode 100644 index 00000000000..840a9b82ace --- /dev/null +++ b/awesome_dashboard/static/src/statistics_service.js @@ -0,0 +1,19 @@ +import { reactive } from "@odoo/owl"; +import { registry } from "@web/core/registry"; +import { rpc } from "@web/core/network/rpc"; + +const statisticsService = { + start() { + const statistics = reactive({ isReady: false }); + async function loadStatistics() { + const updates = await rpc("/awesome_dashboard/statistics"); + Object.assign(statistics, updates, { isReady: true }); + } + + setInterval(loadStatistics, 10000); + loadStatistics(); + return statistics; + }, +}; + +registry.category("services").add("awesome_dashboard.statistics", statisticsService); diff --git a/awesome_owl/static/src/card/card.js b/awesome_owl/static/src/card/card.js new file mode 100644 index 00000000000..68183fc32cc --- /dev/null +++ b/awesome_owl/static/src/card/card.js @@ -0,0 +1,20 @@ +import { Component, useState } from "@odoo/owl"; + +export class Card extends Component { + static template = "awesome_owl.card"; + static props = { + title: String, + slots: { + type: Object, + shape: {default: Object} + } + } + + setup() { + this.state = useState({ open: true }); + } + + toggleOpen() { + this.state.open = !this.state.open; + } +} diff --git a/awesome_owl/static/src/card/card.xml b/awesome_owl/static/src/card/card.xml new file mode 100644 index 00000000000..e4cd318dc4f --- /dev/null +++ b/awesome_owl/static/src/card/card.xml @@ -0,0 +1,17 @@ + + + +
+
+
+ + +
+

+ +

+
+
+
+ +
diff --git a/awesome_owl/static/src/counter/counter.css b/awesome_owl/static/src/counter/counter.css new file mode 100644 index 00000000000..2a772c30d1d --- /dev/null +++ b/awesome_owl/static/src/counter/counter.css @@ -0,0 +1,6 @@ +.counter { + margin: 0 1rem; + border: 1px solid black; + padding: 0.5rem 1rem; + width: fit-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..f5e371c5a3d --- /dev/null +++ b/awesome_owl/static/src/counter/counter.js @@ -0,0 +1,15 @@ +import { Component, useState } from "@odoo/owl"; + +export class Counter extends Component { + static template = "awesome_owl.counter"; + static props = ["onChange?"] + + setup() { + this.state = useState({ value: 0}); + } + + increment() { + this.state.value++; + this.props.onChange?.(); + } +} diff --git a/awesome_owl/static/src/counter/counter.xml b/awesome_owl/static/src/counter/counter.xml new file mode 100644 index 00000000000..bf8aa3d556d --- /dev/null +++ b/awesome_owl/static/src/counter/counter.xml @@ -0,0 +1,12 @@ + + + +
+ Counter: + +
+
+ +
diff --git a/awesome_owl/static/src/hooks.js b/awesome_owl/static/src/hooks.js new file mode 100644 index 00000000000..c356ae980af --- /dev/null +++ b/awesome_owl/static/src/hooks.js @@ -0,0 +1,5 @@ +import { onMounted, useRef } from "@odoo/owl"; +export function useAutoFocus(ref_name) { + const ref = useRef(ref_name); + onMounted(() => ref.el.focus()) +} diff --git a/awesome_owl/static/src/playground.css b/awesome_owl/static/src/playground.css new file mode 100644 index 00000000000..02de697126e --- /dev/null +++ b/awesome_owl/static/src/playground.css @@ -0,0 +1,12 @@ +.counters { + margin: 1rem; + border: 1px solid black; + padding: 1rem 0; + width: fit-content; +} + +.sum { + margin-top: 1rem; + margin-left: 1rem; + width: fit-content; +} diff --git a/awesome_owl/static/src/playground.js b/awesome_owl/static/src/playground.js index 4ac769b0aa5..89e8514d0a2 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 { Card } from "./card/card"; +import { Counter } from "./counter/counter"; +import { TodoList } from "./todo_list/todo_list"; export class Playground extends Component { static template = "awesome_owl.playground"; + static components = { Counter, Card, TodoList }; + static props = [] + + setup() { + this.html = markup("some content rendered from html"); + this.state = useState({ sum: 0}); + } + + incrementSum() { + console.log("incrementSum called") + this.state.sum++; + } } diff --git a/awesome_owl/static/src/playground.xml b/awesome_owl/static/src/playground.xml index 4fb905d59f9..a689d2b119d 100644 --- a/awesome_owl/static/src/playground.xml +++ b/awesome_owl/static/src/playground.xml @@ -1,10 +1,29 @@ - -
- hello world +
+
+ + +
+
+ Sum: +
+ +

+ + + + + content of card 2 + + + + +

+ + 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..977cf959604 --- /dev/null +++ b/awesome_owl/static/src/todo_list/todo_item.js @@ -0,0 +1,20 @@ +import { Component } from "@odoo/owl"; +import { Card } from "../card/card"; + +export class TodoItem extends Component { + static template = "awesome_owl.todo_item"; + static components = { Card }; + static props = { + todo: { + type: Object, + shape: { + id: Number, + description: String, + isCompleted: Boolean, + }, + }, + toggleCompleted: Function, + removeTodo: Function, + } +} + 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..56e154a733e --- /dev/null +++ b/awesome_owl/static/src/todo_list/todo_item.xml @@ -0,0 +1,11 @@ + + + +
+ + + +
+
+ +
diff --git a/awesome_owl/static/src/todo_list/todo_list.css b/awesome_owl/static/src/todo_list/todo_list.css new file mode 100644 index 00000000000..e69de29bb2d 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..8c474e21e24 --- /dev/null +++ b/awesome_owl/static/src/todo_list/todo_list.js @@ -0,0 +1,40 @@ +import { Component, useState } from "@odoo/owl"; +import { TodoItem } from "./todo_item"; +import { useAutoFocus } from "../hooks"; + +export class TodoList extends Component { + static template = "awesome_owl.todo_list"; + static components = { TodoItem }; + static props = []; + + setup() { + this.nextId = 1; + this.todos = useState([]); + useAutoFocus('input'); + } + + addTodo(e) { + if (e.keyCode !== 13) return; + + const input = e.target.value + if (!input) return + + this.todos.push({ + id: this.nextId++, + description: input, + isCompleted: false, + }) + + e.target.value = ""; + } + + toggleCompleted(id) { + const todo = this.todos.find(todo => todo.id === id); + todo.isCompleted = !todo.isCompleted; + } + + removeTodo(id) { + const index = this.todos.findIndex(todo => todo.id === id); + this.todos.splice(index, 1); + } +} 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..b0f4ae6f18f --- /dev/null +++ b/awesome_owl/static/src/todo_list/todo_list.xml @@ -0,0 +1,17 @@ + + + +
+

Todo List

+ + + + +
+
+ +
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..60cbfa736c4 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,20 @@ +{ + 'name': 'Real Estate', + 'category': 'Sales', + 'description': 'Advertise your real estate', + 'author': '[THDES] Thomas des Touches', + 'depends': [ + 'base_setup', + ], + 'application': True, + 'license': 'LGPL-3', + '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_menu_views.xml', + 'views/res_user_views.xml', + ], +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..9a2189b6382 --- /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 00000000000..d62eb216eed --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,129 @@ +from odoo import api, fields, models +from odoo.exceptions import UserError, ValidationError +from odoo.tools.float_utils import float_compare + + +class EstateProperty(models.Model): + _name = "estate.property" + _description = "Estate Property" + _order = "id desc" + + name = fields.Char("Title", required=True) + description = fields.Text("Description") + postcode = fields.Char("Post Code") + property_type_id = fields.Many2one("estate.property.type", string="Property Type") + property_tag_ids = fields.Many2many("estate.property.tag", string="Tags") + + date_availability = fields.Date( + "Available From", + copy=False, + default=fields.Date.add(fields.Date.today(), months=3), + ) + expected_price = fields.Float("Expected Price", required=True) + selling_price = fields.Float("Selling Price", readonly=True, copy=False) + + bedrooms = fields.Integer("# Bedrooms", default=2) + living_area = fields.Integer("Living Area") + facades = fields.Integer("# Facades") + garage = fields.Boolean("Garage") + + garden = fields.Boolean("Garden") + garden_area = fields.Integer("Garden area") + garden_orientation = fields.Selection( + string="Garden Orientation", + selection=[ + ("north", "North"), + ("south", "South"), + ("east", "East"), + ("west", "West"), + ], + ) + + total_area = fields.Float(string="Total Area", compute="_compute_total_area") + + user_id = fields.Many2one("res.users", string="Salesperson", default=lambda self: self.env.uid) + partner_id = fields.Many2one("res.partner", string="Buyer", copy=False) + + offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers") + best_offer = fields.Float(string="Best Offer", compute="_compute_best_offer") + + active = fields.Boolean(default=True) + state = fields.Selection( + string="Status", + selection=[ + ("new", "New"), + ("offer_received", "Offer Received"), + ("offer_accepted", "Offer Accepted"), + ("sold", "Sold"), + ("canceled", "Canceled"), + ], + default="new", + required=True, + ) + + _check_expected_price = models.Constraint( + "CHECK(expected_price > 0)", + "The expected price of a property should be stricly positive", + ) + + _check_selling_price = models.Constraint( + "CHECK(selling_price >= 0)", + "The selling price of a property should be positive", + ) + + @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_offer(self): + for record in self: + record.best_offer = max(record.offer_ids.mapped("price"), default=0) + + @api.constrains("state", "expected_price", "selling_price") + def _check_prices(self): + for record in self: + if ( + record.state == "offer_accepted" + and float_compare(record.selling_price, record.expected_price * 0.9, precision_digits=2) == -1 + ): + raise ValidationError("The selling price must be at least 90% of the expected price") + + @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_sell(self): + for record in self: + if record.state == "canceled": + raise UserError("Canceled properties cannot be sold") + if not record.offer_ids: + raise UserError("Properties without offers cannot be sold") + record.state = "sold" + + return True + + def action_cancel(self): + for record in self: + if record.state == "sold": + raise UserError("Sold properties cannot be canceled") + record.state = "canceled" + + return True + + @api.ondelete(at_uninstall=False) + def _ondelete(self): + for record in self: + if record.state not in ("new", "canceled"): + raise UserError("Only new and canceled properties can be deleted") + + def _set_offer_received(self): + for record in self: + if record.state == "new": + record.state = "offer_received" diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py new file mode 100644 index 00000000000..becb69d6822 --- /dev/null +++ b/estate/models/estate_property_offer.py @@ -0,0 +1,77 @@ +from odoo import api, fields, models +from odoo.exceptions import UserError + + +class EstatePropertyOffer(models.Model): + _name = "estate.property.offer" + _description = "Estate Property Offer" + _order = "price desc" + + price = fields.Float("Price") + state = fields.Selection( + string="Status", + copy=False, + readonly=True, + selection=[("accepted", "Accepted"), ("refused", "Refused")], + ) + + validity = fields.Integer(string="Validity (days)", default=7) + date_deadline = fields.Date(string="Deadline", compute="_compute_deadline", inverse="_compute_validity") + + property_id = fields.Many2one("estate.property", string="Property", required=True) + partner_id = fields.Many2one("res.partner", string="Partner", required=True) + property_type_id = fields.Many2one(related="property_id.property_type_id", store=True) + + @api.depends("validity") + def _compute_deadline(self): + for record in self: + creation_date = record.create_date or fields.Date.today() + record.date_deadline = fields.Date.add(creation_date, days=record.validity) + + def _compute_validity(self): + for record in self: + creation_date = record.create_date or fields.Date.today() + record.validity = (record.date_deadline - creation_date.date()).days + + @api.model + def create(self, vals): + for record in vals: + property = self.env["estate.property"].browse(record["property_id"]) + if property.state == "sold": + raise UserError("A sold property cannot receive offers") + if record["price"] < property.best_offer: + raise UserError(f"The offer must be above {property.best_offer}") + property._set_offer_received() + + return super().create(vals) + + def action_accept(self): + self.ensure_one() + self.state = "accepted" + + property = self.property_id + property.selling_price = self.price + property.partner_id = self.partner_id + + if property.state == "offer_received": + property.state = "offer_accepted" + + for offer in property.offer_ids: + if offer.id != self.id: + offer.state = "refused" + + return True + + def action_refuse(self): + for record in self: + record.state = "refused" + + property = self.property_id + + if "accepted" not in property.offer_ids.mapped("state"): + property.state = "offer_received" + + property.selling_price = None + property.partner_id = None + + return True diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py new file mode 100644 index 00000000000..dff99d26b81 --- /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 Tag' + _order = 'name' + + name = fields.Char('Name', required=True) + color = fields.Integer('Color', required=True) + + _unique_name = models.Constraint( + 'UNIQUE(name)', + 'Property tag names 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..48181e81a39 --- /dev/null +++ b/estate/models/estate_property_type.py @@ -0,0 +1,29 @@ +from odoo import fields, models + + +class EstatePropertyType(models.Model): + _name = 'estate.property.type' + _description = 'Estate Property Type' + _order = 'sequence' + + name = fields.Char('Name', required=True) + sequence = fields.Integer('Sequence', default=1) + + property_ids = fields.One2many('estate.property', 'property_type_id') + offer_ids = fields.One2many('estate.property.offer', 'property_type_id') + offer_count = fields.Integer(compute='_count_offers') + + _unique_name = models.Constraint( + 'UNIQUE(name)', + 'Property type names should be unique' + ) + + def _count_offers(self): + self.offer_count = len(self.offer_ids) + + +class EstatePropertyTypeLine(models.Model): + _name = 'estate.property.type.line' + _description = 'Estate Property Type Line' + + property_type_id = fields.Many2one('estate.property.type') diff --git a/estate/models/res_users.py b/estate/models/res_users.py new file mode 100644 index 00000000000..ee338f412f7 --- /dev/null +++ b/estate/models/res_users.py @@ -0,0 +1,12 @@ +from odoo import fields, models + + +class ResUsers(models.Model): + _name = "res.users" + _inherit = ["res.users"] + + property_ids = fields.One2many( + "estate.property", + "user_id", + domain="[('state', 'in', ('new', 'offer_received'))]", + ) diff --git a/estate/security/ir.model.access.csv b/estate/security/ir.model.access.csv new file mode 100644 index 00000000000..e6f4e755633 --- /dev/null +++ b/estate/security/ir.model.access.csv @@ -0,0 +1,6 @@ +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_type_line,access_estate_property_type_line,estate.model_estate_property_type_line,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/tests/__init__.py b/estate/tests/__init__.py new file mode 100644 index 00000000000..dfd37f0be11 --- /dev/null +++ b/estate/tests/__init__.py @@ -0,0 +1 @@ +from . import test_estate diff --git a/estate/tests/test_estate.py b/estate/tests/test_estate.py new file mode 100644 index 00000000000..8ace91a33ee --- /dev/null +++ b/estate/tests/test_estate.py @@ -0,0 +1,89 @@ +from odoo.exceptions import UserError +from odoo.tests.common import TransactionCase +from odoo.tests import tagged + + +@tagged("post_install", "-at_install") +class EstateTestCase(TransactionCase): + @classmethod + def setUpClass(cls): + super(EstateTestCase, cls).setUpClass() + cls.properties = cls.env["estate.property"].create( + [ + { + "name": "test property", + "expected_price": 100_000, + } + ] + ) + + cls.partners = cls.env["res.partner"].create( + [ + { + "name": "test partner", + } + ] + ) + + def test_make_offer_for_new_property(self): + """The property state should be 'offer_received' after receiving a property""" + self._make_offer_on_property() + + self.assertRecordValues( + self.properties, + [ + { + "name": "test property", + "expected_price": 100_000, + "state": "offer_received", + } + ], + ) + + def test_sell_property_without_offer(self): + """ "It should be impossible to sell a property that received no offer""" + + with self.assertRaises(UserError, msg="Properties without offers cannot be sold"): + self.properties.action_sell() + + def test_sell_property_with_offer(self): + """The property state should be 'sold' after selling the property""" + self._make_offer_on_property() + self.properties.action_sell() + + self.assertRecordValues( + self.properties, + [ + { + "name": "test property", + "state": "sold", + } + ], + ) + + def test_make_offer_for_sold_property(self): + """ "It should be impossible to create an offer for a sold property""" + self._make_offer_on_property() + self.properties.action_sell() + + with self.assertRaises(UserError, msg="Sold properties cannot receive offers"): + self.properties.offer_ids.create( + [ + { + "price": 300_000, + "property_id": self.properties.id, + "partner_id": self.partners.id, + } + ] + ) + + def _make_offer_on_property(self): + self.properties.offer_ids.create( + [ + { + "price": 200_000, + "property_id": self.properties.id, + "partner_id": self.partners.id, + } + ] + ) diff --git a/estate/views/estate_menu_views.xml b/estate/views/estate_menu_views.xml new file mode 100644 index 00000000000..684aded1faf --- /dev/null +++ b/estate/views/estate_menu_views.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + diff --git a/estate/views/estate_property_offer_views.xml b/estate/views/estate_property_offer_views.xml new file mode 100644 index 00000000000..7caa56cddd6 --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,46 @@ + + + + Property Offers + estate.property.offer + list,form + [('property_type_id', '=', active_id)] + + + + estate.property.offer.view.list + estate.property.offer + + + + + + + +
+ +
+

+ +

+
+
+ + + + + + + + + + + + + + + + + + + estate.property.type.view.search + estate.property.type + + + + + + + + diff --git a/estate/views/estate_property_views.xml b/estate/views/estate_property_views.xml new file mode 100644 index 00000000000..86e541b95c5 --- /dev/null +++ b/estate/views/estate_property_views.xml @@ -0,0 +1,136 @@ + + + + Properties + estate.property + list,form,search,kanban + {'search_default_available': True} + + + + estate.property.view.list + estate.property + + + + + + + + + + + + + + + estate.property.view.form + estate.property + +
+
+
+ + +
+

+ +

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+ + + estate.property.view.search + estate.property + + + + + + + + + + + + + + + + + estate.property.view.kanban + estate.property + + + + + +
+ +
+ Expected Price: +
+
+ Best Price: +
+
+ Selling Price: +
+
+ +
+
+
+
+
+
+
+ +
diff --git a/estate/views/res_user_views.xml b/estate/views/res_user_views.xml new file mode 100644 index 00000000000..6736b6b1c64 --- /dev/null +++ b/estate/views/res_user_views.xml @@ -0,0 +1,18 @@ + + + + + res.users.view.form.inherit.estate + res.users + + + + + + + + + + + + diff --git a/estate_account/__init__.py b/estate_account/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/estate_account/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/estate_account/__manifest__.py b/estate_account/__manifest__.py new file mode 100644 index 00000000000..1b0fdeaa11e --- /dev/null +++ b/estate_account/__manifest__.py @@ -0,0 +1,14 @@ +{ + 'name': 'Real Estate Accounting', + 'category': 'Sales/Accouting', + 'description': 'Invoice your real estate', + 'author': '[THDES] Thomas des Touches', + 'depends': [ + 'estate', + 'account' + ], + 'application': True, + 'license': 'LGPL-3', + 'data': [ + ], +} diff --git a/estate_account/models/__init__.py b/estate_account/models/__init__.py new file mode 100644 index 00000000000..5e1963c9d2f --- /dev/null +++ b/estate_account/models/__init__.py @@ -0,0 +1 @@ +from . import estate_property diff --git a/estate_account/models/estate_property.py b/estate_account/models/estate_property.py new file mode 100644 index 00000000000..54439a1093f --- /dev/null +++ b/estate_account/models/estate_property.py @@ -0,0 +1,34 @@ +from odoo import models +from odoo.orm.commands import Command + +class EstateProperty(models.Model): + _inherit = ['estate.property'] + + def action_sell(self): + + journal = self.env['account.journal'].search([('type', 'in', 'sale')], limit=1) + invoice_vals = [] + + for record in self: + invoice_vals.append({ + 'name': record.name, + 'partner_id': record.partner_id.id, + 'move_type': 'out_invoice', + 'journal_id': journal.id, + 'invoice_line_ids': [ + Command.create({ + 'name': record.name, + 'quantity': 1, + 'price_unit': record.selling_price * 0.06, + }), + Command.create({ + 'name': 'Administrative fees', + 'quantity': 1, + 'price_unit': 100.00, + }), + ] + }) + + self.env['account.move'].create(invoice_vals) + + return super().action_sell()