cats_core 1.4.41 → 1.4.44

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/cats/core/cash_donations_controller.rb +19 -0
  3. data/app/controllers/cats/core/commodity_donations_controller.rb +22 -0
  4. data/app/controllers/cats/core/dispatch_plans_controller.rb +24 -0
  5. data/app/controllers/cats/core/loans_controller.rb +20 -0
  6. data/app/controllers/cats/core/purchase_orders_controller.rb +24 -0
  7. data/app/controllers/cats/core/round_beneficiaries_controller.rb +49 -0
  8. data/app/controllers/cats/core/swaps_controller.rb +21 -0
  9. data/app/models/cats/core/beneficiary.rb +9 -0
  10. data/app/models/cats/core/cash_donation.rb +11 -0
  11. data/app/models/cats/core/commodity_donation.rb +15 -0
  12. data/app/models/cats/core/dispatch_plan.rb +1 -1
  13. data/app/models/cats/core/donation.rb +2 -16
  14. data/app/models/cats/core/gift_certificate.rb +3 -23
  15. data/app/models/cats/core/loan.rb +15 -0
  16. data/app/models/cats/core/plan.rb +4 -0
  17. data/app/models/cats/core/purchase_order.rb +5 -13
  18. data/app/models/cats/core/rhn_request.rb +4 -4
  19. data/app/models/cats/core/round_beneficiary.rb +16 -0
  20. data/app/models/cats/core/round_plan.rb +2 -4
  21. data/app/models/cats/core/round_plan_item.rb +1 -0
  22. data/app/models/cats/core/swap.rb +19 -0
  23. data/app/models/concerns/cats/core/dispatchable.rb +8 -0
  24. data/app/serializers/cats/core/cash_donation_serializer.rb +8 -0
  25. data/app/serializers/cats/core/commodity_donation_serializer.rb +9 -0
  26. data/app/serializers/cats/core/loan_serializer.rb +8 -0
  27. data/app/serializers/cats/core/purchase_order_serializer.rb +9 -0
  28. data/app/serializers/cats/core/round_beneficiary_serializer.rb +8 -0
  29. data/app/serializers/cats/core/swap_serializer.rb +9 -0
  30. data/app/services/cats/core/beneficiary_service.rb +87 -0
  31. data/app/services/cats/core/dispatch_plan_service.rb +79 -0
  32. data/config/routes.rb +19 -0
  33. data/db/migrate/20210717032330_create_cats_core_commodity_donations.rb +29 -0
  34. data/db/migrate/20210717032408_create_cats_core_cash_donations.rb +20 -0
  35. data/db/migrate/20210717032602_create_cats_core_gift_certificates.rb +2 -8
  36. data/db/migrate/20210717032855_create_cats_core_purchase_orders.rb +9 -5
  37. data/db/migrate/20220511082354_create_cats_core_beneficiaries.rb +4 -0
  38. data/db/migrate/20220626063501_create_cats_core_loans.rb +21 -0
  39. data/db/migrate/20220626063757_create_cats_core_swaps.rb +29 -0
  40. data/db/migrate/20220626132050_create_cats_core_round_beneficiaries.rb +26 -0
  41. data/lib/cats/core/version.rb +1 -1
  42. data/spec/factories/cats/core/beneficiaries.rb +1 -0
  43. data/spec/factories/cats/core/cash_donations.rb +10 -0
  44. data/spec/factories/cats/core/{donations.rb → commodity_donations.rb} +3 -6
  45. data/spec/factories/cats/core/gift_certificates.rb +5 -6
  46. data/spec/factories/cats/core/loans.rb +11 -0
  47. data/spec/factories/cats/core/projects.rb +1 -1
  48. data/spec/factories/cats/core/purchase_orders.rb +3 -1
  49. data/spec/factories/cats/core/round_beneficiaries.rb +10 -0
  50. data/spec/factories/cats/core/swaps.rb +13 -0
  51. metadata +33 -7
  52. data/db/migrate/20210717032330_create_cats_core_donations.rb +0 -35
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4084a8e84ceacfb0f4e96ebba6f9c1bfb41cdf64e36341955dfd86131a5de89e
4
- data.tar.gz: c091888b5e67a9ecc85e3dce2e5fa33aacab4d617bf31767ded0117329c2f4db
3
+ metadata.gz: fba88be856ad7a27b81c7eca20c2867775d1bd9963b84d6fa4b48ddaa688008f
4
+ data.tar.gz: 50ed67dd7ae4dca66b27205764d13c9a72b53d4088158c09b0165c45f4bd12e2
5
5
  SHA512:
