2019-08-29 21:33:12 +02:00
< ? php
/**
* AvailableBudgetRepository . php
2020-02-16 14:00:57 +01:00
* Copyright ( c ) 2019 james @ firefly - iii . org
2019-08-29 21:33:12 +02:00
*
2019-10-02 06:37:26 +02:00
* This file is part of Firefly III ( https :// github . com / firefly - iii ) .
2019-08-29 21:33:12 +02:00
*
2019-10-02 06:37:26 +02:00
* 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 .
2019-08-29 21:33:12 +02:00
*
2019-10-02 06:37:26 +02:00
* This program is distributed in the hope that it will be useful ,
2019-08-29 21:33:12 +02:00
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
2019-10-02 06:37:26 +02:00
* GNU Affero General Public License for more details .
2019-08-29 21:33:12 +02:00
*
2019-10-02 06:37:26 +02:00
* 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 />.
2019-08-29 21:33:12 +02:00
*/
declare ( strict_types = 1 );
namespace FireflyIII\Repositories\Budget ;
2019-08-30 08:00:52 +02:00
use Carbon\Carbon ;
2019-08-30 07:45:48 +02:00
use FireflyIII\Models\AvailableBudget ;
2019-08-30 08:00:52 +02:00
use FireflyIII\Models\TransactionCurrency ;
2019-08-29 21:33:12 +02:00
use FireflyIII\User ;
2023-02-19 08:43:28 +01:00
use Illuminate\Contracts\Auth\Authenticatable ;
2019-08-31 09:35:35 +02:00
use Illuminate\Database\Eloquent\Builder ;
2019-08-30 08:02:11 +02:00
use Illuminate\Support\Collection ;
2019-08-29 21:33:12 +02:00
/**
*
* Class AvailableBudgetRepository
*/
class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
{
2020-09-18 16:14:17 +02:00
private User $user ;
2019-08-29 21:33:12 +02:00
2023-08-15 17:57:02 +02:00
/**
* @ inheritDoc
*/
public function cleanup () : void
{
$exists = [];
$availableBudgets = $this -> user -> availableBudgets () -> get ();
/** @var AvailableBudget $availableBudget */
foreach ( $availableBudgets as $availableBudget ) {
$start = $availableBudget -> start_date -> format ( 'Y-m-d' );
$end = $availableBudget -> end_date -> format ( 'Y-m-d' );
$key = sprintf ( '%s-%s-%s' , $availableBudget -> transaction_currency_id , $start , $end );
if ( array_key_exists ( $key , $exists )) {
app ( 'log' ) -> debug ( sprintf ( 'Found duplicate AB: %s %s, %s-%s. Has been deleted' , $availableBudget -> transaction_currency_id , $availableBudget -> amount , $start , $end ));
$availableBudget -> delete ();
}
$exists [ $key ] = true ;
}
}
/**
* Return a list of all available budgets ( in all currencies ) ( for the selected period ) .
*
* @ param Carbon | null $start
* @ param Carbon | null $end
*
* @ return Collection
*/
public function get ( ? Carbon $start = null , ? Carbon $end = null ) : Collection
{
$query = $this -> user -> availableBudgets () -> with ([ 'transactionCurrency' ]);
if ( null !== $start && null !== $end ) {
$query -> where (
static function ( Builder $q1 ) use ( $start , $end ) {
$q1 -> where ( 'start_date' , '=' , $start -> format ( 'Y-m-d' ));
$q1 -> where ( 'end_date' , '=' , $end -> format ( 'Y-m-d' ));
}
);
}
return $query -> get ([ 'available_budgets.*' ]);
}
2021-03-12 06:20:01 +01:00
/**
* Delete all available budgets .
*/
public function destroyAll () : void
{
$this -> user -> availableBudgets () -> delete ();
}
2019-08-30 07:45:48 +02:00
/**
2023-06-21 12:34:58 +02:00
* @ param AvailableBudget $availableBudget
2019-08-30 07:45:48 +02:00
*/
public function destroyAvailableBudget ( AvailableBudget $availableBudget ) : void
{
2022-12-30 09:28:03 +01:00
$availableBudget -> delete ();
2019-08-30 07:45:48 +02:00
}
2023-05-29 13:56:55 +02:00
/**
* @ inheritDoc
*/
public function findById ( int $id ) : ? AvailableBudget
{
return $this -> user -> availableBudgets -> find ( $id );
}
/**
2023-06-21 12:34:58 +02:00
* Find existing AB .
2023-05-29 13:56:55 +02:00
*
2023-06-21 12:34:58 +02:00
* @ param TransactionCurrency $currency
* @ param Carbon $start
* @ param Carbon $end
2023-05-29 13:56:55 +02:00
*
2023-06-21 12:34:58 +02:00
* @ return AvailableBudget | null
2023-05-29 13:56:55 +02:00
*/
2023-06-21 12:34:58 +02:00
public function find ( TransactionCurrency $currency , Carbon $start , Carbon $end ) : ? AvailableBudget
2023-05-29 13:56:55 +02:00
{
2023-06-21 12:34:58 +02:00
return $this -> user -> availableBudgets ()
-> where ( 'transaction_currency_id' , $currency -> id )
-> where ( 'start_date' , $start -> format ( 'Y-m-d' ))
-> where ( 'end_date' , $end -> format ( 'Y-m-d' ))
-> first ();
2023-05-29 13:56:55 +02:00
}
2019-08-30 08:00:52 +02:00
/**
2023-06-21 12:34:58 +02:00
* @ param TransactionCurrency $currency
* @ param Carbon $start
* @ param Carbon $end
2019-08-30 08:00:52 +02:00
*
* @ return string
*/
public function getAvailableBudget ( TransactionCurrency $currency , Carbon $start , Carbon $end ) : string
{
$amount = '0' ;
$availableBudget = $this -> user -> availableBudgets ()
-> where ( 'transaction_currency_id' , $currency -> id )
2020-09-24 06:09:26 +02:00
-> where ( 'start_date' , $start -> format ( 'Y-m-d' ))
-> where ( 'end_date' , $end -> format ( 'Y-m-d' )) -> first ();
2019-08-30 08:00:52 +02:00
if ( null !== $availableBudget ) {
2022-12-29 19:42:26 +01:00
$amount = ( string ) $availableBudget -> amount ;
2019-08-30 08:00:52 +02:00
}
return $amount ;
}
/**
2023-06-21 12:34:58 +02:00
* @ param Carbon $start
* @ param Carbon $end
2019-08-30 08:00:52 +02:00
*
* @ return array
*/
public function getAvailableBudgetWithCurrency ( Carbon $start , Carbon $end ) : array
{
$return = [];
$availableBudgets = $this -> user -> availableBudgets ()
2020-09-24 06:09:26 +02:00
-> where ( 'start_date' , $start -> format ( 'Y-m-d' ))
-> where ( 'end_date' , $end -> format ( 'Y-m-d' )) -> get ();
2019-08-30 08:00:52 +02:00
/** @var AvailableBudget $availableBudget */
foreach ( $availableBudgets as $availableBudget ) {
$return [ $availableBudget -> transaction_currency_id ] = $availableBudget -> amount ;
}
return $return ;
}
2019-08-30 08:03:13 +02:00
/**
* Returns all available budget objects .
*
2023-06-21 12:34:58 +02:00
* @ param TransactionCurrency $currency
2019-08-30 08:03:13 +02:00
*
* @ return Collection
*/
public function getAvailableBudgetsByCurrency ( TransactionCurrency $currency ) : Collection
{
return $this -> user -> availableBudgets () -> where ( 'transaction_currency_id' , $currency -> id ) -> get ();
}
/**
* Returns all available budget objects .
*
2023-06-21 12:34:58 +02:00
* @ param Carbon | null $start
* @ param Carbon | null $end
2019-08-30 08:03:13 +02:00
*
* @ return Collection
*
*/
public function getAvailableBudgetsByDate ( ? Carbon $start , ? Carbon $end ) : Collection
{
$query = $this -> user -> availableBudgets ();
if ( null !== $start ) {
2020-09-24 06:09:26 +02:00
$query -> where ( 'start_date' , '>=' , $start -> format ( 'Y-m-d' ));
2019-08-30 08:03:13 +02:00
}
if ( null !== $end ) {
2020-09-24 06:09:26 +02:00
$query -> where ( 'end_date' , '<=' , $end -> format ( 'Y-m-d' ));
2019-08-30 08:03:13 +02:00
}
return $query -> get ();
}
2023-08-15 18:37:47 +02:00
/**
* Returns all available budget objects .
*
* @ param Carbon $start
* @ param Carbon $end
*
* @ return Collection
*
*/
public function getAvailableBudgetsByExactDate ( Carbon $start , Carbon $end ) : Collection
{
return $this -> user -> availableBudgets ()
-> where ( 'start_date' , '=' , $start -> format ( 'Y-m-d' ))
-> where ( 'end_date' , '=' , $end -> format ( 'Y-m-d' ))
-> get ();
}
2021-03-12 06:20:01 +01:00
/**
* @ inheritDoc
*/
public function getByCurrencyDate ( Carbon $start , Carbon $end , TransactionCurrency $currency ) : ? AvailableBudget
{
return $this -> user
-> availableBudgets ()
-> where ( 'transaction_currency_id' , $currency -> id )
-> where ( 'start_date' , $start -> format ( 'Y-m-d' ))
-> where ( 'end_date' , $end -> format ( 'Y-m-d' )) -> first ();
}
2019-08-30 08:19:55 +02:00
/**
2023-06-21 12:34:58 +02:00
* @ param TransactionCurrency $currency
* @ param Carbon $start
* @ param Carbon $end
* @ param string $amount
2019-08-30 08:19:55 +02:00
*
* @ return AvailableBudget
2019-09-04 17:39:39 +02:00
* @ deprecated
2019-08-30 08:19:55 +02:00
*/
public function setAvailableBudget ( TransactionCurrency $currency , Carbon $start , Carbon $end , string $amount ) : AvailableBudget
{
$availableBudget = $this -> user -> availableBudgets ()
-> where ( 'transaction_currency_id' , $currency -> id )
2020-09-24 06:09:26 +02:00
-> where ( 'start_date' , $start -> format ( 'Y-m-d' ))
-> where ( 'end_date' , $end -> format ( 'Y-m-d' )) -> first ();
2019-08-30 08:19:55 +02:00
if ( null === $availableBudget ) {
2022-10-30 14:24:28 +01:00
$availableBudget = new AvailableBudget ();
2019-08-30 08:19:55 +02:00
$availableBudget -> user () -> associate ( $this -> user );
$availableBudget -> transactionCurrency () -> associate ( $currency );
2020-09-24 06:09:26 +02:00
$availableBudget -> start_date = $start -> format ( 'Y-m-d' );
$availableBudget -> end_date = $end -> format ( 'Y-m-d' );
2019-08-30 08:19:55 +02:00
}
$availableBudget -> amount = $amount ;
$availableBudget -> save ();
return $availableBudget ;
}
2019-08-29 21:33:12 +02:00
/**
2023-06-21 12:34:58 +02:00
* @ param User | Authenticatable | null $user
2019-08-29 21:33:12 +02:00
*/
2023-06-21 12:34:58 +02:00
public function setUser ( User | Authenticatable | null $user ) : void
2019-08-29 21:33:12 +02:00
{
2023-02-19 11:16:15 +01:00
if ( null !== $user ) {
2023-02-19 08:43:28 +01:00
$this -> user = $user ;
}
2019-08-29 21:33:12 +02:00
}
2019-08-31 09:35:35 +02:00
/**
2023-06-21 12:34:58 +02:00
* @ param array $data
2019-08-31 09:35:35 +02:00
*
* @ return AvailableBudget | null
*/
public function store ( array $data ) : ? AvailableBudget
{
2020-09-24 06:06:18 +02:00
$start = $data [ 'start' ];
2021-03-12 06:20:01 +01:00
if ( $start instanceof Carbon ) {
2020-09-24 06:06:18 +02:00
$start = $data [ 'start' ] -> startOfDay ();
}
$end = $data [ 'end' ];
2021-03-12 06:20:01 +01:00
if ( $end instanceof Carbon ) {
2020-09-24 06:06:18 +02:00
$end = $data [ 'end' ] -> endOfDay ();
}
2021-03-12 06:20:01 +01:00
2019-08-31 09:35:35 +02:00
return AvailableBudget :: create (
[
'user_id' => $this -> user -> id ,
2023-08-11 19:37:28 +02:00
'user_group_id' => $this -> user -> user_group_id ,
2021-03-13 16:47:29 +01:00
'transaction_currency_id' => $data [ 'currency_id' ],
2019-08-31 09:35:35 +02:00
'amount' => $data [ 'amount' ],
2020-09-24 06:06:18 +02:00
'start_date' => $start ,
'end_date' => $end ,
2019-08-31 09:35:35 +02:00
]
);
}
/**
2023-06-21 12:34:58 +02:00
* @ param AvailableBudget $availableBudget
* @ param array $data
2019-08-31 09:35:35 +02:00
*
* @ return AvailableBudget
*/
public function update ( AvailableBudget $availableBudget , array $data ) : AvailableBudget
{
2021-04-07 07:28:43 +02:00
if ( array_key_exists ( 'amount' , $data )) {
2019-08-31 09:35:35 +02:00
$availableBudget -> amount = $data [ 'amount' ];
}
$availableBudget -> save ();
return $availableBudget ;
}
2019-08-30 09:19:29 +02:00
/**
2023-06-21 12:34:58 +02:00
* @ param AvailableBudget $availableBudget
* @ param array $data
2019-08-30 09:19:29 +02:00
*
* @ return AvailableBudget
*/
public function updateAvailableBudget ( AvailableBudget $availableBudget , array $data ) : AvailableBudget
{
2021-03-13 16:47:29 +01:00
if ( array_key_exists ( 'start' , $data )) {
$start = $data [ 'start' ];
if ( $start instanceof Carbon ) {
$start = $data [ 'start' ] -> startOfDay ();
$availableBudget -> start_date = $start ;
$availableBudget -> save ();
}
2019-08-30 09:19:29 +02:00
}
2020-09-24 06:06:18 +02:00
2021-03-13 16:47:29 +01:00
if ( array_key_exists ( 'end' , $data )) {
$end = $data [ 'end' ];
if ( $end instanceof Carbon ) {
$end = $data [ 'end' ] -> endOfDay ();
$availableBudget -> end_date = $end ;
$availableBudget -> save ();
}
2020-09-24 06:06:18 +02:00
}
2021-03-13 16:47:29 +01:00
if ( array_key_exists ( 'currency_id' , $data )) {
$availableBudget -> transaction_currency_id = $data [ 'currency_id' ];
$availableBudget -> save ();
}
if ( array_key_exists ( 'amount' , $data )) {
$availableBudget -> amount = $data [ 'amount' ];
$availableBudget -> save ();
2020-09-24 06:06:18 +02:00
}
2019-08-30 09:19:29 +02:00
return $availableBudget ;
}
2020-05-30 07:33:06 +02:00
}