Files
gzth/yuthon_salary/models/yuthon_salary_documents.py
2025-05-16 15:53:19 +08:00

64 lines
2.6 KiB
Python

from odoo import api, fields, models
from decimal import ROUND_HALF_UP, Decimal
class YuthonSalaryDocuments(models.Model):
_name = 'yuthon.salary.documents'
_description = '薪资附件管理'
current_year = fields.Integer(string='年份')
current_month = fields.Selection([
('1', '1月份'),
('2', '2月份'),
('3', '3月份'),
('4', '4月份'),
('5', '5月份'),
('6', '6月份'),
('7', '7月份'),
('8', '8月份'),
('9', '9月份'),
('10', '10月份'),
('11', '11月份'),
('12', '12月份')
], string="月份")
company_id = fields.Many2one('res.company', string='公司', index=True, default=lambda self: self.env.company)
documents_ids = fields.Many2many('ir.attachment', string="附件")
def documents_confirm(self):
month = int(self.current_month)
month_mapping = {
1: '一月', 2: '二月', 3: '三月', 4: '四月',
5: '五月', 6: '六月', 7: '七月', 8: '八月',
9: '九月', 10: '十月', 11: '十一月', 12: '十二月'
}
month_name = month_mapping.get(month, '一月')
payslip_ids = self.env['hr.payslip'].search([
('is_salary_release', '=', False),
('is_salary_push', '=', True),
('payslip_run_id.name', '=', f'{self.current_year}年-{month_name}'),
('company_id', '=', self.company_id.id) # 确保公司一致
])
# 定义精度为两位小数
TWOPLACES = Decimal('0.01')
# 按工资组分组并精确计算总和
group_data = {}
for payslip in payslip_ids:
group_id = payslip.salary_group_id.id
if group_id not in group_data:
group_data[group_id] = {
'salary_group_id': group_id,
'actual_salary': Decimal('0.00'), # 使用 Decimal 类型
'current_year': self.current_year,
'current_month': self.current_month,
'company_id': self.company_id.id
}
# 获取工资并转换为 Decimal,舍入到两位小数
salary = Decimal(str(payslip.actual_salary)).quantize(TWOPLACES, rounding=ROUND_HALF_UP)
group_data[group_id]['actual_salary'] += salary
distribution_model = self.env['salary.payroll.distribution']
for group_id, data in group_data.items():
# 将最终结果转回浮点数
data['actual_salary'] = float(data['actual_salary'])
distribution_model.create(data)