diff --git a/.zed/tasks.json b/.zed/tasks.json new file mode 100644 index 00000000000..a8c4ff82dc0 --- /dev/null +++ b/.zed/tasks.json @@ -0,0 +1,42 @@ +[ + { + "label": "Odoo: Enterprise (Update estate)", + "command": "venv/bin/python", + "args": [ + "odoo-bin", + "--addons-path=addons/,../enterprise/,../tutorials", + "-u", "estate", + "-d", "odoo" + ], + "cwd": "/home/feraz/Documents/odoo", + "use_new_terminal": false, + "allow_concurrent_runs": false + }, + { + "label": "Odoo: Community (Update estate)", + "command": "venv/bin/python", + "args": [ + "odoo-bin", + "--addons-path=addons/,../tutorials", + "-u", "estate", + "-d", "odoo" + ], + "cwd": "/home/feraz/Documents/odoo", + "use_new_terminal": false, + "allow_concurrent_runs": false + }, + { + "label": "Odoo: Test (estate)", + "command": "venv/bin/python", + "args": [ + "odoo-bin", + "--addons-path=addons/,../enterprise/,../tutorials", + "-d", "odoo", + "--test-tags", "/estate", + "--stop-after-init" + ], + "cwd": "/home/feraz/Documents/odoo", + "use_new_terminal": false, + "allow_concurrent_runs": false + } +] 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..3a12f60e46a --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,41 @@ +{ + 'name': "Estate", + + 'summary': """ + Tutorial module for estate management + """, + + 'description': """ + Tutorial module for estate management + """, + + 'author': "Odoo", + 'website': "https://www.odoo.com", + + # Categories can be used to filter modules in modules listing + # Check https://github.com/odoo/odoo/blob/15.0/odoo/addons/base/data/ir_module_category_data.xml + # for the full list + 'category': 'Tutorials', + 'version': '0.1', + + # any module necessary for this one to work correctly + 'depends': ['base'], + 'application': True, + 'installable': 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_property_menu_views.xml', + 'views/res_user_views.xml', + + 'report/estate_property_templates.xml', + 'report/estate_property_reports.xml', + ], + 'assets': { + }, + 'license': 'AGPL-3' +} diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 00000000000..6f4f8524e8a --- /dev/null +++ b/estate/models/__init__.py @@ -0,0 +1,2 @@ +# import filename_python_file_within_folder_or_subfolder +from . import estate_property_type, estate_property_tag, estate_property_offer, estate_property, res_users diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py new file mode 100644 index 00000000000..1836017fa75 --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,120 @@ +from dateutil.relativedelta import relativedelta + +from odoo import models, fields, api +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 property" + _order = "id desc" + + name = fields.Char("Property Name", required=True) + description = fields.Text("Description") + postcode = fields.Char("Postcode") + date_availability = fields.Date("Availability Date", copy=False, default=lambda self: fields.Date.today() + relativedelta(months=3)) + expected_price = fields.Float(required=True) + selling_price = fields.Float(readonly=True, copy=False) + bedrooms = fields.Integer("Number of Bedrooms", default=2) + living_area = fields.Integer("Living Area m²") + facades = fields.Integer("Number of Facades") + garage = fields.Boolean() + garden = fields.Boolean() + garden_area = fields.Integer("Garden Area m²") + garden_orientation = fields.Selection(selection=[('north', 'North'), ('east', 'East'), ('south', 'South'), ('west', 'West')]) + state = fields.Selection( + selection=[ + ('new', 'New'), + ('offer_received', 'Offer Received'), + ('offer_accepted', 'Offer Accepted'), + ('sold', 'Sold'), ('cancelled', 'Cancelled'), + ], + default='new', + required=True, + copy=False) + active = fields.Boolean(default=True) + type_id = fields.Many2one(string="Type", comodel_name="estate.property.type") + buyer_id = fields.Many2one("res.partner", copy=False) + seller_id = fields.Many2one(string="Salesperson", comodel_name="res.users", default=lambda self: self.env.user) + tag_ids = fields.Many2many(string="Tags", comodel_name="estate.property.tag") + offer_ids = fields.One2many(string="Offers", comodel_name="estate.property.offer", inverse_name="property_id") + total_area = fields.Integer(compute="_compute_total_area") + best_price = fields.Float("Best offer price", compute="_compute_best_price") + + _expected_price_strictly_positive = models.Constraint( + 'CHECK(expected_price > 0)', + 'Expected price must be strictly positive' + ) + _selling_price_strictly_positive = models.Constraint( + 'CHECK(selling_price > 0)', + 'Selling price must be strictly positive' + ) + + def _no_accepted_offer(self): + self.ensure_one() + return all(offer.state != "accepted" for offer in self.offer_ids) + + @api.constrains("selling_price") + def _check_selling_price(self): + for property in self: + # No accepted offer and price is zero + if float_is_zero(property.selling_price, 2) and property._no_accepted_offer(): + return + + if float_compare(property.selling_price, property.expected_price * 0.9, 2) == -1: + raise ValidationError( + "Selling price must be at least 90% of expected price. Update expected price to accept offer." + ) + + @api.depends("living_area", "garden_area") + def _compute_total_area(self): + for property in self: + property.total_area = property.living_area + property.garden_area + + @api.depends("offer_ids") + def _compute_best_price(self): + for property in self: + if not property.offer_ids: + property.best_price = 0 + continue + best_offer_price = max(property.offer_ids.mapped('price')) + property.best_price = best_offer_price + + @api.onchange("garden") + def _onchange_garden(self): + for property in self: + if property.garden: + property.garden_area = 10 + property.garden_orientation = 'north' + else: + property.garden_area = None + property.garden_orientation = None + + def action_cancel_property(self): + for property in self: + if property.state == "sold": + raise UserError("Sold properties cannot be cancelled") + property.state = "cancelled" + return True + + def action_sell_property(self): + for property in self: + if property.state == "cancelled": + raise UserError("Cancelled properties cannot be sold") + + if not property.offer_ids: + raise UserError("Cannot sell property with no offer") + + if property._no_accepted_offer(): + raise UserError("Cannot sell property with no accepted offer") + + property.state = "sold" + return True + + @api.ondelete(at_uninstall=False) + def _unlink_only_new_cancelled(self): + for property in self: + if property.state not in ('new', 'cancelled'): + raise UserError("You can only delete new or cancelled properties") diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py new file mode 100644 index 00000000000..b494e6544a9 --- /dev/null +++ b/estate/models/estate_property_offer.py @@ -0,0 +1,66 @@ +from datetime import timedelta + +from odoo import models, fields, api, exceptions + + +class EstatePropertyOffer(models.Model): + + _name = "estate.property.offer" + _description = "Estate property offer" + _order = "price desc" + + price = fields.Float("Offer Price") + state = fields.Selection(copy=False, selection=[('accepted', 'Accepted'), ('refused', 'Refused')]) + partner_id = fields.Many2one(string="Buyer", comodel_name="res.partner", required=True) + property_id = fields.Many2one(comodel_name="estate.property", required=True) + property_type_id = fields.Many2one(related="property_id.type_id", store=True) + + validity = fields.Integer(default=7) + date_deadline = fields.Date("Offer Deadline", compute="_compute_deadline", inverse="_inverse_deadline") + + _price_strictly_positive = models.Constraint( + 'CHECK(price > 0)', + 'Offer price must be strictly positive' + ) + + @api.depends("validity") + def _compute_deadline(self): + for offer in self: + compare_date = offer.create_date.date() if offer.create_date else fields.Date.today() + offer.date_deadline = compare_date + timedelta(days=offer.validity) + + def _inverse_deadline(self): + for offer in self: + offer.validity = (offer.date_deadline - offer.create_date.date()).days + + def action_accept_offer(self): + for offer_to_accept in self: + # Ensure there is no existing accepted offer + if not offer_to_accept.property_id._no_accepted_offer(): + raise exceptions.UserError("Property already has an accepted offer.") + + offer_to_accept.state = "accepted" + + offer_to_accept.property_id.write({ + 'buyer_id': offer_to_accept.partner_id.id, + 'state': 'offer_accepted', + 'selling_price': offer_to_accept.price, + }) + + return True + + def action_refuse_offer(self): + for offer in self: + # We could forbid refusing accepted offers + offer.state = "refused" + return True + + @api.model + def create(self, vals_list): + for vals in vals_list: + property = self.env['estate.property'].browse(vals['property_id']) + if property.state == 'new': + property.state = 'offer_received' + elif property.state == 'sold': + raise exceptions.UserError("Property already has been sold, new offers can not be created for it.") + return super().create(vals_list) diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py new file mode 100644 index 00000000000..f2f215bb789 --- /dev/null +++ b/estate/models/estate_property_tag.py @@ -0,0 +1,16 @@ +from odoo import models, fields + + +class EstatePropertyTag(models.Model): + + _name = "estate.property.tag" + _description = "Estate property tag" + _order = "name" + + name = fields.Char("Property Tag Name", required=True) + color = fields.Integer() + + _name_uniq = models.Constraint( + 'unique(name)', + 'A property tag with the same name already exists.', + ) diff --git a/estate/models/estate_property_type.py b/estate/models/estate_property_type.py new file mode 100644 index 00000000000..b281c1407cf --- /dev/null +++ b/estate/models/estate_property_type.py @@ -0,0 +1,25 @@ +from odoo import models, fields, api + + +class EstatePropertyType(models.Model): + + _name = "estate.property.type" + _description = "Estate property type" + _order = "sequence,name" + + name = fields.Char("Property Type Name", required=True) + sequence = fields.Integer(default=1, help="Used to order stages. Lower is ranked higher.") + property_ids = fields.One2many(comodel_name="estate.property", inverse_name="type_id") + offer_ids = fields.One2many(related="property_ids.offer_ids", inverse_name="property_type_id") + + offer_count = fields.Integer(compute="_compute_offer_count") + + _name_uniq = models.Constraint( + 'unique(name)', + 'A property type with the same name already exists.', + ) + + @api.depends("property_ids.offer_ids") + def _compute_offer_count(self): + for type in self: + type.offer_count = self.env['estate.property.offer'].search_count([('property_id', 'in', type.property_ids.ids)]) diff --git a/estate/models/res_users.py b/estate/models/res_users.py new file mode 100644 index 00000000000..fa3361ef315 --- /dev/null +++ b/estate/models/res_users.py @@ -0,0 +1,23 @@ +from odoo import models, fields, api + + +class Users(models.Model): + + _inherit = 'res.users' + + property_ids = fields.One2many( + comodel_name="estate.property", + inverse_name="seller_id" + ) + + available_property_ids = fields.One2many( + comodel_name="estate.property", + compute="_compute_available_properties" + ) + + @api.depends("property_ids") + def _compute_available_properties(self): + for user in self: + user.available_property_ids = user.property_ids.filtered( + lambda property: property.state in ["new", "offer_received"] + ) diff --git a/estate/report/estate_property_reports.xml b/estate/report/estate_property_reports.xml new file mode 100644 index 00000000000..6da015d3f18 --- /dev/null +++ b/estate/report/estate_property_reports.xml @@ -0,0 +1,15 @@ + + + + + Estate Property + estate.property + qweb-pdf + estate.report_property_offers + estate.report_property_offers + 'Estate Property - %s' % (object.name or 'Offer').replace('/','') + + report + + + diff --git a/estate/report/estate_property_templates.xml b/estate/report/estate_property_templates.xml new file mode 100644 index 00000000000..93d796f5326 --- /dev/null +++ b/estate/report/estate_property_templates.xml @@ -0,0 +1,63 @@ + + + + diff --git a/estate/security/ir.model.access.csv b/estate/security/ir.model.access.csv new file mode 100644 index 00000000000..0c0b62b7fee --- /dev/null +++ b/estate/security/ir.model.access.csv @@ -0,0 +1,5 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +estate.access_estate_property,access_estate_property,estate.model_estate_property,base.group_user,1,1,1,1 +estate.access_estate_property_type,access_estate_property_type,estate.model_estate_property_type,base.group_user,1,1,1,1 +estate.access_estate_property_tag,access_estate_property_tag,estate.model_estate_property_tag,base.group_user,1,1,1,1 +estate.access_estate_property_offer,access_estate_property_offer,estate.model_estate_property_offer,base.group_user,1,1,1,1 diff --git a/estate/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..14a4eb07ce8 --- /dev/null +++ b/estate/tests/test_estate.py @@ -0,0 +1,117 @@ +from odoo.tests.common import TransactionCase +from odoo.exceptions import UserError +from odoo.tests import tagged +from odoo.tests.form import Form + + +@tagged('post_install', '-at_install') +class EstateTestCase(TransactionCase): + + @classmethod + def setUpClass(cls): + # add env on cls and many other things + super().setUpClass() + + # create the data for each tests. By doing it in the setUpClass instead + # of in a setUp or in each test case, we reduce the testing time and + # the duplication of code. + cls.properties = cls.env['estate.property'].create([ + {'name': 'property1', 'expected_price': 1000}, + {'name': 'property2', 'expected_price': 10000, 'garden': True, 'garden_area': 40}, + ]) + + cls.property = cls.properties[0] + cls.property_with_garden = cls.properties[1] + + cls.partner = cls.env['res.partner'].create({'name': 'partner1'}) + + def test_creation_area(self): + """Test that the total_area is computed like it should.""" + self.properties.living_area = 20 + self.assertRecordValues(self.properties, [ + {'total_area': 20}, + {'total_area': 60}, + ]) + + def test_action_sell(self): + """Test that everything behaves like it should when selling a property.""" + + # Cannot sell property with no offer + with self.assertRaises(UserError): + self.properties.action_sell_property() + + offers = self.env['estate.property.offer'].create([ + {'price': 2000, 'partner_id': self.partner.id, 'property_id': self.property.id}, + {'price': 10000, 'partner_id': self.partner.id, 'property_id': self.property_with_garden.id}, + ]) + + offers.action_accept_offer() + + self.properties.action_sell_property() + + self.assertRecordValues(self.properties, [ + {'state': 'sold', 'selling_price': 2000}, + {'state': 'sold', 'selling_price': 10000}, + ]) + + # Cannot cancel sold properties + with self.assertRaises(UserError): + self.properties.action_cancel_property() + + def test_action_cancel(self): + """Test that everything behaves like it should when cancelling a property.""" + self.properties.action_cancel_property() + self.assertRecordValues(self.properties, [ + {'state': 'cancelled'}, + {'state': 'cancelled'}, + ]) + + # Cannot sell cancelled properties + with self.assertRaises(UserError): + self.properties.action_sell_property() + + def test_offer_creation(self): + """ + Test that everything behaves like it should when creating an offer. + - The user can create one or several offers for a property + - The user can not sell properties with no accepted offers + - The user can sell properties with an accepted offer + - Offers can't be created for sold properties + """ + offers = self.env['estate.property.offer'].create([ + {'price': 2000, 'partner_id': self.partner.id, 'property_id': self.property.id}, + {'price': 1500, 'partner_id': self.partner.id, 'property_id': self.property.id}, + {'price': 12200, 'partner_id': self.partner.id, 'property_id': self.property_with_garden.id}, + ]) + + # Cannot sell property with no accepted offer + with self.assertRaises(UserError): + self.properties.action_sell_property() + + offers[0].action_accept_offer() + offers[2].action_accept_offer() + + self.properties.action_sell_property() + + # Cannot create offer on sold property + with self.assertRaises(UserError): + self.env['estate.property.offer'].create([ + {'price': 2000, 'partner_id': self.partner.id, 'property_id': self.property.id}, + {'price': 8500, 'partner_id': self.partner.id, 'property_id': self.property_with_garden.id}, + ]) + + def test_garden_reset(self): + """Test that the garden area and orientation correctly reset when garden is set to False in a form""" + with Form(self.property) as property: + property.garden = True + + self.assertRecordValues(self.property, [ + {'name': 'property1', 'garden': True, 'garden_area': 10, 'garden_orientation': 'north'}, + ]) + + with Form(self.property) as property: + property.garden = False + + self.assertRecordValues(self.property, [ + {'name': 'property1', 'garden': False, 'garden_area': 0, 'garden_orientation': None}, + ]) diff --git a/estate/views/estate_property_menu_views.xml b/estate/views/estate_property_menu_views.xml new file mode 100644 index 00000000000..1788eb7a5d3 --- /dev/null +++ b/estate/views/estate_property_menu_views.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/estate/views/estate_property_offer_views.xml b/estate/views/estate_property_offer_views.xml new file mode 100644 index 00000000000..7e1f02ef6ff --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,53 @@ + + + + + Estate Property Offer + estate.property.offer + list,form,search + [('property_type_id', '=', active_id)] + +

+ Create estate property offers to view them here ! +

+
+
+ + + estate.property.offer.list + estate.property.offer + + + + + + + + + +

+ +

+ + + + + + + + + + + + + +
+
+ + + estate.property.type.list + 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..77920b5c4a5 --- /dev/null +++ b/estate/views/estate_property_views.xml @@ -0,0 +1,144 @@ + + + + + Estate Property + estate.property + list,kanban,form,search + {'search_default_available': True} + +

+ Create estate properties to view them here ! +

+
+
+ + + estate.property.list + estate.property + + + + + + + + + + + + + + + + estate.property.form + estate.property + +
+
+
+ +

+ +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+ + + + estate.property.kanban + estate.property + + + + + +
+

+ +

+ Expected Price: +
+ Best Price: +
+
+ Selling Price: +
+ +
+
+
+
+
+
+ + + estate.property.search + estate.property + + + + + + + + + + + + + + + + + + +
diff --git a/estate/views/res_user_views.xml b/estate/views/res_user_views.xml new file mode 100644 index 00000000000..da1e173592d --- /dev/null +++ b/estate/views/res_user_views.xml @@ -0,0 +1,17 @@ + + + + + res.users.form.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..ca384b15083 --- /dev/null +++ b/estate_account/__manifest__.py @@ -0,0 +1,30 @@ +{ + 'name': "Estate Account", + + 'summary': """ + Tutorial module for estate account management + """, + + 'description': """ + Tutorial module for estate account management + """, + + 'author': "Odoo", + 'website': "https://www.odoo.com", + + # Categories can be used to filter modules in modules listing + # Check https://github.com/odoo/odoo/blob/15.0/odoo/addons/base/data/ir_module_category_data.xml + # for the full list + 'category': 'Tutorials', + 'version': '0.1', + + # any module necessary for this one to work correctly + 'depends': ['base', 'estate', 'account'], + 'application': False, + 'installable': True, + 'data': [ + ], + 'assets': { + }, + 'license': 'AGPL-3' +} diff --git a/estate_account/models/__init__.py b/estate_account/models/__init__.py new file mode 100644 index 00000000000..e6ebe16945c --- /dev/null +++ b/estate_account/models/__init__.py @@ -0,0 +1,2 @@ +# import filename_python_file_within_folder_or_subfolder +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..2c938a0dcdf --- /dev/null +++ b/estate_account/models/estate_property.py @@ -0,0 +1,33 @@ +from odoo import models, Command + + +class EstateProperty(models.Model): + + _inherit = 'estate.property' + + def action_sell_property(self): + + vals_list = [] + + for record in self: + vals = { + 'partner_id': record.buyer_id.id, + 'move_type': 'out_invoice', + 'line_ids': [ + Command.create({ + 'name': '6% of selling price', + 'quantity': 1, + 'price_unit': 0.06 * record.selling_price + }), + Command.create({ + 'name': 'Administrative fees', + 'quantity': 1, + 'price_unit': 100 + }) + ] + } + vals_list.append(vals) + + self.env['account.move'].create(vals_list) + + return super().action_sell_property()