caboose-cms 0.7.85 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/caboose/{admin_edit_order.js → admin_edit_invoice.js} +332 -189
  3. data/app/assets/javascripts/caboose/cart.js +15 -15
  4. data/app/assets/javascripts/caboose/cart_old.js +2 -2
  5. data/app/assets/javascripts/caboose/checkout/authnet_payment_method_controller.js +120 -0
  6. data/app/assets/javascripts/caboose/checkout/billing_address_controller.js +85 -0
  7. data/app/assets/javascripts/caboose/checkout/cart_controller.js +257 -0
  8. data/app/assets/javascripts/caboose/checkout/checkout_controller.js +258 -0
  9. data/app/assets/javascripts/caboose/checkout/gift_cards_controller.js +57 -0
  10. data/app/assets/javascripts/caboose/checkout/shipping_address_controller.js +86 -0
  11. data/app/assets/javascripts/caboose/checkout/shipping_method_controller.js +121 -0
  12. data/app/assets/javascripts/caboose/checkout/stripe_payment_method_controller.js +155 -0
  13. data/app/assets/javascripts/caboose/checkout_login_register.js +2 -2
  14. data/app/assets/javascripts/caboose/checkout_module.js +8 -8
  15. data/app/assets/javascripts/caboose/checkout_old.js +151 -0
  16. data/app/assets/javascripts/caboose/{checkout_payment.js → checkout_payment_authnet.js} +1 -1
  17. data/app/assets/javascripts/caboose/checkout_payment_stripe.js +212 -0
  18. data/app/assets/javascripts/caboose/checkout_shipping.js +1 -1
  19. data/app/assets/javascripts/caboose/jquery.payment.js +1 -0
  20. data/app/assets/javascripts/caboose/model/attribute.js +3 -2
  21. data/app/assets/javascripts/caboose/model/bound_control.js +56 -14
  22. data/app/assets/javascripts/caboose/model/bound_select.js +9 -11
  23. data/app/assets/javascripts/caboose/model/bound_select_simple.js +215 -0
  24. data/app/assets/javascripts/caboose/model/bound_text.js +7 -8
  25. data/app/assets/javascripts/caboose/{my_account_edit_order.js → my_account_edit_invoice.js} +55 -55
  26. data/app/assets/javascripts/caboose/united_states.js +65 -0
  27. data/app/assets/stylesheets/caboose/{my_account_edit_order.css.scss → my_account_edit_invoice.css.scss} +1 -1
  28. data/app/assets/templates/caboose/cart/line_items.jst.ejs +3 -3
  29. data/app/assets/templates/caboose/checkout/line_items.jst.ejs +7 -7
  30. data/app/controllers/caboose/ab_options_controller.rb +4 -4
  31. data/app/controllers/caboose/ab_variants_controller.rb +5 -5
  32. data/app/controllers/caboose/admin_controller.rb +0 -6
  33. data/app/controllers/caboose/application_controller.rb +16 -15
  34. data/app/controllers/caboose/billing_addresses_controller.rb +22 -22
  35. data/app/controllers/caboose/block_type_sources_controller.rb +8 -8
  36. data/app/controllers/caboose/block_types_controller.rb +2 -42
  37. data/app/controllers/caboose/blocks_controller.rb +26 -26
  38. data/app/controllers/caboose/calendars_controller.rb +5 -5
  39. data/app/controllers/caboose/cart_controller.rb +55 -36
  40. data/app/controllers/caboose/categories_controller.rb +78 -76
  41. data/app/controllers/caboose/checkout_controller.rb +313 -340
  42. data/app/controllers/caboose/checkout_controller_bak.rb +620 -0
  43. data/app/controllers/caboose/domains_controller.rb +5 -5
  44. data/app/controllers/caboose/event_groups_controller.rb +26 -4
  45. data/app/controllers/caboose/events_controller.rb +7 -6
  46. data/app/controllers/caboose/fonts_controller.rb +2 -2
  47. data/app/controllers/caboose/gift_cards_controller.rb +113 -139
  48. data/app/controllers/caboose/google_spreadsheets_controller.rb +1 -1
  49. data/app/controllers/caboose/{order_packages_controller.rb → invoice_packages_controller.rb} +49 -49
  50. data/app/controllers/caboose/{order_reports_controller.rb → invoice_reports_controller.rb} +11 -13
  51. data/app/controllers/caboose/invoice_transactions_controller.rb +35 -0
  52. data/app/controllers/caboose/invoices_controller.rb +328 -0
  53. data/app/controllers/caboose/line_items_controller.rb +35 -34
  54. data/app/controllers/caboose/login_controller.rb +7 -7
  55. data/app/controllers/caboose/login_logs_controller.rb +4 -4
  56. data/app/controllers/caboose/logout_controller.rb +3 -1
  57. data/app/controllers/caboose/media_categories_controller.rb +9 -9
  58. data/app/controllers/caboose/media_controller.rb +21 -46
  59. data/app/controllers/caboose/modal_controller.rb +4 -3
  60. data/app/controllers/caboose/modification_values_controller.rb +6 -6
  61. data/app/controllers/caboose/modifications_controller.rb +7 -6
  62. data/app/controllers/caboose/my_account_controller.rb +2 -2
  63. data/app/controllers/caboose/my_account_invoices_controller.rb +166 -0
  64. data/app/controllers/caboose/my_account_line_items_controller.rb +15 -13
  65. data/app/controllers/caboose/page_custom_fields_controller.rb +9 -8
  66. data/app/controllers/caboose/page_permissions_controller.rb +3 -3
  67. data/app/controllers/caboose/pages_controller.rb +132 -135
  68. data/app/controllers/caboose/permissions_controller.rb +8 -7
  69. data/app/controllers/caboose/post_categories_controller.rb +22 -7
  70. data/app/controllers/caboose/post_custom_fields_controller.rb +9 -8
  71. data/app/controllers/caboose/posts_controller.rb +35 -43
  72. data/app/controllers/caboose/product_images_controller.rb +5 -5
  73. data/app/controllers/caboose/products_controller.rb +53 -47
  74. data/app/controllers/caboose/redirects_controller.rb +7 -7
  75. data/app/controllers/caboose/register_controller.rb +2 -2
  76. data/app/controllers/caboose/retargeting_controller.rb +2 -61
  77. data/app/controllers/caboose/reviews_controller.rb +1 -0
  78. data/app/controllers/caboose/roles_controller.rb +10 -9
  79. data/app/controllers/caboose/settings_controller.rb +9 -7
  80. data/app/controllers/caboose/shipping_addresses_controller.rb +18 -18
  81. data/app/controllers/caboose/shipping_packages_controller.rb +24 -21
  82. data/app/controllers/caboose/sites_controller.rb +35 -25
  83. data/app/controllers/caboose/smtp_controller.rb +3 -3
  84. data/app/controllers/caboose/sns_controller.rb +6 -6
  85. data/app/controllers/caboose/social_controller.rb +5 -6
  86. data/app/controllers/caboose/stackable_groups_controller.rb +10 -8
  87. data/app/controllers/caboose/station_controller.rb +3 -3
  88. data/app/controllers/caboose/store_controller.rb +93 -87
  89. data/app/controllers/caboose/subscriptions_controller.rb +153 -0
  90. data/app/controllers/caboose/users_controller.rb +25 -22
  91. data/app/controllers/caboose/variants_controller.rb +72 -93
  92. data/app/controllers/caboose/vendors_controller.rb +35 -32
  93. data/app/mailers/caboose/{orders_mailer.rb → invoices_mailer.rb} +21 -21
  94. data/app/mailers/caboose/login_mailer.rb +3 -3
  95. data/app/models/caboose/authnet.rb +295 -195
  96. data/app/models/caboose/comment_routes.rb +68 -41
  97. data/app/models/caboose/core_plugin.rb +1 -1
  98. data/app/models/caboose/discount.rb +14 -3
  99. data/app/models/caboose/domain_constraint.rb +11 -3
  100. data/app/models/caboose/gift_card.rb +8 -8
  101. data/app/models/caboose/invoice.rb +706 -0
  102. data/app/models/caboose/invoice_discount.rb +10 -0
  103. data/app/models/caboose/invoice_package.rb +76 -0
  104. data/app/models/caboose/{order_package_calculator.rb → invoice_package_calculator.rb} +20 -20
  105. data/app/models/caboose/{order_pdf.rb → invoice_pdf.rb} +30 -30
  106. data/app/models/caboose/{order_reporter.rb → invoice_reporter.rb} +5 -5
  107. data/app/models/caboose/invoice_transaction.rb +169 -0
  108. data/app/models/caboose/line_item.rb +11 -8
  109. data/app/models/caboose/payment_processors/authorizenet.rb +15 -15
  110. data/app/models/caboose/payment_processors/base.rb +3 -3
  111. data/app/models/caboose/{pending_orders_pdf.rb → pending_invoices_pdf.rb} +31 -31
  112. data/app/models/caboose/product.rb +2 -4
  113. data/app/models/caboose/schema.rb +159 -111
  114. data/app/models/caboose/shipping_calculator.rb +41 -139
  115. data/app/models/caboose/smtp_config.rb +2 -1
  116. data/app/models/caboose/store_config.rb +7 -7
  117. data/app/models/caboose/subscription.rb +32 -0
  118. data/app/models/caboose/tax_calculator.rb +36 -36
  119. data/app/models/caboose/user.rb +6 -0
  120. data/app/models/caboose/user_subscription.rb +142 -0
  121. data/app/models/caboose/wish_list.rb +14 -0
  122. data/app/models/caboose/wish_list_line_item.rb +13 -0
  123. data/app/views/caboose/blocks/_products.html.erb +10 -13
  124. data/app/views/caboose/checkout/_address_form.html.erb +14 -14
  125. data/app/views/caboose/checkout/_billing_form.html.erb +3 -3
  126. data/app/views/caboose/checkout/_cart.html.erb +7 -7
  127. data/app/views/caboose/checkout/_cart_old.html.erb +9 -9
  128. data/app/views/caboose/checkout/_confirm.html.erb +9 -9
  129. data/app/views/caboose/checkout/_order_discount.html.erb +3 -3
  130. data/app/views/caboose/checkout/_shipping_address.html.erb +1 -1
  131. data/app/views/caboose/checkout/_shipping_method.html.erb +1 -1
  132. data/app/views/caboose/checkout/addresses.html.erb +10 -10
  133. data/app/views/caboose/checkout/authnet.html.erb +34 -0
  134. data/app/views/caboose/checkout/authnet_response_handler.html.erb +3 -0
  135. data/app/views/caboose/checkout/checkout.html.erb +100 -0
  136. data/app/views/caboose/checkout/confirm_without_payment.html.erb +6 -6
  137. data/app/views/caboose/checkout/discount.html.erb +1 -1
  138. data/app/views/caboose/checkout/index.html.erb +4 -4
  139. data/app/views/caboose/checkout/payment_authnet.html.erb +140 -0
  140. data/app/views/caboose/checkout/payment_stripe.html.erb +95 -0
  141. data/app/views/caboose/checkout/shipping.html.erb +4 -4
  142. data/app/views/caboose/checkout/thanks.html.erb +3 -3
  143. data/app/views/caboose/gift_cards/admin_edit.html.erb +19 -19
  144. data/app/views/caboose/gift_cards/admin_index.html.erb +9 -9
  145. data/app/views/caboose/{orders → invoices}/_admin_footer.html.erb +0 -0
  146. data/app/views/caboose/{orders → invoices}/_admin_header.html.erb +0 -0
  147. data/app/views/caboose/{orders/_quickbooks_order.html.erb → invoices/_quickbooks_invoice.html.erb} +0 -0
  148. data/app/views/caboose/{orders → invoices}/admin_city_report.html.erb +1 -1
  149. data/app/views/caboose/invoices/admin_delete_form.html.erb +18 -0
  150. data/app/views/caboose/{orders → invoices}/admin_edit.html.erb +14 -14
  151. data/app/views/caboose/{orders → invoices}/admin_index.html.erb +24 -24
  152. data/app/views/caboose/{orders → invoices}/admin_invalid_order.html.erb +2 -2
  153. data/app/views/caboose/{orders → invoices}/admin_new.html.erb +7 -7
  154. data/app/views/caboose/{orders → invoices}/admin_print.html.erb +15 -15
  155. data/app/views/caboose/{orders → invoices}/admin_summary_report.html.erb +4 -4
  156. data/app/views/caboose/{orders_mailer/customer_new_order.html.erb → invoices_mailer/customer_new_invoice.html.erb} +21 -21
  157. data/app/views/caboose/invoices_mailer/customer_payment_authorization.html.erb +5 -0
  158. data/app/views/caboose/{orders_mailer → invoices_mailer}/customer_status_updated.html.erb +22 -22
  159. data/app/views/caboose/{orders_mailer/fulfillment_new_order.html.erb → invoices_mailer/fulfillment_new_invoice.html.erb} +23 -23
  160. data/app/views/caboose/{orders_mailer/shipping_order_ready.html.erb → invoices_mailer/shipping_invoice_ready.html.erb} +0 -0
  161. data/app/views/caboose/{orders_mailer → invoices_mailer}/test_email.html.erb +0 -0
  162. data/app/views/caboose/line_items/admin_new.html.erb +1 -1
  163. data/app/views/caboose/login_mailer/forgot_password_email.html.erb +2 -3
  164. data/app/views/caboose/my_account/index.html.erb +1 -1
  165. data/app/views/caboose/{my_account_orders → my_account_invoices}/authnet_relay.html.erb +0 -0
  166. data/app/views/caboose/{my_account_orders → my_account_invoices}/authnet_response.html.erb +0 -0
  167. data/app/views/caboose/{my_account_orders → my_account_invoices}/edit.html.erb +9 -9
  168. data/app/views/caboose/my_account_invoices/index.html.erb +36 -0
  169. data/app/views/caboose/{my_account_orders → my_account_invoices}/payment_form.html.erb +10 -10
  170. data/app/views/caboose/post_categories/admin_index.html.erb +1 -1
  171. data/app/views/caboose/sites/admin_edit.html.erb +1 -1
  172. data/app/views/caboose/store/admin_edit_general.html.erb +5 -3
  173. data/app/views/caboose/store/admin_edit_packages.html.erb +6 -6
  174. data/app/views/caboose/store/admin_edit_shipping.html.erb +3 -3
  175. data/app/views/caboose/store/admin_edit_tax.html.erb +1 -1
  176. data/app/views/caboose/subscriptions/admin_edit.html.erb +82 -0
  177. data/app/views/caboose/subscriptions/admin_index.html.erb +45 -0
  178. data/config/routes.rb +1 -759
  179. data/config/routes_old.rb +759 -0
  180. data/lib/caboose/version.rb +1 -1
  181. data/lib/tasks/caboose.rake +41 -15
  182. metadata +101 -61
  183. data/app/controllers/caboose/my_account_orders_controller.rb +0 -165
  184. data/app/controllers/caboose/orders_controller.rb +0 -310
  185. data/app/models/caboose/order.rb +0 -479
  186. data/app/models/caboose/order_discount.rb +0 -10
  187. data/app/models/caboose/order_package.rb +0 -133
  188. data/app/models/caboose/order_transaction.rb +0 -41
  189. data/app/models/caboose/payment_processors/payscape.rb +0 -94
  190. data/app/views/caboose/checkout/payment.html.erb +0 -146
  191. data/app/views/caboose/my_account_orders/index.html.erb +0 -36
  192. data/app/views/caboose/orders/admin_delete_form.html.erb +0 -21
  193. data/app/views/caboose/orders_mailer/customer_payment_authorization.html.erb +0 -5
