My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
FulfillmentIntegration  
A mini how-to on integrating with fulfillment houses
Updated Feb 4, 2010 by subim...@gmail.com

Fulfillment Integration

Do you work with a fulfillment house? Do you need to send your orders to them daily?

No problem - Substruct has the solution for you.

The Order class has a couple methods that you will find interesting.

Order.get_csv_for_orders(order_list)
Order.get_xml_for_orders(order_list)

What these two methods do is pretty straightforward. The first gets a CSV (comma separated values) string representing all the orders you send in. As you would guess, the second does the same, but generates XML instead.

So How Do I Use It?

Assuming you need to connect with a fulfillment house, here's a method that you can add to your app/admin/orders_controller.rb file. (Remember, any method you put in a controller file in your app is additive. Oh the joy of Engines!)

This example sends the file via email - but nothing is stopping you from making a web service call to the fulfillment house of your choice.

class Admin::OrdersController < Admin::BaseController
  # Ships all "ready to ship" orders
  def ship_orders
    # Get orders ready to ship
    @orders = Order.find(:all, :conditions => 'order_status_code_id = 5')
    
    # Get a ship time
    ship_time = Time.now
    ship_time_str = ship_time.strftime("%m_%d_%y-%H-%M")

    attachment = TMail::Mail.new

    file_name = "Orders_#{ship_time_str}.xml"
    mail = OrdersMailer.create_tonys
    attachment.body = Order.get_xml_for_orders(@orders)
    attachment.set_content_type('application', 'xml', 'name' => file_name)

    mail.set_content_type('multipart', 'mixed')
    # Add attachment
    mail.parts << attachment
    # Send the mail
    begin
      OrdersMailer::deliver(mail)
      # Change status on all the orders
      for order in @orders
        order.order_status_code_id = 7
        order.shipped_on = ship_time
        order.save
      end
      flash[:notice] = "All orders have been sent successfully."
    rescue
      flash[:notice] = "There was a problem shipping the orders."
      flash[:notice] << "<br/>Please let the webmaster know."
      flash[:notice] << "<br/><br/><pre>"+ $! +"</pre>"
    end
    redirect_to :action => 'list'
  end
end

I usually call this method from app/views/admin/orders/list.rhtml as such.

  <!-- @list_options[0] = Orders ready to ship -->
  <% if @viewing_by == @list_options[0] && @orders.length > 0 then %>
    <%= link_to "Ship Orders" , {:action => 'ship_orders', :class => 'action' %>
  <% end %>
Powered by Google Project Hosting