@@ -9,13 +9,15 @@ class EstatePropertyOffer(models.Model):
9
9
_description = 'Real Estate Property Offer'
10
10
_order = 'price desc'
11
11
price = fields .Float ()
12
- status = fields .Selection ([('accepted' , 'Accepted' ), ('refused' , 'Refused' )], string = "Status" )
12
+ state = fields .Selection ([('new' , 'New' ),( ' accepted' , 'Accepted' ), ('refused' , 'Refused' )], string = "Status" , default = 'new' )
13
13
partner_id = fields .Many2one ('res.partner' , string = "Partner" , required = True )
14
14
property_id = fields .Many2one ('estate.property' , string = "Property" , required = True )
15
+ status = fields .Selection ([('accepted' , 'Accepted' ), ('refused' , 'Refused' )], string = "Status" )
15
16
property_type_id = fields .Many2one (
16
17
related = 'property_id.property_type_id' ,
17
18
string = "Property Type" ,
18
- readonly = True
19
+ readonly = True ,
20
+ store = True
19
21
)
20
22
buyer_id = fields .Many2one (
21
23
'res.partner' ,
@@ -33,6 +35,8 @@ class EstatePropertyOffer(models.Model):
33
35
inverse = "_inverse_date_deadline" ,
34
36
store = True
35
37
)
38
+ def action_offer_accepted (self ):
39
+ pass
36
40
37
41
@api .depends ('create_date' , 'validity' )
38
42
def _compute_date_deadline (self ):
@@ -49,26 +53,49 @@ def _inverse_date_deadline(self):
49
53
record .validity = max (delta , 0 )
50
54
else :
51
55
record .validity = 7
52
- def action_accept (self ):
53
- if self .property_id .state == 'sold' :
54
- raise UserError ("Cannot accept offers for sold properties." )
55
- existing_offer = self .search ([
56
- ('property_id' , '=' , self .property_id .id ),
57
- ('status' , '=' , 'accepted' )
58
- ])
59
- if existing_offer :
60
- raise UserError ("Only one offer can be accepted per property." )
61
- self .status = 'accepted'
62
- self .property_id .selling_price = self .price
63
- self .property_id .buyer_id = self .env .user .partner_id
64
56
65
- def action_refuse (self ):
66
- if self .status == 'accepted' :
67
- raise UserError ("Accepted offers cannot be refused." )
68
- self .status = 'refused'
57
+ def action_offer_accepted (self ):
58
+ for record in self :
59
+ record .status = 'accepted'
60
+ record .property_id .selling_price = record .price
61
+ record .property_id .buyer_id = record .partner_id
62
+
63
+ # set status 'refused' in the other offers of that particular property
64
+ for offer in record .property_id .offer_ids :
65
+ if offer .id != record .id :
66
+ offer .status = 'refused'
67
+ return True
68
+
69
+ def action_offer_refused (self ):
70
+ for record in self :
71
+ record .status = 'refused'
72
+ return True
73
+
69
74
_sql_constraints = [
70
75
('offer_price_positive' , 'CHECK(price > 0)' ,
71
76
'The offer price must be strictly positive.' )
72
- ]
77
+ ]
78
+
79
+
80
+ @api .model
81
+ def create (self , vals ):
82
+ # Ensure property_id exists in vals
83
+ property_id = self .env ['estate.property' ].browse (vals .get ('property_id' ))
84
+ if not property_id :
85
+ raise UserError ("The property associated with this offer does not exist." )
86
+
87
+ # Check if the new offer amount is lower than any existing offers
88
+ existing_offers = self .search ([('property_id' , '=' , property_id .id )])
89
+ for offer in existing_offers :
90
+ if vals .get ('price' , 0.0 ) <= offer .price :
91
+ raise UserError (
92
+ "You cannot create an offer with a price lower than an existing offer."
93
+ )
94
+
95
+ # Update the property's state to 'Offer Received'
96
+ property_id .state = 'offer_received'
97
+
98
+ # Create the offer as usual
99
+ return super (EstatePropertyOffer , self ).create (vals )
73
100
74
101
0 commit comments