@@ -0,0 +1,10 @@
1
+ module Caboose
2
+ class InvoiceDiscount < ActiveRecord::Base
3
+ self.table_name = 'store_invoice_discounts'
4
+
5
+ belongs_to :invoice
6
+ belongs_to :discount
7
+
8
+ attr_accessible :invoice_id, :discount_id
9
+ end
10
+ end
@@ -0,0 +1,76 @@
1
+ module Caboose
2
+ class InvoicePackage < ActiveRecord::Base
3
+ self.table_name = 'store_invoice_packages'
4
+
5
+ belongs_to :invoice
6
+ belongs_to :shipping_package
7
+ belongs_to :shipping_method
8
+ has_many :line_items
9
+ attr_accessible :id,
10
+ :invoice_id,
11
+ :shipping_method_id,
12
+ :shipping_package_id,
13
+ :status,
14
+ :tracking_number,
15
+ :total
16
+
17
+ STATUS_PENDING = 'Pending'
18
+ STATUS_SHIPPED = 'Shipped'
19
+
20
+ after_initialize :check_nil_fields
21
+
22
+ def check_nil_fields
23
+ self.total = 0.00 if self.total.nil?
24
+ end
25
+
26
+ def self.custom_invoice_packages(store_config, invoice)
27
+ eval(store_config.custom_packages_function)
28
+ end
29
+
30
+ # Calculates the shipping packages required for all the items in the invoice
31
+ def calculate_shipping_package
32
+
33
+ store_config = invoice.site.store_config
34
+ if !store_config.auto_calculate_packages
35
+ return self.custom_shipping_package(store_config, self)
36
+ end
37
+
38
+ variants = self.line_items.sort_by{ |li| li.quantity * (li.variant.volume ? li.variant.volume : 0.00) * -1 }.collect{ |li| li.variant.downloadable ? nil : li.variant }.reject{ |v| v.nil? }
39
+ ShippingPackage.where(:site_id => self.invoice.site_id).reorder(:flat_rate_price).all.each do |sp|
40
+ if sp.fits(variants)
41
+ self.shipping_package_id = sp.id
42
+ self.save
43
+ return true
44
+ end
45
+ end
46
+ Caboose.log("Error: line item #{li.id} (#{li.variant.product.title}) does not fit into any package.")
47
+ return false
48
+ end
49
+
50
+ def fits(line_item = nil)
51
+ variants = self.line_items.collect{ |li| li.variant }
52
+ variants << line_item.variant if line_item
53
+ return self.shipping_package.fits(variants)
54
+ end
55
+
56
+ # Gets the activemerchant package based on the shipping package
57
+ def activemerchant_package
58
+ sc = self.invoice.site.store_config
59
+
60
+ weight = 0.0
61
+ self.line_items.each{ |li| weight = weight + (li.variant.weight * li.quantity) }
62
+ weight = weight * 0.035274 if sc.weight_unit == StoreConfig::WEIGHT_UNIT_METRIC # grams to ounces
63
+
64
+ sp = self.shipping_package
65
+ dimensions = [sp.outside_length, sp.outside_width, sp.outside_height]
66
+ if sc.length_unit == StoreConfig::LENGTH_UNIT_METRIC # cm to inches
67
+ dimensions[0] = dimensions[0] / 2.54
68
+ dimensions[1] = dimensions[0] / 2.54
69
+ dimensions[2] = dimensions[0] / 2.54
70
+ end
71
+
72
+ return ActiveShipping::Package.new(weight, dimensions, :units => :imperial, :container => :variable)
73
+ end
74
+
75
+ end
76
+ end
@@ -2,42 +2,42 @@ require 'active_shipping'
2
2
  #include ActiveMerchant::Shipping
