Class: Cats::Core::RoundPlanService

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

Instance Method Summary collapse

Instance Method Details

#approve(plan_id) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/services/cats/core/round_plan_service.rb', line 150

def approve(plan_id)
  plan = RoundPlan.find(plan_id)
  if plan.plan.program.code == "PSNP"
    rounds = plan.rounds.count
    item_ids = plan.beneficiary_round_plan_items.pluck("beneficiary_plan_item_id").flatten
    plan_items = BeneficiaryPlanItem.includes(plan_item: :fdp).where(id: item_ids)
    invalid = plan_items.select { |pi| pi.rounds - (pi.rounds_served || 0) < rounds }.map do |pi|
      pi.plan_item.fdp.name
    end

    if invalid.count.positive?
      msg = invalid.join(", ")
      error = "The FDPs [#{msg}] exceeded their allowed number of rounds."
      raise(StandardError, error)
    end

    items = plan_items.each_with_object([]) do |plan_item, res|
      rounds_served = plan_item.rounds_served || 0
      res << "(#{plan_item.id}, #{rounds + rounds_served})"
    end
    values = items.join(",")
    sql = <<-SQL
      UPDATE cats_core_beneficiary_plan_items AS bpi
      SET
        rounds_served = l.rounds_served
      FROM (
        VALUES
        #{values}
      ) AS l(id, rounds_served)
      WHERE l.id = bpi.id
    SQL
    ActiveRecord::Base.transaction do
      ActiveRecord::Base.connection.execute(sql)
      plan.approve
    end
  else
    plan.approve
  end
  send_approved_notification(plan)
  plan
end

#clear_needs(plan_id) ⇒ Object



42
43
44
45
46
47
# File 'app/services/cats/core/round_plan_service.rb', line 42

def clear_needs(plan_id)
  Cats::Core::RoundPlanItemDetail
    .joins(beneficiary_round_plan_item: :round_plan_item)
    .where(cats_core_beneficiary_round_plan_items: {cats_core_round_plan_items: {round_plan_id: plan_id}})
    .delete_all
end

#generate_round_needs(plan_id) ⇒ Object

Raises:

  • (StandardError)


4
5
6
7
8
9
10
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
# File 'app/services/cats/core/round_plan_service.rb', line 4

def generate_round_needs(plan_id)
  round_plan = Cats::Core::RoundPlan.find(plan_id)
  raise(StandardError, "Plan has no rations.") if round_plan.round_rations.count.zero?

  raise(StandardError, "Plan has no plan items.") if round_plan.round_plan_items.count.zero?

  clear_needs(round_plan.id)

  details = []
  rations = round_plan.round_rations
  plan = round_plan.plan
  no_of_days = plan.total_days / plan.rounds
  mt = UnitOfMeasure.find_by(abbreviation: "mt")
  round_plan.beneficiary_round_plan_items.each do |plan_item|
    round_rations = rations.select { |r| r.beneficiary_category_id == plan_item.beneficiary_category_id }
    round_rations.each do |ration|
      quantity = (no_of_days / ration.no_of_days * ration.quantity) * plan_item.beneficiaries *
                 round_plan.rounds.count
      quantity = quantity.truncate(2)
      unit = ration.unit
      if ration.unit.abbreviation.downcase != "pc"
        quantity = UnitConversion.convert(unit, mt, quantity).truncate(2)
        unit = mt
      end

      details << {
        beneficiary_round_plan_item_id: plan_item.id,
        round_ration_id: ration.id,
        quantity: quantity,
        unit_id: unit.id
      }
    end
  end

  Cats::Core::RoundPlanItemDetail.insert_all!(details)
  round_plan
end

#generate_round_plan(reference_no, rounds, plan_id, region_id) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/services/cats/core/round_plan_service.rb', line 49

