Exemplo n.º 1
0
 /**
  * This is for the transaction autocomplete
  * @param Request $request
  * @return Response
  */
 public function index(Request $request)
 {
     $transactions = Transaction::forCurrentUser()->limit(50)->orderBy('date', 'desc')->orderBy('id', 'desc')->with('account')->with('budgets');
     if ($request->get('typing') || $request->get('typing') === '') {
         $transactions = $transactions->where($request->get('column'), 'LIKE', '%' . $request->get('typing') . '%')->where('type', '!=', 'transfer');
     }
     $transactions = $transactions->get();
     $transactions = $this->transform($this->createCollection($transactions, new TransactionTransformer()))['data'];
     return response($transactions, Response::HTTP_OK);
 }
Exemplo n.º 2
0
 /**
  * BasicTotal constructor.
  */
 public function __construct($transactions = NULL)
 {
     $this->transactions = $transactions ?: Transaction::forCurrentUser()->get();
     $this->setDebit();
     $this->setCredit();
     $this->setBalance();
     $this->setReconciledSum();
     $this->setExpensesWithoutBudget();
     $this->setSavings();
 }
 /**
  *
  * @param $query
  * @param $value
  * @return mixed
  */
 private function filterOutNumBudgets($query, $value)
 {
     if ($value['out'] === "zero") {
         $ids = Transaction::forCurrentUser()->has('assignedBudgets', 0)->lists('id');
     } elseif ($value['out'] === "single") {
         $ids = Transaction::forCurrentUser()->has('assignedBudgets', 1)->lists('id');
     } elseif ($value['out'] === "multiple") {
         $ids = Transaction::forCurrentUser()->has('assignedBudgets', '>', 1)->lists('id');
     }
     return $query->whereNotIn('transactions.id', $ids);
 }
Exemplo n.º 4
0
 /**
  * @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);
 }
Exemplo n.º 5
0
 /**
  * @test
  */
 public function it_can_calculate_if_the_budget_allocations_for_the_transaction_match_the_total_of_the_transaction()
 {
     $this->logInUser();
     //Find a transaction with multiple budgets
     $transaction = Transaction::forCurrentUser()->has('assignedBudgets', '>', 1)->first();
     $this->assertEquals(1, $transaction->validAllocation);
     //Check the data is as expected before adding a budget
     $this->assertEquals(2, $transaction->budgets[0]->id);
     $this->assertEquals(3, $transaction->budgets[1]->id);
     //Add a budget to the transaction. This should make the budget allocations for transaction not equal the transaction total
     //so the transaction should now be unallocated
     $response = $this->call('PUT', '/api/transactions/' . $transaction->id, ['addingBudgets' => true, 'budget_ids' => [4]]);
     $content = json_decode($response->getContent(), true);
     //        dd($content);
     $this->checkTransactionKeysExist($content);
     $this->assertEquals(0, $content['validAllocation']);
     $this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
 }
Exemplo n.º 6
0
 /**
  * Define your route model bindings, pattern filters, etc.
  *
  * @param  \Illuminate\Routing\Router  $router
  * @return void
  */
 public function boot(Router $router)
 {
     parent::boot($router);
     //		$router->model('accounts', Account::class);
     Route::bind('accounts', function ($id) {
         return Account::forCurrentUser()->findOrFail($id);
     });
     Route::bind('budgets', function ($id) {
         return Budget::forCurrentUser()->findOrFail($id);
     });
     Route::bind('transactions', function ($id) {
         return Transaction::forCurrentUser()->findOrFail($id);
     });
     Route::bind('favouriteTransactions', function ($id) {
         return FavouriteTransaction::forCurrentUser()->findOrFail($id);
     });
     Route::bind('savedFilters', function ($id) {
         return SavedFilter::forCurrentUser()->findOrFail($id);
     });
 }
 /**
  * @test
  * @return void
  */
 public function it_cannot_add_a_budget_to_a_transaction_that_already_has_that_budget()
 {
     DB::beginTransaction();
     $this->logInUser();
     //Find a transaction with a budged_id of one, and no other budgets
     $transaction = Transaction::forCurrentUser()->whereHas('budgets', function ($q) {
         $q->where('budgets.id', 1);
     })->has('budgets', '<', 2)->first();
     $response = $this->call('PUT', '/api/transactions/' . $transaction->id, ['addingBudgets' => true, 'budget_ids' => [1]]);
     $content = json_decode($response->getContent(), true);
     //        dd($content);
     $this->checktransactionKeysExist($content);
     $this->assertCount(1, $content['budgets']);
     $this->assertEquals(1, $content['budgets'][0]['id']);
     //Check a couple of fields are the same because only the budgets should be changed
     $this->assertEquals($transaction->description, $content['description']);
     $this->assertEquals($transaction->merchant, $content['merchant']);
     $this->assertEquals(200, $response->getStatusCode());
     DB::rollBack();
 }
