Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"bssapp-backend/auth"
|
||||
"bssapp-backend/models"
|
||||
"bssapp-backend/queries"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
@@ -40,6 +43,9 @@ func OrderProductionItemsRoute(mssql *sql.DB) http.Handler {
|
||||
if err := rows.Scan(
|
||||
&o.OrderHeaderID,
|
||||
&o.OrderLineID,
|
||||
&o.ItemTypeCode,
|
||||
&o.OldDim1,
|
||||
&o.OldDim3,
|
||||
&o.OldItemCode,
|
||||
&o.OldColor,
|
||||
&o.OldDim2,
|
||||
@@ -48,6 +54,7 @@ func OrderProductionItemsRoute(mssql *sql.DB) http.Handler {
|
||||
&o.NewColor,
|
||||
&o.NewDim2,
|
||||
&o.NewDesc,
|
||||
&o.IsVariantMissing,
|
||||
); err != nil {
|
||||
log.Printf("⚠️ SCAN HATASI: %v", err)
|
||||
continue
|
||||
@@ -64,3 +71,224 @@ func OrderProductionItemsRoute(mssql *sql.DB) http.Handler {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// 📌 OrderProductionInsertMissingRoute — eksik varyantları ekler
|
||||
// ======================================================
|
||||
func OrderProductionInsertMissingRoute(mssql *sql.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
if id == "" {
|
||||
http.Error(w, "OrderHeaderID bulunamadı", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
claims, _ := auth.GetClaimsFromContext(r.Context())
|
||||
username := ""
|
||||
if claims != nil {
|
||||
username = claims.Username
|
||||
}
|
||||
if username == "" {
|
||||
username = "system"
|
||||
}
|
||||
|
||||
affected, err := queries.InsertMissingProductionVariants(mssql, id, username)
|
||||
if err != nil {
|
||||
log.Printf("❌ INSERT varyant hatası: %v", err)
|
||||
http.Error(w, "Veritabanı hatası", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
resp := map[string]any{
|
||||
"inserted": affected,
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
||||
log.Printf("❌ encode error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// OrderProductionValidateRoute - yeni model varyant kontrolu
|
||||
// ======================================================
|
||||
func OrderProductionValidateRoute(mssql *sql.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
if id == "" {
|
||||
http.Error(w, "OrderHeaderID bulunamadi", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var payload models.OrderProductionUpdatePayload
|
||||
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
||||
http.Error(w, "Gecersiz istek", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := validateUpdateLines(payload.Lines); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
missing, err := buildMissingVariants(mssql, id, payload.Lines)
|
||||
if err != nil {
|
||||
log.Printf("❌ validate error: %v", err)
|
||||
http.Error(w, "Veritabani hatasi", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
resp := map[string]any{
|
||||
"missingCount": len(missing),
|
||||
"missing": missing,
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
||||
log.Printf("❌ encode error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// OrderProductionApplyRoute - yeni model varyant guncelleme
|
||||
// ======================================================
|
||||
func OrderProductionApplyRoute(mssql *sql.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
if id == "" {
|
||||
http.Error(w, "OrderHeaderID bulunamadi", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var payload models.OrderProductionUpdatePayload
|
||||
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
||||
http.Error(w, "Gecersiz istek", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := validateUpdateLines(payload.Lines); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
missing, err := buildMissingVariants(mssql, id, payload.Lines)
|
||||
if err != nil {
|
||||
log.Printf("❌ apply validate error: %v", err)
|
||||
http.Error(w, "Veritabani hatasi", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if len(missing) > 0 && !payload.InsertMissing {
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"missingCount": len(missing),
|
||||
"missing": missing,
|
||||
"message": "Eksik varyantlar var",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
claims, _ := auth.GetClaimsFromContext(r.Context())
|
||||
username := ""
|
||||
if claims != nil {
|
||||
username = claims.Username
|
||||
}
|
||||
if strings.TrimSpace(username) == "" {
|
||||
username = "system"
|
||||
}
|
||||
|
||||
tx, err := mssql.Begin()
|
||||
if err != nil {
|
||||
http.Error(w, "Veritabani hatasi", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
var inserted int64
|
||||
if payload.InsertMissing {
|
||||
inserted, err = queries.InsertMissingVariantsTx(tx, missing, username)
|
||||
if err != nil {
|
||||
log.Printf("❌ insert missing error: %v", err)
|
||||
http.Error(w, "Veritabani hatasi", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
updated, err := queries.UpdateOrderLinesTx(tx, id, payload.Lines, username)
|
||||
if err != nil {
|
||||
log.Printf("❌ update order lines error: %v", err)
|
||||
http.Error(w, "Veritabani hatasi", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
log.Printf("❌ commit error: %v", err)
|
||||
http.Error(w, "Veritabani hatasi", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
resp := map[string]any{
|
||||
"updated": updated,
|
||||
"inserted": inserted,
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
||||
log.Printf("❌ encode error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func buildMissingVariants(mssql *sql.DB, orderHeaderID string, lines []models.OrderProductionUpdateLine) ([]models.OrderProductionMissingVariant, error) {
|
||||
missing := make([]models.OrderProductionMissingVariant, 0)
|
||||
|
||||
for _, line := range lines {
|
||||
lineID := strings.TrimSpace(line.OrderLineID)
|
||||
newItem := strings.TrimSpace(line.NewItemCode)
|
||||
newColor := strings.TrimSpace(line.NewColor)
|
||||
newDim2 := strings.TrimSpace(line.NewDim2)
|
||||
|
||||
if lineID == "" || newItem == "" || newColor == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
itemTypeCode, dim1, _, dim3, err := queries.GetOrderLineDims(mssql, orderHeaderID, lineID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
exists, err := queries.VariantExists(mssql, itemTypeCode, newItem, newColor, dim1, newDim2, dim3)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
missing = append(missing, models.OrderProductionMissingVariant{
|
||||
OrderLineID: lineID,
|
||||
ItemTypeCode: itemTypeCode,
|
||||
ItemCode: newItem,
|
||||
ColorCode: newColor,
|
||||
ItemDim1Code: dim1,
|
||||
ItemDim2Code: newDim2,
|
||||
ItemDim3Code: dim3,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return missing, nil
|
||||
}
|
||||
|
||||
func validateUpdateLines(lines []models.OrderProductionUpdateLine) error {
|
||||
for _, line := range lines {
|
||||
if strings.TrimSpace(line.OrderLineID) == "" {
|
||||
return errors.New("OrderLineID zorunlu")
|
||||
}
|
||||
if strings.TrimSpace(line.NewItemCode) == "" {
|
||||
return errors.New("Yeni urun kodu zorunlu")
|
||||
}
|
||||
if strings.TrimSpace(line.NewColor) == "" {
|
||||
return errors.New("Yeni renk kodu zorunlu")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user