Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -89,6 +89,18 @@ func GetProductionProductCostingOnMLPDFHandler(w http.ResponseWriter, r *http.Re
|
||||
return
|
||||
}
|
||||
|
||||
// Exchange rates (needed for EUR conversions in PDF summary tables)
|
||||
usdRate := 0.0
|
||||
eurRate := 0.0
|
||||
if mssqlDB := db.GetDB(); mssqlDB != nil {
|
||||
row, err := queries.GetProductionHasCostDetailExchangeRatesByDate(ctx, mssqlDB, header.MaliyetTarihi)
|
||||
if err == nil {
|
||||
var rateDate string
|
||||
var gbpRate float64
|
||||
_ = row.Scan(&rateDate, &usdRate, &eurRate, &gbpRate)
|
||||
}
|
||||
}
|
||||
|
||||
pdf := gofpdf.New("L", "mm", "A4", "")
|
||||
pdf.SetMargins(8, 8, 8)
|
||||
pdf.SetAutoPageBreak(false, 10)
|
||||
@@ -98,9 +110,11 @@ func GetProductionProductCostingOnMLPDFHandler(w http.ResponseWriter, r *http.Re
|
||||
}
|
||||
|
||||
export := &costingPDF{
|
||||
pdf: pdf,
|
||||
header: header,
|
||||
groups: groups,
|
||||
pdf: pdf,
|
||||
header: header,
|
||||
groups: groups,
|
||||
usdRate: usdRate,
|
||||
eurRate: eurRate,
|
||||
}
|
||||
export.draw()
|
||||
|
||||
@@ -224,6 +238,9 @@ type costingPDF struct {
|
||||
pdf *gofpdf.Fpdf
|
||||
header models.ProductionHasCostDetailHeader
|
||||
groups []models.ProductionHasCostDetailGroup
|
||||
|
||||
usdRate float64
|
||||
eurRate float64
|
||||
}
|
||||
|
||||
func (c *costingPDF) draw() {
|
||||
@@ -312,13 +329,14 @@ type pdfGroupTotalRow struct {
|
||||
group string
|
||||
try float64
|
||||
usd float64
|
||||
eur float64
|
||||
}
|
||||
|
||||
func (c *costingPDF) drawHeaderSummaryTables() {
|
||||
pdf := c.pdf
|
||||
|
||||
partRows := c.computePartSummary()
|
||||
groupRows, grandTRY, grandUSD := c.computeGroupTotals()
|
||||
groupRows, grandTRY, grandUSD, grandEUR := c.computeGroupTotals()
|
||||
|
||||
// Table styling (use same brand palette as statements PDF)
|
||||
// colorPrimary/colorSecondary/colorDetailFill are in statements_pdf.go (same package).
|
||||
@@ -332,28 +350,36 @@ func (c *costingPDF) drawHeaderSummaryTables() {
|
||||
pdf.CellFormat(0, 4.8, "Parca Bazli Maliyet Ozellikleri", "", 1, "L", false, 0, "")
|
||||
partCols := []string{"Parca", "TRY", "USD", "EUR"}
|
||||
partW := []float64{70, 22, 22, 22}
|
||||
// Add TOTAL row
|
||||
totalTry, totalUsd, totalEur := 0.0, 0.0, 0.0
|
||||
for _, r := range partRows {
|
||||
totalTry += r.try
|
||||
totalUsd += r.usd
|
||||
totalEur += r.eur
|
||||
}
|
||||
partRowsWithTotal := append(partRows, pdfPartSummaryRow{name: "TOPLAM", try: totalTry, usd: totalUsd, eur: totalEur})
|
||||
c.drawMiniTable(partCols, partW, func(i int) []string {
|
||||
if i >= len(partRows) {
|
||||
if i >= len(partRowsWithTotal) {
|
||||
return nil
|
||||
}
|
||||
r := partRows[i]
|
||||
r := partRowsWithTotal[i]
|
||||
return []string{r.name, pdfMoney(r.try), pdfMoney(r.usd), pdfMoney(r.eur)}
|
||||
}, len(partRows), true)
|
||||
}, len(partRowsWithTotal), true, true)
|
||||
pdf.Ln(2)
|
||||
|
||||
// Group totals table
|
||||
pdf.SetFont("dejavu", "B", 8.2)
|
||||
pdf.CellFormat(0, 4.8, "Grup Toplamlari", "", 1, "L", false, 0, "")
|
||||
gCols := []string{"Grup", "TRY", "USD"}
|
||||
gW := []float64{30, 22, 22}
|
||||
totalRows := append(groupRows, pdfGroupTotalRow{group: "TOPLAM", try: grandTRY, usd: grandUSD})
|
||||
gCols := []string{"Grup", "TRY", "USD", "EUR"}
|
||||
gW := []float64{30, 22, 22, 22}
|
||||
totalRows := append(groupRows, pdfGroupTotalRow{group: "TOPLAM", try: grandTRY, usd: grandUSD, eur: grandEUR})
|
||||
c.drawMiniTable(gCols, gW, func(i int) []string {
|
||||
if i >= len(totalRows) {
|
||||
return nil
|
||||
}
|
||||
r := totalRows[i]
|
||||
return []string{r.group, pdfMoney(r.try), pdfMoney(r.usd)}
|
||||
}, len(totalRows), true)
|
||||
return []string{r.group, pdfMoney(r.try), pdfMoney(r.usd), pdfMoney(r.eur)}
|
||||
}, len(totalRows), true, true)
|
||||
pdf.Ln(2)
|
||||
}
|
||||
|
||||
@@ -372,7 +398,10 @@ func (c *costingPDF) computePartSummary() []pdfPartSummaryRow {
|
||||
}
|
||||
row.try += it.LTutar
|
||||
row.usd += it.USDTutar
|
||||
row.eur += it.EURTutar
|
||||
// EUR isn't directly returned by the has-cost query; derive from USD using exchange rates when available.
|
||||
if c.usdRate > 0 && c.eurRate > 0 {
|
||||
row.eur += it.USDTutar * (c.usdRate / c.eurRate)
|
||||
}
|
||||
}
|
||||
}
|
||||
out := make([]pdfPartSummaryRow, 0, len(byName))
|
||||
@@ -384,7 +413,7 @@ func (c *costingPDF) computePartSummary() []pdfPartSummaryRow {
|
||||
return out
|
||||
}
|
||||
|
||||
func (c *costingPDF) computeGroupTotals() (rows []pdfGroupTotalRow, grandTRY float64, grandUSD float64) {
|
||||
func (c *costingPDF) computeGroupTotals() (rows []pdfGroupTotalRow, grandTRY float64, grandUSD float64, grandEUR float64) {
|
||||
want := []string{"CM2", "FABRIC", "DT", "TP"}
|
||||
by := map[string]*pdfGroupTotalRow{}
|
||||
for _, w := range want {
|
||||
@@ -400,6 +429,9 @@ func (c *costingPDF) computeGroupTotals() (rows []pdfGroupTotalRow, grandTRY flo
|
||||
// g.TotalTutar is TRY total; g.TotalUSDTutar is USD total
|
||||
t.try += g.TotalTutar
|
||||
t.usd += g.TotalUSDTutar
|
||||
if c.usdRate > 0 && c.eurRate > 0 {
|
||||
t.eur += g.TotalUSDTutar * (c.usdRate / c.eurRate)
|
||||
}
|
||||
}
|
||||
|
||||
for _, w := range want {
|
||||
@@ -407,11 +439,12 @@ func (c *costingPDF) computeGroupTotals() (rows []pdfGroupTotalRow, grandTRY flo
|
||||
rows = append(rows, *r)
|
||||
grandTRY += r.try
|
||||
grandUSD += r.usd
|
||||
grandEUR += r.eur
|
||||
}
|
||||
return rows, grandTRY, grandUSD
|
||||
return rows, grandTRY, grandUSD, grandEUR
|
||||
}
|
||||
|
||||
func (c *costingPDF) drawMiniTable(cols []string, widths []float64, rowFn func(i int) []string, rowCount int, zebra bool) {
|
||||
func (c *costingPDF) drawMiniTable(cols []string, widths []float64, rowFn func(i int) []string, rowCount int, zebra bool, emphasizeLastRow bool) {
|
||||
pdf := c.pdf
|
||||
|
||||
// Header row
|
||||
@@ -433,25 +466,41 @@ func (c *costingPDF) drawMiniTable(cols []string, widths []float64, rowFn func(i
|
||||
pdf.SetXY(x0, y0+hH)
|
||||
|
||||
// Data rows
|
||||
pdf.SetFont("dejavu", "", 7.4)
|
||||
pdf.SetDrawColor(220, 220, 220)
|
||||
for i := 0; i < rowCount; i++ {
|
||||
row := rowFn(i)
|
||||
if row == nil {
|
||||
break
|
||||
}
|
||||
isLast := emphasizeLastRow && (i == rowCount-1)
|
||||
fill := zebra && (i%2 == 1)
|
||||
if fill {
|
||||
pdf.SetFillColor(colorDetailFill[0], colorDetailFill[1], colorDetailFill[2])
|
||||
|
||||
// Style rules:
|
||||
// - Total row: primary fill + bigger bold text
|
||||
// - Zebra rows: detail fill
|
||||
// - Normal: white
|
||||
if isLast {
|
||||
pdf.SetFont("dejavu", "B", 8.6)
|
||||
pdf.SetTextColor(255, 255, 255)
|
||||
pdf.SetFillColor(colorPrimary[0], colorPrimary[1], colorPrimary[2])
|
||||
} else {
|
||||
pdf.SetFillColor(255, 255, 255)
|
||||
pdf.SetFont("dejavu", "", 7.4)
|
||||
pdf.SetTextColor(30, 30, 30)
|
||||
if fill {
|
||||
pdf.SetFillColor(colorDetailFill[0], colorDetailFill[1], colorDetailFill[2])
|
||||
} else {
|
||||
pdf.SetFillColor(255, 255, 255)
|
||||
}
|
||||
}
|
||||
x = x0
|
||||
y := pdf.GetY()
|
||||
rh := 5.0
|
||||
if isLast {
|
||||
rh = 5.6
|
||||
}
|
||||
for cidx, val := range row {
|
||||
style := ""
|
||||
if fill {
|
||||
if fill || isLast {
|
||||
style = "DF"
|
||||
}
|
||||
pdf.Rect(x, y, widths[cidx], rh, style)
|
||||
@@ -465,6 +514,7 @@ func (c *costingPDF) drawMiniTable(cols []string, widths []float64, rowFn func(i
|
||||
}
|
||||
pdf.SetXY(x0, y+rh)
|
||||
}
|
||||
pdf.SetTextColor(0, 0, 0)
|
||||
}
|
||||
|
||||
func formatDateTRDot(s string) string {
|
||||
@@ -503,12 +553,14 @@ func (c *costingPDF) drawGroup(g models.ProductionHasCostDetailGroup, firstGroup
|
||||
"Renk",
|
||||
"Miktar",
|
||||
"Br",
|
||||
"Br\nFiyat",
|
||||
"Pr\nBr",
|
||||
"USD\nFiyat",
|
||||
"USD\nTutar",
|
||||
"TRY\nFiyat",
|
||||
"TRY\nTutar",
|
||||
}
|
||||
wn := []float64{8, 20, 22, 32, 70, 14, 14, 10, 16, 16, 16, 16} // sum = 250
|
||||
wn := []float64{8, 20, 22, 30, 56, 14, 14, 10, 16, 12, 16, 16, 16, 16} // sum = 252
|
||||
|
||||
c.drawTableHeader(cols, wn)
|
||||
// PDF-specific ordering: by hammadde turu no, then code.
|
||||
@@ -648,25 +700,39 @@ func (c *costingPDF) drawRowWithGroup(it models.ProductionHasCostDetailGroupItem
|
||||
c.drawCell(x, y0, wn[7], rowH, strings.TrimSpace(it.SBirim), "C", fill)
|
||||
x += wn[7]
|
||||
|
||||
// Prefer input price if present; otherwise lFiyat.
|
||||
price := it.LFiyat
|
||||
cur := strings.TrimSpace(it.SDovizCinsi)
|
||||
if it.FiyatGirilen != nil && *it.FiyatGirilen > 0 {
|
||||
price = *it.FiyatGirilen
|
||||
if strings.TrimSpace(it.FiyatDoviz) != "" {
|
||||
cur = strings.TrimSpace(it.FiyatDoviz)
|
||||
}
|
||||
}
|
||||
c.drawCell(x, y0, wn[8], rowH, pdfMoney(price), "R", fill)
|
||||
x += wn[8]
|
||||
c.drawCell(x, y0, wn[9], rowH, cur, "C", fill)
|
||||
x += wn[9]
|
||||
|
||||
// Always show USD/TRY unit+total.
|
||||
// In URETIM schema: lFiyat/lTutar are in TRY, lDovizFiyati/usdTutar are in USD.
|
||||
c.drawCell(x, y0, wn[8], rowH, pdfMoney(it.LDovizFiyati), "R", fill)
|
||||
x += wn[8]
|
||||
c.drawCell(x, y0, wn[10], rowH, pdfMoney(it.LDovizFiyati), "R", fill)
|
||||
x += wn[10]
|
||||
usdTotal := it.USDTutar
|
||||
if usdTotal == 0 && it.LMiktar != 0 && it.LDovizFiyati != 0 {
|
||||
usdTotal = it.LMiktar * it.LDovizFiyati
|
||||
}
|
||||
c.drawCell(x, y0, wn[9], rowH, pdfMoney(usdTotal), "R", fill)
|
||||
x += wn[9]
|
||||
c.drawCell(x, y0, wn[11], rowH, pdfMoney(usdTotal), "R", fill)
|
||||
x += wn[11]
|
||||
|
||||
// Prefer input price if present; otherwise lFiyat.
|
||||
unitTRY := it.LFiyat
|
||||
if it.FiyatGirilen != nil && *it.FiyatGirilen > 0 && strings.EqualFold(strings.TrimSpace(it.FiyatDoviz), "TRY") {
|
||||
unitTRY = *it.FiyatGirilen
|
||||
}
|
||||
c.drawCell(x, y0, wn[10], rowH, pdfMoney(unitTRY), "R", fill)
|
||||
x += wn[10]
|
||||
c.drawCell(x, y0, wn[11], rowH, pdfMoney(it.LTutar), "R", fill)
|
||||
c.drawCell(x, y0, wn[12], rowH, pdfMoney(unitTRY), "R", fill)
|
||||
x += wn[12]
|
||||
c.drawCell(x, y0, wn[13], rowH, pdfMoney(it.LTutar), "R", fill)
|
||||
|
||||
pdf.SetXY(x0, y0+rowH)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user