diff --git a/app/Helpers/Attachments/AttachmentHelper.php b/app/Helpers/Attachments/AttachmentHelper.php index 634d912bfb..3189055bb2 100644 --- a/app/Helpers/Attachments/AttachmentHelper.php +++ b/app/Helpers/Attachments/AttachmentHelper.php @@ -16,10 +16,11 @@ namespace FireflyIII\Helpers\Attachments; use Crypt; use FireflyIII\Models\Attachment; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Collection; use Illuminate\Support\MessageBag; use Storage; use Symfony\Component\HttpFoundation\File\UploadedFile; - +use Log; /** * Class AttachmentHelper * @@ -28,6 +29,8 @@ use Symfony\Component\HttpFoundation\File\UploadedFile; class AttachmentHelper implements AttachmentHelperInterface { + /** @var Collection */ + public $attachments; /** @var MessageBag */ public $errors; /** @var MessageBag */ @@ -49,6 +52,7 @@ class AttachmentHelper implements AttachmentHelperInterface $this->allowedMimes = (array)config('firefly.allowedMimes'); $this->errors = new MessageBag; $this->messages = new MessageBag; + $this->attachments = new Collection; $this->uploadDisk = Storage::disk('upload'); } @@ -64,6 +68,14 @@ class AttachmentHelper implements AttachmentHelperInterface return $path; } + /** + * @return Collection + */ + public function getAttachments(): Collection + { + return $this->attachments; + } + /** * @return MessageBag */ @@ -110,7 +122,7 @@ class AttachmentHelper implements AttachmentHelperInterface $md5 = md5_file($file->getRealPath()); $name = $file->getClientOriginalName(); $class = get_class($model); - $count = auth()->user()->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count(); + $count = $model->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count(); if ($count > 0) { $msg = (string)trans('validation.file_already_attached', ['name' => $name]); @@ -137,7 +149,7 @@ class AttachmentHelper implements AttachmentHelperInterface } $attachment = new Attachment; // create Attachment object. - $attachment->user()->associate(auth()->user()); + $attachment->user()->associate($model->user); $attachment->attachable()->associate($model); $attachment->md5 = md5_file($file->getRealPath()); $attachment->filename = $file->getClientOriginalName(); @@ -156,6 +168,7 @@ class AttachmentHelper implements AttachmentHelperInterface $attachment->uploaded = 1; // update attachment $attachment->save(); + $this->attachments->push($attachment); $name = e($file->getClientOriginalName()); // add message: $msg = (string)trans('validation.file_attached', ['name' => $name]); @@ -188,6 +201,7 @@ class AttachmentHelper implements AttachmentHelperInterface } /** + * @codeCoverageIgnore * @param UploadedFile $file * * @return bool @@ -218,7 +232,7 @@ class AttachmentHelper implements AttachmentHelperInterface return false; } if (!$this->validSize($file)) { - return false; + return false; // @codeCoverageIgnore } if ($this->hasFile($file, $model)) { return false; diff --git a/app/Helpers/Attachments/AttachmentHelperInterface.php b/app/Helpers/Attachments/AttachmentHelperInterface.php index aeb718ae11..ad1c80fd1b 100644 --- a/app/Helpers/Attachments/AttachmentHelperInterface.php +++ b/app/Helpers/Attachments/AttachmentHelperInterface.php @@ -15,6 +15,7 @@ namespace FireflyIII\Helpers\Attachments; use FireflyIII\Models\Attachment; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Collection; use Illuminate\Support\MessageBag; /** @@ -32,6 +33,11 @@ interface AttachmentHelperInterface */ public function getAttachmentLocation(Attachment $attachment): string; + /** + * @return Collection + */ + public function getAttachments(): Collection; + /** * @return MessageBag */ diff --git a/app/Helpers/Chart/MetaPieChart.php b/app/Helpers/Chart/MetaPieChart.php index e3b268a812..6dfe7bdb2b 100644 --- a/app/Helpers/Chart/MetaPieChart.php +++ b/app/Helpers/Chart/MetaPieChart.php @@ -23,6 +23,7 @@ use FireflyIII\Repositories\Category\CategoryRepositoryInterface; use FireflyIII\Repositories\Tag\TagRepositoryInterface; use FireflyIII\User; use Illuminate\Support\Collection; +use Log; use Steam; /** @@ -91,6 +92,7 @@ class MetaPieChart implements MetaPieChartInterface if ($this->collectOtherObjects && $direction === 'expense') { /** @var JournalCollectorInterface $collector */ $collector = app(JournalCollectorInterface::class); + $collector->setUser($this->user); $collector->setAccounts($this->accounts)->setRange($this->start, $this->end)->setTypes([TransactionType::WITHDRAWAL]); $journals = $collector->getJournals(); $sum = strval($journals->sum('transaction_amount')); @@ -102,6 +104,7 @@ class MetaPieChart implements MetaPieChartInterface if ($this->collectOtherObjects && $direction === 'income') { /** @var JournalCollectorInterface $collector */ $collector = app(JournalCollectorInterface::class); + $collector->setUser($this->user); $collector->setAccounts($this->accounts)->setRange($this->start, $this->end)->setTypes([TransactionType::DEPOSIT]); $journals = $collector->getJournals(); $sum = strval($journals->sum('transaction_amount')); @@ -114,6 +117,8 @@ class MetaPieChart implements MetaPieChartInterface } /** + * @codeCoverageIgnore + * * @param Collection $accounts * * @return MetaPieChartInterface @@ -126,6 +131,8 @@ class MetaPieChart implements MetaPieChartInterface } /** + * @codeCoverageIgnore + * * @param Collection $budgets * * @return MetaPieChartInterface @@ -138,6 +145,8 @@ class MetaPieChart implements MetaPieChartInterface } /** + * @codeCoverageIgnore + * * @param Collection $categories * * @return MetaPieChartInterface @@ -150,6 +159,8 @@ class MetaPieChart implements MetaPieChartInterface } /** + * @codeCoverageIgnore + * * @param bool $collectOtherObjects * * @return MetaPieChartInterface @@ -162,6 +173,8 @@ class MetaPieChart implements MetaPieChartInterface } /** + * @codeCoverageIgnore + * * @param Carbon $end * * @return MetaPieChartInterface @@ -174,6 +187,8 @@ class MetaPieChart implements MetaPieChartInterface } /** + * @codeCoverageIgnore + * * @param Carbon $start * * @return MetaPieChartInterface @@ -186,6 +201,8 @@ class MetaPieChart implements MetaPieChartInterface } /** + * @codeCoverageIgnore + * * @param Collection $tags * * @return MetaPieChartInterface @@ -198,6 +215,8 @@ class MetaPieChart implements MetaPieChartInterface } /** + * @codeCoverageIgnore + * * @param User $user * * @return MetaPieChartInterface @@ -209,7 +228,12 @@ class MetaPieChart implements MetaPieChartInterface return $this; } - protected function getTransactions(string $direction) + /** + * @param string $direction + * + * @return Collection + */ + protected function getTransactions(string $direction): Collection { $types = [TransactionType::DEPOSIT, TransactionType::TRANSFER]; $modifier = -1; @@ -219,6 +243,7 @@ class MetaPieChart implements MetaPieChartInterface } /** @var JournalCollectorInterface $collector */ $collector = app(JournalCollectorInterface::class); + $collector->setUser($this->user); $collector->setAccounts($this->accounts); $collector->setRange($this->start, $this->end); $collector->setTypes($types); @@ -242,7 +267,19 @@ class MetaPieChart implements MetaPieChartInterface $accountIds = $this->accounts->pluck('id')->toArray(); $transactions = $collector->getJournals(); - $set = Support::filterTransactions($transactions, $accountIds, $modifier); + Log::debug(sprintf('Modifier is %d', $modifier)); + foreach ($transactions as $transaction) { + Log::debug( + sprintf( + 'Included %s #%d (part of #%d) ("%s") amount %f', + $transaction->transaction_type_type, $transaction->id, + $transaction->journal_id + , $transaction->description, $transaction->transaction_amount + ) + ); + } + + $set = Support::filterTransactions($transactions, $accountIds, $modifier); return $set; } @@ -286,6 +323,7 @@ class MetaPieChart implements MetaPieChartInterface $chartData = []; $names = []; $repository = app($this->repositories[$type]); + $repository->setUser($this->user); foreach ($array as $objectId => $amount) { if (!isset($names[$objectId])) { $object = $repository->find(intval($objectId)); @@ -300,6 +338,11 @@ class MetaPieChart implements MetaPieChartInterface } + /** + * @param Collection $set + * + * @return array + */ private function groupByTag(Collection $set): array { $grouped = []; diff --git a/app/Helpers/Collector/JournalCollector.php b/app/Helpers/Collector/JournalCollector.php index cafc431ea4..2e286412da 100644 --- a/app/Helpers/Collector/JournalCollector.php +++ b/app/Helpers/Collector/JournalCollector.php @@ -464,7 +464,9 @@ class JournalCollector implements JournalCollectorInterface */ public function setUser(User $user) { + Log::debug(sprintf('Journal collector now collecting for user #%d', $user->id)); $this->user = $user; + $this->startQuery(); } /** @@ -472,6 +474,7 @@ class JournalCollector implements JournalCollectorInterface */ public function startQuery() { + Log::debug('journalCollector::startQuery'); /** @var EloquentBuilder $query */ $query = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') ->leftJoin('transaction_currencies', 'transaction_currencies.id', 'transaction_journals.transaction_currency_id') diff --git a/app/Providers/JournalServiceProvider.php b/app/Providers/JournalServiceProvider.php index fb88520bc7..42ff742255 100644 --- a/app/Providers/JournalServiceProvider.php +++ b/app/Providers/JournalServiceProvider.php @@ -62,7 +62,7 @@ class JournalServiceProvider extends ServiceProvider if ($app->auth->check()) { $collector->setUser(auth()->user()); } - $collector->startQuery(); + return $collector; }