Exemplo n.º 1
0
 /**
  * Todo: Do validations
  * POST /api/transactions
  * @param Request $request
  * @return Response
  */
 public function store(Request $request)
 {
     $transaction = new Transaction($request->only(['date', 'description', 'merchant', 'total', 'type', 'reconciled', 'minutes']));
     //Make sure total is negative for expense, negative for transfers from, and positive for income
     if ($transaction->type === 'expense' && $transaction->total > 0) {
         $transaction->total *= -1;
     } else {
         if ($transaction->type === 'income' && $transaction->total < 0) {
             $transaction->total *= -1;
         } else {
             if ($transaction->type === 'transfer' && $request->get('direction') === Transaction::DIRECTION_FROM) {
                 $transaction->total *= -1;
             }
         }
     }
     $transaction->account()->associate(Account::find($request->get('account_id')));
     $transaction->user()->associate(Auth::user());
     $transaction->save();
     if ($transaction->type !== 'transfer') {
         $this->budgetTransactionRepository->attachBudgetsWithDefaultAllocation($transaction, $request->get('budget_ids'));
     }
     //Fire event
     event(new TransactionWasCreated($transaction));
     $transaction = $this->transform($this->createItem($transaction, new TransactionTransformer()))['data'];
     return response($transaction, Response::HTTP_CREATED);
 }
Exemplo n.º 2
0
 /**
  *
  * @param $user
  */
 private function createTransfer($user)
 {
     $from_account = Account::whereUserId($user->id)->offset(1)->first();
     $to_account = Account::whereUserId($user->id)->first();
     $date = Config::get('budgets.dateBeforeStartingDateForIncomeTransactions');
     $total = 100;
     $description = $this->faker->sentence(1);
     $transaction = new Transaction(['type' => 'transfer', 'date' => $date, 'account_id' => $from_account->id, 'description' => $description, 'merchant' => NULL, 'total' => $total * -1, 'reconciled' => 0, 'allocated' => 0, 'created_at' => $date]);
     $transaction->user()->associate($user);
     $transaction->save();
     $transaction = new Transaction(['type' => 'transfer', 'date' => $date, 'account_id' => $to_account->id, 'description' => $description, 'merchant' => NULL, 'total' => $total, 'reconciled' => 0, 'allocated' => 0, 'created_at' => $date]);
     $transaction->user()->associate($user);
     $transaction->save();
 }