2024-05-15 13:04:58 -07:00
< ? php
namespace App\Console\Commands ;
2024-12-05 10:08:39 -08:00
use App\Mail\UnacceptedAssetReminderMail ;
2024-05-15 13:04:58 -07:00
use App\Models\Asset ;
use App\Models\CheckoutAcceptance ;
use App\Models\Setting ;
use App\Models\User ;
use App\Notifications\CheckoutAssetNotification ;
use App\Notifications\CurrentInventory ;
use Illuminate\Console\Command ;
2024-12-05 11:37:03 -08:00
use Illuminate\Support\Facades\Mail ;
2024-05-15 13:04:58 -07:00
class SendAcceptanceReminder extends Command
{
/**
* The name and signature of the console command .
*
* @ var string
*/
protected $signature = 'snipeit:acceptance-reminder' ;
/**
* The console command description .
*
* @ var string
*/
2024-05-15 13:20:05 -07:00
protected $description = 'This will resend users with unaccepted assets a reminder to accept or decline them.' ;
2024-05-15 13:04:58 -07:00
/**
* Create a new command instance .
*
* @ return void
*/
public function __construct ()
{
parent :: __construct ();
}
/**
* Execute the console command .
*
* @ return mixed
*/
public function handle ()
{
2024-05-21 09:42:00 -07:00
$pending = CheckoutAcceptance :: pending () -> where ( 'checkoutable_type' , 'App\Models\Asset' )
-> whereHas ( 'checkoutable' , function ( $query ) {
2024-08-22 10:04:46 -07:00
$query -> where ( 'accepted_at' , null )
-> where ( 'declined_at' , null );
2024-05-21 09:42:00 -07:00
})
2024-09-17 14:16:41 -07:00
-> with ([ 'assignedTo' , 'checkoutable.assignedTo' , 'checkoutable.model' , 'checkoutable.adminuser' ])
2024-05-21 09:42:00 -07:00
-> get ();
2024-05-15 13:04:58 -07:00
$count = 0 ;
2024-05-23 12:47:02 -07:00
$unacceptedAssetGroups = $pending
2024-05-15 13:04:58 -07:00
-> filter ( function ( $acceptance ) {
return $acceptance -> checkoutable_type == 'App\Models\Asset' ;
})
-> map ( function ( $acceptance ) {
return [ 'assetItem' => $acceptance -> checkoutable , 'acceptance' => $acceptance ];
2024-05-21 09:42:00 -07:00
})
-> groupBy ( function ( $item ) {
return $item [ 'acceptance' ] -> assignedTo ? $item [ 'acceptance' ] -> assignedTo -> id : '' ;
2024-05-15 13:04:58 -07:00
});
2024-05-23 12:47:02 -07:00
2024-05-21 09:42:00 -07:00
$no_mail_address = [];
2024-05-15 13:04:58 -07:00
2024-05-23 12:47:02 -07:00
foreach ( $unacceptedAssetGroups as $unacceptedAssetGroup ) {
2024-12-05 11:46:56 -08:00
//the [0] is weird, but it allows for the item_count to work and grabs the appropriate info for each user. collapsing and flattening the query doesn't work above.
$acceptance = $unacceptedAssetGroup [ 0 ][ 'acceptance' ];
$locale = $acceptance -> assignedTo ? -> locale ;
$email = $acceptance -> assignedTo ? -> email ;
2024-05-23 12:47:02 -07:00
$item_count = $unacceptedAssetGroup -> count ();
2024-12-05 11:37:03 -08:00
if ( $locale && $email ) {
2024-12-05 11:46:56 -08:00
Mail :: to ( $email ) -> send (( new UnacceptedAssetReminderMail ( $acceptance , $item_count )) -> locale ( $locale ));
2024-12-05 11:37:03 -08:00
} elseif ( $email ) {
2024-12-05 11:46:56 -08:00
Mail :: to ( $email ) -> send (( new UnacceptedAssetReminderMail ( $acceptance , $item_count )));
2024-05-15 13:04:58 -07:00
}
2024-12-05 11:37:03 -08:00
$count ++ ;
2024-05-15 13:04:58 -07:00
}
2024-05-21 09:42:00 -07:00
if ( ! empty ( $no_mail_address )) {
foreach ( $no_mail_address as $user ) {
return $user . ' has no email.' ;
}
2024-05-15 13:04:58 -07:00
}
$this -> info ( $count . ' users notified.' );
}
}