Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PayoutsController < ApplicationController
- require 'will_paginate'
- # GET /index
- # /index.xml
- def index
- # Array of statements and their bind values.
- con_stmt, con_bind, conditions = [], [], []
- # from date
- if (!params[:filter].nil? and !params[:filter]['from(1i)'].nil?)
- con_stmt << "created_at > ?"
- con_bind << Date.new(params[:filter]['from(li)'].to_i,
- params[:filter]['from(2i)'].to_i,
- params[:filter]['from(3i)'].to_i + 1).to_s
- end
- # to date
- if (!params[:filter].nil? and !params[:filter]['to(1i)'].nil?)
- con_stmt << "created_at < ?"
- con_bind << Date.new(params[:filter]['to(1i)'].to_i,
- params[:filter]['to(2i)'].to_i,
- params[:filter]['to(3i)'].to_i + 1).to_s
- end
- # payout_status_id
- if (params[:filter] and !params[:filter][:payout_status_id].empty?)
- con_stmt << "payout_status_id = ?"
- con_bind << params[:filter][:payout_status_id]
- else
- con_stmt << "payout_status_id NOT IN (?,?)"
- # con_bind << PayoutStatus.get(:done_processing).id # pending new data...
- con_bind << PayoutStatus.get(:cleared).id
- con_bind << PayoutStatus.get(:void).id
- end
- conditions << con_stmt.join(" AND ")
- conditions << con_bind
- respond_to do |format|
- format.html { @payouts = Payout.paginate(:page => params[:page],
- :conditions => conditions.flatten!,
- :per_page => 50,
- :order => 'updated_at DESC') } # index.html.erb
- format.xml { render :xml => Payout.find(:all, :conditions => conditions.flatten!) }
- end
- end
- def edit
- @payout = Payout.find(params[:id])
- respond_to do |format|
- format.html # index.html.erb
- format.xml { render :xml => @payouts }
- end
- end
- def show
- @payout = Payout.find_by_id(params[:id])
- @payout_entries = LedgerEntry::Base.find(:all, :conditions => ["source_id=?", @payout.id])
- respond_to do |format|
- format.html # show.html.erb
- format.xml { render :xml => @payout }
- end
- end
- def update
- @payout = Payout.find_by_id(params[:id])
- @status_to_change = [PayoutStatus.get(:cleared), PayoutStatus.get(:failed)]
- if params[:payout]
- new_status = PayoutStatus.find_by_id(params[:payout][:payout_status_id])
- if @payout.payout_status == PayoutStatus.get(:sent)
- @payout.payout_status = new_status if @status_to_change.include? new_status
- end
- @payout.save
- respond_to do |format|
- format.html { flash[:success] = "Record updated!"; redirect_to payout_url(@payout); }
- format.xml { head :ok }
- end
- end
- end
- # Pass an array "y" of id's to include and an array "n" of id's to preclude
- def new
- @ledger_entry_ids = params[:y]
- if @ledger_entry_ids.blank? or (params[:y] == params[:n])
- respond_to do |format|
- format.html { flash[:error] = "Need to pass in at least on ledger entry ID"; redirect_to :action => :prepare_for_new; }
- format.xml { render :status => 500 }
- end
- return
- end
- # Ones not to include
- ledger_entry_ids_preclude = params[:n] || []
- con_stmt, con_bind = [], []
- @ledger_entry_ids.each do |id|
- next if ledger_entry_ids_preclude.include?(id.to_s)
- con_stmt << "?"
- con_bind << id
- end
- conditions = [ "type IN (?,?,?) AND id IN (#{con_stmt.join(",")})",
- 'OwedToSeller',
- 'DueToSeller',
- 'DueToReferrer' ]
- conditions.push(con_bind).flatten!
- @ledger_entries = LedgerEntry::Base.find(:all, :conditions => conditions)
- @ledger_entries.delete_if do |e| !e.could_payout? end
- #@y = Base64.encode64(Marshal.dump(@ledger_entries.map(&:id)))
- if @ledger_entries.size == 0
- flash[:error] = "No entry found!!"
- render :action => :prepare_for_new
- return
- end
- @y = @ledger_entries.map(&:id)
- respond_to do |format|
- format.html # new.html.erb
- format.xml { render :xml => @ledger_entries }
- end
- end
- # This method should accept the array of ID's, check to make sure that none of the
- # ledger entries are already accounted for in any existing payouts. Then we need
- # to create groups of ID's -per user as our payout (i.e. one Payout per customer) .
- def create
- #@potential_ledger_ids_to_pay = Marshal.load(Base64.decode64(params[:y]))
- @potential_ledger_ids_to_pay = params[:y]
- # Grab the first type, as a payout has to pay from a single type.
- @offsetting_type = LedgerEntry::Base.find_by_id(@potential_ledger_ids_to_pay[0]).type
- @payouts = [] # We will create (potentially) many payouts
- # We want to do this in a transaction....
- begin
- # Loop around our id's, find the entry and then test to make sure they are kosher
- # to pay. Have an array of ledger entries to pay per user at the end.
- #@payout_reference_per_user_id = {}
- @payout_references_per_payment_information = {}
- @potential_ledger_ids_to_pay.each do |ledger_entry_id|
- ledger_entry = LedgerEntry::Base.find_by_id(ledger_entry_id)
- customer = ledger_entry.customer
- #raise Payout::IntegrityViolation,
- # "There is no customer record for ledger entry ##{ledger_entry.id}" unless customer
- #
- #raise Payout::IntegrityViolation,
- # "Can't have a mixed type payout, #{@offsetting_type} is not #{ledger_entry.type}" if (ledger_entry.type != @offsetting_type)
- #
- #raise Payout::IntegrityViolation,
- # "Entry #{ledger_entry.id} is already part of a payout ##{ledger_entry.ledger_reference_id}!" unless ledger_entry.could_payout?
- if (@offsetting_type.to_s == "LedgerEntry::OwedToSeller")
- Sr::LedgerEntrySystem::ManualEntry.owedtoseller_to_duetoseller(ledger_entry, current_user)
- # We create the DueToSeller and OwedToSeller offset. Payouts can only have
- # offsetting DueToXxxx types.
- #ledger_entry = LedgerEntry::DueToSeller.create!(:source => ledger_entry.source,
- # :offsetting_type => 'OwedToSeller',
- # :amount => ledger_entry.amount,
- # :user => current_user)
- end
- @payout_references_per_payment_information[ledger_entry.payment_information.id] = [] unless @payout_references_per_payment_information[ledger_entry.payment_information.id]
- @payout_references_per_payment_information[ledger_entry.payment_information.id] << ledger_entry
- #@payout_reference_per_customer[ledger_entry.customer.id] = ledger_entry
- end
- Payout.transaction do
- @payout_references_per_payment_information.each do |id, entries|
- sum = entries.inject(0) { |sum, entry| sum += entry.amount }
- p = Payout.new
- p.payment_information = PaymentInformation.find(id)
- p.payout_status = PayoutStatus.get(:awaiting_submission)
- p.total = sum
- p.payout_type = PaymentInformation.find(id).payout_type
- p.save
- entries.each do |en|
- en.ledger_entry_reference_id = p.id
- en.save
- end
- @payouts << p
- end
- end
- @p_ids = Base64.encode64(Marshal.dump(@payouts.map(&:id)))
- respond_to do |format|
- format.html # create.html.erb
- format.xml { render :xml => @payouts }
- end
- rescue ActiveRecord::RecordInvalid => e
- respond_to do |format|
- format.html { flash[:error] = e; redirect_to :action => :new, :y => params[:y]; }
- format.xml { render :xml => e.to_xml, :status => 500 }
- end
- rescue Payout::IntegrityViolation => e
- respond_to do |format|
- format.html { flash[:error] = e.to_s; redirect_to :action => :new, :y => params[:y]; }
- format.xml { render :xml => e.to_xml, :status => 500 }
- end
- end
- end
- # This method, should be a POST with an :id parameter,
- # submits the payout for background processing.
- def submit_for_processing
- #@payout = Payout.find_by_id(params[:id])
- ids = params[:id].split("/")
- ids.each do |id|
- payout = Payout.find_by_id(id)
- payout.payout_status = PayoutStatus.get(:scheduled)
- payout.save
- end
- #if @payout.valid?
- # @payout.status = PayoutStatus.get(:scheduled)
- # @payout.save!
- # #Bj.submit "./script/runner ./lib/jobs/process_payment.rb",
- # # :env => { :payout_id => params[:id] }
- #end
- jobs = Bj.submit "./script/runner Jobs::PayoutProcessing.run",
- :tag => "payout_export",
- :env => { :payout_id => ids.join(','), :user_id => current_user.id.to_s }
- respond_to do |format|
- format.html # submit_for_processing.html.erb
- format.xml { render :xml => @payout }
- end
- end
- # Can only destroy payouts that are awaiting submission
- def destroy
- @payout = Payout.find_by_id(params[:id])
- @result = @payout.destroy if @payout.payout_status == PayoutStatus.get(:awaiting_submission)
- @payouts_id = params[:ids]
- @payouts_id.delete(params[:id])
- @payouts = []
- @payouts_id.each do |id|
- @payouts << Payout.find(id)
- end
- render :template => "payouts/create", :locals => { :payouts => @payouts }, :layout => false
- #respond_to do |format|
- # format.html # destroy.html.erb
- # format.xml { head :ok }
- #end
- end
- # This in not a restful function. It's purpose is to provide a list of ledger entry
- # id's to pass to new for eventual processing.
- def prepare_for_new
- if (!params[:filter].nil?)
- @type = params[:filter][:type]
- unless (@type == 'OwedToSeller' or
- @type == 'DueToSeller' or
- @type == 'DueToReferrer')
- flash[:error] = "Must select either an Owed to Seller, Due to Seller or Due to Referral account to pay."
- render :action => :prepare_for_new
- return
- end
- @ledger_entries = LedgerEntry::Base.find_ledger_entry_to_payout(params)
- if params[:filter][:type] == "OwedToSeller"
- @ledger_entries.delete_if do |le| le.offsetting_entry.class == LedgerEntry::DueToSeller end
- end
- if @ledger_entries.size == 0
- flash[:error] = "No entry found!!"
- render :action => :prepare_for_new
- return
- end
- @y = []
- @ledger_entries.each { |e| @y << e.id }
- redirect_to :action => :new, :y => @y
- end
- end
- end
Add Comment
Please, Sign In to add comment