3
3
 
4
4
  module Caboose
5
- class OrderPackageCalculator
5
+ class InvoicePackageCalculator
6
6
 
7
- def self.custom_order_packages(store_config, order)
7
+ def self.custom_invoice_packages(store_config, invoice)
8
8
  return eval(store_config.custom_packages_function)
9
9
  end
10
10
 
11
- def self.order_packages(order)
11
+ def self.invoice_packages(invoice)
12
12
  return [] if Caboose::store_shipping.nil?
13
13
 
14
- sc = order.site.store_config
14
+ sc = invoice.site.store_config
15
15
  if !sc.auto_calculate_packages
16
- order_packages = self.custom_order_packages(sc, order)
17
- return order_packages
16
+ invoice_packages = self.custom_invoice_packages(sc, invoice)
17
+ return invoice_packages
18
18
  end
19
19
 
20
- # Remove any order packages
21
- LineItem.where(:order_id => order.id).update_all(:order_package_id => nil)
22
- OrderPackage.where(:order_id => order.id).destroy_all
20
+ # Remove any invoice packages
21
+ LineItem.where(:invoice_id => invoice.id).update_all(:invoice_package_id => nil)
22
+ InvoicePackage.where(:invoice_id => invoice.id).destroy_all
23
23
 
24
24
  # Calculate what shipping packages we'll need
