Class: Cats::Core::AuthorizationService
- Inherits:
-
Object
- Object
- Cats::Core::AuthorizationService
- Defined in:
- app/services/cats/core/authorization_service.rb
Instance Method Summary collapse
-
#create_stack(authorization_id, receipt_number) ⇒ Object
A method to create stack and stack transactions for FDP.
- #driver_confirm(authorization_id, pin, receipt_number) ⇒ Object
- #receipt_authorization(id) ⇒ Object
- #send_dispatch_notification(authorization) ⇒ Object
- #send_receipt_notification(authorization) ⇒ Object
- #stack(authorization_id) ⇒ Object
Instance Method Details
#create_stack(authorization_id, receipt_number) ⇒ Object
A method to create stack and stack transactions for FDP
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/services/cats/core/authorization_service.rb', line 36 def create_stack(, receipt_number) = ReceiptAuthorization.find() commodity = .dispatch.dispatch_plan_item.commodity stack = Stack.create!( code: commodity.batch_no, length: 100, width: 100, height: 100, start_x: 2, start_y: 2, commodity: commodity, unit: commodity.package_unit, store: .store, commodity_status: Commodity::GOOD, stack_status: Stack::ALLOCATED ) ReceiptTransaction.create!( receipt_authorization: , destination: stack, quantity: .received_quantity, unit: .unit, status: Transaction::DRAFT, transaction_date: Date.today, receipt_number: receipt_number ) stack end |
#driver_confirm(authorization_id, pin, receipt_number) ⇒ Object
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 |
# File 'app/services/cats/core/authorization_service.rb', line 4 def driver_confirm(, pin, receipt_number) = ReceiptAuthorization.find() raise(StandardError, "Authorization not confirmed by storekeeper.") if .status == Authorization::AUTHORIZED raise(StandardError, "No pin has been generated for receipt.") if .auth_details.nil? raise(StandardError, "Incorrect pin.") unless pin == .auth_details["pin"] raise(StandardError, "Pin is not active.") unless .auth_details["active"] if DateTime.now > .auth_details["expires_at"] .auth_details[:active] = false .save! raise(StandardError, "Pin has expired.") end .driver_confirmed = true .save! statuses = .dispatch..map(&:driver_confirmed).uniq .dispatch.receive if statuses.length == 1 && statuses[0] # Create destination stack and transaction for FDP location = .dispatch.dispatch_plan_item.destination if location.location_type == Location::FDP create_stack(, receipt_number) stack() end end |
#receipt_authorization(id) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/services/cats/core/authorization_service.rb', line 77 def (id) item = DispatchPlanItem.find(id).includes( :destination, :unit, commodity: {project: :source} ) { date: Date.today, reference_no: item.reference_no, destination: item.destination.name, source: item.commodity.project.source.description, commodity: "#{item.commodity.project.source.commodity_name} (#{item.commodity.batch_no})", unit: item.unit.abbreviation, quantity: item.quantity } end |
#send_dispatch_notification(authorization) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'app/services/cats/core/authorization_service.rb', line 111 def send_dispatch_notification() notification_rule = NotificationRule.find_by(code: "dispatch_authorization") error = "Notification rule not found for dispatch authorization notification." raise(StandardError, error) unless notification_rule users = User.joins(:roles).where(cats_core_roles: {name: notification_rule.roles}) recipients = users.map do |user| details = user.details user if details.key?("stores") && details["stores"].include?(.store_id) end.compact return if recipients.empty? notification = DispatchAuthorizationNotification.with(dispatch_authorization: ) notification.deliver(recipients) end |
#send_receipt_notification(authorization) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'app/services/cats/core/authorization_service.rb', line 94 def send_receipt_notification() notification_rule = NotificationRule.find_by(code: "receipt_authorization") error = "Notification rule not found for receipt authorization notification." raise(StandardError, error) unless notification_rule users = User.joins(:roles).where(cats_core_roles: {name: notification_rule.roles}) recipients = users.map do |user| details = user.details user if details.key?("stores") && details["stores"].include?(.store_id) end.compact return if recipients.empty? notification = ReceiptAuthorizationNotification.with(receipt_authorization: ) notification.deliver(recipients) end |
#stack(authorization_id) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/services/cats/core/authorization_service.rb', line 64 def stack() = ReceiptAuthorization.find() raise(StandardError, "Dispatch is not received.") unless .dispatch.dispatch_status == Dispatch::RECEIVED total = .transactions.sum(:quantity) raise(StandardError, "Received quantity is not the same as quantity to be stacked.") if total != .received_quantity .transactions.each(&:commit) .dispatch.stack end |