Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'csv'
- class Invoice
- attr_accessor :type, :partner, :account_number, :gross_amount, :net_amount, :due_date
- def initialize(type, partner, account_number, gross_amount, net_amount, due_date)
- @type = type
- @partner = partner
- @account_number = account_number.to_s.strip
- @gross_amount = gross_amount.to_f
- @net_amount = net_amount.to_f
- @due_date = due_date.strip
- end
- def to_csv
- ["Bejövő Számla", partner, account_number, gross_amount.round(2), net_amount.round(2), due_date]
- end
- end
- class Bank
- attr_accessor :interest_rate, :transaction_fee_min, :transaction_fee_max,
- :global_tax_min, :global_tax_max
- def initialize(interest_rate=0.05, transaction_fee_min=0.1,
- transaction_fee_max=0.25, global_tax_min=0.01,
- global_tax_max=0.02)
- @interest_rate = interest_rate.to_f
- @transaction_fee_min = transaction_fee_min.to_f
- @transaction_fee_max = transaction_fee_max.to_f
- @global_tax_min = global_tax_min.to_f
- @global_tax_max = global_tax_max.to_f
- end
- def calculate_global_tax(amount, min_tax=0.01, max_tax=0.02)
- tax = amount * [min_tax, max_tax].sample
- [amount + tax]
- end
- def apply_transaction_fee(amount, min_fee=0.1, max_fee=0.25)
- fee = amount * [min_fee, max_fee].sample
- fee += (fee < 0 ? 0 : fee)
- [amount + fee]
- end
- end
- class PaymentEvent
- attr_accessor :invoice, :date, :amount, :taxes, :fees
- def initialize(invoice, date, amount)
- self.invoice = invoice
- self.date = date
- self.amount = amount
- taxes = Bank.new.global_tax_min * amount + (Bank.new.global_tax_max - Bank.new.global_tax_min) * amount.sample
- fees = Bank.new.transaction_fee_min * amount + (Bank.new.transaction_fee_max - Bank.new.transaction_fee_min) * amount.sample
- self.taxes = taxes.round(2)
- self.feeds = fees.round(2)
- end
- def benefit
- # Calculate net gain considering fees and taxes
- benefit = amount - (fees + taxes)
- benefit > 0 ? benefit : 0
- end
- def to_s
- "#{invoice.partner} #{invoice.account_number} on #{date}"
- end
- end
- def main
- filename = "invoices.csv"
- banks = {
- 'Bank1' => Bank.new,
- # Add other banks here with their respective parameters
- }
- csv_data = CSV.read(filename)
- invoices = []
- csv_data.each do |row|
- next if row.empty?
- type, partner, account_number, gross_amount, net_amount, due_date = row
- invoice = Invoice.new(type, partner.to_s.strip, account_number.strip,
- gross_amount.to_f, net_amount.to_f, due_date.strip)
- invoices << invoice unless invoice.nil?
- end
- # Generate payment events
- events = []
- banks.each do |bank_name, bank|
- incoming_events = invoices.select { |inv| inv.type == 'Bejövő Számla' }
- .map do |inv|
- date = Time.now + (due_date_diff(inv))
- amount = inv.net_amount * [bank.global_tax_min, bank.global_tax_max].sample
- PaymentEvent.new(inv, date, amount)
- end
- events << ... # Continue generating events for each bank and invoice type
- # Priority queue based on event benefit and time
- priority_queue = []
- initial_balance = calculate_initial_balance(invoices)
- progress = 0.0
- total_events = events.size
- while !priority_queue.empty?
- event = priority_queue.shift
- process_event(event, initial_balance)
- progress += (event.benefit / total_events) * 100
- puts "Processed #{event.to_s}, Progress: #{progress.round(2)}%"
- end
- # Calculate and display final balance
- final_balance = initial_balance + sum_of_benefits
- puts "\nFinal Balance: #{final_balance.round(2)}"
- end
- def due_date_diff(inv)
- inv.net_amount * 0.05 # Example based on typical due dates, adjust as needed
- end
- def calculate_initial_balance(invoices)
- incoming_deposits = invoices.select { |inv| inv.type == 'Bejövő Számla' }
- .sum(&:net_amount)
- return incoming_deposits
- end
- def process_event(event, balance)
- amount = event.amount
- date = event.date
- # Apply taxes and fees
- taxed_amount = Bank.new.apply_global_tax(amount,
- Bank.new.global_tax_min, Bank.new.global_tax_max)
- fee_amount = Bank.new.apply_transaction_fee(taxed_amount,
- Bank.new.transaction_fee_min, Bank.new.transaction_fee_max)
- net_amount = (amount - fee_amount)
- balance += net_amount
- end
- # Initialize main loop and process each event based on priority
- 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.
- ### Explanation of Key Components:
- - **Data Structures**: Invoice, Bank, PaymentEvent classes model the problem's core entities.
- - **CSV Parsing**: Converts CSV data into objects representing invoices.
- - **Payment Events**: Each event represents a potential action (withdraw or pay) with associated fees and taxes.
- - **Priority Queue**: Manages events to process those yielding the highest benefit first.
- - **Progress Tracking**: Ensures users are informed about processing status.
- This approach efficiently manages the complexity of multiple parameters, ensuring optimal financial decisions while minimizing computational resources.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement