cirandas.net

ref: master

plugins/orders/controllers/myprofile/orders_plugin_admin_controller.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class OrdersPluginAdminController < MyProfileController

  include OrdersPlugin::Report
  include OrdersPlugin::TranslationHelper

  protect 'edit_profile', :profile
  before_filter :set_admin

  helper OrdersPlugin::TranslationHelper
  helper OrdersPlugin::DisplayHelper

  def index
    @purchases_month = profile.purchases.latest.first.created_at.month rescue Date.today.month
    @purchases_year = profile.purchases.latest.first.created_at.year rescue Date.today.year
    @sales_month = profile.sales.latest.first.created_at.month rescue Date.today.month
    @sales_year = profile.sales.latest.first.created_at.year rescue Date.today.year

    @purchases = profile.purchases.latest.by_month(@purchases_month).by_year(@purchases_year).paginate(per_page: 30, page: params[:page])
    @sales = profile.sales.latest.by_month(@sales_month).by_year(@sales_year).paginate(per_page: 30, page: params[:page])
  end

  def filter
    @method = params[:orders_method]
    raise unless self.filter_methods.include? @method

    @actor_name = params[:actor_name].to_sym
    params[:page] = 1 if params[:page].blank?

    @scope ||= profile
    @scope = @scope.send @method
    @orders = hmvc_context::Order.search_scope(@scope, params).paginate(per_page: 30, page: params[:page])

    respond_to do |format|
      format.html{ render partial: 'orders_plugin_admin/results', locals: { orders: @orders, actor_name: @actor_name} }
      format.js{ render template: 'orders_plugin_admin/filter' }
    end
  end

  def edit
    @actor_name = params[:actor_name].to_sym
    @orders_method = if @actor_name == :supplier then :sales else :purchases end

    @order = profile.send(@orders_method).find params[:id]
    return render_access_denied unless @user_is_admin or @order.verify_actor? profile, @actor_name
    @order.update params[:order]

    respond_to do |format|
      format.js{ render 'orders_plugin_admin/edit' }
      format.html{ render partial: 'orders_plugin_admin/edit', locals: {order: @order, actor_name: @actor_name} }
    end
  end

  def report_products
    @method = params[:orders_method]
    raise unless self.filter_methods.include? @method
    @scope ||= profile
    @scope = @scope.send @method
    @orders = @scope.where(id: params[:ids])
    report_file = report_products_by_supplier hmvc_context::Order.supplier_products_by_suppliers @orders

    send_file report_file, type: 'application/xlsx',
      disposition: 'attachment',
      filename: t('controllers.myprofile.admin.products_report') % {
        date: DateTime.now.strftime("%Y-%m-%d"), profile_identifier: profile.identifier, name: ""}
  end

  def report_orders
    @method = params[:orders_method]
    raise unless self.filter_methods.include? @method
    @scope ||= profile
    @scope = @scope.send @method
    @orders = @scope.where(id: params[:ids])
    report_file = report_orders_by_consumer @orders

    send_file report_file, type: 'application/xlsx',
      disposition: 'attachment',
      filename: t('controllers.myprofile.admin.orders_report') % {date: DateTime.now.strftime("%Y-%m-%d"), profile_identifier: profile.identifier, name: ''}
  end

  protected

  def filter_methods
    ['sales', 'purchases']
  end

  def set_admin
    @admin = true
  end

  extend HMVC::ClassMethods
  hmvc OrdersPlugin

end