25
- OrderPackage.create_for_order(order)
25
+ InvoicePackage.create_for_invoice(invoice)
26
26
 
27
27
  return all_rates
28
28
  end
29
29
 
30
- def self.rate(order)
31
- return nil if !order.shipping_service_code
32
- self.rates(order).each { |rate| return rate if rate[:service_code] == order.shipping_service_code }
30
+ def self.rate(invoice)
31
+ return nil if !invoice.shipping_service_code
32
+ self.rates(invoice).each { |rate| return rate if rate[:service_code] == invoice.shipping_service_code }
33
33
  return nil
34
34
  end
35
35
 
36
- # Calculates the packages required for all the items in the order
37
- #def self.packages_for_order(order)
36
+ # Calculates the packages required for all the items in the invoice
37
+ #def self.packages_for_invoice(invoice)
38
38
  #
39
- # # Make sure all the items in the order have attributes set
40
- # order.line_items.each do |li|
39
+ # # Make sure all the items in the invoice have attributes set
40
+ # invoice.line_items.each do |li|
41
41
  # v = li.variant
42
42
  # Caboose.log("Error: variant #{v.id} has a zero weight") and return false if v.weight.nil? || v.weight == 0
43
43
  # next if v.volume && v.volume > 0
@@ -48,9 +48,9 @@ module Caboose
48
48
  # v.save
