Class: HrLite::PayrollRunProcessor

Inherits:
Object
  • Object
show all
Defined in:
app/services/hr_lite/payroll_run_processor.rb

Overview

(Re)computes every slip for a run in one transaction. Missing structures become warnings, manual overrides survive recomputes, employees who became ineligible lose their draft slips.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(run) ⇒ PayrollRunProcessor

Returns a new instance of PayrollRunProcessor.



10
11
12
# File 'app/services/hr_lite/payroll_run_processor.rb', line 10

def initialize(run)
  @run = run
end

Class Method Details

.call(run) ⇒ Object



6
7
8
# File 'app/services/hr_lite/payroll_run_processor.rb', line 6

def self.call(run)
  new(run).call
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/services/hr_lite/payroll_run_processor.rb', line 14

def call
  warnings = []

  ActiveRecord::Base.transaction do
    eligible = EmployeeProfile.active_for(@run.period_month).includes(:user).to_a
    keep_ids = []

    eligible.each do |profile|
      user = profile.user
      structure = SalaryStructure.effective_for(user, @run.period_month)
      if structure.nil?
        warnings << "No salary structure for #{HrLite.display_name(user)} — skipped"
        next
      end

      slip = @run.salary_slips.find_or_initialize_by(user_id: user.id)
      attributes = SlipBuilder.call(
        run: @run, user: user, structure: structure, profile: profile,
        lop_override: slip.lop_override, tds_override: slip.tds_override
      )
      slip.assign_attributes(attributes)
      slip.save!
      keep_ids << slip.id
    end

    @run.salary_slips.where.not(id: keep_ids).destroy_all
    @run.update!(warnings: warnings)
  end

  @run
end