Class: Backend::PurchasesController

Inherits:
BaseController show all
Defined in:
app/controllers/backend/purchases_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

.purchases_conditionsObject

params:

:q Text search
:state State search
:period Two dates with "_" separator

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

def self.purchases_conditions
  code = ''
  code = search_conditions(purchases: [:created_at, :pretax_amount, :amount, :number, :reference_number, :description, :state], entities: [:number, :full_name]) + " ||= []\n"
  code << "if params[:period].present? && params[:period].to_s != 'all'\n"
  code << "  c[0] << ' AND #{Purchase.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 #{Purchase.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 #{Purchase.table_name}.responsible_id = ?'\n"
  code << "  c << params[:responsible_id]\n"
  code << "end\n"
  code << "c\n "
  code.c
end

Instance Method Details

#abortObject


144
145
146
147
148
# File 'app/controllers/backend/purchases_controller.rb', line 144

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

#confirmObject


150
151
152
153
154
# File 'app/controllers/backend/purchases_controller.rb', line 150

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

#correctObject


156
157
158
159
160
# File 'app/controllers/backend/purchases_controller.rb', line 156

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

#generate_parcelObject


139
140
141
142
# File 'app/controllers/backend/purchases_controller.rb', line 139

def generate_parcel
  return unless @purchase = find_and_check
  redirect_to controller: :parcels, action: :new, purchase_id: @purchase.id, nature: :incoming
end

#invoiceObject


162
163
164
165
166
167
168
# File 'app/controllers/backend/purchases_controller.rb', line 162

def invoice
  return unless @purchase = find_and_check
  ActiveRecord::Base.transaction do
    raise ActiveRecord::Rollback unless @purchase.invoice(params[:invoiced_at])
  end
  redirect_to action: :show, id: @purchase.id
end

#newObject


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/controllers/backend/purchases_controller.rb', line 122

def new
  unless nature = PurchaseNature.find_by(id: params[:nature_id]) || PurchaseNature.by_default
    notify_error :need_a_valid_purchase_nature_to_start_new_purchase
    redirect_to action: :index
    return
  end
  @purchase = Purchase.new(nature: nature)
  @purchase.currency = @purchase.nature.currency
  @purchase.responsible = current_user
  @purchase.planned_at = Time.zone.now
  @purchase.invoiced_at = Time.zone.now
  @purchase.supplier_id = params[:supplier_id] if params[:supplier_id]
  if address = Entity.of_company.default_mail_address
    @purchase.delivery_address = address
  end
end

#proposeObject


170
171
172
173
174
# File 'app/controllers/backend/purchases_controller.rb', line 170

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

#propose_and_invoiceObject


176
177
178
179
180
181
182
183
184
185
# File 'app/controllers/backend/purchases_controller.rb', line 176

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

#refuseObject


187
188
189
190
191
# File 'app/controllers/backend/purchases_controller.rb', line 187

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

#showObject

Displays details of one purchase selected with params


108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/backend/purchases_controller.rb', line 108

def show
  return unless @purchase = find_and_check
  respond_with(@purchase, methods: [:taxes_amount, :affair_closed],
                          include: { delivery_address: { methods: [:mail_coordinate] },
                                     supplier: { methods: [:picture_path], include: { default_mail_address: { methods: [:mail_coordinate] } } },
                                     parcels: { include: :items },
                                     affair: { methods: [:balance], include: [outgoing_payments: { include: :mode }] },
                                     items: { methods: [:taxes_amount, :tax_name, :tax_short_label], include: [:variant] } }) do |format|
    format.html do
      t3e @purchase.attributes, supplier: @purchase.supplier.full_name, state: @purchase.state_label, label: @purchase.label
    end
  end
end