Created
February 6, 2017 16:39
-
-
Save hindenbug/efbffc3628592046f0dc5389dedc2fd9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "json" | |
| require "rspec" | |
| require "byebug" | |
| class Reconciler | |
| attr_accessor :invoices, :credit_lines, :max_finance_credit, :total_debtors_invoices, :customer_credits | |
| def financed_amount | |
| @total_debtors_invoices = @invoices.map {|x| x[:amount] }.reduce(:+) | |
| if @total_debtors_invoices >= @credit_lines[:customer] | |
| @credit_lines[:customer] | |
| else | |
| @total_debtors_invoices | |
| end | |
| end | |
| def invoice_finance_amounts | |
| result = Hash.new(0) | |
| @invoices.each do |invoice| | |
| result[invoice[:number]] += calculate_max_financed_invoice_amount_for(invoice[:debtor], invoice[:amount]) | |
| end | |
| result | |
| end | |
| def remaining_customer_credit | |
| @customer_credits = @credit_lines[:customer] - financed_amount | |
| end | |
| def remaining_debtor_credit | |
| end | |
| def fetch_invoices | |
| data = File.read('invoices.json') | |
| @invoices = JSON.parse(data, symbolize_names: true) | |
| end | |
| def fetch_credit_lines | |
| data = File.read('credit-lines.json') | |
| @credit_lines = JSON.parse(data, symbolize_names: true) | |
| end | |
| private | |
| def calculate_max_financed_invoice_amount_for(debtor, invoice_amount) | |
| if invoice_amount >= @credit_lines[:debtors][debtor.to_sym] | |
| finance_amount = @credit_lines[:debtors][debtor.to_sym] | |
| @credit_lines[:debtors][debtor.to_sym] = 0 | |
| else | |
| finance_amount = invoice_amount | |
| @credit_lines[:debtors][debtor.to_sym] -= finance_amount | |
| end | |
| finance_amount | |
| end | |
| end | |
| describe Reconciler do | |
| let(:reconciler) { Reconciler.new } | |
| describe "#fetch_invoices" do | |
| let(:invoice_data) { | |
| [ | |
| { | |
| debtor: "fritz-kola", | |
| number: "fk1", | |
| amount: 16000, | |
| currency: "EUR" | |
| } | |
| ] | |
| } | |
| it "sets the invoices attribute" do | |
| reconciler.fetch_invoices | |
| expect(reconciler.invoices).to_not be_empty | |
| expect(reconciler.invoices).to include(invoice_data[0]) | |
| end | |
| it "should have debtors data" do | |
| reconciler.fetch_invoices | |
| expect(reconciler.invoices[0]).to have_key(:debtor) | |
| expect(reconciler.invoices[0]).to have_key(:amount) | |
| end | |
| end | |
| describe "#fetch_credit_lines" do | |
| let(:credit_line_data) { | |
| { | |
| "customer": 40000, | |
| "debtors": { | |
| "fritz-kola": 20000, | |
| "Tommi's Burger Joint": 20000, | |
| "Fräulein Frost": 10000, | |
| "Mc Donalds": 10000 | |
| } | |
| } | |
| } | |
| it "sets the invoices attribute" do | |
| reconciler.fetch_credit_lines | |
| expect(reconciler.credit_lines).to_not be_empty | |
| expect(reconciler.credit_lines).to eq(credit_line_data) | |
| end | |
| it "should have debtors data" do | |
| reconciler.fetch_credit_lines | |
| expect(reconciler.credit_lines).to have_key(:customer) | |
| expect(reconciler.credit_lines).to have_key(:debtors) | |
| end | |
| end | |
| describe "#financed_amount" do | |
| context "when total debtors credits exceed the customer credit limit" do | |
| it "gets and sets the max finance amount from the credits line data" do | |
| reconciler.fetch_invoices | |
| reconciler.fetch_credit_lines | |
| expect(reconciler.financed_amount).to eq(40000) | |
| end | |
| end | |
| end | |
| describe "#remamining customer credit" do | |
| it "should set the remaining customer credit after deductions" do | |
| reconciler.fetch_invoices | |
| reconciler.fetch_credit_lines | |
| expect(reconciler.remaining_customer_credit).to eq(0) | |
| end | |
| end | |
| describe "invoice_finance_amounts" do | |
| it "should return all invoice's, max finance amount" do | |
| reconciler.fetch_invoices | |
| reconciler.fetch_credit_lines | |
| expected_result = { | |
| "fk1" => 16000, | |
| "tbj1" => 15000, | |
| "fk2" => 4000, | |
| "ff1" => 5000, | |
| "tbj2" => 0 | |
| } | |
| expect(reconciler.invoice_finance_amounts).to eq(expected_result) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment