Class: OrdersController

Inherits:
ApplicationController show all
Includes:
Concerns::SendOrderPdf
Defined in:
app/controllers/orders_controller.rb

Overview

Controller for managing orders, i.e. all actions that require the “orders” role. Normal ordering actions of members of order groups is handled by the OrderingController.

Instance Method Summary collapse

Methods inherited from ApplicationController

current

Methods included from PathHelper

#finance_group_transactions_path

Instance Method Details

#createObject

Save a new order. order_articles will be saved in Order.article_ids=()



82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/orders_controller.rb', line 82

def create
  @order = Order.new(params[:order])
  @order.created_by = current_user
  @order.updated_by = current_user
  if @order.save
    flash[:notice] = I18n.t('orders.create.notice')
    redirect_to @order
  else
    logger.debug "[debug] order errors: #{@order.errors.messages}"
    render action: 'new'
  end
end

#destroyObject

Delete an order.



107
108
109
110
# File 'app/controllers/orders_controller.rb', line 107

def destroy
  Order.find(params[:id]).destroy
  redirect_to action: 'index'
end

#editObject

Page to edit an exsiting order. editing finished orders is done in FinanceController



76
77
78
# File 'app/controllers/orders_controller.rb', line 76

def edit
  @order = Order.includes(:articles).find(params[:id])
end

#finishObject

Finish a current order.



113
114
115
116
117
118
119
# File 'app/controllers/orders_controller.rb', line 113

def finish
  order = Order.find(params[:id])
  order.finish!(@current_user)
  redirect_to order, notice: I18n.t('orders.finish.notice')
rescue StandardError => e
  redirect_to orders_url, alert: I18n.t('errors.general_msg', msg: e.message)
end

#indexObject

List orders



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/orders_controller.rb', line 13

def index
  @open_orders = Order.open.includes(:supplier)
  @finished_orders = Order.finished_not_closed.includes(:supplier)
  @per_page = 15
  sort = if params['sort']
           case params['sort']
           when 'supplier'         then 'suppliers.name, ends DESC'
           when 'pickup'           then 'pickup DESC'
           when 'ends'             then 'ends DESC'
           when 'supplier_reverse' then 'suppliers.name DESC'
           when 'ends_reverse'     then 'ends'
           end
         else
           'ends DESC'
         end
  @suppliers = Supplier.having_articles.order('suppliers.name')
  @orders = Order.closed.includes(:supplier).reorder(sort).page(params[:page]).per(@per_page)
end

#newObject

Page to create a new order.



62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/orders_controller.rb', line 62

def new
  if params[:order_id]
    old_order = Order.find(params[:order_id])
    @order = Order.new(supplier_id: old_order.supplier_id).init_dates
    @order.article_ids = old_order.article_ids
  else
    @order = Order.new(supplier_id: params[:supplier_id]).init_dates
  end
rescue StandardError => e
  redirect_to orders_url, alert: t('errors.general_msg', msg: e.message)
end

#receiveObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/orders_controller.rb', line 130

def receive
  @order = Order.find(params[:id])
  if request.post?
    Order.transaction do
      s = update_order_amounts
      @order.update_attribute(:state, 'received') if @order.state != 'received'

      flash[:notice] = (s ? I18n.t('orders.receive.notice', msg: s) : I18n.t('orders.receive.notice_none'))
    end
    NotifyReceivedOrderJob.perform_later(@order)
    if current_user.role_orders? || current_user.role_finance?
      redirect_to @order
    elsif current_user.role_pickups?
      redirect_to pickups_path
    else
      redirect_to receive_order_path(@order)
    end
  else
    @order_articles = @order.order_articles.ordered_or_member.includes(:article).order('articles.order_number, articles.name')
  end
end

#receive_on_order_article_createObject

See publish/subscribe design pattern in /doc.



152
153
154
155
# File 'app/controllers/orders_controller.rb', line 152

def receive_on_order_article_create # See publish/subscribe design pattern in /doc.
  @order_article = OrderArticle.find(params[:order_article_id])
  render layout: false
end

#receive_on_order_article_updateObject

See publish/subscribe design pattern in /doc.



157
158
159
160
# File 'app/controllers/orders_controller.rb', line 157

