Expand tests and API code.

This commit is contained in:
James Cole
2018-02-18 10:31:15 +01:00
parent 6cda9f2900
commit eb0da038fb
28 changed files with 1171 additions and 172 deletions

View File

@@ -0,0 +1,88 @@
<?php
/**
* PiggyBankEventFactory.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use Log;
/**
* Class PiggyBankEventFactory
*/
class PiggyBankEventFactory
{
/**
* @param TransactionJournal $journal
* @param PiggyBank|null $piggyBank
*
* @return PiggyBankEvent|null
*/
public function create(TransactionJournal $journal, ?PiggyBank $piggyBank): ?PiggyBankEvent
{
if (is_null($piggyBank)) {
return null;
}
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
/** @var PiggyBankRepositoryInterface $piggyRepos */
$piggyRepos = app(PiggyBankRepositoryInterface::class);
$repository->setUser($journal->user);
$piggyRepos->setUser($journal->user);
// is a transfer?
if (!$repository->isTransfer($journal)) {
Log::info(sprintf('Will not connect %s #%d to a piggy bank.', $journal->transactionType->type, $journal->id));
return null;
}
// repetition exists?
$repetition = $piggyRepos->getRepetition($piggyBank, $journal->date);
if (null === $repetition->id) {
Log::error(sprintf('No piggy bank repetition on %s!', $journal->date->format('Y-m-d')));
return null;
}
// get the amount
$amount = $piggyRepos->getExactAmount($piggyBank, $repetition, $journal);
if (0 === bccomp($amount, '0')) {
Log::debug('Amount is zero, will not create event.');
return null;
}
// update amount
$piggyRepos->addAmountToRepetition($repetition, $amount);
$event = $piggyRepos->createEventWithJournal($piggyBank, $amount, $journal);
Log::debug(sprintf('Created piggy bank event #%d', $event->id));
return $event;
}
}

View File

