/*******************************************************************************
***  This file is part of Dux Rei Nummariae.                                 ***
***                                                                          ***
***  Copyright (C) 2017, 2018, 2019, 2020, 2021                              ***
***  John Schneiderman <Licensing _AT_ Schneiderman _DOT_ me>                ***
***                                                                          ***
***  This program is free software: you can redistribute it and/or modify it ***
***  under the terms of the GNU Affero General Public License as published   ***
***  by the Free Software Foundation, either version 3 of the License, or    ***
***  (at your option) any later version.                                     ***
***                                                                          ***
***  This program is distributed in the hope that it will be useful, but     ***
***  WITHOUT ANY WARRANTY; without even the implied warranty of              ***
***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                    ***
***  See the GNU Affero General Public License for more details.             ***
***                                                                          ***
***  You should have received a copy of the GNU Affero General Public License***
***  along with this program. If not, see <http://www.gnu.org/licenses/>.    ***
*******************************************************************************/
#include "Reconciliation.h"
using pecunia::currency::Money;
using ::QDate;
using drn::foundation::Optional;
using drn::banking::BankAccount;
using drn::banking::ReconciledBankAccount;
using drn::banking::ReconciledBankAccounts;

#include <pecunia/Codes.h>
using pecunia::currency::Iso4217Codes;

#include <algorithm>
using std::find_if;
#include <utility>
using std::move;

#include <accounting/AccountNumber.h>
using drn::accounting::AccountNumber;


//{ ReconciledBankAccount

ReconciledBankAccount::ReconciledBankAccount(
	BankAccount bankAccount,
	const Iso4217Codes& currency
) :
	bankAccount_{move(bankAccount)},
	balance_{currency},
	reconciledOn_{}
{}

ReconciledBankAccount::ReconciledBankAccount(
	BankAccount bankAccount,
	Money balance,
	QDate reconciledOn
) :
	bankAccount_{move(bankAccount)},
	balance_{move(balance)},
	reconciledOn_{move(reconciledOn)}
{}

const BankAccount& ReconciledBankAccount::bankAccount() const noexcept
{
	return this->bankAccount_;
}

const Money& ReconciledBankAccount::balance() const noexcept
{
	return this->balance_;
}

const Optional<QDate>& ReconciledBankAccount::reconciledOn() const noexcept
{
	return this->reconciledOn_;
}

void ReconciledBankAccount::reconcile(const QDate& /*reconciledOn*/, const Money& /*balance*/)
{
}

//}

//{ ReconciledBankAccounts

Optional<ReconciledBankAccount> ReconciledBankAccounts::find(
	const BankName& bn,
	const AccountNumber& an
) const
{
	const auto account{
		find_if(
			this->cbegin(),
			this->cend(),
			[&bn, &an] (const auto& bankReconciled)
			{
				return bankReconciled.first.bank_ == bn
						&& bankReconciled.first.account_.code_ == an;
			}
		)
	};

	if (account == this->cend())
		return {};
	return account->second;
}

//}
