Class: Backend::SalesController

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

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_theme

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseController

account_lettering_states_crit, accounts_range_crit, amount_range_crit, crit_params, journal_entries_states_crit, journal_letter_crit, journal_period_crit, journals_crit, search_conditions

Methods inherited from ApplicationController

#after_sign_in_path_for, #authorized?, human_action_name, #human_action_name, #session_controller?

Class Method Details

.sales_conditionsObject

management -> sales_conditions


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

def self.sales_conditions
  code = search_conditions(sales: %i[pretax_amount amount number initial_number description], entities: %i[number full_name]) + " ||= []\n"
  code << "if params[:period].present? && params[:period].to_s != 'all'\n"
  code << "  c[0] << ' AND #{Sale.table_name}.created_at::DATE BETWEEN ? AND ?'\n"
  code << "  if params[:period].to_s == 'interval'\n"
  code << "    c << params[:started_on]\n"
  code << "    c << params[:stopped_on]\n"
  code << "  else\n"
  code << "    interval = params[:period].to_s.split('_')\n"
  code << "    c << interval.first\n"
  code << "    c << interval.second\n"
  code << "  end\n"
  code << "end\n"
  code << "if params[:state].is_a?(Array) && !params[:state].empty?\n"
  code << "  c[0] << ' AND #{Sale.table_name}.state IN (?)'\n"
  code << "  c << params[:state]\n"
  code << "end\n "
  code << "if params[:nature].present? && params[:nature].to_s != 'all'\n"
  code << "  if params[:nature] == 'unpaid'\n"
  code << "    c[0] << ' AND NOT #{Affair.table_name}.closed'\n"
  code << "  end\n"
  code << "end\n"
  code << "if params[:responsible_id].to_i > 0\n"
  code << "  c[0] += \" AND \#{Sale.table_name}.responsible_id = ?\"\n"
  code << "  c << params[:responsible_id]\n"
  code << "end\n"
  code << "c\n "
  code.c
end

Instance Method Details

#abortObject


244
245
246
247
248
# File 'app/controllers/backend/sales_controller.rb', line 244

def abort
  return unless @sale = find_and_check
  @sale.abort
  redirect_to action: :show, id: @sale.id
end

#cancelObject


212
213
214
215
216
217
# File 'app/controllers/backend/sales_controller.rb', line 212

def cancel
  return unless @sale = find_and_check
  url = { controller: :sale_credits, action: :new, credited_sale_id: @sale.id }
  url[:redirect] = params[:redirect] if params[:redirect]
  redirect_to url
end

#confirmObject


219
220
221
222
223
# File 'app/controllers/backend/sales_controller.rb', line 219

def confirm
  return unless @sale = find_and_check
  @sale.confirm
  redirect_to action: :show, id: @sale.id
end

#contactsObject


225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'app/controllers/backend/sales_controller.rb', line 225

def contacts
  if request.xhr?
    address_id = nil
    client = if params[:selected] && address = EntityAddress.find_by(id: params[:selected])
               address.entity
             else
               Entity.find_by(id: params[:client_id])
             end
    if client
      session[:current_entity_id] = client.id
      address_id = (address ? address.id : client.default_mail_address.id)
    end
    @sale = Sale.find_by(id: params[:sale_id]) || Sale.new(address_id: address_id, delivery_address_id: address_id, invoice_address_id: address_id)
    render partial: 'addresses_form', locals: { client: client, object: @sale }
  else
    redirect_to action: :index
  end
end

#correctObject


250
251
252
253
254
# File 'app/controllers/backend/sales_controller.rb', line 250

def correct
  return unless @sale = find_and_check
  @sale.correct
  redirect_to action: :show, id: @sale.id
end

#duplicateObject


201
202
203
204
205
206
207
208
209
210
# File 'app/controllers/backend/sales_controller.rb', line 201

def duplicate
  return unless @sale = find_and_check
  unless @sale.duplicatable?
    notify_error :sale_is_not_duplicatable
    redirect_to params[:redirect] || { action: :index }
    return
  end
  copy = @sale.duplicate(responsible: current_user.person)
  redirect_to params[:redirect] || { action: :show, id: copy.id }
