cafe-plaetschwiesle/internal/controller/bill.go
2023-07-04 23:07:23 +02:00

124 lines
3.7 KiB
Go

package controller
import (
"fmt"
"strconv"
"time"
"gitlab.unjx.de/flohoss/cafe-plaetschwiesle/internal/types"
)
type (
Bill struct {
ID uint64 `gorm:"primaryKey" json:"id" validate:"optional"`
TableID uint64 `json:"table_id" validate:"required"`
Total float32 `json:"total" validate:"required"`
CreatedAt int64 `json:"created_at" validate:"optional"`
}
BillItem struct {
ID uint64 `gorm:"primaryKey" json:"id" validate:"optional"`
BillID uint64 `json:"bill_id" validate:"required"`
Description string `json:"description" validate:"required"`
Total float32 `json:"total" validate:"required"`
Price float32 `json:"price" validate:"required"`
Amount uint64 `json:"amount" validate:"required"`
ItemType types.ItemType `json:"item_type" validate:"required"`
}
)
func (c *Controller) DoesBillExist(id string) (Bill, error) {
var bill Bill
result := c.orm.Limit(1).Find(&bill, id)
if result.RowsAffected == 0 {
return bill, fmt.Errorf(types.CannotFind.String())
}
return bill, nil
}
func (c *Controller) GetAllBillItems(billId uint64) ([]BillItem, error) {
var billItems []BillItem
result := c.orm.Where("bill_id = ?", billId).Find(&billItems)
if result.RowsAffected == 0 {
return billItems, fmt.Errorf(types.CannotFind.String())
}
return billItems, nil
}
func getDate(year string, month string, day string) (time.Time, error) {
yearI, yearErr := strconv.Atoi(year)
if yearErr != nil {
return time.Time{}, fmt.Errorf("jahr " + types.CannotParse.String())
}
monthI, monthErr := strconv.Atoi(month)
if monthErr != nil {
return time.Time{}, fmt.Errorf("monat " + types.CannotParse.String())
}
dayI, dayErr := strconv.Atoi(day)
if dayErr != nil {
return time.Time{}, fmt.Errorf("tag " + types.CannotParse.String())
}
loc, locErr := time.LoadLocation("Local")
if locErr != nil {
return time.Time{}, fmt.Errorf("timezone " + types.CannotParse.String())
}
return time.Date(yearI, time.Month(monthI), dayI, 0, 0, 0, 0, loc), nil
}
func (c *Controller) GetAllBills(year string, month string, day string) ([]Bill, error) {
var bills []Bill
today, err := getDate(year, month, day)
if err != nil {
return bills, err
}
beginningOfDay := today.Unix()
endOfDay := today.Add(23 * time.Hour).Add(59 * time.Minute).Add(59 * time.Second).Unix()
c.orm.Where("created_at BETWEEN ? AND ?", beginningOfDay, endOfDay).Order("created_at").Find(&bills)
return bills, nil
}
func (c *Controller) createBill(options GetOrderOptions) (Bill, error) {
orders := c.getAllOrdersForTable(options)
var bill Bill
var total float32 = 0
for _, order := range orders {
total += order.Total
}
bill.TableID = options.TableId
bill.Total = total
err := c.orm.Create(&bill).Error
if err != nil {
return bill, fmt.Errorf(types.CannotCreate.String())
}
for _, order := range orders {
billItem := BillItem{
BillID: bill.ID,
Description: order.OrderItem.Description,
Total: order.Total,
Price: order.OrderItem.Price,
Amount: order.OrderCount,
ItemType: order.OrderItem.ItemType,
}
c.orm.Create(&billItem)
}
ordersToDelete := c.getAllOrdersForTable(GetOrderOptions{TableId: options.TableId, Grouped: false, Filter: options.Filter})
err = c.orm.Delete(&ordersToDelete).Error
if err != nil {
return bill, fmt.Errorf(types.CannotDelete.String())
}
c.publishMessage(StatusMessage{
Type: types.DeleteAll,
Payload: ordersToDelete,
})
return bill, nil
}
func (c *Controller) deleteBill(bill *Bill) error {
err := c.orm.Delete(bill).Error
if err != nil {
return fmt.Errorf(types.CannotDelete.String())
}
billItemsToDelete, _ := c.GetAllBillItems(bill.ID)
c.orm.Delete(&billItemsToDelete)
return nil
}