update PrintJob model and add Invoice
This commit is contained in:
@@ -2,13 +2,11 @@ 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"
|
||||
)
|
||||
|
||||
@@ -136,47 +134,15 @@ func (rc *printController) PrintHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var coverPage models.Paper
|
||||
doPrintCoverpage := false
|
||||
var coverPage *models.Paper
|
||||
if variantCoverPages[idx] != "0" {
|
||||
coverPage, err = repositories.Papers.GetById(fmt.Sprintf("%v", variantCoverPages[idx]))
|
||||
coverPageTmp, 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
|
||||
coverPage = &coverPageTmp
|
||||
}
|
||||
|
||||
variantAmount, err := strconv.Atoi(variantAmounts[idx])
|
||||
@@ -187,15 +153,13 @@ func (rc *printController) PrintHandler(c *gin.Context) {
|
||||
|
||||
fmt.Println("Printing Costs:")
|
||||
|
||||
colored := variant.Name == "Colored"
|
||||
calculatePrintCosts(shopItem, paperType, coverPage, colored, int(variantAmount))
|
||||
|
||||
printJob, err := models.NewPrintJob(shopItem, variant, doPrintCoverpage, uint(variantAmount))
|
||||
printJob, err := models.NewPrintJob(shopItem, variant, paperType, coverPage, uint(variantAmount))
|
||||
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"data": gin.H{"error": err}})
|
||||
return
|
||||
}
|
||||
printJob.CalculatePrintCosts()
|
||||
|
||||
printJobs = append(printJobs, printJob)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.dynamicdiscord.de/kalipso/zineshop/utils"
|
||||
"gorm.io/gorm"
|
||||
"math"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
@@ -18,12 +21,33 @@ const (
|
||||
TriFold PrintOption = "-o Fold=TriFold -o Binding=TopBinding"
|
||||
)
|
||||
|
||||
type PrintJob struct {
|
||||
type OldPrintJob struct {
|
||||
Pdf string
|
||||
Amount uint
|
||||
Options []PrintOption
|
||||
}
|
||||
|
||||
type Invoice struct {
|
||||
gorm.Model
|
||||
PrintJobs []PrintJob
|
||||
PricePerClick float64
|
||||
PartCosts float64
|
||||
}
|
||||
|
||||
type PrintJob struct {
|
||||
gorm.Model
|
||||
ShopItemID uint
|
||||
ShopItem ShopItem
|
||||
VariantID uint
|
||||
Variant ItemVariant
|
||||
PaperTypeId uint
|
||||
PaperType Paper `gorm:"foreignKey:PaperTypeId"`
|
||||
CoverPaperTypeId *uint
|
||||
CoverPaperType *Paper `gorm:"foreignKey:CoverPaperTypeId"`
|
||||
Amount uint
|
||||
InvoiceID uint
|
||||
}
|
||||
|
||||
func GetPrintMode(mode string) PrintOption {
|
||||
if mode == "LongEdge" {
|
||||
return LongEdge
|
||||
@@ -40,7 +64,7 @@ func GetPrintMode(mode string) PrintOption {
|
||||
return CreateBooklet
|
||||
}
|
||||
|
||||
func NewPrintJob(shopItem ShopItem, variant ItemVariant, coverPage bool, amount uint) (PrintJob, error) {
|
||||
func NewPrintJob(shopItem ShopItem, variant ItemVariant, paperType Paper, coverPaperType *Paper, amount uint) (PrintJob, error) {
|
||||
if shopItem.Pdf == "" {
|
||||
return PrintJob{}, fmt.Errorf("ShopItem has no PDF assigned")
|
||||
}
|
||||
@@ -49,32 +73,44 @@ func NewPrintJob(shopItem ShopItem, variant ItemVariant, coverPage bool, amount
|
||||
return PrintJob{}, fmt.Errorf("Amount to big. This is denied for security reasons")
|
||||
}
|
||||
|
||||
var result PrintJob
|
||||
result.Pdf = shopItem.Pdf
|
||||
result.Amount = amount
|
||||
|
||||
if variant.Name == "Colored" {
|
||||
result.Options = append(result.Options, Colored)
|
||||
result := PrintJob{
|
||||
ShopItem: shopItem,
|
||||
Variant: variant,
|
||||
PaperType: paperType,
|
||||
CoverPaperType: coverPaperType,
|
||||
Amount: amount,
|
||||
}
|
||||
|
||||
if coverPage {
|
||||
result.Options = append(result.Options, CoverPage)
|
||||
}
|
||||
|
||||
result.Options = append(result.Options, GetPrintMode(shopItem.PrintMode))
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (p *PrintJob) IsColored() bool {
|
||||
return p.Variant.Name == "Colored"
|
||||
}
|
||||
|
||||
func (p *PrintJob) GeneratePrintOptions() []PrintOption {
|
||||
var result []PrintOption
|
||||
if p.Variant.Name == "Colored" {
|
||||
result = append(result, Colored)
|
||||
}
|
||||
|
||||
if p.CoverPaperType != nil {
|
||||
result = append(result, CoverPage)
|
||||
}
|
||||
|
||||
result = append(result, GetPrintMode(p.ShopItem.PrintMode))
|
||||
return result
|
||||
}
|
||||
|
||||
func (p *PrintJob) Execute() error {
|
||||
baseCommand := "lp -d KonicaBooklet"
|
||||
baseCommand += fmt.Sprintf(" -n %v ", p.Amount)
|
||||
|
||||
for _, option := range p.Options {
|
||||
for _, option := range p.GeneratePrintOptions() {
|
||||
baseCommand += fmt.Sprintf(" %v ", option)
|
||||
}
|
||||
|
||||
baseCommand += fmt.Sprintf(" -- %s", p.Pdf)
|
||||
baseCommand += fmt.Sprintf(" -- %s", p.ShopItem.Pdf)
|
||||
|
||||
parts := strings.Fields(baseCommand)
|
||||
|
||||
@@ -91,3 +127,34 @@ func (p *PrintJob) Execute() error {
|
||||
fmt.Printf("Output:\n%s\n", output)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *PrintJob) CalculatePrintCosts() (float64, error) {
|
||||
pageCount := utils.CountPagesAtPath(p.ShopItem.Pdf)
|
||||
printMode := GetPrintMode(p.ShopItem.PrintMode)
|
||||
|
||||
//Get actual pagecount depending on printmode
|
||||
actualPageCount := pageCount
|
||||
|
||||
if printMode == CreateBooklet {
|
||||
dividedCount := float64(pageCount) / 4.0
|
||||
actualPageCount = int(math.Ceil(dividedCount))
|
||||
}
|
||||
|
||||
if printMode == LongEdge || printMode == ShortEdge {
|
||||
dividedCount := float64(pageCount) / 2.0
|
||||
actualPageCount = int(math.Ceil(dividedCount))
|
||||
}
|
||||
|
||||
PPC := 0.002604
|
||||
partCost := 0.0067
|
||||
if p.IsColored() {
|
||||
partCost = 0.0478
|
||||
}
|
||||
printingCosts := float64(actualPageCount) * p.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(p.Amount))
|
||||
return printingCosts, nil
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ type ItemVariant struct {
|
||||
type ShopItem struct {
|
||||
gorm.Model
|
||||
Name string `json:"name" binding:"required" gorm:"unique;not null"`
|
||||
Abstract string `json:"Abstract" binding:"required"`
|
||||
Abstract string `json:"abstract" binding:"required"`
|
||||
Description string `json:"description" binding:"required"`
|
||||
Category Category `json:"category"`
|
||||
Variants []ItemVariant `json:"variant"`
|
||||
|
||||
82
repositories/printJobRepository.go
Normal file
82
repositories/printJobRepository.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"git.dynamicdiscord.de/kalipso/zineshop/models"
|
||||
)
|
||||
|
||||
type PrintJobRepository interface {
|
||||
Create(models.PrintJob) (models.PrintJob, error)
|
||||
GetAll() ([]models.PrintJob, error)
|
||||
GetById(string) (models.PrintJob, error)
|
||||
//GetByShopItemId(string) (models.PrintJob, error)
|
||||
Update(models.PrintJob) (models.PrintJob, error)
|
||||
DeleteById(string) error
|
||||
}
|
||||
|
||||
type GORMPrintJobRepository struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func NewGORMPrintJobRepository(db *gorm.DB) PrintJobRepository {
|
||||
return &GORMPrintJobRepository{
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *GORMPrintJobRepository) Create(tag models.PrintJob) (models.PrintJob, error) {
|
||||
result := t.DB.Create(&tag)
|
||||
|
||||
if result.Error != nil {
|
||||
return models.PrintJob{}, result.Error
|
||||
}
|
||||
|
||||
return tag, nil
|
||||
}
|
||||
|
||||
func (t *GORMPrintJobRepository) GetAll() ([]models.PrintJob, error) {
|
||||
var tags []models.PrintJob
|
||||
result := t.DB.Find(&tags)
|
||||
|
||||
return tags, result.Error
|
||||
}
|
||||
|
||||
func (t *GORMPrintJobRepository) GetById(id string) (models.PrintJob, error) {
|
||||
tagId, err := strconv.Atoi(id)
|
||||
|
||||
if err != nil {
|
||||
return models.PrintJob{}, err
|
||||
}
|
||||
|
||||
var tag models.PrintJob
|
||||
result := t.DB.First(&tag, uint(tagId))
|
||||
|
||||
if result.Error != nil {
|
||||
return models.PrintJob{}, result.Error
|
||||
}
|
||||
|
||||
return tag, nil
|
||||
}
|
||||
|
||||
func (t *GORMPrintJobRepository) Update(tag models.PrintJob) (models.PrintJob, error) {
|
||||
result := t.DB.Save(&tag)
|
||||
if result.Error != nil {
|
||||
return models.PrintJob{}, result.Error
|
||||
}
|
||||
|
||||
return tag, nil
|
||||
}
|
||||
|
||||
func (t *GORMPrintJobRepository) DeleteById(id string) error {
|
||||
tagId, err := strconv.Atoi(id)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result := t.DB.Delete(&models.PrintJob{}, tagId)
|
||||
return result.Error
|
||||
}
|
||||
@@ -17,6 +17,7 @@ var (
|
||||
Tokens RegisterTokenRepository
|
||||
ConfigOptions ConfigRepository
|
||||
Papers PaperRepository
|
||||
PrintJobs PrintJobRepository
|
||||
)
|
||||
|
||||
func InitRepositories() {
|
||||
@@ -33,6 +34,8 @@ func InitRepositories() {
|
||||
&models.Order{},
|
||||
&models.Config{},
|
||||
&models.Paper{},
|
||||
&models.PrintJob{},
|
||||
&models.Invoice{},
|
||||
&models.RegisterToken{})
|
||||
|
||||
if err != nil {
|
||||
@@ -47,4 +50,5 @@ func InitRepositories() {
|
||||
Tokens = NewGORMRegisterTokenRepository(db)
|
||||
ConfigOptions = NewGORMConfigRepository(db)
|
||||
Papers = NewGORMPaperRepository(db)
|
||||
PrintJobs = NewGORMPrintJobRepository(db)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user