6
- metadata.gz: ca7b27982330acd7df6ebf118e9412fcca962f6436903f7e48c0f1603b5d1bcc1a04b6bb5a508cd7579aa42bedb0e62ed548495759f7a7291b91f3f064ccaaa7
7
- data.tar.gz: dac15feb2a1280d856a5ac280498a4b5e7c1585bccdcf74d4a5f78eb41a8e7562c6bdeebf5f53c150586d964cdd2af5663fc5fa588c6765917118526bd7c57c2
6
+ metadata.gz: 2ed1ee00dbfc76d8bee529ac9e2788b83183159c8f27059b5c14fafab2129ba60b04d83bf503e5efd1e45581391f9a033d18c00ba24bdc6ca15aee3155f5706e
7
+ data.tar.gz: 2ed989fb152cf0488ce958c373b66b0e1dca324453d82d1ffe3139e4abca362c9926373bc6eb60e85ddb9a7901b466f07711766b682f90de3f020f01df2764df
@@ -0,0 +1,19 @@
1
+ module Cats
2
+ module Core
3
+ class CashDonationsController < ApplicationController
4
+ include Common
5
+
6
+ def index
7
+ super do
8
+ CashDonation.includes(:donor, :currency).all
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def model_params
15
+ params.require(:payload).permit(:reference_no, :description, :donated_on, :donor_id, :amount, :currency_id)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ module Cats
2
+ module Core
3
+ class CommodityDonationsController < ApplicationController
4
+ include Common
5
+
6
+ def index
7
+ super do
8
+ CommodityDonation.includes(:donor, :plan, :unit, :commodity_category).all
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def model_params
15
+ params.require(:payload).permit(
16
+ :reference_no, :description, :shipping_reference, :donated_on, :donor_id, :plan_id, :quantity,
17
+ :commodity_category_id, :unit_id
18
+ )
19
+ end
20
+ end
21
+ end
22
+ end
@@ -27,6 +27,18 @@ module Cats
27
27
  render json: { success: false, error: e.message }, status: :unprocessable_entity
28
28
  end
29
29
 
30
+ def generate
31
+ service = DispatchPlanService.new
32
+ items = service.generate(params[:id])
33
+ render json: { success: true, data: serialize(items) }
34
+ end
35
+
36
+ def bulk_create
37
+ service = DispatchPlanService.new
38
+ plan = service.bulk_create(bulk_create_params, current_user)
39
+ render json: { success: true, data: serialize(plan) }
40
+ end
41
+
30
42
  def approve
31
43
  service = DispatchPlanService.new
32
44
  plan = DispatchPlan.find(params[:id])
@@ -41,6 +53,18 @@ module Cats
41
53
  def model_params
42
54
  params.require(:payload).permit(:reference_no, :dispatchable_id, :dispatchable_type)
43
55
  end
56
+
57
+ def bulk_create_params
58
+ params.require(:payload).permit(
59
+ :reference_no,
60
+ :dispatchable_id,
61
+ :dispatchable_type,
62
+ items: %i[
63
+ reference_no region zone woreda fdp commodity_category unit_id unit quantity source_id destination_id
64
+ commodity_id
65
+ ]
66
+ )
67
+ end
44
68
  end
45
69
  end
46
70
  end
