Browse Source

Adding transactions

denney-disable-on-select
Jason Denney 12 years ago
parent
commit
ec19c8461d
  1. 6
      Gemfile.lock
  2. 23
      app/components/app_tab_panel.rb
  3. 1
      app/components/bike_logs.rb
  4. 1
      app/components/bikes_border.rb
  5. 21
      app/components/customers.rb
  6. 11
      app/components/customers/javascripts/init_component.js
  7. 67
      app/components/transaction_logs.rb
  8. 62
      app/components/transactions.rb
  9. 30
      app/components/transactions_border.rb
  10. 14
      app/components/transactions_border/javascripts/init_component.js
  11. 1
      app/components/user_logs.rb
  12. 40
      app/components/user_transactions.rb
  13. 27
      app/components/user_transactions_border.rb
  14. 14
      app/components/user_transactions_border/javascripts/init_component.js
  15. 14
      app/components/users.rb
  16. 11
      app/components/users/javascripts/init_component.js
  17. 10
      app/components/users_and_customers_accordian.rb
  18. 2
      app/components/users_and_profiles_border.rb
  19. 3
      app/models/acts_as_loggable/transaction_action.rb
  20. 2
      app/models/bike.rb
  21. 22
      app/models/customer.rb
  22. 19
      app/models/transaction.rb
  23. 1
      app/models/user.rb
  24. 6
      db/migrate/20121205043759_create_transactions.rb
  25. 19
      db/migrate/20130120142249_create_customers.rb
  26. 24
      db/schema.rb

6
Gemfile.lock

@ -1,8 +1,8 @@
GIT GIT
remote: https://github.com/spacemunkay/acts_as_loggable.git remote: git@github.com:spacemunkay/acts_as_loggable.git
revision: fd2c276abad00567a294a1a841dce9861e240c82 revision: 6033fe5a44878bb374f73fe473fed9a3396ddc74
specs: specs:
acts_as_loggable (0.0.6) acts_as_loggable (0.0.7)
activerecord (>= 3.0) activerecord (>= 3.0)
activesupport (~> 3.0) activesupport (~> 3.0)

23
app/components/app_tab_panel.rb

@ -10,17 +10,33 @@ class AppTabPanel < Netzke::Basepack::TabPanel
#all users #all users
# (had to use hash for borders to get the title to display properly) # (had to use hash for borders to get the title to display properly)
@@app_tab_panel_items = [ :bikes_border, {layout: :fit, wrappedComponent: :brands_and_models_border, title: "Brands/Models"}] @@app_tab_panel_items = [ :bikes_border,
{ layout: :fit,
wrappedComponent: :brands_and_models_border,
title: "Brands/Models"}
]
#for users #for users
if controller.current_user.user? if controller.current_user.user?
# (had to use hash for borders to get the title to display properly) # (had to use hash for borders to get the title to display properly)
@@app_tab_panel_items.concat [{ layout: :fit, wrappedComponent: :user_profile_border, title: "Profile"}] @@app_tab_panel_items.concat [{ layout: :fit,
wrappedComponent: :user_profile_border,
title: "Profile"},
{ layout: :fit,
wrappedComponent: :user_transactions_border,
title: "Transactions"}
]
end end
#for admins #for admins
if controller.current_user.admin? if controller.current_user.admin?
# (had to use hash for borders to get the title to display properly) # (had to use hash for borders to get the title to display properly)
@@app_tab_panel_items.concat [{ layout: :fit, wrappedComponent: :users_and_profiles_border, title: "Users/Profiles"}, :logs] @@app_tab_panel_items.concat [{ layout: :fit,
wrappedComponent: :users_and_profiles_border,
title: "Users/Profiles"},
{ layout: :fit,
wrappedComponent: :transactions_border,
title: "Transactions"},
:logs]
end end
@@app_tab_panel_items.each do |item| @@app_tab_panel_items.each do |item|
@ -31,7 +47,6 @@ class AppTabPanel < Netzke::Basepack::TabPanel
end end
end end
c.active_tab = 0
c.prevent_header = true c.prevent_header = true
c.tbar = [:sign_out] c.tbar = [:sign_out]
c.items = @@app_tab_panel_items c.items = @@app_tab_panel_items

