diff --git a/.gitignore b/.gitignore index b6e47617de..3efd499f53 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +# IDE +.idea/ 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..45e3f910e9 --- /dev/null +++ b/estate/__manifest__.py @@ -0,0 +1,17 @@ +{ + 'name': 'estate', + 'license': 'AGPL-3', + 'depends': [ + 'base' + ], + 'data': [ + 'ir.model.access.csv', + 'views/estate_custom_user_views.xml', + 'views/estate_property_offer_views.xml', + 'views/estate_property_type_views.xml', + 'views/estate_property_tag_views.xml', + 'views/estate_property_views.xml', + 'views/estate_menus.xml', + ], + 'application': True +} diff --git a/estate/ir.model.access.csv b/estate/ir.model.access.csv new file mode 100644 index 0000000000..462b7b5e65 --- /dev/null +++ b/estate/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_property_model,access_property_model,model_estate_property,base.group_user,1,1,1,1 +access_property_type_model,access_property_type_model,model_estate_property_type,base.group_user,1,1,1,1 +access_property_tag_model,access_property_tag_model,model_estate_property_tag,base.group_user,1,1,1,1 +access_property_offer_model,access_property_offer_model,model_estate_property_offer,base.group_user,1,1,1,1 diff --git a/estate/models/__init__.py b/estate/models/__init__.py new file mode 100644 index 0000000000..34e5a9bc68 --- /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_custom_users diff --git a/estate/models/estate_custom_users.py b/estate/models/estate_custom_users.py new file mode 100644 index 0000000000..6ad767aded --- /dev/null +++ b/estate/models/estate_custom_users.py @@ -0,0 +1,8 @@ +from odoo import models, fields + + +class EstateCustomUsers(models.Model): + _inherit = 'res.users' + + property_ids = fields.One2many("estate.property", inverse_name='salesperson_id', string="Available Properties", + domain=[('state', 'in', ['new', 'offer_received'])]) diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py new file mode 100644 index 0000000000..639838b283 --- /dev/null +++ b/estate/models/estate_property.py @@ -0,0 +1,113 @@ +from datetime import datetime, timedelta +from odoo import api, fields, models, exceptions, tools + + +class EstateProperty(models.Model): + _name = "estate.property" + _description = "Property's properties" + _order = "id desc" + _sql_constraints = [ + ('check_expected_price', 'CHECK (expected_price > 0)', "The expected price must be greater than 0"), + ('check_selling_price', 'CHECK (selling_price >= 0)', "The selling price must be greater or equal to 0"), + ] + + name = fields.Char('Property name', required=True, default='Unknown') + description = fields.Text('Property Description') + postcode = fields.Char('Postcode') + date_availability = fields.Date('Availability', copy=False, + default=lambda self: datetime.now() + timedelta(days=90)) + 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 (sqm)') + facades = fields.Integer('Facades') + garage = fields.Boolean('Garage') + garden = fields.Boolean('Garden') + garden_area = fields.Integer('Garden Area') + garden_orientation = fields.Selection([('north', 'North'), ('east', 'East'), ('south', 'South'), ('west', 'West')], + default='north') + active = fields.Boolean('Active', default=True) + state = fields.Selection([ + ('new', 'New'), ('offer_received', 'Offer Received'), ('offer_accepted', 'Offer accepted'), ('sold', 'Sold'), + ('cancelled', 'Cancelled') + ], default='new', required=True, copy=False) + + total_area = fields.Float('Total Area', compute='_compute_total_area') + best_price = fields.Float('Best Offer', compute='_compute_best_price') + is_state_set = fields.Boolean('Is State Set', compute='_compute_is_state_set') + + property_type_id = fields.Many2one('estate.property.type', string="Property Type") + buyer_id = fields.Many2one("res.partner", string="Buyer") + salesperson_id = fields.Many2one("res.users", string="Salesman", 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") + + @api.depends("garden_area", "living_area") + def _compute_total_area(self): + for record in self: + record.total_area = record.garden_area + record.living_area + + @api.depends("offer_ids") + def _compute_best_price(self): + for record in self: + best_offer = record.get_best_offer() + if best_offer is None: + record.best_price = 0 + continue + record.best_price = best_offer.price + + def get_best_offer(self): + if len(self.offer_ids) == 0: + return None + return self.offer_ids.sorted(lambda offer: offer.price)[-1] + + @api.onchange("garden") + def _onchange_garden(self): + for record in self: + if record.garden: + record.garden_orientation = 'North' + record.garden_area = 10 + else: + record.garden_orientation = None + record.garden_area = 0 + + def action_sold(self): + valid_records = self.filtered(lambda r: r.state != 'cancelled') + if not valid_records: + raise exceptions.UserError(self.env._("Can't sell a cancelled offer")) + for record in valid_records: + best_offer = record.get_best_offer() + if best_offer is None: + raise exceptions.UserError(self.env._("The offers for property %s are empty", record.name)) + record.mark_as_sold(best_offer) + + def mark_as_sold(self, offer): + offer.status = 'accepted' + self.state = 'sold' + self.buyer_id = offer.partner_id + self.selling_price = offer.price + + def action_cancelled(self): + for record in self: + if record.state == 'sold': + raise exceptions.UserError(self.env._("Can't cancel a sold offer")) + record.state = 'cancelled' + + @api.constrains('selling_price', 'expected_price') + def _check_prices(self): + for record in self: + if not tools.float_is_zero(record.selling_price, 3) and record.selling_price < record.expected_price * 0.9: + raise exceptions.ValidationError( + self.env._("The selling price cannot be lower than 90%% of the expected price (min %d€)", + record.expected_price * 0.9)) + + @api.depends('state') + def _compute_is_state_set(self): + for record in self: + record.is_state_set = record.state == 'cancelled' or record.state == 'sold' + + @api.ondelete(at_uninstall=False) + def _on_delete(self): + for record in self: + if not (record.state in ('cancelled', 'new')): + raise exceptions.UserError(self.env._("Can only delete a 'New' or 'Cancelled' property (this property is '%s').", record.state)) diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py new file mode 100644 index 0000000000..521ed9b229 --- /dev/null +++ b/estate/models/estate_property_offer.py @@ -0,0 +1,63 @@ +from datetime import timedelta + +from odoo import api, fields, models, exceptions + + +class EstatePropertyOffer(models.Model): + _name = 'estate.property.offer' + _description = 'An offer' + _order = 'price desc' + _sql_constraints = [ + ('check_price', 'CHECK (price > 0)', 'The price must be greater than 0'), + ] + + price = fields.Float("Price", required=True) + status = fields.Selection([('accepted', 'Accepted'), ('refused', 'Refused')], copy=False) + offer_date = fields.Date(string="Creation Date", default=lambda self: fields.Date.today()) + validity = fields.Integer("Validity (days)") + + date_deadline = fields.Date("Deadline", readonly=False, compute="_compute_date_deadline", + inverse='_compute_date_deadline_inverse') + + partner_id = fields.Many2one('res.partner', string="Partner", required=True) + property_id = fields.Many2one('estate.property', string="Property", required=True) + property_type_id = fields.Many2one('estate.property.type', string="Property Type", + related='property_id.property_type_id') + + @api.depends("validity") + def _compute_date_deadline(self): + for offer in self: + if offer.offer_date is not None: + offer.date_deadline = fields.Date.to_date(offer.offer_date) + timedelta(days=offer.validity) + + def _compute_date_deadline_inverse(self): + for offer in self: + offer.validity = (fields.Date.to_date(offer.date_deadline) - fields.Date.to_date(offer.offer_date)).days + + def action_accepted(self): + for offer in self: + for prop_offer in offer.property_id.offer_ids: + if prop_offer.status == 'accepted': + raise exceptions.UserError(self.env._( + "Can't accept an offer when another offer has already been accepted for this property.")) + offer.property_id.mark_as_sold(self) + offer.status = 'accepted' + + def action_refused(self): + for offer in self: + offer.status = 'refused' + + @api.model_create_multi + def create(self, val_list): + for val in val_list: + property = self.env['estate.property'].browse(val['property_id']) + property.state = "offer_received" + + best_offer = property.get_best_offer() + if best_offer is None: + continue + if best_offer.price > val['price']: + raise exceptions.UserError(self.env._( + f"Can't create an offer with a lower price than the best offer for this property (current best " + f"price is {best_offer.price}€).")) + return super().create(val_list) diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py new file mode 100644 index 0000000000..533e1d9e17 --- /dev/null +++ b/estate/models/estate_property_tag.py @@ -0,0 +1,13 @@ +from odoo import fields, models + + +class EstatePropertyTag(models.Model): + _name = 'estate.property.tag' + _description = 'A tag' + _order = "name asc" + _sql_constraints = [ + ('check_unicity', 'UNIQUE (name)', 'A tag with the same name already exists'), + ] + + name = fields.Char('Name', required=True) + color = fields.Integer('Color') diff --git a/estate/models/estate_property_type.py b/estate/models/estate_property_type.py new file mode 100644 index 0000000000..0b7183fa5a --- /dev/null +++ b/estate/models/estate_property_type.py @@ -0,0 +1,22 @@ +from odoo import fields, models, api + + +class EstatePropertyType(models.Model): + _name = 'estate.property.type' + _description = 'Type of a property' + _order = "sequence, name asc" + _sql_constraints = [ + ('check_unicity', 'UNIQUE (name)', 'A property type with the same name already exists'), + ] + + name = fields.Char('Property Type', required=True) + sequence = fields.Integer('Sequence', default=1, help="Used to order types. Lower is better.") + + property_ids = fields.One2many('estate.property', 'property_type_id', string='Properties') + offer_ids = fields.One2many('estate.property.offer', 'property_type_id', string='Offers') + offer_count = fields.Integer('Offer Count', 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/views/estate_custom_user_views.xml b/estate/views/estate_custom_user_views.xml new file mode 100644 index 0000000000..7b9eb4bea6 --- /dev/null +++ b/estate/views/estate_custom_user_views.xml @@ -0,0 +1,14 @@ + + + estate.res.users.form + res.users + + + + + + + + + + diff --git a/estate/views/estate_menus.xml b/estate/views/estate_menus.xml new file mode 100644 index 0000000000..ef0a3a2fb9 --- /dev/null +++ b/estate/views/estate_menus.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/estate/views/estate_property_offer_views.xml b/estate/views/estate_property_offer_views.xml new file mode 100644 index 0000000000..b6ed258ea8 --- /dev/null +++ b/estate/views/estate_property_offer_views.xml @@ -0,0 +1,43 @@ + + + Offers + estate.property.offer + list,form + + + + estate.property.offer.list + estate.property.offer + + + + + + + + + + + +

