2016-05-20 11:09:02 +02:00
< ? php
/**
* MassController . php
2017-10-21 08:40:00 +02:00
* Copyright ( c ) 2017 thegrumpydictator @ gmail . com
2016-05-20 11:09:02 +02:00
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III .
2016-10-05 06:52:15 +02:00
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* Firefly III is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:41:58 +01:00
* along with Firefly III . If not , see < http :// www . gnu . org / licenses />.
2016-05-20 11:09:02 +02:00
*/
2017-04-09 07:44:22 +02:00
declare ( strict_types = 1 );
2016-05-20 12:41:23 +02:00
2016-05-20 11:09:02 +02:00
namespace FireflyIII\Http\Controllers\Transaction ;
use Carbon\Carbon ;
2019-03-30 11:03:39 +01:00
use FireflyIII\Events\UpdatedTransactionGroup ;
use FireflyIII\Exceptions\FireflyException ;
2019-05-31 13:35:33 +02:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface ;
2018-05-29 07:25:04 +02:00
use FireflyIII\Helpers\Filter\TransactionViewFilter ;
2018-09-26 20:44:43 +02:00
use FireflyIII\Helpers\Filter\TransferFilter ;
2016-05-20 11:09:02 +02:00
use FireflyIII\Http\Controllers\Controller ;
use FireflyIII\Http\Requests\MassDeleteJournalRequest ;
2018-02-09 19:11:55 +01:00
use FireflyIII\Http\Requests\MassEditJournalRequest ;
2016-05-20 11:09:02 +02:00
use FireflyIII\Models\AccountType ;
2018-04-24 19:26:16 +02:00
use FireflyIII\Models\Transaction ;
2016-05-20 11:09:02 +02:00
use FireflyIII\Models\TransactionJournal ;
2016-10-10 07:49:39 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface ;
2017-03-09 08:19:05 +01:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface ;
2016-05-20 11:09:02 +02:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface ;
2018-04-24 19:26:16 +02:00
use FireflyIII\Transformers\TransactionTransformer ;
2018-07-09 19:24:08 +02:00
use FireflyIII\User ;
2016-05-20 11:09:02 +02:00
use Illuminate\Support\Collection ;
2018-06-06 21:23:00 +02:00
use Illuminate\View\View as IlluminateView ;
2018-04-24 19:26:16 +02:00
use Symfony\Component\HttpFoundation\ParameterBag ;
2016-05-20 11:09:02 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class MassController .
2018-07-20 14:34:56 +02:00
*
* @ SuppressWarnings ( PHPMD . CouplingBetweenObjects )
2016-05-20 11:09:02 +02:00
*/
class MassController extends Controller
{
2018-07-20 20:53:48 +02:00
/** @var JournalRepositoryInterface Journals and transactions overview */
2018-02-25 19:09:05 +01:00
private $repository ;
2016-05-20 11:09:02 +02:00
/**
2018-07-22 08:10:16 +02:00
* MassController constructor .
2016-05-20 11:09:02 +02:00
*/
public function __construct ()
{
parent :: __construct ();
2016-10-29 07:44:46 +02:00
$this -> middleware (
function ( $request , $next ) {
2018-07-15 09:38:49 +02:00
app ( 'view' ) -> share ( 'title' , ( string ) trans ( 'firefly.transactions' ));
2017-12-16 19:46:36 +01:00
app ( 'view' ) -> share ( 'mainTitleIcon' , 'fa-repeat' );
2018-02-25 19:09:05 +01:00
$this -> repository = app ( JournalRepositoryInterface :: class );
2016-10-29 07:44:46 +02:00
return $next ( $request );
}
);
2016-05-20 11:09:02 +02:00
}
/**
2018-07-22 08:10:16 +02:00
* Mass delete transactions .
*
2016-05-20 11:09:02 +02:00
* @ param Collection $journals
*
2018-04-27 11:29:09 +02:00
* @ return IlluminateView
2016-05-20 11:09:02 +02:00
*/
2018-04-27 11:29:09 +02:00
public function delete ( Collection $journals ) : IlluminateView
2016-05-20 11:09:02 +02:00
{
2018-07-15 09:38:49 +02:00
$subTitle = ( string ) trans ( 'firefly.mass_delete_journals' );
2016-05-20 11:09:02 +02:00
// put previous url in session
2017-02-05 08:26:54 +01:00
$this -> rememberPreviousUri ( 'transactions.mass-delete.uri' );
2016-05-20 11:09:02 +02:00
2017-12-31 09:01:27 +01:00
return view ( 'transactions.mass.delete' , compact ( 'journals' , 'subTitle' ));
2016-05-20 11:09:02 +02:00
}
/**
2018-07-22 08:10:16 +02:00
* Do the mass delete .
*
2018-02-25 19:09:05 +01:00
* @ param MassDeleteJournalRequest $request
2016-05-20 11:09:02 +02:00
*
* @ return mixed
2018-07-20 14:34:56 +02:00
* @ SuppressWarnings ( PHPMD . CyclomaticComplexity )
2016-05-20 11:09:02 +02:00
*/
2018-02-25 19:09:05 +01:00
public function destroy ( MassDeleteJournalRequest $request )
2016-05-20 11:09:02 +02:00
{
2018-07-20 14:34:56 +02:00
$ids = $request -> get ( 'confirm_mass_delete' );
$count = 0 ;
2019-06-22 13:09:25 +02:00
if ( is_array ( $ids )) {
2018-04-24 19:26:16 +02:00
/** @var string $journalId */
2016-05-20 11:09:02 +02:00
foreach ( $ids as $journalId ) {
/** @var TransactionJournal $journal */
2018-04-24 19:26:16 +02:00
$journal = $this -> repository -> findNull (( int ) $journalId );
if ( null !== $journal && ( int ) $journalId === $journal -> id ) {
2018-07-20 14:34:56 +02:00
$this -> repository -> destroy ( $journal );
++ $count ;
2016-05-20 11:09:02 +02:00
}
}
}
2018-07-08 12:08:53 +02:00
app ( 'preferences' ) -> mark ();
2018-07-15 09:38:49 +02:00
session () -> flash ( 'success' , ( string ) trans ( 'firefly.mass_deleted_transactions_success' , [ 'amount' => $count ]));
2016-05-20 11:09:02 +02:00
// redirect to previous URL:
2017-02-05 08:26:54 +01:00
return redirect ( $this -> getPreviousUri ( 'transactions.mass-delete.uri' ));
2016-05-20 11:09:02 +02:00
}
/**
2018-07-22 08:10:16 +02:00
* Mass edit of journals .
*
2016-05-20 11:09:02 +02:00
* @ param Collection $journals
*
2018-04-28 06:23:13 +02:00
* @ return IlluminateView
2019-05-31 13:35:33 +02:00
*
* TODO rebuild this feature .
* @ throws FireflyException
2016-05-20 11:09:02 +02:00
*/
2018-04-27 11:29:09 +02:00
public function edit ( Collection $journals ) : IlluminateView
2016-05-20 11:09:02 +02:00
{
2019-05-31 13:35:33 +02:00
throw new FireflyException ( sprintf ( 'The mass-editor is not available in v%s of Firefly III. Sorry about that. It will be back soon.' , config ( 'firefly.version' )));
2018-07-09 19:24:08 +02:00
/** @var User $user */
2018-07-13 15:50:42 +02:00
$user = auth () -> user ();
2018-07-15 09:38:49 +02:00
$subTitle = ( string ) trans ( 'firefly.mass_edit_journals' );
2016-10-10 07:49:39 +02:00
/** @var AccountRepositoryInterface $repository */
2017-03-09 08:19:05 +01:00
$repository = app ( AccountRepositoryInterface :: class );
$accounts = $repository -> getAccountsByType ([ AccountType :: DEFAULT , AccountType :: ASSET ]);
/** @var BudgetRepositoryInterface $budgetRepository */
$budgetRepository = app ( BudgetRepositoryInterface :: class );
$budgets = $budgetRepository -> getBudgets ();
2016-05-20 11:09:02 +02:00
2018-04-24 19:26:16 +02:00
$this -> rememberPreviousUri ( 'transactions.mass-edit.uri' );
2016-07-17 08:50:22 +02:00
2018-12-17 07:09:44 +01:00
/** @var TransactionTransformer $transformer */
$transformer = app ( TransactionTransformer :: class );
$transformer -> setParameters ( new ParameterBag );
2019-05-31 13:35:33 +02:00
/** @var GroupCollectorInterface $collector */
$collector = app ( GroupCollectorInterface :: class );
2018-07-09 19:24:08 +02:00
$collector -> setUser ( $user );
2018-04-24 19:26:16 +02:00
$collector -> withOpposingAccount () -> withCategoryInformation () -> withBudgetInformation ();
$collector -> setJournals ( $journals );
2018-05-29 07:25:04 +02:00
$collector -> addFilter ( TransactionViewFilter :: class );
2018-09-26 20:44:43 +02:00
$collector -> addFilter ( TransferFilter :: class );
2018-08-11 14:33:47 +02:00
$collection = $collector -> getTransactions ();
2018-05-29 07:25:04 +02:00
$transactions = $collection -> map (
2018-04-24 19:26:16 +02:00
function ( Transaction $transaction ) use ( $transformer ) {
2018-07-08 07:59:58 +02:00
$transformed = $transformer -> transform ( $transaction );
2018-05-29 07:25:04 +02:00
// make sure amount is positive:
2018-07-08 07:59:58 +02:00
$transformed [ 'amount' ] = app ( 'steam' ) -> positive (( string ) $transformed [ 'amount' ]);
$transformed [ 'foreign_amount' ] = app ( 'steam' ) -> positive (( string ) $transformed [ 'foreign_amount' ]);
2018-06-06 21:23:00 +02:00
2018-07-08 07:59:58 +02:00
return $transformed ;
2016-05-20 11:27:41 +02:00
}
);
2018-05-29 07:25:04 +02:00
return view ( 'transactions.mass.edit' , compact ( 'transactions' , 'subTitle' , 'accounts' , 'budgets' ));
2016-05-20 11:09:02 +02:00
}
/**
2018-07-22 08:10:16 +02:00
* Mass update of journals .
*
2019-05-31 13:35:33 +02:00
* @ param MassEditJournalRequest $request
2016-05-20 11:09:02 +02:00
* @ param JournalRepositoryInterface $repository
*
* @ return mixed
2018-07-20 14:34:56 +02:00
* @ SuppressWarnings ( PHPMD . ExcessiveMethodLength )
* @ SuppressWarnings ( PHPMD . CyclomaticComplexity )
2016-05-20 11:09:02 +02:00
*/
2017-06-07 08:18:42 +02:00
public function update ( MassEditJournalRequest $request , JournalRepositoryInterface $repository )
2016-05-20 11:09:02 +02:00
{
2019-03-30 11:03:39 +01:00
throw new FireflyException ( 'Needs refactor' );
2016-05-20 11:09:02 +02:00
$journalIds = $request -> get ( 'journals' );
$count = 0 ;
2019-06-22 13:09:25 +02:00
if ( is_array ( $journalIds )) {
2016-05-20 11:09:02 +02:00
foreach ( $journalIds as $journalId ) {
2018-07-08 07:59:58 +02:00
$journal = $repository -> findNull (( int ) $journalId );
2018-04-02 15:10:40 +02:00
if ( null !== $journal ) {
2016-05-20 11:09:02 +02:00
// get optional fields:
2018-02-28 20:23:45 +01:00
$what = strtolower ( $this -> repository -> getTransactionType ( $journal ));
2018-06-30 05:21:21 +02:00
$sourceAccountId = $request -> get ( 'source_id' )[ $journal -> id ] ? ? null ;
2018-03-04 09:12:33 +01:00
$currencyId = $request -> get ( 'transaction_currency_id' )[ $journal -> id ] ? ? 1 ;
2018-06-30 05:21:21 +02:00
$sourceAccountName = $request -> get ( 'source_name' )[ $journal -> id ] ? ? null ;
$destAccountId = $request -> get ( 'destination_id' )[ $journal -> id ] ? ? null ;
$destAccountName = $request -> get ( 'destination_name' )[ $journal -> id ] ? ? null ;
2018-04-02 15:10:40 +02:00
$budgetId = ( int )( $request -> get ( 'budget_id' )[ $journal -> id ] ? ? 0.0 );
2016-11-12 06:34:54 +01:00
$category = $request -> get ( 'category' )[ $journal -> id ];
$tags = $journal -> tags -> pluck ( 'tag' ) -> toArray ();
2017-06-04 13:39:16 +02:00
$amount = round ( $request -> get ( 'amount' )[ $journal -> id ], 12 );
$foreignAmount = isset ( $request -> get ( 'foreign_amount' )[ $journal -> id ]) ? round ( $request -> get ( 'foreign_amount' )[ $journal -> id ], 12 ) : null ;
$foreignCurrencyId = isset ( $request -> get ( 'foreign_currency_id' )[ $journal -> id ]) ?
2018-04-02 15:10:40 +02:00
( int ) $request -> get ( 'foreign_currency_id' )[ $journal -> id ] : null ;
2016-05-20 11:09:02 +02:00
// build data array
$data = [
2018-03-04 09:12:33 +01:00
'id' => $journal -> id ,
'what' => $what ,
'description' => $request -> get ( 'description' )[ $journal -> id ],
'date' => new Carbon ( $request -> get ( 'date' )[ $journal -> id ]),
'bill_id' => null ,
'bill_name' => null ,
2018-03-25 09:01:43 +02:00
'notes' => $repository -> getNoteText ( $journal ),
2018-03-04 09:12:33 +01:00
'transactions' => [[
'category_id' => null ,
'category_name' => $category ,
2018-07-08 07:59:58 +02:00
'budget_id' => $budgetId ,
2018-03-04 09:12:33 +01:00
'budget_name' => null ,
2018-04-02 15:10:40 +02:00
'source_id' => ( int ) $sourceAccountId ,
2018-03-04 09:12:33 +01:00
'source_name' => $sourceAccountName ,
2018-04-02 15:10:40 +02:00
'destination_id' => ( int ) $destAccountId ,
2018-03-04 09:12:33 +01:00
'destination_name' => $destAccountName ,
'amount' => $amount ,
'identifier' => 0 ,
'reconciled' => false ,
2018-04-02 15:10:40 +02:00
'currency_id' => ( int ) $currencyId ,
2018-03-04 09:12:33 +01:00
'currency_code' => null ,
'description' => null ,
2018-03-25 09:01:43 +02:00
'foreign_amount' => $foreignAmount ,
2018-03-04 09:12:33 +01:00
'foreign_currency_id' => $foreignCurrencyId ,
'foreign_currency_code' => null ,
]],
'currency_id' => $foreignCurrencyId ,
'tags' => $tags ,
'interest_date' => $journal -> interest_date ,
'book_date' => $journal -> book_date ,
'process_date' => $journal -> process_date ,
2016-05-20 11:09:02 +02:00
];
// call repository update function.
2017-06-07 08:18:42 +02:00
$repository -> update ( $journal , $data );
2016-05-20 11:09:02 +02:00
2019-02-02 07:00:09 +01:00
// trigger rules
2019-03-30 11:03:39 +01:00
event ( new UpdatedTransactionGroup ( $group ));
2019-02-02 07:00:09 +01:00
2017-11-15 12:25:49 +01:00
++ $count ;
2016-05-20 11:09:02 +02:00
}
}
}
2018-07-08 12:08:53 +02:00
app ( 'preferences' ) -> mark ();
2018-07-15 09:38:49 +02:00
session () -> flash ( 'success' , ( string ) trans ( 'firefly.mass_edited_transactions_success' , [ 'amount' => $count ]));
2016-05-20 11:09:02 +02:00
// redirect to previous URL:
2017-02-05 08:26:54 +01:00
return redirect ( $this -> getPreviousUri ( 'transactions.mass-edit.uri' ));
2016-05-20 11:09:02 +02:00
}
2017-12-30 09:21:28 +01:00
2016-08-12 15:10:03 +02:00
}