Files
gzth/yuthon_property_approval/wizard/property_contract_wizard.py
T
2025-01-02 11:16:24 +08:00

100 lines
4.4 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
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_data(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_payment_day = row[37].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_payment_day,
'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