Skip to content

Commit 060d84a

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 earnings 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 90a1f91 commit 060d84a

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

app/Http/Controllers/Seller/ShopController.php

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace App\Http\Controllers\Seller;
44

5-
use App\Http\Controllers\Controller;
6-
use App\Http\Requests\ShopRequest;
75
use App\Models\Shop;
8-
use App\Service\Shop\generateShopNameService;
9-
use App\Services\GenerateUrlResource;
6+
use App\Models\Order;
107
use Illuminate\Http\Request;
8+
use App\Models\ProductVariation;
9+
use App\Http\Requests\ShopRequest;
10+
use Illuminate\Support\Facades\DB;
11+
use App\Http\Controllers\Controller;
1112
use Illuminate\Support\Facades\Auth;
13+
use App\Services\GenerateUrlResource;
14+
use App\Service\Shop\generateShopNameService;
1215

1316
class ShopController extends Controller
1417
{
@@ -76,4 +79,44 @@ public function destroy(string $id)
7679
{
7780
//
7881
}
82+
83+
public function getShopEarnings($shopId){
84+
$shop = Shop::findOrFail($shopId);
85+
$totalEarnings = 0.0;
86+
87+
// Étape 1: Récupérer les IDs des produits de la boutique
88+
$productIds = $shop->products()->pluck('id');
89+
90+
// Étape 2: Récupérer les IDs de commandes de produits simples
91+
$simpleOrderIds = DB::table('order_details')
92+
->whereIn('product_id', $productIds)
93+
->pluck('order_id');
94+
95+
// Étape 3: Récupérer les IDs de commandes de produits variés
96+
$productVariationIds = ProductVariation::whereIn('product_id', $productIds)->pluck('id');
97+
98+
$variedOrderIds = DB::table('order_variations')
99+
->whereIn('product_variation_id', $productVariationIds)
100+
->pluck('order_id');
101+
102+
// Étape 4: Combiner les IDs de commandes et enlever les doublons
103+
$allOrderIds = $simpleOrderIds->merge($variedOrderIds)->unique();
104+
105+
// Étape 5: Récupérer les commandes (payées et livrées) et calculer les gains
106+
$orders = Order::whereIn('id', $allOrderIds)
107+
->get();
108+
109+
foreach ($orders as $order) {
110+
$subtotal = floatval($order->total) - floatval($order->fee_of_shipping);
111+
112+
// On retire la taxe de 5%
113+
$taxAmount = $subtotal * 0.05;
114+
$netEarnings = $subtotal - $taxAmount;
115+
116+
$totalEarnings += $netEarnings;
117+
}
118+
119+
return $totalEarnings;
120+
121+
}
79122
}

routes/api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
Route::get("all/products", [ProductListController::class, "allProducts"]);
165165
Route::get("/attributes/value/{id}", [GetAttributesController::class, 'getValue']);
166166
Route::get("/all/genders", [CurrentGenderController::class, "all"]);
167-
167+
Route::get("/get/earnings/{shopId}",[ShopController::class,"getShopEarnings"]);
168168
Route::get('/attributes/value/by/group/{id}',[ListCategoryController::class,"getAttributeValueByAttributeId"]);
169169
Route::get('/categories/attributes',[ListCategoryController::class,"getCategoriesWithAttributes"]);
170170

0 commit comments

Comments
 (0)