108 lines
4.1 KiB
PHP
108 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use App\Helpers\onesignalhelper;
|
|
use App\Models\IamPrincipal;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Carbon\Carbon;
|
|
use App\Models\ManageRestaurant;
|
|
use App\Models\ManageState;
|
|
use App\Models\RedeemRestaurant;
|
|
use App\Models\RestaurantTimeInterval;
|
|
use App\Models\TimeInterval;
|
|
|
|
class ReinstateRestaurant extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'app:reinstate-restaurant';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'The Command will reinstate the voucher after 4 hour';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
|
|
public function handle()
|
|
{
|
|
try {
|
|
$recordsToUpdate = RedeemRestaurant::where('is_redeem', 1)->get();
|
|
|
|
foreach ($recordsToUpdate as $record) {
|
|
$managerestaurant = ManageRestaurant::where('id', $record->manage_restaurants_xid)->first();
|
|
$restTime = RestaurantTimeInterval::select('time_hours')->where('manage_restaurants_xid', $managerestaurant->id)->first();
|
|
$stateTime = TimeInterval::select('time_hours')->where('manage_state_xid', $managerestaurant->state_xid)->first();
|
|
|
|
$restTimeHours = $restTime ? $restTime->time_hours : 0;
|
|
$stateTimeHours = $stateTime ? $stateTime->time_hours : 0;
|
|
|
|
Log::info('Rest time interval time_hours: ' . $restTimeHours);
|
|
Log::info('State time interval time_hours: ' . $stateTimeHours);
|
|
|
|
$customerData = IamPrincipal::where('id', $record->iam_principal_xid)
|
|
->where('notification_status', 1)
|
|
->where('principal_type_xid', 3)
|
|
->first();
|
|
|
|
if ($managerestaurant && $managerestaurant->is_active == 1) {
|
|
$redeemDate = Carbon::parse($record->redeem_date);
|
|
|
|
// Calculate the state time plus record time
|
|
$stateHourPlusTimeOfRecord = $redeemDate->copy()->addHours($stateTimeHours);
|
|
Log::info('state time' . $stateHourPlusTimeOfRecord);
|
|
|
|
$currentTime = Carbon::now();
|
|
|
|
if ($currentTime > $stateHourPlusTimeOfRecord) {
|
|
// Only proceed if state time condition is met
|
|
$restHourPlusTimeOfRecord = $redeemDate->copy()->addHours($restTimeHours);
|
|
Log::info('Restaurant time ' . $restHourPlusTimeOfRecord);
|
|
|
|
|
|
if ($currentTime > $restHourPlusTimeOfRecord) {
|
|
$record->update([
|
|
'is_redeem' => 0,
|
|
'count' => $record->count + 1,
|
|
'is_redeemption_undone' => 0,
|
|
'redeemption_undone_date' => null,
|
|
]);
|
|
|
|
$restImage = ListingImageUrl('restaurant_images', $managerestaurant->image);
|
|
$title = "Your " . $managerestaurant->name . " Reinstate successfully";
|
|
$message = "Your " . $managerestaurant->name . " Reinstate successfully";
|
|
$content_type = 'Restaurant Reinstate';
|
|
$imageUrl = $restImage;
|
|
|
|
onesignalhelper::sendNotificationApi(
|
|
$customerData->one_signal_player_id,
|
|
$title,
|
|
$message,
|
|
$content_type,
|
|
$imageUrl,
|
|
$id = null
|
|
);
|
|
Log::info('Reinstate of record done at ' . now());
|
|
|
|
onesignalhelper::StoreNotificationDetails($customerData->id, $content_type, $title, $restImage);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error('Reinstate function failed: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|