Compare commits
2 Commits
8ce01417e7
...
992b9c17c3
| Author | SHA1 | Date | |
|---|---|---|---|
|
992b9c17c3
|
|||
|
4b0649439c
|
@@ -2,13 +2,11 @@ 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"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -136,47 +134,15 @@ func (rc *printController) PrintHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var coverPage models.Paper
|
var coverPage *models.Paper
|
||||||
doPrintCoverpage := false
|
|
||||||
if variantCoverPages[idx] != "0" {
|
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 {
|
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}})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
doPrintCoverpage = true
|
coverPage = &coverPageTmp
|
||||||
}
|
|
||||||
|
|
||||||
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])
|
||||||
@@ -187,10 +153,14 @@ func (rc *printController) PrintHandler(c *gin.Context) {
|
|||||||
|
|
||||||
fmt.Println("Printing Costs:")
|
fmt.Println("Printing Costs:")
|
||||||
|
|
||||||
colored := variant.Name == "Colored"
|
printJob, err := models.NewPrintJob(shopItem, variant, paperType, coverPage, uint(variantAmount))
|
||||||
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}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
printJob.CalculatePrintCosts()
|
||||||
|
printJob, err = repositories.PrintJobs.Create(printJob)
|
||||||
|
|
||||||
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}})
|
||||||
@@ -200,6 +170,18 @@ func (rc *printController) PrintHandler(c *gin.Context) {
|
|||||||
printJobs = append(printJobs, printJob)
|
printJobs = append(printJobs, printJob)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
invoice := models.Invoice{
|
||||||
|
PrintJobs: printJobs,
|
||||||
|
PricePerClick: 0.002604,
|
||||||
|
PartCosts: 0.0067,
|
||||||
|
}
|
||||||
|
invoice, err := repositories.Invoices.Create(invoice)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.HTML(http.StatusBadRequest, "error.html", gin.H{"data": gin.H{"error": err}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
executeJobs := func() {
|
executeJobs := func() {
|
||||||
for _, printJob := range printJobs {
|
for _, printJob := range printJobs {
|
||||||
printJob.Execute()
|
printJob.Execute()
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"git.dynamicdiscord.de/kalipso/zineshop/utils"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"math"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -18,12 +21,33 @@ const (
|
|||||||
TriFold PrintOption = "-o Fold=TriFold -o Binding=TopBinding"
|
TriFold PrintOption = "-o Fold=TriFold -o Binding=TopBinding"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PrintJob struct {
|
type OldPrintJob struct {
|
||||||
Pdf string
|
Pdf string
|
||||||
Amount uint
|
Amount uint
|
||||||
Options []PrintOption
|
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 {
|
func GetPrintMode(mode string) PrintOption {
|
||||||
if mode == "LongEdge" {
|
if mode == "LongEdge" {
|
||||||
return LongEdge
|
return LongEdge
|
||||||
@@ -40,7 +64,7 @@ func GetPrintMode(mode string) PrintOption {
|
|||||||
return CreateBooklet
|
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 == "" {
|
if shopItem.Pdf == "" {
|
||||||
return PrintJob{}, fmt.Errorf("ShopItem has no PDF assigned")
|
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")
|
return PrintJob{}, fmt.Errorf("Amount to big. This is denied for security reasons")
|
||||||
}
|
}
|
||||||
|
|
||||||
var result PrintJob
|
result := PrintJob{
|
||||||
result.Pdf = shopItem.Pdf
|
ShopItem: shopItem,
|
||||||
result.Amount = amount
|
Variant: variant,
|
||||||
|
PaperType: paperType,
|
||||||
if variant.Name == "Colored" {
|
CoverPaperType: coverPaperType,
|
||||||
result.Options = append(result.Options, Colored)
|
Amount: amount,
|
||||||
}
|
}
|
||||||
|
|
||||||
if coverPage {
|
|
||||||
result.Options = append(result.Options, CoverPage)
|
|
||||||
}
|
|
||||||
|
|
||||||
result.Options = append(result.Options, GetPrintMode(shopItem.PrintMode))
|
|
||||||
|
|
||||||
return result, nil
|
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 {
|
func (p *PrintJob) Execute() error {
|
||||||
baseCommand := "lp -d KonicaBooklet"
|
baseCommand := "lp -d KonicaBooklet"
|
||||||
baseCommand += fmt.Sprintf(" -n %v ", p.Amount)
|
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(" %v ", option)
|
||||||
}
|
}
|
||||||
|
|
||||||
baseCommand += fmt.Sprintf(" -- %s", p.Pdf)
|
baseCommand += fmt.Sprintf(" -- %s", p.ShopItem.Pdf)
|
||||||
|
|
||||||
parts := strings.Fields(baseCommand)
|
parts := strings.Fields(baseCommand)
|
||||||
|
|
||||||
@@ -91,3 +127,34 @@ func (p *PrintJob) Execute() error {
|
|||||||
fmt.Printf("Output:\n%s\n", output)
|
fmt.Printf("Output:\n%s\n", output)
|
||||||
return nil
|
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 {
|
type ShopItem struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
Name string `json:"name" binding:"required" gorm:"unique;not null"`
|
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"`
|
Description string `json:"description" binding:"required"`
|
||||||
Category Category `json:"category"`
|
Category Category `json:"category"`
|
||||||
Variants []ItemVariant `json:"variant"`
|
Variants []ItemVariant `json:"variant"`
|
||||||
|
|||||||
82
repositories/InvoiceRepository.go
Normal file
82
repositories/InvoiceRepository.go
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
package repositories
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
"git.dynamicdiscord.de/kalipso/zineshop/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InvoiceRepository interface {
|
||||||
|
Create(models.Invoice) (models.Invoice, error)
|
||||||
|
GetAll() ([]models.Invoice, error)
|
||||||
|
GetById(string) (models.Invoice, error)
|
||||||
|
//GetByShopItemId(string) (models.Invoice, error)
|
||||||
|
Update(models.Invoice) (models.Invoice, error)
|
||||||
|
DeleteById(string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type GORMInvoiceRepository struct {
|
||||||
|
DB *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGORMInvoiceRepository(db *gorm.DB) InvoiceRepository {
|
||||||
|
return &GORMInvoiceRepository{
|
||||||
|
DB: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GORMInvoiceRepository) Create(invoice models.Invoice) (models.Invoice, error) {
|
||||||
|
result := t.DB.Omit("PrintJobs").Create(&invoice)
|
||||||
|
|
||||||
|
if result.Error != nil {
|
||||||
|
return models.Invoice{}, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
return invoice, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GORMInvoiceRepository) GetAll() ([]models.Invoice, error) {
|
||||||
|
var invoice []models.Invoice
|
||||||
|
result := t.DB.Preload("PrintJobs").Find(&invoice)
|
||||||
|
|
||||||
|
return invoice, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GORMInvoiceRepository) GetById(id string) (models.Invoice, error) {
|
||||||
|
invoiceId, err := strconv.Atoi(id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return models.Invoice{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var invoice models.Invoice
|
||||||
|
result := t.DB.Preload("PrintJobs").First(&invoice, uint(invoiceId))
|
||||||
|
|
||||||
|
if result.Error != nil {
|
||||||
|
return models.Invoice{}, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
return invoice, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GORMInvoiceRepository) Update(invoice models.Invoice) (models.Invoice, error) {
|
||||||
|
result := t.DB.Save(&invoice)
|
||||||
|
if result.Error != nil {
|
||||||
|
return models.Invoice{}, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
return invoice, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GORMInvoiceRepository) DeleteById(id string) error {
|
||||||
|
invoiceId, err := strconv.Atoi(id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
result := t.DB.Delete(&models.Invoice{}, invoiceId)
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
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(printJob models.PrintJob) (models.PrintJob, error) {
|
||||||
|
result := t.DB.Create(&printJob)
|
||||||
|
|
||||||
|
if result.Error != nil {
|
||||||
|
return models.PrintJob{}, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
return printJob, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GORMPrintJobRepository) GetAll() ([]models.PrintJob, error) {
|
||||||
|
var printJobs []models.PrintJob
|
||||||
|
result := t.DB.Preload("ShopItem").Preload("Variant").Preload("PaperType").Preload("CoverPaperType").Find(&printJobs)
|
||||||
|
|
||||||
|
return printJobs, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GORMPrintJobRepository) GetById(id string) (models.PrintJob, error) {
|
||||||
|
printJobId, err := strconv.Atoi(id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return models.PrintJob{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var printJob models.PrintJob
|
||||||
|
result := t.DB.Preload("ShopItem").Preload("Variant").Preload("PaperType").Preload("CoverPaperType").First(&printJob, uint(printJobId))
|
||||||
|
|
||||||
|
if result.Error != nil {
|
||||||
|
return models.PrintJob{}, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
return printJob, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GORMPrintJobRepository) Update(printJob models.PrintJob) (models.PrintJob, error) {
|
||||||
|
result := t.DB.Save(&printJob)
|
||||||
|
if result.Error != nil {
|
||||||
|
return models.PrintJob{}, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
return printJob, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GORMPrintJobRepository) DeleteById(id string) error {
|
||||||
|
printJobId, err := strconv.Atoi(id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
result := t.DB.Delete(&models.PrintJob{}, printJobId)
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
@@ -17,6 +17,8 @@ var (
|
|||||||
Tokens RegisterTokenRepository
|
Tokens RegisterTokenRepository
|
||||||
ConfigOptions ConfigRepository
|
ConfigOptions ConfigRepository
|
||||||
Papers PaperRepository
|
Papers PaperRepository
|
||||||
|
PrintJobs PrintJobRepository
|
||||||
|
Invoices InvoiceRepository
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitRepositories() {
|
func InitRepositories() {
|
||||||
@@ -33,6 +35,8 @@ func InitRepositories() {
|
|||||||
&models.Order{},
|
&models.Order{},
|
||||||
&models.Config{},
|
&models.Config{},
|
||||||
&models.Paper{},
|
&models.Paper{},
|
||||||
|
&models.PrintJob{},
|
||||||
|
&models.Invoice{},
|
||||||
&models.RegisterToken{})
|
&models.RegisterToken{})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -47,4 +51,6 @@ func InitRepositories() {
|
|||||||
Tokens = NewGORMRegisterTokenRepository(db)
|
Tokens = NewGORMRegisterTokenRepository(db)
|
||||||
ConfigOptions = NewGORMConfigRepository(db)
|
ConfigOptions = NewGORMConfigRepository(db)
|
||||||
Papers = NewGORMPaperRepository(db)
|
Papers = NewGORMPaperRepository(db)
|
||||||
|
PrintJobs = NewGORMPrintJobRepository(db)
|
||||||
|
Invoices = NewGORMInvoiceRepository(db)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user