@@ -0,0 +1,20 @@
1
+ module Cats
2
+ module Core
3
+ class LoansController < ApplicationController
4
+ include Common
5
+
6
+ def index
7
+ super do
8
+ Loan.includes(:commodity_category, :unit).all
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def model_params
15
+ params.require(:payload).permit(:reference_no, :lender, :agreement_date, :repayment_date, :quantity, :unit_id,
16
+ :commodity_category_id)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ module Cats
2
+ module Core
3
+ class PurchaseOrdersController < ApplicationController
4
+ include Common
5
+
6
+ def index
7
+ purchase_orders = Cats::Core::PurchaseOrder.where(cash_donation_id: params[:id]).includes(
8
+ :commodity_category,
9
+ :unit,
10
+ :currency
11
+ )
12
+ render json: { success: true, data: serialize(purchase_orders) }
13
+ end
14
+
15
+ private
16
+
17
+ def model_params
18
+ params.require(:payload).permit(:reference_no, :order_date, :requisition_no, :supplier, :cash_donation_id,
19
+ :commodity_category_id, :quantity, :purchase_type, :unit_id, :price,
20
+ :currency_id)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,49 @@
1
+ module Cats
2
+ module Core
3
+ class RoundBeneficiariesController < ApplicationController
4
+ include Common
5
+
6
+ def index
7
+ beneficiaries = RoundBeneficiary.joins(:round_plan_item)
8
+ .where(round_plan_item: { round_plan_id: params[:id] })
9
+ .includes(:beneficiary, :commodity_category, :unit)
10
+ render json: { success: true, data: serialize(beneficiaries) }
11
+ end
12
+
13
+ def generate
14
+ service = BeneficiaryService.new
15
+ result = service.generate_distribution_list(params[:id])
16
+ render json: { success: result }
17
+ rescue StandardError => e
18
+ render json: { success: false, error: e.message }
19
+ end
20
+
21
+ def filter
22
+ query = RoundBeneficiary.includes(:beneficiary, :commodity_category, :unit).ransack(params[:q])
23
+ render json: { success: true, data: serialize(query.result) }
24
+ end
25
+
26
+ def remove_beneficiaries
27
+ service = BeneficiaryService.new
28
+ result = service.remove_items(params[:id], common_params[:ids])
29
+ render json: { success: result }
30
+ rescue StandardError => e
31
+ render json: { success: false, error: e.message }
32
+ end
33
+
34
+ def confirm_receipt
35
+ service = BeneficiaryService.new
36
+ result = service.confirm_receipt(params[:id], common_params[:ids])
37
+ render json: { success: result }
38
+ rescue StandardError => e
39
+ render json: { success: false, error: e.message }
40
+ end
41
+
42
+ private
43
+
44
+ def common_params
45
+ params.permit(ids: [])
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,21 @@
1
+ module Cats
2
+ module Core
3
+ class SwapsController < ApplicationController
4
+ include Common
5
+
6
+ def index
7
+ super do
8
+ Swap.includes(:issued_commodity, :issued_unit, :received_commodity, :received_unit).all
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def model_params
15
+ params.require(:payload).permit(:reference_no, :swapper, :agreement_date, :issued_commodity_id,
16
+ :issued_quantity, :received_commodity_id, :received_quantity,
17
+ :issued_unit_id, :received_unit_id)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -6,12 +6,21 @@ module Cats
6
6
  GENDERS = [MALE, FEMALE].freeze
7
7
 
8
8
  belongs_to :beneficiary_category
9
+ belongs_to :fdp, class_name: 'Location'
9
10
 
10
11
  validates :full_name, :age, :gender, presence: true
11
12
  validates :gender, inclusion: { in: GENDERS }
12
13
  validates :age, numericality: { greater_than: 0, less_than: 100 }
14
+ validate :validate_fdp
13
15
 
14
16
  delegate(:name, to: :beneficiary_category, prefix: true)
17
+ delegate(:name, to: :fdp, prefix: true)
18
+
19
+ def validate_fdp
20
+ return unless fdp
21
+
22
+ errors.add(:fdp, 'should be a valid distribution point.') unless fdp.location_type == Location::FDP
23
+ end
15
24
  end
16
25
  end
17
26
  end
@@ -0,0 +1,11 @@
1
+ module Cats
2
+ module Core
3
+ class CashDonation < Donation
4
+ belongs_to :currency
5
+
6
+ validates :amount, presence: true, numericality: { greater_than: 0 }
7
+
8
+ delegate(:code, to: :currency, prefix: true)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module Cats
2
+ module Core
3
+ class CommodityDonation < Donation
4
+ belongs_to :plan, optional: true
5
+ belongs_to :commodity_category
6
+ belongs_to :unit, class_name: 'UnitOfMeasure'
7
+
8
+ validates :quantity, presence: true, numericality: { greater_than: 0 }
9
+
10
+ delegate(:reference_no, to: :plan, prefix: true, allow_nil: true)
11
+ delegate(:name, to: :commodity_category, prefix: true)
12
+ delegate(:abbreviation, to: :unit, prefix: true)
13
+ end
14
+ end
15
+ end
@@ -32,7 +32,7 @@ module Cats
32
32
 
