<commit_before><?php

namespace FireflyIII\Helpers\Report;

use Auth;
use Carbon\Carbon;
use Crypt;
use FireflyIII\Models\Account;
use FireflyIII\Models\Budget;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
use Steam;

/**
 * Class ReportQuery
 *
 * @package FireflyIII\Helpers\Report
 */
class ReportQuery implements ReportQueryInterface
{
    /**
     * See ReportQueryInterface::incomeInPeriodCorrected.
     *
     * This method's length is caused mainly by the query build stuff. Therefor:
     *
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     *
     * @param Carbon $start
     * @param Carbon $end
     * @param bool   $includeShared
     *
     * @return Collection
     *
     */
    public function expenseInPeriodCorrected(Carbon $start, Carbon $end, $includeShared = false)
    {
        $query = $this->queryJournalsWithTransactions($start, $end);
        if ($includeShared === false) {
            $query->where(
                function (Builder $query) {
                    $query->where(
                        function (Builder $q) { // only get withdrawals not from a shared account
                            $q->where('transaction_types.type', 'Withdrawal');
                            $q->where('acm_from.data', '!=', '"sharedAsset"');
                        }
                    );
                    $query->orWhere(
                        function (Builder $q) { // and transfers from a shared account.
                            $q->where('transaction_types.type', 'Transfer');
                            $q->where('acm_to.data', '=', '"sharedAsset"');
                        }
                    );
                }
            );
        } else {
            $query->where('transaction_types.type', 'Withdrawal'); // any withdrawal is fine.
        }
        $query->orderBy('transaction_journals.date');<commit_msg>Fix a bug where an transfer to and from a shared asset would count as an expense/income.<commit_after><?php

namespace FireflyIII\Helpers\Report;

use Auth;
use Carbon\Carbon;
use Crypt;
use FireflyIII\Models\Account;
use FireflyIII\Models\Budget;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
use Steam;

/**
 * Class ReportQuery
 *
 * @package FireflyIII\Helpers\Report
 */
class ReportQuery implements ReportQueryInterface
{
    /**
     * See ReportQueryInterface::incomeInPeriodCorrected.
     *
     * This method's length is caused mainly by the query build stuff. Therefor:
     *
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     *
     * @param Carbon $start
     * @param Carbon $end
     * @param bool   $includeShared
     *
     * @return Collection
     *
     */
    public function expenseInPeriodCorrected(Carbon $start, Carbon $end, $includeShared = false)
    {
        $query = $this->queryJournalsWithTransactions($start, $end);
        if ($includeShared === false) {
            $query->where(
                function (Builder $query) {
                    $query->where(
                        function (Builder $q) { // only get withdrawals not from a shared account
                            $q->where('transaction_types.type', 'Withdrawal');
                            $q->where('acm_from.data', '!=', '"sharedAsset"');
                        }
                    );
                    $query->orWhere(
                        function (Builder $q) { // and transfers from a shared account.
                            $q->where('transaction_types.type', 'Transfer');
                            $q->where('acm_to.data', '=', '"sharedAsset"');
                            $q->where('acm_from.data', '!=', '"sharedAsset"');
                        }
                    );
                }
            );
        } else {
            $query->where('transaction_types.type', 'Withdrawal'); // any withdrawal is fine.
        }
        $query->orderBy('transaction_journals.date');
{'fraction_non_alphanumeric': 0.11760426770126091, 'fraction_numerical': 0.0, 'mean_word_length': 1.8967696629213484, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6795332', 'n_tokens_mistral': 1109, 'n_tokens_neox': 1082, 'n_words': 243}