Merge remote-tracking branch 'origin/master'

This commit is contained in:
M_Kececi
2026-03-03 13:29:17 +03:00
parent 4805216808
commit d355ef7acd
21 changed files with 279 additions and 227 deletions

View File

@@ -13,13 +13,48 @@ import (
func GetAccounts(ctx context.Context) ([]models.Account, error) {
piyasaFilter := authz.BuildMSSQLPiyasaFilter(ctx, "f2.CustomerAtt01")
piyasaFilter := authz.BuildMSSQLPiyasaFilter(
ctx,
"CASE WHEN b.CurrAccTypeCode = 1 THEN vp.VendorAtt01 ELSE f2.CustomerAtt01 END",
)
if strings.TrimSpace(piyasaFilter) == "" {
piyasaFilter = "1=1"
}
query := fmt.Sprintf(`
;WITH VendorPiyasa AS
(
SELECT
Cari8 = LEFT(P.CurrAccCode, 8),
VendorAtt01 = MAX(P.VendorAtt01)
FROM
(
SELECT
CurrAccTypeCode,
CurrAccCode,
VendorAtt01 = MAX(ISNULL([1], ''))
FROM
(
SELECT
c.CurrAccTypeCode,
c.CurrAccCode,
a.AttributeTypeCode,
a.AttributeCode
FROM cdCurrAcc c WITH (NOLOCK)
LEFT JOIN prCurrAccAttribute a WITH (NOLOCK)
ON a.CurrAccTypeCode = c.CurrAccTypeCode
AND a.CurrAccCode = c.CurrAccCode
WHERE c.CurrAccTypeCode = 1
) d
PIVOT
(
MAX(AttributeCode) FOR AttributeTypeCode IN ([1])
) pvt
GROUP BY CurrAccTypeCode, CurrAccCode
) P
GROUP BY LEFT(P.CurrAccCode, 8)
)
SELECT
x.AccountCode,
MAX(x.AccountName) AS AccountName
@@ -29,10 +64,16 @@ func GetAccounts(ctx context.Context) ([]models.Account, error) {
COALESCE(d.CurrAccDescription, '') AS AccountName
FROM trCurrAccBook b
LEFT JOIN cdCurrAccDesc d
ON d.CurrAccCode = b.CurrAccCode
JOIN CustomerAttributesFilter f2
ON f2.CurrAccCode = b.CurrAccCode
WHERE %s
ON d.CurrAccTypeCode = b.CurrAccTypeCode
AND d.CurrAccCode = b.CurrAccCode
AND d.LangCode = 'TR'
LEFT JOIN CustomerAttributesFilter f2
ON f2.CurrAccTypeCode = b.CurrAccTypeCode
AND f2.CurrAccCode = b.CurrAccCode
LEFT JOIN VendorPiyasa vp
ON vp.Cari8 = LEFT(b.CurrAccCode, 8)
WHERE b.CurrAccTypeCode IN (1,3)
AND %s
) x
GROUP BY x.AccountCode
ORDER BY x.AccountCode

View File

@@ -314,7 +314,12 @@ ORDER BY F.MasterCari;
}
func loadBalanceLines(ctx context.Context, selectedDate, cariSearch string) ([]mkCariBakiyeLine, error) {
query := `
piyasaScope, err := buildPiyasaExistsForCariCode(ctx, "CariKodu")
if err != nil {
return nil, err
}
query := fmt.Sprintf(`
SELECT
CurrAccTypeCode,
CariKodu,
@@ -326,8 +331,9 @@ func loadBalanceLines(ctx context.Context, selectedDate, cariSearch string) ([]m
CAST(0 AS DECIMAL(18,4)) AS Vade_Gun,
CAST(0 AS DECIMAL(18,4)) AS Vade_BelgeTarihi_Gun
FROM dbo.MK_CARI_BAKIYE_LIST(@SonTarih)
WHERE (@CariSearch = '' OR CariKodu LIKE '%' + @CariSearch + '%')
`
WHERE (@CariSearch = '' OR CariKodu LIKE '%%' + @CariSearch + '%%')
AND %s
`, piyasaScope)
rows, err := db.MssqlDB.QueryContext(ctx, query,
sql.Named("SonTarih", selectedDate),

View File

@@ -182,7 +182,12 @@ func GetStatementAgingBalanceList(ctx context.Context, params models.CustomerBal
}
func loadAgingBalanceLines(ctx context.Context, cariSearch string) ([]mkCariBakiyeLine, error) {
query := `
piyasaScope, err := buildPiyasaExistsForCariCode(ctx, "LTRIM(RTRIM(CariKodu))")
if err != nil {
return nil, err
}
query := fmt.Sprintf(`
SELECT
CurrAccTypeCode,
CariKodu = LTRIM(RTRIM(CariKodu)),
@@ -194,9 +199,10 @@ func loadAgingBalanceLines(ctx context.Context, cariSearch string) ([]mkCariBaki
Vade_Gun,
Vade_BelgeTarihi_Gun
FROM dbo.CARI_BAKIYE_GUN_CACHE
WHERE (@CariSearch = '' OR LTRIM(RTRIM(CariKodu)) LIKE '%' + @CariSearch + '%')
WHERE (@CariSearch = '' OR LTRIM(RTRIM(CariKodu)) LIKE '%%' + @CariSearch + '%%')
AND %s
ORDER BY CariKodu, CariDoviz, PislemTipi
`
`, piyasaScope)
rows, err := db.MssqlDB.QueryContext(ctx, query, sql.Named("CariSearch", strings.TrimSpace(cariSearch)))
if err != nil {

View File

@@ -3,13 +3,14 @@ package queries
import (
"bssapp-backend/db"
"bssapp-backend/models"
"context"
"database/sql"
"fmt"
"strings"
)
// Ana tabloyu getiren fonksiyon (Vue header tablosu için)
func GetStatements(params models.StatementParams) ([]models.StatementHeader, error) {
func GetStatements(ctx context.Context, params models.StatementParams) ([]models.StatementHeader, error) {
// AccountCode normalize: "ZLA0127" → "ZLA 0127"
params.AccountCode = normalizeMasterAccountCode(params.AccountCode)
@@ -33,6 +34,11 @@ func GetStatements(params models.StatementParams) ([]models.StatementHeader, err
}
}
piyasaScope, err := buildPiyasaExistsForCariCode(ctx, "b.CurrAccCode")
if err != nil {
return nil, err
}
query := fmt.Sprintf(`
;WITH CurrDesc AS (
SELECT
@@ -58,6 +64,7 @@ HasMovement AS (
AND f.ATAtt01 IN (%s)
WHERE LEFT(REPLACE(b.CurrAccCode, ' ', ''), 7) = REPLACE(@Carikod, ' ', '')
AND b.DocumentDate BETWEEN @startdate AND @enddate
AND %s
) THEN 1 ELSE 0 END AS HasMov
),
@@ -79,6 +86,7 @@ Opening AS (
ON c.CurrAccBookID = b.CurrAccBookID
AND c.CurrencyCode = b.DocCurrencyCode
WHERE LEFT(REPLACE(b.CurrAccCode, ' ', ''), 7) = REPLACE(@Carikod, ' ', '')
AND %s
AND (
(hm.HasMov = 1 AND b.DocumentDate < @startdate) -- hareket varsa: klasik devir
OR (hm.HasMov = 0 AND b.DocumentDate <= @enddate) -- hareket yoksa: enddate itibariyle bakiye
@@ -135,6 +143,7 @@ Movements AS (
AND c.CurrencyCode = b.DocCurrencyCode
WHERE LEFT(REPLACE(b.CurrAccCode, ' ', ''), 7) = REPLACE(@Carikod, ' ', '')
AND %s
AND b.DocumentDate BETWEEN @startdate AND @enddate
)
@@ -201,12 +210,15 @@ ORDER BY
Para_Birimi,
Belge_Tarihi;
`,
parislemFilter, // HasMovement
parislemFilter, // Opening
parislemFilter, // Movements
parislemFilter, // HasMovement ATAtt01
piyasaScope, // HasMovement piyasa scope
parislemFilter, // Opening ATAtt01
piyasaScope, // Opening piyasa scope
parislemFilter, // Movements ATAtt01
piyasaScope, // Movements piyasa scope
)
rows, err := db.MssqlDB.Query(query,
rows, err := db.MssqlDB.QueryContext(ctx, query,
sql.Named("startdate", params.StartDate),
sql.Named("enddate", params.EndDate),
sql.Named("Carikod", params.AccountCode),

View File

@@ -2,11 +2,12 @@ package queries
import (
"bssapp-backend/models"
"context"
"log"
)
func GetStatementsHPDF(accountCode, startDate, endDate string, parislemler []string) ([]models.StatementHeader, []string, error) {
headers, err := getStatementsForPDF(accountCode, startDate, endDate, parislemler)
func GetStatementsHPDF(ctx context.Context, accountCode, startDate, endDate string, parislemler []string) ([]models.StatementHeader, []string, error) {
headers, err := getStatementsForPDF(ctx, accountCode, startDate, endDate, parislemler)
if err != nil {
log.Printf("Header query error: %v", err)
return nil, nil, err

View File

@@ -1,14 +1,18 @@
package queries
import "bssapp-backend/models"
import (
"bssapp-backend/models"
"context"
)
func getStatementsForPDF(
ctx context.Context,
accountCode string,
startDate string,
endDate string,
parislemler []string,
) ([]models.StatementHeader, error) {
return GetStatements(models.StatementParams{
return GetStatements(ctx, models.StatementParams{
AccountCode: accountCode,
StartDate: startDate,
EndDate: endDate,

View File

@@ -3,15 +3,14 @@ package queries
import (
"bssapp-backend/db"
"bssapp-backend/models"
"context"
"database/sql"
"fmt"
"strings"
)
/* ============================ DETAIL (ALT TABLO) ============================ */
func GetStatementDetails(accountCode, startDate, endDate string, parislemler []string) ([]models.StatementDetail, error) {
// Parislemler filtresi hazırlanır (ör: 1,2,3)
// DETAIL (ALT TABLO)
func GetStatementDetails(ctx context.Context, accountCode, startDate, endDate string, parislemler []string) ([]models.StatementDetail, error) {
inParislem := ""
if len(parislemler) > 0 {
pp := make([]string, len(parislemler))
@@ -20,87 +19,84 @@ func GetStatementDetails(accountCode, startDate, endDate string, parislemler []s
}
inParislem = strings.Join(pp, ",")
}
piyasaScope, err := buildPiyasaExistsForCariCode(ctx, "a.CurrAccCode")
if err != nil {
return nil, err
}
query := fmt.Sprintf(`
SELECT
CONVERT(varchar(10), a.InvoiceDate, 23) AS Belge_Tarihi,
a.InvoiceNumber AS Belge_Ref_Numarasi,
COALESCE(MAX(AnaGrupDesc.AttributeDescription), '') AS Urun_Ana_Grubu,
COALESCE(MAX(AltGrupDesc.AttributeDescription), '') AS Urun_Alt_Grubu,
COALESCE(MAX(GarsonDesc.AttributeDescription), '') AS Yetiskin_Garson,
COALESCE(MAX(FitDesc.AttributeDescription), '') AS Fit,
COALESCE(MAX(KisaKarDesc.AttributeDescription), '') AS Icerik,
a.ItemCode AS Urun_Kodu,
a.ColorCode AS Urun_Rengi,
COALESCE(MAX(AnaGrupDesc.AttributeDescription), '') AS Urun_Ana_Grubu,
COALESCE(MAX(AltGrupDesc.AttributeDescription), '') AS Urun_Alt_Grubu,
COALESCE(MAX(GarsonDesc.AttributeDescription), '') AS Yetiskin_Garson,
COALESCE(MAX(FitDesc.AttributeDescription), '') AS Fit,
COALESCE(MAX(KisaKarDesc.AttributeDescription), '') AS Icerik,
a.ItemCode AS Urun_Kodu,
a.ColorCode AS Urun_Rengi,
SUM(a.Qty1) AS Toplam_Adet,
CAST(
SUM(a.Qty1 * ABS(a.Doc_Price))
/ NULLIF(SUM(a.Qty1),0)
AS numeric(18,4)) AS Doviz_Fiyat,
CAST(
SUM(a.Qty1 * ABS(a.Doc_Price))
AS numeric(18,2)) AS Toplam_Tutar
CAST(SUM(a.Qty1 * ABS(a.Doc_Price)) / NULLIF(SUM(a.Qty1),0) AS numeric(18,4)) AS Doviz_Fiyat,
CAST(SUM(a.Qty1 * ABS(a.Doc_Price)) AS numeric(18,2)) AS Toplam_Tutar
FROM AllInvoicesWithAttributes a
LEFT JOIN prItemAttribute AnaGrup
ON a.ItemCode = AnaGrup.ItemCode AND AnaGrup.AttributeTypeCode = 1
ON a.ItemCode = AnaGrup.ItemCode AND AnaGrup.AttributeTypeCode = 1
LEFT JOIN cdItemAttributeDesc AnaGrupDesc
ON AnaGrup.AttributeTypeCode = AnaGrupDesc.AttributeTypeCode
AND AnaGrup.AttributeCode = AnaGrupDesc.AttributeCode
AND AnaGrup.ItemTypeCode = AnaGrupDesc.ItemTypeCode
ON AnaGrup.AttributeTypeCode = AnaGrupDesc.AttributeTypeCode
AND AnaGrup.AttributeCode = AnaGrupDesc.AttributeCode
AND AnaGrup.ItemTypeCode = AnaGrupDesc.ItemTypeCode
LEFT JOIN prItemAttribute AltGrup
ON a.ItemCode = AltGrup.ItemCode AND AltGrup.AttributeTypeCode = 2
ON a.ItemCode = AltGrup.ItemCode AND AltGrup.AttributeTypeCode = 2
LEFT JOIN cdItemAttributeDesc AltGrupDesc
ON AltGrup.AttributeTypeCode = AltGrupDesc.AttributeTypeCode
AND AltGrup.AttributeCode = AltGrupDesc.AttributeCode
AND AltGrup.ItemTypeCode = AltGrupDesc.ItemTypeCode
ON AltGrup.AttributeTypeCode = AltGrupDesc.AttributeTypeCode
AND AltGrup.AttributeCode = AltGrupDesc.AttributeCode
AND AltGrup.ItemTypeCode = AltGrupDesc.ItemTypeCode
LEFT JOIN prItemAttribute Garson
ON a.ItemCode = Garson.ItemCode AND Garson.AttributeTypeCode = 44
ON a.ItemCode = Garson.ItemCode AND Garson.AttributeTypeCode = 44
LEFT JOIN cdItemAttributeDesc GarsonDesc
ON Garson.AttributeTypeCode = GarsonDesc.AttributeTypeCode
AND Garson.AttributeCode = GarsonDesc.AttributeCode
AND Garson.ItemTypeCode = GarsonDesc.ItemTypeCode
ON Garson.AttributeTypeCode = GarsonDesc.AttributeTypeCode
AND Garson.AttributeCode = GarsonDesc.AttributeCode
AND Garson.ItemTypeCode = GarsonDesc.ItemTypeCode
LEFT JOIN prItemAttribute FitTbl
ON a.ItemCode = FitTbl.ItemCode AND FitTbl.AttributeTypeCode = 38
ON a.ItemCode = FitTbl.ItemCode AND FitTbl.AttributeTypeCode = 38
LEFT JOIN cdItemAttributeDesc FitDesc
ON FitTbl.AttributeTypeCode = FitDesc.AttributeTypeCode
AND FitTbl.AttributeCode = FitDesc.AttributeCode
AND FitTbl.ItemTypeCode = FitDesc.ItemTypeCode
ON FitTbl.AttributeTypeCode = FitDesc.AttributeTypeCode
AND FitTbl.AttributeCode = FitDesc.AttributeCode
AND FitTbl.ItemTypeCode = FitDesc.ItemTypeCode
LEFT JOIN prItemAttribute KisaKar
ON a.ItemCode = KisaKar.ItemCode AND KisaKar.AttributeTypeCode = 41
ON a.ItemCode = KisaKar.ItemCode AND KisaKar.AttributeTypeCode = 41
LEFT JOIN cdItemAttributeDesc KisaKarDesc
ON KisaKar.AttributeTypeCode = KisaKarDesc.AttributeTypeCode
AND KisaKar.AttributeCode = KisaKarDesc.AttributeCode
AND KisaKar.ItemTypeCode = KisaKarDesc.ItemTypeCode
ON KisaKar.AttributeTypeCode = KisaKarDesc.AttributeTypeCode
AND KisaKar.AttributeCode = KisaKarDesc.AttributeCode
AND KisaKar.ItemTypeCode = KisaKarDesc.ItemTypeCode
WHERE a.CurrAccCode LIKE @Carikod
AND a.InvoiceDate BETWEEN @StartDate AND @EndDate
AND %s
%s
GROUP BY a.InvoiceDate, a.InvoiceNumber, a.ItemCode, a.ColorCode
ORDER BY Belge_Tarihi, Belge_Ref_Numarasi, Urun_Kodu;`,
piyasaScope,
func() string {
if inParislem == "" {
return ""
}
return fmt.Sprintf(`AND EXISTS (
SELECT 1
FROM CurrAccBookATAttributesFilter f
WHERE f.CurrAccBookID = a.CurrAccBookID
AND f.ATAtt01 IN (%s)
)`, inParislem)
SELECT 1
FROM CurrAccBookATAttributesFilter f
WHERE f.CurrAccBookID = a.CurrAccBookID
AND f.ATAtt01 IN (%s)
)`, inParislem)
}(),
)
rows, err := db.MssqlDB.Query(query,
rows, err := db.MssqlDB.QueryContext(ctx, query,
sql.Named("Carikod", "%"+accountCode+"%"),
sql.Named("StartDate", startDate),
sql.Named("EndDate", endDate),
)
if err != nil {
return nil, fmt.Errorf("detay sorgu hatası: %v", err)
return nil, fmt.Errorf("detay sorgu hatasi: %v", err)
}
defer rows.Close()

View File

@@ -4,14 +4,15 @@ package queries
import (
"bssapp-backend/db"
"bssapp-backend/models"
"context"
"database/sql"
"fmt"
"log"
"strings"
)
func GetStatementsPDF(accountCode, startDate, endDate string, parislemler []string) ([]models.StatementHeader, []string, error) {
headers, err := getStatementsForPDF(accountCode, startDate, endDate, parislemler)
func GetStatementsPDF(ctx context.Context, accountCode, startDate, endDate string, parislemler []string) ([]models.StatementHeader, []string, error) {
headers, err := getStatementsForPDF(ctx, accountCode, startDate, endDate, parislemler)
if err != nil {
log.Printf("Header query error: %v", err)
return nil, nil, err

View File

@@ -52,12 +52,19 @@ ORDER BY d.code
// 🌍 PIYASALAR
// ======================================================
const GetUserPiyasalar = `
SELECT p.code, p.title
SELECT
COALESCE(p_code.code, p_title.code, up.piyasa_code) AS code,
COALESCE(p_code.title, p_title.title, up.piyasa_code) AS title
FROM dfusr_piyasa up
JOIN mk_sales_piy p ON p.code = up.piyasa_code
LEFT JOIN mk_sales_piy p_code
ON UPPER(translate(TRIM(p_code.code), 'çğıöşüÇĞİÖŞÜ', 'CGIOSUCGIOSU'))
= UPPER(translate(TRIM(up.piyasa_code), 'çğıöşüÇĞİÖŞÜ', 'CGIOSUCGIOSU'))
LEFT JOIN mk_sales_piy p_title
ON UPPER(translate(TRIM(p_title.title), 'çğıöşüÇĞİÖŞÜ', 'CGIOSUCGIOSU'))
= UPPER(translate(TRIM(up.piyasa_code), 'çğıöşüÇĞİÖŞÜ', 'CGIOSUCGIOSU'))
WHERE up.dfusr_id = $1
AND up.is_allowed = true
ORDER BY p.code
ORDER BY 1
`
// ======================================================