Laravel 按利率计算总余额

Laravel calculate Total balance by rates

我的电子商务网站有 4 tables

(Users, Vendors, Products and Orders). 

这里的供应商是指商店。

用户是一个人,可以拥有许多不同的供应商。

这些供应商可以有许多不同的产品(Vendor hasMany Product 关系和 Product belongsTo Vendor 关系)。

订单table保存了客户购买时的所有交易信息。

现在我们通过电子商务网站上发生的每笔销售的佣金来赚钱。

每个产品的佣金是根据其所在的供应商(商店)的百分比计算的。这些费率存储在 Vendors table.

在我的产品中table我有

id
product_name
product_price
product_image
product_file
product_description
product_level
vendor_id

在我的供应商中 table 我有:

id
user_name
vendor_email
vendor_name
rate

在我的订单中 table 我有:

id
customer_name
customer_tel
product_name
product_price
product_id
vendor_email
transaction_id
payment_status

需要注意的关键事项:供应商电子邮件是唯一的。 vendor_id in the products table is the id field in the vendors table etc.

现在,假设用户 John 有 3 个供应商(A、B 和 C),这些供应商下各有 5 个产品。

对于供应商 A,我们有与供应商 B(B1、B2 ..)和供应商 C(C1、C2 等)相同的产品 A1、A2、A3 等。

假设约翰对商店 A 的佣金率为 5%,对商店 B 的佣金率为 10%,对商店 C 的佣金率为 0%。

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]

这意味着如果产品 A1(在商店 A 中找到)售价 10 美元并且被购买,我们将获得销售额的 5%:

5/100 * 10 美元 = 0.5 美元

John 获得 95%,即

- [=24=].5 = 9.5$.

让我们以另一种产品 B2(在商店 B 中以 10% 的价格找到)为例,价格为 100 美元。这意味着我们获得每笔销售的 %10,John 获得 90%。

即:10/100 * 0 = 对我们而言,John 得到 0 - =

让我们以最终产品 c1(在商店 C 中以 0% 的价格找到)为例,价格为 200 美元。这意味着当购买该产品时,John 得到了所有的钱。

现在对于这三种产品,如果约翰要计算他的总收入,这将是:

.5 +  + 0 = 9.5

我们的总佣金将是:

[=17=].5 +  + [=17=] = .5

我希望能够向用户显示此数据。太胖了,我可以在我的控制器中使用 eloquent 获取 collection 并将数据显示给用户。

在我的控制器中我有:

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'));                
    }

在我看来,为了获得 John 为每种产品赚取的金额,我在我的 blade:

中使用了 Foreach 循环
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

这非常有效,因为它 return 为每个已购买的产品提供了正确的数字。如果我们以上面的例子为例,它将 return

 Total: 3 
    Product |  Price |Rate |Income |Vendor
      A1    |     |5%   |.5   |A
      B2    |   0 |10%  |    |B
      C1    |   0 |0%   |0   |C

我最大的问题是如何从控制器动态获取总收入并将其显示为 Blade 视图中的变量。我希望能够在控制器中获得这个值,以便我可以对其进行计算,假设用户想要提取他的部分收入,我应该能够从总收入中扣除提取金额并显示剩余金额等。 我一直被这个问题困扰,我已经进行了大量的谷歌搜索,但似乎找不到任何解决方案。我得到了每个用户的产品总数:

$totalOrders = Order::where('vendor_email', Auth()->User()->email)->where('payment_status', 'SUCCESSFUL')->count();

由此我可以通过简单地在 blade:

中调用这个变量来显示每个用户的总订单
Total: {{$totalOrders}}

我现在的问题是如何计算用户的总收入:即用每件售出产品的价格乘以供应商商店的佣金率,这样我就可以得到一些变量,例如:

Total Income: {{some Variable}}

我真的需要帮助。

订购型号

public function product(){
   return $this->belongsTo(App\Product::class, 'product_id', 'id');
}

产品型号

public function orders(){
   return $this->hasMany(App\Order::class, 'order_id', 'id');
}

public function vendor(){
   return $this->belongsTo(App\Vendor::class, 'vendor_id', 'id');
}

控制器

// 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}}