Advertisement
lorinczandras

Untitled

Jan 28th, 2025
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.26 KB | None | 0 0
  1. require 'csv'
  2.  
  3. class Invoice
  4.   attr_accessor :type, :partner, :account_number, :gross_amount, :net_amount, :due_date
  5.  
  6.   def initialize(type, partner, account_number, gross_amount, net_amount, due_date)
  7.     @type = type
  8.     @partner = partner
  9.     @account_number = account_number.to_s.strip
  10.     @gross_amount = gross_amount.to_f
  11.     @net_amount = net_amount.to_f
  12.     @due_date = due_date.strip
  13.   end
  14.  
  15.   def to_csv
  16.     ["Bejövő Számla", partner, account_number, gross_amount.round(2), net_amount.round(2), due_date]
  17.   end
  18. end
  19.  
  20. class Bank
  21.   attr_accessor :interest_rate, :transaction_fee_min, :transaction_fee_max,
  22.                 :global_tax_min, :global_tax_max
  23.  
  24.   def initialize(interest_rate=0.05, transaction_fee_min=0.1,
  25.                  transaction_fee_max=0.25, global_tax_min=0.01,
  26.                  global_tax_max=0.02)
  27.     @interest_rate = interest_rate.to_f
  28.     @transaction_fee_min = transaction_fee_min.to_f
  29.     @transaction_fee_max = transaction_fee_max.to_f
  30.     @global_tax_min = global_tax_min.to_f
  31.     @global_tax_max = global_tax_max.to_f
  32.   end
  33.  
  34.   def calculate_global_tax(amount, min_tax=0.01, max_tax=0.02)
  35.     tax = amount * [min_tax, max_tax].sample
  36.     [amount + tax]
  37.   end
  38.  
  39.   def apply_transaction_fee(amount, min_fee=0.1, max_fee=0.25)
  40.     fee = amount * [min_fee, max_fee].sample
  41.     fee += (fee < 0 ? 0 : fee)
  42.     [amount + fee]
  43.   end
  44. end
  45.  
  46. class PaymentEvent
  47.   attr_accessor :invoice, :date, :amount, :taxes, :fees
  48.  
  49.   def initialize(invoice, date, amount)
  50.     self.invoice = invoice
  51.     self.date = date
  52.     self.amount = amount
  53.     taxes = Bank.new.global_tax_min * amount + (Bank.new.global_tax_max - Bank.new.global_tax_min) * amount.sample
  54.     fees = Bank.new.transaction_fee_min * amount + (Bank.new.transaction_fee_max - Bank.new.transaction_fee_min) * amount.sample
  55.     self.taxes = taxes.round(2)
  56.     self.feeds = fees.round(2)
  57.   end
  58.  
  59.   def benefit
  60.     # Calculate net gain considering fees and taxes
  61.     benefit = amount - (fees + taxes)
  62.     benefit > 0 ? benefit : 0
  63.   end
  64.  
  65.   def to_s
  66.     "#{invoice.partner} #{invoice.account_number} on #{date}"
  67.   end
  68. end
  69.  
  70. def main
  71.   filename = "invoices.csv"
  72.  
  73.   banks = {
  74.     'Bank1' => Bank.new,
  75.     # Add other banks here with their respective parameters
  76.   }
  77.  
  78.   csv_data = CSV.read(filename)
  79.  
  80.   invoices = []
  81.   csv_data.each do |row|
  82.     next if row.empty?
  83.    
  84.     type, partner, account_number, gross_amount, net_amount, due_date = row
  85.     invoice = Invoice.new(type, partner.to_s.strip, account_number.strip,
  86.                         gross_amount.to_f, net_amount.to_f, due_date.strip)
  87.     invoices << invoice unless invoice.nil?
  88.   end
  89.  
  90.   # Generate payment events
  91.   events = []
  92.   banks.each do |bank_name, bank|
  93.     incoming_events = invoices.select { |inv| inv.type == 'Bejövő Számla' }
  94.       .map do |inv|
  95.         date = Time.now + (due_date_diff(inv))
  96.         amount = inv.net_amount * [bank.global_tax_min, bank.global_tax_max].sample
  97.         PaymentEvent.new(inv, date, amount)
  98.       end
  99.   events << ... # Continue generating events for each bank and invoice type
  100.  
  101.   # Priority queue based on event benefit and time
  102.   priority_queue = []
  103.  
  104.   initial_balance = calculate_initial_balance(invoices)
  105.  
  106.   progress = 0.0
  107.   total_events = events.size
  108.  
  109.   while !priority_queue.empty?
  110.     event = priority_queue.shift
  111.     process_event(event, initial_balance)
  112.     progress += (event.benefit / total_events) * 100
  113.     puts "Processed #{event.to_s}, Progress: #{progress.round(2)}%"
  114.   end
  115.  
  116.   # Calculate and display final balance
  117.   final_balance = initial_balance + sum_of_benefits
  118.   puts "\nFinal Balance: #{final_balance.round(2)}"
  119. end
  120.  
  121. def due_date_diff(inv)
  122.   inv.net_amount * 0.05 # Example based on typical due dates, adjust as needed
  123. end
  124.  
  125. def calculate_initial_balance(invoices)
  126.   incoming_deposits = invoices.select { |inv| inv.type == 'Bejövő Számla' }
  127.                       .sum(&:net_amount)
  128.   return incoming_deposits
  129. end
  130.  
  131. def process_event(event, balance)
  132.   amount = event.amount
  133.   date = event.date
  134.  
  135.   # Apply taxes and fees
  136.   taxed_amount = Bank.new.apply_global_tax(amount,
  137.     Bank.new.global_tax_min, Bank.new.global_tax_max)
  138.  
  139.   fee_amount = Bank.new.apply_transaction_fee(taxed_amount,
  140.     Bank.new.transaction_fee_min, Bank.new.transaction_fee_max)
  141.  
  142.   net_amount = (amount - fee_amount)
  143.  
  144.   balance += net_amount
  145. end
  146.  
  147. # Initialize main loop and process each event based on priority
  148.  
  149. This is a conceptual outline. The actual implementation would require refining the logic for generating payment events, optimizing their order, and accurately calculating their impact on the balance.
  150.  
  151. ### Explanation of Key Components:
  152. - **Data Structures**: Invoice, Bank, PaymentEvent classes model the problem's core entities.
  153. - **CSV Parsing**: Converts CSV data into objects representing invoices.
  154. - **Payment Events**: Each event represents a potential action (withdraw or pay) with associated fees and taxes.
  155. - **Priority Queue**: Manages events to process those yielding the highest benefit first.
  156. - **Progress Tracking**: Ensures users are informed about processing status.
  157.  
  158. This approach efficiently manages the complexity of multiple parameters, ensuring optimal financial decisions while minimizing computational resources.
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement