cats_core 1.4.11 → 1.4.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/cats/core/{monthly_plans_controller.rb → round_plans_controller.rb} +11 -10
  3. data/app/models/cats/core/beneficiary.rb +15 -0
  4. data/app/models/cats/core/beneficiary_category.rb +10 -0
  5. data/app/models/cats/core/beneficiary_plan_item.rb +11 -0
  6. data/app/models/cats/core/beneficiary_round_plan_item.rb +14 -0
  7. data/app/models/cats/core/plan.rb +2 -2
  8. data/app/models/cats/core/plan_item.rb +9 -3
  9. data/app/models/cats/core/plan_item_detail.rb +4 -4
  10. data/app/models/cats/core/ration.rb +7 -1
  11. data/app/models/cats/core/{monthly_plan.rb → round_plan.rb} +22 -11
  12. data/app/models/cats/core/round_plan_item.rb +25 -0
  13. data/app/models/cats/core/round_plan_item_detail.rb +11 -0
  14. data/app/models/cats/core/round_ration.rb +17 -0
  15. data/app/models/cats/core/transport_order.rb +8 -1
  16. data/app/models/cats/core/transport_order_item.rb +4 -1
  17. data/app/serializers/cats/core/round_plan_serializer.rb +7 -0
  18. data/app/services/cats/core/round_plan_service.rb +108 -0
  19. data/config/routes.rb +5 -5
  20. data/db/migrate/20210717031810_create_cats_core_plans.rb +0 -4
  21. data/db/migrate/20210717032024_create_cats_core_plan_items.rb +13 -2
  22. data/db/migrate/20210717032260_create_cats_core_beneficiary_categories.rb +14 -0
  23. data/db/migrate/{20210717031343_create_cats_core_ration_items.rb → 20210717032270_create_cats_core_rations.rb} +9 -8
  24. data/db/migrate/20210717032290_create_cats_core_beneficiary_plan_items.rb +23 -0
  25. data/db/migrate/20210717032295_create_cats_core_plan_item_details.rb +18 -0
  26. data/db/migrate/{20220107121752_create_cats_core_monthly_plans.rb → 20220107121752_create_cats_core_round_plans.rb} +3 -4
  27. data/db/migrate/20220107122280_create_cats_core_round_rations.rb +26 -0
  28. data/db/migrate/20220107125025_create_cats_core_round_plan_items.rb +33 -0
  29. data/db/migrate/20220107126025_create_cats_core_beneficiary_round_plan_items.rb +24 -0
  30. data/db/migrate/20220107132433_create_cats_core_round_plan_item_details.rb +17 -0
  31. data/db/migrate/20220506082329_create_cats_core_transport_orders.rb +2 -1
  32. data/db/migrate/20220506083042_create_cats_core_transport_order_items.rb +0 -1
  33. data/db/migrate/20220511082354_create_cats_core_beneficiaries.rb +16 -0
  34. data/lib/cats/core/version.rb +1 -1
  35. data/spec/factories/cats/core/beneficiaries.rb +9 -0
  36. data/spec/factories/cats/core/beneficiary_categories.rb +7 -0
  37. data/spec/factories/cats/core/beneficiary_plan_items.rb +7 -0
  38. data/spec/factories/cats/core/beneficiary_round_plan_items.rb +8 -0
  39. data/spec/factories/cats/core/dispatch_plans.rb +3 -3
  40. data/spec/factories/cats/core/locations.rb +4 -4
  41. data/spec/factories/cats/core/plan_item_details.rb +3 -3
  42. data/spec/factories/cats/core/plan_items.rb +4 -2
  43. data/spec/factories/cats/core/plans.rb +0 -1
  44. data/spec/factories/cats/core/rations.rb +5 -2
  45. data/spec/factories/cats/core/round_plan_item_details.rb +7 -0
  46. data/spec/factories/cats/core/round_plan_items.rb +11 -0
  47. data/spec/factories/cats/core/{monthly_plans.rb → round_plans.rb} +2 -3
  48. data/spec/factories/cats/core/round_rations.rb +10 -0
  49. data/spec/factories/cats/core/transport_order_items.rb +0 -1
  50. data/spec/factories/cats/core/transport_orders.rb +1 -0
  51. metadata +31 -22
  52. data/app/models/cats/core/monthly_plan_item.rb +0 -15
  53. data/app/models/cats/core/monthly_plan_item_detail.rb +0 -14
  54. data/app/models/cats/core/monthly_ration.rb +0 -15
  55. data/app/models/cats/core/ration_item.rb +0 -14
  56. data/app/serializers/cats/core/monthly_plan_serializer.rb +0 -8
  57. data/app/services/cats/core/monthly_plan_service.rb +0 -86
  58. data/db/migrate/20210717031216_create_cats_core_rations.rb +0 -14
  59. data/db/migrate/20210717032240_create_cats_core_plan_item_details.rb +0 -18
  60. data/db/migrate/20220107124630_create_cats_core_monthly_rations.rb +0 -22
  61. data/db/migrate/20220107125025_create_cats_core_monthly_plan_items.rb +0 -17
  62. data/db/migrate/20220107132433_create_cats_core_monthly_plan_item_details.rb +0 -17
  63. data/spec/factories/cats/core/monthly_plan_item_details.rb +0 -7
  64. data/spec/factories/cats/core/monthly_plan_items.rb +0 -7
  65. data/spec/factories/cats/core/monthly_rations.rb +0 -9
  66. data/spec/factories/cats/core/ration_items.rb +0 -9
