# encoding: utf-8 class InvoicesController < ApplicationController def index @invoices = Invoice.where(user_id: current_user.id).order('created_at DESC') end def show @invoice = Invoice.find(params[:id]) @vats = Vat.all 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', page_size: 'A4', print_media_type: true, render pdf: "Faktura_VAT_#{@invoice.id}", # Excluding ".pdf" extension. disposition: 'attachment', layout: 'pdf', encoding: 'UTF-8', page_size: 'A4', margin: { top: 8, bottom: 8, left: 8, right: 8 }, viewport_size: '1336x1024' 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 unless 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