Merge remote-tracking branch 'origin/master'

This commit is contained in:
2026-02-13 07:27:57 +03:00
parent d571fe2fd5
commit b2ce30fe79
37 changed files with 1667 additions and 447 deletions

View File

@@ -8,15 +8,12 @@ import (
"fmt"
)
// GetOrderByID — Sipariş başlığı (header) ve satırlarını (lines) getirir.
// GetOrderByID returns order header and lines.
func GetOrderByID(orderID string) (*models.OrderHeader, []models.OrderDetail, error) {
conn := db.GetDB()
logger.Printf("🧾 [GetOrderByID] begin id=%s", orderID)
logger.Printf("[GetOrderByID] begin id=%s", orderID)
// =====================================================
// HEADER (Cari adı join'li)
// =====================================================
var header models.OrderHeader
qHeader := `
SELECT
@@ -176,61 +173,76 @@ func GetOrderByID(orderID string) (*models.OrderHeader, []models.OrderDetail, er
&header.LastUpdatedDate,
&header.IsProposalBased,
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
logger.Printf("⚠️ [GetOrderByID] sipariş bulunamadı: %s", orderID)
logger.Printf("[GetOrderByID] not found: %s", orderID)
return nil, nil, sql.ErrNoRows
}
logger.Printf("[GetOrderByID] header sorgu hatası: %v", err)
logger.Printf("[GetOrderByID] header error: %v", err)
return nil, nil, err
}
logger.Printf("✅ [GetOrderByID] header loaded • orderNo=%v currAcc=%v",
header.OrderNumber, header.CurrAccCode.String)
// =====================================================
// LINES
// =====================================================
qLines := `
SELECT
CAST(L.OrderLineID AS varchar(36)) AS OrderLineID,
L.SortOrder,
L.ItemTypeCode,
L.ItemCode,
L.ColorCode,
L.ItemDim1Code,
L.ItemDim2Code,
L.ItemDim3Code,
L.Qty1,
L.Qty2,
L.Price,
L.VatRate,
L.PCTRate,
L.DocCurrencyCode,
L.DeliveryDate,
L.PlannedDateOfLading,
L.LineDescription,
L.IsClosed,
L.CreatedUserName,
L.CreatedDate,
L.LastUpdatedUserName,
L.LastUpdatedDate,
P.ProductAtt42Desc AS UrunIlkGrubu,
P.ProductAtt01Desc AS UrunAnaGrubu,
P.ProductAtt02Desc AS UrunAltGrubu,
P.ProductAtt38Desc AS Fit1,
P.ProductAtt39Desc AS Fit2
FROM BAGGI_V3.dbo.trOrderLine AS L
LEFT JOIN ProductFilterWithDescription('TR') AS P
ON LTRIM(RTRIM(P.ProductCode)) = LTRIM(RTRIM(L.ItemCode))
WHERE L.OrderHeaderID = @p1
ORDER BY L.SortOrder ASC;
`
SELECT
CAST(L.OrderLineID AS varchar(36)) AS OrderLineID,
L.SortOrder,
L.ItemTypeCode,
L.ItemCode,
L.ColorCode,
L.ItemDim1Code,
L.ItemDim2Code,
L.ItemDim3Code,
L.Qty1,
L.Qty2,
ISNULL(CD.Price, 0) AS Price,
ISNULL(CD.CurrencyCode, ISNULL(L.DocCurrencyCode, 'TRY')) AS DocCurrencyCode,
ISNULL(CD.RelationCurrencyCode, ISNULL(L.DocCurrencyCode, 'TRY')) AS RelationCurrencyCode,
ISNULL(CD.ExchangeRate, ISNULL(L.PriceExchangeRate, 1)) AS PriceExchangeRate,
ISNULL(CD.PriceVI, ISNULL(L.Price, 0)) AS DocPrice,
ISNULL(CD.AmountVI, ISNULL(L.Price, 0) * ISNULL(L.Qty1, 0)) AS DocAmount,
ISNULL(CD.LDiscount1, 0) AS LineDiscount,
ISNULL(CD.TDiscount1, 0) AS TotalDiscount,
ISNULL(CD.TaxBase, 0) AS TaxBase,
ISNULL(CD.Pct, 0) AS Pct,
ISNULL(CD.Vat, 0) AS VatAmount,
ISNULL(CD.VatDeducation, 0) AS VatDeducation,
ISNULL(CD.NetAmount, 0) AS NetAmount,
ISNULL(CL.Price, ISNULL(CD.Price, 0)) AS LocalPrice,
ISNULL(CL.Amount, ISNULL(CD.Amount, 0)) AS LocalAmount,
L.VatRate,
L.PCTRate,
L.DeliveryDate,
L.PlannedDateOfLading,
L.LineDescription,
L.IsClosed,
L.CreatedUserName,
L.CreatedDate,
L.LastUpdatedUserName,
L.LastUpdatedDate,
P.ProductAtt42Desc AS UrunIlkGrubu,
P.ProductAtt01Desc AS UrunAnaGrubu,
P.ProductAtt02Desc AS UrunAltGrubu,
P.ProductAtt38Desc AS Fit1,
P.ProductAtt39Desc AS Fit2
FROM BAGGI_V3.dbo.trOrderLine AS L
LEFT JOIN BAGGI_V3.dbo.trOrderLineCurrency AS CD WITH (NOLOCK)
ON CD.OrderLineID = L.OrderLineID
AND CD.CurrencyCode = ISNULL(NULLIF(LTRIM(RTRIM(L.DocCurrencyCode)), ''), 'TRY')
LEFT JOIN BAGGI_V3.dbo.trOrderLineCurrency AS CL WITH (NOLOCK)
ON CL.OrderLineID = L.OrderLineID
AND CL.CurrencyCode = 'TRY'
LEFT JOIN ProductFilterWithDescription('TR') AS P
ON LTRIM(RTRIM(P.ProductCode)) = LTRIM(RTRIM(L.ItemCode))
WHERE L.OrderHeaderID = @p1
ORDER BY L.SortOrder ASC;
`
rows, err := conn.Query(qLines, orderID)
if err != nil {
logger.Printf("[GetOrderByID] line sorgu hatası: %v", err)
logger.Printf("[GetOrderByID] lines error: %v", err)
return &header, nil, err
}
defer rows.Close()
@@ -250,9 +262,22 @@ func GetOrderByID(orderID string) (*models.OrderHeader, []models.OrderDetail, er
&ln.Qty1,
&ln.Qty2,
&ln.Price,
&ln.DocCurrencyCode,
&ln.RelationCurrencyCode,
&ln.PriceExchangeRate,
&ln.DocPrice,
&ln.DocAmount,
&ln.LineDiscount,
&ln.TotalDiscount,
&ln.TaxBase,
&ln.Pct,
&ln.VatAmount,
&ln.VatDeducation,
&ln.NetAmount,
&ln.LocalPrice,
&ln.LocalAmount,
&ln.VatRate,
&ln.PCTRate,
&ln.DocCurrencyCode,
&ln.DeliveryDate,
&ln.PlannedDateOfLading,
&ln.LineDescription,
@@ -267,14 +292,14 @@ func GetOrderByID(orderID string) (*models.OrderHeader, []models.OrderDetail, er
&ln.Fit1,
&ln.Fit2,
); err != nil {
return &header, nil, fmt.Errorf("line scan hatası: %w", err)
return &header, nil, fmt.Errorf("line scan error: %w", err)
}
lines = append(lines, ln)
}
if err := rows.Err(); err != nil {
return &header, nil, fmt.Errorf("line rows hatası: %w", err)
return &header, nil, fmt.Errorf("line rows error: %w", err)
}
logger.Printf("📦 [GetOrderByID] lines loaded count=%d", len(lines))
logger.Printf("[GetOrderByID] lines loaded count=%d", len(lines))
return &header, lines, nil
}