mercredi 25 mars 2015

Displaying unique transactions in Ruby on Rails

Currently there are multiple transactions with the same txnid. But for each transaction the payment mode is credit card, debit card or net banking.


If the customer has to pay $3000 he might pay that $3000 like $1000 with Credit card, $1000 with Debit Card, $1000 with Net banking. Currently am displaying all those 3 transactions in 3 rows but with the same txnid. But I want to display those 3 transactions in a single row by mentioning in the payment mode column ($1000- Credit Card, $1000 Debit Card, $1000 Net bank)


Thanks for your help .


transactions.html.erb



<tr>
<th>Merchant Name</th>
<th>Transaction ID</th>
<th>Payment Mode</th>
<th>Amount</th>
<th>Status</th>
<th>Transaction Fee</th>
<th>Discount</th>
<th>Additional Charges</th>
<th>Added On</th>
</tr>
</thead>
<tbody id="details">
<% @all_settlement_details.map(&:txnid).uniq.each do |sd| %>
<tr>
<td><%= sd.merchantname %></td>
<td><%= sd.txnid %></td>
<td><%= get_mode_ui.value(sd.mode) %></td>
<td><%= sd.amount %></td>
<td><%= get_status(sd.status) %></td>
<td><%= sd.mer_service_fee %></td>
<td><%= sd.discount %></td>
<td><%= sd.additional_charges%></td>
<td><%= get_added_on_date sd.addedon %></td>
</tr>
<% end %>
</tbody>


trasactions.helper.rb



INDEX_OPTIONS = {'dashboard' => 'get_dashboard_data', 'merchants' => 'get_merchants', 'transactions' => 'get_transactions',
'admin' => 'get_admin_data', 'profile' => 'get_merchant_data'}

INDEX_PARTIAL = {'dashboard' => 'dashboard', 'merchants' => 'merchant_list', 'transactions' => 'transactions',
'admin' => 'admin', 'profile' => 'merchant_profile'}

AJAX_OPTIONS = {'stat-board' => 'get_stats', 'transactions' => 'get_graph_transactions', 'payment-methods' => 'get_payment_method_graph_data',
'merchants' => 'get_merchant_graph_data', 'merchant_profile' => 'get_merchant_profile_data', 'load_more' => 'load_more',
'merchant_list' => 'get_merchants'}

AJAX_PARTIAL = {'stat-board' => 'stat_board', 'transactions' => 'transactions_graph', 'payment-methods' => 'payment_methods_graph',
'merchants' => 'merchant_graphs', 'load_more' => 'transaction_details', 'merchant_profile' => 'merchant_profile_detail',
'merchant_list' => 'filtered_merchants',}

STATUS={"dropped"=>0,"bounced"=>1,"captured"=>2,"auth"=>3,"failed"=>4,"usercancelled"=>5,"pending"=>6}
MODE={'CC'=>0,'DC'=>1,'NB'=>2,'EMI'=>3,'CASH'=>4,'IVR'=>5,'COD'=>6}
MODE_UI = {'CC'=>'Credit Card','DC'=>'Debit Card','NB'=>'Net Banking','EMI'=>'EMI','CASH'=>'Cash','IVR'=>'IVR','COD'=>'Cash on Delivery'}
PG_MODE = {'Credit Card'=>0,'Debit Card'=>1,'Net Banking'=>2,'EMI'=>3,'Cash'=>4,'IVR'=>5,'Cash on Delivery'=>6,'Others'=>nil}
STATUS_UI={"Dropped"=>0,"Bounced"=>1,"Captured"=>2,"Authenticated"=>3,"Failed"=>4,"User Cancelled"=>5,"Pending"=>6}
CARDTYPE={'domestic'=>1,'international'=>2}
TRANSACTIONS = ["Merchant Name", "Transaction ID", "Payment Mode","Amount","Status","Transaction Fee","Discount","Additional Charges","Added On"]

TXN_SUCCESS_STATUS = [2,3]
TXN_FAILURE_STATUS = [0,1,4,5]
TXN_PENDING_STATUS = 6