end

#indexObject

Displays the main page with the list of sales


79
80
81
82
83
84
85
# File 'app/controllers/backend/sales_controller.rb', line 79

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

#invoiceObject


256
257
258
259
260
261
262
263
264
265
266
# File 'app/controllers/backend/sales_controller.rb', line 256

def invoice
  return unless @sale = find_and_check
  if @sale.client..present?
    ActiveRecord::Base.transaction do
      raise ActiveRecord::Rollback unless @sale.invoice
    end
  else
    notify_error :error_client_account_empty
  end
  redirect_to action: :show, id: @sale.id
end

#newObject


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

def new
  unless nature = SaleNature.find_by(id: params[:nature_id]) || SaleNature.by_default
    notify_error :need_a_valid_sale_nature_to_start_new_sale
    redirect_to action: :index
    return
  end
  @sale = if params[:intervention_ids]
            Intervention.convert_to_sale(params[:intervention_ids])
          else
            Sale.new(nature: nature)
          end
  @sale.currency = @sale.nature.currency
  if client = Entity.find_by(id: @sale.client_id || params[:client_id] || params[:entity_id] || session[:current_entity_id])
    if client.default_mail_address
      cid = client.default_mail_address.id
      @sale.attributes = { address_id: cid, delivery_address_id: cid, invoice_address_id: cid }
    end
  end
  session[:current_entity_id] = (client ? client.id : nil)
  @sale.responsible = current_user.person
  @sale.client_id = session[:current_entity_id]
  @sale.letter_format = false
  @sale.function_title = :default_letter_function_title.tl
  @sale.introduction = :default_letter_introduction.tl
  @sale.conclusion = :default_letter_conclusion.tl
  @sale.items_attributes = params[:items_attributes] if params[:items_attributes]
  render locals: { with_continue: true }
end

#proposeObject


268
269
270
271
272
# File 'app/controllers/backend/sales_controller.rb', line 268

def propose
  return unless @sale = find_and_check
  @sale.propose
  redirect_to action: :show, id: @sale.id
end

#propose_and_invoiceObject


274
275
276
277
278
279
280
281
282
283
# File 'app/controllers/backend/sales_controller.rb', line 274

def propose_and_invoice
  return unless @sale = find_and_check
  ActiveRecord::Base.transaction do
    raise ActiveRecord::Rollback unless @sale.propose
    raise ActiveRecord::Rollback unless @sale.confirm
    # raise ActiveRecord::Rollback unless @sale.deliver
    raise ActiveRecord::Rollback unless @sale.invoice
  end
  redirect_to action: :show, id: @sale.id
end

#refuseObject


285
286
287
288
289
# File 'app/controllers/backend/sales_controller.rb', line 285

def refuse
  return unless @sale = find_and_check
  @sale.refuse
  redirect_to action: :show, id: @sale.id
end

#showObject

Displays details of one sale selected with params


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/controllers/backend/sales_controller.rb', line 149

def show
  return unless @sale = find_and_check
  @sale.other_deals
  respond_with(@sale, methods: %i[taxes_amount affair_closed client_number sales_conditions sales_mentions],
                      include: { address: { methods: [:mail_coordinate] },
                                 nature: { include: { payment_mode: { include: :cash } } },
                                 supplier: { methods: [:picture_path], include: { default_mail_address: { methods: [:mail_coordinate] }, websites: {}, emails: {}, mobiles: {} } },
                                 responsible: {},
                                 credits: {},
                                 parcels: { methods: %i[human_delivery_mode human_delivery_nature items_quantity], include: {
                                   address: {},
                                   sender: {},
                                   recipient: {}
                                 } },
                                 affair: { methods: [:balance], include: [incoming_payments: { include: :mode }] },
                                 invoice_address: { methods: [:mail_coordinate] },
                                 items: { methods: %i[taxes_amount tax_name tax_short_label], include: [:variant, shipment_items: { include: %i[product parcel] }] } }) do |format|
    format.html do
      t3e @sale.attributes, client: @sale.client.full_name, state: @sale.state_label, label: @sale.label
    end
  end
end