Class: Finance::InvoicesController

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

Instance Method Summary collapse

Instance Method Details

#createObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/finance/invoices_controller.rb', line 49

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

#delete_attachmentObject



82
83
84
85
86
87
88
# File 'app/controllers/finance/invoices_controller.rb', line 82

def delete_attachment
  attachment = ActiveStorage::Attachment.find(params[:attachment_id])
  attachment.purge_later
  respond_to do |format|
    format.js
  end
end

#destroyObject



76
77
78
79
80
# File 'app/controllers/finance/invoices_controller.rb', line 76

def destroy
  @invoice.destroy

  redirect_to finance_invoices_url
end

#editObject



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

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

#fill_deliveries_and_orders_collection(invoice_id, supplier_id) ⇒ Object



42
43
44
45
46
47
# File 'app/controllers/finance/invoices_controller.rb', line 42

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(date: :desc).limit(25)
  @orders_collection = Order.where('invoice_id = ? OR (invoice_id IS NULL AND supplier_id = ?)', invoice_id,
                                   supplier_id).order(ends: :desc).limit(25)
end

#form_on_supplier_id_changeObject



37
38
39
40
# File 'app/controllers/finance/invoices_controller.rb', line 37

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
10
11
12
13
14
15
16
17
18
# File 'app/controllers/finance/invoices_controller.rb', line 7

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

  respond_to do |format|
    format.js
    format.html { render }
    format.csv do
      send_data InvoicesCsv.new(@invoices_all).to_csv, filename: 'invoices.csv', type: 'text/csv'
    end
  end
end

#newObject



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

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



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

def show; end

#unpaidObject



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

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

#updateObject



67
68
69
70
71
72
73
74
# File 'app/controllers/finance/invoices_controller.rb', line 67

def update
  if @invoice.update(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