PARTNER_PANEL_REDMINE_STATUS_MAPPING = {
16 => "In Process",
3 => "In Process",
69 => "In Process",
11 => "Line of Business Rejected",
14 => "Live",
42 => "In Process",
51 => "In Process",
15 => "In Process",
17 => "In Process",
64 => "In Process",
46 => "In Process",
62 => "Line of Business Rejected",
65 => "In Process",
18 => "Assigned to Sales",
79 => "In Process",
61 => "In Process",
5 => "In Process",
70 => "In Process",
71 => "In Process",
50 => "Line of Business Rejected",
32 => "Contacted",
48 => "In Process",
59 => "In Process",
49 => "In Process",
47 => "In Process",
60 => "In Process",
29 => "Line of Business Rejected",
78 => "Assigned to Sales"
}

PARTNER_PANEL_REDMINE_STATUS_MAPPING_UI = {
"In Process" => "16,3,69,42,51,15,17,64,46,65,79,61,5,70,71,48,59,49,47,60",
"Line of Business Rejected" => "11,62,50,29",
"Assigned to Sales" => "78,18",
"Live" => "14",
"Contacted" => "32"
}

def get_status _status_flag
STATUS.key(_status_flag)
end

def get_mode _mode_flag
MODE.key(_mode_flag)
end

def get_mode_ui
PG_MODE
end

def get_transactions_ui
TRANSACTIONS
end

def get_status_ui
STATUS_UI
end

def get_merchant_status_ui
PARTNER_PANEL_REDMINE_STATUS_MAPPING_UI
end

def status_merchant
PARTNER_PANEL_REDMINE_STATUS_MAPPING
end

def get_added_on_date _date
_date.strftime("%A, %d %B\'%y %H:%M")
end

