Class: Backend::ParcelsController

Inherits:
BaseController show all
Defined in:
app/controllers/backend/parcels_controller.rb

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_theme

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseController

accounts_range_crit, crit_params, journal_entries_states_crit, journal_period_crit, journals_crit, search_conditions

Methods inherited from ApplicationController

#after_sign_in_path_for, #authorized?, human_action_name, #human_action_name

Class Method Details

.parcels_conditionsObject

params:

:q Text search
:s State search
:period Two Dates with _ separator
:recipient_id
:sender_id
:transporter_id
:delivery_mode Choice
:nature Choice

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
# File 'app/controllers/backend/parcels_controller.rb', line 36

def self.parcels_conditions
  code = ''
  code = search_conditions(parcels: [:number, :reference_number], entities: [:full_name, :number]) + " ||= []\n"
  code << "unless params[:period].blank? || params[:period].is_a?(Symbol)\n"
  code << "  if params[:period] != 'all'\n"
  code << "    interval = params[:period].split('_')\n"
  code << "    first_date = interval.first\n"
  code << "    last_date = interval.last\n"
  code << "    c[0] << \" AND #{Parcel.table_name}.planned_at BETWEEN ? AND ?\"\n"
  code << "    c << first_date\n"
  code << "    c << last_date\n"
  code << "  end\n "
  code << "end\n "
  code << "if params[:recipient_id].to_i > 0\n"
  code << "  c[0] << \" AND \#{Parcel.table_name}.recipient_id = ?\"\n"
  code << "  c << params[:recipient_id].to_i\n"
  code << "end\n"
  code << "if params[:sender_id].to_i > 0\n"
  code << "  c[0] << \" AND \#{Parcel.table_name}.sender_id = ?\"\n"
  code << "  c << params[:sender_id].to_i\n"
  code << "end\n"
  code << "if params[:transporter_id].to_i > 0\n"
  code << "  c[0] << \" AND \#{Parcel.table_name}.transporter_id = ?\"\n"
  code << "  c << params[:transporter_id].to_i\n"
  code << "end\n"
  code << "if params[:delivery_mode].present? && params[:delivery_mode] != 'all'\n"
  code << "  if Parcel.delivery_mode.values.include?(params[:delivery_mode].to_sym)\n"
  code << "    c[0] << ' AND #{Parcel.table_name}.delivery_mode = ?'\n"
  code << "    c << params[:delivery_mode]\n"
  code << "  end\n"
  code << "end\n"
  code << "if params[:nature].present? && params[:nature] != 'all'\n"
  code << "  if Parcel.nature.values.include?(params[:nature].to_sym)\n"
  code << "    c[0] << ' AND #{Parcel.table_name}.nature = ?'\n"
  code << "    c << params[:nature]\n"
  code << "  end\n"
  code << "end\n"
  code << "c\n"
  code.c
end

Instance Method Details

#indexObject

Displays the main page with the list of parcels


125
126
127
128
129
130
131
# File 'app/controllers/backend/parcels_controller.rb', line 125

def index
  respond_to do |format|
    format.html
    format.xml { render xml: @parcels }
    format.pdf { render pdf: @parcels, with: params[:template] }
  end
end

#invoiceObject

Converts parcel to trade


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/controllers/backend/parcels_controller.rb', line 189

def invoice
  parcels = find_parcels
  return unless parcels
  parcel = parcels.first
  if parcels.all? { |p| p.incoming? && p.third_id == parcel.third_id && p.invoiceable? }
    purchase = Parcel.convert_to_purchase(parcels)
    redirect_to backend_purchase_path(purchase)
  elsif parcels.all? { |p| p.outgoing? && p.third_id == parcel.third_id && p.invoiceable? }
    sale = Parcel.convert_to_sale(parcels)
    redirect_to backend_sale_path(sale)
  else
    notify_error(:all_parcels_must_be_invoiceable_and_of_same_nature_and_third)
    redirect_to(params[:redirect] || { action: :index })
  end
end

#newObject


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/controllers/backend/parcels_controller.rb', line 154

def new
  columns = Parcel.columns_definition.keys
  columns = columns.delete_if { |c| [:depth, :rgt, :lft, :id, :lock_version, :updated_at, :updater_id, :creator_id, :created_at].include?(c.to_sym) }
  values = columns.map(&:to_sym).uniq.reduce({}) do |hash, attr|
    hash[attr] = params[:"#{attr}"] unless attr.blank? || attr.to_s.match(/_attributes$/)
    hash
  end

  @parcel = Parcel.new(values)
  if params[:sale_id]
    sale = Sale.find(params[:sale_id])
    @parcel.recipient = sale.client
    @parcel.address = sale.address

    sale.items.each do |item|
      item.variant.take(item.quantity).each do |product, quantity|
        @parcel.items.new(source_product: product, quantity: quantity)
      end
    end
  end

  if params[:purchase_id]
    purchase = Purchase.find(params[:purchase_id])
    @parcel.sender = purchase.supplier
    @parcel.address = purchase.delivery_address

    purchase.items.each do |item|
      @parcel.items.new(quantity: item.quantity, variant: item.variant)
    end
  end

  t3e(@parcel.attributes.merge(nature: @parcel.nature.text))
end

#shipObject

Pre-fill delivery form with given parcels. Nothing else. Only a shortcut now.


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/controllers/backend/parcels_controller.rb', line 207

def ship
  parcels = find_parcels
  return unless parcels
  parcel = parcels.detect(&:shippable?)
  options = { parcel_ids: parcels.map(&:id) }
  if !parcel
    redirect_to(options.merge(controller: :deliveries, action: :new))
  elsif parcels.all? { |p| p.shippable? && (p.delivery_mode == parcel.delivery_mode) }
    options[:mode] = parcel.delivery_mode
    options[:transporter_id] = parcel.transporter_id if parcel.transporter
    redirect_to(options.merge(controller: :deliveries, action: :new))
  else
    notify_error(:some_parcels_are_not_shippable)
    redirect_to(params[:redirect] || { action: :index })
  end
end

#showObject

Displays details of one parcel selected with params


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/controllers/backend/parcels_controller.rb', line 134

def show
  return unless @parcel = find_and_check
  respond_with(@parcel, methods: [:all_item_prepared, :status, :items_quantity],
                        include: { address: { methods: [:mail_coordinate] },
                                   sale: {},
                                   purchase: {},
                                   recipient: {},
                                   sender: {},
                                   transporter: {},
                                   items: { methods: [:status, :prepared], include: [:product, :variant] } }) do |format|
    format.html do
      t3e @parcel.attributes.merge(nature: @parcel.nature.text)
    end
  end
end