Class: ShipmentReceiptsWorker

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

Overview

Sidekiq worker: shipment receipts.

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

Parameters:

  • options (Hash) (defaults to: {})

    Job arguments

Options Hash (options):

  • shipment_receipt (Hash)

    Attributes for the new ShipmentReceipt

  • purchase_order_shipment_id (Integer)
  • shipment_items (Hash)

    Items and quantities to enter on the receipt



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
52
53
54
55
56
57
58
59
60
61
62
# File 'app/workers/shipment_receipts_worker.rb', line 14

def perform(options = {})
  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')
    begin
      shipment_receipt_saved = shipment_receipt.save
    rescue ActiveRecord::StatementInvalid => e
      raise unless e.message.include?('locked delivery')

      Rails.logger.warn("[ShipmentReceiptsWorker] Skipping receipt for locked delivery (PO shipment #{purchase_order_shipment.id}): #{e.message}")
      shipment_receipt_saved = false
    end
    at(4, 'Updating store items ')
    sleep 1
    at(5, 'Finishing process')
    sleep 1
  end

  message = '<b>Create Shipment Receipt failed.</b>' if shipment_receipt_saved == false

  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