Exemplo n.º 8
0
 /**
  * @test
  * @return void
  */
 public function it_does_not_filter_out_more_transactions_than_it_should_when_the_description_filter_out_is_used()
 {
     $this->setFilterDefaults();
     $this->logInUser();
     //Count the number of transactions that do contain 'e' in the description
     //Todo: perhaps make seeder consistent so it's the same each time
     $count = Transaction::forCurrentUser()->where('description', 'LIKE', '%e%')->count();
     $filter = ['description' => ['in' => '', 'out' => 'e']];
     $this->filter = array_merge($this->defaults, $filter);
     $data = ['filter' => $this->filter];
     $this->setTransactions($data);
     $this->checkTransactionKeysExist($this->transactions[0]);
     foreach ($this->transactions as $transaction) {
         if ($transaction['merchant']) {
             $this->assertNotContains('e', $transaction['description'], '', true);
         }
     }
     //There are 16 transactions in total, so there should be 16 - $count in the filtered results
     $this->assertCount(16 - $count, $this->transactions);
     $this->assertEquals(Response::HTTP_OK, $this->response->getStatusCode());
 }
Exemplo n.º 9
0
 /**
  *
  */
 private function makeSomeTransactionsHaveInvalidAllocation()
 {
     //Find a transaction with a budged_id of one, and no other budgets
     $transaction = Transaction::forCurrentUser()->whereHas('budgets', function ($q) {
         $q->where('budgets.id', 2);
     })->has('budgets', '<', 2)->first();
     $this->assertEquals(12, $transaction->id);
     $response = $this->call('PUT', '/api/transactions/' . $transaction->id, ['addingBudgets' => true, 'budget_ids' => [3]]);
     //        dd(count(Transaction::find(12)->budgets));
     //Find a transaction with a budged_id of one, and no other budgets
     $transaction = Transaction::forCurrentUser()->whereHas('budgets', function ($q) {
         $q->where('budgets.id', 3);
     })->first();
     $this->assertEquals(5, $transaction->id);
     //Check the budgets for the transaction are as expected, so we know that adding a budget should make the allocation invalid
     $this->assertEquals('fixed', $transaction->budgets[0]->type);
     $this->assertEquals(2, $transaction->budgets[0]->id);
     $this->assertEquals('fixed', $transaction->budgets[1]->type);
     $this->assertEquals(3, $transaction->budgets[1]->id);
     $this->assertCount(2, $transaction->budgets);
     $response = $this->call('PUT', '/api/transactions/' . $transaction->id, ['addingBudgets' => true, 'budget_ids' => [4]]);
     $content = json_decode($response->getContent(), true);
     //        dd(count(Transaction::find(5)->budgets));
 }
Exemplo n.º 10
0
 /**
  * @test
  * @return void
  */
 public function it_can_remove_a_transaction_description()
 {
     $this->logInUser();
     $transaction = Transaction::forCurrentUser()->first();
     $data = ['description' => ''];
     $response = $this->apiCall('PUT', '/api/transactions/' . $transaction->id, $data);
     $content = json_decode($response->getContent(), true);
     $this->checkTransactionKeysExist($content);
     $this->assertEquals('', $content['description']);
     //Check the status code
     $this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
 }