Laravel逻辑 - 转换价格,所以在前端我总是chf值 [英] Laravel logical - convert price, so on frontend I have always chf value

查看:73
本文介绍了Laravel逻辑 - 转换价格,所以在前端我总是chf值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点重。我在我的cms四种货币,usd,gbp,欧元和主要一个chf。另外,在我的cms中,管理员可以添加学校,在学校里面你可以创建课程,然后管理员可以设置特定课程的价格在usd或gbp,欧元或chf。我的任务是,如果学校有多个课程,并且这些课程有不同的价值,我需要以最低的价格赶上课程,所以我可以写在我的前端:课程从XX - CHF开始。
在我的cms中,管理员可以设置自己的汇率,CHF总是设置为1,但他们可以为usd,gbp或euro添加自己的值。

This is a little bit heavy. I have in my cms four currencies, usd, gbp, euro and the main one chf. Also, in my cms, admin can add schools, inside of schools you can create courses, and then admin can set the price for the specific course in usd or in gbp, euro or chf. What my assignment is, if school has multiple courses and those courses have all different values, I need to catch the course with lowest price, so I can write on my frontend: "Courses start from XX - CHF". In my cms, admin can set it's own exchange rate, CHF is always set to 1, but they can add their own values for usd, gbp or euro.

我到目前为止做的是,取该课程的最小值,并将其转换为CHF。

What I have done so far, is take the minimum value for that course and converted it to CHF. And it is working ok.

问题是:如果一个课程设置为10欧元,另一个课程设置为10 usd,我将采用第一个值,因此它可以计算10欧元作为最低价格,10 usd是最低价格。所以我需要以某种方式计算查询中的货币,并没有线索如何做。

PROBLEM IS: if one course is set to 10 euro, and another one is set to 10 usd, I will take the first value, so it can calculate 10 euro as a minimal price aldo the 10 usd is the minimum price. So I need somehow, to calculate the currencies inside of query and have no clue how to do that.

这是我到目前为止做的:

Here is what I have done so far:

public function schoolMinPrice()
{
    $minPrice = DB::table('schools')
            ->select('season_prices.currency', \DB::raw('MIN(season_prices.price) AS min'))
            ->join('courses', function ($query) {
                $query->on('courses.school_id', '=', 'schools.id');
                $query->where('schools.id', '=', $this->id);
            })
            ->join('seasons', 'seasons.course_id', '=', 'courses.id')
            ->join('season_prices', 'season_prices.season_id', '=', 'seasons.id')
            ->first();


    $exchangeRate = DB::table('exchange_rates')
            ->select('dollar', 'euro', 'gbp')->get();

    foreach ($exchangeRate as $rate) {
        if ($minPrice->currency == 'chf'){
            return round($minPrice->min);
        } else if ($minPrice->currency == 'usd') {
            $calculated_rate  = 1 / $rate->dollar;
            return round($minPrice->min * $calculated_rate);
        } else if ($minPrice->currency == 'eur') {
            $calculated_rate  = 1 / $rate->euro;
            return round($minPrice->min * $calculated_rate);
        } else if ($minPrice->currency == 'gbp') {
            $calculated_rate  = 1 / $rate->gbp;
            return round($minPrice->min * $calculated_rate);
        }
    }
}

我在数据库中获得最低价格:

So, in my first query I am getting the lowest price in the database:

{#732 ▼
+"currency": "usd"
+"min": "5.00"
}

我从数据库获取货币设置由管理:

And in second query I am getting the currencies from the database set by admin:

array:1 [▼
  0 => {#844 ▼
    +"dollar": "1.0000"
    +"euro": "2.0000"
    +"gbp": "3.0000"
  }
]

然后我进行计算,将usd,gbp,euro转换为chf

And then I am doing the calculation to convert usd, gbp, euro to chf

    foreach ($exchangeRate as $rate) {
        if ($minPrice->currency == 'chf'){
            return round($minPrice->min);
        } else if ($minPrice->currency == 'usd') {
            $calculated_rate  = 1 / $rate->dollar;
            return round($minPrice->min * $calculated_rate);
        } else if ($minPrice->currency == 'eur') {
            $calculated_rate  = 1 / $rate->euro;
            return round($minPrice->min * $calculated_rate);
        } else if ($minPrice->currency == 'gbp') {
            $calculated_rate  = 1 / $rate->gbp;
            return round($minPrice->min * $calculated_rate);
        }
    }

任何帮助将不胜感激。

推荐答案

以下是解决此问题的一种方法:

Here's one way to solve this problem:

1)所以所有的费率都是相对于CHF。因此, exchange_rates 表可以有以下列: id,currency_name,currency_code,rate 。示例条目为:
1,'US Dollar','USD',0.99

1) Generalize your currency exchange table so that all the rates are relative to CHF. So maybe exchange_rates table can have the following columns: id, currency_name, currency_code, rate. An example entry would be: 1, 'US Dollar', 'USD', 0.99.

2)将 season_prices.currency 修改为指向 currency_exchange.id

3)修改现有查询以链接货币兑换表,并动态计算汇率并选择最小值:

3) Modify your existing query to link the currency exchange table in and calculate the rate dynamically and select the minimum:

$minPrice = DB::table('schools')
                ->join('courses', function ($query) {
                    $query->on('courses.school_id', '=', 'schools.id');
                    $query->where('schools.id', '=', $this->id);
                  })
                ->join('seasons', 'seasons.course_id', '=', 'courses.id')
                ->join('season_prices', 'season_prices.season_id', '=', 'seasons.id')
                ->join('exchange_rates', 'season_prices.currency', '=', 'exchange_rates.id')
                ->select('season_prices.currency', \DB::raw('MIN(season_prices.price * exchange_rate.rate) AS min'))
                ->first();

这篇关于Laravel逻辑 - 转换价格,所以在前端我总是chf值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