Beispiel #1
0
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  * @param User $user
  */
 public static function createPiggybanks(User $user)
 {
     $account = self::findAccount($user, 'TestData Savings');
     $camera = PiggyBank::create(['account_id' => $account->id, 'name' => 'New camera', 'targetamount' => 1000, 'startdate' => '2015-04-01', 'reminder_skip' => 0, 'remind_me' => 0, 'order' => 1]);
     $repetition = $camera->piggyBankRepetitions()->first();
     $repetition->currentamount = 735;
     $repetition->save();
     // events:
     PiggyBankEvent::create(['piggy_bank_id' => $camera->id, 'date' => '2015-05-01', 'amount' => '245']);
     PiggyBankEvent::create(['piggy_bank_id' => $camera->id, 'date' => '2015-06-01', 'amount' => '245']);
     PiggyBankEvent::create(['piggy_bank_id' => $camera->id, 'date' => '2015-07-01', 'amount' => '245']);
     $phone = PiggyBank::create(['account_id' => $account->id, 'name' => 'New phone', 'targetamount' => 600, 'startdate' => '2015-04-01', 'reminder_skip' => 0, 'remind_me' => 0, 'order' => 2]);
     $repetition = $phone->piggyBankRepetitions()->first();
     $repetition->currentamount = 333;
     $repetition->save();
     // events:
     PiggyBankEvent::create(['piggy_bank_id' => $phone->id, 'date' => '2015-05-01', 'amount' => '111']);
     PiggyBankEvent::create(['piggy_bank_id' => $phone->id, 'date' => '2015-06-01', 'amount' => '111']);
     PiggyBankEvent::create(['piggy_bank_id' => $phone->id, 'date' => '2015-07-01', 'amount' => '111']);
     $couch = PiggyBank::create(['account_id' => $account->id, 'name' => 'New couch', 'targetamount' => 500, 'startdate' => '2015-04-01', 'reminder_skip' => 0, 'remind_me' => 0, 'order' => 3]);
     $repetition = $couch->piggyBankRepetitions()->first();
     $repetition->currentamount = 120;
     $repetition->save();
     // events:
     PiggyBankEvent::create(['piggy_bank_id' => $couch->id, 'date' => '2015-05-01', 'amount' => '40']);
     PiggyBankEvent::create(['piggy_bank_id' => $couch->id, 'date' => '2015-06-01', 'amount' => '40']);
     PiggyBankEvent::create(['piggy_bank_id' => $couch->id, 'date' => '2015-07-01', 'amount' => '40']);
     // empty one.
     PiggyBank::create(['account_id' => $account->id, 'name' => 'New head set', 'targetamount' => 500, 'startdate' => '2015-04-01', 'reminder_skip' => 0, 'remind_me' => 0, 'order' => 4]);
 }
 /**
  *
  */
 protected function registerCreateEvents()
 {
     // move this routine to a filter
     // in case of repeated piggy banks and/or other problems.
     PiggyBank::created(function (PiggyBank $piggyBank) {
         $repetition = new PiggyBankRepetition();
         $repetition->piggyBank()->associate($piggyBank);
         $repetition->startdate = is_null($piggyBank->startdate) ? null : $piggyBank->startdate;
         $repetition->targetdate = is_null($piggyBank->targetdate) ? null : $piggyBank->targetdate;
         $repetition->currentamount = 0;
         $repetition->save();
     });
 }
 /**
  * @param $name
  *
  * @return PiggyBank|null
  */
 protected function findPiggyBank($name)
 {
     /** @var Budget $budget */
     foreach (PiggyBank::get() as $piggyBank) {
         $account = $piggyBank->account()->first();
         if ($piggyBank->name == $name && $this->user->id == $account->user_id) {
             return $piggyBank;
             break;
         }
     }
     return null;
 }
