Skip to content

Commit 58584b3

Browse files
committed
Add getShopEarnings method to ShopController and update API route
This commit introduces the getShopEarnings method in the ShopController, which calculates the total earnings for a specified shop by aggregating revenue from both simple and varied product orders. Additionally, it updates the API routes to include a new endpoint for retrieving shop earnings, enhancing the functionality available to sellers.
1 parent 060d84a commit 58584b3

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

app/Jobs/GetShopEarningsJob.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\Shop;
6+
use App\Models\Product;
7+
use Illuminate\Bus\Queueable;
8+
use Illuminate\Queue\SerializesModels;
9+
use Illuminate\Queue\InteractsWithQueue;
10+
use Illuminate\Contracts\Queue\ShouldQueue;
11+
use Illuminate\Foundation\Bus\Dispatchable;
12+
use App\Services\Shop\GetTotalEarningService;
13+
14+
class GetShopEarningsJob implements ShouldQueue
15+
{
16+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
17+
18+
/**
19+
* Create a new job instance.
20+
*/
21+
22+
public $productId;
23+
public function __construct($productId)
24+
{
25+
$this->productId=$productId;
26+
}
27+
28+
/**
29+
* Execute the job.
30+
*/
31+
public function handle(): void
32+
{
33+
$product=Product::findOrFail($this->productId);
34+
$shopId=$product->shop_id;
35+
$totalEarnings=(new GetTotalEarningService())->getTotalEarning($shopId);
36+
$shop=Shop::findOrFail($shopId);
37+
$shop->total_earning=$totalEarnings;
38+
$shop->save();
39+
}
40+
}

app/Services/Payment/ValidatePaymentProductService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Models\OrderDetail;
1010
use Illuminate\Http\Request;
1111
use App\Models\OrderVariation;
12+
use App\Jobs\GetShopEarningsJob;
1213
use App\Models\ProductVariation;
1314
use App\Models\VariationAttribute;
1415
use Illuminate\Support\Facades\Log;
@@ -94,7 +95,7 @@ private function createOrder($userId,$amount,$shipping,$productId,$quantity,$qua
9495
if($order->save()){
9596

9697
SendNewOrderNotificationJob::dispatch($order->id,$productId)->delay(now()->addMinutes(1));
97-
98+
GetShopEarningsJob::dispatch($productId)->delay(now()->addMinutes(1));
9899
$this->savePaymentAndOrder($reference,$order->id);
99100
if($hasVariation=="false"){
100101

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Services\Shop;
4+
5+
use App\Models\Shop;
6+
use App\Models\Order;
7+
use App\Models\ProductVariation;
8+
use Illuminate\Support\Facades\DB;
9+
10+
class GetTotalEarningService{
11+
12+
public function getTotalEarning($shopId){
13+
$shop = Shop::findOrFail($shopId);
14+
$totalEarnings = 0.0;
15+
16+
// Étape 1: Récupérer les IDs des produits de la boutique
17+
$productIds = $shop->products()->pluck('id');
18+
19+
// Étape 2: Récupérer les IDs de commandes de produits simples
20+
$simpleOrderIds = DB::table('order_details')
21+
->whereIn('product_id', $productIds)
22+
->pluck('order_id');
23+
24+
// Étape 3: Récupérer les IDs de commandes de produits variés
25+
$productVariationIds = ProductVariation::whereIn('product_id', $productIds)->pluck('id');
26+
27+
$variedOrderIds = DB::table('order_variations')
28+
->whereIn('product_variation_id', $productVariationIds)
29+
->pluck('order_id');
30+
31+
// Étape 4: Combiner les IDs de commandes et enlever les doublons
32+
$allOrderIds = $simpleOrderIds->merge($variedOrderIds)->unique();
33+
34+
// Étape 5: Récupérer les commandes (payées et livrées) et calculer les gains
35+
$orders = Order::whereIn('id', $allOrderIds)
36+
->get();
37+
38+
foreach ($orders as $order) {
39+
$subtotal = floatval($order->total) - floatval($order->fee_of_shipping);
40+
41+
// On retire la taxe de 5%
42+
$taxAmount = $subtotal * 0.05;
43+
$netEarnings = $subtotal - $taxAmount;
44+
45+
$totalEarnings += $netEarnings;
46+
}
47+
48+
return $totalEarnings;
49+
}
50+
}

0 commit comments

Comments
 (0)