1
app/components/bike_logs.rb

@ -44,6 +44,7 @@ class BikeLogs < Netzke::Basepack::Grid
{ :name => :start_date}, { :name => :start_date},
{ :name => :end_date}, { :name => :end_date},
{ :name => :description}, { :name => :description},
#had to hack acts_as_loggable/log.rb to get this to work
{ :name => :bike_action__action, :field_label => 'Action'} { :name => :bike_action__action, :field_label => 'Action'}
] ]
end end

1
app/components/bikes_border.rb

@ -6,6 +6,7 @@ class BikesBorder < Netzke::Base
def configure(c) def configure(c)
super super
c.header = false
c.title = "Bikes" c.title = "Bikes"
c.items = [ c.items = [
{ netzke_component: :bikes, region: :center, split: true }, { netzke_component: :bikes, region: :center, split: true },

21
app/components/customers.rb

@ -0,0 +1,21 @@
class Customers < Netzke::Basepack::Grid
def configure(c)
c.model = "Customer"
end
#override with nil to remove actions
def default_bbar
[ :apply, :add_in_form, :search ]
end
#needed for transactions customer selection
js_configure do |c|
c.mixin :init_component
end
#needed for transactions customer selection
endpoint :select_customer do |params, this|
session[:selected_customer_id] = params[:customer_id]
session[:selected_customer_type] = params[:customer_type]
end
end

11
app/components/customers/javascripts/init_component.js

@ -0,0 +1,11 @@
{
initComponent: function(){
// calling superclass's initComponent
this.callParent();
this.getView().on('itemclick', function(view, record){
// The beauty of using Ext.Direct: calling 3 endpoints in a row, which results in a single call to the server!
console.log("user: " + record.get('id') );
this.selectCustomer({customer_id: record.get('id'), customer_type: 'Customer'});
}, this);
}
}

67
app/components/transaction_logs.rb

@ -0,0 +1,67 @@
class TransactionLogs < Netzke::Basepack::Grid
def configure(c)
super
c.model = "ActsAsLoggable::Log"
c.title = "Transaction Payments"
c.data_store = {auto_load: false}
c.scope = lambda { |rel| rel.where(:loggable_type => 'Transaction',:loggable_id => session[:selected_transaction_id]);}
c.strong_default_attrs = {
:loggable_type => 'Transaction',
:loggable_id => session[:selected_transaction_id],
:log_action_type => 'ActsAsLoggable::TransactionAction',
:logger_type => 'User',
:logger_id => controller.current_user.id,
:start_date => Time.now.to_formatted_s(:db),
:end_date => Time.now.to_formatted_s(:db)
}
c.columns = [
{ :name => :start_date, :format => "g:ia - D, M j - Y", :width => 165, :default_value => Time.now.to_formatted_s(:db), :text => 'Date' },
{ :name => :description, :text => "Amount"} ,
{ :name => :transaction_action__action, :text => 'Method'},
{ :name => :logged_by, :getter => lambda{ |rec|
user = User.find_by_id(rec.logger_id)
user.nil? ? "" : "#{user.first_name} #{user.last_name}"
},
:text => "Processed by"
}
]
if controller.current_user.user?
c.prohibit_update = true
c.prohibit_create = true
c.prohibit_delete = true
end
end
def default_fields_for_forms
customer = nil
item = nil
if session[:selected_transaction_id]
trans = Transaction.find_by_id(session[:selected_transaction_id])
customer = trans.customer
item = trans.item
end
customer = "No Customer Selected" if customer.nil?
item = "No Item Selected" if item.nil?
[
{ :no_binding => true, :xtype => 'displayfield', :fieldLabel => "Payment from:", :value => "#{customer.to_s}"},
{ :no_binding => true, :xtype => 'displayfield', :fieldLabel => "Payment for:", :value => "#{item.to_s}"},
{ :name => :description, :xtype => 'numberfield', :field_label => 'Amount'},
#had to hack acts_as_loggable/log.rb to get this to work
{ :name => :transaction_action__action, :field_label => 'Payment Method'}
]
end
#override with nil to remove actions
def default_bbar
bbar = [ :search ]
bbar.concat [ :apply, :add_in_form ] if not controller.current_user.user?
bbar
end
end

62
app/components/transactions.rb

@ -0,0 +1,62 @@
class Transactions < Netzke::Basepack::Grid
def configure(c)
super
c.model = "Transaction"
c.strong_default_attrs = {
:vendor_id => controller.current_user.id,
:customer_id => session[:selected_customer_id],
:customer_type => session[:selected_customer_type]
}
c.columns = [
:amount,
:item,
{ :name => :bike__serial_number},
{ :name => :vendor, :getter => lambda { |rec|
user = rec.vendor
user.nil? ? "" : "#{user.first_name} #{user.last_name}"
}
},
{ :name => :customer, :getter => lambda { |rec|
user = rec.customer
user.nil? ? "" : "#{user.first_name} #{user.last_name}"
}
},
:created_at
]
end
def default_fields_for_forms
bike_store = Bike.all.map { |b| [b.id, b.serial_number] }
user_store = User.all.map { |u| [u.id, u.to_s] }
customer = nil
if session[:selected_customer_type] == "User"
customer = User.find_by_id(session[:selected_customer_id])
elsif session[:selected_customer_type] == "Customer"
customer = Customer.find_by_id(session[:selected_customer_id])
end
customer = "No User Selected" if customer.nil?
[
{ :no_binding => true, :xtype => 'displayfield', :fieldLabel => "Creating Transaction for:", :value => "#{customer.to_s}"},
:amount,
:item,
{ :name => :for_bike, :checkboxName => :bike_item, :inputValue => true, :title => "Selling a bike?",
:xtype => 'fieldset', :checkboxToggle => true, :collapsed => true, :items => [
{:xtype => 'combo', :no_binding => true, :name => :bike_id, :title => 'Bike', :fieldLabel => 'Bike', :store => bike_store}
]
}
]
end
#override with nil to remove actions
def default_bbar
[ :apply, :add_in_form, :search ]
end
=begin
#needed for transaction selection
js_configure do |c|
c.mixin :init_component
end
=end
end

30
app/components/transactions_border.rb

@ -0,0 +1,30 @@
class TransactionsBorder < Netzke::Base
# Remember regions collapse state and size
include Netzke::Basepack::ItemPersistence
component :transactions
component :transaction_logs
#users and customers components are required for the transactions form
component :users_and_customers_accordian
def configure(c)
super
c.header = false
c.title = "Transactions"
c.items = [
{ netzke_component: :transactions, region: :center, height: 300, split: true },
{ netzke_component: :transaction_logs, region: :east, width: 300, split: true },
{ netzke_component: :users_and_customers_accordian, region: :south, height: 300, split: true }
]
end
js_configure do |c|
c.layout = :border
c.border = false
c.mixin :init_component
end
endpoint :select_transaction do |params, this|
session[:selected_transaction_id] = params[:transaction_id]
end
end

14
app/components/transactions_border/javascripts/init_component.js

@ -0,0 +1,14 @@
{
initComponent: function(){
// calling superclass's initComponent
this.callParent();
// setting the 'rowclick' event
var view = this.getComponent('transactions').getView();
view.on('itemclick', function(view, record){
// The beauty of using Ext.Direct: calling 3 endpoints in a row, which results in a single call to the server!
this.selectTransaction({transaction_id: record.get('id')});
this.getComponent('transaction_logs').getStore().load();
}, this);
}
}

1
app/components/user_logs.rb

@ -58,6 +58,7 @@ class UserLogs < Netzke::Basepack::Grid
{ :name => :start_date}, { :name => :start_date},
{ :name => :end_date}, { :name => :end_date},
{ :name => :description}, { :name => :description},
#had to hack acts_as_loggable/log.rb to get this to work
{ :name => :user_action__action, :field_label => 'Action', :value => action_id}, { :name => :user_action__action, :field_label => 'Action', :value => action_id},
{ :name => :for_bike, :checkboxName => :copy_log, :inputValue => true, :title => "Copy description to a Bike's History?", :xtype => 'fieldset', :checkboxToggle => true, :collapsed => true, :items => [ { :name => :for_bike, :checkboxName => :copy_log, :inputValue => true, :title => "Copy description to a Bike's History?", :xtype => 'fieldset', :checkboxToggle => true, :collapsed => true, :items => [
{:xtype => 'combo', :no_binding => true, :name => :copy_id, :title => 'Bike', :fieldLabel => 'Bike', :store => bike_store, :value => bike_id} {:xtype => 'combo', :no_binding => true, :name => :copy_id, :title => 'Bike', :fieldLabel => 'Bike', :store => bike_store, :value => bike_id}

40
app/components/user_transactions.rb

@ -0,0 +1,40 @@
class UserTransactions < Netzke::Basepack::Grid
def configure(c)
super
c.model = "Transaction"
c.title = "Transactions"
c.scope = lambda { |rel| rel.where(:customer_id => controller.current_user.id, :customer_type => 'User');}
c.data_store = { auto_load: true }
c.columns = [
:amount,
:item,
{ :name => :bike__serial_number},
{ :name => :vendor, :getter => lambda { |rec|
user = rec.vendor
user.nil? ? "" : "#{user.first_name} #{user.last_name}"
}
},
{ :name => :customer, :getter => lambda { |rec|
user = rec.customer
user.nil? ? "" : "#{user.first_name} #{user.last_name}"
}
},
:created_at
]
if controller.current_user.user?
c.prohibit_update = true
c.prohibit_create = true
c.prohibit_delete = true
end
end
#override with nil to remove actions
def default_bbar
bbar = [ :search ]
bbar.concat [ :apply, :add_in_form ] if not controller.current_user.user?
bbar
end
end

27
app/components/user_transactions_border.rb

@ -0,0 +1,27 @@
class UserTransactionsBorder < Netzke::Base
# Remember regions collapse state and size
include Netzke::Basepack::ItemPersistence
component :user_transactions
component :transaction_logs
def configure(c)
super
c.header = false
c.title = "Transactions"
c.items = [
{ netzke_component: :user_transactions, region: :center, height: 300, split: true },
{ netzke_component: :transaction_logs, region: :south, height: 300, split: true }
]
end
js_configure do |c|
c.layout = :border
c.border = false
c.mixin :init_component
end
endpoint :select_transaction do |params, this|
session[:selected_transaction_id] = params[:transaction_id]
end
end

14
app/components/user_transactions_border/javascripts/init_component.js

@ -0,0 +1,14 @@
{
initComponent: function(){
// calling superclass's initComponent
this.callParent();
// setting the 'rowclick' event
var view = this.getComponent('user_transactions').getView();
view.on('itemclick', function(view, record){
// The beauty of using Ext.Direct: calling 3 endpoints in a row, which results in a single call to the server!
this.selectTransaction({transaction_id: record.get('id')});
this.getComponent('transaction_logs').getStore().load();
}, this);
}
}

14
app/components/users.rb

@ -1,6 +1,7 @@
class Users < Netzke::Basepack::Grid class Users < Netzke::Basepack::Grid
def configure(c) def configure(c)
super super
c.header = false
c.model = "User" c.model = "User"
c.columns = [ c.columns = [
@ -15,6 +16,17 @@ class Users < Netzke::Basepack::Grid
#override with nil to remove actions #override with nil to remove actions
def default_bbar def default_bbar
[ :apply, :add_in_form ] [ :apply, :add_in_form, :search ]
end
#needed for transactions customer selection
js_configure do |c|
c.mixin :init_component
end
#needed for transactions customer selection
endpoint :select_customer do |params, this|
session[:selected_customer_id] = params[:customer_id]
session[:selected_customer_type] = params[:customer_type]
end end
end end

11
app/components/users/javascripts/init_component.js

@ -0,0 +1,11 @@
{
initComponent: function(){
// calling superclass's initComponent
this.callParent();
this.getView().on('itemclick', function(view, record){
// The beauty of using Ext.Direct: calling 3 endpoints in a row, which results in a single call to the server!
console.log("user: " + record.get('id') );
this.selectCustomer({customer_id: record.get('id'), customer_type: 'User'});
}, this);
}
}

10
app/components/users_and_customers_accordian.rb

@ -0,0 +1,10 @@
class UsersAndCustomersAccordian < Netzke::Basepack::Accordion
component :customers
component :users
def configure(c)
c.prevent_header = true
c.items = [ :customers, :users ]
super
end
end

2
app/components/users_and_profiles_border.rb

@ -9,7 +9,7 @@ class UsersAndProfilesBorder < Netzke::Base
super super
c.header = false c.header = false
c.items = [ c.items = [
{ netzke_component: :users, region: :center, width: 300, split: true }, { netzke_component: :users, header: "Users", region: :center, width: 300, split: true },
{ netzke_component: :user_profiles, region: :south, height: 150, split: true}, { netzke_component: :user_profiles, region: :south, height: 150, split: true},
{ netzke_component: :user_logs, region: :east, split: true} { netzke_component: :user_logs, region: :east, split: true}
] ]

3
app/models/acts_as_loggable/transaction_action.rb

@ -1,7 +1,8 @@
class ActsAsLoggable::TransactionAction < ActiveRecord::Base class ActsAsLoggable::TransactionAction < ActiveRecord::Base
attr_accessible :action attr_accessible :action
belongs_to :bike has_many :logs
#belongs_to :bike
def to_s def to_s
self.action self.action

2
app/models/bike.rb

@ -3,6 +3,8 @@ class Bike < ActiveRecord::Base
attr_accessible :serial_number, :bike_brand_id, :bike_model_id, :color, :bike_style_id, :seat_tube_height, attr_accessible :serial_number, :bike_brand_id, :bike_model_id, :color, :bike_style_id, :seat_tube_height,
:top_tube_length, :wheel_size, :value, :bike_condition_id, :bike_status_id :top_tube_length, :wheel_size, :value, :bike_condition_id, :bike_status_id
has_many :transactions
has_one :owner, :class_name => 'User' has_one :owner, :class_name => 'User'
belongs_to :bike_brand belongs_to :bike_brand
belongs_to :bike_model belongs_to :bike_model

22
app/models/customer.rb

@ -0,0 +1,22 @@
class Customer < ActiveRecord::Base
attr_accessible :first_name, :last_name, :addrStreet1,
:addrStreet2, :addrCity, :addrState, :addrZip, :phone, :email
has_many :transactions, :as => :customer
validates :first_name, :presence => true
validates :last_name, :presence => true
#validates :addrStreet1, :presence => true
#validates :addrStreet2, :presence => true
#validates :addrCity, :presence => true
#validates :addrState, :presence => true
#validates :addrZip, :presence => true
#validates :phone, :presence => true
#validates :email, :presence => true
self.per_page = 15
def to_s
"#{first_name} #{last_name}"
end
end

19
app/models/transaction.rb

@ -0,0 +1,19 @@
class Transaction < ActiveRecord::Base
acts_as_loggable
attr_accessible :vendor_id, :customer_id, :customer_type, :bike_id, :amount, :item
belongs_to :vendor, :class_name => 'User', :foreign_key => 'vendor_id'
belongs_to :bike
belongs_to :customer, :polymorphic => true
validates :vendor_id, :presence => true
validates :customer_id, :presence => { :message => "Choose a User or Customer"}
validates :customer_type, :presence => { :message => "Choose a User or Customer"}
validates :amount, :presence => true
validates :item, :presence => true
def to_s
"#{amount} #{item} #{bike_id}"
end
end

1
app/models/user.rb

@ -10,6 +10,7 @@ class User < ActiveRecord::Base
:first_name, :last_name, :nickname, :user_role_id, :bike_id, :first_name, :last_name, :nickname, :user_role_id, :bike_id,
:user_profiles_attributes :user_profiles_attributes
has_many :transactions
has_many :user_profiles has_many :user_profiles
accepts_nested_attributes_for :user_profiles, allow_destroy: false accepts_nested_attributes_for :user_profiles, allow_destroy: false

6
db/migrate/20121205043759_create_transactions.rb

@ -1,9 +1,12 @@
class CreateTransactions < ActiveRecord::Migration class CreateTransactions < ActiveRecord::Migration
def change def change
create_table :transactions do |t| create_table :transactions do |t|
t.integer "user_id", :null => false t.integer "vendor_id", :null => false
t.integer "customer_id", :null => false
t.string "customer_type", :null => false
t.integer "bike_id" t.integer "bike_id"
t.integer "amount", :null => false t.integer "amount", :null => false
t.string "item", :null => false
#Adding whether or not a user sold or purchased the bike #Adding whether or not a user sold or purchased the bike
#could be used to help keep track of external sales. #could be used to help keep track of external sales.
#aka, a collective member (user) sold a bike to #aka, a collective member (user) sold a bike to
@ -11,6 +14,7 @@ class CreateTransactions < ActiveRecord::Migration
#Currently this model automatically assumes that the user is #Currently this model automatically assumes that the user is
#purchasing a bike, or a part for a bike from the collective #purchasing a bike, or a part for a bike from the collective
#t.boolean "user_sold_flag", :default => false #t.boolean "user_sold_flag", :default => false
t.timestamps
end end
end end
end end

19
db/migrate/20130120142249_create_customers.rb

@ -0,0 +1,19 @@
class CreateCustomers < ActiveRecord::Migration
def change
create_table :customers do |t|
t.string "first_name", :null => false
t.string "last_name", :null => false
t.string "addrStreet1"
t.string "addrStreet2"
t.string "addrCity"
t.string "addrState"
t.string "addrZip"
t.string "phone"
t.string "email"
end
add_index :customers, :phone, :unique => true
add_index :customers, :email, :unique => true
end
end

24
db/schema.rb

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20121229160809) do ActiveRecord::Schema.define(:version => 20130120142249) do
create_table "bike_actions", :force => true do |t| create_table "bike_actions", :force => true do |t|
t.string "action", :limit => 128, :null => false t.string "action", :limit => 128, :null => false
@ -64,6 +64,21 @@ ActiveRecord::Schema.define(:version => 20121229160809) do
add_index "bikes", ["serial_number"], :name => "index_bikes_on_serial_number", :unique => true add_index "bikes", ["serial_number"], :name => "index_bikes_on_serial_number", :unique => true
create_table "customers", :force => true do |t|
t.string "first_name", :null => false
t.string "last_name", :null => false
t.string "addrStreet1"
t.string "addrStreet2"
t.string "addrCity"
t.string "addrState"
t.string "addrZip"
t.string "phone"
t.string "email"
end
add_index "customers", ["email"], :name => "index_customers_on_email", :unique => true
add_index "customers", ["phone"], :name => "index_customers_on_phone", :unique => true
create_table "logs", :force => true do |t| create_table "logs", :force => true do |t|
t.integer "loggable_id" t.integer "loggable_id"
t.string "loggable_type" t.string "loggable_type"
@ -88,9 +103,14 @@ ActiveRecord::Schema.define(:version => 20121229160809) do
end end
create_table "transactions", :force => true do |t| create_table "transactions", :force => true do |t|
t.integer "user_id", :null => false t.integer "vendor_id", :null => false
t.integer "customer_id", :null => false
t.string "customer_type", :null => false
t.integer "bike_id" t.integer "bike_id"
t.integer "amount", :null => false t.integer "amount", :null => false
t.string "item", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end end
create_table "user_actions", :force => true do |t| create_table "user_actions", :force => true do |t|

Loading…
Cancel
Save