migrate to gtea from bistbucket
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Modules\RestaurantDelivery\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Modules\RestaurantDelivery\Models\Delivery;
|
||||
use Modules\RestaurantDelivery\Models\RiderEarning;
|
||||
use Modules\RestaurantDelivery\Services\Earnings\EarningsCalculator;
|
||||
|
||||
class CalculateEarningsJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* The number of times the job may be attempted.
|
||||
*/
|
||||
public int $tries = 3;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly Delivery $delivery
|
||||
) {
|
||||
$this->onQueue(config('restaurant-delivery.queue.queues.earnings', 'restaurant-delivery-earnings'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(EarningsCalculator $calculator): void
|
||||
{
|
||||
// Skip if no rider assigned
|
||||
if (! $this->delivery->rider_id) {
|
||||
Log::warning('Cannot calculate earnings: no rider assigned', [
|
||||
'delivery_id' => $this->delivery->id,
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Skip if earnings already exist
|
||||
if ($this->delivery->riderEarning()->exists()) {
|
||||
Log::info('Earnings already calculated for delivery', [
|
||||
'delivery_id' => $this->delivery->id,
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($calculator) {
|
||||
// Calculate earnings
|
||||
$earningsData = $calculator->calculateForDelivery($this->delivery);
|
||||
|
||||
// Create rider earning record
|
||||
RiderEarning::create([
|
||||
'rider_id' => $this->delivery->rider_id,
|
||||
'delivery_id' => $this->delivery->id,
|
||||
'restaurant_id' => $this->delivery->restaurant_id,
|
||||
'type' => 'delivery',
|
||||
'description' => "Delivery #{$this->delivery->tracking_code}",
|
||||
'base_amount' => $earningsData['base_amount'],
|
||||
'distance_amount' => $earningsData['distance_amount'],
|
||||
'bonus_amount' => $earningsData['total_bonus'],
|
||||
'penalty_amount' => $earningsData['total_penalty'],
|
||||
'tip_amount' => $this->delivery->tip_amount ?? 0,
|
||||
'gross_amount' => $earningsData['gross_amount'],
|
||||
'commission_rate' => $earningsData['commission_rate'],
|
||||
'commission_amount' => $earningsData['commission_amount'],
|
||||
'net_amount' => $earningsData['net_amount'],
|
||||
'currency' => config('restaurant-delivery.pricing.currency', 'BDT'),
|
||||
'breakdown' => $earningsData['breakdown'],
|
||||
'bonuses' => $earningsData['bonuses'],
|
||||
'penalties' => $earningsData['penalties'],
|
||||
'status' => 'pending',
|
||||
'earned_at' => now(),
|
||||
]);
|
||||
|
||||
Log::info('Earnings calculated for delivery', [
|
||||
'delivery_id' => $this->delivery->id,
|
||||
'rider_id' => $this->delivery->rider_id,
|
||||
'net_amount' => $earningsData['net_amount'],
|
||||
]);
|
||||
});
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to calculate earnings', [
|
||||
'delivery_id' => $this->delivery->id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a job failure.
|
||||
*/
|
||||
public function failed(\Throwable $exception): void
|
||||
{
|
||||
Log::error('Earnings calculation job failed', [
|
||||
'delivery_id' => $this->delivery->id,
|
||||
'error' => $exception->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user