Module: Spree::Admin::ShipmentsControllerDecorator

Defined in:
app/controllers/spree/admin/shipments_controller_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



4
5
6
7
# File 'app/controllers/spree/admin/shipments_controller_decorator.rb', line 4

def self.prepended(base)
  # Ensure we can access these methods inside the controller
  base.helper_method :delhivery_integration
end

Instance Method Details

#delhivery_cancelObject

POST /admin/shipments/:id/delhivery_cancel



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/spree/admin/shipments_controller_decorator.rb', line 86

def delhivery_cancel
  @shipment = Spree::Shipment.find(params[:id])
  
  client = SpreeDelhivery::Client.new
  response = client.cancel_shipment(@shipment.delhivery_waybill)
  
  # Delhivery cancellation response varies, usually checks for success code
  if response['status'] == "True" || response['success'] == true
    @shipment.update(
      delhivery_waybill: nil, 
      tracking: nil,
      state: 'ready' # Revert state so we can ship again
    )
    flash[:success] = "Shipment Cancelled successfully."
  else
    flash[:error] = "Cancellation Failed: #{response['error'] || response['message']}"
  end
  
  redirect_back(fallback_location: edit_admin_order_path(@shipment.order))
end

#delhivery_labelObject

GET /admin/shipments/:id/delhivery_label



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/spree/admin/shipments_controller_decorator.rb', line 108

def delhivery_label
  @shipment = Spree::Shipment.find(params[:id])
  
  client = SpreeDelhivery::Client.new
  response = client.fetch_label(@shipment.delhivery_waybill)
  
  # API usually returns a JSON with a 'packages' array containing a 'pdf_download_link'
  # Or sometimes raw PDF data depending on endpoint. 
  # Assuming the 'packing_slip' endpoint returns JSON with a URL:
  if response['packages'].present? && response['packages'][0]['pdf_download_link'].present?
    redirect_to response['packages'][0]['pdf_download_link'], allow_other_host: true
  else
    flash[:error] = "Label URL not found in response."
    redirect_back(fallback_location: edit_admin_order_path(@shipment.order))
  end
end

#delhivery_manifestObject

POST /admin/shipments/:id/delhivery_manifest



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
52
53
54
55
56
57
58
# File 'app/controllers/spree/admin/shipments_controller_decorator.rb', line 10

def delhivery_manifest
  @shipment = Spree::Shipment.find(params[:id])
  
  # 1. Validation
  unless delhivery_integration
    flash[:error] = "Delhivery Integration is not active."
    return redirect_back(fallback_location: admin_orders_path)
  end
  
  unless @shipment.stock_location.delhivery_warehouse_name.present?
    flash[:error] = "Stock Location is missing 'Delhivery Warehouse Name'. Please configure it first."
    return redirect_to edit_admin_stock_location_path(@shipment.stock_location)
  end
  
  # 2. Build Payload
  # We delegate this complex logic to a dedicated helper method below
  payload = build_delhivery_payload(@shipment)
  
  # 3. Call API
  client = SpreeDelhivery::Client.new
  response = client.create_shipment(payload)
  
  # 4. Handle Response
  if response['packages'].present? && response['packages'][0]['status'] == 'Success'
    # Success!
    waybill = response['packages'][0]['waybill']
    ref_id  = response['packages'][0]['refnum'] # Our shipment number
    
    @shipment.update!(
      delhivery_waybill: waybill,
      delhivery_ref_id: ref_id,
      tracking: waybill,
      state: 'shipped', # Mark as shipped in Spree immediately
      shipped_at: Time.current,
      delhivery_response_data: response
    )
  
    flash[:success] = "Delhivery Waybill Generated: #{waybill}"
  else
    # Error
    error_msg = response['error'] || response['packages']&.first&.fetch('remarks', nil) || "Unknown API Error"
    flash[:error] = "Delhivery Failed: #{error_msg}"
  end
  
  redirect_back(fallback_location: edit_admin_order_path(@shipment.order))
rescue StandardError => e
  flash[:error] = "System Error: #{e.message}"
  redirect_back(fallback_location: edit_admin_order_path(@shipment.order))
end

#delhivery_trackObject

POST /admin/shipments/:id/delhivery_track



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/spree/admin/shipments_controller_decorator.rb', line 61

def delhivery_track
  @shipment = Spree::Shipment.find(params[:id])
  
  if @shipment.delhivery_waybill.blank?
    flash[:error] = "No Waybill found to track."
  else
    client = SpreeDelhivery::Client.new
    response = client.track_shipment(@shipment.delhivery_waybill)
    
    # Parse flexible response (sometimes Array, sometimes Hash)
    data = response.is_a?(Array) ? response.first : response
    
    if data && data['ShipmentData']
      status = data['ShipmentData'][0]['Shipment']['Status']['Status'] rescue "Unknown"
      @shipment.update(tracking_status: status)
      flash[:success] = "Tracking Updated: #{status}"
    else
      flash[:warning] = "Tracking info not available yet."
    end
  end
  
  redirect_back(fallback_location: edit_admin_order_path(@shipment.order))
end