Class: Spree::Admin::DelhiveryController
- Inherits:
-
BaseController
- Object
- BaseController
- Spree::Admin::DelhiveryController
- Defined in:
- app/controllers/spree/admin/delhivery_controller.rb
Instance Method Summary collapse
- #create_manifest ⇒ Object
- #create_pickup ⇒ Object
- #delhivery_cancel ⇒ Object
- #download_label ⇒ Object
- #sync_tracking ⇒ Object
Instance Method Details
#create_manifest ⇒ Object
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 |
# File 'app/controllers/spree/admin/delhivery_controller.rb', line 28 def create_manifest sender = SpreeDelhivery::ShipmentSender.new(@shipment) result = sender.call if result.success? # Force the shipment from 'pending' directly to 'shipped' on a successful API response ActiveRecord::Base.transaction do @shipment.update_columns( state: 'shipped', shipped_at: @shipment.shipped_at || Time.current ) # Log all inventory units out of warehouse stock management @shipment.inventory_units.where.not(state: 'shipped').update_all(state: 'shipped') # Trigger downstream order pipeline status calculations @shipment.order.updater.update end flash[:success] = "Shipment Manifested! Waybill: #{@shipment.delhivery_waybill}" else flash[:error] = "Delhivery Error: #{result.error}" end # Turbo Fix redirect_to spree.edit_admin_order_path(@shipment.order), status: :see_other end |
#create_pickup ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'app/controllers/spree/admin/delhivery_controller.rb', line 7 def create_pickup stock_id = params[:id] @stock_location = if stock_id.to_s.start_with?('stl_') && Spree::StockLocation.respond_to?(:find_by_prefix_id) Spree::StockLocation.find_by_prefix_id(stock_id) else Spree::StockLocation.find(stock_id) end service = SpreeDelhivery::PickupService.new(@stock_location, count: 5) result = service.call if result.success? flash[:success] = result. else flash[:error] = "Pickup Failed: #{result.}" end # Turbo Fix for seamless UI updates redirect_to spree.edit_admin_stock_location_path(@stock_location), status: :see_other end |
#delhivery_cancel ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/controllers/spree/admin/delhivery_controller.rb', line 54 def delhivery_cancel result = SpreeDelhivery::ShipmentCanceler.new(@shipment).call if result.success? flash[:success] = "Shipment Waybill Voided Successfully." else flash[:error] = "Delhivery Error: #{result.error}" end # Turbo Fix redirect_to spree.edit_admin_order_path(@shipment.order), status: :see_other end |
#download_label ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/controllers/spree/admin/delhivery_controller.rb', line 67 def download_label if @shipment.delhivery_label_url.present? redirect_to @shipment.delhivery_label_url, allow_other_host: true else client = SpreeDelhivery::Client.new label_res = client.fetch_label(@shipment.delhivery_waybill) if label_res['packages'].present? && label_res['packages'][0]['pdf_download_link'].present? url = label_res['packages'][0]['pdf_download_link'] @shipment.update(delhivery_label_url: url) redirect_to url, allow_other_host: true else flash[:error] = "Label not generated yet. Please try again later." redirect_to spree.edit_admin_order_path(@shipment.order), status: :see_other end end end |
#sync_tracking ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'app/controllers/spree/admin/delhivery_controller.rb', line 85 def sync_tracking result = SpreeDelhivery::ShipmentTracker.new(@shipment).call current_status = result.status.to_s.upcase raw_data_string = result.data.inspect.upcase if result.data is_cancelled = current_status.include?("CANCEL") || current_status.include?("VOID") || (raw_data_string && (raw_data_string.include?("CANCEL") || raw_data_string.include?("VOID"))) if is_cancelled ActiveRecord::Base.transaction do @shipment.update_columns( delhivery_waybill: nil, tracking: nil, tracking_status: "CANCELLED", state: "ready", shipped_at: nil ) @shipment.inventory_units.update_all(state: "on_hand") @shipment.order.updater.update end flash[:success] = "Remote cancellation detected. Waybill cleared and shipment reset to Ready." redirect_to spree.edit_admin_order_path(@shipment.order), status: :see_other return end # THE COD AUTO-CAPTURE ENGINE is_delivered = current_status.include?("DELIVERED") || (raw_data_string && raw_data_string.include?("DELIVERED")) if is_delivered ActiveRecord::Base.transaction do # Find any pending COD payments on this order and capture them @shipment.order.payments.valid.where(state: ['pending', 'checkout']).each do |payment| if payment.payment_method&.type == 'Spree::PaymentMethod::DelhiveryCod' payment.capture! Rails.logger.info "[Delhivery] Auto-captured COD Payment #{payment.number} for Order #{@shipment.order.number}" end end # Crash Prevention: Spree doesn't natively support a 'delivered' state-machine path. # We explicitly update columns to avoid NoMethodError on deliver! @shipment.update_columns( state: 'shipped', tracking_status: 'DELIVERED', shipped_at: @shipment.shipped_at || Time.current ) # Cleanly verify inventory states match deployment metrics @shipment.inventory_units.where.not(state: 'shipped').update_all(state: 'shipped') # Force downstream state engine recalculations (Clears "Balance Due" badge to green Paid) @shipment.order.updater.update end flash[:success] = "Shipment Delivered! COD Payment automatically captured and reconciled." redirect_to spree.edit_admin_order_path(@shipment.order), status: :see_other return end # STANDARD TRACKING UPDATE if result.success? @shipment.update_column(:tracking_status, current_status) flash[:success] = "Status Updated: #{current_status}" else flash[:error] = "Tracking Error: #{result.error || current_status}" end # Turbo Fix redirect_to spree.edit_admin_order_path(@shipment.order), status: :see_other end |