Files
bssapp/svc/routes/orderpricelistb2b.go
2026-02-11 17:46:22 +03:00

132 lines
2.5 KiB
Go
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package routes
import (
"bssapp-backend/models"
"bssapp-backend/queries"
"database/sql"
"encoding/json"
"fmt"
"net/http"
"strings"
)
// ✅ Model + pb/currency paramına göre fiyat döner (debug loglu)
func GetOrderPriceListB2BHandler(pg *sql.DB, mssql *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
model := r.URL.Query().Get("model")
if model == "" {
model = r.URL.Query().Get("code")
}
cur := r.URL.Query().Get("currency")
if cur == "" {
cur = r.URL.Query().Get("pb")
}
if model == "" || cur == "" {
http.Error(w, "Eksik parametre", 400)
return
}
cur = strings.ToUpper(cur)
fmt.Printf("💰 [MINPRICE] model=%s cur=%s\n", model, cur)
var (
price float64
rate float64 = 1
)
/* ======================
USD / EUR → Direkt
====================== */
if cur == "USD" || cur == "EUR" {
p, err := queries.GetOrderPriceListB2B(pg, model, cur)
if err != nil {
http.Error(w, cur+" fiyat bulunamadı", 404)
return
}
price = p.Price
} else if cur == "TRY" {
/* ======================
TRY → USD * Kur
====================== */
// 1⃣ USD fiyat
p, err := queries.GetOrderPriceListB2B(pg, model, "USD")
if err != nil {
http.Error(w, "USD fiyat bulunamadı", 404)
return
}
// 2⃣ USD→TRY satış
r, err := queries.GetCachedCurrencyV3(mssql, "USD")
if err != nil {
http.Error(w, "USD kuru bulunamadı", 503)
return
}
rate = r.Rate
price = p.Price * rate
} else if cur == "GBP" {
/* ======================
GBP → USD Bridge
====================== */
// 1⃣ USD fiyat
p, err := queries.GetOrderPriceListB2B(pg, model, "USD")
if err != nil {
http.Error(w, "USD fiyat bulunamadı", 404)
return
}
// 2⃣ USD→TRY
usd, err := queries.GetCachedCurrencyV3(mssql, "USD")
if err != nil {
http.Error(w, "USD kuru yok", 503)
return
}
// 3⃣ GBP→TRY
gbp, err := queries.GetCachedCurrencyV3(mssql, "GBP")
if err != nil {
http.Error(w, "GBP kuru yok", 503)
return
}
rate = usd.Rate / gbp.Rate
price = p.Price * rate
} else {
http.Error(w, "Desteklenmeyen para birimi", 400)
return
}
resp := models.OrderPriceListB2B{
ModelCode: model,
CurrencyCode: cur,
Price: price,
RateToTRY: rate,
PriceTRY: price,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resp)
fmt.Printf("✅ [MINPRICE] %s → %.2f %s\n", model, price, cur)
}
}