11# frozen_string_literal: true
22
3- require "csv"
4-
53class DividendReportCsv
6- HEADERS = [ "Date initiated" , "Date paid" , "Dividend round ID" , "Client name" , "Total dividends" ,
7- "Flexile fees" , "Transfer fees" , "Total ACH pull" , "Number of investors" ,
8- "Dividend round status" , "Payment processor" , "Transfer reference" ]
4+ HEADERS = [ "Date initiated" , "Date paid" , "Client name" , "Total dividends ($)" , "Flexile fees ($)" ,
5+ "Transfer fees ($)" , "Total ACH pull ($)" , "Number of investors" , "Dividend round status" ]
96
107 def initialize ( dividend_rounds )
118 @dividend_rounds = dividend_rounds
@@ -23,48 +20,31 @@ def generate
2320
2421 private
2522 def dividend_round_data
26- @dividend_rounds . each_with_object ( [ ] ) do |round , rows |
27- successful_dividends = round . dividends . joins ( :dividend_payments )
28- . merge ( DividendPayment . successful )
29- . includes ( :dividend_payments , company_investor : :user )
30-
31- next if successful_dividends . empty?
32-
33- total_dividends = successful_dividends . sum ( :total_amount_in_cents ) / 100.0
34- total_transfer_fees = successful_dividends . joins ( :dividend_payments )
35- . sum ( "dividend_payments.transfer_fee_in_cents" ) / 100.0
36-
37- flexile_fees = successful_dividends . map do |dividend |
23+ @dividend_rounds . each_with_object ( [ ] ) do |dividend_round , rows |
24+ dividends = dividend_round . dividends
25+ total_dividends = dividends . sum ( :total_amount_in_cents ) / 100.0
26+ total_transfer_fees = dividends . joins ( :dividend_payments )
27+ . where ( dividend_payments : { status : Payments ::Status ::SUCCEEDED } )
28+ . sum ( "dividend_payments.transfer_fee_in_cents" ) / 100.0
29+
30+ flexile_fees = dividends . map do |dividend |
3831 calculated_fee = ( ( dividend . total_amount_in_cents . to_d * 2.9 . to_d / 100 . to_d ) + 30 . to_d ) . round . to_i
3932 [ 30_00 , calculated_fee ] . min
4033 end . sum / 100.0
4134
42- total_ach_pull = total_dividends + flexile_fees + total_transfer_fees
35+ total_ach_pull = total_dividends + flexile_fees
4336
44- payment = successful_dividends . first . dividend_payments . successful . first
4537 rows << [
46- round . issued_at . to_fs ( :us_date ) ,
47- successful_dividends . first . paid_at &.to_fs ( :us_date ) ,
48- round . id ,
49- round . company . name ,
38+ dividend_round . issued_at . to_fs ( :us_date ) ,
39+ dividends . paid . first &.paid_at &.to_fs ( :us_date ) ,
40+ dividend_round . company . name ,
5041 total_dividends ,
5142 flexile_fees ,
5243 total_transfer_fees ,
5344 total_ach_pull ,
54- successful_dividends . count ,
55- round . status ,
56- payment &.processor_name ,
57- payment &.transfer_id
45+ dividends . count ,
46+ dividend_round . status ,
5847 ]
5948 end
6049 end
6150end
62-
63- ### Usage:
64- =begin
65- dividend_rounds = DividendRound.includes(:dividends, :company, dividends: [:dividend_payments, company_investor: :user])
66- .where("issued_at >= ? AND issued_at <= ?", Time.current.last_month.beginning_of_month, Time.current.last_month.end_of_month)
67- .order(issued_at: :asc)
68- attached = { "DividendReport.csv" => DividendReportCsv.new(dividend_rounds).generate }
69- AdminMailer.custom(to: ["solson@earlygrowth.com"], subject: "Flexile Dividend Report CSV", body: "Attached", attached:).deliver_now
70- =end
0 commit comments