Class: Cats::Core::BeneficiaryService

Inherits:
Object
  • Object
show all
Defined in:
app/services/cats/core/beneficiary_service.rb

Instance Method Summary collapse

Instance Method Details

#clear_distribution_list(id) ⇒ Object

Raises:

  • (StandardError)


4
5
6
7
8
9
# File 'app/services/cats/core/beneficiary_service.rb', line 4

def clear_distribution_list(id)
  round_plan = RoundPlan.find(id)
  raise(StandardError, "Round plan is not in draft state.") unless round_plan.status == RoundPlan::DRAFT

  RoundBeneficiary.joins(:round_plan_item).where(round_plan_item: {round_plan_id: id}).delete_all
end

#confirm_receipt(plan_id, ids) ⇒ Object

Raises:

  • (StandardError)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/services/cats/core/beneficiary_service.rb', line 69

def confirm_receipt(plan_id, ids)
  raise(StandardError, "No beneficiaries specified.") if ids.count.zero?

  begin
    plan = RoundPlan.find(plan_id)
  rescue ActiveRecord::RecordNotFound
    raise(StandardError, "Round plan not found.") unless plan
  end

  beneficiaries = RoundBeneficiary.joins(:round_plan_item)
                                  .where(round_plan_item: {round_plan_id: plan_id})
  to_confirm = RoundBeneficiary.where(id: ids)
  diff = to_confirm - beneficiaries
  raise(StandardError, "Round plan beneficiaries should be from the same plan.") if diff.count.positive?

  to_confirm.update_all(received: true)
  true
end

#generate_distribution_list(id) ⇒ Object



11
12
13
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
45
46
47
48
# File 'app/services/cats/core/beneficiary_service.rb', line 11

def generate_distribution_list(id)
  clear_distribution_list(id)
  round_plan = RoundPlan.find(id)
  plan_days = round_plan.plan.no_of_round_days
  rounds = round_plan.rounds.count
  beneficiaries = Beneficiary.joins(:beneficiary_category).where(
    beneficiary_category: {plan_id: round_plan.plan_id}
  ).group_by(&:fdp_id)
  beneficiaries.each_key do |key|
    beneficiaries[key] = beneficiaries[key].group_by(&:beneficiary_category_id)
  end
  rations = round_plan.round_rations

  round_beneficiaries = []
  round_plan.round_plan_items.each do |rpi|
    fdp_beneficiaries = beneficiaries[rpi.fdp_id]
    next unless fdp_beneficiaries

    fdp_beneficiaries.each_key do |key|
      category_rations = rations.select { |r| r.beneficiary_category_id == key }
      category_rations.each do |ration|
        fdp_beneficiaries[key].each do |beneficiary|
          round_beneficiaries << {
            beneficiary_id: beneficiary.id,
            round_plan_item_id: rpi.id,
            commodity_category_id: ration.commodity_category_id,
            quantity: plan_days / ration.no_of_days * ration.quantity * rounds,
            unit_id: ration.unit_of_measure_id,
            received: false
          }
        end
      end
    end
  end

  RoundBeneficiary.insert_all!(round_beneficiaries, record_timestamps: true)
  true
end

#remove_items(plan_id, ids) ⇒ Object

Raises:

  • (StandardError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/services/cats/core/beneficiary_service.rb', line 50

def remove_items(plan_id, ids)
  raise(StandardError, "No beneficiaries specified.") if ids.count.zero?

  begin
    plan = RoundPlan.find(plan_id)
  rescue ActiveRecord::RecordNotFound
    raise(StandardError, "Round plan not found.") unless plan
  end

  beneficiaries = RoundBeneficiary.joins(:round_plan_item)
                                  .where(round_plan_item: {round_plan_id: plan_id})
  to_delete = RoundBeneficiary.where(id: ids)
  diff = to_delete - beneficiaries
  raise(StandardError, "Round plan beneficiaries should be from the same plan.") if diff.count.positive?

  to_delete.delete_all
  true
end