Guest User

Untitled

a guest
May 20th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.89 KB | None | 0 0
  1. class PayoutsController < ApplicationController
  2. require 'will_paginate'
  3.  
  4. # GET /index
  5. # /index.xml
  6. def index
  7.  
  8. # Array of statements and their bind values.
  9. con_stmt, con_bind, conditions = [], [], []
  10.  
  11. # from date
  12. if (!params[:filter].nil? and !params[:filter]['from(1i)'].nil?)
  13. con_stmt << "created_at > ?"
  14. con_bind << Date.new(params[:filter]['from(li)'].to_i,
  15. params[:filter]['from(2i)'].to_i,
  16. params[:filter]['from(3i)'].to_i + 1).to_s
  17. end
  18.  
  19. # to date
  20. if (!params[:filter].nil? and !params[:filter]['to(1i)'].nil?)
  21. con_stmt << "created_at < ?"
  22. con_bind << Date.new(params[:filter]['to(1i)'].to_i,
  23. params[:filter]['to(2i)'].to_i,
  24. params[:filter]['to(3i)'].to_i + 1).to_s
  25. end
  26.  
  27. # payout_status_id
  28. if (params[:filter] and !params[:filter][:payout_status_id].empty?)
  29. con_stmt << "payout_status_id = ?"
  30. con_bind << params[:filter][:payout_status_id]
  31. else
  32. con_stmt << "payout_status_id NOT IN (?,?)"
  33. # con_bind << PayoutStatus.get(:done_processing).id # pending new data...
  34. con_bind << PayoutStatus.get(:cleared).id
  35. con_bind << PayoutStatus.get(:void).id
  36. end
  37.  
  38. conditions << con_stmt.join(" AND ")
  39. conditions << con_bind
  40.  
  41. respond_to do |format|
  42. format.html { @payouts = Payout.paginate(:page => params[:page],
  43. :conditions => conditions.flatten!,
  44. :per_page => 50,
  45. :order => 'updated_at DESC') } # index.html.erb
  46. format.xml { render :xml => Payout.find(:all, :conditions => conditions.flatten!) }
  47. end
  48. end
  49.  
  50. def edit
  51. @payout = Payout.find(params[:id])
  52. respond_to do |format|
  53. format.html # index.html.erb
  54. format.xml { render :xml => @payouts }
  55. end
  56. end
  57.  
  58. def show
  59. @payout = Payout.find_by_id(params[:id])
  60. @payout_entries = LedgerEntry::Base.find(:all, :conditions => ["source_id=?", @payout.id])
  61.  
  62. respond_to do |format|
  63. format.html # show.html.erb
  64. format.xml { render :xml => @payout }
  65. end
  66. end
  67.  
  68. def update
  69. @payout = Payout.find_by_id(params[:id])
  70. @status_to_change = [PayoutStatus.get(:cleared), PayoutStatus.get(:failed)]
  71. if params[:payout]
  72. new_status = PayoutStatus.find_by_id(params[:payout][:payout_status_id])
  73. if @payout.payout_status == PayoutStatus.get(:sent)
  74. @payout.payout_status = new_status if @status_to_change.include? new_status
  75. end
  76. @payout.save
  77. respond_to do |format|
  78. format.html { flash[:success] = "Record updated!"; redirect_to payout_url(@payout); }
  79. format.xml { head :ok }
  80. end
  81. end
  82. end
  83.  
  84.  
  85. # Pass an array "y" of id's to include and an array "n" of id's to preclude
  86. def new
  87. @ledger_entry_ids = params[:y]
  88.  
  89. if @ledger_entry_ids.blank? or (params[:y] == params[:n])
  90. respond_to do |format|
  91. format.html { flash[:error] = "Need to pass in at least on ledger entry ID"; redirect_to :action => :prepare_for_new; }
  92. format.xml { render :status => 500 }
  93. end
  94. return
  95. end
  96.  
  97. # Ones not to include
  98. ledger_entry_ids_preclude = params[:n] || []
  99.  
  100. con_stmt, con_bind = [], []
  101.  
  102. @ledger_entry_ids.each do |id|
  103. next if ledger_entry_ids_preclude.include?(id.to_s)
  104. con_stmt << "?"
  105. con_bind << id
  106. end
  107.  
  108. conditions = [ "type IN (?,?,?) AND id IN (#{con_stmt.join(",")})",
  109. 'OwedToSeller',
  110. 'DueToSeller',
  111. 'DueToReferrer' ]
  112.  
  113. conditions.push(con_bind).flatten!
  114.  
  115. @ledger_entries = LedgerEntry::Base.find(:all, :conditions => conditions)
  116. @ledger_entries.delete_if do |e| !e.could_payout? end
  117. #@y = Base64.encode64(Marshal.dump(@ledger_entries.map(&:id)))
  118. if @ledger_entries.size == 0
  119. flash[:error] = "No entry found!!"
  120. render :action => :prepare_for_new
  121. return
  122. end
  123. @y = @ledger_entries.map(&:id)
  124. respond_to do |format|
  125. format.html # new.html.erb
  126. format.xml { render :xml => @ledger_entries }
  127. end
  128.  
  129. end
  130.  
  131. # This method should accept the array of ID's, check to make sure that none of the
  132. # ledger entries are already accounted for in any existing payouts. Then we need
  133. # to create groups of ID's -per user as our payout (i.e. one Payout per customer) .
  134. def create
  135.  
  136. #@potential_ledger_ids_to_pay = Marshal.load(Base64.decode64(params[:y]))
  137. @potential_ledger_ids_to_pay = params[:y]
  138.  
  139. # Grab the first type, as a payout has to pay from a single type.
  140. @offsetting_type = LedgerEntry::Base.find_by_id(@potential_ledger_ids_to_pay[0]).type
  141.  
  142.  
  143. @payouts = [] # We will create (potentially) many payouts
  144.  
  145. # We want to do this in a transaction....
  146. begin
  147. # Loop around our id's, find the entry and then test to make sure they are kosher
  148. # to pay. Have an array of ledger entries to pay per user at the end.
  149. #@payout_reference_per_user_id = {}
  150.  
  151. @payout_references_per_payment_information = {}
  152.  
  153. @potential_ledger_ids_to_pay.each do |ledger_entry_id|
  154. ledger_entry = LedgerEntry::Base.find_by_id(ledger_entry_id)
  155. customer = ledger_entry.customer
  156.  
  157. #raise Payout::IntegrityViolation,
  158. # "There is no customer record for ledger entry ##{ledger_entry.id}" unless customer
  159. #
  160. #raise Payout::IntegrityViolation,
  161. # "Can't have a mixed type payout, #{@offsetting_type} is not #{ledger_entry.type}" if (ledger_entry.type != @offsetting_type)
  162. #
  163. #raise Payout::IntegrityViolation,
  164. # "Entry #{ledger_entry.id} is already part of a payout ##{ledger_entry.ledger_reference_id}!" unless ledger_entry.could_payout?
  165.  
  166. if (@offsetting_type.to_s == "LedgerEntry::OwedToSeller")
  167. Sr::LedgerEntrySystem::ManualEntry.owedtoseller_to_duetoseller(ledger_entry, current_user)
  168. # We create the DueToSeller and OwedToSeller offset. Payouts can only have
  169. # offsetting DueToXxxx types.
  170. #ledger_entry = LedgerEntry::DueToSeller.create!(:source => ledger_entry.source,
  171. # :offsetting_type => 'OwedToSeller',
  172. # :amount => ledger_entry.amount,
  173. # :user => current_user)
  174. end
  175.  
  176. @payout_references_per_payment_information[ledger_entry.payment_information.id] = [] unless @payout_references_per_payment_information[ledger_entry.payment_information.id]
  177. @payout_references_per_payment_information[ledger_entry.payment_information.id] << ledger_entry
  178.  
  179. #@payout_reference_per_customer[ledger_entry.customer.id] = ledger_entry
  180. end
  181.  
  182. Payout.transaction do
  183. @payout_references_per_payment_information.each do |id, entries|
  184. sum = entries.inject(0) { |sum, entry| sum += entry.amount }
  185. p = Payout.new
  186. p.payment_information = PaymentInformation.find(id)
  187. p.payout_status = PayoutStatus.get(:awaiting_submission)
  188. p.total = sum
  189. p.payout_type = PaymentInformation.find(id).payout_type
  190. p.save
  191. entries.each do |en|
  192. en.ledger_entry_reference_id = p.id
  193. en.save
  194. end
  195. @payouts << p
  196. end
  197. end
  198. @p_ids = Base64.encode64(Marshal.dump(@payouts.map(&:id)))
  199.  
  200. respond_to do |format|
  201. format.html # create.html.erb
  202. format.xml { render :xml => @payouts }
  203. end
  204.  
  205. rescue ActiveRecord::RecordInvalid => e
  206. respond_to do |format|
  207. format.html { flash[:error] = e; redirect_to :action => :new, :y => params[:y]; }
  208. format.xml { render :xml => e.to_xml, :status => 500 }
  209. end
  210.  
  211. rescue Payout::IntegrityViolation => e
  212. respond_to do |format|
  213. format.html { flash[:error] = e.to_s; redirect_to :action => :new, :y => params[:y]; }
  214. format.xml { render :xml => e.to_xml, :status => 500 }
  215. end
  216.  
  217. end
  218.  
  219. end
  220.  
  221. # This method, should be a POST with an :id parameter,
  222. # submits the payout for background processing.
  223. def submit_for_processing
  224. #@payout = Payout.find_by_id(params[:id])
  225. ids = params[:id].split("/")
  226.  
  227. ids.each do |id|
  228. payout = Payout.find_by_id(id)
  229. payout.payout_status = PayoutStatus.get(:scheduled)
  230. payout.save
  231. end
  232. #if @payout.valid?
  233. # @payout.status = PayoutStatus.get(:scheduled)
  234. # @payout.save!
  235. # #Bj.submit "./script/runner ./lib/jobs/process_payment.rb",
  236. # # :env => { :payout_id => params[:id] }
  237. #end
  238.  
  239. jobs = Bj.submit "./script/runner Jobs::PayoutProcessing.run",
  240. :tag => "payout_export",
  241. :env => { :payout_id => ids.join(','), :user_id => current_user.id.to_s }
  242.  
  243. respond_to do |format|
  244. format.html # submit_for_processing.html.erb
  245. format.xml { render :xml => @payout }
  246. end
  247.  
  248. end
  249.  
  250. # Can only destroy payouts that are awaiting submission
  251. def destroy
  252. @payout = Payout.find_by_id(params[:id])
  253. @result = @payout.destroy if @payout.payout_status == PayoutStatus.get(:awaiting_submission)
  254. @payouts_id = params[:ids]
  255. @payouts_id.delete(params[:id])
  256. @payouts = []
  257. @payouts_id.each do |id|
  258. @payouts << Payout.find(id)
  259. end
  260. render :template => "payouts/create", :locals => { :payouts => @payouts }, :layout => false
  261. #respond_to do |format|
  262. # format.html # destroy.html.erb
  263. # format.xml { head :ok }
  264. #end
  265. end
  266.  
  267.  
  268. # This in not a restful function. It's purpose is to provide a list of ledger entry
  269. # id's to pass to new for eventual processing.
  270. def prepare_for_new
  271.  
  272. if (!params[:filter].nil?)
  273. @type = params[:filter][:type]
  274. unless (@type == 'OwedToSeller' or
  275. @type == 'DueToSeller' or
  276. @type == 'DueToReferrer')
  277. flash[:error] = "Must select either an Owed to Seller, Due to Seller or Due to Referral account to pay."
  278. render :action => :prepare_for_new
  279. return
  280. end
  281. @ledger_entries = LedgerEntry::Base.find_ledger_entry_to_payout(params)
  282.  
  283. if params[:filter][:type] == "OwedToSeller"
  284. @ledger_entries.delete_if do |le| le.offsetting_entry.class == LedgerEntry::DueToSeller end
  285. end
  286. if @ledger_entries.size == 0
  287. flash[:error] = "No entry found!!"
  288. render :action => :prepare_for_new
  289. return
  290. end
  291. @y = []
  292. @ledger_entries.each { |e| @y << e.id }
  293.  
  294. redirect_to :action => :new, :y => @y
  295.  
  296. end
  297.  
  298. end
  299.  
  300. end
Add Comment
Please, Sign In to add comment