Class: ShipmentReceiptsWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job, Workers::StatusBroadcastable
Defined in:
app/workers/shipment_receipts_worker.rb

Instance Attribute Summary

Attributes included from Workers::StatusBroadcastable

#broadcast_status_updates

Instance Method Summary collapse

Methods included from Workers::StatusBroadcastable::Overrides

#at, #store, #total

Instance Method Details

#perform(options = {}) ⇒ Object



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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/workers/shipment_receipts_worker.rb', line 6

def perform(options={})
  options = options.with_indifferent_access
  options = options.deep_symbolize_keys
  shipment_receipt = ShipmentReceipt.new(options[:shipment_receipt])
  purchase_order_shipment = PurchaseOrderShipment.find(options[:purchase_order_shipment_id])
  shipment_items = options[:shipment_items]
  shipment_receipt_saved = true
  total_steps = 5

  # If shipment_items is empty then the shipment receipt will be invalid and won't save
  ShipmentReceipt.with_advisory_lock("receiving_items_pos_#{purchase_order_shipment.id}", timeout_seconds: 0) do
    total total_steps
    at(1, 'Creating shipment items')
    sleep 1
    shipment_receipt.enter_receipts(shipment_items.stringify_keys)
    if purchase_order_shipment.estimated_landed_cost.present?
      total total_steps
      at(2, 'Calculating estimated landed cost')
      sleep 1
      shipment_receipt.estimated_landed_cost = shipment_receipt.prorate_estimated_landed_cost
      shipment_receipt.currency = purchase_order_shipment.currency
    end
    total total_steps
    at(3, 'Creating shipment receipt')
    shipment_receipt_saved = shipment_receipt.save
    at(4, 'Updating store items ')
    sleep 1
    at(5, 'Finishing process')
    sleep 1
  end

  if shipment_receipt_saved == false
    message = '<b>Create Shipment Receipt failed.</b>'
  end
  
  if shipment_receipt.persisted?
    if shipment_receipt.serial_number_state == "awaiting_assignment"
      store redirect_to: "/shipment_receipts/#{shipment_receipt.id}/assign_serial_numbers"
    else
      store redirect_to: "/purchase_orders/#{shipment_receipt.shipment_receipt_items.first.purchase_order.id}"
    end
  else
    store redirect_to: "/purchase_orders/new"
  end
  store message: message
end