I have an ecommerce site with 4 tables
(Users, Vendors, Products and Orders).
Vendor here means a shop.
A User is a person and can own many different Vendors.
These Vendors can have many different products (Vendor hasMany Product relationship and a Product belongsTo Vendor relationship).
The orders table saves all transaction information when a customer buys.
Now we make our money via commission for each sale that happens on the eCommerce site.
The commission is calculated for each product based on the percentage rate of the Vendor(Shop) it is found under. These rates are stored in the Vendors table.
In my products table I have
id
product_name
product_price
product_image
product_file
product_description
product_level
vendor_id
In my Vendors table I have:
id
user_name
vendor_email
vendor_name
rate
And in my orders table I have:
id
customer_name
customer_tel
product_name
product_price
product_id
vendor_email
transaction_id
payment_status
Key things to note: Vendor Email is unique. vendor_id in the products table is the id field in the vendors table etc.
Now, let's take a user John who has 3 Vendors (A, B and C) and 5 products each under these vendors.
For Vendor A, we have products A1, A2, A3 etc same for Vendor B (B1, B2 ..) and Vendor C (C1, C2 etc).
Let's say John has a 5% commission rate to shop A, a 10% rate to shop B and 0% rate to shop C.
Vendor A: rate = 5%.
Products = [A1, A2, A3, A4, A5]
Vendor B: rate = 10%.
Products = [B1, B2, B3, B4, B5]
Vendor C: rate = 0%.
Products = [C1, C2, C3, C4, C5]
This means if product A1 (found in Shop A) cost $10 and it is bought we get 5% of the sale:
5/100 * $10 = $0.5
and John gets 95% which is
$10 - $0.5 = 9.5$
.
Let's take another product B2 (found in Shop B with a rate of 10%) and cost $100. this means we get %10 of each sale and John gets 90%.
that is: 10/100 * $100 = $10
for us and John gets $100 - $10 = $90
.
Let's take a final product c1 (found in shop C with a rate of 0%) and cost $200. This means when this product is bought, John gets all of the money.
Now for this three products, if John was to calculate his total income, this will be:
$9.5 + $90 + $200 = $299.5
and our total commision will be:
$0.5 + $10 + $0 = $10.5
I want to be able to display this data to the user. So fat, I am able to get the collection using eloquent in my controller and display the data to the user.
In my controller I have:
public function index()
{
$myOrders = ['vendor_email' => Auth()->User()->email, 'payment_status' => 'SUCCESSFUL'];
$orders = Order::where($myOrders)->orderBy('created_at', 'desc')->paginate(10);
$products = Product::with('vendor')->get();
$totalOrders = Order::where($myOrders)->count();
return view('admin.orders', compact('orders', 'products', 'totalOrders'));
}
And in my view, to get the amount John makes for each product, I use a Foreach loop in my blade:
Total: {{totalOrders}}
@foreach($orders as $order)
@if(Auth()->User()->email == $order->vendor_email)
Product Name: {{$order->product_name}}
Product Price: {{$order->product_price}}
@foreach($products as $product)
@if($order->product_id == $product->id)
Rate: {{$product->vendor->rate}}
Income: {{$order->product_price * $product->vendor->rate}}
Vendor: {{$product->vendor->vendor_name}}
@endif
@endforeach
@endif
@endforeach
This works perfectly as it returns the correct figures for each product which has been bought. If we take our example above, it will return
Total: 3
Product | Price |Rate |Income |Vendor
A1 | $10 |5% |$9.5 |A
B2 | $100 |10% |$90 |B
C1 | $200 |0% |$200 |C
My biggest problem is how to dynamically get the total of the income from the controller and displaying it as a variable in the Blade View. I want to be able to get this value in the controller so I can do calculations on it, say the User wants to withdraw part of his income, I should be able to deduct the withdrawal amount from the total income and display what's left etc. I've been stuck with this problem and I have done a lot of googling but can't seem to find any solution. I got the total count of the products of each user by:
$totalOrders = Order::where('vendor_email', Auth()->User()->email)->where('payment_status', 'SUCCESSFUL')->count();
From that I can display this total orders of each user by simply calling this variable in blade:
Total: {{$totalOrders}}
My question now is how do I calculate the total income of the user: that is taking the price of each product sold and multiplying it by the commision rate of the Vendor shop so I can have some variable such as:
Total Income: {{some Variable}}
I really need help with this.
Order Model
public function product(){
return $this->belongsTo(App\Product::class, 'product_id', 'id');
}
Product Model
public function orders(){
return $this->hasMany(App\Order::class, 'order_id', 'id');
}
public function vendor(){
return $this->belongsTo(App\Vendor::class, 'vendor_id', 'id');
}
Controller
// These are orders for the logged in user.
$orders = Order::with('product.vendor')->where([
'vendor_email' => Auth()->User()->email, 'payment_status' => 'SUCCESSFUL'])
->get();
// Their income should be (100 - rate)% since the rate is the commission you get // as you stated in your post.
$totalIncome = $orders->sum(function ($order) {
return ($order->product->product_price * (100 - $order->product->vendor->rate);
});
$totalOrders = sizeof($orders);
return view('admin.orders', compact('orders', 'totalIncome', 'totalOrders'));
Blade
Total Orders: {{totalOrders}}
@foreach($orders as $order)
Product Name: {{$order->product->name}}
Product Price: {{$order->product->product_price}}
Rate: {{$order->product->vendor->rate}}
Income: {{$order->product->product_price * (100 - $order->product->vendor->rate)}}
Vendor: {{$order->product->vendor->vendor_name}}
@endforeach
Total Income: {{totalIncome}}