2021-03-05 20:17:39 +01:00
< ? php
2021-08-10 19:31:55 +02:00
2021-03-05 20:17:39 +01:00
/*
2021-08-10 19:31:55 +02:00
* BillController . php
2021-03-05 20:17:39 +01:00
* Copyright ( c ) 2021 james @ firefly - iii . org
*
* This file is part of Firefly III ( https :// github . com / firefly - iii ) .
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program 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 Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
2021-08-10 19:31:55 +02:00
declare ( strict_types = 1 );
2021-03-05 20:17:39 +01:00
namespace FireflyIII\Api\V1\Controllers\Insight\Expense ;
use FireflyIII\Api\V1\Controllers\Controller ;
use FireflyIII\Api\V1\Requests\Insight\GenericRequest ;
2024-11-25 07:14:14 +01:00
use FireflyIII\Enums\TransactionTypeEnum ;
2021-03-05 20:17:39 +01:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface ;
use FireflyIII\Repositories\Bill\BillRepositoryInterface ;
2024-12-29 13:47:48 +01:00
use FireflyIII\Support\Facades\Amount ;
2021-03-05 20:17:39 +01:00
use Illuminate\Http\JsonResponse ;
2024-12-29 13:47:48 +01:00
use Illuminate\Support\Facades\Log ;
2021-03-05 20:17:39 +01:00
/**
* Class BillController
*/
class BillController extends Controller
{
private BillRepositoryInterface $repository ;
/**
* BillController constructor .
*/
public function __construct ()
{
parent :: __construct ();
$this -> middleware (
function ( $request , $next ) {
$user = auth () -> user ();
$this -> repository = app ( BillRepositoryInterface :: class );
$this -> repository -> setUser ( $user );
return $next ( $request );
}
);
}
/**
2021-09-18 05:58:22 +02:00
* This endpoint is documented at :
2023-02-12 06:53:36 +01:00
* https :// api - docs . firefly - iii . org / ? urls . primaryName = 2.0 . 0 % 20 ( v1 ) #/insight/insightExpenseBill
2021-09-18 05:58:22 +02:00
*
2021-03-05 20:17:39 +01:00
* Expenses per bill , possibly filtered by bill and account .
*/
public function bill ( GenericRequest $request ) : JsonResponse
{
2024-12-29 13:47:48 +01:00
$accounts = $request -> getAssetAccounts ();
$bills = $request -> getBills ();
$start = $request -> getStart ();
$end = $request -> getEnd ();
$convertToNative = Amount :: convertToNative ();
2025-01-19 19:07:19 +01:00
$default = Amount :: getNativeCurrency ();
2024-12-29 13:47:48 +01:00
$response = [];
2021-03-05 20:17:39 +01:00
// get all bills:
if ( 0 === $bills -> count ()) {
$bills = $this -> repository -> getBills ();
}
// collect all expenses in this period (regardless of type) by the given bills and accounts.
2024-12-30 04:12:18 +01:00
$collector = app ( GroupCollectorInterface :: class );
2024-11-25 07:14:14 +01:00
$collector -> setTypes ([ TransactionTypeEnum :: WITHDRAWAL -> value ]) -> setRange ( $start , $end ) -> setSourceAccounts ( $accounts );
2021-03-05 20:17:39 +01:00
$collector -> setBills ( $bills );
2024-12-30 04:12:18 +01:00
$genericSet = $collector -> getExtractedJournals ();
2021-03-05 20:17:39 +01:00
foreach ( $genericSet as $journal ) {
2024-12-29 13:47:48 +01:00
$billId = ( int ) $journal [ 'bill_id' ];
$currencyId = ( int ) $journal [ 'currency_id' ];
$currencyCode = $journal [ 'currency_code' ];
$field = 'amount' ;
// use the native amount if the user wants to convert to native currency
if ( $convertToNative && $currencyId !== $default -> id ) {
$currencyId = $default -> id ;
$currencyCode = $default -> code ;
$field = 'native_amount' ;
}
// use foreign amount when the foreign currency IS the default currency.
if ( $convertToNative && $journal [ 'currency_id' ] !== $default -> id && $default -> id === $journal [ 'foreign_currency_id' ]) {
$field = 'foreign_amount' ;
}
Log :: debug ( sprintf ( 'Journal #%d in bill #%d will use %s (%s %s)' , $journal [ 'transaction_group_id' ], $billId , $field , $currencyCode , $journal [ $field ] ? ? '0' ));
2024-12-30 04:12:18 +01:00
$key = sprintf ( '%d-%d' , $billId , $currencyId );
2021-03-05 20:17:39 +01:00
if ( 0 !== $currencyId ) {
2024-12-30 04:12:18 +01:00
$response [ $key ] ? ? = [
2024-12-22 08:43:12 +01:00
'id' => ( string ) $billId ,
2022-12-29 19:41:57 +01:00
'name' => $journal [ 'bill_name' ],
'difference' => '0' ,
'difference_float' => 0 ,
2024-12-22 08:43:12 +01:00
'currency_id' => ( string ) $currencyId ,
2024-12-29 13:47:48 +01:00
'currency_code' => $currencyCode ,
2022-12-29 19:41:57 +01:00
];
2024-12-29 13:47:48 +01:00
$response [ $key ][ 'difference' ] = bcadd ( $response [ $key ][ 'difference' ], ( string ) ( $journal [ $field ] ? ? '0' ));
2024-12-22 08:43:12 +01:00
$response [ $key ][ 'difference_float' ] = ( float ) $response [ $key ][ 'difference' ]; // intentional float
2021-03-05 20:17:39 +01:00
}
}
return response () -> json ( array_values ( $response ));
}
/**
2021-09-18 05:58:22 +02:00
* This endpoint is documented at :
2023-02-12 06:53:36 +01:00
* https :// api - docs . firefly - iii . org / ? urls . primaryName = 2.0 . 0 % 20 ( v1 ) #/insight/insightExpenseNoBill
2021-09-18 05:58:22 +02:00
*
2021-03-05 20:17:39 +01:00
* Expenses for no bill filtered by account .
*/
public function noBill ( GenericRequest $request ) : JsonResponse
{
2024-12-29 13:47:48 +01:00
$accounts = $request -> getAssetAccounts ();
$start = $request -> getStart ();
$end = $request -> getEnd ();
$convertToNative = Amount :: convertToNative ();
2025-01-19 19:07:19 +01:00
$default = Amount :: getNativeCurrency ();
2024-12-29 13:47:48 +01:00
$response = [];
2021-03-05 20:17:39 +01:00
// collect all expenses in this period (regardless of type) by the given bills and accounts.
2024-12-30 04:12:18 +01:00
$collector = app ( GroupCollectorInterface :: class );
2024-11-25 07:14:14 +01:00
$collector -> setTypes ([ TransactionTypeEnum :: WITHDRAWAL -> value ]) -> setRange ( $start , $end ) -> setSourceAccounts ( $accounts );
2021-03-05 20:17:39 +01:00
$collector -> withoutBill ();
2024-12-30 04:12:18 +01:00
$genericSet = $collector -> getExtractedJournals ();
2021-03-05 20:17:39 +01:00
foreach ( $genericSet as $journal ) {
2024-12-29 13:47:48 +01:00
$currencyId = ( int ) $journal [ 'currency_id' ];
$currencyCode = $journal [ 'currency_code' ];
$field = 'amount' ;
// use the native amount if the user wants to convert to native currency
if ( $convertToNative && $currencyId !== $default -> id ) {
$currencyId = $default -> id ;
$currencyCode = $default -> code ;
$field = 'native_amount' ;
}
// use foreign amount when the foreign currency IS the default currency.
if ( $convertToNative && $journal [ 'currency_id' ] !== $default -> id && $default -> id === $journal [ 'foreign_currency_id' ]) {
$field = 'foreign_amount' ;
}
Log :: debug ( sprintf ( 'Journal #%d will use %s (%s %s)' , $journal [ 'transaction_group_id' ], $field , $currencyCode , $journal [ $field ] ? ? '0' ));
2021-03-05 20:17:39 +01:00
if ( 0 !== $currencyId ) {
2024-12-30 04:12:18 +01:00
$response [ $currencyId ] ? ? = [
2022-12-29 19:41:57 +01:00
'difference' => '0' ,
'difference_float' => 0 ,
2024-12-22 08:43:12 +01:00
'currency_id' => ( string ) $currencyId ,
2024-12-29 13:47:48 +01:00
'currency_code' => $currencyCode ,
2022-12-29 19:41:57 +01:00
];
2024-12-29 13:47:48 +01:00
$response [ $currencyId ][ 'difference' ] = bcadd ( $response [ $currencyId ][ 'difference' ], ( string ) ( $journal [ $field ] ? ? '0' ));
2024-12-22 08:43:12 +01:00
$response [ $currencyId ][ 'difference_float' ] = ( float ) $response [ $currencyId ][ 'difference' ]; // intentional float
2021-03-05 20:17:39 +01:00
}
}
return response () -> json ( array_values ( $response ));
}
2021-03-28 11:39:26 +02:00
}