33
33
  return if dispatchable.status == RhnRequest::APPROVED
34
34
 
35
- errors.add(:dispatchable, 'is already reserved.') if id.nil?
35
+ errors.add(:dispatchable, 'is already reserved.') if id.nil? && dispatchable.status == Dispatchable::RESERVED
36
36
  end
37
37
 
38
38
  def approve
@@ -1,28 +1,14 @@
1
1
  module Cats
2
2
  module Core
3
3
  class Donation < ApplicationRecord
4
- CASH = 'Cash'.freeze
5
- KIND = 'Kind'.freeze
6
- DONATION_TYPES = [CASH, KIND].freeze
4
+ self.abstract_class = true
7
5
 
8
6
  belongs_to :donor
9
- belongs_to :plan, optional: true
10
- belongs_to :currency, optional: true
11
- belongs_to :commodity_category, optional: true
12
- belongs_to :unit_of_measure, optional: true
13
7
 
14
- validates :reference_no, :amount, :donation_type, :donated_on, presence: true
8
+ validates :reference_no, :donated_on, presence: true
15
9
  validates :reference_no, uniqueness: true
16
- validates :donation_type, inclusion: { in: DONATION_TYPES }
17
- validates :currency, presence: true, if: -> { donation_type == CASH }
18
- validates :commodity_category, presence: true, if: -> { donation_type == KIND }
19
- validates :unit_of_measure, presence: true, if: -> { donation_type == KIND }
20
10
 
21
11
  delegate(:name, to: :donor, prefix: true)
22
- delegate(:reference_no, to: :plan, prefix: true, allow_nil: true)
23
- delegate(:name, to: :commodity_category, prefix: true, allow_nil: true)
24
- delegate(:code, to: :currency, prefix: true, allow_nil: true)
25
- delegate(:abbreviation, to: :unit_of_measure, prefix: true, allow_nil: true)
26
12
  end
27
13
  end
28
14
  end
@@ -3,9 +3,9 @@ module Cats
3
3
  class GiftCertificate < ApplicationRecord
4
4
  belongs_to :donation, optional: true
5
5
  belongs_to :commodity_category
6
- belongs_to :unit_of_measure
6
+ belongs_to :unit, class_name: 'UnitOfMeasure'
7
7
  belongs_to :currency
8
- belongs_to :destination_warehouse, class_name: 'Cats::Core::Location'
8
+ belongs_to :destination_warehouse, class_name: 'Location'
9
9
 
10
10
  validates :reference_no, presence: true, uniqueness: true
11
11
  validates :gift_date, :quantity, :requested_by, :customs_office, presence: true
@@ -13,28 +13,8 @@ module Cats
13
13
 
14
14
  delegate(:name, to: :commodity_category, prefix: true)
15
15
  delegate(:name, to: :destination_warehouse, prefix: true)
16
- delegate(:abbreviation, to: :unit_of_measure, prefix: true)
16
+ delegate(:abbreviation, to: :unit, prefix: true)
17
17
  delegate(:code, to: :currency, prefix: true)
18
-
19
- before_validation :set_commodity_category
20
- after_create :update_donation
21
-
22
- def update_donation
23
- return unless donation
24
-
25
- return if donation.active
26
-
27
- donation.active = true
28
- donation.save!
29
- end
30
-
31
- def set_commodity_category
32
- return unless donation
33
-
34
- return if commodity_category == donation.commodity_category
35
-
36
- self.commodity_category = donation.commodity_category
37
- end
38
18
  end
39
19
  end
40
20
  end
