Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Invoices

  • Loading branch information...
commit 4a196da197883d6940353df479c5a3386ffa92e0 1 parent 6c06450
@ianmurrays authored
View
30 app/admin/invoices.rb
@@ -0,0 +1,30 @@
+ActiveAdmin.register Invoice do
+ form do |f|
+ f.inputs "Client" do
+ f.input :client
+ end
+
+ f.inputs "Items" do
+ f.has_many :items do |i|
+ i.input :quantity
+ i.input :description
+ i.input :amount
+ end
+ end
+
+ f.inputs "Options" do
+ f.input :code, :hint => "The invoice's code, should be incremental. Suggested code supplied.", :input_html => {:value => Invoice.suggest_code}
+ f.input :status, :collection => Invoice.status_collection, :as => :radio
+ f.input :due_date
+ f.input :tax, :input_html => { :style => "width: 20px", :value => "0"}, :hint => "This should be a percentage, from 0 to 100 (without the % sign)"
+ f.input :discount, :input_html => { :style => "width: 20px", :value => "0"}, :hint => "This should be a percentage, from 0 to 100 (without the % sign)"
+ end
+
+ f.inputs "Other Fields" do
+ f.input :terms, :input_html => { :rows => 4 }, :label => "Terms & Conditions"
+ f.input :notes, :input_html => { :rows => 4 }
+ end
+
+ f.buttons
+ end
+end
View
2  app/models/client.rb
@@ -1,4 +1,6 @@
class Client < ActiveRecord::Base
+ has_many :invoices, :dependent => :destroy
+
validates :name, :presence => true
validates :email, :format => { :with => /^(.+@.+\..+)?$/, :message => "is not a valid email address." }
end
View
31 app/models/invoice.rb
@@ -0,0 +1,31 @@
+class Invoice < ActiveRecord::Base
+ STATUS_DRAFT = 'draft'
+ STATUS_SENT = 'sent'
+ STATUS_PAID = 'paid'
+
+ belongs_to :client
+ has_many :items, :dependent => :destroy
+
+ accepts_nested_attributes_for :items
+
+ validates :code, :client_id, :presence => true
+ validates :status, :inclusion => { :in => [STATUS_PAID, STATUS_SENT, STATUS_DRAFT], :message => "You need to pick one status." }
+ validates :tax, :discount, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 100 }
+
+ def self.suggest_code
+ invoice = order('created_at desc').limit(1).first
+ if invoice
+ "INV-#{invoice.id + 1}"
+ else
+ "INV-1"
+ end
+ end
+
+ def self.status_collection
+ {
+ "Draft" => STATUS_DRAFT,
+ "Sent" => STATUS_SENT,
+ "Paid" => STATUS_PAID
+ }
+ end
+end
View
7 app/models/item.rb
@@ -0,0 +1,7 @@
+class Item < ActiveRecord::Base
+ belongs_to :invoice
+
+ validates :quantity, :presence => true, :numericality => { :integer => true }
+ validates :amount, :presence => true, :numericality => true
+ validates :description, :presence => true
+end
View
20 db/migrate/20110818220252_create_invoices.rb
@@ -0,0 +1,20 @@
+class CreateInvoices < ActiveRecord::Migration
+ def self.up
+ create_table :invoices do |t|
+ t.string :code
+ t.text :notes
+ t.text :terms
+ t.string :status
+ t.datetime :due_date
+ t.float :tax
+ t.float :discount
+ t.integer :client_id
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :invoices
+ end
+end
View
16 db/migrate/20110818222838_create_items.rb
@@ -0,0 +1,16 @@
+class CreateItems < ActiveRecord::Migration
+ def self.up
+ create_table :items do |t|
+ t.integer :quantity
+ t.float :amount
+ t.string :description
+ t.integer :invoice_id
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :items
+ end
+end
View
24 db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110818214436) do
+ActiveRecord::Schema.define(:version => 20110818222838) do
create_table "active_admin_comments", :force => true do |t|
t.integer "resource_id", :null => false
@@ -54,4 +54,26 @@
t.datetime "updated_at"
end
+ create_table "invoices", :force => true do |t|
+ t.string "code"
+ t.text "notes"
+ t.text "terms"
+ t.string "status"
+ t.datetime "due_date"
+ t.float "tax"
+ t.float "discount"
+ t.integer "client_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ create_table "items", :force => true do |t|
+ t.integer "quantity"
+ t.float "amount"
+ t.string "description"
+ t.integer "invoice_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
end
View
21 test/fixtures/invoices.yml
@@ -0,0 +1,21 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+ code: MyString
+ notes: MyText
+ terms: MyText
+ status: MyString
+ due_date: 2011-08-18 18:02:52
+ tax: 1.5
+ discount: 1.5
+ client_id: 1
+
+two:
+ code: MyString
+ notes: MyText
+ terms: MyText
+ status: MyString
+ due_date: 2011-08-18 18:02:52
+ tax: 1.5
+ discount: 1.5
+ client_id: 1
View
13 test/fixtures/items.yml
@@ -0,0 +1,13 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+ quantity: 1
+ amount: 1.5
+ description: MyString
+ invoice_id: 1
+
+two:
+ quantity: 1
+ amount: 1.5
+ description: MyString
+ invoice_id: 1
View
8 test/unit/invoice_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class InvoiceTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
8 test/unit/item_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class ItemTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.