Exemple #1
0
 public function run()
 {
     Savings::truncate();
     $users = User::all();
     foreach ($users as $user) {
         $savings = new Savings(['amount' => 50]);
         $savings->user()->associate($user);
         $savings->save();
     }
 }
Exemple #2
0
 /**
  * Create a new user instance after a valid registration.
  *
  * @param  array  $data
  * @return User
  */
 public function create(array $data)
 {
     $user = new User(['name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), 'preferences' => Config::get('user-preferences.defaults')]);
     $user->save();
     $savings = new Savings(['amount' => 0]);
     $savings->user()->associate($user);
     $savings->save();
     return $user;
     //		return User::create([
     //			'name' => $data['name'],
     //			'email' => $data['email'],
     //			'password' => bcrypt($data['password']),
     //            'preferences' => Config::get('user-preferences.defaults')
     //		]);
 }
 /**
  * Add an amount into savings.
  * For when an income transaction is added.
  * @param $transaction
  * @return float
  */
 public static function calculateAfterIncomeAdded($transaction)
 {
     //$percent is planned to be configurable and stored in the database
     $percent = 10;
     $savings = Savings::forCurrentUser()->first();
     return $transaction->total / 100 * $percent;
 }
 /**
  * Decrease the savings amount for the user
  * PUT api/savings/decrease
  * @param UpdateSavingsTotalRequest $updateSavingsTotalRequest
  * @return string
  */
 public function decrease(UpdateSavingsTotalRequest $updateSavingsTotalRequest)
 {
     $amount = $updateSavingsTotalRequest->get('amount');
     $savings = Savings::forCurrentUser()->first();
     $savings->decrease($amount);
     $savings->save();
     return response($savings->amount, Response::HTTP_OK);
 }
 /**
  * @test
  * @return void
  */
 public function it_does_not_change_the_savings_when_an_expense_transaction_is_deleted()
 {
     $this->logInUser();
     $this->assertEquals('50.00', Savings::forCurrentUser()->first()->amount);
     $transaction = Transaction::forCurrentUser()->where('type', 'expense')->first();
     $response = $this->apiCall('DELETE', '/api/transactions/' . $transaction->id);
     $this->assertEquals(204, $response->getStatusCode());
     $this->missingFromDatabase('transactions', ['user_id' => $this->user->id, 'id' => $transaction->id]);
     //Check the savings decreased
     $this->assertEquals('50.00', Savings::forCurrentUser()->first()->amount);
 }
Exemple #6
0
 private function truncate()
 {
     User::truncate();
     Savings::truncate();
     Budget::truncate();
     Account::truncate();
     Transaction::truncate();
     FavouriteTransaction::truncate();
     SavedFilter::truncate();
     DB::table('budgets_favourite_transactions')->truncate();
     DB::table('budgets_transactions')->truncate();
 }
 /**
  * Handle the event.
  *
  * @param  TransactionWasCreated  $event
  * @return void
  */
 public function handle($event)
 {
     switch (class_basename($event)) {
         case "TransactionWasCreated":
             $transaction = $event->transaction;
             // Put an amount into savings if it is an income transaction
             if ($transaction->type === Transaction::TYPE_INCOME) {
                 $savings = Savings::forCurrentUser()->first();
                 $savings->increase($this->savingsRepository->calculateAfterIncomeAdded($transaction));
             }
             break;
         case "TransactionWasUpdated":
             $oldTotal = $event->oldTransaction->total;
             if (!isset($event->newTransaction['total']) && !isset($event->newTransaction['type'])) {
                 return;
             }
             if (isset($event->newTransaction['total'])) {
                 $newTotal = $event->newTransaction['total'];
             }
             $savings = Savings::forCurrentUser()->first();
             if (isset($event->newTransaction['type']) && $event->oldTransaction->type !== $event->newTransaction['type']) {
                 //The transaction type has changed
                 if ($event->newTransaction['type'] === 'income') {
                     //Increase the savings
                     if (isset($event->newTransaction['total'])) {
                         $savings->increase($event->newTransaction['total'] / 10);
                     } else {
                         $savings->increase($event->oldTransaction->total / 10);
                     }
                 } else {
                     if ($event->newTransaction['type'] === 'expense') {
                         //Decrease the savings
                         $savings->decrease($event->oldTransaction->total * -1 / 10);
                     }
                 }
             } else {
                 //The transaction type is the same
                 // If it is an income transaction, and if the total has decreased,
                 // remove a percentage from savings
                 if ($event->oldTransaction->type === 'income' && $newTotal < $oldTotal) {
                     $savings->decrease($this->savingsRepository->calculateAfterDecrease($oldTotal, $newTotal));
                 }
                 // If it is an income transaction, and if the total has increased,
                 // add a percentage to savings
                 if ($event->oldTransaction->type === 'income' && $newTotal > $oldTotal) {
                     $savings->increase($this->savingsRepository->calculateAfterIncrease($oldTotal, $newTotal));
                 }
             }
             break;
         default:
     }
 }
 /**
  * @test
  * @return void
  */
 public function it_does_not_change_the_savings_if_an_expense_transaction_total_is_increased()
 {
     $this->logInUser();
     $this->assertEquals('50.00', Savings::forCurrentUser()->first()->amount);
     $transaction = Transaction::forCurrentUser()->where('type', 'expense')->first();
     $data = ['total' => -60];
     $response = $this->apiCall('PUT', '/api/transactions/' . $transaction->id, $data);
     $content = json_decode($response->getContent(), true);
     $this->checkTransactionKeysExist($content);
     //Check the savings did not change
     $this->assertEquals('50.00', Savings::forCurrentUser()->first()->amount);
     //Check the status code
     $this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
 }
Exemple #9
0
 /**
  * A basic functional test example.
  * @test
  * @return void
  */
 public function it_cannot_set_a_string_as_savings_amount()
 {
     $this->logInUser();
     $savings = Savings::forCurrentUser()->first();
     $this->seeInDatabase('savings', ['user_id' => $this->user->id, 'amount' => $savings->amount]);
     $response = $this->apiCall('PUT', '/api/savings/set', ['amount' => 'kangaroo']);
     $this->assertEquals(422, $response->getStatusCode());
 }
 /**
  * Delete a transaction, only if it belongs to the user
  * @param Transaction $transaction
  * @return Response
  * @throws \Exception
  */
 public function destroy(Transaction $transaction)
 {
     $transaction->delete();
     //Reverse the automatic insertion into savings if it is an income expense
     if ($transaction->type === 'income') {
         $savings = Savings::forCurrentUser()->first();
         $savings->decrease($this->savingsRepository->calculateAmountToSubtract($transaction));
     }
     return $this->responseNoContent();
 }
Exemple #11
0
 /**
  * Find savings total
  */
 public function setSavings()
 {
     $this->savings = Savings::getSavingsTotal();
 }
 /**
  * Todo: Do the same for checking total is positive for income transactions, and also check these things when transaction is updated
  * @test
  * @return void
  */
 public function it_converts_a_positive_total_to_negative_when_inserting_an_expense_transaction()
 {
     $this->logInUser();
     $this->assertEquals('50.00', Savings::forCurrentUser()->first()->amount);
     $transaction = ['date' => '2015-01-01', 'account_id' => 1, 'type' => 'expense', 'description' => 'interesting description', 'merchant' => 'some store', 'total' => 5, 'reconciled' => 0, 'allocated' => 0, 'budget_ids' => [2, 4]];
     $response = $this->apiCall('POST', '/api/transactions', $transaction);
     $content = json_decode($response->getContent(), true);
     $this->assertEquals(201, $response->getStatusCode());
     $this->seeInDatabase('budgets_transactions', ['transaction_id' => $content['id'], 'budget_id' => 2]);
     $this->seeInDatabase('budgets_transactions', ['transaction_id' => $content['id'], 'budget_id' => 4]);
     $this->checkTransactionKeysExist($content);
     $this->assertEquals('2015-01-01', $content['date']);
     $this->assertEquals('1', $content['account_id']);
     $this->assertEquals('expense', $content['type']);
     $this->assertEquals('interesting description', $content['description']);
     $this->assertEquals('some store', $content['merchant']);
     $this->assertEquals('-5', $content['total']);
     $this->assertEquals(0, $content['reconciled']);
     $this->assertEquals(0, $content['allocated']);
     $this->assertEquals('business', $content['budgets'][0]['name']);
     $this->assertEquals('busking', $content['budgets'][1]['name']);
     //Check the savings remained the same
     $this->assertEquals('50.00', Savings::forCurrentUser()->first()->amount);
 }