PageRenderTime 1341ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/core/app/controllers/spree/admin/orders_controller.rb

https://github.com/fabien/spree
Ruby | 127 lines | 94 code | 23 blank | 10 comment | 12 complexity | a32f3fac25de74a682b23372d7894dcc MD5 | raw file
  1. module Spree
  2. module Admin
  3. class OrdersController < Spree::Admin::BaseController
  4. require 'spree/core/gateway_error'
  5. before_filter :initialize_txn_partials
  6. before_filter :initialize_order_events
  7. before_filter :load_order, :only => [:show, :edit, :update, :fire, :resend]
  8. respond_to :html
  9. def index
  10. params[:q] ||= {}
  11. params[:q][:completed_at_not_null] ||= '1' if Spree::Config[:show_only_complete_orders_by_default]
  12. @show_only_completed = params[:q][:completed_at_not_null].present?
  13. params[:q][:s] ||= @show_only_completed ? 'completed_at desc' : 'created_at desc'
  14. # As date params are deleted if @show_only_completed, store
  15. # the original date so we can restore them into the params
  16. # after the search
  17. created_at_gt = params[:q][:created_at_gt]
  18. created_at_lt = params[:q][:created_at_lt]
  19. params[:q].delete(:inventory_units_shipment_id_null) if params[:q][:inventory_units_shipment_id_null] == "0"
  20. if !params[:q][:created_at_gt].blank?
  21. params[:q][:created_at_gt] = Time.zone.parse(params[:q][:created_at_gt]).beginning_of_day rescue ""
  22. end
  23. if !params[:q][:created_at_lt].blank?
  24. params[:q][:created_at_lt] = Time.zone.parse(params[:q][:created_at_lt]).end_of_day rescue ""
  25. end
  26. if @show_only_completed
  27. params[:q][:completed_at_gt] = params[:q].delete(:created_at_gt)
  28. params[:q][:completed_at_lt] = params[:q].delete(:created_at_lt)
  29. end
  30. @search = Order.ransack(params[:q])
  31. @orders = @search.result(:distinct => true).includes([:user, :shipments, :payments]).page(params[:page]).per(Spree::Config[:orders_per_page])
  32. # Restore dates
  33. params[:q][:created_at_gt] = created_at_gt
  34. params[:q][:created_at_lt] = created_at_lt
  35. respond_with(@orders)
  36. end
  37. def show
  38. respond_with(@order)
  39. end
  40. def new
  41. @order = Order.create
  42. respond_with(@order)
  43. end
  44. def edit
  45. respond_with(@order)
  46. end
  47. def update
  48. return_path = nil
  49. if @order.update_attributes(params[:order]) && @order.line_items.present?
  50. @order.update!
  51. unless @order.complete?
  52. # Jump to next step if order is not complete.
  53. return_path = admin_order_customer_path(@order)
  54. else
  55. # Otherwise, go back to first page since all necessary information has been filled out.
  56. return_path = admin_order_path(@order)
  57. end
  58. else
  59. @order.errors.add(:line_items, t('errors.messages.blank')) if @order.line_items.empty?
  60. end
  61. respond_with(@order) do |format|
  62. format.html do
  63. if return_path
  64. redirect_to return_path
  65. else
  66. render :action => :edit
  67. end
  68. end
  69. end
  70. end
  71. def fire
  72. # TODO - possible security check here but right now any admin can before any transition (and the state machine
  73. # itself will make sure transitions are not applied in the wrong state)
  74. event = params[:e]
  75. if @order.send("#{event}")
  76. flash.notice = t(:order_updated)
  77. else
  78. flash[:error] = t(:cannot_perform_operation)
  79. end
  80. rescue Spree::Core::GatewayError => ge
  81. flash[:error] = "#{ge.message}"
  82. ensure
  83. respond_with(@order) { |format| format.html { redirect_to :back } }
  84. end
  85. def resend
  86. OrderMailer.confirm_email(@order, true).deliver
  87. flash.notice = t(:order_email_resent)
  88. respond_with(@order) { |format| format.html { redirect_to :back } }
  89. end
  90. private
  91. def load_order
  92. @order = Order.find_by_number!(params[:id], :include => :adjustments) if params[:id]
  93. end
  94. # Allows extensions to add new forms of payment to provide their own display of transactions
  95. def initialize_txn_partials
  96. @txn_partials = []
  97. end
  98. # Used for extensions which need to provide their own custom event links on the order details view.
  99. def initialize_order_events
  100. @order_events = %w{cancel resume}
  101. end
  102. end
  103. end
  104. end