PageRenderTime 1054ms CodeModel.GetById 4ms RepoModel.GetById 7ms app.codeStats 0ms

/app/controllers/spree/admin/orders_controller_decorator.rb

https://gitlab.com/srihas/openfoodnetwork
Ruby | 128 lines | 83 code | 28 blank | 17 comment | 8 complexity | bf1ee64f8df8da9fe59329fc78338307 MD5 | raw file
  1. require 'open_food_network/spree_api_key_loader'
  2. Spree::Admin::OrdersController.class_eval do
  3. include OpenFoodNetwork::SpreeApiKeyLoader
  4. helper CheckoutHelper
  5. before_filter :load_spree_api_key, :only => :bulk_management
  6. # We need to add expections for collection actions other than :index here
  7. # because spree_auth_devise causes load_order to be called, which results
  8. # in an auth failure as the @order object is nil for collection actions
  9. before_filter :check_authorization, except: [:bulk_management, :managed]
  10. before_filter :load_distribution_choices, only: [:new, :edit, :update]
  11. # After updating an order, the fees should be updated as well
  12. # Currently, adding or deleting line items does not trigger updating the
  13. # fees! This is a quick fix for that.
  14. # TODO: update fees when adding/removing line items
  15. # instead of the update_distribution_charge method.
  16. after_filter :update_distribution_charge, :only => :update
  17. before_filter :require_distributor_abn, only: :invoice
  18. respond_to :html, :json
  19. # Mostly the original Spree method, tweaked to allow us to ransack with completed_at in a sane way
  20. def index
  21. params[:q] ||= {}
  22. params[:q][:completed_at_not_null] ||= '1' if Spree::Config[:show_only_complete_orders_by_default]
  23. @show_only_completed = params[:q][:completed_at_not_null].present?
  24. params[:q][:s] ||= @show_only_completed ? 'completed_at desc' : 'created_at desc'
  25. # As date params are deleted if @show_only_completed, store
  26. # the original date so we can restore them into the params
  27. # after the search
  28. created_at_gt = params[:q][:created_at_gt]
  29. created_at_lt = params[:q][:created_at_lt]
  30. params[:q].delete(:inventory_units_shipment_id_null) if params[:q][:inventory_units_shipment_id_null] == "0"
  31. if !params[:q][:created_at_gt].blank?
  32. params[:q][:created_at_gt] = Time.zone.parse(params[:q][:created_at_gt]).beginning_of_day rescue ""
  33. end
  34. if !params[:q][:created_at_lt].blank?
  35. params[:q][:created_at_lt] = Time.zone.parse(params[:q][:created_at_lt]).end_of_day rescue ""
  36. end
  37. # Changed this to stop completed_at being overriden when present
  38. if @show_only_completed
  39. params[:q][:completed_at_gt] = params[:q].delete(:created_at_gt) unless params[:q][:completed_at_gt]
  40. params[:q][:completed_at_lt] = params[:q].delete(:created_at_lt) unless params[:q][:completed_at_gt]
  41. end
  42. @orders = orders
  43. # Restore dates
  44. params[:q][:created_at_gt] = created_at_gt
  45. params[:q][:created_at_lt] = created_at_lt
  46. respond_with(@orders) do |format|
  47. format.html
  48. format.json do
  49. render_as_json @orders
  50. end
  51. end
  52. end
  53. # Overwrite to use confirm_email_for_customer instead of confirm_email.
  54. # This uses a new template. See mailers/spree/order_mailer_decorator.rb.
  55. def resend
  56. Spree::OrderMailer.confirm_email_for_customer(@order.id, true).deliver
  57. flash[:success] = t(:order_email_resent)
  58. respond_with(@order) { |format| format.html { redirect_to :back } }
  59. end
  60. def invoice
  61. pdf = render_to_string pdf: "invoice-#{@order.number}.pdf", template: "spree/admin/orders/invoice", formats: [:html], encoding: "UTF-8"
  62. Spree::OrderMailer.invoice_email(@order.id, pdf).deliver
  63. flash[:success] = t(:invoice_email_sent)
  64. respond_with(@order) { |format| format.html { redirect_to edit_admin_order_path(@order) } }
  65. end
  66. def print
  67. render pdf: "invoice-#{@order.number}", template: "spree/admin/orders/invoice", encoding: "UTF-8"
  68. end
  69. def update_distribution_charge
  70. @order.update_distribution_charge!
  71. end
  72. private
  73. def orders
  74. if json_request?
  75. @search = OpenFoodNetwork::Permissions.new(spree_current_user).editable_orders.ransack(params[:q])
  76. @search.result.reorder('id ASC')
  77. else
  78. @search = Spree::Order.accessible_by(current_ability, :index).ransack(params[:q])
  79. # Replaced this search to filter orders to only show those distributed by current user (or all for admin user)
  80. @search.result.includes([:user, :shipments, :payments]).
  81. distributed_by_user(spree_current_user).
  82. page(params[:page]).
  83. per(params[:per_page] || Spree::Config[:orders_per_page])
  84. end
  85. end
  86. def require_distributor_abn
  87. unless @order.distributor.abn.present?
  88. flash[:error] = t(:must_have_valid_business_number, enterprise_name: @order.distributor.name)
  89. respond_with(@order) { |format| format.html { redirect_to edit_admin_order_path(@order) } }
  90. end
  91. end
  92. def load_distribution_choices
  93. @shops = Enterprise.is_distributor.managed_by(spree_current_user).by_name
  94. ocs = OrderCycle.managed_by(spree_current_user)
  95. @order_cycles = ocs.soonest_closing +
  96. ocs.soonest_opening +
  97. ocs.closed +
  98. ocs.undated
  99. end
  100. end