<commit_before><?php

namespace FireflyIII\Database;


use Carbon\Carbon;
use FireflyIII\Collection\PiggybankPart;
use FireflyIII\Database\Ifaces\CommonDatabaseCalls;
use FireflyIII\Database\Ifaces\CUD;
use FireflyIII\Database\Ifaces\PiggybankInterface;
use FireflyIII\Exception\NotImplementedException;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
use LaravelBook\Ardent\Ardent;

class RepeatedExpense implements CUD, CommonDatabaseCalls, PiggybankInterface
{
    use SwitchUser;

    /**
     *
     */
    public function __construct()
    {
        $this->setUser(\Auth::user());
    }

    /**
     * Based on the piggy bank, the reminder-setting and
     * other variables this method tries to divide the piggy bank into equal parts. Each is
     * accommodated by a reminder (if everything goes to plan).
     *
     * @return \PiggybankRepetition
     */
    public function calculateParts(\PiggybankRepetition $repetition)
    {
        \Log::debug('NOW in calculateParts()');
        \Log::debug('Repetition id is ' . $repetition->id);
        /** @var \Piggybank $piggyBank */
        $piggyBank = $repetition->piggybank()->first();
        $bars      = new Collection;
        \Log::debug('connected piggy bank is: ' . $piggyBank->name . ' (#' . $piggyBank->id . ')');

        /*
         * If no reminders are set, the repetition is split in exactly one part:
         */
        if (is_null($piggyBank->reminder)) {
            $part = new PiggybankPart;
            $part->setRepetition($repetition);
            $part->setAmountPerBar(floatval($piggyBank->targetamount));
            $part->setCurrentamount($repetition->currentamount);
            $part->setCumulativeAmount($piggyBank->targetamount);
            $part->setStartdate(clone $repetition->startdate);
            $part->setTargetdate(clone $repetition->targetdate);
            $bars->push($part);
            $repetition->bars = $bars;

            return $repetition;
        }
        $currentStart = clone $repetition->startdate;
        /*
         * Loop between start and target instead of counting manually.
         */
        $index = 0;
        //echo 'Looping!<br>';
        //echo $repetition->startdate . ' until ' . $repetition->targetdate . '<br>';
        while ($currentStart < $repetition->targetdate) {
            $currentTarget = \DateKit::endOfX($currentStart, $piggyBank->reminder);
            if ($currentTarget > $repetition->targetdate) {
                $currentTarget = clone $repetition->targetdate;
            }

            // create a part:
            $part = new PiggybankPart;
            $part->setRepetition($repetition);
            $part->setCurrentamount($repetition->currentamount);
            $part->setStartdate($currentStart);
            $part->setTargetdate($currentTarget);
            $bars->push($part);
            //echo 'Loop #' . $index . ', from ' . $currentStart . ' until ' . $currentTarget . '<br />';


            /*
             * Jump to the next period by adding a day.
             */
            $currentStart = clone $currentTarget;
            $currentStart->addDay();//\DateKit::addPeriod($currentTarget, $piggyBank->reminder, 0);
            $index++;

        }
        /*
         * Loop parts again to add some
         */
        $parts        = $bars->count();
        $amountPerBar = floatval($piggyBank->targetamount) / $parts;
        $cumulative   = $amountPerBar;
        /** @var PiggybankPart $bar */
        foreach ($bars as $bar) {
            $bar->setAmountPerBar($amountPerBar);
            $bar->setCumulativeAmount($cumulative);

            $reminder = $piggyBank->reminders()
                                  ->where('startdate', $bar->getStartdate()->format('Y-m-d'))
                                  ->where('enddate', $bar->getTargetdate()->format('Y-m-d'))
                                  ->first();
            if ($reminder) {
                $bar->setReminder($reminder);
            }

            $cumulative += $amountPerBar;
        }

        $repetition->bars = $bars;

        return $repetition;
        //
        //        //        if($parts > 12) {
        //        //            $parts = 12;
        //        //            $currentStart = \DateKit::startOfPeriod(Carbon::now(), $piggyBank->reminder);
        //        //            $currentEnd = \DateKit::endOfPeriod($currentEnd, $piggyBank->reminder);
        //        //        }<commit_msg>Fixed a bug where the associated total amount for a bar would be too high.<commit_after><?php

namespace FireflyIII\Database;


use Carbon\Carbon;
use FireflyIII\Collection\PiggybankPart;
use FireflyIII\Database\Ifaces\CommonDatabaseCalls;
use FireflyIII\Database\Ifaces\CUD;
use FireflyIII\Database\Ifaces\PiggybankInterface;
use FireflyIII\Exception\NotImplementedException;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
use LaravelBook\Ardent\Ardent;

class RepeatedExpense implements CUD, CommonDatabaseCalls, PiggybankInterface
{
    use SwitchUser;

    /**
     *
     */
    public function __construct()
    {
        $this->setUser(\Auth::user());
    }

    /**
     * Based on the piggy bank, the reminder-setting and
     * other variables this method tries to divide the piggy bank into equal parts. Each is
     * accommodated by a reminder (if everything goes to plan).
     *
     * @return \PiggybankRepetition
     */
    public function calculateParts(\PiggybankRepetition $repetition)
    {
        \Log::debug('NOW in calculateParts()');
        \Log::debug('Repetition id is ' . $repetition->id);
        /** @var \Piggybank $piggyBank */
        $piggyBank = $repetition->piggybank()->first();
        $bars      = new Collection;
        \Log::debug('connected piggy bank is: ' . $piggyBank->name . ' (#' . $piggyBank->id . ')');

        /*
         * If no reminders are set, the repetition is split in exactly one part:
         */
        if (is_null($piggyBank->reminder)) {
            $part = new PiggybankPart;
            $part->setRepetition($repetition);
            $part->setAmountPerBar(floatval($piggyBank->targetamount));
            $part->setCurrentamount($repetition->currentamount);
            $part->setCumulativeAmount($piggyBank->targetamount);
            $part->setStartdate(clone $repetition->startdate);
            $part->setTargetdate(clone $repetition->targetdate);
            $bars->push($part);
            $repetition->bars = $bars;

            return $repetition;
        }
        $currentStart = clone $repetition->startdate;
        /*
         * Loop between start and target instead of counting manually.
         */
        $index = 0;
        //echo 'Looping!<br>';
        //echo $repetition->startdate . ' until ' . $repetition->targetdate . '<br>';
        while ($currentStart < $repetition->targetdate) {
            $currentTarget = \DateKit::endOfX($currentStart, $piggyBank->reminder);
            if ($currentTarget > $repetition->targetdate) {
                $currentTarget = clone $repetition->targetdate;
            }

            // create a part:
            $part = new PiggybankPart;
            $part->setRepetition($repetition);
            $part->setCurrentamount($repetition->currentamount);
            $part->setStartdate($currentStart);
            $part->setTargetdate($currentTarget);
            $bars->push($part);
            //echo 'Loop #' . $index . ', from ' . $currentStart . ' until ' . $currentTarget . '<br />';


            /*
             * Jump to the next period by adding a day.
             */
            $currentStart = clone $currentTarget;
            $currentStart->addDay();//\DateKit::addPeriod($currentTarget, $piggyBank->reminder, 0);
            $index++;

        }
        /*
         * Loop parts again to add some
         */
        $parts        = $bars->count();
        $amountPerBar = floatval($piggyBank->targetamount) / $parts;
        $cumulative   = $amountPerBar;
        /** @var PiggybankPart $bar */
        foreach ($bars as $index => $bar) {
            $bar->setAmountPerBar($amountPerBar);
            $bar->setCumulativeAmount($cumulative);
            if($parts -1 == $index) {
                $bar->setCumulativeAmount($piggyBank->targetamount);
            }

            $reminder = $piggyBank->reminders()
                                  ->where('startdate', $bar->getStartdate()->format('Y-m-d'))
                                  ->where('enddate', $bar->getTargetdate()->format('Y-m-d'))
                                  ->first();
            if ($reminder) {
                $bar->setReminder($reminder);
            }

            $cumulative += $amountPerBar;
        }

        $repetition->bars = $bars;

        return $repetition;
        //
        //        //        if($parts > 12) {
        //        //            $parts = 12;
        //        //            $currentStart = \DateKit::startOfPeriod(Carbon::now(), $piggyBank->reminder);
        //        //            $currentEnd = \DateKit::endOfPeriod($currentEnd, $piggyBank->reminder);
        //        //        }
{'fraction_non_alphanumeric': 0.13679142044210987, 'fraction_numerical': 0.0014226307725979427, 'mean_word_length': 1.9329268292682926, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 118, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7479229', 'n_tokens_mistral': 2657, 'n_tokens_neox': 2552, 'n_words': 497}