/**
  * UPDATE /api/favouritesTransactions/{favouriteTransactions}
  * @param Request $request
  * @param FavouriteTransaction $favourite
  * @return Response
  */
 public function update(Request $request, FavouriteTransaction $favourite)
 {
     // Create an array with the new fields merged
     $data = array_compare($favourite->toArray(), $request->only(['name', 'type', 'description', 'merchant', 'total']));
     $favourite->update($data);
     if ($request->has('account_id')) {
         $favourite->account()->associate(Account::findOrFail($request->get('account_id')));
         $favourite->fromAccount()->dissociate();
         $favourite->toAccount()->dissociate();
         $favourite->save();
     }
     if ($request->has('from_account_id')) {
         $favourite->fromAccount()->associate(Account::findOrFail($request->get('from_account_id')));
         $favourite->account()->dissociate();
         $favourite->save();
     }
     if ($request->has('to_account_id')) {
         $favourite->toAccount()->associate(Account::findOrFail($request->get('to_account_id')));
         $favourite->account()->dissociate();
         $favourite->save();
     }
     if ($request->has('budget_ids')) {
         $favourite->budgets()->sync($request->get('budget_ids'));
     }
     $favourite = $this->transform($this->createItem($favourite, new FavouriteTransactionTransformer()))['data'];
     return response($favourite, Response::HTTP_OK);
 }
Пример #2
0
 /**
  * PUT /api/transactions/{transactions}
  * @param Request $request
  * @param Transaction $transaction
  * @return Response
  */
 public function update(Request $request, Transaction $transaction)
 {
     //For adding budgets to many transactions at once
     if ($request->has('addingBudgets')) {
         $transaction = $this->budgetTransactionRepository->addBudgets($request, $transaction);
     } else {
         $previousTotal = $transaction->total;
         $data = array_filter(array_diff_assoc($request->only(['date', 'description', 'merchant', 'total', 'type', 'reconciled', 'allocated', 'minutes']), $transaction->toArray()), 'removeFalseKeepZeroAndEmptyStrings');
         if ($request->has('account_id')) {
             $transaction->account()->associate(Account::findOrFail($request->get('account_id')));
         }
         //Make the total positive if the type has been changed from expense to income
         if (isset($data['type']) && $transaction->type === 'expense' && $data['type'] === 'income') {
             if (isset($data['total']) && $data['total'] < 0) {
                 //The user has changed the total as well as the type,
                 //but the total is negative and it should be positive
                 $data['total'] *= -1;
             } else {
                 //The user has changed the type but not the total
                 $transaction->total *= -1;
                 $transaction->save();
             }
         } else {
             if (isset($data['type']) && $transaction->type === 'income' && $data['type'] === 'expense') {
                 if (isset($data['total']) && $data['total'] > 0) {
                     //The user has changed the total as well as the type,
                     //but the total is positive and it should be negative
                     $data['total'] *= -1;
                 } else {
                     //The user has changed the type but not the total
                     $transaction->total *= -1;
                     $transaction->save();
                 }
             } else {
                 if ($transaction->type === 'expense' && array_key_exists('total', $data) && $data['total'] > 0) {
                     $data['total'] *= -1;
                 } else {
                     if ($transaction->type === 'income' && array_key_exists('total', $data) && $data['total'] < 0) {
                         $data['total'] *= -1;
                     }
                 }
             }
         }
         //        if(empty($data)) {
         //            return $this->responseNotModified();
         //        }
         //Fire event
         //Todo: update the savings when event is fired
         event(new TransactionWasUpdated($transaction, $data));
         $transaction->update($data);
         $transaction->save();
         if ($request->has('budget_ids')) {
             $transaction->budgets()->sync($request->get('budget_ids'));
             //Change calculated_allocation from null to 0
             $budgetsAdded = $transaction->budgets()->wherePivot('calculated_allocation', null)->get();
             foreach ($budgetsAdded as $budget) {
                 $transaction->budgets()->updateExistingPivot($budget->id, ['calculated_allocation' => '0']);
             }
         }
         //If the total has changed, update the allocation if the allocation is a percentage of the transaction total
         if ($previousTotal !== $transaction->total) {
             $budgetsToUpdateAllocation = $transaction->budgets()->wherePivot('allocated_percent', '>', 0)->get();
             foreach ($budgetsToUpdateAllocation as $budget) {
                 $calculatedAllocation = $transaction->total / 100 * $budget->pivot->allocated_percent;
                 $transaction->budgets()->updateExistingPivot($budget->id, ['calculated_allocation' => $calculatedAllocation]);
             }
         }
     }
     $transaction = $this->transform($this->createItem($transaction, new TransactionTransformer()))['data'];
     return response($transaction, Response::HTTP_OK);
 }