Compare commits
2 Commits
17a1ef0123
...
8ce01417e7
| Author | SHA1 | Date | |
|---|---|---|---|
|
8ce01417e7
|
|||
|
8e1df934b3
|
@@ -2,11 +2,13 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"git.dynamicdiscord.de/kalipso/zineshop/models"
|
"git.dynamicdiscord.de/kalipso/zineshop/models"
|
||||||
"git.dynamicdiscord.de/kalipso/zineshop/repositories"
|
"git.dynamicdiscord.de/kalipso/zineshop/repositories"
|
||||||
|
"git.dynamicdiscord.de/kalipso/zineshop/utils"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -63,7 +65,14 @@ func (rc *printController) PrintCartView(c *gin.Context) {
|
|||||||
return
|
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{
|
data := CreateSessionData(c, gin.H{
|
||||||
|
"paper": paper,
|
||||||
"cartItems": cartItems,
|
"cartItems": cartItems,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -78,9 +87,16 @@ func (rc *printController) PrintOrderView(c *gin.Context) {
|
|||||||
return
|
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
|
cartItems := order.CartItems
|
||||||
|
|
||||||
data := CreateSessionData(c, gin.H{
|
data := CreateSessionData(c, gin.H{
|
||||||
|
"paper": paper,
|
||||||
"cartItems": cartItems,
|
"cartItems": cartItems,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -90,6 +106,7 @@ func (rc *printController) PrintOrderView(c *gin.Context) {
|
|||||||
func (rc *printController) PrintHandler(c *gin.Context) {
|
func (rc *printController) PrintHandler(c *gin.Context) {
|
||||||
variantIds := c.PostFormArray("variant-id[]")
|
variantIds := c.PostFormArray("variant-id[]")
|
||||||
variantAmounts := c.PostFormArray("variant-amount[]")
|
variantAmounts := c.PostFormArray("variant-amount[]")
|
||||||
|
variantPapertypes := c.PostFormArray("variant-papertype[]")
|
||||||
variantCoverPages := c.PostFormArray("variant-coverpage[]")
|
variantCoverPages := c.PostFormArray("variant-coverpage[]")
|
||||||
|
|
||||||
if len(variantIds) != len(variantAmounts) || len(variantIds) != len(variantCoverPages) {
|
if len(variantIds) != len(variantAmounts) || len(variantIds) != len(variantCoverPages) {
|
||||||
@@ -113,9 +130,53 @@ func (rc *printController) PrintHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
coverPage := false
|
paperType, err := repositories.Papers.GetById(fmt.Sprintf("%v", variantPapertypes[idx]))
|
||||||
if variantCoverPages[idx] == "1" {
|
if err != nil {
|
||||||
coverPage = true
|
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])
|
variantAmount, err := strconv.Atoi(variantAmounts[idx])
|
||||||
@@ -124,7 +185,12 @@ func (rc *printController) PrintHandler(c *gin.Context) {
|
|||||||
return
|
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 {
|
if err != nil {
|
||||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"data": gin.H{"error": err}})
|
c.HTML(http.StatusBadRequest, "error.html", gin.H{"data": gin.H{"error": err}})
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateSessionId(length int) string {
|
func GenerateSessionId(length int) string {
|
||||||
@@ -17,3 +20,41 @@ func GenerateSessionId(length int) string {
|
|||||||
func GenerateToken() string {
|
func GenerateToken() string {
|
||||||
return GenerateSessionId(16)
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -38,6 +38,10 @@
|
|||||||
hover:text-white">Print</a>
|
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
|
<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>
|
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>
|
<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>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|||||||
@@ -37,10 +37,20 @@
|
|||||||
</div>
|
</div>
|
||||||
</td>
|
</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">
|
<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">
|
<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 selected value="0">None</option>
|
||||||
<option value="1">CoverPage</option>
|
{{ range $.data.paper }}
|
||||||
|
<option value="{{ .ID }}">{{ .Brand }} - {{ .Name }}: {{ .Size }} {{ .Weight }}g/m2</option>
|
||||||
|
{{ end}}
|
||||||
</select>
|
</select>
|
||||||
</td>
|
</td>
|
||||||
<td class="whitespace-nowrap py-4">
|
<td class="whitespace-nowrap py-4">
|
||||||
|
|||||||
Reference in New Issue
Block a user