33from datetime import timedelta
44
55from odoo import api , fields , models
6+ from odoo .exceptions import ValidationError
67
78
89class EstatePropertyOffer (models .Model ):
@@ -16,7 +17,7 @@ class EstatePropertyOffer(models.Model):
1617 selection = [('accepted' , 'Accepted' ), ('refused' , 'Refused' )],
1718 copy = False )
1819 partner_id = fields .Many2one ('res.partner' , string = 'Partner' , required = True )
19- property_id = fields .Many2one ('estate.property' , string = 'Property' , required = True )
20+ property_id = fields .Many2one ('estate.property' , string = 'Property' , required = True , ondelete = 'cascade' )
2021 validity = fields .Integer ('Validity (days)' , default = 7 )
2122 date_deadline = fields .Date ('Deadline' , compute = '_compute_date_deadline' , inverse = '_inverse_date_deadline' )
2223 property_type_id = fields .Many2one (related = 'property_id.property_type_id' , store = True )
@@ -48,3 +49,17 @@ def action_refuse_offer(self):
4849 'CHECK(price > 0)' ,
4950 'The price of an offer should be strictly positive.' ,
5051 )
52+
53+ @api .model
54+ def create (self , vals ):
55+ if len (vals ) == 0 :
56+ return super ().create (vals )
57+ property_id = vals [0 ].get ('property_id' )
58+ price = vals [0 ].get ('price' )
59+ if property_id and price :
60+ property_record = self .env ['estate.property' ].browse (property_id )
61+ if property_record .best_price and price <= property_record .best_price :
62+ error_msg = "The offer price should be higher than the best offer of the property."
63+ raise ValidationError (error_msg )
64+ property_record .state = 'offer_received'
65+ return super ().create (vals )
0 commit comments