def link_to_add_issues(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_issues(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
end

def link_to_remove_issues(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_issues(this)")
end

def get_issue_object _partner
_partner.issues.blank? ? _partner.class.reflect_on_association(:issues).klass.new : _partner.issues
end

def get_contact_detail_object issue
issue.contact_detail.blank? ? issue.class.reflect_on_association(:contact_detail).klass.new : issue.contact_detail
end

def issue_contact_detail issue, contact_column_name
issue.contact_detail ? issue.contact_detail.send(contact_column_name) : ""
end

def options_for_state(issue)
options_for_select(Setting[:states].split(/\r\n/).sort, issue.state).html_safe
end

# First Landing page for a Partner
def get_dashboard_data
get_stats
get_merchants
end

# Merchant list for a Partner
def get_merchants
_merchant_filter_scope = {'merchant_status' => ["merchant_status", (params[:merchant_status].split(',') rescue nil)],
'merchant_email' => ["merchant_mail", params[:merchant_email]],
'merchant_url' => ["merchant_url", params[:merchant_url]],
'merchant_mid' => ['payu_mid', params[:merchant_mid]],
'merchant_name' => ['merchant_name', params[:merchant_name]],
'order' => ['order_issue', params[:order]]
}
method_array = [['issues'],["page", params[:page]],["per", per_page_option],["date_filter", @start_date, @end_date]]
@partner_merchants = call_scope_methods_dynamically(method_array, _merchant_filter_scope)
end

# Transactions for a Partner
def get_transactions
#get more records to load
load_more
end

# Merchant Profile page for a Partner
def get_merchant_data
get_stats
load_more
end

# Admin page for Partners
def get_admin_data
@partners = Partner.where(partner_admin: false)
end

# Stat Board tab on Dashboard
def get_stats
method_array = [['settlement_details'],['stat_board_details'],['count_as_graph_data']]
method_array << ['merchant_mid', @merchant_mid] if @merchant_mid
method_array = add_date_or_hour_filter method_array
data1 = call_methods(method_array)
method_array << ['group_by_settlements_date']
data2 = call_methods(method_array)
@graph_data = [data1, data2]
end

# Merchant tab on Dashboard
def get_merchant_graph_data
method_array = [['issues']]
method_array = add_date_or_hour_filter method_array
method_array << ['group_by_merchant_date']
data1 = @bank_partner.issues.date_filter(@start_date, @end_date).group_by(&:status_id)
data2 = call_methods(method_array)
@merchant_status_hash = {}
data1.each{|key, value| get_partner_status_hash(get_merchant_status(key), value.count)}
[@merchant_status_hash, data2]
end

def get_merchant_status redmine_status
PARTNER_PANEL_REDMINE_STATUS_MAPPING[redmine_status] || 'In Process'
end

def get_partner_status_hash key, value
@merchant_status_hash.update({key => value}){|k, ov, nv| ov + nv}
end

# Transaction tab on Dashboard
def get_graph_transactions
method_array = [['settlement_details'],['count_as_graph_data']]
method_array << ['merchant_mid', @merchant_mid] if @merchant_mid
method_array = add_date_or_hour_filter method_array
method_array << ['group_by_settlements_date']
@data1 = call_methods(method_array << ["status", TXN_SUCCESS_STATUS])
@data2 = call_methods(method_array << ["status", TXN_FAILURE_STATUS])
@data3 = call_methods(method_array << ["status", TXN_PENDING_STATUS])
[@data1, @data2, @data3]
end

# Payment Method tab on Dashboard
def get_payment_method_graph_data
method_array = [['settlement_details'],["date_filter", @start_date, @end_date],['payment_mode']]
method_array << ['merchant_mid', @merchant_mid] if @merchant_mid
method_array << ['count']
call_methods(method_array)
end

def get_transaction_graph_hash successful, failure, pending
data_array= []
successful_hash = {}
failure_hash = {}
pending_hash = {}
successful.each{|d| successful_hash.merge!({(d.hour rescue d.date) => d.graph_data})}
failure.each{|d| failure_hash.merge!({(d.hour rescue d.date) => d.graph_data})}
pending.each{|d| pending_hash.merge!({(d.hour rescue d.date) => d.graph_data})}
(successful_hash.keys + failure_hash.keys + pending_hash.keys).uniq.each do |datetime|
data_array << {'datetime'=> "#{datetime}",'successful'=> (successful_hash[datetime] || 0) ,'unsuccessful'=> (failure_hash[datetime] || 0) ,'pending'=> (pending_hash[datetime] || 0)}
end
data_array
end

private

def load_more
@all_settlement_details = get_all_settlement_details || []
end

def get_all_settlement_details
# Offset increments by 10 and Limit remains 11
_txn_filter_scope = {'transaction_status' => ["status", (params[:transaction_status].split(',') rescue nil)],
'payment_options' => ["payment_options", (params[:payment_options].split(',') rescue nil)],
'transaction_id' => ['transaction_id', params[:transaction_id]],
'transaction_amount' => ['amount_range', ((params[:transaction_amount].split(',')[0] || 0) rescue nil), ((params[:transaction_amount].split(',')[1] || 100000000) rescue nil)],
'merchant_mid' => ['merchant_mid',params[:merchant_mid]],
'merchant_name' => ['merchant_name', params[:merchant_name]],
'clicks' => ['txn_offset', ((params[:clicks].to_i)*10 rescue 0)]
}
method_array = [['settlement_details'],["date_filter", @start_date, @end_date]]
method_array << ["txn_limit", 11] unless @csv_format
method_array << ['merchant_mid', @merchant_mid] if @merchant_mid
records = call_scope_methods_dynamically(method_array, _txn_filter_scope)
records.empty? ? nil : records
end
# extend Object to introduce the call_methods method
def call_methods(methods)
methods.inject(@bank_partner) do |obj, method|
case method.count
when 3
obj.send(method.first, method[1], method[2])
when 2
obj.send(method.first, method.last)
when 1
obj.send(method.first)
else
obj
end
end
end

def call_scope_methods_dynamically(_method_array, _method_hash)
method_array = _method_array
_dynamic_method_hash = _method_hash
# Add scopes in the methods array to call dynamically
params.each{|key, value| method_array << _dynamic_method_hash[key]}
call_methods(method_array.compact)
end

def add_date_or_hour_filter _method_array
if @start_date == @end_date
@end_date = (Date.parse(@end_date) + 1.day).strftime("%Y/%m/%d")
_method_array << ['hour_filter']
end
_method_array << ["date_filter", @start_date, @end_date]
_method_array
end


My final output should be look like.



Txnid payment mode Amount
23234XS24XS $1000Credit Card, $3000
$1000 Debit Card,
$1000 Net bank


Some one said I need to create a helper method for that. But I am not sure how! Thanks for your help!


Aucun commentaire:

Enregistrer un commentaire