invoice_keeper/app/controllers/invoices_controller.rb

75 lines
1.9 KiB
Ruby

# encoding: utf-8
class InvoicesController < ApplicationController
def index
@invoices = Invoice.where(user_id: current_user.id)
end
def show
@invoice = Invoice.find(params[:id])
respond_to do |format|
format.html {@inv_format = 'html'}
format.pdf do
render pdf: "Faktura_VAT_#{@invoice.id}", # Excluding ".pdf" extension.
disposition: 'attachment',
layout: 'pdf',
encoding: 'UTF-8',
viewport_size: '1280x1024'
end
end
end
def new
@invoice = Invoice.new(date: Date.today, date_of_payment: Date.today + 14.days)
inv = Invoice.where('date > ?', Date.new(Date.today.year,1,1)).last
if !inv.blank?
nr = inv.number
new_nr = (nr.split("/")[0].to_i + 1).to_s + "/" + nr.split("/")[1]
@invoice.number = new_nr
end
@invoice.invoice_products.build
@user_firms = current_user.user_firms
@customers = current_user.customers
end
def create
@invoice = Invoice.new(invoice_params)
@invoice.user_id = current_user.id
if @invoice.save
redirect_to @invoice
else
@user_firms = current_user.user_firms
@customers = current_user.customers
render :new
end
end
def edit
@invoice = Invoice.find(params[:id])
@user_firms = current_user.user_firms
@customers = current_user.customers
end
def update
@invoice = Invoice.find(params[:id])
if @invoice.update_attributes(invoice_params)
redirect_to @invoice
else
@user_firms = current_user.user_firms
@customers = current_user.customers
render :edit
end
end
def destroy
@invoice = Invoice.find(params[:id])
@invoice.destroy unless @invoice.blank?
redirect_to invoices_path
end
private
def invoice_params
params.require(:invoice).permit(:number, :user_firm_id, :customer_id, :date, :date_of_payment, invoice_products_attributes: [:id, :product_id, :qty, :netto_price, :_destroy])
end
end