diff --git a/yuthon_property/models/yuthon_property.py b/yuthon_property/models/yuthon_property.py index 579a022cb..951897e76 100644 --- a/yuthon_property/models/yuthon_property.py +++ b/yuthon_property/models/yuthon_property.py @@ -100,12 +100,24 @@ class YuthonProperty(models.Model): today = date.today() current_year = today.year current_month = today.month - rentable_properties = self.search([('purpose_id.name', '=', '可租')]) + rentable_properties = self.env["yuthon.property"].browse( + self._context.get('active_ids', self._context.get('active_id'))) + # rentable_properties = self.search([('purpose_id.name', '=', '可租')]) for prop in rentable_properties: - lease_id = self.env['property.lease.contract.line'].search([ + contract_lines = self.env['property.lease.contract.line'].search([ ('address_id', '=', prop.id), ('lease_id.contact_state', '=', 'ing') - ], limit=1) + ]) + if contract_lines: + sorted_lines = sorted( + contract_lines, + key=lambda line: line.lease_id.actual_end_time, + reverse=True + ) + lease_id = sorted_lines[0] + else: + lease_id = self.env['property.lease.contract.line'] + if not lease_id: prop.write({'property_state': 'vacant'}) continue