132 lines
5.5 KiB
Python
132 lines
5.5 KiB
Python
from odoo import models, fields, api, _, SUPERUSER_ID
|
|
from odoo.exceptions import UserError, ValidationError
|
|
from dateutil.relativedelta import relativedelta
|
|
from datetime import datetime, time, timedelta
|
|
import xlrd
|
|
import base64
|
|
import pandas as pd
|
|
import re
|
|
|
|
class PropertyStatementsWizard(models.Model):
|
|
_name = 'property.statements.wizard'
|
|
_description = '银行流水向导弹窗'
|
|
|
|
selection_bank = fields.Selection([('js', '建设银行'), ('gs', '工商银行')], string="选择银行")
|
|
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)
|
|
if self.selection_bank == 'js':
|
|
self.property_bank_statements_jh_data(row)
|
|
elif self.selection_bank == 'gs':
|
|
self.property_bank_statements_gc_data(row)
|
|
|
|
def property_bank_statements_gc_data(self, row):
|
|
if row[0].value:
|
|
start_time = datetime.strptime(row[0].value, '%Y-%m-%d %H:%M:%S')
|
|
else:
|
|
start_time = None
|
|
name = row[4].value
|
|
name_account = row[1].value
|
|
remake = row[2].value
|
|
original_amount = row[3].value
|
|
your_own_account = row[6].value
|
|
cleaned_amount = original_amount.replace(',', '') # 移除逗号
|
|
float_amount = float(cleaned_amount)
|
|
your_own_unit = self.classify_by_last_four(your_own_account)
|
|
sequence_number = row[5].value
|
|
bank_data = {
|
|
'name': name,
|
|
'your_own_unit': your_own_unit,
|
|
'original_amount': float_amount,
|
|
'sequence_number': sequence_number,
|
|
'start_time': start_time,
|
|
'your_own_account': your_own_account,
|
|
'name_account': name_account,
|
|
'remake': remake,
|
|
}
|
|
existing_record = self.env['property.bank.statements'].search(
|
|
[('sequence_number', '=', sequence_number)], limit=1
|
|
)
|
|
if not existing_record:
|
|
self.env['property.bank.statements'].create(bank_data)
|
|
|
|
def property_bank_statements_jh_data(self, row):
|
|
your_own_account = row[0].value
|
|
if row[1].value:
|
|
start_time = datetime.strptime(row[1].value, '%Y%m%d %H:%M:%S')
|
|
else:
|
|
start_time = None
|
|
original_amount = float(row[2].value)
|
|
name_account = row[3].value
|
|
name = row[4].value
|
|
remake = row[5].value
|
|
serial_number = row[6].value
|
|
your_own_unit = self.classify_by_last_four(your_own_account)
|
|
bank_data = {
|
|
'name': name,
|
|
'serial_number': serial_number,
|
|
'your_own_unit': your_own_unit,
|
|
'no_amount': original_amount,
|
|
'original_amount': original_amount,
|
|
'start_time': start_time,
|
|
'your_own_account': your_own_account,
|
|
'name_account': name_account,
|
|
'remake': remake,
|
|
}
|
|
if original_amount and serial_number:
|
|
existing_record = self.env['property.bank.statements'].search(
|
|
[('serial_number', '=', serial_number)], limit=1
|
|
)
|
|
if not existing_record:
|
|
self.env['property.bank.statements'].create(bank_data)
|
|
else:
|
|
existing_record.write(bank_data)
|
|
|
|
elif original_amount:
|
|
self.env['property.bank.statements'].create(bank_data)
|
|
|
|
def _handle_contract_penalty(self, record):
|
|
'''
|
|
处理合同违约金
|
|
:param record: property.bank.statements 记录
|
|
:return:
|
|
'''
|
|
for contract_id in record.contract_ids:
|
|
receivable_id = self.env['property.accounts.receivable'].search([('contract_id', '=', contract_id.id)])
|
|
if receivable_id:
|
|
dt = datetime.strptime(str(record.start_time), '%Y-%m-%d %H:%M:%S').date() # 流水交易时间
|
|
due = datetime.strptime(str(receivable_id.due_date), '%Y-%m-%d').date() + relativedelta(months=1, day=1) # 应收日期
|
|
# 判断是否违约
|
|
if receivable_id.submitted and not receivable_id.is_reconciliation or not receivable_id.is_suspend:
|
|
original_amount = (due - dt).days * receivable_id.original_amount * int(receivable_id.percentage_liquidated_damages.name)
|
|
self.env['property.penalty.list'].create({
|
|
'contract_id': contract_id.id,
|
|
'property_id': contract_id.lease_ids[0].address_id.id,
|
|
'lessee_id': contract_id.lessee_id.id,
|
|
'original_amount': original_amount
|
|
})
|
|
receivable_id.write({'penalty':original_amount})
|
|
receivable_id.is_reconciliation = True
|
|
|
|
def classify_by_last_four(self, input_str):
|
|
if not isinstance(input_str, str) or len(input_str) < 4:
|
|
return "天投城建" # 输入不是字符串或长度不足4,默认为天投城建
|
|
|
|
last_four = input_str[-4:] # 获取后四位字符
|
|
|
|
if last_four == "3619":
|
|
return "天河物管"
|
|
elif last_four == "0222":
|
|
return "天河投资"
|
|
else:
|
|
return "天投城建" |