<commit_before>require 'nokogiri'
require 'stripe'
require 'oauth'
include OAuth::Helper
include EtradeHelper
include DwollaHelper

GIV_FEE_AMOUNT = App.giv["giv_fee_percentage"].to_f
GIV_GRANT_AMOUNT = App.giv["giv_grant_amount"]
SHARE_PRECISION = App.giv["share_precision"]
PIN_DWOLLA = App.dwolla["pin_account"]

module CalculationShare
  class Calculation

    class << self

      def compute_share_price
        stripe_balance = get_stripe_balance
        etrade_balance = get_etrade_balance
        givbalance = stripe_balance + etrade_balance
      
        date_yesterday = Date.yesterday.strftime('%Y%m%d')

        # shares added by donation
        shares_donated_yesterday = Donation.where("date_format(created_at, '%Y%m%d') = ?", date_yesterday).sum(:shares_added)
        # shares removed by grant
        shares_granted_yesterday = CharityGrant.where("status = ?", "sent").where("date_format(created_at, '%Y%m%d') = ?", date_yesterday).sum(:shares_subtracted)
        donors_shares_total_beginning = Share.order("created_At DESC").last.share_total_end.to_f rescue 0.0
        share_total_end = (BigDecimal("#{donors_shares_total_beginning}") + BigDecimal("#{shares_donated_yesterday}") - BigDecimal("#{shares_granted_yesterday}")).round(SHARE_PRECISION)

        # get donation share price
        # givbalance / total_donor_shares_all_time
        preliminary_share_price = (BigDecimal("#{givbalance}") / BigDecimal("#{share_total_end}")).to_f

        preliminary_share_price = 100000.0 unless preliminary_share_price.finite?
        if preliminary_share_price.to_f.nan?
          preliminary_share_price = 100000.0
        end

        donation_share_price = (preliminary_share_price * 10).ceil / 10.0
        grant_share_price = (preliminary_share_price * 10).floor / 10.0

        new_record_share = Share.new(
                                     :stripe_balance => stripe_balance,
                                     :etrade_balance => etrade_balance,
                                     :share_total_beginning => donors_shares_total_beginning,
                                     :shares_added_by_donation => shares_donated_yesterday,
                                     :shares_subtracted_by_grants => shares_granted_yesterday,
                                     :share_total_end => share_total_end,
                                     :donation_price => donation_share_price,
                                     :grant_price => grant_share_price
                                    )
        if new_record_share.save
          puts "Share Price has been updated"
          JobMailer.success_compute_share_price(App.giv["email_support"], "compute_share_price").deliver
        else
          puts "ERROR"
        end
      end

      def grant_step_1
        stripe_balance = get_stripe_balance
        etrade_balance = get_etrade_balance
        givbalance = stripe_balance + etrade_balance
        
        endowments = Endowment.all

        endowments.each do |endowment|

          endowment_share_balance = BigDecimal("#{endowment.donations.sum(:shares_added)}") - BigDecimal("#{endowment.charity_grants.sum(:shares_subtracted)}")
          endowment_grant_shares = (BigDecimal("#{endowment_share_balance}") * BigDecimal("#{GIV_GRANT_AMOUNT}")).round(SHARE_PRECISION)
          charity_grant_shares = (BigDecimal("#{endowment_grant_shares}") / BigDecimal("#{endowment.charities.count}")).round(SHARE_PRECISION)

          endowment_donors = endowment.donations
          charities = endowment.charities

          charities.each do |charity|
            if charity.active.eql?("true")<commit_msg>wrong function name<commit_after>require 'nokogiri'
require 'stripe'
require 'oauth'
include OAuth::Helper
include EtradeHelper
include DwollaHelper

GIV_FEE_AMOUNT = App.giv["giv_fee_percentage"].to_f
GIV_GRANT_AMOUNT = App.giv["giv_grant_amount"]
SHARE_PRECISION = App.giv["share_precision"]
PIN_DWOLLA = App.dwolla["pin_account"]

module CalculationShare
  class Calculation

    class << self

      def compute_share_price
        stripe_balance = get_stripe_balance
        etrade_balance = get_etrade_balance
        givbalance = stripe_balance + etrade_balance
      
        date_yesterday = Date.yesterday.strftime('%Y%m%d')

        # shares added by donation
        shares_donated_yesterday = Donation.where("date_format(created_at, '%Y%m%d') = ?", date_yesterday).sum(:shares_added)
        # shares removed by grant
        shares_granted_yesterday = CharityGrant.where("status = ?", "sent").where("date_format(created_at, '%Y%m%d') = ?", date_yesterday).sum(:shares_subtracted)
        donors_shares_total_beginning = Share.order("created_At DESC").last.share_total_end.to_f rescue 0.0
        share_total_end = (BigDecimal("#{donors_shares_total_beginning}") + BigDecimal("#{shares_donated_yesterday}") - BigDecimal("#{shares_granted_yesterday}")).round(SHARE_PRECISION)

        # get donation share price
        # givbalance / total_donor_shares_all_time
        preliminary_share_price = (BigDecimal("#{givbalance}") / BigDecimal("#{share_total_end}")).to_f

        preliminary_share_price = 100000.0 unless preliminary_share_price.finite?
        if preliminary_share_price.to_f.nan?
          preliminary_share_price = 100000.0
        end

        donation_share_price = (preliminary_share_price * 10).ceil / 10.0
        grant_share_price = (preliminary_share_price * 10).floor / 10.0

        new_record_share = Share.new(
                                     :stripe_balance => stripe_balance,
                                     :etrade_balance => etrade_balance,
                                     :share_total_beginning => donors_shares_total_beginning,
                                     :shares_added_by_donation => shares_donated_yesterday,
                                     :shares_subtracted_by_grants => shares_granted_yesterday,
                                     :share_total_end => share_total_end,
                                     :donation_price => donation_share_price,
                                     :grant_price => grant_share_price
                                    )
        if new_record_share.save
          puts "Share Price has been updated"
          JobMailer.success_compute(App.giv["email_support"], "compute_share_price").deliver
        else
          puts "ERROR"
        end
      end

      def grant_step_1
        stripe_balance = get_stripe_balance
        etrade_balance = get_etrade_balance
        givbalance = stripe_balance + etrade_balance
        
        endowments = Endowment.all

        endowments.each do |endowment|

          endowment_share_balance = BigDecimal("#{endowment.donations.sum(:shares_added)}") - BigDecimal("#{endowment.charity_grants.sum(:shares_subtracted)}")
          endowment_grant_shares = (BigDecimal("#{endowment_share_balance}") * BigDecimal("#{GIV_GRANT_AMOUNT}")).round(SHARE_PRECISION)
          charity_grant_shares = (BigDecimal("#{endowment_grant_shares}") / BigDecimal("#{endowment.charities.count}")).round(SHARE_PRECISION)

          endowment_donors = endowment.donations
          charities = endowment.charities

          charities.each do |charity|
            if charity.active.eql?("true")
{'fraction_non_alphanumeric': 0.09223568281938326, 'fraction_numerical': 0.007433920704845815, 'mean_word_length': 2.6934417895271987, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2224009', 'n_tokens_mistral': 2322, 'n_tokens_neox': 2204, 'n_words': 309}