2023-03-07 18:52:37 +07:00
|
|
|
package api
|
2023-03-08 15:22:57 +07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
db "git.nochill.in/nochill/naice_pos/db/sqlc"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CreatePurchaseOrderRequest struct {
|
|
|
|
MerchantID uuid.UUID `json:"merchant_id" 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
|
|
|
|
|
|
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
|
|
ctx.JSON(http.StatusBadRequest, errorResponse(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
arg := db.PurchasoOrderTxParams{
|
|
|
|
MerchantID: req.MerchantID,
|
|
|
|
SupplierID: req.SupplierID,
|
|
|
|
Code: sql.NullString{String: req.Code, Valid: len(req.Code) > 0},
|
|
|
|
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)
|
|
|
|
}
|