131 lines
5.9 KiB
Python
131 lines
5.9 KiB
Python
import datetime
|
|
|
|
from odoo import fields, models, api
|
|
|
|
|
|
class YuthonProperty(models.Model):
|
|
_inherit = 'yuthon.property'
|
|
|
|
@api.model
|
|
def get_complete_ledger_report(self, params):
|
|
domain = []
|
|
if params.get("keyword"):
|
|
domain.append(
|
|
domain.append(("name", "ilike", params["keyword"]))
|
|
)
|
|
|
|
|
|
if params.get("date_start"):
|
|
date_start = datetime.datetime.fromisoformat(params.get("date_start")).date()
|
|
domain.append(("effective_date", ">=", date_start))
|
|
else:
|
|
date_start = None
|
|
if params.get("date_end"):
|
|
date_end = datetime.datetime.fromisoformat(params.get("date_start")).date()
|
|
domain.append([
|
|
"|",
|
|
("end_date", "<=", date_end),
|
|
("end_date", "=", False)
|
|
])
|
|
else:
|
|
date_end = None
|
|
|
|
condition = params.get('condition',False)
|
|
partner_id = params.get('partner_id',False)
|
|
company_id = params.get('company_id',False)
|
|
|
|
if condition:
|
|
domain.append(('tenure_type','=', condition))
|
|
|
|
if partner_id:
|
|
domain.append(('admin_id', '=', partner_id))
|
|
if company_id:
|
|
domain.append(('company_id', '=', company_id))
|
|
|
|
print(domain)
|
|
# 查询数据,限制100条
|
|
properties = self.search(domain, limit=100)
|
|
state_selection = dict(self._fields['property_state']._description_selection(self.env))
|
|
|
|
cycle_convert = {
|
|
'monthly': 1,
|
|
'quarter': 4,
|
|
'half': 6,
|
|
'year': 12
|
|
}
|
|
|
|
result = []
|
|
for p in properties:
|
|
filtered_contracts = p.property_contract_ids.filtered(
|
|
lambda contract:
|
|
contract.start_time and
|
|
(not date_start or not contract.end_date or contract.end_date >= date_start) and
|
|
(not date_end or contract.start_time <= date_end)
|
|
)
|
|
|
|
target_contract = False
|
|
if filtered_contracts:
|
|
sorted_contracts = filtered_contracts.sorted(key=lambda c: c.start_time)
|
|
target_contract = sorted_contracts[0].lease_contract_id
|
|
|
|
# 3. 获取合同信息
|
|
rent_company = target_contract.rent_company if target_contract else ""
|
|
lessee_id = target_contract.lessee_id.name if (target_contract and target_contract.lessee_id) else ""
|
|
property_name = target_contract.property_name if target_contract else ""
|
|
contact = target_contract.lessee_id.contact if (target_contract and target_contract.lessee_id) else ""
|
|
rental_purposes = target_contract.rental_purposes.name if target_contract else ""
|
|
contract_code = target_contract.contract_code if target_contract else ""
|
|
total_area = target_contract.total_area if target_contract else 0.0
|
|
first_year_rent = target_contract.first_year_rent if target_contract else 0.0
|
|
contract_date = target_contract.contract_date.strftime("%Y-%m-%d") if (
|
|
target_contract and target_contract.contract_date) else ""
|
|
term_start_time = target_contract.term_start_time.strftime("%Y-%m-%d") if (
|
|
target_contract and target_contract.term_start_time) else ""
|
|
term_end_time = target_contract.term_end_time.strftime("%Y-%m-%d") if (
|
|
target_contract and target_contract.term_end_time) else ""
|
|
unit_price = target_contract.unit_price if target_contract else 0.0
|
|
total_contract_receivables = target_contract.total_contract_receivables if target_contract else 0.0
|
|
total_receivable = target_contract.total_receivable if target_contract else 0.0
|
|
|
|
# 4. 计算月租金
|
|
rent = target_contract.rent if target_contract else 0.0
|
|
cycle = target_contract.cycle_dict if (target_contract and target_contract.cycle_dict) else 'monthly'
|
|
monthly_rent = rent / cycle_convert.get(cycle, 1) if cycle_convert.get(cycle, 1) != 0 else 0.0
|
|
|
|
# 5. 从property.lease.contract.line提取评估单价(按address_id匹配)
|
|
evaluate_unit_price = 0.0
|
|
if target_contract:
|
|
lease_line = self.env['property.lease.contract.line'].search([
|
|
('address_id', '=', p.id), # 按资产ID匹配明细行
|
|
('lease_id', '=', target_contract.id) # 按目标合同匹配
|
|
], limit=1)
|
|
evaluate_unit_price = lease_line.evaluate_unit_price if lease_line else 0.0
|
|
|
|
# 6. 组装当前资产的结果
|
|
result.append({
|
|
"资产地址": p.name or "",
|
|
"资产面积": p.equity_area or 0.0,
|
|
"街道": p.street or "",
|
|
"小区": p.precinct_id.name if (p.precinct_id) else "",
|
|
"证载用途": p.registered_id.name if (p.registered_id) else "",
|
|
"资产目前状态": state_selection.get(p.property_state, ""),
|
|
"片区管理员": p.admin_id.name if (p.admin_id) else "",
|
|
"出租单位": rent_company,
|
|
"承租方": lessee_id,
|
|
"出租地址": property_name,
|
|
"联系人": contact,
|
|
"出租用途": rental_purposes,
|
|
"审批情况": "已审批",
|
|
"租赁合同号": contract_code,
|
|
"出租面积": total_area,
|
|
"评估价(元/m² •月)": evaluate_unit_price,
|
|
"首年租金": first_year_rent,
|
|
"合同签订日期": contract_date,
|
|
"合约开始时间": term_start_time,
|
|
"合约结束时间": term_end_time,
|
|
"合同总金额": total_contract_receivables,
|
|
"单价": unit_price,
|
|
"月租金": monthly_rent,
|
|
"实际已收到金额": total_receivable
|
|
})
|
|
return result |