49
49
  # end
50
50
  #
51
- # # Reorder the items in the order by volume
51
+ # # Reorder the items in the invoice by volume
52
52
  # h = {}
53
- # order.line_items.each do |li|
53
+ # invoice.line_items.each do |li|
54
54
  # (1..li.quantity).each do |i|
55
55
  # v = li.variant
56
56
  # h[v.volume] = v
@@ -58,7 +58,7 @@ module Caboose
58
58
  # end
59
59
  # variants = h.sort_by{ |k,v| k }.collect{ |x| x[1] }
60
60
  #
61
- # all_packages = ShippingPackage.reorder(:price).all
61
+ # all_packages = ShippingPackage.reinvoice(:price).all
62
62
  # packages = []
63
63
  #
64
64
  # # Now go through each variant and fit it in a new or existing package
@@ -2,9 +2,9 @@ require 'prawn'
2
2
  require 'prawn/table'
3
3
 
4
4
  module Caboose
5
- class OrderPdf < Prawn::Document
5
+ class InvoicePdf < Prawn::Document
6
6
 
7
- attr_accessor :order, :card_type, :card_number
7
+ attr_accessor :invoice, :card_type, :card_number
8
8
 
9
9
  def to_pdf
10
10
 
@@ -18,9 +18,9 @@ module Caboose
18
18
 
19
19
  #image open("https://dmwwflw4i3miv.cloudfront.net/logo.png"), :position => :center
20
20
  move_down 10
21
- order_info
21
+ invoice_info
22
22
  move_down 15
23
- order_table
23
+ invoice_table
24
24
  move_down 15
25
25
  customer_info
26
26
  move_down 15
@@ -39,8 +39,8 @@ module Caboose
39
39
 
40
40
  def get_card_details
41
41
 
42
- sc = self.order.site.store_config
43
- ot = self.order.order_transactions.where(:transaction_type => OrderTransaction::TYPE_AUTHORIZE, :success => true).first
42
+ sc = self.invoice.site.store_config
43
+ ot = self.invoice.invoice_transactions.where(:transaction_type => InvoiceTransaction::TYPE_AUTHORIZE, :success => true).first
44
44
 
45
45
  case sc.pp_name
46
46
  when 'authorize.net'
@@ -62,14 +62,14 @@ module Caboose
62
62
 
63
63
  end
64
64
 
65
- def order_info
65
+ def invoice_info
66
66
 
67
- order_info = "Order Number: #{order.order_number}\n"
68
- order_info << "Order Date: #{order.date_created.strftime('%d %b %Y %H:%M:%S %p')}\n"
69
- order_info << "Status: #{order.status.capitalize}\n"
67
+ invoice_info = "Invoice Number: #{invoice.invoice_number}\n"
68
+ invoice_info << "Invoice Date: #{invoice.date_created.strftime('%d %b %Y %H:%M:%S %p')}\n"
69
+ invoice_info << "Status: #{invoice.status.capitalize}\n"
70
70
  tbl = []
71
71
  tbl << [
72
- { :content => order_info }
72
+ { :content => invoice_info }
73
73
  ]