+ +

+
+ + + + + + + + + + + +
+ +
+
+
\ No newline at end of file diff --git a/estate/views/estate_property_views.xml b/estate/views/estate_property_views.xml new file mode 100644 index 0000000000..12e05401a4 --- /dev/null +++ b/estate/views/estate_property_views.xml @@ -0,0 +1,147 @@ + + + Properties + estate.property + {'search_default_available': True} + list,form,kanban + + + + estate.property.list + estate.property + + + + + + + + + + + + + + + estate.property.kanban + estate.property + + + + + +
+

+ +

+
+ Selling Price: + + +
+
+ Expected Price: + + +
+
+
+ Best price: + + +
+
+ +
+
+
+ +
+
+
+ + + estate.property.form + estate.property + +
+
+
+ +
+

+ +

+ +

+

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+ + + estate.property.search + estate.property + + + + + + + + + + + + + + + + +
\ No newline at end of file diff --git a/estate_account/__init__.py b/estate_account/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /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 0000000000..54142eb2d1 --- /dev/null +++ b/estate_account/__manifest__.py @@ -0,0 +1,15 @@ +{ + 'name': 'estate_account', + 'license': 'AGPL-3', + 'depends': [ + 'base', + 'estate', + 'account', + ], + 'data': [ + 'ir.model.access.csv', + 'views/estate_account_views.xml', + 'views/estate_account_menus.xml', + ], + 'application': True +} diff --git a/estate_account/ir.model.access.csv b/estate_account/ir.model.access.csv new file mode 100644 index 0000000000..46cae8e440 --- /dev/null +++ b/estate_account/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id/id,group_id/id,perm_read,perm_write,perm_create,perm_unlink +access_estate_account_invoice_model,access_estate_account_invoice_model,model_estate_account_invoice,base.group_user,1,1,1,1 diff --git a/estate_account/models/__init__.py b/estate_account/models/__init__.py new file mode 100644 index 0000000000..c910012991 --- /dev/null +++ b/estate_account/models/__init__.py @@ -0,0 +1,2 @@ +from . import estate_account_invoice +from . import estate_property diff --git a/estate_account/models/estate_account_invoice.py b/estate_account/models/estate_account_invoice.py new file mode 100644 index 0000000000..b10f7de1c0 --- /dev/null +++ b/estate_account/models/estate_account_invoice.py @@ -0,0 +1,6 @@ +from odoo import models + + +class EstateAccountInvoice(models.Model): + _name = 'estate.account.invoice' + _description = 'An Estate invoice' diff --git a/estate_account/models/estate_property.py b/estate_account/models/estate_property.py new file mode 100644 index 0000000000..4c33214eae --- /dev/null +++ b/estate_account/models/estate_property.py @@ -0,0 +1,32 @@ +from odoo import models, Command + + +class EstateProperty(models.Model): + _inherit = "estate.property" + + def action_sold(self): + self._create_invoices() + return super().action_sold() + + def _create_invoices(self): + self.env['account.move'].create({ + 'partner_id': self.buyer_id.id, + 'move_type': 'out_invoice', + 'invoice_line_ids': [ + Command.create({ + 'name': f'The property {self.name}', + 'quantity': 1, + 'price_unit': self.selling_price, + }), + Command.create({ + 'name': f'Tax 6% {self.name}', + 'quantity': 1, + 'price_unit': self.selling_price * 0.06, + }), + Command.create({ + 'name': 'Administrative fees', + 'quantity': 1, + 'price_unit': 100.0, + }) + ] + }) diff --git a/estate_account/views/estate_account_menus.xml b/estate_account/views/estate_account_menus.xml new file mode 100644 index 0000000000..a073bf55e5 --- /dev/null +++ b/estate_account/views/estate_account_menus.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/estate_account/views/estate_account_views.xml b/estate_account/views/estate_account_views.xml new file mode 100644 index 0000000000..c84d7c507d --- /dev/null +++ b/estate_account/views/estate_account_views.xml @@ -0,0 +1,7 @@ + + + Invoices + estate.account.invoice + list,form + + \ No newline at end of file