diff --git a/yuthon_property/models/property_accounts_receivable.py b/yuthon_property/models/property_accounts_receivable.py index 5b2dafdcd..2a3106143 100644 --- a/yuthon_property/models/property_accounts_receivable.py +++ b/yuthon_property/models/property_accounts_receivable.py @@ -212,17 +212,51 @@ class PropertyAccountsReceivable(models.Model): receivable_records = self.env['property.accounts.receivable'].browse(receivable_ids) # 加载新模板文件 - template_path = get_module_resource('yuthon_property', 'static', 'tmpl', 'piliangkaipiao (2).xlsx') + template_path = get_module_resource('yuthon_property', 'static', 'tmpl', 'piliangkaipiao (3).xlsx') workbook = load_workbook(template_path) # 指定写入"原始数据"sheet sheet = workbook['原始数据'] - # 从第3行开始写入数据(第1-2行是表头) - row = 3 + # 统计导出数据中最常见的年月(用于B2单元格) + year_month_list = [] + for record in receivable_records: + if record.due_date: + year_month_list.append((record.due_date.year, record.due_date.month)) - # 获取模板样式行(使用第3行作为参考) - template_row_idx = 3 + if year_month_list: + # 取出现次数最多的年月 + from collections import Counter + most_common_ym = Counter(year_month_list).most_common(1)[0][0] + title_year, title_month = most_common_ym + else: + # 如果没有应收日期,使用当前年月 + now = datetime.now() + title_year, title_month = now.year, now.month + + # 填充B2单元格:年月 + sheet['B2'] = f'{title_year}年{title_month}月' + + # 统计导出数据中最常见的公司(用于C2单元格) + company_list = [] + for record in receivable_records: + if record.contract_id and record.contract_id.company_id: + company_list.append(record.contract_id.company_id.name) + + if company_list: + from collections import Counter + most_common_company = Counter(company_list).most_common(1)[0][0] + else: + most_common_company = '' + + # 填充C2单元格:所属公司 + sheet['C2'] = most_common_company + + # 从第4行开始写入数据(第1-2行是标题,第3行是表头) + row = 4 + + # 获取模板样式行(使用第4行作为参考,因为第3行是表头) + template_row_idx = 4 if sheet.max_row >= template_row_idx: template_styles = {} for col in range(1, 11): # A到J列(10列) @@ -266,10 +300,22 @@ class PropertyAccountsReceivable(models.Model): date_range = record.due_date.strftime('%Y-%m-%d') sheet.cell(row=row, column=2, value=date_range) - # C列:承租方名称(lessee_id) - sheet.cell(row=row, column=3, value=record.lessee_id.name if record.lessee_id else '') + # C列:金额(original_amount) + sheet.cell(row=row, column=3, value=record.original_amount or 0) - # D列:统一社会信用代码/纳税人识别号(从invoicing.information.line获取) + # D列:物业名称(property_name) + sheet.cell(row=row, column=4, value=record.property_name or '') + + # E列:合同编号(contract_code,从合同获取) + contract_code = '' + if record.contract_id: + contract_code = record.contract_id.contract_code or '' + sheet.cell(row=row, column=5, value=contract_code) + + # F列:承租方名称(lessee_id) + sheet.cell(row=row, column=6, value=record.lessee_id.name if record.lessee_id else '') + + # G列:统一社会信用代码/纳税人识别号(从invoicing.information.line获取) tax_number = '' if record.lessee_id and record.lessee_id.invoicing_ids: # 取第一条有效记录的税号 @@ -277,21 +323,15 @@ class PropertyAccountsReceivable(models.Model): if inv_line.tax_number: tax_number = inv_line.tax_number break - sheet.cell(row=row, column=4, value=tax_number) + sheet.cell(row=row, column=7, value=tax_number) - # E列:金额(original_amount) - sheet.cell(row=row, column=5, value=record.original_amount or 0) - - # F列:物业名称(property_name) - sheet.cell(row=row, column=6, value=record.property_name or '') - - # G列:产权证(title_information,从合同获取) + # H列:产权证(title_information,从合同获取) title_info = '' if record.contract_id: title_info = record.contract_id.title_information or '' - sheet.cell(row=row, column=7, value=title_info) + sheet.cell(row=row, column=8, value=title_info) - # H列:发票类型(从invoicing.information.line的invoicing_type_id获取) + # I列:发票类型(从invoicing.information.line的invoicing_type_id获取) invoicing_type = '' if record.lessee_id and record.lessee_id.invoicing_ids: # 取第一条有效记录的发票类型 @@ -299,19 +339,23 @@ class PropertyAccountsReceivable(models.Model): if inv_line.invoicing_type_id: invoicing_type = inv_line.invoicing_type_id.name or '' break - sheet.cell(row=row, column=8, value=invoicing_type) + sheet.cell(row=row, column=9, value=invoicing_type) - # I列:税率(从合同的tax_rate获取) - tax_rate_name = '' + # J列:税率(从合同的tax_rate获取,使用小数格式,如0.05) + tax_rate_value = 0.0 if record.contract_id and record.contract_id.tax_rate: - tax_rate_name = record.contract_id.tax_rate.name or '' - sheet.cell(row=row, column=9, value=tax_rate_name) - - # J列:合同编号(contract_code,从合同获取) - contract_code = '' - if record.contract_id: - contract_code = record.contract_id.contract_code or '' - sheet.cell(row=row, column=10, value=contract_code) + # 将税率名称(如"0.05"或"5%")转换为小数 + tax_rate_name = record.contract_id.tax_rate.name or '0' + try: + # 如果是百分比格式(如"5%"),转换为小数 + if '%' in tax_rate_name: + tax_rate_value = float(tax_rate_name.replace('%', '')) / 100 + else: + # 直接是小数格式(如"0.05") + tax_rate_value = float(tax_rate_name) + except (ValueError, TypeError): + tax_rate_value = 0.0 + sheet.cell(row=row, column=10, value=tax_rate_value) # 设置最小行高 if not sheet.row_dimensions[row].height or sheet.row_dimensions[row].height < 30: diff --git a/yuthon_property/static/tmpl/piliangkaipiao (2).xlsx b/yuthon_property/static/tmpl/piliangkaipiao (2).xlsx deleted file mode 100644 index efbdd3753..000000000 Binary files a/yuthon_property/static/tmpl/piliangkaipiao (2).xlsx and /dev/null differ diff --git a/yuthon_property/static/tmpl/piliangkaipiao (3).xlsx b/yuthon_property/static/tmpl/piliangkaipiao (3).xlsx new file mode 100644 index 000000000..67eabc0fd Binary files /dev/null and b/yuthon_property/static/tmpl/piliangkaipiao (3).xlsx differ