From 873bccb0307e67a5d31c5eeb325fb7a871f7f262 Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Sat, 2 Feb 2013 14:08:42 -0500 Subject: [PATCH] Completed adding transactions --- app/components/app_tab_panel.rb | 24 ++++++- app/components/bike_logs.rb | 1 + app/components/transaction_logs.rb | 67 +++++++++++++++++++ app/components/transactions.rb | 13 +++- app/components/transactions_border.rb | 9 +-- .../javascripts/init_component.js | 14 ++++ app/components/user_logs.rb | 1 + app/components/user_transactions.rb | 40 +++++++++++ app/components/user_transactions_border.rb | 27 ++++++++ .../javascripts/init_component.js | 14 ++++ .../acts_as_loggable/transaction_action.rb | 5 +- 11 files changed, 203 insertions(+), 12 deletions(-) create mode 100644 app/components/transaction_logs.rb create mode 100644 app/components/transactions_border/javascripts/init_component.js create mode 100644 app/components/user_transactions.rb create mode 100644 app/components/user_transactions_border.rb create mode 100644 app/components/user_transactions_border/javascripts/init_component.js diff --git a/app/components/app_tab_panel.rb b/app/components/app_tab_panel.rb index da91645..1b92ebb 100644 --- a/app/components/app_tab_panel.rb +++ b/app/components/app_tab_panel.rb @@ -10,17 +10,35 @@ class AppTabPanel < Netzke::Basepack::TabPanel #all users # (had to use hash for borders to get the title to display properly) - @@app_tab_panel_items = [ :transactions_border, {layout: :fit, wrappedComponent: :bikes_border, title: "Bikes"}, {layout: :fit, wrappedComponent: :brands_and_models_border, title: "Brands/Models"}] + @@app_tab_panel_items = [ { layout: :fit, + wrappedComponent: :bikes_border, + title: "Bikes"}, + { layout: :fit, + wrappedComponent: :brands_and_models_border, + title: "Brands/Models"} + ] #for users if controller.current_user.user? # (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 #for admins if controller.current_user.admin? # (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 @@app_tab_panel_items.each do |item| diff --git a/app/components/bike_logs.rb b/app/components/bike_logs.rb index 9564af4..9c0b1fe 100644 --- a/app/components/bike_logs.rb +++ b/app/components/bike_logs.rb @@ -44,6 +44,7 @@ class BikeLogs < Netzke::Basepack::Grid { :name => :start_date}, { :name => :end_date}, { :name => :description}, + #had to hack acts_as_loggable/log.rb to get this to work { :name => :bike_action__action, :field_label => 'Action'} ] end diff --git a/app/components/transaction_logs.rb b/app/components/transaction_logs.rb new file mode 100644 index 0000000..7cbdc4e --- /dev/null +++ b/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 diff --git a/app/components/transactions.rb b/app/components/transactions.rb index 0246f39..4fe60bd 100644 --- a/app/components/transactions.rb +++ b/app/components/transactions.rb @@ -20,9 +20,10 @@ class Transactions < Netzke::Basepack::Grid user = rec.customer user.nil? ? "" : "#{user.first_name} #{user.last_name}" } - } + }, + :created_at ] - + end def default_fields_for_forms @@ -34,7 +35,7 @@ class Transactions < Netzke::Basepack::Grid 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}"}, @@ -52,4 +53,10 @@ class Transactions < Netzke::Basepack::Grid 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 diff --git a/app/components/transactions_border.rb b/app/components/transactions_border.rb index 29bf7c9..b66296f 100644 --- a/app/components/transactions_border.rb +++ b/app/components/transactions_border.rb @@ -2,6 +2,7 @@ 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 @@ -11,6 +12,7 @@ class TransactionsBorder < Netzke::Base 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 @@ -21,9 +23,8 @@ class TransactionsBorder < Netzke::Base c.mixin :init_component end - endpoint :select_customer do |params, this| - session[:selected_customer_id] = params[:customer_id] - session[:selected_customer_type] = params[:customer_type] + endpoint :select_transaction do |params, this| + session[:selected_transaction_id] = params[:transaction_id] end - + end diff --git a/app/components/transactions_border/javascripts/init_component.js b/app/components/transactions_border/javascripts/init_component.js new file mode 100644 index 0000000..5d41919 --- /dev/null +++ b/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); + } +} diff --git a/app/components/user_logs.rb b/app/components/user_logs.rb index c5dfbbf..ee0aebd 100644 --- a/app/components/user_logs.rb +++ b/app/components/user_logs.rb @@ -58,6 +58,7 @@ class UserLogs < Netzke::Basepack::Grid { :name => :start_date}, { :name => :end_date}, { :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 => :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} diff --git a/app/components/user_transactions.rb b/app/components/user_transactions.rb new file mode 100644 index 0000000..78688a3 --- /dev/null +++ b/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 diff --git a/app/components/user_transactions_border.rb b/app/components/user_transactions_border.rb new file mode 100644 index 0000000..0b50dd1 --- /dev/null +++ b/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 diff --git a/app/components/user_transactions_border/javascripts/init_component.js b/app/components/user_transactions_border/javascripts/init_component.js new file mode 100644 index 0000000..8266a14 --- /dev/null +++ b/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); + } +} diff --git a/app/models/acts_as_loggable/transaction_action.rb b/app/models/acts_as_loggable/transaction_action.rb index f4a2307..fbf95b7 100644 --- a/app/models/acts_as_loggable/transaction_action.rb +++ b/app/models/acts_as_loggable/transaction_action.rb @@ -1,7 +1,8 @@ class ActsAsLoggable::TransactionAction < ActiveRecord::Base attr_accessible :action - - belongs_to :bike + + has_many :logs + #belongs_to :bike def to_s self.action