/*******************************************************************************
***  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 "AccountingErrors.h"
using std::exception;
using drn::accounting::AccountError;
using drn::accounting::TransactionError;
using drn::foundation::Error;

#include <pecunia/Money.h>
#include <QObject>
using ::QObject;
#include <QString>
using ::QString;

#include "Account.h"
using drn::accounting::AccountCode;
using drn::accounting::presentationText;
#include <foundation/Optional.hpp>
using drn::foundation::Optional;
#include "Transaction.h"
using drn::accounting::Transaction;
using drn::accounting::TransactionNumber;


AccountError::AccountError(const AccountCode& ac, const QString& message, const exception& root) :
	Error{
		QObject::tr("The account, %1, is invalid. %2").arg(presentationText(ac)).arg(message),
		root
	}
{}

TransactionError::TransactionError(
	const Optional<TransactionNumber>& tn,
	const Transaction& t,
	const QString& message,
	const exception& root
) :
	Error{
		QObject::tr(
			"The transaction occurring on %1 #%2 for %3 which debits account %4 and credits "
				"account %5 is invalid. %6"
		)
			.arg(t.occurredOn().toString())
			.arg(tn.hasValue() ? presentationText(*tn) : QObject::tr("Not-Posted"))
			.arg(QString::fromStdString(to_string(t.amount())))
			.arg(presentationText(t.debiting()))
			.arg(presentationText(t.crediting()))
			.arg(message),
		root
	}
{}