def receive_on_order_article_update # See publish/subscribe design pattern in /doc.
  @order_article = OrderArticle.find(params[:order_article_id])
  render layout: false
end

#remove_empty_articleObject (protected)



215
216
217
# File 'app/controllers/orders_controller.rb', line 215

def remove_empty_article
  params[:order][:article_ids].compact_blank! if params[:order] && params[:order][:article_ids]
end

#send_result_to_supplierObject

Send a order to the supplier.



122
123
124
125
126
127
128
# File 'app/controllers/orders_controller.rb', line 122

def send_result_to_supplier
  order = Order.find(params[:id])
  order.send_to_supplier!(@current_user)
  redirect_to order, notice: I18n.t('orders.send_to_supplier.notice')
rescue StandardError => e
  redirect_to order, alert: I18n.t('errors.general_msg', msg: e.message)
end

#showObject

Gives a view for the results to a specific order Renders also the pdf



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
# File 'app/controllers/orders_controller.rb', line 34

def show
  @order = Order.find(params[:id])
  @view = (params[:view] || 'default').gsub(/[^-_a-zA-Z0-9]/, '')
  @partial = case @view
             when 'default'  then 'articles'
             when 'groups'   then 'shared/articles_by/groups'
             when 'articles' then 'shared/articles_by/articles'
             else 'articles'
             end

  respond_to do |format|
    format.html
    format.js do
      render layout: false
    end
    format.pdf do
      send_order_pdf @order, params[:document]
    end
    format.csv do
      send_data OrderCsv.new(@order).to_csv, filename: @order.name + '.csv', type: 'text/csv'
    end
    format.text do
      send_data OrderTxt.new(@order).to_txt, filename: @order.name + '.txt', type: 'text/plain'
    end
  end
end

#updateObject

Update an existing order.



96
97
98
99
100
101
102
103
104
# File 'app/controllers/orders_controller.rb', line 96

def update
  @order = Order.find params[:id]
  if @order.update(params[:order].merge(updated_by: current_user))
    flash[:notice] = I18n.t('orders.update.notice')
    redirect_to action: 'show', id: @order
  else
    render action: 'edit'
  end
end

#update_order_amountsObject (protected)



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/controllers/orders_controller.rb', line 164

def update_order_amounts
  return unless params[:order_articles]

  # where to leave remainder during redistribution
  rest_to = []
  rest_to << :tolerance if params[:rest_to_tolerance]
  rest_to << :stock if params[:rest_to_stock]
  rest_to << nil
  # count what happens to the articles:
  #   changed, rest_to_tolerance, rest_to_stock, left_over
  counts = [0] * 4
  cunits = [0] * 4
  # This was once wrapped in a transaction, but caused
  # "MySQL lock timeout exceeded" errors. It's ok to do
  # this article-by-article anway.
  params[:order_articles].each do |oa_id, oa_params|
    next if oa_params.blank?

    oa = OrderArticle.find(oa_id)
    # update attributes; don't use update_attribute because it calls save
    # which makes received_changed? not work anymore
    oa.attributes = oa_params
    if oa.units_received_changed?
      counts[0] += 1
      if oa.units_received.present?
        cunits[0] += oa.units_received * oa.article.unit_quantity
        oacounts = oa.redistribute oa.units_received * oa.price.unit_quantity, rest_to
        oacounts.each_with_index do |c, i|
          cunits[i + 1] += c
          counts[i + 1] += 1 if c > 0
        end
      end
    end
    oa.save!
  end
  return nil if counts[0] == 0

  notice = []
  notice << I18n.t('orders.update_order_amounts.msg1', count: counts[0], units: cunits[0])
  if params[:rest_to_tolerance]
    notice << I18n.t('orders.update_order_amounts.msg2', count: counts[1],
                                                         units: cunits[1])
  end
  notice << I18n.t('orders.update_order_amounts.msg3', count: counts[2], units: cunits[2]) if params[:rest_to_stock]
  if counts[3] > 0 || cunits[3] > 0
    notice << I18n.t('orders.update_order_amounts.msg4', count: counts[3],
                                                         units: cunits[3])
  end
  notice.join(', ')
end