theseus/app/controllers/customs_receipts_controller.rb
2025-05-31 23:25:41 -04:00

37 lines
1.2 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class CustomsReceiptsController < ApplicationController
def index
authorize :customs_receipt
end
def generate
authorize :customs_receipt
return redirect_to customs_receipts_path, alert: "well, ya gotta search for *something*" if params[:search].blank?
receiptable = get_receiptable(params[:search])
if receiptable.nil?
flash[:error] = "couldn't find anything about #{params[:search]}... better luck next time?"
return redirect_to customs_receipts_path
end
send_data CustomsReceipt::Generate.run(receiptable), filename: "customs_receipt.pdf", type: "application/pdf", disposition: "inline"
end
private
def get_receiptable(search)
order = Warehouse::Order.where("hc_id = :search or tracking_number = :search", search:).first
return CustomsReceipt::TheseusSpecific.receiptable_from_warehouse_order(order) if order
sanitized_airtable_search = search.gsub("'", "\\'")
msr = LSV::MarketingShipmentRequest.first_where(
"OR({Airtable ID (Automation)} = '#{sanitized_airtable_search}', {WarehouseTracking Number} = '#{sanitized_airtable_search}')"
)
return CustomsReceipt::TheseusSpecific.receiptable_from_msr(msr) if msr
nil
end
end