Merge remote-tracking branch 'origin/master'

This commit is contained in:
M_Kececi
2026-03-06 10:56:53 +03:00
parent 9097b5af2d
commit 9e534e9a34
18 changed files with 2204 additions and 2015 deletions

View File

@@ -7,9 +7,7 @@ import (
"bytes"
"database/sql"
"fmt"
"log"
"net/http"
"runtime/debug"
"sort"
"strconv"
"strings"
@@ -33,18 +31,10 @@ type balanceSummaryPDF struct {
TLBakiye13 float64
VadeGun float64
VadeBelge float64
VadeBase float64
}
func ExportCustomerBalancePDFHandler(_ *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rec := recover(); rec != nil {
log.Printf("PANIC ExportCustomerBalancePDFHandler: %v\n%s", rec, string(debug.Stack()))
http.Error(w, "internal server error", http.StatusInternalServerError)
}
}()
claims, ok := auth.GetClaimsFromContext(r.Context())
if !ok || claims == nil {
http.Error(w, "unauthorized", http.StatusUnauthorized)
@@ -147,6 +137,9 @@ func filterCustomerBalanceRowsForPDF(rows []models.CustomerBalanceListRow, exclu
func buildCustomerBalancePDFData(rows []models.CustomerBalanceListRow) ([]balanceSummaryPDF, map[string][]models.CustomerBalanceListRow) {
summaryMap := make(map[string]*balanceSummaryPDF)
detailsByMaster := make(map[string][]models.CustomerBalanceListRow)
vadeWeightMap := make(map[string]float64)
vadeGunSumMap := make(map[string]float64)
vadeBelgeSumMap := make(map[string]float64)
for _, row := range rows {
master := strings.TrimSpace(row.AnaCariKodu)
@@ -194,11 +187,11 @@ func buildCustomerBalancePDFData(rows []models.CustomerBalanceListRow) ([]balanc
s.TLBakiye12 += row.TLBakiye12
s.USDBakiye13 += row.USDBakiye13
s.TLBakiye13 += row.TLBakiye13
w := absFloat(row.Bakiye12) + absFloat(row.Bakiye13)
w := absFloatExcel(row.USDBakiye12) + absFloatExcel(row.TLBakiye12) + absFloatExcel(row.USDBakiye13) + absFloatExcel(row.TLBakiye13)
if w > 0 {
s.VadeBase += w
s.VadeGun += row.VadeGun * w
s.VadeBelge += row.VadeBelgeGun * w
vadeWeightMap[master] += w
vadeGunSumMap[master] += row.VadeGun * w
vadeBelgeSumMap[master] += row.VadeBelgeGun * w
}
detailsByMaster[master] = append(detailsByMaster[master], row)
@@ -212,10 +205,9 @@ func buildCustomerBalancePDFData(rows []models.CustomerBalanceListRow) ([]balanc
summaries := make([]balanceSummaryPDF, 0, len(masters))
for _, m := range masters {
s := summaryMap[m]
if s != nil && s.VadeBase > 0 {
s.VadeGun = s.VadeGun / s.VadeBase
s.VadeBelge = s.VadeBelge / s.VadeBase
if base := vadeWeightMap[m]; base > 0 {
summaryMap[m].VadeGun = vadeGunSumMap[m] / base
summaryMap[m].VadeBelge = vadeBelgeSumMap[m] / base
}
summaries = append(summaries, *summaryMap[m])
d := detailsByMaster[m]
@@ -248,11 +240,11 @@ func drawCustomerBalancePDF(
marginL, marginT, marginR, marginB := 8.0, 8.0, 8.0, 12.0
tableW := pageW - marginL - marginR
summaryCols := []string{"Ana Cari Kod", "Ana Cari Detay", "Piyasa", "Temsilci", "Risk", "1_2 Pr.Br", "1_3 Pr.Br", "1_2 USD", "1_2 TRY", "1_3 USD", "1_3 TRY", "Vade Gun", "Belge Gun"}
summaryW := normalizeWidths([]float64{18, 46, 14, 18, 12, 20, 20, 12, 12, 12, 12, 10, 10}, tableW)
summaryCols := []string{"Ana Cari Kod", "Ana Cari Detay", "Piyasa", "Temsilci", "Risk", "1_2 Pr.Br", "1_3 Pr.Br", "1_2 USD", "1_2 TRY", "1_3 USD", "1_3 TRY"}
summaryW := normalizeWidths([]float64{20, 43, 18, 18, 16, 27, 27, 15, 15, 15, 15}, tableW)
detailCols := []string{"Cari Kod", "Cari Detay", "Sirket", "Muhasebe", "Doviz", "1_2 Pr.Br", "1_3 Pr.Br", "1_2 USD", "1_2 TRY", "1_3 USD", "1_3 TRY", "Vade Gun", "Belge Gun"}
detailW := normalizeWidths([]float64{22, 40, 9, 16, 8, 20, 20, 12, 12, 12, 12, 9, 9}, tableW)
detailCols := []string{"Cari Kod", "Cari Detay", "Sirket", "Muhasebe", "Doviz", "1_2 Pr.Br", "1_3 Pr.Br", "1_2 USD", "1_2 TRY", "1_3 USD", "1_3 TRY"}
detailW := normalizeWidths([]float64{26, 46, 10, 20, 10, 24, 24, 15, 15, 15, 15}, tableW)
header := func() {
pdf.AddPage()
@@ -288,44 +280,6 @@ func drawCustomerBalancePDF(
return pdf.GetY()+needH+marginB > 210.0
}
wrappedLines := func(text string, w float64) [][]byte {
t := strings.TrimSpace(sanitizePDFText(text))
if t == "" {
t = "-"
}
return splitLinesSafe(pdf, t, w)
}
calcWrappedRowHeight := func(row []string, widths []float64, wrapIdx map[int]bool, lineH float64, minH float64) float64 {
maxLines := 1
for i, v := range row {
if !wrapIdx[i] {
continue
}
ln := len(wrappedLines(v, widths[i]-2))
if ln > maxLines {
maxLines = ln
}
}
h := float64(maxLines)*lineH + 1.2
if h < minH {
return minH
}
return h
}
drawWrapped := func(text string, x, y, w, rowH, lineH float64, align string) {
lines := wrappedLines(text, w-2)
total := float64(len(lines)) * lineH
startY := y + (rowH-total)/2
cy := startY
for _, ln := range lines {
pdf.SetXY(x+1, cy)
pdf.CellFormat(w-2, lineH, sanitizePDFText(string(ln)), "", 0, align, false, 0, "")
cy += lineH
}
}
drawSummaryHeader := func() {
pdf.SetFont("dejavu", "B", 7.5)
pdf.SetFillColor(149, 113, 22)
@@ -362,7 +316,12 @@ func drawCustomerBalancePDF(
pdf.SetFont("dejavu", "", 7.2)
pdf.SetTextColor(20, 20, 20)
drawSummaryRow := func(s balanceSummaryPDF) {
for _, s := range summaries {
if needPage(6.2) {
header()
drawSummaryHeader()
}
row := []string{
s.AnaCariKodu,
s.AnaCariAdi,
@@ -375,56 +334,31 @@ func drawCustomerBalancePDF(
formatMoneyPDF(s.TLBakiye12),
formatMoneyPDF(s.USDBakiye13),
formatMoneyPDF(s.TLBakiye13),
formatMoneyPDF(s.VadeGun),
formatMoneyPDF(s.VadeBelge),
}
wrapCols := map[int]bool{1: true, 3: true}
rowH := calcWrappedRowHeight(row, summaryW, wrapCols, 3.2, 6.2)
if needPage(rowH) {
header()
drawSummaryHeader()
pdf.SetFont("dejavu", "", 7.2)
pdf.SetTextColor(20, 20, 20)
}
y := pdf.GetY()
x := marginL
for i, v := range row {
if detailed {
pdf.SetFillColor(246, 241, 231)
pdf.Rect(x, y, summaryW[i], rowH, "FD")
} else {
pdf.Rect(x, y, summaryW[i], rowH, "")
}
pdf.Rect(x, y, summaryW[i], 6.2, "")
align := "L"
if i >= 7 {
align = "R"
}
if wrapCols[i] {
drawWrapped(v, x, y, summaryW[i], rowH, 3.2, "L")
} else {
pdf.SetXY(x+1, y+(rowH-4.2)/2)
pdf.CellFormat(summaryW[i]-2, 4.2, sanitizePDFText(v), "", 0, align, false, 0, "")
}
pdf.SetXY(x+1, y+1)
pdf.CellFormat(summaryW[i]-2, 4.2, v, "", 0, align, false, 0, "")
x += summaryW[i]
}
pdf.SetY(y + rowH)
pdf.SetY(y + 6.2)
}
if !detailed {
for _, s := range summaries {
drawSummaryRow(s)
}
return
}
pdf.Ln(1.8)
for _, s := range summaries {
drawSummaryRow(s)
pdf.Ln(1.2)
rows := detailsByMaster[s.AnaCariKodu]
if len(rows) == 0 {
pdf.Ln(1.0)
continue
}
@@ -446,25 +380,7 @@ func drawCustomerBalancePDF(
pdf.SetTextColor(40, 40, 40)
for _, r := range rows {
line := []string{
r.CariKodu,
r.CariDetay,
r.Sirket,
r.MuhasebeKodu,
r.CariDoviz,
formatMoneyPDF(r.Bakiye12),
formatMoneyPDF(r.Bakiye13),
formatMoneyPDF(r.USDBakiye12),
formatMoneyPDF(r.TLBakiye12),
formatMoneyPDF(r.USDBakiye13),
formatMoneyPDF(r.TLBakiye13),
formatMoneyPDF(r.VadeGun),
formatMoneyPDF(r.VadeBelgeGun),
}
detailWrapCols := map[int]bool{1: true}
rowH := calcWrappedRowHeight(line, detailW, detailWrapCols, 3.0, 5.8)
if needPage(rowH) {
if needPage(5.8) {
header()
pdf.SetFont("dejavu", "B", 8)
pdf.SetFillColor(218, 193, 151)
@@ -479,23 +395,33 @@ func drawCustomerBalancePDF(
pdf.SetTextColor(40, 40, 40)
}
line := []string{
r.CariKodu,
r.CariDetay,
r.Sirket,
r.MuhasebeKodu,
r.CariDoviz,
formatMoneyPDF(r.Bakiye12),
formatMoneyPDF(r.Bakiye13),
formatMoneyPDF(r.USDBakiye12),
formatMoneyPDF(r.TLBakiye12),
formatMoneyPDF(r.USDBakiye13),
formatMoneyPDF(r.TLBakiye13),
}
rowY := pdf.GetY()
rowX := marginL
for i, v := range line {
pdf.Rect(rowX, rowY, detailW[i], rowH, "")
pdf.Rect(rowX, rowY, detailW[i], 5.8, "")
align := "L"
if i >= 5 {
align = "R"
}
if detailWrapCols[i] {
drawWrapped(v, rowX, rowY, detailW[i], rowH, 3.0, "L")
} else {
pdf.SetXY(rowX+1, rowY+(rowH-4.0)/2)
pdf.CellFormat(detailW[i]-2, 4.0, sanitizePDFText(v), "", 0, align, false, 0, "")
}
pdf.SetXY(rowX+1, rowY+0.8)
pdf.CellFormat(detailW[i]-2, 4.0, v, "", 0, align, false, 0, "")
rowX += detailW[i]
}
pdf.SetY(rowY + rowH)
pdf.SetY(rowY + 5.8)
}
pdf.Ln(1.2)
}
@@ -548,24 +474,3 @@ func formatMoneyPDF(v float64) string {
return sign + strings.Join(out, ".") + "," + decPart
}
func absFloat(v float64) float64 {
if v < 0 {
return -v
}
return v
}
func sanitizePDFText(s string) string {
s = strings.ToValidUTF8(s, "?")
s = strings.ReplaceAll(s, "\x00", " ")
return strings.Map(func(r rune) rune {
if r == '\n' || r == '\r' || r == '\t' {
return ' '
}
if r < 32 {
return -1
}
return r
}, s)
}