Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ok200manami committed Sep 18, 2024
1 parent ac1696b commit 3d2ed03
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 0 deletions.
102 changes: 102 additions & 0 deletions app/Console/Commands/VoucherSets/GenerateVoucherSetVouchersCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace App\Console\Commands\VoucherSets;

use App\Models\User;
use App\Models\Voucher;
use App\Models\VoucherSet;
use App\Services\VoucherSetService;
use Illuminate\Console\Command;

class GenerateVoucherSetVouchersCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:voucher-set:vouchers:generate';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generates vouchers for a newly created voucher set';

/**
* Execute the console command.
*/
public function handle(): int
{
$voucherSet = VoucherSet::where('is_denomination_valid', 1)->whereNull('voucher_generation_started_at')->whereNull('voucher_generation_finished_at')->first();

if ($voucherSet) {
$voucherSet->voucher_generation_started_at = now();
$voucherSet->save();

/**
* Re-validate the denomination JSON
*/
$voucherSetIsValid = VoucherSetService::validateVoucherSetDenominations($voucherSet);

if (!$voucherSetIsValid) {
$notifyUser = User::where('email', env('MAIL_DEFAULT_EMAIL'))->first();

if ($notifyUser) {
// notify that 'The voucher set denomination was invalid'
// $notifyUser->notify(new VoucherSetCanNotBeGenerated($voucherSet, 'The voucher set denomination was invalid.'));
}

return 0;
}

/**
* voucher set denomination is valid
*/
$denominationArray = json_decode($voucherSet->denomination_json, true);
$numCreated = 0;
foreach ($denominationArray as $denominationListing) {
for ($i = 1; $i <= $denominationListing['number']; $i++) {
$model = new Voucher();
$model->voucher_set_id = $voucherSet->id;
$model->created_by_team_id = $voucherSet->created_by_team_id;
$model->allocated_to_service_team_id = $voucherSet->allocated_to_service_team_id;
$model->voucher_value_original = $denominationListing['value'];
$model->voucher_value_remaining = $denominationListing['value'];
$model->is_test = $voucherSet->is_test;
$model->save();

$numCreated++;

if (($numCreated % 100) == 0) {
$this->line($numCreated . ' vouchers generated.');
}
}
}

$voucherSet->voucher_generation_finished_at = now();
$voucherSet->save();

$notificationUser = User::find($voucherSet->created_by_user_id);

if ($notificationUser) {
// notify that 'The voucher set was successfully generated'
// $notificationUser->notify(new VoucherSetWasGeneratedOkay($voucherSet));
}

/**
* Set the voucher aggregates
*/
// dispatch(new SetVoucherSetAggregates($voucherSet));

/**
* Set the team / agency budget aggregate data
*/
// dispatch(new QueueTeamAgencyBudgetAggregates($voucherSet));

}

return 0;
}
}
62 changes: 62 additions & 0 deletions app/Jobs/Vouchers/GenerateStorageVoucherQrCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Jobs\Vouchers;

use App\Models\Voucher;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\URL;

class GenerateStorageVoucherQrCode implements ShouldQueue
{
use Queueable;

/**
* Create a new job instance.
*/
public function __construct(public Voucher $voucher)
{
//
}

/**
* Execute the job.
*/
public function handle(): void
{
$redeemUrl = URL::to('/redeem/' . $this->voucher->voucher_set_id . '/' . $this->voucher->id);

/**
* PNG
*/







// PNG
$dataPng = (new Generator())->format('png')->size(600)->generate($redeemUrl);
$path = '/voucher-sets/'.$this->voucher->voucher_set_id.'/vouchers/individual/'.$this->voucher->id.'/png/voucher-qr.png';
$file = Storage::put($path, $dataPng);

$path = '/voucher-sets/'.$this->voucher->voucher_set_id.'/vouchers/all/png/'.$this->voucher->id.'.png';
$file = Storage::put($path, $dataPng);

// SVG
$dataSvg = (new Generator())->format('svg')->size(600)->generate($redeemUrl);
$path = '/voucher-sets/'.$this->voucher->voucher_set_id.'/vouchers/individual/'.$this->voucher->id.'/svg/voucher-qr.svg';
$file = Storage::put($path, $dataSvg);

$path = '/voucher-sets/'.$this->voucher->voucher_set_id.'/vouchers/all/svg/'.$this->voucher->id.'.svg';
$file = Storage::put($path, $dataSvg);

if (isset($this->voucher->team->voucherTemplate->voucher_template_path)) {
dispatch(new GenerateStorageBrandedQrTemplate($this->voucher));
}
}
}
5 changes: 5 additions & 0 deletions app/Listeners/Vouchers/HandleVoucherWasCreatedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Events\Vouchers\VoucherWasCreated;
use App\Jobs\Vouchers\AssignUniqueShortCodeToVoucherJob;
use App\Jobs\Vouchers\CollateVoucherAggregatesJob;
use App\Jobs\Vouchers\GenerateStorageVoucherQrCode;
use App\Jobs\VoucherSets\CollateVoucherSetAggregatesJob;
use App\Models\VoucherSet;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand Down Expand Up @@ -33,5 +34,9 @@ public function handle(VoucherWasCreated $event): void
dispatch(new CollateVoucherSetAggregatesJob($voucherSet));

}

if (env('APP_ENV') != 'testing') {
dispatch(new GenerateStorageVoucherQrCode($event->voucher));
}
}
}
19 changes: 19 additions & 0 deletions app/Services/VoucherSetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,23 @@ public static function collateVoucherSetAggregates(VoucherSet $voucherSet): void

$voucherSet->saveQuietly();
}

/**
* Determine if a voucher set's JSON is valid, and matched its value
*
* @param VoucherSet $voucherSet
* @return mixed
*/
public static function validateVoucherSetDenominations(VoucherSet $voucherSet): bool
{
$denominationArray = json_decode($voucherSet->denomination_json, true);

$totalValue = 0;

foreach ($denominationArray as $denominationListing) {
$totalValue += ($denominationListing['value'] * $denominationListing['number']);
}

return $totalValue == $voucherSet->total_set_value;
}
}

0 comments on commit 3d2ed03

Please sign in to comment.