2009-01-10 19:36:58 +01:00
class Finance :: InvoicesController < ApplicationController
2009-01-08 16:33:27 +01:00
2016-02-17 21:07:35 +01:00
before_filter :find_invoice , only : [ :show , :edit , :update , :destroy ]
before_filter :ensure_can_edit , only : [ :edit , :update , :destroy ]
2009-01-08 16:33:27 +01:00
def index
2016-02-25 12:56:34 +01:00
@invoices = Invoice . includes ( :supplier , :deliveries , :orders ) . order ( 'date DESC' ) . page ( params [ :page ] ) . per ( @per_page )
2009-01-08 16:33:27 +01:00
end
def show
end
def new
2016-02-25 12:56:34 +01:00
@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 ]
2016-03-11 13:23:13 +01:00
fill_deliveries_and_orders_collection @invoice . id , @invoice . supplier_id
2009-01-08 16:33:27 +01:00
end
def edit
2016-03-11 13:23:13 +01:00
fill_deliveries_and_orders_collection @invoice . id , @invoice . supplier_id
end
def form_on_supplier_id_change
fill_deliveries_and_orders_collection params [ :invoice_id ] , params [ :supplier_id ]
render :layout = > false
end
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 )
2009-01-08 16:33:27 +01:00
end
def create
@invoice = Invoice . new ( params [ :invoice ] )
2016-02-17 16:11:01 +01:00
@invoice . created_by = current_user
2009-01-08 16:33:27 +01:00
2009-01-29 21:28:22 +01:00
if @invoice . save
2013-02-22 00:44:39 +01:00
flash [ :notice ] = I18n . t ( 'finance.create.notice' )
2016-02-25 12:56:34 +01:00
if @invoice . orders . count == 1
2009-01-29 21:28:22 +01:00
# Redirect to balancing page
2016-02-25 12:56:34 +01:00
redirect_to new_finance_order_url ( order_id : @invoice . orders . first . id )
2009-01-08 16:33:27 +01:00
else
2009-01-29 21:28:22 +01:00
redirect_to [ :finance , @invoice ]
2009-01-08 16:33:27 +01:00
end
2009-01-29 21:28:22 +01:00
else
render :action = > " new "
2009-01-08 16:33:27 +01:00
end
end
def update
2012-11-10 16:44:05 +01:00
if @invoice . update_attributes ( params [ :invoice ] )
2013-02-22 00:44:39 +01:00
redirect_to [ :finance , @invoice ] , notice : I18n . t ( 'finance.update.notice' )
2012-11-10 16:44:05 +01:00
else
render :edit
2009-01-08 16:33:27 +01:00
end
end
def destroy
@invoice . destroy
2012-11-10 16:44:05 +01:00
redirect_to finance_invoices_url
2009-01-08 16:33:27 +01:00
end
2016-02-17 21:07:35 +01:00
2016-05-06 15:04:58 +02:00
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
2016-02-17 21:07:35 +01:00
private
def find_invoice
@invoice = Invoice . find ( params [ :id ] )
end
# Returns true if @current_user can edit the invoice..
def ensure_can_edit
unless @invoice . user_can_edit? ( current_user )
deny_access
end
end
2009-01-08 16:33:27 +01:00
end