From 090fd5b051cdef7f74ff09927008d5c2573572d7 Mon Sep 17 00:00:00 2001 From: amrendra yadav Date: Wed, 14 Aug 2024 10:33:17 +0530 Subject: [PATCH] [ADD] estate: models method, inherited the res.users, add estate_account module - Add ondelete decorator in models and override the models method - Created the res_user_model and inherit the res.users - Add new module estate_account - Create the invoice on action of property sold from estate_account to invoice --- estate/__manifest__.py | 1 + estate/models/__init__.py | 1 + estate/models/estate_property.py | 22 +++++++---- estate/models/estate_property_offer.py | 29 +++++++++++---- estate/models/estate_property_tag.py | 6 +-- estate/models/res_user_model.py | 6 +++ estate/views/estate_menus.xml | 3 -- estate/views/estate_property_offer_view.xml | 4 -- estate/views/estate_property_tag_view.xml | 3 -- estate/views/estate_property_type.xml | 8 ---- estate/views/estate_property_views.xml | 18 --------- estate/views/res_user_views.xml | 30 +++++++++++++++ estate_account/__init__.py | 1 + estate_account/__manifest__.py | 9 +++++ estate_account/models/__init__.py | 1 + estate_account/models/estate_property.py | 41 +++++++++++++++++++++ 16 files changed, 129 insertions(+), 54 deletions(-) create mode 100644 estate/models/res_user_model.py create mode 100644 estate/views/res_user_views.xml create mode 100644 estate_account/__init__.py create mode 100644 estate_account/__manifest__.py create mode 100644 estate_account/models/__init__.py create mode 100644 estate_account/models/estate_property.py diff --git a/estate/__manifest__.py b/estate/__manifest__.py index f2de6c52cd..29b080e4a8 100644 --- a/estate/__manifest__.py +++ b/estate/__manifest__.py @@ -8,6 +8,7 @@ 'data': [ 'security/ir.model.access.csv', + 'views/res_user_views.xml', 'views/estate_property_views.xml', 'views/estate_property_offer_view.xml', 'views/estate_property_type.xml', diff --git a/estate/models/__init__.py b/estate/models/__init__.py index 2f1821a39c..fd50743b4f 100644 --- a/estate/models/__init__.py +++ b/estate/models/__init__.py @@ -2,3 +2,4 @@ from . import estate_property_type from . import estate_property_tag from . import estate_property_offer +from . import res_user_model diff --git a/estate/models/estate_property.py b/estate/models/estate_property.py index 840c40b9c7..d672ff7e33 100644 --- a/estate/models/estate_property.py +++ b/estate/models/estate_property.py @@ -46,22 +46,20 @@ class EstateProperty(models.Model): ) _order = "id desc" salesman_id = fields.Many2one( - "res.users", # The model this field relates to - string="Salesman", # Label for the field - default=lambda self: self.env.user, # Set default to the current user's partner + "res.users", + string="Salesman", + default=lambda self: self.env.user, ) buyer_id = fields.Many2one( - "res.partner", # The model this field relates to - string="Buyer", # Label for the field - copy=False, # Prevent the field value from being copied when duplicating the record + "res.partner", + string="Buyer", + copy=False, ) - tag_ids = fields.Many2many("estate.property.tag", string="Tags", ondelete="cascade") offer_ids = fields.One2many("estate.property.offer", "property_id") total = fields.Integer(compute="total_area") best_offer = fields.Float(compute="best_offer_selete", store=True) property_type_id = fields.Many2one("estate.property.type") - sql_constraints = [ ( "check_expected_price", @@ -117,3 +115,11 @@ def _check_date_end(self): price_percent = (record.selling_price / record.expected_price) * 100 if price_percent < 90: raise ValidationError("Selling Price must be at least 90%") + + @api.ondelete(at_uninstall=False) + def _check_property_state(self): + for recod in self: + if recod.state == "new" or recod.state == "canceled": + continue + else: + raise ValidationError("New or Canceled property can be delete only.") diff --git a/estate/models/estate_property_offer.py b/estate/models/estate_property_offer.py index 87dba46b7c..c167c8b2e9 100644 --- a/estate/models/estate_property_offer.py +++ b/estate/models/estate_property_offer.py @@ -1,6 +1,6 @@ -from odoo import models, fields, api -from datetime import timedelta, date -from odoo.exceptions import ValidationError +from odoo import api, fields, models +from datetime import date, timedelta +from odoo.exceptions import UserError, ValidationError class EstatePropertyOffer(models.Model): @@ -21,7 +21,6 @@ class EstatePropertyOffer(models.Model): def _check_offer(self): for record in self: if record.status == "accepted": - # raise ValidationError("Accepted offer have benn deleted") record.property_id.selling_price = 0 record.property_id.buyer_id = None else: @@ -43,13 +42,11 @@ def _compute_deadline(self): def _inverse_deadline(self): for record in self: if record.deadline: - # Convert create_date to a date object today = fields.Date.to_date(record.create_date) - # Convert deadline to a date object deadline_date = fields.Date.from_string(record.deadline) record.validity = (deadline_date - today).days else: - record.validity = 7 # Default to 7 if deadline is not set + record.validity = 7 def action_status_accept(self): self.status = "accepted" @@ -73,3 +70,21 @@ def aciton_status_refused(self): _sql_constraints = [ ("check_price", "CHECK(price >= 0)", "The Price of Offer should be positive"), ] + + @api.model + def create(self, vals): + property_id = vals.get('property_id') + price = vals.get('price', 0) + if not property_id: + raise ValidationError("Property ID is required.") + property_record = self.env['estate.property'].browse(property_id) + existing_offers = self.search([('property_id', '=', property_id)]) + max_price = max(existing_offers.mapped('price'), default=0) + if price < max_price: + raise UserError("The offer price must be higher than the existing offers.") + record = super().create(vals) + if property_record: + property_record.state = 'offer_recived' + else: + raise ValidationError("Property record could not be found.") + return record diff --git a/estate/models/estate_property_tag.py b/estate/models/estate_property_tag.py index 6f5ec3d7f3..8c5b4f077d 100644 --- a/estate/models/estate_property_tag.py +++ b/estate/models/estate_property_tag.py @@ -1,10 +1,10 @@ -from odoo import models, fields +from odoo import fields, models class EstatePropertyTag(models.Model): _name = 'estate.property.tag' _description = 'Estate Property tag' name = fields.Char(required=True) - _sql_constraints = [('name_uniq', 'unique (name)', "Tag name already exists!")] - _order = 'name' color = fields.Integer() + _order = 'name' + _sql_constraints = [('name_uniq', 'unique (name)', "Tag name already exists!")] diff --git a/estate/models/res_user_model.py b/estate/models/res_user_model.py new file mode 100644 index 0000000000..fd93b6c81d --- /dev/null +++ b/estate/models/res_user_model.py @@ -0,0 +1,6 @@ +from odoo import models, fields + + +class ResUserModel(models.Model): + _inherit = 'res.users' + property_ids = fields.One2many('estate.property', 'salesman_id', string='Properties', domain="['|', ('state', '=', 'new'),('state', '=', 'offer_recived')]") diff --git a/estate/views/estate_menus.xml b/estate/views/estate_menus.xml index 2b020751b2..ab84e5e5e5 100644 --- a/estate/views/estate_menus.xml +++ b/estate/views/estate_menus.xml @@ -1,14 +1,11 @@ - - - diff --git a/estate/views/estate_property_offer_view.xml b/estate/views/estate_property_offer_view.xml index d756fcbcff..f04d9c9310 100644 --- a/estate/views/estate_property_offer_view.xml +++ b/estate/views/estate_property_offer_view.xml @@ -7,7 +7,6 @@ - -

diff --git a/estate/views/estate_property_views.xml b/estate/views/estate_property_views.xml index 30f1fc4405..a0c22ca7ff 100644 --- a/estate/views/estate_property_views.xml +++ b/estate/views/estate_property_views.xml @@ -4,15 +4,12 @@ estate.property tree,form {'search_default_state': True, 'search_default_current': True} - -

Create a new Property product

- estate_property_tree estate.property @@ -31,7 +28,6 @@
- estate_property_form estate.property @@ -50,7 +46,6 @@ - @@ -69,22 +64,14 @@ - - - - - - - - @@ -105,10 +92,8 @@ - - state_property_search estate.property @@ -122,15 +107,12 @@ - - - diff --git a/estate/views/res_user_views.xml b/estate/views/res_user_views.xml new file mode 100644 index 0000000000..0fdb643c89 --- /dev/null +++ b/estate/views/res_user_views.xml @@ -0,0 +1,30 @@ + + + + + res.users.form.inherit.properties + res.users + + + + + + + + + + + + + + + + + + + + + + + + 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..2dda6d4312 --- /dev/null +++ b/estate_account/__manifest__.py @@ -0,0 +1,9 @@ +{ + 'name': "estate_account", + 'version': '0.1', + 'depends': ['base', 'account', 'estate'], + 'description': "Technical practice", + 'installable': True, + 'application': True, + 'license': 'LGPL-3', +} diff --git a/estate_account/models/__init__.py b/estate_account/models/__init__.py new file mode 100644 index 0000000000..5e1963c9d2 --- /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 0000000000..87869a3de2 --- /dev/null +++ b/estate_account/models/estate_property.py @@ -0,0 +1,41 @@ +from odoo import models, Command +import logging +from datetime import date + + +class EstateProperty(models.Model): + _inherit = "estate.property" + + def status_action_sold_button(self): + logging.info("Request to create the invoices of property") + for record in self: + if record.buyer_id: + partner_id = record.buyer_id.id + self.env["account.move"].create( + { + "partner_id": partner_id, + "move_type": "out_invoice", + "invoice_date": date.today(), + "narration": "Tutorials traning", + "amount_total": record.selling_price, + "invoice_line_ids": [ + Command.create( + { + "name": record.name, + "quantity": 1, + "price_unit": record.selling_price * 0.06, + "invoice_date": date.today(), + } + ), + Command.create( + { + "name": "Administrative Fee", + "quantity": 1, + "price_unit": 100, + "invoice_date": date.today(), + }, + ), + ], + } + ) + return super().status_action_sold_button()