2 Commits

Author SHA1 Message Date
8ce01417e7 select papertype on print
All checks were successful
Go / build (push) Successful in 12m15s
2025-06-29 16:58:07 +02:00
8e1df934b3 WIP cost calculation 2025-06-29 16:57:51 +02:00
4 changed files with 127 additions and 6 deletions

View File

@@ -2,11 +2,13 @@ package controllers
import (
"fmt"
"math"
"net/http"
"strconv"
"git.dynamicdiscord.de/kalipso/zineshop/models"
"git.dynamicdiscord.de/kalipso/zineshop/repositories"
"git.dynamicdiscord.de/kalipso/zineshop/utils"
"github.com/gin-gonic/gin"
)
@@ -63,7 +65,14 @@ func (rc *printController) PrintCartView(c *gin.Context) {
return
}
paper, err := repositories.Papers.GetAll()
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{"data": gin.H{"error": err}})
return
}
data := CreateSessionData(c, gin.H{
"paper": paper,
"cartItems": cartItems,
})
@@ -78,9 +87,16 @@ func (rc *printController) PrintOrderView(c *gin.Context) {
return
}
paper, err := repositories.Papers.GetAll()
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{"data": gin.H{"error": err}})
return
}
cartItems := order.CartItems
data := CreateSessionData(c, gin.H{
"paper": paper,
"cartItems": cartItems,
})
@@ -90,6 +106,7 @@ func (rc *printController) PrintOrderView(c *gin.Context) {
func (rc *printController) PrintHandler(c *gin.Context) {
variantIds := c.PostFormArray("variant-id[]")
variantAmounts := c.PostFormArray("variant-amount[]")
variantPapertypes := c.PostFormArray("variant-papertype[]")
variantCoverPages := c.PostFormArray("variant-coverpage[]")
if len(variantIds) != len(variantAmounts) || len(variantIds) != len(variantCoverPages) {
@@ -113,9 +130,53 @@ func (rc *printController) PrintHandler(c *gin.Context) {
return
}
coverPage := false
if variantCoverPages[idx] == "1" {
coverPage = true
paperType, err := repositories.Papers.GetById(fmt.Sprintf("%v", variantPapertypes[idx]))
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{"data": gin.H{"error": err}})
return
}
var coverPage models.Paper
doPrintCoverpage := false
if variantCoverPages[idx] != "0" {
coverPage, err = repositories.Papers.GetById(fmt.Sprintf("%v", variantCoverPages[idx]))
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{"data": gin.H{"error": err}})
return
}
doPrintCoverpage = true
}
calculatePrintCosts := func(shopItem models.ShopItem, paperType models.Paper, coverPagePaperType models.Paper, colored bool, amount int) (float64, error) {
pageCount := utils.CountPagesAtPath(shopItem.Pdf)
printMode := models.GetPrintMode(shopItem.PrintMode)
//Get actual pagecount depending on printmode
actualPageCount := pageCount
if printMode == models.CreateBooklet {
dividedCount := float64(pageCount) / 4.0
actualPageCount = int(math.Ceil(dividedCount))
}
if printMode == models.LongEdge || printMode == models.ShortEdge {
dividedCount := float64(pageCount) / 2.0
actualPageCount = int(math.Ceil(dividedCount))
}
PPC := 0.002604
partCost := 0.0067
if colored {
partCost = 0.0478
}
printingCosts := float64(actualPageCount) * paperType.Price
printingCosts += float64(actualPageCount/2) * PPC
printingCosts += partCost * float64(actualPageCount)
fmt.Printf("Printing Costs per Zine: %v\n", printingCosts)
fmt.Printf("Printing Costs Total: %v\n", printingCosts*float64(amount))
return printingCosts, nil
}
variantAmount, err := strconv.Atoi(variantAmounts[idx])
@@ -124,7 +185,12 @@ func (rc *printController) PrintHandler(c *gin.Context) {
return
}
printJob, err := models.NewPrintJob(shopItem, variant, coverPage, uint(variantAmount))
fmt.Println("Printing Costs:")
colored := variant.Name == "Colored"
calculatePrintCosts(shopItem, paperType, coverPage, colored, int(variantAmount))
printJob, err := models.NewPrintJob(shopItem, variant, doPrintCoverpage, uint(variantAmount))
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{"data": gin.H{"error": err}})

View File

@@ -1,8 +1,11 @@
package utils
import (
"bufio"
"crypto/rand"
"encoding/hex"
"io"
"os"
)
func GenerateSessionId(length int) string {
@@ -17,3 +20,41 @@ func GenerateSessionId(length int) string {
func GenerateToken() string {
return GenerateSessionId(16)
}
const match = "/Page\x00"
// Pages reads the given io.ByteReader until EOF is reached, returning the
// number of pages encountered.
func Pages(reader io.ByteReader) (pages int) {
i := 0
for {
b, err := reader.ReadByte()
if err != nil {
return
}
check:
switch match[i] {
case 0:
if !(b >= 'A' && b <= 'Z' || b >= 'a' && b <= 'z') {
pages++
}
i = 0
goto check
case b:
i++
default:
i = 0
}
}
}
// PagesAtPath opens a PDF file at the given file path, returning the number
// of pages found.
func CountPagesAtPath(path string) (pages int) {
if reader, err := os.Open(path); err == nil {
reader.Chdir()
pages = Pages(bufio.NewReader(reader))
reader.Close()
}
return
}

View File

@@ -38,6 +38,10 @@
hover:text-white">Print</a>
<a href="/cart" class="rounded-md bg-gray-900 m-2 px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700
hover:text-white">Cart</a>
<a href="/config" class="rounded-md bg-gray-900 m-2 px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700
hover:text-white">Config</a>
<a href="/paper" class="rounded-md bg-gray-900 m-2 px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700
hover:text-white">Paper</a>
<a href="/logout" class="rounded-md bg-gray-900 m-2 px-3 py-2 text-sm font-medium text-red-300 hover:bg-gray-700 hover:text-white">Logout</a>
</div>
{{ end }}

View File

@@ -37,10 +37,20 @@
</div>
</td>
<td class="whitespace-nowrap py-4">
<select name="variant-papertype[]" required class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
{{ range $.data.paper }}
<option value="{{ .ID }}">{{ .Brand }} - {{ .Name }}: {{ .Size }} {{ .Weight }}g/m2</option>
{{ end}}
</select>
</td>
<td class="whitespace-nowrap py-4">
<select name="variant-coverpage[]" required class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
<option selected value="0">Normal</option>
<option value="1">CoverPage</option>
<option selected value="0">None</option>
{{ range $.data.paper }}
<option value="{{ .ID }}">{{ .Brand }} - {{ .Name }}: {{ .Size }} {{ .Weight }}g/m2</option>
{{ end}}
</select>
</td>
<td class="whitespace-nowrap py-4">