Created
February 6, 2014 01:48
-
-
Save hecbuma/8837036 to your computer and use it in GitHub Desktop.
Quick books report
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
module Spree | |
class SalesTotalQuickBooksCSV | |
def initialize | |
end | |
def to_csv | |
@processed_orders = [] | |
CSV.generate() do |csv| | |
# csv_string = CSV.open("file.csv", "wb") do |csv| | |
csv << header | |
p line_items.size | |
line_items.each_with_index do |line, n| | |
p n | |
@order = line.order | |
next if (@order.payments.try(:first).try(:state) == 'pending' and @order.state == 'cancelled') | |
@show_header = show_header?(@order) | |
values = [] | |
values << @order.number | |
values << @order.state | |
values << @order.payments.try(:first).try(:updated_at).try(:in_time_zone, "EST") | |
values << conditional_show(@order.shipment_state) | |
values << conditional_show(ship_time) | |
values << conditional_show(@order.item_total) | |
values << conditional_show(@order.ship_total) | |
values << conditional_show(@order.tax_total) | |
values << conditional_show(@order.total) | |
values << conditional_show(@order.promo_total) | |
values << line.created_at.try(:in_time_zone, "EST") | |
values << line.quantity | |
values << line.product.name | |
values << line.price | |
values << line.variant.sku | |
values << @order.shipment_state | |
values << @order.try(:shipping_address).try(:state_text) | |
values << @order.try(:shipping_address).try(:country) | |
values << (@order.credit_cards.first.try(:cc_type) || @order.payments.try(:first).try(:payment_method).try(:name)) | |
values << @order.credit_cards.first.try(:last_digits) | |
values << @order.payments.first.try(:response_code) | |
values << @order.try(:email) | |
values << @order.shipment.try(:fulfilled_from) | |
values << @order.customer_type | |
csv << values | |
end | |
end | |
end | |
def name | |
"Sales Report - Quick Books - #{Time.now}" | |
end | |
def ship_time | |
if @order.shipments.try(:first).try(:state) == 'shipped' | |
return @order.shipments.try(:first).try(:shipped_at).try(:in_time_zone, "EST") || @order.shipments.try(:last).try(:updated_at).try(:in_time_zone, "EST") | |
else | |
return @order.shipments.try(:first).try(:shipped_at).try(:in_time_zone, "EST") | |
end | |
end | |
def line_items | |
Spree::LineItem.order(:order_id).includes(:order).where("spree_orders.completed_at > '2014-01-01' and spree_orders.completed_at < '2014-01-31' ").all | |
end | |
def header | |
["OrderID","Financial Status","Paid at","Fulfillment Status","Fulfilled at","Subtotal","Shipping","Taxes", | |
"Total","Discount Amount","Created at","Lineitem quantity","Lineitem name","Lineitem price","Lineitem sku", | |
"Lineitem fulfillment status","Shipping Province","Shipping Country","CC Type","Last 4","CC Response", "email", | |
"fulfilled_from", "Customer Type"] | |
end | |
def conditional_show(text) | |
if @show_header | |
text | |
else | |
"" | |
end | |
end | |
def show_header?(order) | |
if @processed_orders.include?(order) | |
return false | |
else | |
@processed_orders << order | |
return true | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment