mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Various code clean up.
This commit is contained in:
@@ -163,7 +163,7 @@ class MonthReportGenerator implements ReportGeneratorInterface
|
||||
/** @var Transaction $journal */
|
||||
foreach ($journals as $transaction) {
|
||||
$transaction->before = $startBalance;
|
||||
$transactionAmount = $transaction->transaction_amount;
|
||||
$transactionAmount = $transaction->transaction_amount;
|
||||
|
||||
if ($currency->id === $transaction->foreign_currency_id) {
|
||||
$transactionAmount = $transaction->transaction_foreign_amount;
|
||||
|
@@ -120,7 +120,7 @@ class CategoryController extends Controller
|
||||
foreach ($categories as $category) {
|
||||
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $start, $end);
|
||||
if (bccomp($spent, '0') !== 0) {
|
||||
$report[$category->id] = ['name' => $category->name, 'spent' => $spent,'id' => $category->id];
|
||||
$report[$category->id] = ['name' => $category->name, 'spent' => $spent, 'id' => $category->id];
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -62,8 +62,6 @@ class ImportJournal
|
||||
private $modifiers = [];
|
||||
/** @var array */
|
||||
private $tags = [];
|
||||
/** @var string */
|
||||
private $transactionType = '';
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
|
@@ -1,157 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* ImportTransaction.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
|
||||
*
|
||||
* See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Import\Object;
|
||||
|
||||
|
||||
use FireflyIII\Import\Converter\ConverterInterface;
|
||||
use Steam;
|
||||
|
||||
class ImportTransaction
|
||||
{
|
||||
/** @var string */
|
||||
private $amount;
|
||||
|
||||
/** @var ImportCurrency */
|
||||
private $currency;
|
||||
|
||||
/** @var string */
|
||||
private $date;
|
||||
|
||||
/** @var string */
|
||||
private $description;
|
||||
private $modifiers = [];
|
||||
/** @var bool */
|
||||
private $positive = true;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->currency = new ImportCurrency;
|
||||
}
|
||||
|
||||
public function addToModifier(array $modifier)
|
||||
{
|
||||
$this->modifiers[] = $modifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAmount(): string
|
||||
{
|
||||
// use converter:
|
||||
$this->amount = strval($this->parseAmount());
|
||||
|
||||
|
||||
// also apply modifiers:
|
||||
$this->amount = Steam::positive($this->amount);
|
||||
|
||||
// Can handle ING
|
||||
foreach ($this->modifiers as $modifier) {
|
||||
$class = sprintf('FireflyIII\Import\Converter\%s', config(sprintf('csv.import_roles.%s.converter', $modifier['role'])));
|
||||
/** @var ConverterInterface $converter */
|
||||
$converter = app($class);
|
||||
if ($converter->convert($modifier['value']) === -1) {
|
||||
$this->amount = Steam::negative($this->amount);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $amount
|
||||
*/
|
||||
public function setAmount(string $amount)
|
||||
{
|
||||
$this->amount = $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ImportCurrency
|
||||
*/
|
||||
public function getCurrency(): ImportCurrency
|
||||
{
|
||||
return $this->currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportCurrency $currency
|
||||
*/
|
||||
public function setCurrency(ImportCurrency $currency)
|
||||
{
|
||||
$this->currency = $currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
*/
|
||||
public function setDate(string $date)
|
||||
{
|
||||
$this->date = $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $description
|
||||
*/
|
||||
public function setDescription(string $description)
|
||||
{
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $positive
|
||||
*/
|
||||
public function setPositive(bool $positive)
|
||||
{
|
||||
$this->positive = $positive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
|
||||
* - Jamie Zawinski
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
private function parseAmount()
|
||||
{
|
||||
$value = $this->amount;
|
||||
$len = strlen($value);
|
||||
$decimalPosition = $len - 3;
|
||||
$decimal = null;
|
||||
|
||||
if (($len > 2 && $value{$decimalPosition} == '.') || ($len > 2 && strpos($value, '.') > $decimalPosition)) {
|
||||
$decimal = '.';
|
||||
}
|
||||
if ($len > 2 && $value{$decimalPosition} == ',') {
|
||||
$decimal = ',';
|
||||
}
|
||||
|
||||
// if decimal is dot, replace all comma's and spaces with nothing. then parse as float (round to 4 pos)
|
||||
if ($decimal === '.') {
|
||||
$search = [',', ' '];
|
||||
$value = str_replace($search, '', $value);
|
||||
}
|
||||
if ($decimal === ',') {
|
||||
$search = ['.', ' '];
|
||||
$value = str_replace($search, '', $value);
|
||||
$value = str_replace(',', '.', $value);
|
||||
}
|
||||
if (is_null($decimal)) {
|
||||
// replace all:
|
||||
$search = ['.', ' ', ','];
|
||||
$value = str_replace($search, '', $value);
|
||||
}
|
||||
|
||||
return round(floatval($value), 12);
|
||||
}
|
||||
|
||||
}
|
@@ -160,42 +160,19 @@ class CategoryRepository implements CategoryRepositoryInterface
|
||||
*/
|
||||
public function lastUseDate(Category $category, Collection $accounts): Carbon
|
||||
{
|
||||
$last = null;
|
||||
$last = new Carbon('1900-01-01');
|
||||
$lastJournalDate = $this->getLastJournalDate($category, $accounts);
|
||||
|
||||
/** @var TransactionJournal $first */
|
||||
$lastJournalQuery = $category->transactionJournals()->orderBy('date', 'DESC');
|
||||
|
||||
if ($accounts->count() > 0) {
|
||||
// filter journals:
|
||||
$ids = $accounts->pluck('id')->toArray();
|
||||
$lastJournalQuery->leftJoin('transactions as t', 't.transaction_journal_id', '=', 'transaction_journals.id');
|
||||
$lastJournalQuery->whereIn('t.account_id', $ids);
|
||||
if ($lastJournalDate->year !== 1900) {
|
||||
$last = clone $lastJournalDate;
|
||||
unset($lastJournalDate);
|
||||
}
|
||||
|
||||
$lastJournal = $lastJournalQuery->first(['transaction_journals.*']);
|
||||
$lastTransactionDate = $this->getLastTransactionDate($category, $accounts);
|
||||
|
||||
if ($lastJournal) {
|
||||
$last = $lastJournal->date;
|
||||
}
|
||||
|
||||
// check transactions:
|
||||
|
||||
$lastTransactionQuery = $category->transactions()
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
->orderBy('transaction_journals.date', 'DESC');
|
||||
if ($accounts->count() > 0) {
|
||||
// filter journals:
|
||||
$ids = $accounts->pluck('id')->toArray();
|
||||
$lastTransactionQuery->whereIn('transactions.account_id', $ids);
|
||||
}
|
||||
|
||||
$lastTransaction = $lastTransactionQuery->first(['transaction_journals.*']);
|
||||
if (!is_null($lastTransaction) && ((!is_null($last) && $lastTransaction->date < $last) || is_null($last))) {
|
||||
$last = new Carbon($lastTransaction->date);
|
||||
}
|
||||
|
||||
if (is_null($last)) {
|
||||
return new Carbon('1900-01-01');
|
||||
if ($lastTransactionDate->year !== 1900 && $lastTransactionDate < $last) {
|
||||
$last = clone $lastTransactionDate;
|
||||
unset($lastTransactionDate);
|
||||
}
|
||||
|
||||
return $last;
|
||||
@@ -479,4 +456,53 @@ class CategoryRepository implements CategoryRepositoryInterface
|
||||
return $category;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Category $category
|
||||
* @param Collection $accounts
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
private function getLastJournalDate(Category $category, Collection $accounts): Carbon
|
||||
{
|
||||
$query = $category->transactionJournals()->orderBy('date', 'DESC');
|
||||
|
||||
if ($accounts->count() > 0) {
|
||||
$query->leftJoin('transactions as t', 't.transaction_journal_id', '=', 'transaction_journals.id');
|
||||
$query->whereIn('t.account_id', $accounts->pluck('id')->toArray());
|
||||
}
|
||||
|
||||
$result = $query->first(['transaction_journals.*']);
|
||||
|
||||
if (!is_null($result)) {
|
||||
return $result->date;
|
||||
}
|
||||
|
||||
return new Carbon('1900-01-01');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Category $category
|
||||
* @param Collection $accounts
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
private function getLastTransactionDate(Category $category, Collection $accounts): Carbon
|
||||
{
|
||||
// check transactions:
|
||||
$query = $category->transactions()
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
->orderBy('transaction_journals.date', 'DESC');
|
||||
if ($accounts->count() > 0) {
|
||||
// filter journals:
|
||||
$query->whereIn('transactions.account_id', $accounts->pluck('id')->toArray());
|
||||
}
|
||||
|
||||
$lastTransaction = $query->first(['transaction_journals.*']);
|
||||
if (!is_null($lastTransaction)) {
|
||||
return new Carbon($lastTransaction->date);
|
||||
}
|
||||
|
||||
return new Carbon('1900-01-01');
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -42,17 +42,17 @@ interface ImportJobRepositoryInterface
|
||||
* @param ImportJob $job
|
||||
* @param UploadedFile $file
|
||||
*
|
||||
* @return mixed
|
||||
* @return bool
|
||||
*/
|
||||
public function processFile(ImportJob $job, UploadedFile $file): bool;
|
||||
public function processConfiguration(ImportJob $job, UploadedFile $file): bool;
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
* @param UploadedFile $file
|
||||
*
|
||||
* @return bool
|
||||
* @return mixed
|
||||
*/
|
||||
public function processConfiguration(ImportJob $job, UploadedFile $file): bool;
|
||||
public function processFile(ImportJob $job, UploadedFile $file): bool;
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
|
@@ -26,6 +26,7 @@ use Log;
|
||||
class Initial implements ConfigurationInterface
|
||||
{
|
||||
private $job;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
@@ -106,6 +107,21 @@ class Initial implements ConfigurationInterface
|
||||
Log::error('Could not find anything for csv_import_account.', ['id' => $importId]);
|
||||
}
|
||||
|
||||
$config = $this->storeSpecifics($data, $config);
|
||||
$this->job->configuration = $config;
|
||||
$this->job->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param array $config
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function storeSpecifics(array $data, array $config): array
|
||||
{
|
||||
// loop specifics.
|
||||
if (isset($data['specifics']) && is_array($data['specifics'])) {
|
||||
foreach ($data['specifics'] as $name => $enabled) {
|
||||
@@ -116,9 +132,7 @@ class Initial implements ConfigurationInterface
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->job->configuration = $config;
|
||||
$this->job->save();
|
||||
|
||||
return true;
|
||||
return $config;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user