74
74
  move_down 4
75
75
  table tbl, :position => 7, :width => 530
@@ -78,19 +78,19 @@ module Caboose
78
78
 
79
79
  def customer_info
80
80
 
81
- c = order.customer
81
+ c = invoice.customer
82
82
 
83
83
  # #{self.card_type} ending in #{self.card_number
84
- ba = order.billing_address
85
- ba_address = "#{ba.address1}" + (ba.address2.blank? ? '' : "\n#{ba.address2}") + "\n#{ba.city}, #{ba.state} #{ba.zip}"
84
+ ba = invoice.billing_address
85
+ ba_address = ba ? "#{ba.address1}" + (ba.address2.blank? ? '' : "\n#{ba.address2}") + "\n#{ba.city}, #{ba.state} #{ba.zip}" : ''
86
86
  billed_to = [
87
- [{ :content => "Name" , :border_width => 0, :width => 55 },{ :content => "#{ba.first_name} #{ba.last_name}" , :border_width => 0, :width => 200 }],
88
- [{ :content => "Address" , :border_width => 0, :width => 55 },{ :content => ba_address , :border_width => 0, :width => 200 }],
89
- [{ :content => "Email" , :border_width => 0, :width => 55 },{ :content => "#{c.email}" , :border_width => 0, :width => 200 }],
90
- [{ :content => "Phone" , :border_width => 0, :width => 55 },{ :content => "#{self.formatted_phone(c.phone)}" , :border_width => 0, :width => 200 }]
87
+ [{ :content => "Name" , :border_width => 0, :width => 55 },{ :content => ba ? "#{ba.first_name} #{ba.last_name}" : '' , :border_width => 0, :width => 200 }],
88
+ [{ :content => "Address" , :border_width => 0, :width => 55 },{ :content => ba ? ba_address : '' , :border_width => 0, :width => 200 }],
89
+ [{ :content => "Email" , :border_width => 0, :width => 55 },{ :content => "#{c.email}" , :border_width => 0, :width => 200 }],
90
+ [{ :content => "Phone" , :border_width => 0, :width => 55 },{ :content => "#{self.formatted_phone(c.phone)}" , :border_width => 0, :width => 200 }]
91
91
  ]
92
92
 
93
- sa = order.shipping_address
93
+ sa = invoice.shipping_address
94
94
  sa_address = "#{sa.address1}" + (sa.address2.blank? ? '' : "\n#{sa.address2}") + "\n#{sa.city}, #{sa.state} #{sa.zip}"
