package api import ( "database/sql" "net/http" db "git.nochill.in/nochill/naice_pos/db/sqlc" "git.nochill.in/nochill/naice_pos/util" "github.com/gin-gonic/gin" "github.com/google/uuid" ) type CreatePurchaseOrderRequest struct { MerchantID uuid.UUID `json:"merchant_id" binding:"required"` MerchantIdx int64 `json:"merchant_index" binding:"required,number"` CreatedBy uuid.UUID `json:"created_by" binding:"required"` SupplierID uuid.UUID `json:"supplier_id" binding:"required"` Code string `json:"code"` IsPaid bool `json:"is_paid" binding:"required"` Total float64 `json:"total" binding:"required"` PaidNominal float64 `json:"paid_nominal" binding:"required"` Note string `json:"note"` Products []db.PurchaseOrderProduct `json:"products" binding:"required"` } func (server Server) createPurchase(ctx *gin.Context) { var req CreatePurchaseOrderRequest var code sql.NullString if len(req.Code) > 0 { code = sql.NullString{Valid: true, String: req.Code} } else { code = sql.NullString{Valid: true, String: util.RandomTransactionCode("P", req.MerchantIdx)} } if err := ctx.ShouldBindJSON(&req); err != nil { ctx.JSON(http.StatusBadRequest, errorResponse(err, "")) return } arg := db.PurchasoOrderTxParams{ MerchantID: req.MerchantID, CreatedBy: req.CreatedBy, SupplierID: req.SupplierID, Code: code, IsPaid: req.IsPaid, Total: req.Total, PaidNominal: req.PaidNominal, Note: sql.NullString{String: req.Note, Valid: len(req.Code) > 0}, Products: req.Products, } result, err := server.store.PurchaseOrderTx(ctx, arg) if err != nil { ctx.JSON(http.StatusInternalServerError, errorResponse(err, "")) return } ctx.JSON(http.StatusOK, result) }