Class: Finance::InvoicesController

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

Instance Method Summary collapse

Instance Method Details

#attachmentObject


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

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


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

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 && current_user.role_finance?
      # 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


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

def destroy
  @invoice.destroy

  redirect_to finance_invoices_url
end

#editObject


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

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

#fill_deliveries_and_orders_collection(invoice_id, supplier_id) ⇒ Object


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

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


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

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

#indexObject


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

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

#newObject


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

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


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

def show
end

#unpaidObject


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

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

#updateObject


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

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