Class: Caboose::OrderPdf

Inherits:
Prawn::Document
  • Object
show all
Defined in:
app/models/caboose/order_pdf.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#card_numberObject

Returns the value of attribute card_number.



6
7
8
# File 'app/models/caboose/order_pdf.rb', line 6

def card_number
  @card_number
end

#card_typeObject

Returns the value of attribute card_type.



6
7
8
# File 'app/models/caboose/order_pdf.rb', line 6

def card_type
  @card_type
end

#orderObject

Returns the value of attribute order.



6
7
8
# File 'app/models/caboose/order_pdf.rb', line 6

def order
  @order
end

Instance Method Details

#customer_infoObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/caboose/order_pdf.rb', line 48

def customer_info
  
  order_info = "Order ##{order.id}\n"
  order_info << "Status: #{order.status.capitalize}\n"
  order_info << "Received: #{order.date_created.strftime("%m/%d/%Y")}\n"
  if order.status == 'shipped' && order.date_shipped
    order_info << "Shipped: #{order.date_shipped.strftime("%m/%d/%Y")}"
  end
  
  c = order.customer
  billed_to = "#{c.first_name} #{c.last_name}\n#{c.email}\n#{self.formatted_phone(c.phone)}\n#{self.card_type} ending in #{self.card_number}"
  
  sa = order.shipping_address
  shipped_to = "#{sa.name}\n#{sa.address1}\n"
  shipped_to << "#{sa.address2}\n" if sa.address2.strip.length > 0
  shipped_to << "#{sa.city}, #{sa.state} #{sa.zip}"
  
  tbl = []
  tbl << [
    { :content => "Order Info" , :align => :center, :valign => :bottom, :width => 180 },
    { :content => "Billed To"  , :align => :center, :valign => :bottom, :width => 180 },
    { :content => "Shipped To" , :align => :center, :valign => :bottom, :width => 180 }
  ]    
  tbl << [
    { :content => order_info, :valign => :top, :height => 75 },
    { :content => billed_to , :valign => :top, :height => 75 },
    { :content => shipped_to, :valign => :top, :height => 75 }
  ]    
  table tbl
  
end

#formatted_phone(str) ⇒ Object



21
22
23
24
25
26
27
28
# File 'app/models/caboose/order_pdf.rb', line 21

def formatted_phone(str)
  return '' if str.nil?
  str = str.gsub(/[^0-9]/i, '')
  return "#{str[0]} (#{str[1..3]}) #{str[4..6]}-#{str[7..10]}" if str.length == 11
  return "(#{str[0..2]}) #{str[3..5]}-#{str[6..9]}"            if str.length == 10
  return "#{str[0..2]}-#{str[3..6]}"                           if str.length == 7
  return str
end

#get_card_detailsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/caboose/order_pdf.rb', line 30

def get_card_details
        
  sc = self.order.site.store_config
  ot = self.order.order_transactions.where(:transaction_type => OrderTransaction::TYPE_AUTHORIZE, :success => true).first
  
  case sc.pp_name
    when 'authorize.net'
      
      t = AuthorizeNet::Reporting::Transaction.new(sc.pp_username, sc.pp_password)
      resp = t.get_transaction_details(ot.transaction_id)
      t2 = resp.transaction
      self.card_type = t2.payment_method.card_type
      self.card_number = t2.payment_method.card_number.gsub('X', '')
    
  end
        
end

#order_tableObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/caboose/order_pdf.rb', line 80

def order_table
  
  tbl = []
  tbl << [
    { :content => "Item"            , :align => :center, :valign => :bottom },
    { :content => "Tracking Number" , :align => :center, :valign => :bottom },
    { :content => "Unit Price"      , :align => :center, :valign => :bottom },
    { :content => "Quantity"        , :align => :center, :valign => :bottom },                
    { :content => "Subtotal"        , :align => :center, :valign => :bottom, :width => 60 }
  ]
  
  order.line_items.each do |li|
    tbl << [        
      "#{li.variant.product.title}\n#{li.variant.sku}\n#{li.variant.title}",
      { :content => li.tracking_number },
      { :content => sprintf("%.2f", li.variant.price) , :align => :right },
      { :content => "#{li.quantity}"                  , :align => :right },          
      { :content => sprintf("%.2f", li.subtotal)      , :align => :right }
    ]
  end            
  tbl << [{ :content => "Subtotal"                                      , :colspan => 4, :align => :right }, { :content => sprintf("%.2f", order.subtotal                        ) , :align => :right }]
  tbl << [{ :content => "Tax"                                           , :colspan => 4, :align => :right }, { :content => sprintf("%.2f", order.tax ? order.tax : 0.0           ) , :align => :right }]
  #tbl << [{ :content => "#{order.shipping_method} Shipping & Handling"  , :colspan => 4, :align => :right }, { :content => sprintf("%.2f", order.shipping_and_handling           ) , :align => :right }]    
  tbl << [{ :content => "Discount"                                      , :colspan => 4, :align => :right }, { :content => sprintf("%.2f", order.discount ? order.discount : 0.0 ) , :align => :right }]
  tbl << [{ :content => "Total"                                         , :colspan => 4, :align => :right }, { :content => sprintf("%.2f", order.total                           ) , :align => :right }]
  
  table tbl
end

#to_pdfObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/caboose/order_pdf.rb', line 8

def to_pdf
  
  # Get the type of card and last four digits
  get_card_details
  
  #image open("https://dmwwflw4i3miv.cloudfront.net/logo.png"), :position => :center
  text " "
  customer_info
  text " "
  order_table
  render
end