95
95
  shipped_to = [
96
96
  [{ :content => "Name" , :border_width => 0, :width => 55 },{ :content => "#{sa.first_name} #{sa.last_name}" , :border_width => 0, :width => 200 }],
@@ -113,9 +113,9 @@ module Caboose
113
113
 
114
114
  end
115
115
 
116
- def order_table
116
+ def invoice_table
117
117
 
118
- hide_prices = order.hide_prices_for_any_line_item?
118
+ hide_prices = invoice.hide_prices_for_any_line_item?
119
119
 
120
120
  tbl = []
121
121
  tbl << [
@@ -129,9 +129,9 @@ module Caboose
129
129
  tbl[0] << { :content => "Amount" , :align => :right , :valign => :bottom }
130
130
  end
131
131
 
132
- order.calculate
132
+ invoice.calculate
133
133
 
134
- order.order_packages.all.each do |pk|
134
+ invoice.invoice_packages.all.each do |pk|
135
135
 
136
136
  carrier = pk.shipping_method.carrier
137
137
  service = pk.shipping_method.service_name
@@ -172,7 +172,7 @@ module Caboose
172
172
  end
173
173
  end
174
174
 
175
- unassigned = order.line_items.where("order_package_id IS NULL OR order_package_id = ?",-1)
175
+ unassigned = invoice.line_items.where("invoice_package_id IS NULL OR invoice_package_id = ?",-1)
176
176
  unassigned.each_with_index do |li, index|
177
177
  options = ''
178
178
  if li.variant.product.option1 && li.variant.option1 then options += li.variant.product.option1 + ": " + li.variant.option1 + "\n" end
@@ -208,11 +208,11 @@ module Caboose
208
208
  tbl << arr
209
209
  end
210
210
  if !hide_prices
211
- tbl << [{ :content => "Subtotal" , :colspan => 6, :align => :right }, { :content => "$" + sprintf("%.2f", order.subtotal ) , :align => :right }]
212
- tbl << [{ :content => "Discount" , :colspan => 6, :align => :right }, { :content => "(-) $" + sprintf("%.2f", order.discount ? order.discount : 0.0 ) , :align => :right }]
213
- tbl << [{ :content => "Shipping and Handling Charges" , :colspan => 6, :align => :right }, { :content => "(+) $" + sprintf("%.2f", order.shipping_and_handling ) , :align => :right }]
214
- tbl << [{ :content => "Sales Tax" , :colspan => 6, :align => :right }, { :content => "(+) $" + sprintf("%.2f", order.tax ? order.tax : 0.0 ) , :align => :right }]
215
- tbl << [{ :content => "Grand Total" , :colspan => 6, :align => :right, :font_style => :bold }, { :content => "$" + sprintf("%.2f", order.total ) , :align => :right, :font_style => :bold }]
211
+ tbl << [{ :content => "Subtotal" , :colspan => 6, :align => :right }, { :content => "$" + sprintf("%.2f", invoice.subtotal ) , :align => :right }]
212
+ tbl << [{ :content => "Discount" , :colspan => 6, :align => :right }, { :content => "(-) $" + sprintf("%.2f", invoice.discount ? invoice.discount : 0.0 ) , :align => :right }]
213
+ tbl << [{ :content => "Shipping and Handling Charges" , :colspan => 6, :align => :right }, { :content => "(+) $" + sprintf("%.2f", invoice.shipping_and_handling ) , :align => :right }]
214
+ tbl << [{ :content => "Sales Tax" , :colspan => 6, :align => :right }, { :content => "(+) $" + sprintf("%.2f", invoice.tax ? invoice.tax : 0.0 ) , :align => :right }]
215
+ tbl << [{ :content => "Grand Total" , :colspan => 6, :align => :right, :font_style => :bold }, { :content => "$" + sprintf("%.2f", invoice.total ) , :align => :right, :font_style => :bold }]
216
216
  end
217
217
 
218
218
  table tbl , :position => 7, :width => 530
@@ -229,7 +229,7 @@ module Caboose
229
229
 
230
230
  def payment_info
231
231
 
232
- trans = order.order_transactions.where(:transaction_type => OrderTransaction::TYPE_AUTHORIZE, :success => true).first
232
+ trans = invoice.invoice_transactions.where(:transaction_type => InvoiceTransaction::TYPE_AUTHORIZE, :success => true).first
233
233
  tbl = []
234
234
  tbl2 = []
235
235
  tbl3 = []
@@ -1,7 +1,7 @@
1
1
  module Caboose
2
- class OrderReporter
2
+ class InvoiceReporter
3
3
 
4
- def OrderReporter.summary_report(site_id, d1, d2)
4
+ def InvoiceReporter.summary_report(site_id, d1, d2)
5
5
  q = ["select
6
6
  concat(date_part('year', date_authorized), '-', date_part('month', date_authorized), '-', date_part('day', date_authorized)),
7
7
  count(*),
@@ -13,7 +13,7 @@ module Caboose
13
13
  sum(total),
14
14
  sum(cost),
15
15
  sum(profit)
16
- from store_orders
16
+ from store_invoices
17
17
  where site_id = ?
18
18
  and (financial_status = ? or financial_status = ?)
19
19
  and date_authorized >= ?
@@ -68,7 +68,7 @@ module Caboose
68
68
  return days2
69
69
  end
70
70
 
71
- def OrderReporter.city_report(site_id, d1, d2)
71
+ def InvoiceReporter.city_report(site_id, d1, d2)
72
72
  q = ["select
73
73
  count(*),
74
74
  sum(O.subtotal),
@@ -79,7 +79,7 @@ module Caboose
79
79
  sum(O.total),
80
80
  SA.city,
81
81
  SA.state
82
- from store_orders O
82
+ from store_invoices O
83
83
  left join store_addresses SA on O.shipping_address_id = SA.id
84
84
  where O.site_id = ?
85
85
  and (O.financial_status = ? or O.financial_status = ?)
@@ -0,0 +1,169 @@
1
+ module Caboose
2
+ class InvoiceTransaction < ActiveRecord::Base
3
+ self.table_name = 'store_invoice_transactions'
4
+ self.primary_key = 'id'
5
+
6
+ belongs_to :invoice
7
+ belongs_to :parent, :class_name => 'Caboose::InvoiceTransaction', :foreign_key => :parent_id
8
+ attr_accessible :id,
9
+ :invoice_id,
10
+ :parent_id,
11
+ :transaction_id,
12
+ :transaction_type,
13
+ :amount,
14
+ :amount_refunded,
15
+ :auth_code,
16
+ :date_processed,
17
+ :response_code,
18
+ :success,
19
+ :captured,
20
+ :refunded
21
+
22
+ TYPE_AUTHORIZE = 'auth'
23
+ TYPE_CAPTURE = 'capture'
24
+ TYPE_AUTHCAP = 'authcap'
25
+ TYPE_VOID = 'void'
26
+ TYPE_REFUND = 'refund'
27
+
28
+ after_initialize :check_nil_fields
29
+
30
+ def children
31
+ InvoiceTransaction.where(:parent_id => self.id).all
32
+ end
33
+
34
+ def check_nil_fields
35
+ self.amount = 0.00 if self.amount.nil?
36
+ end
37
+
38
+ def self.type_from_authnet_status(status)
39
+ case status
40
+ when 'settledSuccessfully' then InvoiceTransaction::TYPE_CAPTURE
41
+ when 'voided' then InvoiceTransaction::TYPE_VOID
42
+ when 'declined' then InvoiceTransaction::TYPE_AUTHORIZE
43
+ when 'authorizedPendingCapture' then InvoiceTransaction::TYPE_AUTHORIZE
44
+ when 'refundSettledSuccessfully' then InvoiceTransaction::TYPE_REFUND
45
+ end
46
+ end
47
+
48
+ # Capture funds from a previously authorized transaction
49
+ def capture
50
+
51
+ return { :error => "This invoice doesn't seem to be authorized." } if !self.success
52
+
53
+ ct = InvoiceTransaction.where(:parent_id => self.id, :transaction_type => InvoiceTransaction::TYPE_CAPTURE, :success => true).first
54
+ return { :error => "Funds for this invoice have already been captured." } if ct
55
+
56
+ resp = Caboose::StdClass.new
57
+ sc = self.invoice.site.store_config
58
+ case sc.pp_name
59
+
60
+ when StoreConfig::PAYMENT_PROCESSOR_STRIPE
61
+
62
+ Stripe.api_key = sc.stripe_secret_key.strip
63
+ bt = nil
64
+ begin
65
+ c = Stripe::Charge.retrieve(self.transaction_id)
66
+ c = c.capture
67
+ bt = Stripe::BalanceTransaction.retrieve(c.balance_transaction)
68
+ rescue Exception => ex
69
+ resp.error = "Error during capture process\n#{ex.message}"
70
+ end
71
+
72
+ if resp.error.nil?
73
+ InvoiceTransaction.create(
74
+ :invoice_id => self.invoice_id,
75
+ :parent_id => self.id,
76
+ :transaction_id => bt.id,
77
+ :transaction_type => InvoiceTransaction::TYPE_CAPTURE,
78
+ :amount => bt.amount / 100.0,
79
+ :date_processed => DateTime.strptime(bt.created.to_s, '%s'),
80
+ :success => bt.status == 'succeeded' || bt.status == 'pending'
81
+ )
82
+ if bt.status == 'succeeded' || bt.status == 'pending'
83
+ self.captured = true
84
+ self.save
85
+ self.invoice.financial_status = Invoice::FINANCIAL_STATUS_CAPTURED
86
+ self.invoice.save
87
+ resp.success = true
88
+ else
89
+ resp.error = "Error capturing funds."
90
+ end
91
+ end
92
+
93
+ end
94
+ return resp
95
+
96
+ end
97
+
98
+ def refund(amount_to_refund = nil)
99
+
100
+ resp = StdClass.new
101
+
102
+ amount_available_to_refund = self.amount - (self.amount_refunded ? self.amount_refunded : 0.00)
103
+ amount_to_refund = amount_available_to_refund if amount_to_refund.nil?
104
+
105
+ if self.transaction_type != InvoiceTransaction::TYPE_AUTHORIZE && self.transaction_type != InvoiceTransaction::TYPE_AUTHCAP
106
+ resp.error = "You can only refund authorize transactions."
107
+ elsif amount_to_refund > amount_available_to_refund
108
+ resp.error = "The amount to refund exceeds the amount available to refund."
109
+ else
110
+
111
+ sc = self.invoice.site.store_config
112
+ case sc.pp_name
113
+
114
+ when StoreConfig::PAYMENT_PROCESSOR_STRIPE
115
+
116
+ Stripe.api_key = sc.stripe_secret_key.strip
117
+ bt = nil
118
+ begin
119
+ r = Stripe::Refund.create(:charge => self.transaction_id, :amount => (amount_to_refund * 100).to_i)
120
+ rescue Exception => ex
121
+ resp.error = "Error during refund process\n#{ex.message}"
122
+ end
123
+
124
+ if resp.error.nil?
125
+ it = InvoiceTransaction.create(
126
+ :invoice_id => self.invoice_id,
127
+ :parent_id => self.id,
128
+ :transaction_id => r.id,
129
+ :transaction_type => InvoiceTransaction::TYPE_REFUND,
130
+ :amount => r.amount / 100.0,
131
+ :date_processed => DateTime.strptime(r.created.to_s, '%s'),
132
+ :success => r.status == 'succeeded' || r.status == 'pending'
133
+ )
134
+ if it.success
135
+ self.amount_refunded = (self.amount_refunded ? self.amount_refunded : 0.00) + amount_to_refund
136
+ self.refunded = self.amount_refunded >= self.amount
137
+ self.save
138
+ if self.amount_refunded >= self.invoice.total
139
+ self.invoice.financial_status = Invoice::FINANCIAL_STATUS_REFUNDED
140
+ self.invoice.save
141
+ end
142
+ resp.success = true
143
+ else
144
+ resp.error = "Error refunding transaction."
145
+ end
146
+ end
147
+
148
+ end
149
+ end
150
+ return resp
151
+ end
152
+
153
+ def void
154
+
155
+ resp = StdClass.new
156
+ sc = self.invoice.site.store_config
157
+ case sc.pp_name
158
+
159
+ when StoreConfig::PAYMENT_PROCESSOR_STRIPE
160
+ return self.refund
161
+
162
+ end
163
+ return resp
164
+ end
165
+
166
+ end
167
+ end
168
+
169
+