Class: Cats::Core::DispatchPlanItem
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Cats::Core::DispatchPlanItem
- Defined in:
- app/models/cats/core/dispatch_plan_item.rb
Constant Summary collapse
- AUTHORIZED =
"Authorized".freeze
- SOURCE_AUTHORIZED =
"Source Authorized".freeze
- DESTINATION_AUTHORIZED =
"Destination Authorized".freeze
- UNAUTHORIZED =
"Unauthorized".freeze
- STATUSES =
[AUTHORIZED, DESTINATION_AUTHORIZED, SOURCE_AUTHORIZED, UNAUTHORIZED].freeze
Class Method Summary collapse
- .ransackable_associations(_auth_object = nil) ⇒ Object
- .ransackable_attributes(_auth_object = nil) ⇒ Object
Instance Method Summary collapse
-
#authorize ⇒ Object
Authorize a dispatch plan item if it contains authorization entries under it.
- #region ⇒ Object
- #validate_quantity(quantity, type) ⇒ Object
- #woreda ⇒ Object
- #zone ⇒ Object
Class Method Details
.ransackable_associations(_auth_object = nil) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'app/models/cats/core/dispatch_plan_item.rb', line 117 def self.ransackable_associations(_auth_object = nil) %w[ source destination dispatch_plan commodity unit ] end |
.ransackable_attributes(_auth_object = nil) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/cats/core/dispatch_plan_item.rb', line 103 def self.ransackable_attributes(_auth_object = nil) %w[ commodity_id commodity_status reference_no destination_id dispatch_plan_id quantity source_id status unit_id ] end |
Instance Method Details
#authorize ⇒ Object
Authorize a dispatch plan item if it contains authorization entries under it. A dispatch plan item can be authorized by source hub, or source and destination hub. Hence the status can be ‘SOURCE_AUTHORIZED`, `AUTHORIZED`, or `UNAUTHORIZED` (default). This method sets those authorization values without any other interference by looking at available data and therefore no parameters are required.
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 |
# File 'app/models/cats/core/dispatch_plan_item.rb', line 52 def count = .count raise(StandardError, "No authorization entries found.") if count.zero? source_auth = .where(authorization_type: HubAuthorization::SOURCE) dest_auth = .where(authorization_type: HubAuthorization::DESTINATION) if source_auth.count.positive? && dest_auth.count.zero? && status == SOURCE_AUTHORIZED raise( StandardError, "Plan item already authorized by source. Destination hub authorizations are required." ) end if source_auth.count.zero? && dest_auth.count.positive? && status == DESTINATION_AUTHORIZED raise( StandardError, "Plan item already authorized by destination. Source hub authorizations are required." ) end total_src = UnitConversion.harmonized_total(source_auth, unit) total_dest = UnitConversion.harmonized_total(dest_auth, unit) if source_auth.count == count validate_quantity(total_src, SOURCE_AUTHORIZED) self.status = SOURCE_AUTHORIZED elsif dest_auth.count == count validate_quantity(total_dest, DESTINATION_AUTHORIZED) self.status = DESTINATION_AUTHORIZED else validate_quantity(total_src, SOURCE_AUTHORIZED) validate_quantity(total_dest, DESTINATION_AUTHORIZED) self.status = AUTHORIZED end save! self end |
#region ⇒ Object
41 42 43 |
# File 'app/models/cats/core/dispatch_plan_item.rb', line 41 def region destination.location_type == "Fdp" ? destination.parent.parent.parent.name : "" end |
#validate_quantity(quantity, type) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'app/models/cats/core/dispatch_plan_item.rb', line 91 def validate_quantity(quantity, type) return if quantity == self.quantity diff = self.quantity - quantity desc = type.split(" ")[0].downcase amount = "#{diff} #{unit.abbreviation}" raise( StandardError, "Authorized #{desc} quantity is not the same as plan item quantity (#{amount} unaccounted for)." ) end |
#woreda ⇒ Object
33 34 35 |
# File 'app/models/cats/core/dispatch_plan_item.rb', line 33 def woreda destination.location_type == "Fdp" ? destination.parent.name : "" end |
#zone ⇒ Object
37 38 39 |
# File 'app/models/cats/core/dispatch_plan_item.rb', line 37 def zone destination.location_type == "Fdp" ? destination.parent.parent.name : "" end |