@@ -1,86 +0,0 @@
1
- module Cats
2
- module Core
3
- class MonthlyPlanService
4
- def generate_monthly_needs(plan_id)
5
- plan = Cats::Core::MonthlyPlan.find(plan_id)
6
- raise(StandardError, 'Plan has no rations.') if plan.monthly_rations.count.zero?
7
-
8
- raise(StandardError, 'Plan has no plan items.') if plan.monthly_plan_items.count.zero?
9
-
10
- plan.monthly_plan_item_details.delete_all if plan.monthly_plan_item_details.count.positive?
11
-
12
- details = []
13
- plan.monthly_plan_items.each do |plan_item|
14
- plan.monthly_rations.each do |ration|
15
- details << {
16
- monthly_plan_item_id: plan_item.id,
17
- monthly_ration_id: ration.id,
18
- amount: (plan.no_of_days / ration.no_of_days * ration.amount) * plan_item.beneficiaries,
19
- created_at: Date.today,
20
- updated_at: Date.today
21
- }
22
- end
23
- end
24
-
25
- Cats::Core::MonthlyPlanItemDetail.insert_all!(details)
26
- plan
27
- end
28
-
29
- def generate_monthly_plan(params)
30
- region = Cats::Core::Location.find(params[:region_id])
31
- plan = Cats::Core::Plan.includes(plan_items: :plan_item_details).find(params[:plan_id])
32
- rations = Cats::Core::RationItem.where(ration_id: plan.ration_id)
33
- monthly_plan = Cats::Core::MonthlyPlan.create!(
34
- plan_id: plan.id,
35
- reference_no: params[:reference_no],
36
- region_id: params[:region_id],
37
- status: Cats::Core::Plan::DRAFT,
38
- month: params[:month],
39
- no_of_days: params[:no_of_days]
40
- )
41
- ration_items = rations.map do |ration|
42
- {
43
- commodity_category_id: ration.commodity_category_id,
44
- amount: ration.amount,
45
- unit_of_measure_id: ration.unit_of_measure_id,
46
- no_of_days: ration.no_of_days,
47
- monthly_plan_id: monthly_plan.id,
48
- created_at: Date.today,
49
- updated_at: Date.today
50
- }
51
- end
52
- Cats::Core::MonthlyRation.insert_all!(ration_items)
53
-
54
- plan_items = plan.plan_items.where(woreda_id: region.descendant_ids).map do |plan_item|
55
- {
56
- monthly_plan_id: monthly_plan.id,
57
- beneficiaries: plan_item.beneficiaries,
58
- plan_item_id: plan_item.id,
59
- created_at: Date.today,
60
- updated_at: Date.today
61
- }
62
- end
63
- Cats::Core::MonthlyPlanItem.insert_all!(plan_items)
64
- monthly_plan
65
- end
66
-
67
- def remove_items(plan_id, ids)
68
- raise(StandardError, 'No plan items specified.') if ids.count.zero?
69
-
70
- begin
71
- plan = Cats::Core::MonthlyPlan.find(plan_id)
72
- rescue ActiveRecord::RecordNotFound
73
- raise(StandardError, 'Monthly plan not found.') unless plan
74
- end
75
-
76
- plans = Cats::Core::MonthlyPlan.includes(:monthly_plan_items).where(monthly_plan_items: { id: ids })
77
- raise(StandardError, 'Plan items should be from the same plan.') if plans.count > 1
78
-
79
- raise(StandardError, 'Items are not from the given monthly plan.') unless plan_id == plans[0].id
80
-
81
- Cats::Core::MonthlyPlanItem.delete_by(id: ids)
82
- plan
83
- end
84
- end
85
- end
86
- end
@@ -1,14 +0,0 @@
1
- class CreateCatsCoreRations < ActiveRecord::Migration[6.1]
2
- def change
3
- create_table :cats_core_rations do |t|
4
- t.string :reference_no, unique: true
5
- t.references :program,
6
- null: false,
7
- index: { name: 'program_on_ration_indx' },
8
- foreign_key: { to_table: :cats_core_programs }
9
- t.boolean :current, null: false, default: true
10
-
11
- t.timestamps
12
- end
13
- end
14
- end
@@ -1,18 +0,0 @@
1
- class CreateCatsCorePlanItemDetails < ActiveRecord::Migration[6.1]
2
- def change
3
- create_table :cats_core_plan_item_details do |t|
4
- t.references :plan_item,
5
- null: false,
6
- index: { name: 'pi_on_pid_indx' },
7
- foreign_key: { to_table: :cats_core_plan_items }
8
- t.references :ration_item,
9
- null: false,
10
- index: { name: 'ri_on_pid_indx' },
11
- foreign_key: { to_table: :cats_core_ration_items }
12
- t.float :amount, null: false
13
-
14
- t.timestamps
15
- end
16
- add_index(:cats_core_plan_item_details, [:plan_item_id, :ration_item_id], unique: true, name: 'pii_rii_on_pid_indx')
17
- end
18
- end
@@ -1,22 +0,0 @@
1
- class CreateCatsCoreMonthlyRations < ActiveRecord::Migration[6.1]
2
- def change
3
- create_table :cats_core_monthly_rations do |t|
4
- t.references :commodity_category,
5
- null: false,
6
- index: { name: 'cc_on_mr_indx' },
7
- foreign_key: { to_table: :cats_core_commodity_categories }
8
- t.float :amount, null: false
9
- t.references :unit_of_measure,
10
- null: false,
11
- index: { name: 'uom_on_mr_indx' },
12
- foreign_key: { to_table: :cats_core_unit_of_measures }
13
- t.integer :no_of_days, null: false
14
- t.references :monthly_plan,
15
- index: { name: 'mp_on_mr_indx' },
16
- null: false,
17
- foreign_key: { to_table: :cats_core_monthly_plans }
18
-
19
- t.timestamps
20
- end
21
- end
22
- end
@@ -1,17 +0,0 @@
1
- class CreateCatsCoreMonthlyPlanItems < ActiveRecord::Migration[6.1]
2
- def change
3
- create_table :cats_core_monthly_plan_items do |t|
4
- t.references :monthly_plan,
5
- null: false,
6
- index: { name: 'mp_on_mpi_indx' },
7
- foreign_key: { to_table: :cats_core_monthly_plans }
8
- t.integer :beneficiaries, null: false
9
- t.references :plan_item,
10
- null: false,
11
- index: { name: 'pi_on_mpi_indx' },
12
- foreign_key: { to_table: :cats_core_plan_items }
13
-
14
- t.timestamps
15
- end
16
- end
17
- end
@@ -1,17 +0,0 @@
1
- class CreateCatsCoreMonthlyPlanItemDetails < ActiveRecord::Migration[6.1]
2
- def change
3
- create_table :cats_core_monthly_plan_item_details do |t|
4
- t.references :monthly_plan_item,
5
- null: false,
6
- index: { name: 'mpi_on_mpid_indx' },
7
- foreign_key: { to_table: :cats_core_monthly_plan_items }
8
- t.references :monthly_ration,
9
- null: false,
10
- index: { name: 'ri_on_mr_indx' },
11
- foreign_key: { to_table: :cats_core_monthly_rations }
12
- t.float :amount, null: false
13
-
14
- t.timestamps
15
- end
16
- end
17
- end
@@ -1,7 +0,0 @@
1
- FactoryBot.define do
2
- factory :monthly_plan_item_detail, class: 'Cats::Core::MonthlyPlanItemDetail' do
3
- monthly_plan_item
4
- monthly_ration
5
- amount { 150 }
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- FactoryBot.define do
2
- factory :monthly_plan_item, class: 'Cats::Core::MonthlyPlanItem' do
3
- monthly_plan
4
- beneficiaries { 100 }
5
- plan_item
6
- end
7
- end
@@ -1,9 +0,0 @@
1
- FactoryBot.define do
2
- factory :monthly_ration, class: 'Cats::Core::MonthlyRation' do
3
- commodity_category
4
- amount { 100 }
5
- unit_of_measure
6
- no_of_days { 30 }
7
- monthly_plan
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- FactoryBot.define do
2
- factory :ration_item, class: 'Cats::Core::RationItem' do
3
- commodity_category
4
- ration
5
- amount { 150 }
6
- unit_of_measure
7
- no_of_days { 1 }
8
- end
9
- end