WIP cost calculation
This commit is contained in:
@@ -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}})
|
||||
|
||||
Reference in New Issue
Block a user