@@ -0,0 +1,15 @@
1
+ module Cats
2
+ module Core
3
+ class Loan < ApplicationRecord
4
+ belongs_to :commodity_category
5
+ belongs_to :unit, class_name: 'UnitOfMeasure'
6
+
7
+ validates :reference_no, presence: true, uniqueness: true
8
+ validates :lender, :agreement_date, presence: true
9
+ validates :quantity, presence: true, numericality: { greater_than: 0 }
10
+
11
+ delegate(:name, to: :commodity_category, prefix: true)
12
+ delegate(:abbreviation, to: :unit, prefix: true)
13
+ end
14
+ end
15
+ end
@@ -27,6 +27,10 @@ module Cats
27
27
  validates :season, presence: true, inclusion: { in: SEASONS }
28
28
  validates :reference_no, uniqueness: true
29
29
  validates :status, inclusion: { in: STATUSES }
30
+
31
+ def no_of_round_days
32
+ total_days / rounds
33
+ end
30
34
  end
31
35
  end
32
36
  end
@@ -5,27 +5,19 @@ module Cats
5
5
  FOREIGN = 'Foreign'.freeze
6
6
  PURCHASE_TYPES = [LOCAL, FOREIGN].freeze
7
7
 
8
- belongs_to :donation
8
+ belongs_to :cash_donation
9
9
  belongs_to :commodity_category
10
+ belongs_to :currency
10
11
  belongs_to :unit, class_name: 'Cats::Core::UnitOfMeasure'
11
12
 
12
- validates :reference_no, :order_date, :supplier, :quantity, :purchase_type, presence: true
13
+ validates :reference_no, :order_date, :supplier, :purchase_type, presence: true
13
14
  validates :reference_no, uniqueness: true
14
15
  validates :purchase_type, inclusion: { in: PURCHASE_TYPES }
15
- validates :quantity, numericality: { greater_than: 0 }
16
+ validates :quantity, :price, presence: true, numericality: { greater_than: 0 }
16
17
 
17
18
  delegate(:name, to: :commodity_category, prefix: true)
18
19
  delegate(:name, to: :unit, prefix: true)
19
-
20
- after_create :update_donation
21
-
22
- def update_donation
23
- donation = self.donation
24
- return if donation.active
25
-
26
- donation.active = true
27
- donation.save!
28
- end
20
+ delegate(:code, to: :currency, prefix: true)
29
21
  end
30
22
  end
31
23
  end
@@ -3,11 +3,11 @@ module Cats
3
3
  class RhnRequest < ApplicationRecord
4
4
  include Dispatchable
5
5
 
6
- DRAFT = 'Draft'.freeze
7
- APPROVED = 'Approved'.freeze
8
- RESERVED = 'Reserved'.freeze
6
+ # DRAFT = 'Draft'.freeze
7
+ # APPROVED = 'Approved'.freeze
8
+ # RESERVED = 'Reserved'.freeze
9
9
  ALLOCATED = 'Allocated'.freeze
10
- STATUSES = [DRAFT, APPROVED, RESERVED, ALLOCATED].freeze
10
+ STATUSES << ALLOCATED
11
11
 
12
12
  belongs_to :commodity
13
13
 
@@ -0,0 +1,16 @@
1
+ module Cats
2
+ module Core
3
+ class RoundBeneficiary < ApplicationRecord
4
+ belongs_to :beneficiary
5
+ belongs_to :round_plan_item
6
+ belongs_to :commodity_category
7
+ belongs_to :unit, class_name: 'UnitOfMeasure'
8
+
9
+ validates :quantity, presence: true, numericality: { greater_than: 0 }
10
+
11
+ delegate(:full_name, to: :beneficiary, prefix: true)
12
+ delegate(:name, to: :commodity_category, prefix: true)
13
+ delegate(:abbreviation, to: :unit, prefix: true)
14
+ end
15
+ end
16
+ end
@@ -3,12 +3,9 @@ module Cats
3
3
  class RoundPlan < ApplicationRecord
4
4
  include Dispatchable
5
5
 
6
- DRAFT = 'Draft'.freeze
7
- APPROVED = 'Approved'.freeze
8
- RESERVED = 'Reserved'.freeze
9
6
  NEEDS_APPROVED = 'Needs Approved'.freeze
10
7
 
11
- STATUSES = [DRAFT, APPROVED, RESERVED, NEEDS_APPROVED].freeze
8
+ STATUSES << NEEDS_APPROVED
12
9
 
13
10
  belongs_to :plan
14
11
  belongs_to :region, class_name: 'Cats::Core::Location'