def generate_round_plan(reference_no, rounds, plan_id, region_id)
  round_plan = Cats::Core::RoundPlan.create!(
    plan_id: plan_id,
    reference_no: reference_no,
    region_id: region_id,
    status: Cats::Core::Plan::DRAFT,
    rounds: rounds
  )
  rations = Cats::Core::Ration.joins(:beneficiary_category).where(beneficiary_category: {plan_id: plan_id})
  round_rations = rations.map do |ration|
    {
      beneficiary_category_id: ration.beneficiary_category_id,
      commodity_category_id: ration.commodity_category_id,
      quantity: ration.quantity,
      unit_of_measure_id: ration.unit_of_measure_id,
      round_plan_id: round_plan.id,
      no_of_days: ration.no_of_days
    }
  end
  Cats::Core::RoundRation.insert_all!(round_rations, record_timestamps: true)
  plan_items = Cats::Core::PlanItem.where(plan_id: plan_id, region_id: region_id)
  ben_plan_items = Cats::Core::BeneficiaryPlanItem.where(plan_item: plan_items)
  round_plan_items = plan_items.map do |plan_item|
    {
      round_plan_id: round_plan.id,
      region_id: region_id,
      zone_id: plan_item.zone_id,
      woreda_id: plan_item.woreda_id,
      fdp_id: plan_item.fdp_id,
      operator_id: plan_item.operator_id,
      plan_item_id: plan_item.id
    }
  end
  result = Cats::Core::RoundPlanItem.insert_all!(
    round_plan_items,
    returning: %w[id plan_item_id],
    record_timestamps: true
  )
  rows = result.rows
  ben_round_plan_items = ben_plan_items.map do |ben_plan_item|
    item = rows.find { |r| r[1] == ben_plan_item.plan_item_id }
    {
      round_plan_item_id: item[0],
      beneficiary_category_id: ben_plan_item.beneficiary_category_id,
      planned_beneficiaries: ben_plan_item.beneficiaries,
      beneficiaries: ben_plan_item.beneficiaries,
      beneficiary_plan_item_id: ben_plan_item.id
    }
  end
  Cats::Core::BeneficiaryRoundPlanItem.insert_all!(ben_round_plan_items, record_timestamps: true)
  send_approval_notification(round_plan)
  round_plan
end

#remove_items(plan_id, ids) ⇒ Object

This method deletes beneficiary round plan items based on the selection of the user in a given round.

Params:

plan_id => The id of the round plan.
ids => A list of beneficiary round plan item ids to delete.

When deleting the selected beneficiary round plan items, if the corresponding round plan item becomes empty, then it too should be deleted.

Raises:

  • (StandardError)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/services/cats/core/round_plan_service.rb', line 113

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

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

  plans = Cats::Core::RoundPlan.includes(:beneficiary_round_plan_items)
                               .where(beneficiary_round_plan_items: {id: ids})
  raise(StandardError, "Round plan items should be from the same plan.") if plans.count > 1

  raise(StandardError, "Round plan items are not from the given round plan.") unless plans.count.positive? && plan.id == plans[0].id

  to_delete = Cats::Core::BeneficiaryRoundPlanItem.where(id: ids)
  keys = to_delete.map(&:round_plan_item_id).uniq

  item_counts = Cats::Core::BeneficiaryRoundPlanItem.where(round_plan_item_id: keys)
                                                    .group(:round_plan_item_id).count

  delete_hash = {}
  keys.each { |k| delete_hash[k] = [] }
  to_delete.each { |td| delete_hash[td.round_plan_item_id] << td.id }

  parent_ids = []
  child_ids = []
  delete_hash.each_key do |key|
    child_ids += delete_hash[key]
    parent_ids << key if delete_hash[key].count == item_counts[key]
  end

  Cats::Core::BeneficiaryRoundPlanItem.delete_by(id: child_ids) if child_ids.count.positive?
  Cats::Core::RoundPlanItem.delete_by(id: parent_ids) if parent_ids.count.positive?
  plan
end

#send_approval_notification(round_plan) ⇒ Object

Raises:

  • (StandardError)


192
193
194
195
196
197
198
199
200
201
202
# File 'app/services/cats/core/round_plan_service.rb', line 192

def send_approval_notification(round_plan)
  notification_rule = NotificationRule.find_by(code: "round_plan_approval")
  error = "Notification rule not found for round plan approval notification."
  raise(StandardError, error) unless notification_rule

  users = User.joins(:roles).where(cats_core_roles: {name: notification_rule.roles})
  return if users.empty?

  notification = RoundPlanNotification.with(round_plan: round_plan)
  notification.deliver(users)
end

#send_approved_notification(round_plan) ⇒ Object

Raises:

  • (StandardError)


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/services/cats/core/round_plan_service.rb', line 204

def send_approved_notification(round_plan)
  notification_rule = NotificationRule.find_by(code: "round_plan_approved")
  error = "Notification rule not found for round plan approved notification."
  raise(StandardError, error) unless notification_rule

  users = User.joins(roles: :application_module).where(
    cats_core_roles: {
      name: notification_rule.roles,
      cats_core_application_modules: {prefix: "CATS-TMS"}
    }
  )
  return if users.empty?

  notification = RoundPlanNotification.with(round_plan: round_plan)
  notification.deliver(users)
end