Beispiel #4
0
 /**
  * @param $name
  *
  * @return PiggyBank|null
  */
 protected function findPiggyBank($name)
 {
     // account
     $user = User::whereEmail('*****@*****.**')->first();
     /** @var Budget $budget */
     foreach (PiggyBank::get() as $piggyBank) {
         $account = $piggyBank->account()->first();
         if ($piggyBank->name == $name && $user->id == $account->user_id) {
             return $piggyBank;
             break;
         }
     }
     return null;
 }
 /**
  * @param PiggyBank $piggyBank
  * @param array     $data
  *
  * @return PiggyBank
  */
 public function update(PiggyBank $piggyBank, array $data) : PiggyBank
 {
     $piggyBank->name = $data['name'];
     $piggyBank->account_id = intval($data['account_id']);
     $piggyBank->targetamount = round($data['targetamount'], 2);
     $piggyBank->targetdate = $data['targetdate'];
     $piggyBank->startdate = $data['startdate'];
     $piggyBank->save();
     // if the piggy bank is now smaller than the current relevant rep,
     // remove money from the rep.
     $repetition = $piggyBank->currentRelevantRep();
     if ($repetition->currentamount > $piggyBank->targetamount) {
         $repetition->currentamount = $piggyBank->targetamount;
         $repetition->save();
     }
     return $piggyBank;
 }
Beispiel #6
0
        }
    }
    throw new NotFoundHttpException();
});
Route::bind('limitrepetition', function ($value) {
    if (Auth::check()) {
        $object = LimitRepetition::where('limit_repetitions.id', $value)->leftjoin('budget_limits', 'budget_limits.id', '=', 'limit_repetitions.budget_limit_id')->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')->where('budgets.user_id', Auth::user()->id)->first(['limit_repetitions.*']);
        if ($object) {
            return $object;
        }
    }
    throw new NotFoundHttpException();
});
Route::bind('piggyBank', function ($value) {
    if (Auth::check()) {
        $object = PiggyBank::where('piggy_banks.id', $value)->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', Auth::user()->id)->first(['piggy_banks.*']);
        if ($object) {
            return $object;
        }
    }
    throw new NotFoundHttpException();
});
Route::bind('category', function ($value) {
    if (Auth::check()) {
        $object = Category::where('id', $value)->where('user_id', Auth::user()->id)->first();
        if ($object) {
            return $object;
        }
    }
    throw new NotFoundHttpException();
});
 /**
  * @param PiggyBankRepositoryInterface $repository
  * @param PiggyBank                    $piggyBank
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function postRemove(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank)
 {
     $amount = round(Input::get('amount'), 2);
     bcscale(2);
     $savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
     if ($amount <= $savedSoFar) {
         $repetition = $piggyBank->currentRelevantRep();
         $repetition->currentamount = bcsub($repetition->currentamount, $amount);
         $repetition->save();
         // create event
         $repository->createEvent($piggyBank, $amount * -1);
         Session::flash('success', 'Removed ' . Amount::format($amount, false) . ' from "' . e($piggyBank->name) . '".');
         Preferences::mark();
     } else {
         Session::flash('error', 'Could not remove ' . Amount::format($amount, false) . ' from "' . e($piggyBank->name) . '".');
     }
     return redirect(route('piggy-banks.index'));
 }
 /**
  * @param PiggyBank $piggyBank
  * @param array     $data
  *
  * @return PiggyBank
  */
 public function update(PiggyBank $piggyBank, array $data)
 {
     $piggyBank->name = $data['name'];
     $piggyBank->account_id = intval($data['account_id']);
     $piggyBank->targetamount = floatval($data['targetamount']);
     $piggyBank->targetdate = $data['targetdate'];
     $piggyBank->startdate = $data['startdate'];
     $piggyBank->save();
     return $piggyBank;
 }
 /**
  * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::setOrder
  */
 public function testSetOrder()
 {
     $piggyBank = FactoryMuffin::create('FireflyIII\\Models\\PiggyBank');
     $piggyBank->order = 4;
     $this->be($piggyBank->account->user);
     $piggyBank->save();
     $this->object->setOrder($piggyBank->id, 3);
     $newPiggy = PiggyBank::find($piggyBank->id);
     $this->assertEquals(3, $newPiggy->order);
 }
 /**
  * @param PiggyBankRepositoryInterface $repository
  * @param PiggyBank                    $piggyBank
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function postRemove(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank)
 {
     $amount = strval(round(Input::get('amount'), 2));
     $savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
     if ($amount <= $savedSoFar) {
         $repetition = $piggyBank->currentRelevantRep();
         $repetition->currentamount = bcsub($repetition->currentamount, $amount);
         $repetition->save();
         // create event
         $repository->createEvent($piggyBank, bcmul($amount, '-1'));
         Session::flash('success', strval(trans('firefly.removed_amount_from_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)])));
         Preferences::mark();
         return redirect(route('piggy-banks.index'));
     }
     Session::flash('error', strval(trans('firefly.cannot_remove_from_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)])));
     return redirect(route('piggy-banks.index'));
 }