@@ -17,6 +14,7 @@ module Cats
17
14
  has_many :round_rations
18
15
  has_many :beneficiary_round_plan_items, through: :round_plan_items
19
16
  has_many :round_plan_item_details, through: :beneficiary_round_plan_items
17
+ has_many :round_beneficiaries, through: :round_plan_items
20
18
 
21
19
  validates :rounds, presence: true
22
20
  validates :reference_no, presence: true, uniqueness: true
@@ -10,6 +10,7 @@ module Cats
10
10
 
11
11
  has_many :beneficiary_round_plan_items
12
12
  has_many :round_plan_item_details, through: :beneficiary_round_plan_items
13
+ has_many :round_beneficiaries
13
14
 
14
15
  validates :plan_item_id, presence: true
15
16
  validates :round_plan_id, uniqueness: { scope: :fdp_id }
@@ -0,0 +1,19 @@
1
+ module Cats
2
+ module Core
3
+ class Swap < ApplicationRecord
4
+ belongs_to :issued_commodity, class_name: 'CommodityCategory'
5
+ belongs_to :issued_unit, class_name: 'UnitOfMeasure'
6
+ belongs_to :received_commodity, class_name: 'CommodityCategory'
7
+ belongs_to :received_unit, class_name: 'UnitOfMeasure'
8
+
9
+ validates :reference_no, presence: true, uniqueness: true
10
+ validates :swapper, :agreement_date, :issued_quantity, :received_quantity, presence: true
11
+ validates :issued_quantity, :received_quantity, presence: true, numericality: { greater_than: 0 }
12
+
13
+ delegate(:name, to: :issued_commodity, prefix: true)
14
+ delegate(:name, to: :received_commodity, prefix: true)
15
+ delegate(:abbreviation, to: :issued_unit, prefix: true)
16
+ delegate(:abbreviation, to: :received_unit, prefix: true)
17
+ end
18
+ end
19
+ end
@@ -7,6 +7,14 @@ module Cats
7
7
  module Dispatchable
8
8
  extend ActiveSupport::Concern
9
9
 
10
+ DRAFT = 'Draft'.freeze
11
+ APPROVED = 'Approved'.freeze
12
+ RESERVED = 'Reserved'.freeze
13
+
14
+ # rubocop:disable Style/MutableConstant
15
+ STATUSES = [DRAFT, APPROVED, RESERVED]
16
+ # rubocop:enable Style/MutableConstant
17
+
10
18
  included do
11
19
  has_many :dispatch_plans, as: :dispatchable
12
20
  end
