2015-02-23 21:55:52 +01:00
<? php namespace FireflyIII\Http\Controllers ;
2015-07-31 13:44:56 +02:00
use Amount ;
2015-02-23 21:55:52 +01:00
use Auth ;
2015-05-04 23:46:14 +02:00
use Carbon\Carbon ;
2015-07-06 22:12:35 +02:00
use Config ;
2015-02-24 21:10:25 +01:00
use ExpandedForm ;
2015-03-02 20:05:28 +01:00
use FireflyIII\Events\JournalCreated ;
2015-03-01 08:34:59 +01:00
use FireflyIII\Events\JournalSaved ;
2015-07-18 09:49:59 +02:00
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface ;
2015-02-24 22:53:38 +01:00
use FireflyIII\Http\Requests\JournalFormRequest ;
2015-07-31 13:44:56 +02:00
use FireflyIII\Models\PiggyBank ;
2015-12-28 07:27:16 +01:00
use FireflyIII\Models\PiggyBankEvent ;
2015-02-25 21:19:06 +01:00
use FireflyIII\Models\Transaction ;
use FireflyIII\Models\TransactionJournal ;
2015-12-09 22:39:50 -02:00
use FireflyIII\Models\TransactionType ;
2015-12-30 08:00:52 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI ;
2015-02-24 22:53:38 +01:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface ;
2015-12-28 07:27:16 +01:00
use Illuminate\Support\Collection ;
2015-02-24 21:10:25 +01:00
use Input ;
2015-06-02 17:44:50 +02:00
use Preferences ;
2015-03-29 11:51:26 +02:00
use Response ;
2015-02-24 21:10:25 +01:00
use Session ;
2015-07-19 14:30:20 +02:00
use Steam ;
2015-03-31 20:22:51 +02:00
use URL ;
2015-04-01 09:16:41 +02:00
use View ;
2015-02-27 14:27:04 +01:00
2015-02-23 21:55:52 +01:00
/**
* Class TransactionController
*
* @package FireflyIII\Http\Controllers
*/
class TransactionController extends Controller
{
/**
2015-05-23 20:49:57 +02:00
* @codeCoverageIgnore
2015-02-23 21:55:52 +01:00
*/
public function __construct ()
{
2016-01-08 18:29:47 +01:00
$this -> middleware ( 'auth' );
2015-04-28 15:26:30 +02:00
parent :: __construct ();
2015-05-14 15:53:56 +02:00
View :: share ( 'title' , trans ( 'firefly.transactions' ));
2015-02-23 21:55:52 +01:00
View :: share ( 'mainTitleIcon' , 'fa-repeat' );
}
2015-02-24 21:10:25 +01:00
/**
2016-01-01 12:41:00 +01:00
* @param ARI $repository
* @param string $what
2015-02-24 21:10:25 +01:00
*
2015-05-26 18:57:31 +00:00
* @return \Illuminate\View\View
2015-02-24 21:10:25 +01:00
*/
2015-12-30 08:00:52 +01:00
public function create ( ARI $repository , $what = TransactionType :: DEPOSIT )
2015-02-24 21:10:25 +01:00
{
2015-12-18 16:38:50 +01:00
$what = strtolower ( $what );
2015-07-19 14:30:20 +02:00
$maxFileSize = Steam :: phpBytes ( ini_get ( 'upload_max_filesize' ));
$maxPostSize = Steam :: phpBytes ( ini_get ( 'post_max_size' ));
$uploadSize = min ( $maxFileSize , $maxPostSize );
$accounts = ExpandedForm :: makeSelectList ( $repository -> getAccounts ([ 'Default account' , 'Asset account' ]));
$budgets = ExpandedForm :: makeSelectList ( Auth :: user () -> budgets () -> get ());
$budgets [ 0 ] = trans ( 'form.noBudget' );
2015-07-31 13:44:56 +02:00
// piggy bank list:
$piggyBanks = Auth :: user () -> piggyBanks () -> orderBy ( 'order' , 'ASC' ) -> get ();
/** @var PiggyBank $piggy */
foreach ( $piggyBanks as $piggy ) {
$piggy -> name = $piggy -> name . ' (' . Amount :: format ( $piggy -> currentRelevantRep () -> currentamount , false ) . ')' ;
}
$piggies = ExpandedForm :: makeSelectList ( $piggyBanks );
$piggies [ 0 ] = trans ( ' form . noPiggybank ');
$preFilled = Session::has(' preFilled ') ? Session::get(' preFilled ') : [];
$respondTo = [' account_id ', ' account_from_id '];
$subTitle = trans(' form . add_new_ ' . $what);
2015-02-24 21:10:25 +01:00
foreach ($respondTo as $r) {
2015-05-17 10:30:18 +02:00
$preFilled[$r] = Input::get($r);
2015-02-24 21:10:25 +01:00
}
Session::put(' preFilled ', $preFilled);
2015-04-01 09:12:49 +02:00
// put previous url in session if not redirect from store (not "create another").
2015-04-01 09:16:41 +02:00
if (Session::get(' transactions . create . fromStore ') !== true) {
2015-04-01 09:12:49 +02:00
Session::put(' transactions . create . url ', URL::previous());
}
Session::forget(' transactions . create . fromStore ');
2015-05-25 08:12:31 +02:00
Session::flash(' gaEventCategory ', ' transactions ');
Session::flash(' gaEventAction ', ' create - ' . $what);
2015-04-01 09:12:49 +02:00
2015-02-24 21:10:25 +01:00
asort($piggies);
2015-07-19 14:30:20 +02:00
return view(' transactions . create ', compact(' accounts ', ' uploadSize ', ' budgets ', ' what ', ' piggies ', ' subTitle '));
2015-02-24 21:10:25 +01:00
}
2015-02-27 14:27:04 +01:00
/**
* Shows the form that allows a user to delete a transaction journal.
*
* @param TransactionJournal $journal
*
2015-05-26 18:57:31 +00:00
* @return \Illuminate\View\View
2015-02-27 14:27:04 +01:00
*/
public function delete(TransactionJournal $journal)
{
2015-12-09 22:39:50 -02:00
$what = strtolower($journal->getTransactionType());
2015-06-06 23:09:12 +02:00
$subTitle = trans(' firefly . delete_ ' . $what, [' description ' => $journal->description]);
2015-02-27 14:27:04 +01:00
2015-04-01 09:23:51 +02:00
// put previous url in session
Session::put(' transactions . delete . url ', URL::previous());
2015-05-25 08:12:31 +02:00
Session::flash(' gaEventCategory ', ' transactions ');
2015-06-06 23:09:12 +02:00
Session::flash(' gaEventAction ', ' delete - ' . $what);
2015-04-01 09:23:51 +02:00
2015-06-13 10:02:36 +02:00
return view(' transactions . delete ', compact(' journal ', ' subTitle ', ' what '));
2015-02-27 14:27:04 +01:00
}
/**
2015-05-05 07:48:34 +02:00
* @param JournalRepositoryInterface $repository
* @param TransactionJournal $transactionJournal
2015-02-27 14:27:04 +01:00
*
* @return \Illuminate\Http\RedirectResponse
*/
2015-05-04 23:46:14 +02:00
public function destroy(JournalRepositoryInterface $repository, TransactionJournal $transactionJournal)
2015-02-27 14:27:04 +01:00
{
Session::flash(' success ', ' Transaction "' . e( $transactionJournal->description ) . '" destroyed . ');
2015-05-04 23:46:14 +02:00
$repository->delete($transactionJournal);
2015-02-27 14:27:04 +01:00
2015-06-02 17:44:50 +02:00
Preferences::mark();
2015-04-01 09:23:51 +02:00
// redirect to previous URL:
2015-07-06 16:27:21 +02:00
return redirect(Session::get(' transactions . delete . url '));
2015-02-27 14:27:04 +01:00
}
/**
* Shows the view to edit a transaction.
*
2016-01-01 12:41:00 +01:00
* @param ARI $repository
* @param TransactionJournal $journal
2015-02-27 14:27:04 +01:00
*
* @return $this
*/
2015-12-30 08:00:52 +01:00
public function edit(ARI $repository, TransactionJournal $journal)
2015-02-27 14:27:04 +01:00
{
2015-08-15 21:45:29 +02:00
// cannot edit opening balance
2015-12-09 22:39:50 -02:00
if ($journal->isOpeningBalance()) {
2015-08-15 21:45:29 +02:00
return view(' error ')->with(' message ', ' Cannot edit this transaction . Edit the account instead ! ');
}
2015-07-19 14:30:20 +02:00
$maxFileSize = Steam::phpBytes(ini_get(' upload_max_filesize '));
$maxPostSize = Steam::phpBytes(ini_get(' post_max_size '));
$uploadSize = min($maxFileSize, $maxPostSize);
2015-12-09 22:39:50 -02:00
$what = strtolower($journal->getTransactionType());
2015-07-19 14:30:20 +02:00
$accounts = ExpandedForm::makeSelectList($repository->getAccounts([' Default account ', ' Asset account ']));
$budgets = ExpandedForm::makeSelectList(Auth::user()->budgets()->get());
$budgets[0] = trans(' form . noBudget ');
$piggies = ExpandedForm::makeSelectList(Auth::user()->piggyBanks()->get());
$piggies[0] = trans(' form . noPiggybank ');
$subTitle = trans(' breadcrumbs . edit_journal ', [' description ' => $journal->description]);
$preFilled = [
2015-02-27 14:27:04 +01:00
' date ' => $journal->date->format(' Y - m - d '),
' category ' => '',
' budget_id ' => 0,
' piggy_bank_id ' => 0
];
2015-04-28 10:36:13 +02:00
// get tags:
2016-01-02 19:33:44 +01:00
$preFilled[' tags '] = join(' , ', $journal->tags->pluck(' tag ')->toArray());
2015-02-27 14:27:04 +01:00
$category = $journal->categories()->first();
if (!is_null($category)) {
$preFilled[' category '] = $category->name;
}
$budget = $journal->budgets()->first();
if (!is_null($budget)) {
$preFilled[' budget_id '] = $budget->id;
}
if ($journal->piggyBankEvents()->count() > 0) {
2015-03-02 20:05:28 +01:00
$preFilled[' piggy_bank_id '] = $journal->piggyBankEvents()->orderBy(' date ', ' DESC ')->first()->piggy_bank_id;
2015-02-27 14:27:04 +01:00
}
2015-09-25 20:40:24 +02:00
$preFilled[' amount '] = $journal->amount_positive;
2015-06-16 18:26:17 +02:00
2015-12-09 22:39:50 -02:00
if ($journal->isWithdrawal()) {
2015-06-16 18:26:17 +02:00
$preFilled[' account_id '] = $journal->source_account->id;
2015-06-26 04:57:30 +02:00
$preFilled[' expense_account '] = $journal->destination_account->name_for_editform;
2015-06-16 18:26:17 +02:00
} else {
$preFilled[' account_id '] = $journal->destination_account->id;
2015-06-26 04:57:30 +02:00
$preFilled[' revenue_account '] = $journal->source_account->name_for_editform;
2015-06-16 18:26:17 +02:00
}
$preFilled[' account_from_id '] = $journal->source_account->id;
$preFilled[' account_to_id '] = $journal->destination_account->id;
2015-02-27 14:27:04 +01:00
2015-04-28 15:26:30 +02:00
Session::flash(' preFilled ', $preFilled);
2015-05-25 08:12:31 +02:00
Session::flash(' gaEventCategory ', ' transactions ');
Session::flash(' gaEventAction ', ' edit - ' . $what);
2015-04-28 10:36:13 +02:00
2015-04-01 09:12:49 +02:00
// put previous url in session if not redirect from store (not "return_to_edit").
2015-04-01 09:40:19 +02:00
if (Session::get(' transactions . edit . fromUpdate ') !== true) {
2015-04-01 09:12:49 +02:00
Session::put(' transactions . edit . url ', URL::previous());
}
2015-04-01 09:40:19 +02:00
Session::forget(' transactions . edit . fromUpdate ');
2015-04-01 09:12:49 +02:00
2015-02-27 14:27:04 +01:00
2015-07-19 14:30:20 +02:00
return view(' transactions . edit ', compact(' journal ', ' uploadSize ', ' accounts ', ' what ', ' budgets ', ' piggies ', ' subTitle '))->with(' data ', $preFilled);
2015-02-27 14:27:04 +01:00
}
2015-02-23 21:55:52 +01:00
/**
2015-05-04 23:46:14 +02:00
* @param JournalRepositoryInterface $repository
* @param $what
2015-02-23 21:55:52 +01:00
*
2015-05-26 18:57:31 +00:00
* @return \Illuminate\View\View
2015-02-23 21:55:52 +01:00
*/
2015-05-04 23:46:14 +02:00
public function index(JournalRepositoryInterface $repository, $what)
2015-02-23 21:55:52 +01:00
{
2015-07-06 22:12:35 +02:00
$subTitleIcon = Config::get(' firefly . transactionIconsByWhat . ' . $what);
$types = Config::get(' firefly . transactionTypesByWhat . ' . $what);
$subTitle = trans(' firefly . title_ ' . $what);
$page = intval(Input::get(' page '));
$offset = $page > 0 ? ($page - 1) * 50 : 0;
$journals = $repository->getJournalsOfTypes($types, $offset, $page);
2015-05-04 23:46:14 +02:00
2015-02-23 21:55:52 +01:00
$journals->setPath(' transactions / ' . $what);
2015-02-24 22:53:38 +01:00
return view(' transactions . index ', compact(' subTitle ', ' what ', ' subTitleIcon ', ' journals '));
}
2015-03-27 07:20:32 +01:00
/**
2015-05-04 23:46:14 +02:00
* @param JournalRepositoryInterface $repository
*
* @return \Symfony\Component\HttpFoundation\Response
2015-03-27 07:20:32 +01:00
*/
2015-05-04 23:46:14 +02:00
public function reorder(JournalRepositoryInterface $repository)
2015-03-27 07:20:32 +01:00
{
2015-05-04 23:46:14 +02:00
$ids = Input::get(' items ');
$date = new Carbon(Input::get(' date '));
2015-03-27 07:29:01 +01:00
if (count($ids) > 0) {
$order = 0;
foreach ($ids as $id) {
2015-05-04 23:46:14 +02:00
$journal = $repository->getWithDate($id, $date);
2015-03-29 11:51:26 +02:00
if ($journal) {
2015-03-27 07:29:01 +01:00
$journal->order = $order;
$order++;
$journal->save();
}
}
}
2015-06-02 17:44:50 +02:00
Preferences::mark();
2015-03-29 11:51:26 +02:00
2015-05-27 07:58:54 +02:00
return Response::json([true]);
2015-03-27 07:20:32 +01:00
}
2015-02-25 21:19:06 +01:00
/**
2015-05-05 07:48:34 +02:00
* @param JournalRepositoryInterface $repository
* @param TransactionJournal $journal
2015-02-25 21:19:06 +01:00
*
2015-05-26 18:57:31 +00:00
* @return \Illuminate\View\View
2015-02-25 21:19:06 +01:00
*/
2015-05-04 23:46:14 +02:00
public function show(JournalRepositoryInterface $repository, TransactionJournal $journal)
2015-02-25 21:19:06 +01:00
{
2015-12-28 07:27:16 +01:00
/** @var Collection $set */
$events = $journal->piggyBankEvents()->get();
$events->each(
function (PiggyBankEvent $event) {
$event->piggyBank = $event->piggyBank()->withTrashed()->first();
}
);
2015-07-26 19:42:28 +02:00
bcscale(2);
2015-02-25 21:19:06 +01:00
$journal->transactions->each(
2015-06-06 23:09:12 +02:00
function (Transaction $t) use ($journal, $repository) {
2015-05-04 23:46:14 +02:00
$t->before = $repository->getAmountBefore($journal, $t);
2015-07-26 19:42:28 +02:00
$t->after = bcadd($t->before, $t->amount);
2015-02-25 21:19:06 +01:00
}
);
2015-12-09 22:39:50 -02:00
$what = strtolower($journal->getTransactionType());
$subTitle = trans(' firefly . ' . $journal->getTransactionType()) . ' "' . e( $journal->description ) . '" ';
2015-02-25 21:19:06 +01:00
2015-12-29 08:27:05 +01:00
return view(' transactions . show ', compact(' journal ', ' events ', ' subTitle ', ' what '));
2015-02-25 21:19:06 +01:00
}
2015-03-26 17:45:03 +01:00
/**
* @param JournalFormRequest $request
* @param JournalRepositoryInterface $repository
*
2015-12-28 07:55:09 +01:00
* @param AttachmentHelperInterface $att
*
2015-05-26 18:57:31 +00:00
* @return \Illuminate\Http\RedirectResponse
2015-03-26 17:45:03 +01:00
*/
2015-07-18 09:49:59 +02:00
public function store(JournalFormRequest $request, JournalRepositoryInterface $repository, AttachmentHelperInterface $att)
2015-02-24 22:53:38 +01:00
{
2015-04-01 09:12:49 +02:00
2015-03-29 11:51:26 +02:00
$journalData = $request->getJournalData();
2015-07-17 21:03:13 +02:00
// if not withdrawal, unset budgetid.
2015-12-09 22:39:50 -02:00
if ($journalData[' what '] != strtolower(TransactionType::WITHDRAWAL)) {
2015-07-17 21:03:13 +02:00
$journalData[' budget_id '] = 0;
}
2015-07-18 09:49:59 +02:00
$journal = $repository->store($journalData);
// save attachments:
$att->saveAttachmentsForModel($journal);
2015-07-19 13:46:34 +02:00
// flash errors
if (count($att->getErrors()->get(' attachments ')) > 0) {
Session::flash(' error ', $att->getErrors()->get(' attachments '));
2015-07-18 09:49:59 +02:00
}
2015-07-19 13:46:34 +02:00
// flash messages
if (count($att->getMessages()->get(' attachments ')) > 0) {
Session::flash(' info ', $att->getMessages()->get(' attachments '));
2015-07-18 09:49:59 +02:00
}
2015-03-31 14:16:25 +02:00
// rescan journal, UpdateJournalConnection
2015-03-01 08:34:59 +01:00
event(new JournalSaved($journal));
2015-07-18 09:49:59 +02:00
2015-12-09 22:39:50 -02:00
if ($journal->isTransfer() && intval($request->get(' piggy_bank_id ')) > 0) {
2015-05-17 10:30:18 +02:00
event(new JournalCreated($journal, intval($request->get(' piggy_bank_id '))));
}
2015-03-01 08:34:59 +01:00
2015-02-24 22:53:38 +01:00
Session::flash(' success ', ' New transaction "' . $journal->description . '" stored ! ');
2015-06-02 17:44:50 +02:00
Preferences::mark();
2015-03-02 20:05:28 +01:00
2015-03-01 10:44:10 +01:00
if (intval(Input::get(' create_another ')) === 1) {
2015-04-01 09:12:49 +02:00
// set value so create routine will not overwrite URL:
2015-04-01 09:16:41 +02:00
Session::put(' transactions . create . fromStore ', true);
2015-07-06 16:27:21 +02:00
return redirect(route(' transactions . create ', [$request->input(' what ')]))->withInput();
2015-03-01 10:44:10 +01:00
}
2015-02-24 22:53:38 +01:00
2015-04-01 09:12:49 +02:00
// redirect to previous URL.
2015-07-06 16:27:21 +02:00
return redirect(Session::get(' transactions . create . url '));
2015-02-23 21:55:52 +01:00
}
2015-05-05 07:28:04 +02:00
2015-02-27 14:27:04 +01:00
/**
2015-05-03 12:54:39 +02:00
* @param JournalFormRequest $request
* @param JournalRepositoryInterface $repository
2015-07-18 21:46:16 +02:00
* @param AttachmentHelperInterface $att
2015-05-03 12:54:39 +02:00
* @param TransactionJournal $journal
2015-02-27 14:27:04 +01:00
*
2015-07-18 21:46:16 +02:00
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-27 14:27:04 +01:00
*/
2015-07-18 21:46:16 +02:00
public function update(JournalFormRequest $request, JournalRepositoryInterface $repository, AttachmentHelperInterface $att, TransactionJournal $journal)
2015-02-27 14:27:04 +01:00
{
2015-12-09 22:39:50 -02:00
if ($journal->isOpeningBalance()) {
2015-08-15 21:45:29 +02:00
return view(' error ')->with(' message ', ' Cannot edit this transaction . Edit the account instead ! ');
}
2015-03-29 11:51:26 +02:00
$journalData = $request->getJournalData();
2015-02-27 14:27:04 +01:00
$repository->update($journal, $journalData);
2015-03-01 08:34:59 +01:00
2015-07-18 21:46:16 +02:00
// save attachments:
$att->saveAttachmentsForModel($journal);
2015-07-19 14:30:20 +02:00
// flash errors
if (count($att->getErrors()->get(' attachments ')) > 0) {
Session::flash(' error ', $att->getErrors()->get(' attachments '));
2015-07-18 21:46:16 +02:00
}
2015-07-19 14:30:20 +02:00
// flash messages
if (count($att->getMessages()->get(' attachments ')) > 0) {
Session::flash(' info ', $att->getMessages()->get(' attachments '));
2015-07-18 21:46:16 +02:00
}
2015-03-01 08:34:59 +01:00
event(new JournalSaved($journal));
2015-03-02 20:05:28 +01:00
// update, get events by date and sort DESC
2015-03-01 08:34:59 +01:00
2015-02-27 14:27:04 +01:00
Session::flash(' success ', ' Transaction "' . e( $journalData['description'] ) . '" updated . ');
2015-06-02 17:44:50 +02:00
Preferences::mark();
2015-02-27 14:27:04 +01:00
2015-03-01 10:44:10 +01:00
if (intval(Input::get(' return_to_edit ')) === 1) {
2015-04-01 09:12:49 +02:00
// set value so edit routine will not overwrite URL:
2015-04-01 09:40:19 +02:00
Session::put(' transactions . edit . fromUpdate ', true);
2015-04-01 09:16:41 +02:00
2015-07-06 16:27:21 +02:00
return redirect(route(' transactions . edit ', [$journal->id]))->withInput([' return_to_edit ' => 1]);
2015-03-01 10:44:10 +01:00
}
2015-04-01 09:12:49 +02:00
// redirect to previous URL.
2015-07-06 16:27:21 +02:00
return redirect(Session::get(' transactions . edit . url ' ));
2015-02-27 14:27:04 +01:00
}
2015-02-23 21:55:52 +01:00
}