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

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

.sales_conditionsObject

management -> sales_conditions


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

def self.sales_conditions
  code = ''
  code = search_conditions(sales: [:pretax_amount, :amount, :number, :initial_number, :description], entities: [:number, :full_name]) + " ||= []\n"
  code << "if params[:period].present? && params[:period].to_s != 'all'\n"
  code << "  c[0] << ' AND #{Sale.table_name}.invoiced_at 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


239
240
241
242
243
# File 'app/controllers/backend/sales_controller.rb', line 239

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

#cancelObject


206
207
208
209
210
211
# File 'app/controllers/backend/sales_controller.rb', line 206

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


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

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

#contactsObject


219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'app/controllers/backend/sales_controller.rb', line 219

def contacts
  if request.xhr?
    client = nil
    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


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

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

#duplicateObject


190
191
192
193
194
195
196
197
198
199
# File 'app/controllers/backend/sales_controller.rb', line 190

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

#generate_parcelObject


201
202
203
204
# File 'app/controllers/backend/sales_controller.rb', line 201

def generate_parcel
  return unless @sale = find_and_check
  redirect_to controller: :parcels, action: :new, sale_id: @sale.id, nature: :outgoing
end

#indexObject

Displays the main page with the list of sales


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

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

#invoiceObject


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

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

#newObject


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/controllers/backend/sales_controller.rb', line 167

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 = Sale.new(nature: nature)
  @sale.currency = @sale.nature.currency
  if client = Entity.find_by_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
end

#proposeObject


259
260
261
262
263
# File 'app/controllers/backend/sales_controller.rb', line 259

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

#propose_and_invoiceObject


265
266
267
268
269
270
271
272
273
274
# File 'app/controllers/backend/sales_controller.rb', line 265

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


276
277
278
279
280
# File 'app/controllers/backend/sales_controller.rb', line 276

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


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/controllers/backend/sales_controller.rb', line 144

def show
  return unless @sale = find_and_check
  @sale.other_deals
  respond_with(@sale, methods: [: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: [: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: [:taxes_amount, :tax_name, :tax_short_label], include: [:variant, parcel_items: { include: [: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