@@ -0,0 +1,8 @@
1
+ module Cats
2
+ module Core
3
+ class CashDonationSerializer < ActiveModel::Serializer
4
+ attributes :id, :reference_no, :description, :donated_on, :donor_id, :donor_name, :amount, :currency_id,
5
+ :currency_code
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ module Cats
2
+ module Core
3
+ class CommodityDonationSerializer < ActiveModel::Serializer
4
+ attributes :id, :reference_no, :donated_on, :donor_id, :donor_name, :plan_id, :plan_reference_no,
5
+ :quantity, :unit_id, :unit_abbreviation, :description, :commodity_category_id,
6
+ :commodity_category_name, :shipping_reference
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module Cats
2
+ module Core
3
+ class LoanSerializer < ActiveModel::Serializer
4
+ attributes :id, :reference_no, :lender, :agreement_date, :repayment_date, :commodity_category_id,
5
+ :commodity_category_name, :quantity, :unit_id, :unit_abbreviation
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ module Cats
2
+ module Core
3
+ class PurchaseOrderSerializer < ActiveModel::Serializer
4
+ attributes :id, :reference_no, :order_date, :requisition_no, :supplier, :cash_donation_id, :purchase_type,
5
+ :commodity_category_id, :commodity_category_name, :quantity, :unit_id, :unit_name, :currency_id,
6
+ :currency_code, :price
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module Cats
2
+ module Core
3
+ class RoundBeneficiarySerializer < ActiveModel::Serializer
4
+ attributes :id, :beneficiary_id, :beneficiary_full_name, :round_plan_item_id, :commodity_category_id,
5
+ :commodity_category_name, :quantity, :unit_id, :unit_abbreviation, :received
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ module Cats
2
+ module Core
3
+ class SwapSerializer < ActiveModel::Serializer
4
+ attributes :id, :reference_no, :swapper, :agreement_date, :issued_commodity_id, :issued_commodity_name,
5
+ :issued_quantity, :received_commodity_id, :received_commodity_name, :received_quantity,
6
+ :issued_unit_id, :issued_unit_abbreviation, :received_unit_id, :received_unit_abbreviation
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,87 @@
1
+ module Cats
2
+ module Core
3
+ class BeneficiaryService
4
+ def clear_distribution_list(id)
5
+ round_plan = RoundPlan.find(id)
6
+ raise(StandardError, 'Round plan is not in draft state.') unless round_plan.status == RoundPlan::DRAFT
7
+
8
+ RoundBeneficiary.joins(:round_plan_item).where(round_plan_item: { round_plan_id: id }).delete_all
9
+ end
10
+
11
+ def generate_distribution_list(id)
12
+ clear_distribution_list(id)
13
+ round_plan = RoundPlan.find(id)
14
+ plan_days = round_plan.plan.no_of_round_days
15
+ rounds = round_plan.rounds.count
16
+ beneficiaries = Beneficiary.joins(:beneficiary_category).where(
17
+ beneficiary_category: { plan_id: round_plan.plan_id }
18
+ ).group_by(&:fdp_id)
19
+ beneficiaries.each_key do |key|
20
+ beneficiaries[key] = beneficiaries[key].group_by(&:beneficiary_category_id)
21
+ end
22
+ rations = round_plan.round_rations
23
+
24
+ round_beneficiaries = []
25
+ round_plan.round_plan_items.each do |rpi|
26
+ fdp_beneficiaries = beneficiaries[rpi.fdp_id]
27
+ fdp_beneficiaries.each_key do |key|
28
+ category_rations = rations.select { |r| r.beneficiary_category_id == key }
29
+ category_rations.each do |ration|
30
+ fdp_beneficiaries[key].each do |beneficiary|
31
+ round_beneficiaries << {
32
+ beneficiary_id: beneficiary.id,
33
+ round_plan_item_id: rpi.id,
34
+ commodity_category_id: ration.commodity_category_id,
35
+ quantity: plan_days / ration.no_of_days * ration.quantity * rounds,
36
+ unit_id: ration.unit_of_measure_id,
37
+ received: false
38
+ }
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ RoundBeneficiary.insert_all!(round_beneficiaries, record_timestamps: true)
45
+ true
46
+ end
47
+
48
+ def remove_items(plan_id, ids)
49
+ raise(StandardError, 'No beneficiaries specified.') if ids.count.zero?
50
+
51
+ begin
52
+ plan = RoundPlan.find(plan_id)
53
+ rescue ActiveRecord::RecordNotFound
54
+ raise(StandardError, 'Round plan not found.') unless plan
55
+ end
56
+
57
+ beneficiaries = RoundBeneficiary.joins(:round_plan_item)
58
+ .where(round_plan_item: { round_plan_id: plan_id })
59
+ to_delete = RoundBeneficiary.where(id: ids)
60
+ diff = to_delete - beneficiaries
61
+ raise(StandardError, 'Round plan beneficiaries should be from the same plan.') if diff.count.positive?
62
+
63
+ to_delete.delete_all
64
+ true
65
+ end
66
+
67
+ def confirm_receipt(plan_id, ids)
68
+ raise(StandardError, 'No beneficiaries specified.') if ids.count.zero?
69
+
70
+ begin
71
+ plan = RoundPlan.find(plan_id)
72
+ rescue ActiveRecord::RecordNotFound
73
+ raise(StandardError, 'Round plan not found.') unless plan
74
+ end
75
+
76
+ beneficiaries = RoundBeneficiary.joins(:round_plan_item)
77
+ .where(round_plan_item: { round_plan_id: plan_id })
78
+ to_confirm = RoundBeneficiary.where(id: ids)
79
+ diff = to_confirm - beneficiaries
80
+ raise(StandardError, 'Round plan beneficiaries should be from the same plan.') if diff.count.positive?
81
+
82
+ to_confirm.update_all(received: true)
83
+ true
84
+ end
85
+ end
86
+ end
87
+ end