Skip to content

Commit 4446c54

Browse files
committed
[ADD] real_estate: tests for property selling and offer creation
Prevent creation of offers on sold properties and Prevent selling properties without accepted offers Ensure successful property sale updates the state correctly Add tests to validate offer and sale constraints Add test to ensure Garden Area and Orientation are reset when Garden is unchecked
1 parent b0fb6e3 commit 4446c54

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed

estate_account/models/estate_property.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class EstateProperty(models.Model):
77

88
def action_sold(self):
99
res = super().action_sold()
10+
self.check_access('write')
1011

1112
journal = self.env['account.journal'].search([('type', '=', 'sale')], limit=1)
1213
if not journal:
@@ -21,8 +22,6 @@ def action_sold(self):
2122
journal = self.env['account.journal'].sudo().search([('type', '=', 'sale')], limit=1)
2223
if not journal:
2324
raise UserError("No sale journal found. Please configure at least one sale journal.")
24-
record.check_access_rights('write')
25-
record.check_access_rule('write')
2625
invoice_vals = {
2726
"partner_id": record.buyer_id.id,
2827
"move_type": "out_invoice",
@@ -40,6 +39,5 @@ def action_sold(self):
4039
}),
4140
]
4241
}
43-
4442
self.env["account.move"].sudo().create(invoice_vals)
4543
return res

real_estate/models/estate_property.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def action_cancel(self):
9999

100100
def action_sold(self):
101101
for record in self:
102+
if not any(offer.state == 'accepted' for offer in record.offer_ids):
103+
raise UserError("You cannot sell a property without an accepted offer.")
102104
if record.state == 'cancelled':
103105
raise UserError("Cancelled properties cannot be sold.")
104106
record.state = 'sold'

real_estate/models/estate_property_offer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ def create(self, vals_list):
6262
for vals in vals_list:
6363
price = vals.get('price')
6464
property_obj = self.env['estate.property'].browse(vals.get('property_id'))
65-
existing_offers = property_obj.offer_ids.filtered(lambda o: o.price >= price)
65+
existing_offers = property_obj.offer_ids.filtered(lambda o: o.price is not None and price is not None and o.price >= price)
66+
if property_obj.state == 'sold':
67+
raise UserError("Cannot create offer for a sold property.")
6668
if existing_offers:
6769
raise ValidationError("You cannot create an offer with a lower or equal price than existing offers.")
6870
property_obj.state = 'offer_received'

real_estate/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import test_property_logic
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from odoo.tests.common import TransactionCase
2+
from odoo.tests import Form
3+
from odoo.exceptions import UserError
4+
from odoo.tests import tagged
5+
6+
7+
@tagged('post_install', '-at_install')
8+
class TestEstatePropertyLogic(TransactionCase):
9+
def setUp(self):
10+
super().setUp()
11+
Property = self.env['estate.property']
12+
Offer = self.env['estate.property.offer']
13+
14+
self.partner = self.env['res.partner'].create({
15+
'name': 'Test Buyer',
16+
'email': '[email protected]'
17+
})
18+
19+
self.property = Property.create({
20+
'name': 'Test Property',
21+
'state': 'new',
22+
'expected_price': 150000,
23+
'garden': True,
24+
'garden_area': 100,
25+
'garden_orientation': 'north',
26+
})
27+
28+
self.offer = Offer.create({
29+
'property_id': self.property.id,
30+
'partner_id': self.partner.id,
31+
'price': 160000,
32+
})
33+
34+
def test_cannot_create_offer_on_sold_property(self):
35+
self.property.state = 'sold'
36+
with self.assertRaises(UserError):
37+
self.env['estate.property.offer'].create({
38+
'property_id': self.property.id,
39+
'partner_id': self.partner.id,
40+
'price': 170000
41+
})
42+
43+
def test_cannot_sell_property_without_accepted_offer(self):
44+
self.property.offer_ids.unlink()
45+
with self.assertRaises(UserError):
46+
self.property.action_sold()
47+
48+
def test_can_sell_property_with_accepted_offer(self):
49+
self.offer.action_accept()
50+
self.property.action_sold()
51+
self.assertEqual(self.property.selling_price, self.offer.price)
52+
self.assertEqual(self.property.buyer_id, self.partner)
53+
54+
def test_reset_garden_fields_when_unchecked(self):
55+
form = Form(self.env['estate.property'])
56+
form.name = 'Garden Test'
57+
form.garden = True
58+
form.expected_price = 15000
59+
form.garden_area = 50
60+
form.garden_orientation = 'east'
61+
prop = form.save()
62+
63+
prop.garden = False
64+
prop._onchange_garden()
65+
66+
self.assertEqual(prop.garden_area, 0)
67+
self.assertFalse(prop.garden_orientation)

0 commit comments

Comments
 (0)