Compare commits
1 commit
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59617df1e8 |
2 changed files with 93 additions and 47 deletions
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace App\Jobs;
|
namespace App\Jobs;
|
||||||
|
|
||||||
|
use App\Models\ArchiveCampaign;
|
||||||
|
use App\Models\ArchiveCoupon;
|
||||||
use App\Models\ArchiveStat;
|
use App\Models\ArchiveStat;
|
||||||
use App\Models\Statistique;
|
use App\Models\Statistique;
|
||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
|
|
@ -19,39 +21,41 @@ class ArchiveStatsJob implements ShouldQueue
|
||||||
use Queueable;
|
use Queueable;
|
||||||
use SerializesModels;
|
use SerializesModels;
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new job instance.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
// Construct method.
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the job.
|
* Execute the job.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle(): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
DB::transaction(function () {
|
DB::transaction(function () {
|
||||||
Statistique::query()
|
Statistique::query()
|
||||||
->where('date', '<', now())
|
->where('date', '<', now())
|
||||||
->each(function ($stat) {
|
->each(function (Statistique $stat) {
|
||||||
$archiveStat = new ArchiveStat();
|
$archiveCampaign = ArchiveCampaign::query()->where('campaign_id', $stat->stat_campId)->first();
|
||||||
$archiveStat->quantity = $stat->stat_couponPrinted;
|
$archiveCoupon = ArchiveCoupon::query()->where('coupon_code', $stat->stat_couponId)->first();
|
||||||
$archiveStat->date = $stat->date;
|
if (!is_null($archiveCampaign) && !is_null($archiveCoupon)) {
|
||||||
$archiveStat->archive_coupon_id = $stat->stat_couponId;
|
$options = [
|
||||||
$archiveStat->archive_campaign_id = $stat->stat_campId;
|
['date', $stat->date],
|
||||||
$archiveStat->saveOrFail();
|
['archive_coupon_id', $archiveCoupon->getKey()],
|
||||||
|
['archive_campaign_id', $archiveCampaign->getKey()],
|
||||||
|
];
|
||||||
|
$archiveStat = ArchiveStat::query()->where($options)->first();
|
||||||
|
if (!is_null($archiveStat)) {
|
||||||
|
$newQuantity = $archiveStat->quantity + $stat->stat_couponPrinted;
|
||||||
|
$archiveStat->quantity = $newQuantity;
|
||||||
|
$archiveStat->saveOrFail();
|
||||||
|
} else {
|
||||||
|
$this->createArchiveStat($stat, $archiveCampaign->getKey(), $archiveCoupon->getKey());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$this->createArchiveStat($stat);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
Statistique::query()
|
||||||
Statistique::query()
|
->where('date', '<', now())
|
||||||
->where('date', '<', now())
|
->delete();
|
||||||
->delete();
|
|
||||||
});
|
});
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Log::error(
|
Log::error(
|
||||||
|
|
@ -64,4 +68,25 @@ class ArchiveStatsJob implements ShouldQueue
|
||||||
\report($e);
|
\report($e);
|
||||||
} //end try
|
} //end try
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new archive statistique.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Statistique $stat
|
||||||
|
* @param integer|null $archiveCampaignKey
|
||||||
|
* @param integer|null $archiveCouponKey
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function createArchiveStat(
|
||||||
|
Statistique $stat,
|
||||||
|
?int $archiveCampaignKey = null,
|
||||||
|
?int $archiveCouponKey = null
|
||||||
|
): void {
|
||||||
|
$newArchiveStat = new ArchiveStat();
|
||||||
|
$newArchiveStat->quantity = $stat->stat_couponPrinted;
|
||||||
|
$newArchiveStat->date = $stat->date;
|
||||||
|
$newArchiveStat->archive_coupon_id = $archiveCouponKey ?? $stat->stat_couponId;
|
||||||
|
$newArchiveStat->archive_campaign_id = $archiveCampaignKey ?? $stat->stat_campId;
|
||||||
|
$newArchiveStat->saveOrFail();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,8 @@ class ArchiveStat extends Model
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'date',
|
'date',
|
||||||
'quantity',
|
'quantity',
|
||||||
|
'archive_coupon_id',
|
||||||
|
'archive_campaign_id',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -38,37 +40,56 @@ class ArchiveStat extends Model
|
||||||
protected static function booted(): void
|
protected static function booted(): void
|
||||||
{
|
{
|
||||||
static::creating(function (self $model) {
|
static::creating(function (self $model) {
|
||||||
$campaign = Campagne::withTrashed()->where('camp_id', $model->archive_campaign_id)->firstOrFail();
|
$model->archive_campaign_id = static::resolveArchiveCampaignId($model->archive_campaign_id);
|
||||||
$archiveCampaign = ArchiveCampaign::query()->firstOrCreate(
|
$model->archive_coupon_id = static::resolveArchiveCouponId($model->archive_coupon_id);
|
||||||
[
|
|
||||||
'campaign_id' => $model->archive_campaign_id
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => $campaign->camp_name,
|
|
||||||
'archive_site_id' => $campaign->camp_siteId,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
$model->archiveCampaign()->associate($archiveCampaign);
|
|
||||||
|
|
||||||
$coupon = Coupon::withTrashed()->where('coupon_id', $model->archive_coupon_id)->firstOrFail();
|
|
||||||
$archiveCoupon = ArchiveCoupon::query()->firstOrCreate(
|
|
||||||
[
|
|
||||||
'coupon_code' => $model->archive_coupon_id
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => $coupon->coupon_code,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
$model->archiveCoupon()->associate($archiveCoupon);
|
|
||||||
});
|
|
||||||
static::updating(function () {
|
|
||||||
throw new \RuntimeException('Upating an archived stat is not authorized');
|
|
||||||
});
|
});
|
||||||
static::deleting(function () {
|
static::deleting(function () {
|
||||||
throw new \RuntimeException('Deleting an archived stat is not authorized');
|
throw new \RuntimeException('Deleting an archived stat is not authorized');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// * METHODS
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get archive campaign key.
|
||||||
|
*
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
protected static function resolveArchiveCampaignId(int $id): int
|
||||||
|
{
|
||||||
|
$archiveCampaign = ArchiveCampaign::query()->find($id);
|
||||||
|
if ($archiveCampaign) {
|
||||||
|
return $archiveCampaign->getKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
$campaign = Campagne::query()->where('camp_id', $id)->firstOrFail();
|
||||||
|
|
||||||
|
return ArchiveCampaign::query()->firstOrCreate(
|
||||||
|
['campaign_id' => $id],
|
||||||
|
['name' => $campaign->camp_name, 'archive_site_id' => $campaign->camp_siteId]
|
||||||
|
)->getKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get archive coupon key.
|
||||||
|
*
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
protected static function resolveArchiveCouponId(int $id): int
|
||||||
|
{
|
||||||
|
$archiveCoupon = ArchiveCoupon::query()->find($id);
|
||||||
|
if ($archiveCoupon) {
|
||||||
|
return $archiveCoupon->getKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
$coupon = Coupon::withTrashed()->where('coupon_id', $id)->firstOrFail();
|
||||||
|
|
||||||
|
return ArchiveCoupon::query()->firstOrCreate(
|
||||||
|
['coupon_code' => $id],
|
||||||
|
['name' => $coupon->coupon_code]
|
||||||
|
)->getKey();
|
||||||
|
}
|
||||||
|
|
||||||
// * RELATIONS
|
// * RELATIONS
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue