Class: Finance::InvoicesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/finance/invoices_controller.rb

Instance Method Summary collapse

Instance Method Details

#attachmentObject


71
72
73
74
75
76
# File 'app/controllers/finance/invoices_controller.rb', line 71

def attachment
  @invoice = Invoice.find(params[:invoice_id])
  type = MIME::Types[@invoice.attachment_mime].first
  filename = "invoice_#{@invoice.id}_attachment.#{type.preferred_extension}"
  send_data(@invoice.attachment_data, :filename => filename, :type => type)
end

#createObject


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/finance/invoices_controller.rb', line 38

def create
  @invoice = Invoice.new(params[:invoice])
  @invoice.created_by = current_user

  if @invoice.save
    flash[:notice] = I18n.t('finance.create.notice')
    if @invoice.orders.count == 1
      # Redirect to balancing page
      redirect_to new_finance_order_url(order_id: @invoice.orders.first.id)
    else
      redirect_to [:finance, @invoice]
    end
  else
    fill_deliveries_and_orders_collection @invoice.id, @invoice.supplier_id
    render :action => "new"
  end
end

#destroyObject


65
66
67
68
69
# File 'app/controllers/finance/invoices_controller.rb', line 65

def destroy
  @invoice.destroy

  redirect_to finance_invoices_url
end

#editObject


24
25
26
# File 'app/controllers/finance/invoices_controller.rb', line 24

def edit
  fill_deliveries_and_orders_collection @invoice.id, @invoice.supplier_id
end

#fill_deliveries_and_orders_collection(invoice_id, supplier_id) ⇒ Object


33
34
35
36
# File 'app/controllers/finance/invoices_controller.rb', line 33

def fill_deliveries_and_orders_collection(invoice_id, supplier_id)
  @deliveries_collection = Delivery.where('invoice_id = ? OR (invoice_id IS NULL AND supplier_id = ?)', invoice_id, supplier_id).order(:delivered_on)
  @orders_collection = Order.where('invoice_id = ? OR (invoice_id IS NULL AND supplier_id = ? AND state = ?)', invoice_id, supplier_id, 'finished').order(:ends)
end

#form_on_supplier_id_changeObject


28
29
30
31
# File 'app/controllers/finance/invoices_controller.rb', line 28

def form_on_supplier_id_change
  fill_deliveries_and_orders_collection params[:invoice_id], params[:supplier_id]
  render :layout => false
end

#indexObject


6
7
8
# File 'app/controllers/finance/invoices_controller.rb', line 6

def index
  @invoices = Invoice.includes(:supplier, :deliveries, :orders).order('date DESC').page(params[:page]).per(@per_page)
end

#newObject


17
18
19
20
21
22
# File 'app/controllers/finance/invoices_controller.rb', line 17

def new
  @invoice = Invoice.new :supplier_id => params[:supplier_id]
  @invoice.deliveries << Delivery.find_by_id(params[:delivery_id]) if params[:delivery_id]
  @invoice.orders << Order.find_by_id(params[:order_id]) if params[:order_id]
  fill_deliveries_and_orders_collection @invoice.id, @invoice.supplier_id
end

#showObject


14
15
# File 'app/controllers/finance/invoices_controller.rb', line 14

def show
end

#unpaidObject


10
11
12
# File 'app/controllers/finance/invoices_controller.rb', line 10

def unpaid
  @suppliers = Supplier.includes(:invoices).where('invoices.paid_on IS NULL').references(:invoices)
end

#updateObject


56
57
58
59
60
61
62
63
# File 'app/controllers/finance/invoices_controller.rb', line 56

def update
  if @invoice.update_attributes(params[:invoice])
    redirect_to [:finance, @invoice], notice: I18n.t('finance.update.notice')
  else
    fill_deliveries_and_orders_collection @invoice.id, @invoice.supplier_id
    render :edit
  end
end