@@ -46,9 +46,31 @@ use Illuminate\Support\Collection;
*/
class TransactionFactory
{
/** @var AccountRepositoryInterface */
private $accountRepository;
/** @var BudgetRepositoryInterface */
private $budgetRepository;
/** @var CategoryRepositoryInterface */
private $categoryRepository;
/** @var CurrencyRepositoryInterface */
private $currencyRepository;
/** @var JournalRepositoryInterface */
private $repository;
/** @var User */
private $user;
/**
* TransactionFactory constructor.
*/
public function __construct()
{
$this->repository = app(JournalRepositoryInterface::class);
$this->accountRepository = app(AccountRepositoryInterface::class);
$this->budgetRepository = app(BudgetRepositoryInterface::class);
$this->categoryRepository = app(CategoryRepositoryInterface::class);
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
}
/**
* @param array $data
*
@@ -68,12 +90,10 @@ class TransactionFactory
'foreign_currency_id' => $foreignCurrencyId,
'identifier' => $data['identifier'],
];
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$transaction = $repository->storeBasicTransaction($values);
$transaction = $this->repository->storeBasicTransaction($values);
// todo: add budget, category, etc.
// todo link budget, category
return $transaction;
}
@@ -94,21 +114,23 @@ class TransactionFactory
$foreignCurrency = $this->findCurrency($data['foreign_currency_id'], $data['foreign_currency_code']);
$budget = $this->findBudget($data['budget_id'], $data['budget_name']);
$category = $this->findCategory($data['category_id'], $data['category_name']);
$description = $journal->description === $data['description'] ? null : $data['description'];
// type of source account depends on journal type:
$sourceType = $this->accountType($journal, 'source');
$sourceAccount = $this->findAccount($sourceType, $data['source_account_id'], $data['source_account_name']);
$sourceAccount = $this->findAccount($sourceType, $data['source_id'], $data['source_name']);
$sourceFa = is_null($data['foreign_amount']) ? null : app('steam')->negative(strval($data['foreign_amount']));
// same for destination account:
$destinationType = $this->accountType($journal, 'destination');
$destinationAccount = $this->findAccount($destinationType, $data['destination_account_id'], $data['destination_account_name']);
$destinationAccount = $this->findAccount($destinationType, $data['destination_id'], $data['destination_name']);
$destinationFa = is_null($data['foreign_amount']) ? null : app('steam')->positive(strval($data['foreign_amount']));
// first make a "negative" (source) transaction based on the data in the array.
$sourceTransactionData = [
'description' => $journal->description === $data['description'] ? null : $data['description'],
'description' => $description,
'amount' => app('steam')->negative(strval($data['amount'])),
'foreign_amount' => is_null($data['foreign_amount']) ? null : app('steam')->negative(strval($data['foreign_amount'])),
'foreign_amount' => $sourceFa,
'currency' => $currency,
'foreign_currency' => $foreignCurrency,
'budget' => $budget,
@@ -124,7 +146,7 @@ class TransactionFactory
$destTransactionData = [
'description' => $sourceTransactionData['description'],
'amount' => app('steam')->positive(strval($data['amount'])),
'foreign_amount' => is_null($data['foreign_amount']) ? null : app('steam')->positive(strval($data['foreign_amount'])),
'foreign_amount' => $destinationFa,
'currency' => $currency,
'foreign_currency' => $foreignCurrency,
'budget' => $budget,
@@ -136,8 +158,6 @@ class TransactionFactory
];
$dest = $this->create($destTransactionData);
// todo link budget, category
return new Collection([$source, $dest]);
}
@@ -147,6 +167,11 @@ class TransactionFactory
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
$this->accountRepository->setUser($user);
$this->budgetRepository->setUser($user);
$this->categoryRepository->setUser($user);
$this->currencyRepository->setUser($user);
}
/**
@@ -195,45 +220,42 @@ class TransactionFactory
{
$accountId = intval($accountId);
$accountName = strval($accountName);
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($this->user);
switch ($expectedType) {
case AccountType::ASSET:
if ($accountId > 0) {
// must be able to find it based on ID. Validator should catch invalid ID's.
return $repository->findNull($accountId);
return $this->accountRepository->findNull($accountId);
}
// alternatively, return by name. Validator should catch invalid names.
return $repository->findByName($accountName, [AccountType::ASSET]);
return $this->accountRepository->findByName($accountName, [AccountType::ASSET]);
break;
case AccountType::EXPENSE:
if ($accountId > 0) {
// must be able to find it based on ID. Validator should catch invalid ID's.
return $repository->findNull($accountId);
return $this->accountRepository->findNull($accountId);
}
if (strlen($accountName) > 0) {
// alternatively, return by name. Validator should catch invalid names.
return $repository->findByName($accountName, [AccountType::EXPENSE]);
return $this->accountRepository->findByName($accountName, [AccountType::EXPENSE]);
}
// return cash account:
return $repository->getCashAccount();
return $this->accountRepository->getCashAccount();
break;
case AccountType::REVENUE:
if ($accountId > 0) {
// must be able to find it based on ID. Validator should catch invalid ID's.
return $repository->findNull($accountId);
return $this->accountRepository->findNull($accountId);
}
if (strlen($accountName) > 0) {
// alternatively, return by name. Validator should catch invalid names.
return $repository->findByName($accountName, [AccountType::REVENUE]);
return $this->accountRepository->findByName($accountName, [AccountType::REVENUE]);
}
// return cash account:
return $repository->getCashAccount();
return $this->accountRepository->getCashAccount();
default:
throw new FireflyException(sprintf('Cannot find account of type "%s".', $expectedType));
@@ -254,21 +276,17 @@ class TransactionFactory
if (strlen($budgetName) === 0 && $budgetId === 0) {
return null;
}
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$budget = null;
$repository->setUser($this->user);
// first by ID:
if ($budgetId > 0) {
$budget = $repository->findNull($budgetId);
$budget = $this->budgetRepository->findNull($budgetId);
if (!is_null($budget)) {
return $budget;
}
}
if (strlen($budgetName) > 0) {
$budget = $repository->findByName($budgetName);
$budget = $this->budgetRepository->findByName($budgetName);
if (!is_null($budget)) {
return $budget;
}
@@ -291,25 +309,21 @@ class TransactionFactory
if (strlen($categoryName) === 0 && $categoryId === 0) {
return null;
}
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$category = null;
$repository->setUser($this->user);
// first by ID:
if ($categoryId > 0) {
$category = $repository->findNull($categoryId);
$category = $this->categoryRepository->findNull($categoryId);
if (!is_null($category)) {
return $category;
}
}
if (strlen($categoryName) > 0) {
$category = $repository->findByName($categoryName);
$category = $this->categoryRepository->findByName($categoryName);
if (!is_null($category)) {
return $category;
}
// create it?
die('create category');
}
return null;
@@ -329,21 +343,17 @@ class TransactionFactory
if (strlen($currencyCode) === 0 && intval($currencyId) === 0) {
return null;
}
/** @var CurrencyRepositoryInterface $repository */
$repository = app(CurrencyRepositoryInterface::class);
$currency = null;
$repository->setUser($this->user);
// first by ID:
if ($currencyId > 0) {
$currency = $repository->findNull($currencyId);
$currency = $this->currencyRepository->findNull($currencyId);
if (!is_null($currency)) {
return $currency;
}
}
// then by code:
if (strlen($currencyCode) > 0) {
$currency = $repository->findByCodeNull($currencyCode);
$currency = $this->currencyRepository->findByCodeNull($currencyCode);
if (!is_null($currency)) {
return $currency;
}

View File

@@ -23,12 +23,15 @@ declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Events\StoredTransactionJournal;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Bill;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Repositories\TransactionType\TransactionTypeRepositoryInterface;
use FireflyIII\User;
@@ -37,6 +40,14 @@ use FireflyIII\User;
*/
class TransactionJournalFactory
{
/** @var BillRepositoryInterface */
private $billRepository;
/** @var PiggyBankRepositoryInterface */
private $piggyRepository;
/** @var JournalRepositoryInterface */
private $repository;
/** @var TransactionTypeRepositoryInterface */
private $ttRepository;
/** @var User */
private $user;
@@ -45,6 +56,10 @@ class TransactionJournalFactory
*/
public function __construct()
{
$this->repository = app(JournalRepositoryInterface::class);
$this->billRepository = app(BillRepositoryInterface::class);
$this->piggyRepository = app(PiggyBankRepositoryInterface::class);
$this->ttRepository = app(TransactionTypeRepositoryInterface::class);
}
@@ -60,11 +75,9 @@ class TransactionJournalFactory
{
$type = $this->findTransactionType($data['type']);
$bill = $this->findBill($data['bill_id'], $data['bill_name']);
$defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user());
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$values = [
$piggyBank = $this->findPiggyBank($data['piggy_bank_id'], $data['piggy_bank_name']);
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
$values = [
'user_id' => $data['user'],
'transaction_type_id' => $type->id,
'bill_id' => is_null($bill) ? null : $bill->id,
@@ -76,20 +89,28 @@ class TransactionJournalFactory
'completed' => 0,
];
$journal = $repository->storeBasic($values);
$journal = $this->repository->storeBasic($values);
// todo link other stuff to journal (meta-data etc). tags
// todo
// start creating transactions:
$factory = app(TransactionFactory::class);
$factory->setUser($this->user);
/** @var array $trData */
foreach ($data['transactions'] as $trData) {
$factory = new TransactionFactory();
$factory->setUser($this->user);
$trData['reconciled'] = $data['reconciled'] ?? false;
$factory->createPair($journal, $trData);
}
$repository->markCompleted($journal);
$this->repository->markCompleted($journal);
// link piggy bank:
if ($type->type === TransactionType::TRANSFER && !is_null($piggyBank)) {
/** @var PiggyBankEventFactory $factory */
$factory = app(PiggyBankEventFactory::class);
$factory->create($journal, $piggyBank);
}
return $journal;
}
@@ -102,6 +123,9 @@ class TransactionJournalFactory
public function setUser(User $user): void
{
$this->user = $user;
$this->repository->setUser($user);
$this->billRepository->setUser($user);
$this->piggyRepository->setUser($user);
}
/**
@@ -117,14 +141,10 @@ class TransactionJournalFactory
if (strlen($billName) === 0 && $billId === 0) {
return null;
}
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class);
$repository->setUser($this->user);
// first find by ID:
if ($billId > 0) {
/** @var Bill $bill */
$bill = $repository->find($billId);
$bill = $this->billRepository->find($billId);
if (!is_null($bill)) {
return $bill;
}
@@ -132,7 +152,7 @@ class TransactionJournalFactory
// then find by name:
if (strlen($billName) > 0) {
$bill = $repository->findByName($billName);
$bill = $this->billRepository->findByName($billName);
if (!is_null($bill)) {
return $bill;
}
@@ -141,6 +161,41 @@ class TransactionJournalFactory
return null;
}
/**
* Find the given bill based on the ID or the name. ID takes precedence over the name.
*
* @param int $piggyBankId
* @param string $piggyBankName
*
* @return PiggyBank|null
*/
protected function findPiggyBank(int $piggyBankId, string $piggyBankName): ?PiggyBank
{
if (strlen($piggyBankName) === 0 && $piggyBankId === 0) {
return null;
}
// first find by ID:
if ($piggyBankId > 0) {
/** @var PiggyBank $piggyBank */
$piggyBank = $this->piggyRepository->find($piggyBankId);
if (!is_null($piggyBank)) {
return $piggyBank;
}
}
// then find by name:
if (strlen($piggyBankName) > 0) {
/** @var PiggyBank $piggyBank */
$piggyBank = $this->piggyRepository->findByName($piggyBankName);
if (!is_null($piggyBank)) {
return $piggyBank;
}
}
return null;
}
/**
* Get the transaction type. Since this is mandatory, will throw an exception when nothing comes up. Will always
* use TransactionType repository.
@@ -150,11 +205,9 @@ class TransactionJournalFactory
* @return TransactionType
* @throws FireflyException
*/
private function findTransactionType(string $type): TransactionType
protected function findTransactionType(string $type): TransactionType
{
/** @var TransactionTypeRepositoryInterface $repository */
$repository = app(TransactionTypeRepositoryInterface::class);
$transactionType = $repository->findByType($type);
$transactionType = $this->ttRepository->findByType($type);
if (is_null($transactionType)) {
throw new FireflyException(sprintf('Could not find transaction type for "%s"', $type));
}