Merge remote-tracking branch 'origin/master'
This commit is contained in:
461
svc/routes/customer_balance_pdf.go
Normal file
461
svc/routes/customer_balance_pdf.go
Normal file
@@ -0,0 +1,461 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"bssapp-backend/auth"
|
||||
"bssapp-backend/models"
|
||||
"bssapp-backend/queries"
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jung-kurt/gofpdf"
|
||||
)
|
||||
|
||||
type balanceSummaryPDF struct {
|
||||
AnaCariKodu string
|
||||
AnaCariAdi string
|
||||
Piyasa string
|
||||
Temsilci string
|
||||
RiskDurumu string
|
||||
|
||||
Bakiye12Map map[string]float64
|
||||
Bakiye13Map map[string]float64
|
||||
USDBakiye12 float64
|
||||
TLBakiye12 float64
|
||||
USDBakiye13 float64
|
||||
TLBakiye13 float64
|
||||
}
|
||||
|
||||
func ExportCustomerBalancePDFHandler(_ *sql.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
claims, ok := auth.GetClaimsFromContext(r.Context())
|
||||
if !ok || claims == nil {
|
||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
selectedDate := strings.TrimSpace(r.URL.Query().Get("selected_date"))
|
||||
if selectedDate == "" {
|
||||
selectedDate = time.Now().Format("2006-01-02")
|
||||
}
|
||||
|
||||
params := models.CustomerBalanceListParams{
|
||||
SelectedDate: selectedDate,
|
||||
CariSearch: strings.TrimSpace(r.URL.Query().Get("cari_search")),
|
||||
CariIlkGrup: strings.TrimSpace(r.URL.Query().Get("cari_ilk_grup")),
|
||||
Piyasa: strings.TrimSpace(r.URL.Query().Get("piyasa")),
|
||||
Temsilci: strings.TrimSpace(r.URL.Query().Get("temsilci")),
|
||||
RiskDurumu: strings.TrimSpace(r.URL.Query().Get("risk_durumu")),
|
||||
IslemTipi: strings.TrimSpace(r.URL.Query().Get("islem_tipi")),
|
||||
Ulke: strings.TrimSpace(r.URL.Query().Get("ulke")),
|
||||
Il: strings.TrimSpace(r.URL.Query().Get("il")),
|
||||
Ilce: strings.TrimSpace(r.URL.Query().Get("ilce")),
|
||||
}
|
||||
|
||||
detailed := parseBoolQuery(r.URL.Query().Get("detailed"))
|
||||
excludeZero12 := parseBoolQuery(r.URL.Query().Get("exclude_zero_12"))
|
||||
excludeZero13 := parseBoolQuery(r.URL.Query().Get("exclude_zero_13"))
|
||||
|
||||
rows, err := queries.GetCustomerBalanceList(r.Context(), params)
|
||||
if err != nil {
|
||||
http.Error(w, "db error: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
rows = filterCustomerBalanceRowsForPDF(rows, excludeZero12, excludeZero13)
|
||||
summaries, detailsByMaster := buildCustomerBalancePDFData(rows)
|
||||
|
||||
pdf := gofpdf.New("L", "mm", "A4", "")
|
||||
pdf.SetMargins(8, 8, 8)
|
||||
pdf.SetAutoPageBreak(false, 12)
|
||||
if err := registerDejavuFonts(pdf, "dejavu"); err != nil {
|
||||
http.Error(w, "pdf font error: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
drawCustomerBalancePDF(
|
||||
pdf,
|
||||
selectedDate,
|
||||
params.CariSearch,
|
||||
detailed,
|
||||
summaries,
|
||||
detailsByMaster,
|
||||
)
|
||||
|
||||
if err := pdf.Error(); err != nil {
|
||||
http.Error(w, "pdf render error: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := pdf.Output(&buf); err != nil {
|
||||
http.Error(w, "pdf output error: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
filename := "customer-balance-summary.pdf"
|
||||
if detailed {
|
||||
filename = "customer-balance-detailed.pdf"
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/pdf")
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=%q", filename))
|
||||
_, _ = w.Write(buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func parseBoolQuery(v string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(v)) {
|
||||
case "1", "true", "yes", "on":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func filterCustomerBalanceRowsForPDF(rows []models.CustomerBalanceListRow, excludeZero12, excludeZero13 bool) []models.CustomerBalanceListRow {
|
||||
out := make([]models.CustomerBalanceListRow, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
if excludeZero12 && row.Bakiye12 == 0 {
|
||||
continue
|
||||
}
|
||||
if excludeZero13 && row.Bakiye13 == 0 {
|
||||
continue
|
||||
}
|
||||
out = append(out, row)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func buildCustomerBalancePDFData(rows []models.CustomerBalanceListRow) ([]balanceSummaryPDF, map[string][]models.CustomerBalanceListRow) {
|
||||
summaryMap := make(map[string]*balanceSummaryPDF)
|
||||
detailsByMaster := make(map[string][]models.CustomerBalanceListRow)
|
||||
|
||||
for _, row := range rows {
|
||||
master := strings.TrimSpace(row.AnaCariKodu)
|
||||
if master == "" {
|
||||
master = strings.TrimSpace(row.CariKodu)
|
||||
}
|
||||
if master == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
s := summaryMap[master]
|
||||
if s == nil {
|
||||
s = &balanceSummaryPDF{
|
||||
AnaCariKodu: master,
|
||||
AnaCariAdi: strings.TrimSpace(row.AnaCariAdi),
|
||||
Piyasa: strings.TrimSpace(row.Piyasa),
|
||||
Temsilci: strings.TrimSpace(row.Temsilci),
|
||||
RiskDurumu: strings.TrimSpace(row.RiskDurumu),
|
||||
Bakiye12Map: map[string]float64{},
|
||||
Bakiye13Map: map[string]float64{},
|
||||
}
|
||||
summaryMap[master] = s
|
||||
}
|
||||
|
||||
if s.AnaCariAdi == "" && strings.TrimSpace(row.AnaCariAdi) != "" {
|
||||
s.AnaCariAdi = strings.TrimSpace(row.AnaCariAdi)
|
||||
}
|
||||
if s.Piyasa == "" && strings.TrimSpace(row.Piyasa) != "" {
|
||||
s.Piyasa = strings.TrimSpace(row.Piyasa)
|
||||
}
|
||||
if s.Temsilci == "" && strings.TrimSpace(row.Temsilci) != "" {
|
||||
s.Temsilci = strings.TrimSpace(row.Temsilci)
|
||||
}
|
||||
if s.RiskDurumu == "" && strings.TrimSpace(row.RiskDurumu) != "" {
|
||||
s.RiskDurumu = strings.TrimSpace(row.RiskDurumu)
|
||||
}
|
||||
|
||||
curr := strings.ToUpper(strings.TrimSpace(row.CariDoviz))
|
||||
if curr == "" {
|
||||
curr = "N/A"
|
||||
}
|
||||
s.Bakiye12Map[curr] += row.Bakiye12
|
||||
s.Bakiye13Map[curr] += row.Bakiye13
|
||||
s.USDBakiye12 += row.USDBakiye12
|
||||
s.TLBakiye12 += row.TLBakiye12
|
||||
s.USDBakiye13 += row.USDBakiye13
|
||||
s.TLBakiye13 += row.TLBakiye13
|
||||
|
||||
detailsByMaster[master] = append(detailsByMaster[master], row)
|
||||
}
|
||||
|
||||
masters := make([]string, 0, len(summaryMap))
|
||||
for m := range summaryMap {
|
||||
masters = append(masters, m)
|
||||
}
|
||||
sort.Strings(masters)
|
||||
|
||||
summaries := make([]balanceSummaryPDF, 0, len(masters))
|
||||
for _, m := range masters {
|
||||
summaries = append(summaries, *summaryMap[m])
|
||||
d := detailsByMaster[m]
|
||||
sort.SliceStable(d, func(i, j int) bool {
|
||||
if d[i].CariKodu == d[j].CariKodu {
|
||||
if d[i].CariDoviz == d[j].CariDoviz {
|
||||
si, _ := strconv.Atoi(d[i].Sirket)
|
||||
sj, _ := strconv.Atoi(d[j].Sirket)
|
||||
return si < sj
|
||||
}
|
||||
return d[i].CariDoviz < d[j].CariDoviz
|
||||
}
|
||||
return d[i].CariKodu < d[j].CariKodu
|
||||
})
|
||||
detailsByMaster[m] = d
|
||||
}
|
||||
|
||||
return summaries, detailsByMaster
|
||||
}
|
||||
|
||||
func drawCustomerBalancePDF(
|
||||
pdf *gofpdf.Fpdf,
|
||||
selectedDate string,
|
||||
searchText string,
|
||||
detailed bool,
|
||||
summaries []balanceSummaryPDF,
|
||||
detailsByMaster map[string][]models.CustomerBalanceListRow,
|
||||
) {
|
||||
pageW, _ := pdf.GetPageSize()
|
||||
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"}
|
||||
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"}
|
||||
detailW := normalizeWidths([]float64{26, 46, 10, 20, 10, 24, 24, 15, 15, 15, 15}, tableW)
|
||||
|
||||
header := func() {
|
||||
pdf.AddPage()
|
||||
pdf.SetFont("dejavu", "B", 15)
|
||||
pdf.SetTextColor(149, 113, 22)
|
||||
pdf.SetXY(marginL, marginT)
|
||||
pdf.CellFormat(120, 7, "Cari Bakiye Listesi", "", 0, "L", false, 0, "")
|
||||
|
||||
pdf.SetFont("dejavu", "", 9)
|
||||
pdf.SetTextColor(20, 20, 20)
|
||||
pdf.SetXY(pageW-marginR-80, marginT+1)
|
||||
pdf.CellFormat(80, 5, "Tarih: "+selectedDate, "", 0, "R", false, 0, "")
|
||||
|
||||
mode := "Detaysiz"
|
||||
if detailed {
|
||||
mode = "Detayli"
|
||||
}
|
||||
pdf.SetXY(pageW-marginR-80, marginT+6)
|
||||
pdf.CellFormat(80, 5, "Mod: "+mode, "", 0, "R", false, 0, "")
|
||||
|
||||
if strings.TrimSpace(searchText) != "" {
|
||||
pdf.SetXY(marginL, marginT+8)
|
||||
pdf.CellFormat(tableW, 5, "Arama: "+searchText, "", 0, "L", false, 0, "")
|
||||
}
|
||||
|
||||
pdf.SetDrawColor(149, 113, 22)
|
||||
pdf.Line(marginL, marginT+14, pageW-marginR, marginT+14)
|
||||
pdf.SetDrawColor(210, 210, 210)
|
||||
pdf.SetY(marginT + 17)
|
||||
}
|
||||
|
||||
needPage := func(needH float64) bool {
|
||||
return pdf.GetY()+needH+marginB > 210.0
|
||||
}
|
||||
|
||||
drawSummaryHeader := func() {
|
||||
pdf.SetFont("dejavu", "B", 7.5)
|
||||
pdf.SetFillColor(149, 113, 22)
|
||||
pdf.SetTextColor(255, 255, 255)
|
||||
y := pdf.GetY()
|
||||
x := marginL
|
||||
for i, c := range summaryCols {
|
||||
pdf.Rect(x, y, summaryW[i], 7, "DF")
|
||||
pdf.SetXY(x+1, y+1.2)
|
||||
pdf.CellFormat(summaryW[i]-2, 4.6, c, "", 0, "C", false, 0, "")
|
||||
x += summaryW[i]
|
||||
}
|
||||
pdf.SetY(y + 7)
|
||||
}
|
||||
|
||||
drawDetailHeader := func() {
|
||||
pdf.SetFont("dejavu", "B", 7.2)
|
||||
pdf.SetFillColor(149, 113, 22)
|
||||
pdf.SetTextColor(255, 255, 255)
|
||||
y := pdf.GetY()
|
||||
x := marginL
|
||||
for i, c := range detailCols {
|
||||
pdf.Rect(x, y, detailW[i], 6, "DF")
|
||||
pdf.SetXY(x+1, y+1)
|
||||
pdf.CellFormat(detailW[i]-2, 4, c, "", 0, "C", false, 0, "")
|
||||
x += detailW[i]
|
||||
}
|
||||
pdf.SetY(y + 6)
|
||||
}
|
||||
|
||||
header()
|
||||
drawSummaryHeader()
|
||||
|
||||
pdf.SetFont("dejavu", "", 7.2)
|
||||
pdf.SetTextColor(20, 20, 20)
|
||||
|
||||
for _, s := range summaries {
|
||||
if needPage(6.2) {
|
||||
header()
|
||||
drawSummaryHeader()
|
||||
}
|
||||
|
||||
row := []string{
|
||||
s.AnaCariKodu,
|
||||
s.AnaCariAdi,
|
||||
s.Piyasa,
|
||||
s.Temsilci,
|
||||
s.RiskDurumu,
|
||||
formatCurrencyMapPDF(s.Bakiye12Map),
|
||||
formatCurrencyMapPDF(s.Bakiye13Map),
|
||||
formatMoneyPDF(s.USDBakiye12),
|
||||
formatMoneyPDF(s.TLBakiye12),
|
||||
formatMoneyPDF(s.USDBakiye13),
|
||||
formatMoneyPDF(s.TLBakiye13),
|
||||
}
|
||||
|
||||
y := pdf.GetY()
|
||||
x := marginL
|
||||
for i, v := range row {
|
||||
pdf.Rect(x, y, summaryW[i], 6.2, "")
|
||||
align := "L"
|
||||
if i >= 7 {
|
||||
align = "R"
|
||||
}
|
||||
pdf.SetXY(x+1, y+1)
|
||||
pdf.CellFormat(summaryW[i]-2, 4.2, v, "", 0, align, false, 0, "")
|
||||
x += summaryW[i]
|
||||
}
|
||||
pdf.SetY(y + 6.2)
|
||||
}
|
||||
|
||||
if !detailed {
|
||||
return
|
||||
}
|
||||
|
||||
pdf.Ln(1.8)
|
||||
for _, s := range summaries {
|
||||
rows := detailsByMaster[s.AnaCariKodu]
|
||||
if len(rows) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if needPage(12.4) {
|
||||
header()
|
||||
}
|
||||
|
||||
pdf.SetFont("dejavu", "B", 8)
|
||||
pdf.SetFillColor(218, 193, 151)
|
||||
pdf.SetTextColor(20, 20, 20)
|
||||
y := pdf.GetY()
|
||||
pdf.Rect(marginL, y, tableW, 6.2, "DF")
|
||||
pdf.SetXY(marginL+1.5, y+1)
|
||||
pdf.CellFormat(tableW-3, 4.2, "Detay: "+s.AnaCariKodu, "", 0, "L", false, 0, "")
|
||||
pdf.SetY(y + 6.2)
|
||||
drawDetailHeader()
|
||||
|
||||
pdf.SetFont("dejavu", "", 7)
|
||||
pdf.SetTextColor(40, 40, 40)
|
||||
|
||||
for _, r := range rows {
|
||||
if needPage(5.8) {
|
||||
header()
|
||||
pdf.SetFont("dejavu", "B", 8)
|
||||
pdf.SetFillColor(218, 193, 151)
|
||||
pdf.SetTextColor(20, 20, 20)
|
||||
y := pdf.GetY()
|
||||
pdf.Rect(marginL, y, tableW, 6.2, "DF")
|
||||
pdf.SetXY(marginL+1.5, y+1)
|
||||
pdf.CellFormat(tableW-3, 4.2, "Detay: "+s.AnaCariKodu, "", 0, "L", false, 0, "")
|
||||
pdf.SetY(y + 6.2)
|
||||
drawDetailHeader()
|
||||
pdf.SetFont("dejavu", "", 7)
|
||||
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], 5.8, "")
|
||||
align := "L"
|
||||
if i >= 5 {
|
||||
align = "R"
|
||||
}
|
||||
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 + 5.8)
|
||||
}
|
||||
pdf.Ln(1.2)
|
||||
}
|
||||
}
|
||||
|
||||
func formatCurrencyMapPDF(m map[string]float64) string {
|
||||
if len(m) == 0 {
|
||||
return "-"
|
||||
}
|
||||
keys := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
parts := make([]string, 0, len(keys))
|
||||
for _, k := range keys {
|
||||
if m[k] == 0 {
|
||||
continue
|
||||
}
|
||||
parts = append(parts, k+": "+formatMoneyPDF(m[k]))
|
||||
}
|
||||
if len(parts) == 0 {
|
||||
return "-"
|
||||
}
|
||||
return strings.Join(parts, " | ")
|
||||
}
|
||||
|
||||
func formatMoneyPDF(v float64) string {
|
||||
s := fmt.Sprintf("%.2f", v)
|
||||
parts := strings.SplitN(s, ".", 2)
|
||||
intPart, decPart := parts[0], "00"
|
||||
if len(parts) == 2 {
|
||||
decPart = parts[1]
|
||||
}
|
||||
|
||||
sign := ""
|
||||
if strings.HasPrefix(intPart, "-") {
|
||||
sign = "-"
|
||||
intPart = strings.TrimPrefix(intPart, "-")
|
||||
}
|
||||
|
||||
var out []string
|
||||
for len(intPart) > 3 {
|
||||
out = append([]string{intPart[len(intPart)-3:]}, out...)
|
||||
intPart = intPart[:len(intPart)-3]
|
||||
}
|
||||
if intPart != "" {
|
||||
out = append([]string{intPart}, out...)
|
||||
}
|
||||
|
||||
return sign + strings.Join(out, ".") + "," + decPart
|
||||
}
|
||||
Reference in New Issue
Block a user