This commit is contained in:
gsinghpal
2026-02-23 00:32:20 -05:00
parent d6bac8e623
commit e8e554de95
549 changed files with 1330 additions and 124935 deletions

View File

@@ -254,126 +254,6 @@ class FusionClockReport(models.Model):
except Exception as e:
_logger.error("Fusion Clock: Error generating batch report: %s", e)
@api.model
def _backfill_historical_reports(self):
"""Create reports for all past pay periods that have attendance data
but no corresponding report. Does NOT send emails.
Called from settings button or post_init_hook on install.
"""
HrAtt = self.env['hr.attendance']
today = fields.Date.today()
# Get all distinct periods that have completed attendance records
all_atts = HrAtt.search([
('check_out', '!=', False),
('x_fclk_pay_period_start', '!=', False),
], order='x_fclk_pay_period_start')
# Group by (period_start, pay_period_label) to find distinct periods
period_map = {}
for att in all_atts:
key = att.x_fclk_pay_period_start
if key and key not in period_map:
period_map[key] = att.x_fclk_pay_period
ICP = self.env['ir.config_parameter'].sudo()
schedule_type = ICP.get_param('fusion_clock.pay_period_type', 'biweekly')
anchor_str = ICP.get_param('fusion_clock.pay_period_start', '')
if anchor_str:
try:
anchor = fields.Date.from_string(anchor_str)
except Exception:
anchor = None
else:
anchor = None
created_count = 0
for period_start_date, period_label in period_map.items():
# Calculate period end from the start date
_, period_end = HrAtt._calc_period(
schedule_type, anchor, period_start_date,
)
# Skip the current (incomplete) period
if period_end >= today:
continue
# Find all employees with attendance in this period
period_atts = HrAtt.search([
('check_out', '!=', False),
('x_fclk_pay_period_start', '=', period_start_date),
])
employee_ids = period_atts.mapped('employee_id')
for employee in employee_ids:
# Check if report already exists
existing = self.search([
('employee_id', '=', employee.id),
('date_start', '=', period_start_date),
('date_end', '=', period_end),
], limit=1)
if existing:
continue
emp_atts = period_atts.filtered(
lambda a, e=employee: a.employee_id == e
)
report = self.create({
'date_start': period_start_date,
'date_end': period_end,
'schedule_type': schedule_type,
'employee_id': employee.id,
'company_id': employee.company_id.id,
'attendance_ids': [(6, 0, emp_atts.ids)],
})
try:
report._generate_pdf()
report.state = 'generated'
created_count += 1
except Exception as e:
_logger.warning(
"Fusion Clock backfill: PDF failed for %s period %s: %s",
employee.name, period_label, e,
)
report.state = 'generated'
created_count += 1
# Batch report for this period
existing_batch = self.search([
('employee_id', '=', False),
('date_start', '=', period_start_date),
('date_end', '=', period_end),
], limit=1)
if not existing_batch and period_atts:
company = period_atts[0].employee_id.company_id
batch = self.create({
'date_start': period_start_date,
'date_end': period_end,
'schedule_type': schedule_type,
'employee_id': False,
'company_id': company.id,
'attendance_ids': [(6, 0, period_atts.ids)],
})
try:
batch._generate_pdf()
batch.state = 'generated'
created_count += 1
except Exception as e:
_logger.warning(
"Fusion Clock backfill: batch PDF failed for %s: %s",
period_label, e,
)
batch.state = 'generated'
created_count += 1
# Commit after each period to avoid losing everything on error
self.env.cr.commit() # pylint: disable=invalid-commit
_logger.info(
"Fusion Clock: Backfill complete. Created %d reports.", created_count,
)
return created_count
@api.model
def _calculate_current_period(self, schedule_type, period_start_str, reference_date):
"""Calculate the period start/end dates based on schedule type."""