Files
gzth/yuthon_property_approval/wizard/property_contract_wizard.py
T
2025-06-20 19:46:39 +08:00

136 lines
5.8 KiB
Python

from odoo import models, fields, api, _, SUPERUSER_ID
from odoo.exceptions import ValidationError, UserError
import xlrd
import base64
import datetime
from datetime import datetime
import pandas as pd
class PropertyContractWizard(models.Model):
_name = 'property.contract.wizard'
excel_file = fields.Binary(string="上传Excel表")
excel_name = fields.Char(string="文件名")
def confirm(self):
"""
导入excel文件,按行读取数据并处理
"""
if not self.excel_file:
raise ValidationError('请上传文件。')
book = xlrd.open_workbook(file_contents=base64.decodebytes(self.excel_file))
sh = book.sheet_by_index(0)
for rx in range(1, sh.nrows):
row = sh.row(rx)
self.yuthon_contract_update(row)
def yuthon_contract_data(self, row):
admin_id = self.env['res.users'].search([('name', '=', row[0].value)])
contract_code = row[1].value
name = row[2].value
address_id = self.env['yuthon.property'].search([('name', '=', (row[2].value))])
lessee_id = self.env['property.tenant.information'].search([('name', '=', row[3].value)], limit=1)
contact = row[4].value
invoicing_begins = row[5].value
invoicing_type_dict = {'专票': 'spot', '普票': 'cape', '': ''}
invoicing_type = invoicing_type_dict.get(row[6].value, '')
contract_time = row[9].value
if type(contract_time)== float:
contract_time =datetime.fromtimestamp(contract_time)
else:
contract_time = datetime.strptime(contract_time, '%Y-%m-%d')
term_start_time = row[10].value
if type(term_start_time)== float:
term_start_time =datetime.fromtimestamp(term_start_time)
else:
term_start_time = datetime.strptime(term_start_time, '%Y-%m-%d')
term_end_time = row[11].value
if type(term_end_time)== float:
term_end_time = datetime.fromtimestamp(term_end_time)
else:
term_end_time = datetime.strptime(term_end_time, '%Y-%m-%d')
total_area = str(row[12].value)
deposit = row[13].value
increments_every_few_years = row[15].value
increment_percentage = row[22].value
unit_price = row[23].value
first_year_rent = row[25].value
tax_rate = self.env['yuthon.tariff.setting'].search([('name', '=', row[26].value)])
if not tax_rate:
tax_rate = self.env['yuthon.tariff.setting'].create({'name': row[26].value})
contract_type_dict = {'续租': 'lease_renewal', '临租': 'interim', '补充协议': 'new', '首租': 'first'}
contract_type = contract_type_dict.get(row[29].value, '')
cycle_type_dict = {'月': 'monthly', '季度': 'quarter', '半年': 'half', '年度': 'year'}
cycle_dict = cycle_type_dict.get(row[35].value, '')
percentage_liquidated_damages = self.env['yuthon.penalty.setting'].search([('name', '=', row[36].value)])
if not percentage_liquidated_damages:
percentage_liquidated_damages = self.env['yuthon.penalty.setting'].create({'name': row[36].value})
tax = row[40].value
date_of_payment = row[37].value
performance_bonds = row[40].value
contract_date = {
'name': name,
'admin_id': admin_id.id,
'contract_code': contract_code,
'contact': contact,
'phone': invoicing_begins,
'invoicing_type': invoicing_type,
'contract_time':contract_time,
'term_start_time':term_start_time,
'term_end_time':term_end_time,
'total_area':total_area,
'deposit':deposit,
'tax':tax,
'date_payment_day':date_of_payment,
'increments_every_few_years':increments_every_few_years,
'increment_percentage':increment_percentage,
'unit_price':unit_price,
'first_year_rent':first_year_rent,
'tax_rate':tax_rate.id,
'contract_type':contract_type,
'cycle_dict':cycle_dict,
'lessee_id':lessee_id.id,
'percentage_liquidated_damages':percentage_liquidated_damages.id,
}
property_contract_id = self.env['property.lease.contract'].create(contract_date)
management_list = []
management_list.append((0, 0, {
'lease_id': property_contract_id.id,
'address_id': address_id.id,
}))
property_contract_id.lease_ids = management_list
def yuthon_contract_update(self, row):
contract_date = row[1].value
if contract_date:
if type(contract_date)== float:
contract_date = pd.to_datetime(contract_date, unit='d', origin='1899-12-30')
else:
contract_date = datetime.strptime(contract_date, '%Y-%m-%d')
deposit = row[2].value
performance_bonds = row[3].value
if performance_bonds:
deposit_type = 'performance'
elif deposit:
deposit_type = 'deposit'
else:
deposit_type = None
contract_id = self.env['property.lease.contract'].search([('contract_code', '=', row[0].value)])
if contract_id:
contract_id.write({
'contract_date':contract_date,
'deposit':deposit,
'performance_bonds':performance_bonds,
'deposit_type':deposit_type,
'increments_every_few_years': row[4].value,
'increment_percentage':row[5].value,
'increment_price':row[6].value,
'increment_amount':row[7].value,
'rent_free_period_month':row[8].value,
'rent_free_period_days':row[9].value,
'breach': row[10].value,
'first_year_rent': row[11].value,
})