Skip to content

Instantly share code, notes, and snippets.

@dombarnes
Created October 19, 2011 10:11
Show Gist options
  • Save dombarnes/1297892 to your computer and use it in GitHub Desktop.
Save dombarnes/1297892 to your computer and use it in GitHub Desktop.
Invoices App
class Invoice < ActiveRecord::Base
attr_accessible :invoice_date, :company_id, :purchase_order, :vat_rate, :date_sent, :paid, :date_paid;
validate :invoice_date, :company_id, :vat_rate, :date_sent, :presence => true
validate :status, :inclusion => { :in => ['draft', 'sent', 'paid'] }
belongs_to :company
has_many :invoice_items, :dependent => :destroy
accepts_nested_attributes_for :invoice_items, :allow_destroy => true
scope :outstanding, where(:paid => "false")
scope :overdue, lambda { where('date_sent < ?', Date.today - 30.days)}
scope :recently_paid, lambda { where('date_paid < ?', Date.today - 14.days)}
end
class Invoice::InvoiceItems < ActiveRecord::Base
attr_accessible :quantity, :unit_type, :unit_price, :details, :vat_rate
belongs_to :invoice
scope :all, order('due_date ASC')
validate :description, :quantity, :unit_cost, :presence => true
validate :unit_price, :numericality => { :greater_than_or_equal_to => 0 }
validate :quantity, :numericality => { :greater_than => 0 }
# validate :discount, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 100}
validate :invoice_id, :presence => true, :unless => :nested
attr_accessor :nested
end
class Invoices::InvoiceItemsController < ApplicationController
def show
@item = InvoiceItem.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @item }
end
end
end
class InvoicesController < ApplicationController
def show
@invoice = Invoice.find(params[:id])
@items = @invoice.invoice_items.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.pdf { render :layout => false }
format.json { render json: @invoice }
end
end
end
Internalapp::Application.routes.draw do
resources :invoices do
resource :invoice_items
end
resources :notes
resources :contracts
resources :tasks
resources :people
resource :user, :as => 'account'
resources :adhoc_supports
resources :users
resources :roles
# resources :admin
resources :companies
resources :mac_quotes
resources :mac_values
resources :ios_quotes
resources :ios_values
resources :users, :user_sessions
resources :password_resets
get "user_sessions/new"
root :to => "home#index"
get "home/index"
match '/', :to => 'home#index'
match 'signup' => 'users#new', :as => :signup
match 'login' => 'user_sessions#new', :as => :login
match 'logout' => 'user_sessions#destroy', :as => :logout
match 'activate(/:activation_code)' => 'users#activate', :as => :activate_account
match 'send_activation(/:user_id)' => 'users#send_activation', :as => :send_activation
match '/admin', :to => 'admin#index'
match '/dashboard', :to => 'home#dashboard'
match '/adhocsupport', :to => 'adhoc_supports#index'
match '/people', :to => 'people#index'
match '/companies', :to => 'companies#index'
match '/quotes', :to => 'home#quotes'
match '/help', :to =>'home#help'
match '/admin/roles', :to => 'admin#roles#index'
match '/profile', :to => 'users#show'
end
<p id="notice"><%= notice %></p>
<p>
<b>Invoice date:</b>
<%= @invoice.invoice_date %>
</p>
<p>
<b>Purchase order:</b>
<%= @invoice.purchase_order %>
</p>
<p>
<b>Date sent:</b>
<%= @invoice.date_sent %>
</p>
<p>
<b>Paid:</b>
<%= @invoice.paid %>
</p>
<p>
<b>Date paid:</b>
<%= @invoice.date_paid %>
</p>
<p>
<b>Sub total:</b>
<%= @invoice.sub_total %>
</p>
<p>
<b>Vat rate:</b>
<%= @invoice.vat_rate %>
</p>
<p>
<b>Vat total:</b>
<%= @invoice.vat_total %>
</p>
<p>
<b>Discount total:</b>
<%= @invoice.discount_total %>
</p>
<p>
<b>Total:</b>
<%= @invoice.total %>
</p>
<p>
<b>Company:</b>
<%= @invoice.company_id %>
</p>
<%= link_to 'New invoice item', new_invoice_invoice_items_path(@invoice) %>
<table id="invoiceitems">
<tr>
<th>Qty</th>
<th>Price</th>
<th>Description</th>
<th>VAT Rate</th>
<th>Total</th>
</tr>
<% @items.each do |item| %>
<tr>
<td><%= item.quantity %></td>
<td><%= item.unit_type %></td>
<td><%= item.unit_price %></td>
<td><%= item.detail %></td>
<td><%= item.vat_rate %></td>
<td><%= item.total_cost %></td>
</tr>
<% end %>
</table>
<%= link_to 'Edit', edit_invoice_path(@invoice) %> |
<%= link_to 'Back', invoices_path %>
@twss
Copy link

twss commented Oct 19, 2011

show.html.erb, line 68, should that read

<% @invoice.items.each do |item| %>

and make invoice.rb, line 7

has_many :items, :class_name => :invoice_items, :dependent => :destroy

Also, is that a stray semi-colon at the end of invoice.rb, line 2?

@twss
Copy link

twss commented Oct 19, 2011

Also, your InvoiceItems class should really be InvoiceItem it will cause you big problems!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment