feat: refine OT flow, reports, theming and sidebar UX

This commit is contained in:
Luciano Milani
2026-07-02 22:19:03 +01:00
parent 8231bba3f9
commit 9bf176b385
48 changed files with 3469 additions and 599 deletions
+107 -11
View File
@@ -2,6 +2,7 @@ package workorder
import (
"errors"
"time"
"github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v5"
@@ -53,6 +54,28 @@ type woBody struct {
VehicleID string `json:"vehicle_id"`
InternalNotes string `json:"internal_notes"`
ClientNotes string `json:"client_notes"`
ETADays int `json:"eta_days"`
RealDeadline string `json:"real_deadline"`
PaymentMethod string `json:"payment_method"`
PaymentDate string `json:"payment_date"`
}
func isValidETADays(v int) bool {
switch v {
case 1, 2, 3, 7, 15, 30, 31:
return true
default:
return false
}
}
func isValidPaymentMethod(v string) bool {
switch v {
case "", "numerario", "multibanco", "transferencia_bancaria", "mb_way", "cartao_debito", "cartao_credito", "cheque", "outro":
return true
default:
return false
}
}
func createWOH() fiber.Handler {
@@ -61,13 +84,32 @@ func createWOH() fiber.Handler {
if err := c.BodyParser(&b); err != nil {
return fiber.NewError(400, "corpo inválido")
}
if b.ETADays == 0 {
b.ETADays = 1
}
if !isValidETADays(b.ETADays) {
return fiber.NewError(400, "eta_days inválido")
}
if b.RealDeadline != "" {
if _, err := time.Parse("2006-01-02", b.RealDeadline); err != nil {
return fiber.NewError(400, "real_deadline inválido (YYYY-MM-DD)")
}
}
if !isValidPaymentMethod(b.PaymentMethod) {
return fiber.NewError(400, "payment_method inválido")
}
if b.PaymentDate != "" {
if _, err := time.Parse("2006-01-02", b.PaymentDate); err != nil {
return fiber.NewError(400, "payment_date inválido (YYYY-MM-DD)")
}
}
claims, _ := c.Locals("claims").(*auth.Claims)
createdBy := ""
if claims != nil {
createdBy = claims.UserID
}
conn := auth.GetConn(c)
wo, err := CreateWorkOrder(c.Context(), conn, b.ClientID, b.VehicleID, b.InternalNotes, createdBy)
wo, err := CreateWorkOrder(c.Context(), conn, b.ClientID, b.VehicleID, b.InternalNotes, b.ClientNotes, b.ETADays, b.RealDeadline, createdBy)
if err != nil {
return fiber.NewError(500, "erro ao criar ordem")
}
@@ -95,11 +137,30 @@ func updateWOH() fiber.Handler {
if err := c.BodyParser(&b); err != nil {
return fiber.NewError(400, "corpo inválido")
}
if b.ETADays == 0 {
b.ETADays = 1
}
if !isValidETADays(b.ETADays) {
return fiber.NewError(400, "eta_days inválido")
}
if b.RealDeadline != "" {
if _, err := time.Parse("2006-01-02", b.RealDeadline); err != nil {
return fiber.NewError(400, "real_deadline inválido (YYYY-MM-DD)")
}
}
if !isValidPaymentMethod(b.PaymentMethod) {
return fiber.NewError(400, "payment_method inválido")
}
if b.PaymentDate != "" {
if _, err := time.Parse("2006-01-02", b.PaymentDate); err != nil {
return fiber.NewError(400, "payment_date inválido (YYYY-MM-DD)")
}
}
conn := auth.GetConn(c)
wo, err := UpdateWorkOrder(c.Context(), conn, c.Params("id"), b.ClientID, b.VehicleID, b.InternalNotes, b.ClientNotes)
wo, err := UpdateWorkOrder(c.Context(), conn, c.Params("id"), b.ClientID, b.VehicleID, b.InternalNotes, b.ClientNotes, b.ETADays, b.RealDeadline, b.PaymentMethod, b.PaymentDate)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return fiber.NewError(404, "ordem não encontrada")
return fiber.NewError(400, "ordem não editável (cancelada/faturada) ou não encontrada")
}
return fiber.NewError(500, "erro ao actualizar ordem")
}
@@ -110,18 +171,36 @@ func updateWOH() fiber.Handler {
func transitionWOH() fiber.Handler {
return func(c *fiber.Ctx) error {
var body struct {
Status string `json:"status"`
Status string `json:"status"`
PaymentMethod string `json:"payment_method"`
PaymentDate string `json:"payment_date"`
}
if err := c.BodyParser(&body); err != nil || body.Status == "" {
return fiber.NewError(400, "status é obrigatório")
}
if !isValidPaymentMethod(body.PaymentMethod) {
return fiber.NewError(400, "payment_method inválido")
}
if body.PaymentDate != "" {
if _, err := time.Parse("2006-01-02", body.PaymentDate); err != nil {
return fiber.NewError(400, "payment_date inválido (YYYY-MM-DD)")
}
}
if body.Status == "invoiced" {
if body.PaymentMethod == "" {
return fiber.NewError(400, "meio de pagamento é obrigatório para faturar")
}
if body.PaymentDate == "" {
body.PaymentDate = time.Now().Format("2006-01-02")
}
}
claims, _ := c.Locals("claims").(*auth.Claims)
changedBy := ""
if claims != nil {
changedBy = claims.UserID
}
conn := auth.GetConn(c)
wo, err := TransitionStatus(c.Context(), conn, c.Params("id"), body.Status, changedBy)
wo, err := TransitionStatus(c.Context(), conn, c.Params("id"), body.Status, body.PaymentMethod, body.PaymentDate, changedBy)
if err != nil {
return fiber.NewError(400, err.Error())
}
@@ -130,11 +209,12 @@ func transitionWOH() fiber.Handler {
}
type woItemBody struct {
CatalogItemID string `json:"catalog_item_id"`
Description string `json:"description"`
Qty float64 `json:"qty"`
UnitPrice float64 `json:"unit_price"`
DiscountPct float64 `json:"discount_pct"`
CatalogItemID string `json:"catalog_item_id"`
Description string `json:"description"`
ChangeJustification string `json:"change_justification"`
Qty float64 `json:"qty"`
UnitPrice float64 `json:"unit_price"`
DiscountPct float64 `json:"discount_pct"`
}
func addItemH() fiber.Handler {
@@ -147,8 +227,21 @@ func addItemH() fiber.Handler {
return fiber.NewError(400, "descrição, quantidade e preço unitário são obrigatórios")
}
conn := auth.GetConn(c)
item, err := AddItem(c.Context(), conn, c.Params("id"), b.CatalogItemID, b.Description, b.Qty, b.UnitPrice, b.DiscountPct)
var status string
if err := conn.QueryRow(c.Context(), `SELECT status FROM work_orders WHERE id=$1`, c.Params("id")).Scan(&status); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return fiber.NewError(404, "ordem não encontrada")
}
return fiber.NewError(500, "erro ao validar ordem")
}
if status == "open" && b.ChangeJustification == "" {
return fiber.NewError(400, "justificação da alteração é obrigatória em orçamento aprovado")
}
item, err := AddItem(c.Context(), conn, c.Params("id"), b.CatalogItemID, b.Description, b.ChangeJustification, b.Qty, b.UnitPrice, b.DiscountPct)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return fiber.NewError(400, "ordem não editável (cancelada/faturada) ou dados inválidos")
}
return fiber.NewError(500, "erro ao adicionar item")
}
return c.Status(201).JSON(fiber.Map{"data": item, "error": nil})
@@ -183,6 +276,9 @@ func addStaffHoursH() fiber.Handler {
conn := auth.GetConn(c)
sh, err := AddStaffHours(c.Context(), conn, c.Params("id"), b.StaffID, b.Hours, b.CostPerHour)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return fiber.NewError(400, "ordem não editável (cancelada/faturada) ou não encontrada")
}
return fiber.NewError(500, "erro ao adicionar horas")
}
return c.Status(201).JSON(fiber.Map{"data": sh, "error": nil})