119 lines
5.3 KiB
Python
119 lines
5.3 KiB
Python
from odoo import models, fields, api, _, SUPERUSER_ID
|
|
from datetime import datetime, timedelta
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
|
|
class ClickReconciliationWizard(models.Model):
|
|
_name = 'click.reconciliation.wizard'
|
|
|
|
message = fields.Text(string='确认信息', readonly=True)
|
|
select_month = fields.Selection([
|
|
('1', '一月'), ('2', '二月'), ('3', '三月'), ('4', '四月'),
|
|
('5', '五月'), ('6', '六月'), ('7', '七月'), ('8', '八月'),
|
|
('9', '九月'), ('10', '十月'), ('11', '十一月'), ('12', '十二月')
|
|
], string='月份', default=lambda self: str(datetime.now().month))
|
|
|
|
select_year = fields.Selection(
|
|
selection=[(str(i), str(i)) for i in range(2024, 2060)],
|
|
string='年份', default=lambda self: str(datetime.now().year)
|
|
)
|
|
lessor_ids = fields.Many2many('yuthon.property.company', string="出租方公司")
|
|
|
|
def action_confirm(self):
|
|
year = int(self.select_year)
|
|
month = int(self.select_month)
|
|
|
|
# 构建月份区间
|
|
month_start = datetime(year, month, 1).date()
|
|
if month == 12:
|
|
month_end = datetime(year + 1, 1, 1).date()
|
|
else:
|
|
month_end = datetime(year, month + 1, 1).date()
|
|
|
|
name_list = self.lessor_ids.mapped('referred')
|
|
if not name_list:
|
|
return
|
|
domain = [
|
|
('bank_state', 'in', ['not_reconciled', 'registered']),
|
|
]
|
|
domain += ['|'] * (len(name_list) - 1)
|
|
for name in name_list:
|
|
domain.append(('your_own_unit', 'ilike', name))
|
|
bank_statements = self.env["property.bank.statements"].search(domain, order='start_time asc')
|
|
stmt_groups = {}
|
|
for stmt in bank_statements:
|
|
stmt_datetime = fields.Datetime.from_string(stmt.start_time)
|
|
stmt_date = (stmt_datetime - timedelta(hours=8)).date()
|
|
if not (stmt_date.year == year and stmt_date.month == month):
|
|
continue
|
|
|
|
contract_ids = tuple(stmt.contract_ids.ids)
|
|
if not contract_ids:
|
|
continue
|
|
group_key = (month, contract_ids, stmt.id)
|
|
if group_key not in stmt_groups:
|
|
stmt_groups[group_key] = {
|
|
'total_stmt_amount': stmt.original_amount,
|
|
'statements': self.env["property.bank.statements"] + stmt,
|
|
}
|
|
|
|
for group_key, group_data in stmt_groups.items():
|
|
month_key, contract_ids, stmt_id = group_key
|
|
total_stmt_amount = group_data['total_stmt_amount']
|
|
statements = group_data['statements']
|
|
|
|
receivables = self.env["property.accounts.receivable"].search([
|
|
('is_reconciliation', '=', False),
|
|
('contract_id', 'in', list(contract_ids)),
|
|
('due_date', '>=', month_start),
|
|
('due_date', '<', month_end),
|
|
])
|
|
if not receivables:
|
|
continue
|
|
|
|
total_receivable = sum(rec.original_amount for rec in receivables)
|
|
has_prev_unreconciled = self.env["property.accounts.receivable"].search([
|
|
('contract_id', 'in', list(contract_ids)),
|
|
('is_reconciliation', '=', False),
|
|
('due_date', '<', month_start),
|
|
], limit=1)
|
|
|
|
if total_stmt_amount == total_receivable and not has_prev_unreconciled:
|
|
for stmt in statements:
|
|
stmt.write({
|
|
'change_amount': stmt.original_amount,
|
|
'bank_state': 'reconciled',
|
|
'reconciliation_time': fields.Datetime.now() - timedelta(hours=8),
|
|
'no_amount': 0,
|
|
'yes_amount': stmt.original_amount,
|
|
'marking': '系统一键对账'
|
|
})
|
|
|
|
for rec in receivables:
|
|
rec.write({
|
|
'is_reconciliation': True,
|
|
'delivered1': rec.original_amount,
|
|
'reconciliation_time': fields.Datetime.now() - timedelta(hours=8),
|
|
'payout_date': statements[0].start_time.date() if statements else False,
|
|
})
|
|
|
|
modify = self.env['receivable.modify.line'].search([
|
|
('receivable_id', '=', rec.id),
|
|
('bank_id', '=', statements[0].id),
|
|
('is_write', '=', True),
|
|
('is_penalty', '=', False),
|
|
('reconciliation_date', '=',
|
|
(fields.Datetime.from_string(statements[0].start_time) - timedelta(hours=8)).date()),
|
|
('amount', '=', rec.original_amount)
|
|
])
|
|
if not modify:
|
|
self.env['receivable.modify.line'].create({
|
|
'is_write': True,
|
|
'is_penalty': False,
|
|
'bank_id': statements[0].id,
|
|
'reconciliation_date': (fields.Datetime.from_string(statements[0].start_time) - timedelta(
|
|
hours=8)).date(),
|
|
'operation_date': fields.Date.today(),
|
|
'receivable_id': rec.id,
|
|
'amount': rec.original_amount,
|
|
}) |