Class: Cats::Core::AuthorizationService

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

Instance Method Summary collapse

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(authorization_id, receipt_number)
  authorization = ReceiptAuthorization.find(authorization_id)
  commodity = authorization.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: authorization.store,
    commodity_status: Commodity::GOOD,
    stack_status: Stack::ALLOCATED
  )
  ReceiptTransaction.create!(
    receipt_authorization: authorization,
    destination: stack,
    quantity: authorization.received_quantity,
    unit: authorization.unit,
    status: Transaction::DRAFT,
    transaction_date: Date.today,
    receipt_number: receipt_number
  )
  stack
end

#driver_confirm(authorization_id, pin, receipt_number) ⇒ 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
# File 'app/services/cats/core/authorization_service.rb', line 4

def driver_confirm(authorization_id, pin, receipt_number)
  authorization = ReceiptAuthorization.find(authorization_id)
  raise(StandardError, "Authorization not confirmed by storekeeper.") if authorization.status == Authorization::AUTHORIZED

  raise(StandardError, "No pin has been generated for receipt.") if authorization.auth_details.nil?

  raise(StandardError, "Incorrect pin.") unless pin == authorization.auth_details["pin"]

  raise(StandardError, "Pin is not active.") unless authorization.auth_details["active"]

  if DateTime.now > authorization.auth_details["expires_at"]
    authorization.auth_details[:active] = false
    authorization.save!
    raise(StandardError, "Pin has expired.")
  end

  authorization.driver_confirmed = true
  authorization.save!
  statuses = authorization.dispatch.receipt_authorizations.map(&:driver_confirmed).uniq
  authorization.dispatch.receive if statuses.length == 1 && statuses[0]

  # Create destination stack and transaction for FDP
  location = authorization.dispatch.dispatch_plan_item.destination
  if location.location_type == Location::FDP
    create_stack(authorization_id, receipt_number)
    stack(authorization_id)
  end

  authorization
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 receipt_authorization(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

Raises:

  • (StandardError)


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(authorization)
  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?(authorization.store_id)
  end.compact
  return if recipients.empty?

  notification = DispatchAuthorizationNotification.with(dispatch_authorization: authorization)
  notification.deliver(recipients)
end

#send_receipt_notification(authorization) ⇒ Object

Raises:

  • (StandardError)


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(authorization)
  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?(authorization.store_id)
  end.compact
  return if recipients.empty?

  notification = ReceiptAuthorizationNotification.with(receipt_authorization: authorization)
  notification.deliver(recipients)
end

#stack(authorization_id) ⇒ Object

Raises:

  • (StandardError)


64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/services/cats/core/authorization_service.rb', line 64

def stack(authorization_id)
  authorization = ReceiptAuthorization.find(authorization_id)
  raise(StandardError, "Dispatch is not received.") unless authorization.dispatch.dispatch_status == Dispatch::RECEIVED

  total = authorization.transactions.sum(:quantity)
  raise(StandardError, "Received quantity is not the same as quantity to be stacked.") if total != authorization.received_quantity

  authorization.transactions.each(&:commit)

  authorization.dispatch.stack
  authorization
end