Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class InvoicesController < ApplicationController
- before_filter :authorize, :load_invoice
- layout "main", :except => 'print'
- require 'params_filter'
- include ParamsFilter
- verify :method => :post, :only => [ :destroy, :create, :update ],
- :redirect_to => { :action => :list }
- private
- def load_invoice
- @invoice = Invoice.find( params[:id] ) if params[:id]
- rescue ActiveRecord::RecordNotFound
- flash[:notice] = "Invoice not found"
- redirect_to :action => :index
- end
- public
- def index
- list
- render :action => 'list'
- end
- def list
- @invoices = Invoice.paginate(:per_page => 10, :page => params[:page] || 1, :order => 'created_at DESC')
- params[:search] ||= {}
- end
- def search
- prepare_search_dates if params[:search]
- params[:search] ||= {}
- @invoices = Invoice.search(params[:search])
- render :partial => '/invoices/list'
- end
- def print
- @invoice = Invoice.find(params[:id])
- send_data InvoiceDrawer.draw(@invoice), :filename => 'invoice.pdf', :type => 'application/pdf', :disposition => 'inline'
- end
- def show
- assert_params_must_have :id
- end
- def new
- @invoice = Invoice.new
- end
- def create
- @invoice = Invoice.new(params[:invoice])
- @invoice.user_id = @current_user.id
- @invoice.created_at = Time.now
- if @invoice.save
- flash[:notice] = 'Invoice was successfully created.'
- redirect_to :action => 'list'
- else
- render :action => 'new'
- end
- end
- def edit
- assert_params_must_have :id
- end
- def update
- assert_params_must_have :id
- @invoice.issued_at = Time.now if !params[:invoice].nil? && params[:invoice][:is_issued]
- @invoice.user_id = @current_user.id
- if @invoice.update_attributes(params[:invoice])
- flash[:notice] = 'Invoice was successfully updated.'
- redirect_to :action => 'show', :id => @invoice
- else
- render :action => 'edit'
- end
- end
- def issue
- assert_params_must_have :id
- unless @invoice.activities.empty?
- if @invoice.update_attributes({'is_issued' => 1, 'issued_at' => Time.now})
- flash[:notice] = 'Invoice has been issued.'
- else
- flash[:notice] = 'Error with issuing invoice.'
- end
- else
- flash[:notice] = 'No activities on this invoice! Cannot be issued!'
- end
- redirect_to :action => :show, :id => @invoice
- end
- def add_activities
- @activities = Activity.find(:all, :conditions => ["invoice_id IS NULL AND id IN (?)",params[:activity_id]])
- @success = true
- @activities.each do |activity|
- @success = activity.update_attribute('invoice_id',params[:invoice_id])
- end
- if @success
- flash[:notice] = 'Activities has been added to invoice.'
- else
- flash[:notice] = 'Error with adding activities to an invoice.'
- end
- redirect_to :action => 'show', :id => params[:invoice_id]
- end
- def remove_activities
- assert_params_must_have :id
- @activities = Activity.find(:all, :conditions => ["id IN (?)",params[:activity_id]])
- @success = true
- @activities.each do |activity|
- @success = activity.update_attribute('invoice_id', nil)
- end
- if @success
- flash[:notice] = 'Activities has been removed from invoice.'
- else
- flash[:notice] = 'Error with removing activities from invoice.'
- end
- redirect_to :action => 'show', :id => params[:id]
- end
- def add_new
- @invoice = Invoice.new
- @invoice.client_id = params[:client_id]
- render :partial => '/invoices/add_new'
- end
- def create_new
- @invoice = Invoice.new(params[:invoice])
- @invoice.user_id = @current_user.id
- @success = false
- if @invoice.save
- @success = true
- end
- @invoices = Invoice.find(:all, :conditions => ["client_id = ? AND is_issued=0", @invoice.client_id])
- end
- end
Add Comment
Please, Sign In to add comment