Complete all for Items
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
// Single typed fetch client for the ERPCore API (docs/20-FRONTEND.md §1). Per-endpoint
|
||||
// methods live in lib/api/*.ts — no scattered fetch() in components.
|
||||
//
|
||||
// Transport: the API is reached SAME-ORIGIN through the Next rewrite in next.config.ts
|
||||
// (/api/* -> BACKEND_ORIGIN). That is why API_BASE is relative and why no CORS setup
|
||||
// exists on the backend: there is no cross-origin request to allow.
|
||||
//
|
||||
// Auth: the session is an httpOnly `erp_at` cookie issued by POST /auth/login (docs/11
|
||||
// §2.0) — there is no bearer token to read, and by design JS cannot read the cookie.
|
||||
// `credentials: "include"` is what actually authenticates every call.
|
||||
import { ApiResult, ProblemDetails } from "@/types/common"
|
||||
|
||||
const API_BASE = "/api/v1"
|
||||
|
||||
/** Normalized RFC 7807 error (docs/11-BACKEND-PHASE1.md §1.8) thrown on any non-2xx response. */
|
||||
export class ApiError extends Error {
|
||||
status: number
|
||||
code?: string
|
||||
detail?: string
|
||||
errors?: Record<string, string[]>
|
||||
traceId?: string
|
||||
|
||||
constructor(problem: ProblemDetails) {
|
||||
super(problem.title || "Request failed")
|
||||
this.status = problem.status
|
||||
this.code = problem.code
|
||||
this.detail = problem.detail
|
||||
this.errors = problem.errors
|
||||
this.traceId = problem.traceId
|
||||
}
|
||||
}
|
||||
|
||||
export interface RequestOptions extends Omit<RequestInit, "body"> {
|
||||
body?: unknown
|
||||
/** Sent as If-Match for concurrency-guarded PUT/PATCH (docs/11 §1.6). Echo the ETag verbatim, quotes included. */
|
||||
ifMatch?: string
|
||||
/** Sent as Idempotency-Key for transactional POSTs (e.g. GRN confirm, docs/11 §1.6). */
|
||||
idempotencyKey?: string
|
||||
/** Sent as X-XSRF-TOKEN. Only the [ValidateCsrf] actions on AuthController need it (docs/11 §2.0). */
|
||||
csrf?: boolean
|
||||
}
|
||||
|
||||
/** Reads the non-httpOnly CSRF cookie. It rotates on every session write, so read it per call. */
|
||||
export function readCsrfToken(): string | null {
|
||||
if (typeof document === "undefined") return null
|
||||
const match = document.cookie.match(/(?:^|;\s*)XSRF-TOKEN=([^;]*)/)
|
||||
return match ? decodeURIComponent(match[1]) : null
|
||||
}
|
||||
|
||||
async function rawRequest(path: string, options: RequestOptions = {}): Promise<Response> {
|
||||
const { body, ifMatch, idempotencyKey, csrf, headers, ...rest } = options
|
||||
const csrfToken = csrf ? readCsrfToken() : null
|
||||
|
||||
const finalHeaders: Record<string, string> = {
|
||||
Accept: "application/json",
|
||||
...(body !== undefined ? { "Content-Type": "application/json" } : {}),
|
||||
...(ifMatch ? { "If-Match": ifMatch } : {}),
|
||||
...(idempotencyKey ? { "Idempotency-Key": idempotencyKey } : {}),
|
||||
...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}),
|
||||
...((headers as Record<string, string> | undefined) ?? {}),
|
||||
}
|
||||
|
||||
const response = await fetch(`${API_BASE}${path}`, {
|
||||
...rest,
|
||||
credentials: "include", // sends erp_at; the whole auth story depends on this
|
||||
headers: finalHeaders,
|
||||
body: body !== undefined ? JSON.stringify(body) : undefined,
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
let problem: ProblemDetails
|
||||
try {
|
||||
problem = (await response.json()) as ProblemDetails
|
||||
} catch {
|
||||
// e.g. a proxy/network failure with a non-JSON body.
|
||||
problem = { title: response.statusText || "Request failed", status: response.status }
|
||||
}
|
||||
if (!problem.status) problem.status = response.status
|
||||
throw new ApiError(problem)
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
/** Fire a request and decode the JSON body only (no ETag needed). */
|
||||
export async function apiRequest<T>(path: string, options?: RequestOptions): Promise<T> {
|
||||
const response = await rawRequest(path, options)
|
||||
if (response.status === 204) return undefined as T
|
||||
return (await response.json()) as T
|
||||
}
|
||||
|
||||
/** Fire a request and also surface the ETag header, for resources that support If-Match. */
|
||||
export async function apiRequestWithETag<T>(path: string, options?: RequestOptions): Promise<ApiResult<T>> {
|
||||
const response = await rawRequest(path, options)
|
||||
const etag = response.headers.get("ETag")
|
||||
const data = response.status === 204 ? (undefined as T) : ((await response.json()) as T)
|
||||
return { data, etag }
|
||||
}
|
||||
|
||||
/** Build a `?a=1&b=2` query string, dropping null/undefined/empty values. */
|
||||
export function buildQuery(params: object): string {
|
||||
const search = new URLSearchParams()
|
||||
for (const [key, value] of Object.entries(params) as [string, string | number | boolean | null | undefined][]) {
|
||||
if (value === null || value === undefined || value === "") continue
|
||||
search.set(key, String(value))
|
||||
}
|
||||
const qs = search.toString()
|
||||
return qs ? `?${qs}` : ""
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Auth endpoints (docs/11-BACKEND-PHASE1.md §2.0). ERPCore proxies the AuthHex IdP and
|
||||
// delivers the session as httpOnly cookies — there is no token for JS to hold or attach.
|
||||
import { apiRequest } from "@/lib/api-client"
|
||||
import { AuthSession, LoginRequest, RegisterRequest } from "@/types/auth"
|
||||
|
||||
export const authApi = {
|
||||
/** Sets erp_at / erp_rt / XSRF-TOKEN cookies on success. Body carries no tokens. */
|
||||
login(request: LoginRequest): Promise<AuthSession> {
|
||||
return apiRequest<AuthSession>("/auth/login", { method: "POST", body: request })
|
||||
},
|
||||
|
||||
/** Also issues a session, same as login. */
|
||||
register(request: RegisterRequest): Promise<AuthSession> {
|
||||
return apiRequest<AuthSession>("/auth/register", { method: "POST", body: request })
|
||||
},
|
||||
|
||||
/**
|
||||
* Clears all three cookies server-side.
|
||||
*
|
||||
* `userId` is optional and normally null: AuthHex omits it from its own login response,
|
||||
* so the browser never learns it. The server falls back to the session token's UserId
|
||||
* claim, and clears the cookies regardless of what the upstream revoke does.
|
||||
*/
|
||||
logout(userId: string | null = null): Promise<void> {
|
||||
return apiRequest<void>("/auth/logout", { method: "POST", body: { userId } })
|
||||
},
|
||||
|
||||
/** Exchanges the path-scoped erp_rt cookie for a fresh session. */
|
||||
refresh(): Promise<AuthSession> {
|
||||
return apiRequest<AuthSession>("/auth/refresh-token", { method: "POST", body: {} })
|
||||
},
|
||||
}
|
||||
@@ -1,64 +1,39 @@
|
||||
// One typed client method per Brand endpoint, mirroring lib/api/uoms.ts.
|
||||
// In-memory sample data (lib/api/mock-data.ts) — no backend API calls.
|
||||
import { PagedResponse } from "@/types/common"
|
||||
// One typed client method per Brand endpoint (docs/11-BACKEND-PHASE1.md §2.6; FR-MD-09).
|
||||
import { apiRequest, apiRequestWithETag, buildQuery } from "@/lib/api-client"
|
||||
import { ApiResult, EntityStatus, PagedResponse } from "@/types/common"
|
||||
import { Brand, CreateBrandRequest, UpdateBrandRequest } from "@/types/master-data"
|
||||
import { allocateBrandId, mockBrands, mockDelay } from "@/lib/api/mock-data"
|
||||
|
||||
export interface ListBrandsParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
q?: string
|
||||
sortOrder?: "asc" | "desc"
|
||||
status?: EntityStatus
|
||||
/** `name` / `-name` etc. (docs/11 §1.5). */
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export const brandsApi = {
|
||||
list(params: ListBrandsParams = {}): Promise<PagedResponse<Brand>> {
|
||||
const term = params.q?.trim().toLowerCase()
|
||||
const sortOrder = params.sortOrder ?? "asc"
|
||||
const filtered = mockBrands
|
||||
.filter((b) => !term || b.name.toLowerCase().includes(term))
|
||||
.sort((a, b) => (sortOrder === "asc" ? a.name.localeCompare(b.name) : b.name.localeCompare(a.name)))
|
||||
|
||||
const page = params.page ?? 1
|
||||
const pageSize = params.pageSize ?? 5
|
||||
const start = (page - 1) * pageSize
|
||||
const items = filtered.slice(start, start + pageSize)
|
||||
const totalItems = filtered.length
|
||||
const totalPages = pageSize <= 0 ? 0 : Math.ceil(totalItems / pageSize)
|
||||
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page, pageSize, totalItems, totalPages },
|
||||
})
|
||||
return apiRequest<PagedResponse<Brand>>(`/brands${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
create(request: CreateBrandRequest): Promise<Brand> {
|
||||
const name = request.name.trim()
|
||||
if (!name) return Promise.reject(new Error("Brand name is required."))
|
||||
if (mockBrands.some((b) => b.name.toLowerCase() === name.toLowerCase())) {
|
||||
return Promise.reject(new Error(`Brand "${name}" already exists.`))
|
||||
}
|
||||
const brand: Brand = { brandId: allocateBrandId(), name, createdAt: new Date().toISOString() }
|
||||
mockBrands.push(brand)
|
||||
return mockDelay(brand)
|
||||
get(brandId: number): Promise<ApiResult<Brand>> {
|
||||
return apiRequestWithETag<Brand>(`/brands/${brandId}`)
|
||||
},
|
||||
|
||||
update(brandId: number, request: UpdateBrandRequest): Promise<Brand> {
|
||||
const name = request.name.trim()
|
||||
if (!name) return Promise.reject(new Error("Brand name is required."))
|
||||
const brand = mockBrands.find((b) => b.brandId === brandId)
|
||||
if (!brand) return Promise.reject(new Error("Brand not found."))
|
||||
if (mockBrands.some((b) => b.brandId !== brandId && b.name.toLowerCase() === name.toLowerCase())) {
|
||||
return Promise.reject(new Error(`Brand "${name}" already exists.`))
|
||||
}
|
||||
brand.name = name
|
||||
return mockDelay(brand)
|
||||
create(request: CreateBrandRequest): Promise<ApiResult<Brand>> {
|
||||
return apiRequestWithETag<Brand>("/brands", { method: "POST", body: request })
|
||||
},
|
||||
|
||||
remove(brandId: number): Promise<void> {
|
||||
const index = mockBrands.findIndex((b) => b.brandId === brandId)
|
||||
if (index === -1) return Promise.reject(new Error("Brand not found."))
|
||||
mockBrands.splice(index, 1)
|
||||
return mockDelay(undefined)
|
||||
update(brandId: number, request: UpdateBrandRequest, ifMatch: string): Promise<ApiResult<Brand>> {
|
||||
return apiRequestWithETag<Brand>(`/brands/${brandId}`, { method: "PUT", body: request, ifMatch })
|
||||
},
|
||||
|
||||
/**
|
||||
* Deactivate/reactivate. There is no DELETE anywhere in the API: masters referenced by
|
||||
* transactions are deactivated, never removed (FR-MD-08).
|
||||
*/
|
||||
updateStatus(brandId: number, status: EntityStatus): Promise<void> {
|
||||
return apiRequest<void>(`/brands/${brandId}/status`, { method: "PATCH", body: { status } })
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,62 +1,80 @@
|
||||
// One typed client method per Category endpoint (docs/11-BACKEND-PHASE1.md §2.3, FR-MD-04).
|
||||
// In-memory sample data (lib/api/mock-data.ts) — no backend API calls.
|
||||
import { PagedResponse } from "@/types/common"
|
||||
import { Category, CreateCategoryRequest, UpdateCategoryRequest } from "@/types/master-data"
|
||||
import { allocateCategoryId, mockCategories, mockDelay } from "@/lib/api/mock-data"
|
||||
// One typed client method per Category / SubCategory endpoint
|
||||
// (docs/11-BACKEND-PHASE1.md §2.3; FR-MD-04).
|
||||
//
|
||||
// The hierarchy is exactly two levels. Categories no longer self-nest — `parentId` and
|
||||
// `GET /categories?tree=true` were removed on 2026-07-16 — so there is no tree() here.
|
||||
// Subcategories are listed/created under their parent; updates address them by their own id.
|
||||
import { apiRequest, apiRequestWithETag, buildQuery } from "@/lib/api-client"
|
||||
import { ApiResult, EntityStatus, PagedResponse } from "@/types/common"
|
||||
import {
|
||||
Category,
|
||||
CreateCategoryRequest,
|
||||
CreateSubCategoryRequest,
|
||||
SubCategory,
|
||||
UpdateCategoryRequest,
|
||||
UpdateSubCategoryRequest,
|
||||
} from "@/types/master-data"
|
||||
|
||||
export interface ListCategoriesParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
q?: string
|
||||
sortOrder?: "asc" | "desc"
|
||||
status?: EntityStatus
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export type ListSubCategoriesParams = ListCategoriesParams
|
||||
|
||||
export const categoriesApi = {
|
||||
list(params: ListCategoriesParams = {}): Promise<PagedResponse<Category>> {
|
||||
const term = params.q?.trim().toLowerCase()
|
||||
const sortOrder = params.sortOrder ?? "asc"
|
||||
const filtered = mockCategories
|
||||
.filter((c) => !term || c.name.toLowerCase().includes(term))
|
||||
.sort((a, b) => (sortOrder === "asc" ? a.name.localeCompare(b.name) : b.name.localeCompare(a.name)))
|
||||
return apiRequest<PagedResponse<Category>>(`/categories${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
const page = params.page ?? 1
|
||||
const pageSize = params.pageSize ?? 5
|
||||
const start = (page - 1) * pageSize
|
||||
const items = filtered.slice(start, start + pageSize)
|
||||
const totalItems = filtered.length
|
||||
const totalPages = pageSize <= 0 ? 0 : Math.ceil(totalItems / pageSize)
|
||||
get(categoryId: number): Promise<ApiResult<Category>> {
|
||||
return apiRequestWithETag<Category>(`/categories/${categoryId}`)
|
||||
},
|
||||
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page, pageSize, totalItems, totalPages },
|
||||
create(request: CreateCategoryRequest): Promise<ApiResult<Category>> {
|
||||
return apiRequestWithETag<Category>("/categories", { method: "POST", body: request })
|
||||
},
|
||||
|
||||
update(categoryId: number, request: UpdateCategoryRequest, ifMatch: string): Promise<ApiResult<Category>> {
|
||||
return apiRequestWithETag<Category>(`/categories/${categoryId}`, { method: "PUT", body: request, ifMatch })
|
||||
},
|
||||
|
||||
/** Deactivate/reactivate — there is no DELETE (FR-MD-08). */
|
||||
updateStatus(categoryId: number, status: EntityStatus): Promise<void> {
|
||||
return apiRequest<void>(`/categories/${categoryId}/status`, { method: "PATCH", body: { status } })
|
||||
},
|
||||
|
||||
/** 404s if the parent category does not exist. */
|
||||
listSubCategories(categoryId: number, params: ListSubCategoriesParams = {}): Promise<PagedResponse<SubCategory>> {
|
||||
return apiRequest<PagedResponse<SubCategory>>(`/categories/${categoryId}/subcategories${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
createSubCategory(categoryId: number, request: CreateSubCategoryRequest): Promise<ApiResult<SubCategory>> {
|
||||
return apiRequestWithETag<SubCategory>(`/categories/${categoryId}/subcategories`, {
|
||||
method: "POST",
|
||||
body: request,
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
export const subCategoriesApi = {
|
||||
get(subCategoryId: number): Promise<ApiResult<SubCategory>> {
|
||||
return apiRequestWithETag<SubCategory>(`/subcategories/${subCategoryId}`)
|
||||
},
|
||||
|
||||
/** Name only — a subcategory cannot be moved to another category (docs/11 §2.3). */
|
||||
update(subCategoryId: number, request: UpdateSubCategoryRequest, ifMatch: string): Promise<ApiResult<SubCategory>> {
|
||||
return apiRequestWithETag<SubCategory>(`/subcategories/${subCategoryId}`, {
|
||||
method: "PUT",
|
||||
body: request,
|
||||
ifMatch,
|
||||
})
|
||||
},
|
||||
|
||||
create(request: CreateCategoryRequest): Promise<Category> {
|
||||
const name = request.name.trim()
|
||||
if (!name) return Promise.reject(new Error("Category name is required."))
|
||||
const parentId = request.parentId ?? null
|
||||
if (parentId !== null && !mockCategories.some((c) => c.categoryId === parentId)) {
|
||||
return Promise.reject(new Error("Selected parent category does not exist."))
|
||||
}
|
||||
const category: Category = { categoryId: allocateCategoryId(), name, parentId, createdAt: new Date().toISOString() }
|
||||
mockCategories.push(category)
|
||||
return mockDelay(category)
|
||||
},
|
||||
|
||||
update(categoryId: number, request: UpdateCategoryRequest): Promise<Category> {
|
||||
const name = request.name.trim()
|
||||
if (!name) return Promise.reject(new Error("Category name is required."))
|
||||
const category = mockCategories.find((c) => c.categoryId === categoryId)
|
||||
if (!category) return Promise.reject(new Error("Category not found."))
|
||||
category.name = name
|
||||
return mockDelay(category)
|
||||
},
|
||||
|
||||
remove(categoryId: number): Promise<void> {
|
||||
const index = mockCategories.findIndex((c) => c.categoryId === categoryId)
|
||||
if (index === -1) return Promise.reject(new Error("Category not found."))
|
||||
mockCategories.splice(index, 1)
|
||||
return mockDelay(undefined)
|
||||
updateStatus(subCategoryId: number, status: EntityStatus): Promise<void> {
|
||||
return apiRequest<void>(`/subcategories/${subCategoryId}/status`, { method: "PATCH", body: { status } })
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,223 +1,61 @@
|
||||
// One typed client method per GRN endpoint (docs/11-BACKEND-PHASE1.md §4).
|
||||
// In-memory mock store (lib/api/mock-data.ts) — no backend API calls. Note
|
||||
// GET /grns and GET /grns/{id} are not in docs/11-BACKEND-PHASE1.md §4 — see
|
||||
// the note in Frontend/PROGRESS.md §4.
|
||||
//
|
||||
// `confirm` is the transactional one: the SERVER creates the FIFO layers, posts the
|
||||
// inbound ledger and accrues PO receipts (FR-GRN-06). This client only triggers it and
|
||||
// renders the returned side effects — the browser no longer does inventory maths.
|
||||
//
|
||||
// There is deliberately no update()/remove(): the API has no PUT or DELETE for a GRN.
|
||||
// A confirmed receipt is corrected with a reversing document, never edited (FR-X-05).
|
||||
import { apiRequest, buildQuery } from "@/lib/api-client"
|
||||
import { PagedResponse } from "@/types/common"
|
||||
import {
|
||||
ConfirmGrnResponse,
|
||||
CreateGrnRequest,
|
||||
CreatedLayer,
|
||||
Grn,
|
||||
GrnStatus,
|
||||
GrnSummary,
|
||||
ReleaseAction,
|
||||
ReleaseGrnLineResponse,
|
||||
} from "@/types/grn"
|
||||
import { allocateGrnId, allocateGrnLineId, mockDelay, mockGrns, mockPurchaseOrders, receiveLayer } from "@/lib/api/mock-data"
|
||||
|
||||
export interface ListGrnsParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
/** Free-text search over doc no. and vendor/PO/warehouse id (docs/11 §1.5). */
|
||||
q?: string
|
||||
status?: GrnStatus
|
||||
poId?: number
|
||||
vendorId?: number
|
||||
warehouseId?: number
|
||||
}
|
||||
|
||||
function toSummary(grn: Grn): GrnSummary {
|
||||
return {
|
||||
grnId: grn.grnId,
|
||||
docNo: grn.docNo,
|
||||
poId: grn.poId,
|
||||
vendorId: grn.vendorId,
|
||||
warehouseId: grn.warehouseId,
|
||||
status: grn.status,
|
||||
createdAt: grn.createdAt,
|
||||
}
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export const grnsApi = {
|
||||
list(params: ListGrnsParams = {}): Promise<PagedResponse<GrnSummary>> {
|
||||
const term = params.q?.trim().toLowerCase()
|
||||
|
||||
const filtered = mockGrns
|
||||
.filter((g) => !params.status || g.status === params.status)
|
||||
.filter((g) => !params.poId || g.poId === params.poId)
|
||||
.filter((g) => !params.warehouseId || g.warehouseId === params.warehouseId)
|
||||
.filter((g) => {
|
||||
if (!term) return true
|
||||
const haystack = [g.docNo, String(g.poId ?? ""), String(g.vendorId), String(g.warehouseId)]
|
||||
.join(" ")
|
||||
.toLowerCase()
|
||||
return haystack.includes(term)
|
||||
})
|
||||
.map(toSummary)
|
||||
.sort((a, b) => b.grnId - a.grnId)
|
||||
|
||||
const page = params.page ?? 1
|
||||
const pageSize = params.pageSize ?? 20
|
||||
const start = (page - 1) * pageSize
|
||||
const items = filtered.slice(start, start + pageSize)
|
||||
const totalItems = filtered.length
|
||||
const totalPages = pageSize <= 0 ? 0 : Math.ceil(totalItems / pageSize)
|
||||
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page, pageSize, totalItems, totalPages },
|
||||
})
|
||||
return apiRequest<PagedResponse<GrnSummary>>(`/grns${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
get(grnId: number): Promise<Grn> {
|
||||
const grn = mockGrns.find((g) => g.grnId === grnId)
|
||||
if (!grn) return Promise.reject(new Error(`Mock GRN ${grnId} not found`))
|
||||
return mockDelay(grn)
|
||||
return apiRequest<Grn>(`/grns/${grnId}`)
|
||||
},
|
||||
|
||||
/** 422 OVER_RECEIPT_TOLERANCE if qty exceeds the PO's open qty beyond tolerance. */
|
||||
create(request: CreateGrnRequest): Promise<Grn> {
|
||||
const grnId = allocateGrnId()
|
||||
const referencedPo = request.poId ? mockPurchaseOrders.find((p) => p.poId === request.poId) : undefined
|
||||
const grn: Grn = {
|
||||
grnId,
|
||||
docNo: `GRN-2026-${String(grnId).padStart(5, "0")}`,
|
||||
poId: request.poId ?? null,
|
||||
// Vendor is derived from the PO when receiving against one (as the real
|
||||
// backend does) — request.vendorId is only meaningful for a direct receipt.
|
||||
vendorId: referencedPo?.vendorId ?? request.vendorId ?? 0,
|
||||
warehouseId: request.warehouseId,
|
||||
status: "Draft",
|
||||
createdBy: 17,
|
||||
createdAt: new Date().toISOString(),
|
||||
lines: request.lines.map((line) => ({
|
||||
grnLineId: allocateGrnLineId(),
|
||||
poLineId: line.poLineId ?? null,
|
||||
itemId: line.itemId,
|
||||
uomId: line.uomId,
|
||||
binId: line.binId ?? null,
|
||||
qty: line.qty,
|
||||
unitCost: line.unitCost,
|
||||
receivedValue: Math.round(line.qty * line.unitCost * 100) / 100,
|
||||
holdStatus: line.holdStatus,
|
||||
batchId: line.batch ? allocateGrnLineId() : null,
|
||||
})),
|
||||
}
|
||||
mockGrns.push(grn)
|
||||
return mockDelay(grn)
|
||||
return apiRequest<Grn>("/grns", { method: "POST", body: request })
|
||||
},
|
||||
|
||||
/**
|
||||
* Posts the receipt. Pass a stable idempotencyKey per detail-page session so a retry
|
||||
* cannot double-post stock — unlike the old mock, the server genuinely dedupes on it.
|
||||
*/
|
||||
confirm(grnId: number, idempotencyKey?: string): Promise<ConfirmGrnResponse> {
|
||||
void idempotencyKey // real backend dedupes on this; the mock always reprocesses
|
||||
const grn = mockGrns.find((g) => g.grnId === grnId)
|
||||
if (!grn) return Promise.reject(new Error(`Mock GRN ${grnId} not found`))
|
||||
if (grn.status === "Confirmed" || grn.status === "Closed") {
|
||||
return Promise.reject(new Error(`${grn.docNo} has already been confirmed.`))
|
||||
}
|
||||
|
||||
grn.status = "Confirmed"
|
||||
|
||||
const createdLayers: CreatedLayer[] = []
|
||||
const ledgerRefs: number[] = []
|
||||
|
||||
for (const line of grn.lines) {
|
||||
// FR-GRN-06: each line creates a FIFO layer + posts an inbound ledger entry.
|
||||
const { layer, ledger } = receiveLayer({
|
||||
itemId: line.itemId,
|
||||
warehouseId: grn.warehouseId,
|
||||
binId: line.binId,
|
||||
batchId: line.batchId,
|
||||
grnLineId: line.grnLineId,
|
||||
qty: line.qty,
|
||||
unitCost: line.unitCost,
|
||||
userId: grn.createdBy,
|
||||
sourceDocType: "GRN",
|
||||
sourceDocId: grn.grnId,
|
||||
})
|
||||
createdLayers.push({
|
||||
layerId: layer.layerId,
|
||||
itemId: layer.itemId,
|
||||
warehouseId: layer.warehouseId,
|
||||
batchId: layer.batchId,
|
||||
qtyReceived: layer.qtyReceived,
|
||||
qtyRemaining: layer.qtyRemaining,
|
||||
unitCost: layer.unitCost,
|
||||
receiptDate: layer.receiptDate,
|
||||
})
|
||||
ledgerRefs.push(ledger.ledgerId)
|
||||
|
||||
// FR-PROC-07: accrue the PO line's received quantity as GRNs confirm.
|
||||
if (line.poLineId && grn.poId) {
|
||||
const po = mockPurchaseOrders.find((p) => p.poId === grn.poId)
|
||||
const poLine = po?.lines.find((l) => l.poLineId === line.poLineId)
|
||||
if (poLine) poLine.qtyReceived = Math.min(poLine.qty, poLine.qtyReceived + line.qty)
|
||||
}
|
||||
}
|
||||
|
||||
let poStatus: string | null = null
|
||||
if (grn.poId) {
|
||||
const po = mockPurchaseOrders.find((p) => p.poId === grn.poId)
|
||||
if (po) {
|
||||
const fullyReceived = po.lines.every((l) => l.qtyReceived >= l.qty)
|
||||
const anyReceived = po.lines.some((l) => l.qtyReceived > 0)
|
||||
po.status = fullyReceived ? "FullyReceived" : anyReceived ? "PartiallyReceived" : po.status
|
||||
poStatus = po.status
|
||||
}
|
||||
}
|
||||
|
||||
const response: ConfirmGrnResponse = {
|
||||
grnId: grn.grnId,
|
||||
status: grn.status,
|
||||
postedAt: new Date().toISOString(),
|
||||
createdLayers,
|
||||
ledgerRefs,
|
||||
poStatus,
|
||||
}
|
||||
return mockDelay(response)
|
||||
return apiRequest<ConfirmGrnResponse>(`/grns/${grnId}/confirm`, { method: "POST", idempotencyKey })
|
||||
},
|
||||
|
||||
/** "Release" makes an on-hold line issuable; "Reject" routes it to a purchase return. */
|
||||
releaseLine(grnId: number, grnLineId: number, action: ReleaseAction): Promise<ReleaseGrnLineResponse> {
|
||||
const grn = mockGrns.find((g) => g.grnId === grnId)
|
||||
const line = grn?.lines.find((l) => l.grnLineId === grnLineId)
|
||||
if (!grn || !line) return Promise.reject(new Error(`Mock GRN line ${grnLineId} not found`))
|
||||
|
||||
line.holdStatus = action === "Release" ? "Available" : "Rejected"
|
||||
return mockDelay({ grnLineId: line.grnLineId, holdStatus: line.holdStatus })
|
||||
},
|
||||
|
||||
// Draft-only — once confirmed, a GRN has created stock layers/ledger entries
|
||||
// and is no longer safe to rewrite in place.
|
||||
update(grnId: number, request: CreateGrnRequest): Promise<Grn> {
|
||||
const grn = mockGrns.find((g) => g.grnId === grnId)
|
||||
if (!grn) return Promise.reject(new Error(`Mock GRN ${grnId} not found`))
|
||||
if (grn.status !== "Draft") {
|
||||
return Promise.reject(new Error(`${grn.docNo} is ${grn.status.toLowerCase()} and can no longer be edited.`))
|
||||
}
|
||||
|
||||
const referencedPo = request.poId ? mockPurchaseOrders.find((p) => p.poId === request.poId) : undefined
|
||||
grn.poId = request.poId ?? null
|
||||
grn.vendorId = referencedPo?.vendorId ?? request.vendorId ?? grn.vendorId
|
||||
grn.warehouseId = request.warehouseId
|
||||
grn.lines = request.lines.map((line) => ({
|
||||
grnLineId: allocateGrnLineId(),
|
||||
poLineId: line.poLineId ?? null,
|
||||
itemId: line.itemId,
|
||||
uomId: line.uomId,
|
||||
binId: line.binId ?? null,
|
||||
qty: line.qty,
|
||||
unitCost: line.unitCost,
|
||||
receivedValue: Math.round(line.qty * line.unitCost * 100) / 100,
|
||||
holdStatus: line.holdStatus,
|
||||
batchId: line.batch ? allocateGrnLineId() : null,
|
||||
}))
|
||||
return mockDelay(grn)
|
||||
},
|
||||
|
||||
remove(grnId: number): Promise<void> {
|
||||
const grn = mockGrns.find((g) => g.grnId === grnId)
|
||||
if (!grn) return Promise.reject(new Error(`Mock GRN ${grnId} not found`))
|
||||
if (grn.status !== "Draft") {
|
||||
return Promise.reject(new Error(`${grn.docNo} is ${grn.status.toLowerCase()} and can no longer be deleted.`))
|
||||
}
|
||||
mockGrns.splice(mockGrns.indexOf(grn), 1)
|
||||
return mockDelay(undefined)
|
||||
return apiRequest<ReleaseGrnLineResponse>(`/grns/${grnId}/lines/${grnLineId}/release`, {
|
||||
method: "POST",
|
||||
body: { action },
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
// One typed client method per Item Type endpoint (docs/11-BACKEND-PHASE1.md §2.7; FR-MD-10).
|
||||
//
|
||||
// Formerly `variants.ts` / `variantCategoriesApi`. An item type is a dimension NAME
|
||||
// (Color, Size, Material) and nothing more: no item references one, and there is no value
|
||||
// resource. `list()` exists to populate the item builder's dropdown — the chosen values are
|
||||
// encoded into the client-generated SKU and never stored (docs/10 Part C.9).
|
||||
import { apiRequest, apiRequestWithETag, buildQuery } from "@/lib/api-client"
|
||||
import { ApiResult, EntityStatus, PagedResponse } from "@/types/common"
|
||||
import { CreateItemTypeRequest, ItemType, UpdateItemTypeRequest } from "@/types/master-data"
|
||||
|
||||
export interface ListItemTypesParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
q?: string
|
||||
status?: EntityStatus
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export const itemTypesApi = {
|
||||
list(params: ListItemTypesParams = {}): Promise<PagedResponse<ItemType>> {
|
||||
return apiRequest<PagedResponse<ItemType>>(`/item-types${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
get(itemTypeId: number): Promise<ApiResult<ItemType>> {
|
||||
return apiRequestWithETag<ItemType>(`/item-types/${itemTypeId}`)
|
||||
},
|
||||
|
||||
create(request: CreateItemTypeRequest): Promise<ApiResult<ItemType>> {
|
||||
return apiRequestWithETag<ItemType>("/item-types", { method: "POST", body: request })
|
||||
},
|
||||
|
||||
/** Renaming does not touch existing items — their SKUs already encode the old value. */
|
||||
update(itemTypeId: number, request: UpdateItemTypeRequest, ifMatch: string): Promise<ApiResult<ItemType>> {
|
||||
return apiRequestWithETag<ItemType>(`/item-types/${itemTypeId}`, { method: "PUT", body: request, ifMatch })
|
||||
},
|
||||
|
||||
/** Deactivate/reactivate — there is no DELETE (FR-MD-08). */
|
||||
updateStatus(itemTypeId: number, status: EntityStatus): Promise<void> {
|
||||
return apiRequest<void>(`/item-types/${itemTypeId}/status`, { method: "PATCH", body: { status } })
|
||||
},
|
||||
}
|
||||
@@ -1,25 +1,18 @@
|
||||
// One typed client method per Item endpoint (docs/11-BACKEND-PHASE1.md §2.1, FR-MD-01/05/08).
|
||||
// `list` also backs the GRN/PO/Requisition/RFQ item pickers built in earlier sessions.
|
||||
// In-memory sample data (lib/api/mock-data.ts) — no backend API calls.
|
||||
// `list` also backs the GRN/PO/Requisition/RFQ item pickers.
|
||||
import { apiRequest, apiRequestWithETag, buildQuery } from "@/lib/api-client"
|
||||
import { ApiResult, EntityStatus, PagedResponse } from "@/types/common"
|
||||
import {
|
||||
CreateItemRequest,
|
||||
Item,
|
||||
ItemListItem,
|
||||
ItemReorderSetting,
|
||||
TrackingMode,
|
||||
UpdateItemReorderRequest,
|
||||
UpdateItemRequest,
|
||||
UpdateUomConversionsRequest,
|
||||
UpdateUomConversionsResponse,
|
||||
} from "@/types/master-data"
|
||||
import {
|
||||
allocateItemId,
|
||||
bumpItemVersion,
|
||||
getItemVersion,
|
||||
initItemVersion,
|
||||
mockDelay,
|
||||
mockItems,
|
||||
} from "@/lib/api/mock-data"
|
||||
|
||||
export interface ListItemsParams {
|
||||
page?: number
|
||||
@@ -27,139 +20,50 @@ export interface ListItemsParams {
|
||||
q?: string
|
||||
status?: EntityStatus
|
||||
categoryId?: number
|
||||
subCategoryId?: number
|
||||
brandId?: number
|
||||
trackingMode?: TrackingMode
|
||||
}
|
||||
|
||||
function toListItem(item: Item): ItemListItem {
|
||||
return {
|
||||
itemId: item.itemId,
|
||||
sku: item.sku,
|
||||
name: item.name,
|
||||
categoryId: item.categoryId,
|
||||
brandId: item.brandId ?? null,
|
||||
baseUomId: item.baseUomId,
|
||||
defaultVendorId: item.defaultVendorId,
|
||||
itemType: item.itemType,
|
||||
trackingMode: item.trackingMode,
|
||||
taxClass: item.taxClass,
|
||||
status: item.status,
|
||||
}
|
||||
}
|
||||
|
||||
function skuTaken(sku: string, excludeItemId?: number) {
|
||||
return mockItems.some((i) => i.itemId !== excludeItemId && i.sku.toLowerCase() === sku.toLowerCase())
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export const itemsApi = {
|
||||
list(params: ListItemsParams = {}): Promise<PagedResponse<ItemListItem>> {
|
||||
const term = params.q?.trim().toLowerCase()
|
||||
const filtered = mockItems
|
||||
.filter((i) => !params.status || i.status === params.status)
|
||||
.filter((i) => !params.categoryId || i.categoryId === params.categoryId)
|
||||
.filter((i) => !params.trackingMode || i.trackingMode === params.trackingMode)
|
||||
.filter((i) => !term || `${i.sku} ${i.name}`.toLowerCase().includes(term))
|
||||
.sort((a, b) => a.sku.localeCompare(b.sku))
|
||||
.map(toListItem)
|
||||
|
||||
const page = params.page ?? 1
|
||||
const pageSize = params.pageSize ?? 20
|
||||
const start = (page - 1) * pageSize
|
||||
const items = filtered.slice(start, start + pageSize)
|
||||
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page, pageSize, totalItems: filtered.length, totalPages: Math.ceil(filtered.length / pageSize) || 0 },
|
||||
})
|
||||
return apiRequest<PagedResponse<ItemListItem>>(`/items${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
get(itemId: number): Promise<ApiResult<Item>> {
|
||||
const item = mockItems.find((i) => i.itemId === itemId)
|
||||
if (!item) return Promise.reject(new Error(`Mock item ${itemId} not found`))
|
||||
return mockDelay({ data: item, etag: String(getItemVersion(itemId)) })
|
||||
return apiRequestWithETag<Item>(`/items/${itemId}`)
|
||||
},
|
||||
|
||||
/**
|
||||
* 400 SKU_DUPLICATE if the SKU exists; 422 CONFIG_DISABLED if subCategoryId/brandId is
|
||||
* sent while that feature is switched off; 422 if the subcategory belongs to a
|
||||
* different category.
|
||||
*/
|
||||
create(request: CreateItemRequest): Promise<ApiResult<Item>> {
|
||||
const sku = request.sku.trim()
|
||||
if (!sku) return Promise.reject(new Error("SKU is required."))
|
||||
if (skuTaken(sku)) {
|
||||
return Promise.reject(Object.assign(new Error(`SKU "${sku}" already exists.`), { code: "SKU_DUPLICATE" }))
|
||||
}
|
||||
const item: Item = {
|
||||
itemId: allocateItemId(),
|
||||
sku,
|
||||
name: request.name.trim(),
|
||||
description: request.description?.trim() || null,
|
||||
categoryId: request.categoryId,
|
||||
brandId: request.brandId ?? null,
|
||||
baseUomId: request.baseUomId,
|
||||
defaultVendorId: request.defaultVendorId ?? null,
|
||||
itemType: request.itemType,
|
||||
trackingMode: request.trackingMode,
|
||||
taxClass: request.taxClass?.trim() || null,
|
||||
status: "Active",
|
||||
reorder: [],
|
||||
conversions: [],
|
||||
initialQty: request.initialQty ?? null,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: null,
|
||||
}
|
||||
mockItems.push(item)
|
||||
initItemVersion(item.itemId)
|
||||
return mockDelay({ data: item, etag: "1" })
|
||||
return apiRequestWithETag<Item>("/items", { method: "POST", body: request })
|
||||
},
|
||||
|
||||
update(itemId: number, request: UpdateItemRequest, ifMatch: string): Promise<ApiResult<Item>> {
|
||||
const item = mockItems.find((i) => i.itemId === itemId)
|
||||
if (!item) return Promise.reject(new Error(`Mock item ${itemId} not found`))
|
||||
if (String(getItemVersion(itemId)) !== ifMatch) {
|
||||
return Promise.reject(Object.assign(new Error("The item was modified by another request."), { code: "CONCURRENCY_CONFLICT" }))
|
||||
}
|
||||
const sku = request.sku.trim()
|
||||
if (!sku) return Promise.reject(new Error("SKU is required."))
|
||||
if (skuTaken(sku, itemId)) {
|
||||
return Promise.reject(Object.assign(new Error(`SKU "${sku}" already exists.`), { code: "SKU_DUPLICATE" }))
|
||||
}
|
||||
|
||||
item.sku = sku
|
||||
item.name = request.name.trim()
|
||||
item.description = request.description?.trim() || null
|
||||
item.categoryId = request.categoryId
|
||||
item.brandId = request.brandId ?? null
|
||||
item.baseUomId = request.baseUomId
|
||||
item.defaultVendorId = request.defaultVendorId ?? null
|
||||
item.itemType = request.itemType
|
||||
item.trackingMode = request.trackingMode
|
||||
item.taxClass = request.taxClass?.trim() || null
|
||||
item.updatedAt = new Date().toISOString()
|
||||
|
||||
const next = bumpItemVersion(itemId)
|
||||
return mockDelay({ data: item, etag: String(next) })
|
||||
return apiRequestWithETag<Item>(`/items/${itemId}`, { method: "PUT", body: request, ifMatch })
|
||||
},
|
||||
|
||||
/** Deactivate/reactivate — there is no DELETE (FR-MD-08). */
|
||||
updateStatus(itemId: number, status: EntityStatus): Promise<void> {
|
||||
const item = mockItems.find((i) => i.itemId === itemId)
|
||||
if (!item) return Promise.reject(new Error(`Mock item ${itemId} not found`))
|
||||
item.status = status
|
||||
item.updatedAt = new Date().toISOString()
|
||||
bumpItemVersion(itemId)
|
||||
return mockDelay(undefined)
|
||||
return apiRequest<void>(`/items/${itemId}/status`, { method: "PATCH", body: { status } })
|
||||
},
|
||||
|
||||
updateReorder(itemId: number, request: UpdateItemReorderRequest): Promise<{ settings: UpdateItemReorderRequest["settings"] }> {
|
||||
const item = mockItems.find((i) => i.itemId === itemId)
|
||||
if (!item) return Promise.reject(new Error(`Mock item ${itemId} not found`))
|
||||
item.reorder = request.settings
|
||||
item.updatedAt = new Date().toISOString()
|
||||
bumpItemVersion(itemId)
|
||||
return mockDelay({ settings: item.reorder })
|
||||
updateReorder(itemId: number, request: UpdateItemReorderRequest): Promise<{ settings: ItemReorderSetting[] }> {
|
||||
return apiRequest<{ settings: ItemReorderSetting[] }>(`/items/${itemId}/reorder`, {
|
||||
method: "PUT",
|
||||
body: request,
|
||||
})
|
||||
},
|
||||
|
||||
updateUomConversions(itemId: number, request: UpdateUomConversionsRequest): Promise<UpdateUomConversionsResponse> {
|
||||
const item = mockItems.find((i) => i.itemId === itemId)
|
||||
if (!item) return Promise.reject(new Error(`Mock item ${itemId} not found`))
|
||||
item.conversions = request.conversions.map((c, i) => ({ conversionId: 1000 + itemId * 10 + i, fromUom: c.fromUom, toUom: c.toUom, factor: c.factor }))
|
||||
item.updatedAt = new Date().toISOString()
|
||||
bumpItemVersion(itemId)
|
||||
return mockDelay({ itemId: item.itemId, baseUomId: item.baseUomId, conversions: item.conversions })
|
||||
return apiRequest<UpdateUomConversionsResponse>(`/items/${itemId}/uom-conversions`, {
|
||||
method: "PUT",
|
||||
body: request,
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,833 +0,0 @@
|
||||
// In-memory sample data backing every lib/api/*.ts module — the app has no
|
||||
// fetch-based backend connection (lib/api-client.ts and lib/auth-token.ts were
|
||||
// removed). Shapes mirror docs/11-BACKEND-PHASE1.md.
|
||||
import { Bin, Brand, Category, Item, Uom, Vendor, VariantCategory, Warehouse } from "@/types/master-data"
|
||||
import { PurchaseOrder, Quotation, Requisition, Rfq, PurchaseReturn } from "@/types/procurement"
|
||||
import { Grn } from "@/types/grn"
|
||||
import {
|
||||
AdjustmentStatus,
|
||||
CountStatus,
|
||||
CountType,
|
||||
LedgerDirection,
|
||||
LedgerEntry,
|
||||
ReasonCode,
|
||||
TransferStatus,
|
||||
} from "@/types/stock"
|
||||
|
||||
export const mockWarehouses: Warehouse[] = [
|
||||
{ warehouseId: 1, code: "WH-MAIN", name: "Main Warehouse - Negombo" },
|
||||
{ warehouseId: 2, code: "WH-COLOMBO", name: "Colombo Distribution Center" },
|
||||
]
|
||||
|
||||
export const mockBins: Bin[] = [
|
||||
{ binId: 1, warehouseId: 1, code: "A-01-01", binType: "Shelf" },
|
||||
{ binId: 2, warehouseId: 1, code: "A-01-02", binType: "Shelf" },
|
||||
{ binId: 3, warehouseId: 1, code: "B-02-01", binType: "Pallet" },
|
||||
{ binId: 4, warehouseId: 2, code: "C-01-01", binType: "Shelf" },
|
||||
{ binId: 5, warehouseId: 2, code: "C-01-02", binType: "Shelf" },
|
||||
]
|
||||
|
||||
let nextWarehouseId = 3
|
||||
let nextBinId = 6
|
||||
|
||||
export function allocateWarehouseId() {
|
||||
return nextWarehouseId++
|
||||
}
|
||||
|
||||
export function allocateBinId() {
|
||||
return nextBinId++
|
||||
}
|
||||
|
||||
export const mockUoms: Uom[] = [
|
||||
{ uomId: 1, name: "EA" },
|
||||
{ uomId: 2, name: "Box-12" },
|
||||
{ uomId: 3, name: "KG" },
|
||||
]
|
||||
|
||||
let nextUomId = 4
|
||||
|
||||
export function allocateUomId() {
|
||||
return nextUomId++
|
||||
}
|
||||
|
||||
export const mockCategories: Category[] = [
|
||||
{ categoryId: 3, name: "Hardware", parentId: null, createdAt: "2026-06-01T08:00:00Z" },
|
||||
{ categoryId: 12, name: "Fasteners", parentId: 3, createdAt: "2026-06-01T08:05:00Z" },
|
||||
{ categoryId: 20, name: "Power Tools", parentId: null, createdAt: "2026-06-02T09:00:00Z" },
|
||||
]
|
||||
|
||||
let nextCategoryId = 21
|
||||
|
||||
export function allocateCategoryId() {
|
||||
return nextCategoryId++
|
||||
}
|
||||
|
||||
export const mockBrands: Brand[] = [
|
||||
{ brandId: 1, name: "Bosch", createdAt: "2026-06-01T08:00:00Z" },
|
||||
{ brandId: 2, name: "Makita", createdAt: "2026-06-02T09:00:00Z" },
|
||||
]
|
||||
|
||||
let nextBrandId = 3
|
||||
|
||||
export function allocateBrandId() {
|
||||
return nextBrandId++
|
||||
}
|
||||
|
||||
export const mockVariantCategories: VariantCategory[] = [
|
||||
{ variantCategoryId: 1, name: "Color", createdAt: "2026-06-01T08:00:00Z" },
|
||||
{ variantCategoryId: 2, name: "Size", createdAt: "2026-06-01T08:00:00Z" },
|
||||
]
|
||||
|
||||
let nextVariantCategoryId = 3
|
||||
|
||||
export function allocateVariantCategoryId() {
|
||||
return nextVariantCategoryId++
|
||||
}
|
||||
|
||||
export const mockVendors: Vendor[] = [
|
||||
{
|
||||
vendorId: 5,
|
||||
code: "VN-005",
|
||||
name: "Lanka Steel Traders (Pvt) Ltd",
|
||||
terms: "NET30",
|
||||
taxReg: "134567890-7000",
|
||||
currency: "LKR",
|
||||
status: "Active",
|
||||
createdAt: "2026-06-01T08:00:00Z",
|
||||
updatedAt: null,
|
||||
},
|
||||
{
|
||||
vendorId: 8,
|
||||
code: "VN-008",
|
||||
name: "Ceylon Hardware Supplies",
|
||||
terms: "NET45",
|
||||
taxReg: "198765432-1000",
|
||||
currency: "LKR",
|
||||
status: "Active",
|
||||
createdAt: "2026-06-05T08:00:00Z",
|
||||
updatedAt: null,
|
||||
},
|
||||
]
|
||||
|
||||
// A handful more so the vendors list has something real to paginate/search through.
|
||||
const extraVendorNames = [
|
||||
"Colombo Timber & Plywood Co.",
|
||||
"Kandy Electrical Distributors",
|
||||
"Galle Packaging Solutions",
|
||||
"Jaffna Agro Supplies",
|
||||
"Negombo Fasteners (Pvt) Ltd",
|
||||
"Kurunegala Paints & Coatings",
|
||||
"Trinco Marine Hardware",
|
||||
"Ratnapura Gems & Tools",
|
||||
]
|
||||
for (let i = 0; i < extraVendorNames.length; i++) {
|
||||
const vendorId = 9 + i
|
||||
mockVendors.push({
|
||||
vendorId,
|
||||
code: `VN-${String(vendorId).padStart(3, "0")}`,
|
||||
name: extraVendorNames[i],
|
||||
terms: i % 2 === 0 ? "NET30" : "NET60",
|
||||
taxReg: `1${String(10000000 + vendorId * 137)}-${7000 + i}`,
|
||||
currency: "LKR",
|
||||
status: i % 5 === 0 ? "Inactive" : "Active",
|
||||
createdAt: new Date(Date.UTC(2026, 6, 1 + i, 9, 0, 0)).toISOString(),
|
||||
updatedAt: null,
|
||||
})
|
||||
}
|
||||
|
||||
let nextVendorId = 9 + extraVendorNames.length
|
||||
|
||||
export function allocateVendorId() {
|
||||
return nextVendorId++
|
||||
}
|
||||
|
||||
// Concurrency token per vendor (stands in for the real backend's xmin/RowVersion
|
||||
// ETag, docs/11 §1.6) — kept out-of-band since the public Vendor type has no
|
||||
// version field of its own (it travels as an HTTP ETag header, not a body field).
|
||||
const mockVendorVersions = new Map<number, number>(mockVendors.map((v) => [v.vendorId, 1]))
|
||||
|
||||
export function getVendorVersion(vendorId: number): number {
|
||||
return mockVendorVersions.get(vendorId) ?? 1
|
||||
}
|
||||
|
||||
export function bumpVendorVersion(vendorId: number): number {
|
||||
const next = getVendorVersion(vendorId) + 1
|
||||
mockVendorVersions.set(vendorId, next)
|
||||
return next
|
||||
}
|
||||
|
||||
export function initVendorVersion(vendorId: number) {
|
||||
mockVendorVersions.set(vendorId, 1)
|
||||
}
|
||||
|
||||
// Full Item records (docs/11 §2.1). ItemListItem (the list/GRN-picker view) is
|
||||
// derived from these in lib/api/items.ts, same "full record → mapped summary"
|
||||
// pattern as mockPurchaseOrders → PurchaseOrderSummary.
|
||||
export const mockItems: Item[] = [
|
||||
{
|
||||
itemId: 1001,
|
||||
sku: "ITM-1001",
|
||||
name: "Steel Bolt M8x40",
|
||||
description: "Grade 8.8 zinc-plated hex bolt",
|
||||
categoryId: 12,
|
||||
baseUomId: 1,
|
||||
defaultVendorId: 5,
|
||||
itemType: "Stocked",
|
||||
trackingMode: "Batch",
|
||||
taxClass: "STD",
|
||||
status: "Active",
|
||||
reorder: [{ warehouseId: 1, reorderPoint: 2500, reorderQty: 5000 }],
|
||||
conversions: [{ conversionId: 33, fromUom: 2, toUom: 1, factor: 12 }],
|
||||
createdAt: "2026-06-01T08:00:00Z",
|
||||
updatedAt: null,
|
||||
},
|
||||
{
|
||||
itemId: 1002,
|
||||
sku: "ITM-1002",
|
||||
name: "Steel Nut M8",
|
||||
description: "Grade 8 zinc-plated hex nut",
|
||||
categoryId: 12,
|
||||
baseUomId: 1,
|
||||
defaultVendorId: 5,
|
||||
itemType: "Stocked",
|
||||
trackingMode: "None",
|
||||
taxClass: "STD",
|
||||
status: "Active",
|
||||
reorder: [{ warehouseId: 1, reorderPoint: 5000, reorderQty: 8000 }],
|
||||
conversions: [],
|
||||
createdAt: "2026-06-01T08:05:00Z",
|
||||
updatedAt: null,
|
||||
},
|
||||
{
|
||||
itemId: 1003,
|
||||
sku: "ITM-1003",
|
||||
name: "Cordless Drill 18V",
|
||||
description: "18V lithium-ion cordless drill/driver, includes charger",
|
||||
categoryId: 20,
|
||||
baseUomId: 1,
|
||||
defaultVendorId: 8,
|
||||
itemType: "Stocked",
|
||||
trackingMode: "Serial",
|
||||
taxClass: "STD",
|
||||
status: "Active",
|
||||
reorder: [{ warehouseId: 2, reorderPoint: 15, reorderQty: 20 }],
|
||||
conversions: [],
|
||||
createdAt: "2026-06-05T08:10:00Z",
|
||||
updatedAt: null,
|
||||
},
|
||||
]
|
||||
|
||||
let nextItemId = 1004
|
||||
|
||||
export function allocateItemId() {
|
||||
return nextItemId++
|
||||
}
|
||||
|
||||
// Concurrency token per item (same out-of-band ETag pattern as mockVendorVersions).
|
||||
const mockItemVersions = new Map<number, number>(mockItems.map((i) => [i.itemId, 1]))
|
||||
|
||||
export function getItemVersion(itemId: number): number {
|
||||
return mockItemVersions.get(itemId) ?? 1
|
||||
}
|
||||
|
||||
export function bumpItemVersion(itemId: number): number {
|
||||
const next = getItemVersion(itemId) + 1
|
||||
mockItemVersions.set(itemId, next)
|
||||
return next
|
||||
}
|
||||
|
||||
export function initItemVersion(itemId: number) {
|
||||
mockItemVersions.set(itemId, 1)
|
||||
}
|
||||
|
||||
export const mockPurchaseOrders: PurchaseOrder[] = [
|
||||
{
|
||||
poId: 342,
|
||||
docNo: "PO-2026-00342",
|
||||
vendorId: 5,
|
||||
requisitionId: 210,
|
||||
status: "Approved",
|
||||
approvalRequired: false,
|
||||
createdBy: 17,
|
||||
createdAt: "2026-07-07T09:40:00Z",
|
||||
updatedAt: null,
|
||||
totals: { subTotal: 112100.0, tax: 20178.0, grandTotal: 132278.0, currency: "LKR" },
|
||||
lines: [
|
||||
{ poLineId: 900, itemId: 1001, uomId: 1, warehouseId: 1, qty: 5000, unitPrice: 12.5, tax: 0.18, qtyReceived: 0 },
|
||||
{ poLineId: 901, itemId: 1002, uomId: 1, warehouseId: 1, qty: 8000, unitPrice: 6.2, tax: 0.18, qtyReceived: 0 },
|
||||
],
|
||||
},
|
||||
{
|
||||
poId: 350,
|
||||
docNo: "PO-2026-00350",
|
||||
vendorId: 8,
|
||||
requisitionId: null,
|
||||
status: "PartiallyReceived",
|
||||
approvalRequired: false,
|
||||
createdBy: 17,
|
||||
createdAt: "2026-07-09T09:00:00Z",
|
||||
updatedAt: "2026-07-10T11:00:00Z",
|
||||
totals: { subTotal: 22500.0, tax: 4050.0, grandTotal: 26550.0, currency: "LKR" },
|
||||
lines: [
|
||||
{ poLineId: 910, itemId: 1003, uomId: 1, warehouseId: 2, qty: 50, unitPrice: 450.0, tax: 0.18, qtyReceived: 20 },
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
let nextPoId = 351
|
||||
|
||||
export function allocatePoId() {
|
||||
return nextPoId++
|
||||
}
|
||||
|
||||
// Concurrency token per PO (same out-of-band ETag pattern as mockVendorVersions,
|
||||
// docs/11 §1.6) — backs PUT /purchase-orders/{poId}'s If-Match (FR-PROC-05, Option B).
|
||||
const mockPoVersions = new Map<number, number>(mockPurchaseOrders.map((p) => [p.poId, 1]))
|
||||
|
||||
export function getPoVersion(poId: number): number {
|
||||
return mockPoVersions.get(poId) ?? 1
|
||||
}
|
||||
|
||||
export function bumpPoVersion(poId: number): number {
|
||||
const next = getPoVersion(poId) + 1
|
||||
mockPoVersions.set(poId, next)
|
||||
return next
|
||||
}
|
||||
|
||||
export function initPoVersion(poId: number) {
|
||||
mockPoVersions.set(poId, 1)
|
||||
}
|
||||
|
||||
export const mockGrns: Grn[] = [
|
||||
{
|
||||
grnId: 780,
|
||||
docNo: "GRN-2026-00780",
|
||||
poId: 342,
|
||||
vendorId: 5,
|
||||
warehouseId: 1,
|
||||
status: "Draft",
|
||||
createdBy: 17,
|
||||
createdAt: "2026-07-11T10:00:00Z",
|
||||
lines: [
|
||||
{
|
||||
grnLineId: 1300,
|
||||
poLineId: 900,
|
||||
itemId: 1001,
|
||||
uomId: 1,
|
||||
binId: 1,
|
||||
qty: 5000,
|
||||
unitCost: 12.5,
|
||||
receivedValue: 62500.0,
|
||||
holdStatus: "OnHold",
|
||||
batchId: 410,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
grnId: 781,
|
||||
docNo: "GRN-2026-00781",
|
||||
poId: null,
|
||||
vendorId: 8,
|
||||
warehouseId: 2,
|
||||
status: "Confirmed",
|
||||
createdBy: 17,
|
||||
createdAt: "2026-07-10T14:30:00Z",
|
||||
lines: [
|
||||
{
|
||||
grnLineId: 1310,
|
||||
poLineId: null,
|
||||
itemId: 1003,
|
||||
uomId: 1,
|
||||
binId: 4,
|
||||
qty: 5,
|
||||
unitCost: 450.0,
|
||||
receivedValue: 2250.0,
|
||||
holdStatus: "Available",
|
||||
batchId: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
// A handful more so the list screen's pagination/search/filter controls have
|
||||
// something real to page through (10 items total across statuses/warehouses).
|
||||
const extraStatuses: Grn["status"][] = ["Draft", "Confirmed", "Closed", "Confirmed", "Draft", "Confirmed", "Closed", "Draft"]
|
||||
for (let i = 0; i < extraStatuses.length; i++) {
|
||||
const grnId = 782 + i
|
||||
const warehouseId = i % 2 === 0 ? 1 : 2
|
||||
const vendorId = i % 2 === 0 ? 5 : 8
|
||||
const itemId = i % 2 === 0 ? 1001 : 1003
|
||||
const status = extraStatuses[i]
|
||||
mockGrns.push({
|
||||
grnId,
|
||||
docNo: `GRN-2026-${String(grnId).padStart(5, "0")}`,
|
||||
poId: i % 3 === 0 ? null : 342,
|
||||
vendorId,
|
||||
warehouseId,
|
||||
status,
|
||||
createdBy: 17,
|
||||
createdAt: new Date(Date.UTC(2026, 6, 1 + i, 9, 0, 0)).toISOString(),
|
||||
lines: [
|
||||
{
|
||||
grnLineId: 2000 + i,
|
||||
poLineId: i % 3 === 0 ? null : 900,
|
||||
itemId,
|
||||
uomId: 1,
|
||||
binId: warehouseId === 1 ? 1 : 4,
|
||||
qty: 100 * (i + 1),
|
||||
unitCost: 10 + i,
|
||||
receivedValue: 100 * (i + 1) * (10 + i),
|
||||
holdStatus: status === "Draft" ? "OnHold" : "Available",
|
||||
batchId: null,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
let nextGrnId = 782 + extraStatuses.length
|
||||
let nextGrnLineId = 2000 + extraStatuses.length
|
||||
|
||||
export function allocateGrnId() {
|
||||
return nextGrnId++
|
||||
}
|
||||
|
||||
export function allocateGrnLineId() {
|
||||
return nextGrnLineId++
|
||||
}
|
||||
|
||||
/** Small delay so loading states are visible when reviewing the UI. */
|
||||
export function mockDelay<T>(value: T, ms = 300): Promise<T> {
|
||||
return new Promise((resolve) => setTimeout(() => resolve(value), ms))
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Stock Core (FIFO layers + immutable ledger) — docs/10 Part C.5, FR-STK-01..04.
|
||||
// GRN confirm and every stock transaction below post through these helpers so
|
||||
// Stock Enquiry / Ledger / Valuation reflect what actually happened this session.
|
||||
// ============================================================================
|
||||
|
||||
export interface MockStockLayer {
|
||||
layerId: number
|
||||
itemId: number
|
||||
warehouseId: number
|
||||
batchId: number | null
|
||||
serialId: number | null
|
||||
grnLineId: number | null
|
||||
qtyReceived: number
|
||||
qtyRemaining: number
|
||||
unitCost: number
|
||||
receiptDate: string
|
||||
}
|
||||
|
||||
export const mockStockLayers: MockStockLayer[] = []
|
||||
export const mockStockLedger: LedgerEntry[] = []
|
||||
|
||||
let nextLayerId = 9001
|
||||
let nextLedgerId = 55010
|
||||
|
||||
export function allocateLayerId() {
|
||||
return nextLayerId++
|
||||
}
|
||||
|
||||
export function allocateLedgerId() {
|
||||
return nextLedgerId++
|
||||
}
|
||||
|
||||
function round2(n: number) {
|
||||
return Math.round(n * 100) / 100
|
||||
}
|
||||
|
||||
function latestRunningBalance(itemId: number, warehouseId: number): number {
|
||||
for (let i = mockStockLedger.length - 1; i >= 0; i--) {
|
||||
const entry = mockStockLedger[i]
|
||||
if (entry.itemId === itemId && entry.warehouseId === warehouseId) return entry.runningBalance
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
export function postLedgerEntry(input: {
|
||||
itemId: number
|
||||
warehouseId: number
|
||||
binId?: number | null
|
||||
batchId?: number | null
|
||||
serialId?: number | null
|
||||
userId: number
|
||||
direction: LedgerDirection
|
||||
qtyBase: number
|
||||
unitCost: number
|
||||
sourceDocType: string
|
||||
sourceDocId: number
|
||||
}): LedgerEntry {
|
||||
const prior = latestRunningBalance(input.itemId, input.warehouseId)
|
||||
const delta = input.direction === "In" ? input.qtyBase : -input.qtyBase
|
||||
const entry: LedgerEntry = {
|
||||
ledgerId: allocateLedgerId(),
|
||||
itemId: input.itemId,
|
||||
warehouseId: input.warehouseId,
|
||||
binId: input.binId ?? null,
|
||||
batchId: input.batchId ?? null,
|
||||
serialId: input.serialId ?? null,
|
||||
direction: input.direction,
|
||||
qtyBase: input.qtyBase,
|
||||
unitCost: input.unitCost,
|
||||
value: round2(input.qtyBase * input.unitCost),
|
||||
runningBalance: round2(prior + delta),
|
||||
sourceDocType: input.sourceDocType,
|
||||
sourceDocId: input.sourceDocId,
|
||||
userId: input.userId,
|
||||
createdAt: new Date().toISOString(),
|
||||
}
|
||||
mockStockLedger.push(entry)
|
||||
return entry
|
||||
}
|
||||
|
||||
/** Creates a FIFO layer + posts the matching inbound ledger entry (FR-GRN-06 / FR-STK-02). */
|
||||
export function receiveLayer(input: {
|
||||
itemId: number
|
||||
warehouseId: number
|
||||
binId?: number | null
|
||||
batchId?: number | null
|
||||
serialId?: number | null
|
||||
grnLineId?: number | null
|
||||
qty: number
|
||||
unitCost: number
|
||||
userId: number
|
||||
sourceDocType: string
|
||||
sourceDocId: number
|
||||
}): { layer: MockStockLayer; ledger: LedgerEntry } {
|
||||
const layer: MockStockLayer = {
|
||||
layerId: allocateLayerId(),
|
||||
itemId: input.itemId,
|
||||
warehouseId: input.warehouseId,
|
||||
batchId: input.batchId ?? null,
|
||||
serialId: input.serialId ?? null,
|
||||
grnLineId: input.grnLineId ?? null,
|
||||
qtyReceived: input.qty,
|
||||
qtyRemaining: input.qty,
|
||||
unitCost: input.unitCost,
|
||||
receiptDate: new Date().toISOString(),
|
||||
}
|
||||
mockStockLayers.push(layer)
|
||||
const ledger = postLedgerEntry({
|
||||
itemId: input.itemId,
|
||||
warehouseId: input.warehouseId,
|
||||
binId: input.binId,
|
||||
batchId: input.batchId,
|
||||
serialId: input.serialId,
|
||||
userId: input.userId,
|
||||
direction: "In",
|
||||
qtyBase: input.qty,
|
||||
unitCost: input.unitCost,
|
||||
sourceDocType: input.sourceDocType,
|
||||
sourceDocId: input.sourceDocId,
|
||||
})
|
||||
return { layer, ledger }
|
||||
}
|
||||
|
||||
/** 409 STOCK_NEGATIVE_BLOCKED (docs/11 §7) — thrown by consumeFifo when available < requested. */
|
||||
export class StockNegativeError extends Error {
|
||||
code = "STOCK_NEGATIVE_BLOCKED"
|
||||
constructor(itemId: number, warehouseId: number) {
|
||||
super(`Not enough available stock for item #${itemId} at warehouse #${warehouseId}.`)
|
||||
}
|
||||
}
|
||||
|
||||
/** A layer is unavailable while its originating GRN line is still on hold/rejected (docs/10 C.9). */
|
||||
function isLayerOnHold(layer: MockStockLayer): boolean {
|
||||
if (!layer.grnLineId) return false
|
||||
for (const grn of mockGrns) {
|
||||
const line = grn.lines.find((l) => l.grnLineId === layer.grnLineId)
|
||||
if (line) return line.holdStatus === "OnHold" || line.holdStatus === "Rejected"
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/** Consumes the oldest open (non-held) layers first (FR-STK-03); throws StockNegativeError if insufficient. */
|
||||
export function consumeFifo(
|
||||
itemId: number,
|
||||
warehouseId: number,
|
||||
qty: number
|
||||
): { layerId: number; qtyConsumed: number; unitCost: number }[] {
|
||||
let remaining = qty
|
||||
const consumed: { layerId: number; qtyConsumed: number; unitCost: number }[] = []
|
||||
const candidates = mockStockLayers
|
||||
.filter((l) => l.itemId === itemId && l.warehouseId === warehouseId && l.qtyRemaining > 0 && !isLayerOnHold(l))
|
||||
.sort((a, b) => new Date(a.receiptDate).getTime() - new Date(b.receiptDate).getTime())
|
||||
|
||||
for (const layer of candidates) {
|
||||
if (remaining <= 0) break
|
||||
const take = Math.min(layer.qtyRemaining, remaining)
|
||||
layer.qtyRemaining = round2(layer.qtyRemaining - take)
|
||||
remaining = round2(remaining - take)
|
||||
consumed.push({ layerId: layer.layerId, qtyConsumed: take, unitCost: layer.unitCost })
|
||||
}
|
||||
if (remaining > 0.0001) throw new StockNegativeError(itemId, warehouseId)
|
||||
return consumed
|
||||
}
|
||||
|
||||
/** "Last cost" for an adjustment increase (FR-STK-07) when no more specific cost is supplied. */
|
||||
export function lastKnownCost(itemId: number, warehouseId: number): number {
|
||||
const layers = mockStockLayers
|
||||
.filter((l) => l.itemId === itemId && l.warehouseId === warehouseId)
|
||||
.sort((a, b) => new Date(b.receiptDate).getTime() - new Date(a.receiptDate).getTime())
|
||||
return layers[0]?.unitCost ?? 10
|
||||
}
|
||||
|
||||
/**
|
||||
* Consumes stock for a Purchase Return against the specific layer its GRN line created
|
||||
* (FR-PROC-08) — deliberately not routed through consumeFifo: a return disposes of the
|
||||
* exact received batch (often On-hold/Rejected, which consumeFifo's isLayerOnHold filter
|
||||
* would otherwise skip), not just "the oldest open layer for this item/warehouse".
|
||||
* Throws StockNegativeError (409 STOCK_NEGATIVE_BLOCKED, docs/11 §3.4) if the return
|
||||
* qty exceeds what remains on that layer.
|
||||
*/
|
||||
export function consumeLayerByGrnLine(
|
||||
grnLineId: number,
|
||||
qty: number
|
||||
): { layerId: number; qtyConsumed: number; unitCost: number; itemId: number; warehouseId: number } {
|
||||
const layer = mockStockLayers.find((l) => l.grnLineId === grnLineId)
|
||||
if (!layer || layer.qtyRemaining < qty) {
|
||||
const itemId = layer?.itemId ?? 0
|
||||
const warehouseId = layer?.warehouseId ?? 0
|
||||
throw new StockNegativeError(itemId, warehouseId)
|
||||
}
|
||||
layer.qtyRemaining = round2(layer.qtyRemaining - qty)
|
||||
return { layerId: layer.layerId, qtyConsumed: qty, unitCost: layer.unitCost, itemId: layer.itemId, warehouseId: layer.warehouseId }
|
||||
}
|
||||
|
||||
export function computeOnHand(itemId: number, warehouseId: number) {
|
||||
const layers = mockStockLayers.filter((l) => l.itemId === itemId && l.warehouseId === warehouseId)
|
||||
const onHand = round2(layers.reduce((sum, l) => sum + l.qtyRemaining, 0))
|
||||
const onHold = round2(layers.filter(isLayerOnHold).reduce((sum, l) => sum + l.qtyRemaining, 0))
|
||||
const inTransit = round2(
|
||||
mockStockTransfers
|
||||
.filter((t) => t.status === "InTransit" && t.destWarehouseId === warehouseId)
|
||||
.flatMap((t) => t.lines)
|
||||
.filter((l) => l.itemId === itemId)
|
||||
.reduce((sum, l) => sum + l.qty, 0)
|
||||
)
|
||||
const reserved = 0
|
||||
const available = Math.max(0, round2(onHand - onHold - reserved))
|
||||
return { onHand, onHold, inTransit, reserved, available }
|
||||
}
|
||||
|
||||
/** Every item/warehouse combination that currently has (or ever had) a layer — drives the Enquiry screen. */
|
||||
export function knownStockKeys(): { itemId: number; warehouseId: number }[] {
|
||||
const seen = new Map<string, { itemId: number; warehouseId: number }>()
|
||||
for (const layer of mockStockLayers) {
|
||||
seen.set(`${layer.itemId}:${layer.warehouseId}`, { itemId: layer.itemId, warehouseId: layer.warehouseId })
|
||||
}
|
||||
return [...seen.values()]
|
||||
}
|
||||
|
||||
// --- Reference data (docs/11 §6) --------------------------------------------------
|
||||
|
||||
export interface MockItemReorder {
|
||||
itemId: number
|
||||
warehouseId: number
|
||||
reorderPoint: number
|
||||
reorderQty: number
|
||||
}
|
||||
|
||||
export const mockItemReorders: MockItemReorder[] = [
|
||||
{ itemId: 1001, warehouseId: 1, reorderPoint: 2500, reorderQty: 5000 },
|
||||
{ itemId: 1002, warehouseId: 1, reorderPoint: 5000, reorderQty: 8000 },
|
||||
{ itemId: 1003, warehouseId: 2, reorderPoint: 15, reorderQty: 20 },
|
||||
]
|
||||
|
||||
export const mockReasonCodes: ReasonCode[] = [
|
||||
{ reasonCodeId: 1, code: "DMG", description: "Damage", context: "Adjustment" },
|
||||
{ reasonCodeId: 2, code: "LOSS", description: "Theft/Loss", context: "Adjustment" },
|
||||
{ reasonCodeId: 3, code: "CNTVAR", description: "Count Variance", context: "Adjustment" },
|
||||
{ reasonCodeId: 4, code: "EXPWO", description: "Expiry Write-off", context: "Adjustment" },
|
||||
{ reasonCodeId: 5, code: "SYSCORR", description: "System Correction", context: "Adjustment" },
|
||||
{ reasonCodeId: 22, code: "QREJ", description: "Quality Reject", context: "Return" },
|
||||
]
|
||||
|
||||
// --- Seed some prior receipts so Enquiry/Ledger/Valuation aren't empty on first load ---
|
||||
|
||||
receiveLayer({
|
||||
itemId: 1001, warehouseId: 1, binId: 1, batchId: 411, qty: 3000, unitCost: 12.5,
|
||||
userId: 17, sourceDocType: "GRN", sourceDocId: 779,
|
||||
})
|
||||
receiveLayer({
|
||||
itemId: 1002, warehouseId: 1, binId: 2, qty: 6000, unitCost: 6.2,
|
||||
userId: 17, sourceDocType: "GRN", sourceDocId: 779,
|
||||
})
|
||||
receiveLayer({
|
||||
itemId: 1003, warehouseId: 2, binId: 4, qty: 20, unitCost: 450,
|
||||
userId: 17, sourceDocType: "GRN", sourceDocId: 781,
|
||||
})
|
||||
|
||||
// ============================================================================
|
||||
// Transfers (FR-STK-05/06) — create → dispatch (consume src) → receive (create dest).
|
||||
// ============================================================================
|
||||
|
||||
export interface MockTransferLine {
|
||||
transferLineId: number
|
||||
itemId: number
|
||||
srcBinId: number | null
|
||||
destBinId: number | null
|
||||
batchId: number | null
|
||||
qty: number
|
||||
/** Recorded on dispatch so receive() can create cost-preserving destination layers (FR-STK-06). */
|
||||
dispatchedChunks: { layerId: number; qtyConsumed: number; unitCost: number }[]
|
||||
}
|
||||
|
||||
export interface MockStockTransfer {
|
||||
transferId: number
|
||||
docNo: string
|
||||
srcWarehouseId: number
|
||||
destWarehouseId: number
|
||||
status: TransferStatus
|
||||
createdBy: number
|
||||
createdAt: string
|
||||
lines: MockTransferLine[]
|
||||
}
|
||||
|
||||
export const mockStockTransfers: MockStockTransfer[] = []
|
||||
let nextTransferId = 55
|
||||
let nextTransferLineId = 300
|
||||
|
||||
export function allocateTransferId() {
|
||||
return nextTransferId++
|
||||
}
|
||||
|
||||
export function allocateTransferLineId() {
|
||||
return nextTransferLineId++
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Adjustments (FR-STK-07) — auto-post on creation.
|
||||
// ============================================================================
|
||||
|
||||
export interface MockAdjustmentLine {
|
||||
adjLineId: number
|
||||
itemId: number
|
||||
binId: number | null
|
||||
batchId: number | null
|
||||
qtyDelta: number
|
||||
}
|
||||
|
||||
export interface MockStockAdjustment {
|
||||
adjustmentId: number
|
||||
docNo: string
|
||||
warehouseId: number
|
||||
reasonCodeId: number
|
||||
status: AdjustmentStatus
|
||||
createdBy: number
|
||||
createdAt: string
|
||||
lines: MockAdjustmentLine[]
|
||||
ledgerRefs: number[]
|
||||
}
|
||||
|
||||
export const mockStockAdjustments: MockStockAdjustment[] = []
|
||||
let nextAdjustmentId = 77
|
||||
let nextAdjLineId = 210
|
||||
|
||||
export function allocateAdjustmentId() {
|
||||
return nextAdjustmentId++
|
||||
}
|
||||
|
||||
export function allocateAdjLineId() {
|
||||
return nextAdjLineId++
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Counts (FR-STK-08) — snapshot system qty → enter counted qty → post variance.
|
||||
// ============================================================================
|
||||
|
||||
export interface MockCountLine {
|
||||
countLineId: number
|
||||
itemId: number
|
||||
binId: number | null
|
||||
systemQty: number
|
||||
countedQty: number | null
|
||||
variance: number | null
|
||||
}
|
||||
|
||||
export interface MockStockCount {
|
||||
countId: number
|
||||
docNo: string
|
||||
warehouseId: number
|
||||
countType: CountType
|
||||
status: CountStatus
|
||||
createdBy: number
|
||||
createdAt: string
|
||||
lines: MockCountLine[]
|
||||
}
|
||||
|
||||
export const mockStockCounts: MockStockCount[] = []
|
||||
let nextCountId = 30
|
||||
let nextCountLineId = 400
|
||||
|
||||
export function allocateCountId() {
|
||||
return nextCountId++
|
||||
}
|
||||
|
||||
export function allocateCountLineId() {
|
||||
return nextCountLineId++
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Procurement (FR-PROC-01..09) — Requisition → RFQ → Quotations → PO → Return.
|
||||
// No Procurement backend exists yet; same frontend-only posture as GRN/Stock.
|
||||
// ============================================================================
|
||||
|
||||
export const mockRequisitions: Requisition[] = [
|
||||
// Seeded to match mockPurchaseOrders[0].requisitionId (PO-2026-00342 was raised
|
||||
// against this requisition) so the two screens cross-reference consistently.
|
||||
{
|
||||
requisitionId: 210,
|
||||
docNo: "PR-2026-00210",
|
||||
status: "Submitted",
|
||||
requestedBy: 17,
|
||||
createdAt: "2026-07-06T08:30:00Z",
|
||||
lines: [
|
||||
{ reqLineId: 501, itemId: 1001, qty: 5000, requiredBy: "2026-07-20" },
|
||||
{ reqLineId: 502, itemId: 1002, qty: 8000, requiredBy: "2026-07-20" },
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
let nextRequisitionId = 211
|
||||
let nextReqLineId = 503
|
||||
|
||||
export function allocateRequisitionId() {
|
||||
return nextRequisitionId++
|
||||
}
|
||||
|
||||
export function allocateReqLineId() {
|
||||
return nextReqLineId++
|
||||
}
|
||||
|
||||
export const mockRfqs: Rfq[] = []
|
||||
let nextRfqId = 89
|
||||
let nextRfqLineId = 703
|
||||
|
||||
export function allocateRfqId() {
|
||||
return nextRfqId++
|
||||
}
|
||||
|
||||
export function allocateRfqLineId() {
|
||||
return nextRfqLineId++
|
||||
}
|
||||
|
||||
export const mockQuotations: Quotation[] = []
|
||||
let nextQuotationId = 141
|
||||
|
||||
export function allocateQuotationId() {
|
||||
return nextQuotationId++
|
||||
}
|
||||
|
||||
export const mockPurchaseReturns: PurchaseReturn[] = []
|
||||
let nextPurchaseReturnId = 61
|
||||
let nextPurchaseReturnLineId = 121
|
||||
|
||||
export function allocatePurchaseReturnId() {
|
||||
return nextPurchaseReturnId++
|
||||
}
|
||||
|
||||
export function allocatePurchaseReturnLineId() {
|
||||
return nextPurchaseReturnLineId++
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Product configuration (docs/11-BACKEND-PHASE1.md §2.8; FR-MD-11). Singleton — no id.
|
||||
//
|
||||
// `subcategoriesEnabled`/`brandsEnabled` are enforced server-side: an item write carrying
|
||||
// a gated field while its flag is off returns 422 CONFIG_DISABLED. `itemTypesEnabled` is
|
||||
// ADVISORY — items hold no item-type reference, so there is nothing for the server to
|
||||
// reject; this frontend is what honours it by hiding the builder's type section.
|
||||
import { apiRequest, apiRequestWithETag } from "@/lib/api-client"
|
||||
import { ApiResult } from "@/types/common"
|
||||
import { ProductConfig, UpdateProductConfigRequest } from "@/types/master-data"
|
||||
|
||||
export const productConfigApi = {
|
||||
get(): Promise<ApiResult<ProductConfig>> {
|
||||
return apiRequestWithETag<ProductConfig>("/product-config")
|
||||
},
|
||||
|
||||
/** All three flags are required; a partial body is a 400, never a silent disable. */
|
||||
update(request: UpdateProductConfigRequest, ifMatch: string): Promise<ApiResult<ProductConfig>> {
|
||||
return apiRequestWithETag<ProductConfig>("/product-config", { method: "PUT", body: request, ifMatch })
|
||||
},
|
||||
}
|
||||
|
||||
/** Read-only helper for screens that only need the flags. */
|
||||
export function productConfig(): Promise<ProductConfig> {
|
||||
return apiRequest<ProductConfig>("/product-config")
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
// One typed client method per Purchase Order endpoint (docs/11-BACKEND-PHASE1.md §3.3,
|
||||
// FR-PROC-03..07). `get`/`list` also back the GRN "against a PO" picker.
|
||||
// In-memory sample data (lib/api/mock-data.ts) — no backend API calls.
|
||||
import { apiRequest, apiRequestWithETag, buildQuery } from "@/lib/api-client"
|
||||
import { ApiResult, PagedResponse } from "@/types/common"
|
||||
import {
|
||||
CancelPurchaseOrderRequest,
|
||||
@@ -10,14 +10,6 @@ import {
|
||||
PurchaseOrderSummary,
|
||||
UpdatePurchaseOrderRequest,
|
||||
} from "@/types/procurement"
|
||||
import {
|
||||
allocatePoId,
|
||||
bumpPoVersion,
|
||||
getPoVersion,
|
||||
initPoVersion,
|
||||
mockDelay,
|
||||
mockPurchaseOrders,
|
||||
} from "@/lib/api/mock-data"
|
||||
|
||||
export interface ListPurchaseOrdersParams {
|
||||
page?: number
|
||||
@@ -25,138 +17,45 @@ export interface ListPurchaseOrdersParams {
|
||||
q?: string
|
||||
status?: PurchaseOrderStatus
|
||||
vendorId?: number
|
||||
sort?: string
|
||||
}
|
||||
|
||||
/** FR-PROC-05 (Option B): freely editable while open — not once fully received/closed/cancelled. */
|
||||
/** Editable while open (FR-PROC-05, Option B). The server is authoritative — it returns
|
||||
* 409 PO_NOT_EDITABLE regardless — this only drives UI affordances. */
|
||||
export function isPoEditable(status: PurchaseOrderStatus): boolean {
|
||||
return status !== "FullyReceived" && status !== "Closed" && status !== "Cancelled"
|
||||
}
|
||||
|
||||
function computeTotals(lines: CreatePurchaseOrderRequest["lines"], currency = "LKR") {
|
||||
const subTotal = Math.round(lines.reduce((sum, l) => sum + l.qty * l.unitPrice, 0) * 100) / 100
|
||||
const tax = Math.round(lines.reduce((sum, l) => sum + l.qty * l.unitPrice * l.tax, 0) * 100) / 100
|
||||
return { subTotal, tax, grandTotal: Math.round((subTotal + tax) * 100) / 100, currency }
|
||||
}
|
||||
|
||||
export const purchaseOrdersApi = {
|
||||
list(params: ListPurchaseOrdersParams = {}): Promise<PagedResponse<PurchaseOrderSummary>> {
|
||||
const term = params.q?.trim().toLowerCase()
|
||||
const filtered = mockPurchaseOrders
|
||||
.filter((po) => !params.status || po.status === params.status)
|
||||
.filter((po) => !params.vendorId || po.vendorId === params.vendorId)
|
||||
.filter((po) => !term || `${po.docNo} ${po.vendorId}`.toLowerCase().includes(term))
|
||||
.sort((a, b) => b.poId - a.poId)
|
||||
.map(
|
||||
(po): PurchaseOrderSummary => ({
|
||||
poId: po.poId,
|
||||
docNo: po.docNo,
|
||||
vendorId: po.vendorId,
|
||||
status: po.status,
|
||||
approvalRequired: po.approvalRequired,
|
||||
createdAt: po.createdAt,
|
||||
totals: po.totals,
|
||||
})
|
||||
)
|
||||
|
||||
const page = params.page ?? 1
|
||||
const pageSize = params.pageSize ?? 20
|
||||
const start = (page - 1) * pageSize
|
||||
const items = filtered.slice(start, start + pageSize)
|
||||
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page, pageSize, totalItems: filtered.length, totalPages: Math.ceil(filtered.length / pageSize) || 0 },
|
||||
})
|
||||
return apiRequest<PagedResponse<PurchaseOrderSummary>>(`/purchase-orders${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
get(poId: number): Promise<PurchaseOrder> {
|
||||
const po = mockPurchaseOrders.find((p) => p.poId === poId)
|
||||
if (!po) return Promise.reject(new Error(`Mock purchase order ${poId} not found`))
|
||||
return mockDelay(po)
|
||||
return apiRequest<PurchaseOrder>(`/purchase-orders/${poId}`)
|
||||
},
|
||||
|
||||
getWithETag(poId: number): Promise<ApiResult<PurchaseOrder>> {
|
||||
const po = mockPurchaseOrders.find((p) => p.poId === poId)
|
||||
if (!po) return Promise.reject(new Error(`Mock purchase order ${poId} not found`))
|
||||
return mockDelay({ data: po, etag: String(getPoVersion(poId)) })
|
||||
return apiRequestWithETag<PurchaseOrder>(`/purchase-orders/${poId}`)
|
||||
},
|
||||
|
||||
/** Auto-approved on creation in Phase 1 (approvalRequired defaults false, FR-PROC-04). */
|
||||
create(request: CreatePurchaseOrderRequest): Promise<ApiResult<PurchaseOrder>> {
|
||||
if (request.lines.length === 0) return Promise.reject(new Error("Add at least one line."))
|
||||
const poId = allocatePoId()
|
||||
// FR-PROC-04: approvalRequired defaults false → auto-approved on creation.
|
||||
const po: PurchaseOrder = {
|
||||
poId,
|
||||
docNo: `PO-2026-${String(poId).padStart(5, "0")}`,
|
||||
vendorId: request.vendorId,
|
||||
requisitionId: request.requisitionId ?? null,
|
||||
status: "Approved",
|
||||
approvalRequired: false,
|
||||
createdBy: 17,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: null,
|
||||
totals: computeTotals(request.lines),
|
||||
lines: request.lines.map((l, i) => ({
|
||||
poLineId: 900 + poId * 10 + i,
|
||||
itemId: l.itemId,
|
||||
uomId: l.uomId,
|
||||
warehouseId: l.warehouseId,
|
||||
qty: l.qty,
|
||||
unitPrice: l.unitPrice,
|
||||
tax: l.tax,
|
||||
qtyReceived: 0,
|
||||
})),
|
||||
}
|
||||
mockPurchaseOrders.push(po)
|
||||
initPoVersion(poId)
|
||||
return mockDelay({ data: po, etag: "1" })
|
||||
return apiRequestWithETag<PurchaseOrder>("/purchase-orders", { method: "POST", body: request })
|
||||
},
|
||||
|
||||
/** 409 PO_NOT_EDITABLE once fully received/closed/cancelled. */
|
||||
update(poId: number, request: UpdatePurchaseOrderRequest, ifMatch: string): Promise<ApiResult<PurchaseOrder>> {
|
||||
const po = mockPurchaseOrders.find((p) => p.poId === poId)
|
||||
if (!po) return Promise.reject(new Error(`Mock purchase order ${poId} not found`))
|
||||
if (!isPoEditable(po.status)) {
|
||||
return Promise.reject(Object.assign(new Error(`${po.docNo} is ${po.status} and can no longer be edited.`), { code: "PO_NOT_EDITABLE" }))
|
||||
}
|
||||
if (String(getPoVersion(poId)) !== ifMatch) {
|
||||
return Promise.reject(Object.assign(new Error("The purchase order was modified by another request."), { code: "CONCURRENCY_CONFLICT" }))
|
||||
}
|
||||
if (request.lines.length === 0) return Promise.reject(new Error("Add at least one line."))
|
||||
|
||||
const priorQtyReceived = new Map(po.lines.map((l) => [l.poLineId, l.qtyReceived]))
|
||||
po.vendorId = request.vendorId
|
||||
po.requisitionId = request.requisitionId ?? null
|
||||
po.totals = computeTotals(request.lines, po.totals.currency)
|
||||
po.lines = request.lines.map((l, i) => {
|
||||
// Preserve qtyReceived for lines that already existed (edit-while-open must not erase receipt progress).
|
||||
const existingLineId = po.lines[i]?.poLineId
|
||||
return {
|
||||
poLineId: existingLineId ?? 900 + poId * 10 + i,
|
||||
itemId: l.itemId,
|
||||
uomId: l.uomId,
|
||||
warehouseId: l.warehouseId,
|
||||
qty: l.qty,
|
||||
unitPrice: l.unitPrice,
|
||||
tax: l.tax,
|
||||
qtyReceived: existingLineId ? (priorQtyReceived.get(existingLineId) ?? 0) : 0,
|
||||
}
|
||||
})
|
||||
po.updatedAt = new Date().toISOString()
|
||||
|
||||
const next = bumpPoVersion(poId)
|
||||
return mockDelay({ data: po, etag: String(next) })
|
||||
return apiRequestWithETag<PurchaseOrder>(`/purchase-orders/${poId}`, { method: "PUT", body: request, ifMatch })
|
||||
},
|
||||
|
||||
/** No-op in Phase 1 unless approvals are enabled (FR-PROC-04). */
|
||||
approve(poId: number): Promise<PurchaseOrder> {
|
||||
return apiRequest<PurchaseOrder>(`/purchase-orders/${poId}/approve`, { method: "POST" })
|
||||
},
|
||||
|
||||
/** 409 if any receipt exists against the PO. */
|
||||
cancel(poId: number, request: CancelPurchaseOrderRequest): Promise<PurchaseOrder> {
|
||||
const po = mockPurchaseOrders.find((p) => p.poId === poId)
|
||||
if (!po) return Promise.reject(new Error(`Mock purchase order ${poId} not found`))
|
||||
if (po.lines.some((l) => l.qtyReceived > 0)) {
|
||||
return Promise.reject(new Error(`${po.docNo} has receipts against it and can no longer be cancelled.`))
|
||||
}
|
||||
if (!request.reason.trim()) return Promise.reject(new Error("A cancellation reason is required."))
|
||||
po.status = "Cancelled"
|
||||
po.updatedAt = new Date().toISOString()
|
||||
bumpPoVersion(poId)
|
||||
return mockDelay(po)
|
||||
return apiRequest<PurchaseOrder>(`/purchase-orders/${poId}/cancel`, { method: "POST", body: request })
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,93 +1,32 @@
|
||||
// One typed client method per Purchase Return endpoint (docs/11-BACKEND-PHASE1.md §3.4, FR-PROC-08).
|
||||
// In-memory sample data (lib/api/mock-data.ts) — no backend API calls.
|
||||
// One typed client method per Purchase Return endpoint (docs/11-BACKEND-PHASE1.md §3.4,
|
||||
// FR-PROC-08). Auto-posts an outbound FIFO movement on create.
|
||||
import { apiRequest, buildQuery } from "@/lib/api-client"
|
||||
import { PagedResponse } from "@/types/common"
|
||||
import { CreatePurchaseReturnRequest, PurchaseReturn, PurchaseReturnSummary } from "@/types/procurement"
|
||||
import {
|
||||
allocatePurchaseReturnId,
|
||||
allocatePurchaseReturnLineId,
|
||||
consumeLayerByGrnLine,
|
||||
mockDelay,
|
||||
mockPurchaseReturns,
|
||||
postLedgerEntry,
|
||||
} from "@/lib/api/mock-data"
|
||||
|
||||
function toSummary(r: PurchaseReturn): PurchaseReturnSummary {
|
||||
return {
|
||||
returnId: r.returnId,
|
||||
docNo: r.docNo,
|
||||
vendorId: r.vendorId,
|
||||
warehouseId: r.warehouseId,
|
||||
reasonCodeId: r.reasonCodeId,
|
||||
status: r.status,
|
||||
createdAt: r.createdAt,
|
||||
}
|
||||
export interface ListPurchaseReturnsParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
q?: string
|
||||
vendorId?: number
|
||||
warehouseId?: number
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export const purchaseReturnsApi = {
|
||||
list(): Promise<PagedResponse<PurchaseReturnSummary>> {
|
||||
const items = [...mockPurchaseReturns].sort((a, b) => b.returnId - a.returnId).map(toSummary)
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page: 1, pageSize: 20, totalItems: items.length, totalPages: 1 },
|
||||
})
|
||||
list(params: ListPurchaseReturnsParams = {}): Promise<PagedResponse<PurchaseReturnSummary>> {
|
||||
return apiRequest<PagedResponse<PurchaseReturnSummary>>(`/purchase-returns${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
get(returnId: number): Promise<PurchaseReturn> {
|
||||
const r = mockPurchaseReturns.find((x) => x.returnId === returnId)
|
||||
if (!r) return Promise.reject(new Error(`Mock purchase return ${returnId} not found`))
|
||||
return mockDelay(r)
|
||||
return apiRequest<PurchaseReturn>(`/purchase-returns/${returnId}`)
|
||||
},
|
||||
|
||||
/**
|
||||
* 400 REASON_CODE_REQUIRED without a reason; 422 if it is not a Return-context reason;
|
||||
* 409 STOCK_NEGATIVE_BLOCKED if the return exceeds available stock.
|
||||
*/
|
||||
create(request: CreatePurchaseReturnRequest): Promise<PurchaseReturn> {
|
||||
if (!request.reasonCodeId) {
|
||||
return Promise.reject(Object.assign(new Error("A reason code is required for returns."), { code: "REASON_CODE_REQUIRED" }))
|
||||
}
|
||||
if (request.lines.length === 0) return Promise.reject(new Error("Add at least one line."))
|
||||
|
||||
const returnId = allocatePurchaseReturnId()
|
||||
const ledgerRefs: number[] = []
|
||||
const lines: PurchaseReturn["lines"] = []
|
||||
|
||||
try {
|
||||
for (const line of request.lines) {
|
||||
// FR-PROC-08: consumes the exact layer the GRN line created; throws
|
||||
// StockNegativeError (409 STOCK_NEGATIVE_BLOCKED) if qty exceeds it.
|
||||
const chunk = consumeLayerByGrnLine(line.grnLineId, line.qty)
|
||||
const ledger = postLedgerEntry({
|
||||
itemId: line.itemId,
|
||||
warehouseId: chunk.warehouseId,
|
||||
userId: 17,
|
||||
direction: "Out",
|
||||
qtyBase: chunk.qtyConsumed,
|
||||
unitCost: chunk.unitCost,
|
||||
sourceDocType: "PurchaseReturn",
|
||||
sourceDocId: returnId,
|
||||
})
|
||||
ledgerRefs.push(ledger.ledgerId)
|
||||
lines.push({
|
||||
returnLineId: allocatePurchaseReturnLineId(),
|
||||
grnLineId: line.grnLineId,
|
||||
itemId: line.itemId,
|
||||
qty: line.qty,
|
||||
})
|
||||
}
|
||||
} catch (err) {
|
||||
return Promise.reject(err)
|
||||
}
|
||||
|
||||
const purchaseReturn: PurchaseReturn = {
|
||||
returnId,
|
||||
docNo: `PRET-2026-${String(returnId).padStart(5, "0")}`,
|
||||
vendorId: request.vendorId,
|
||||
warehouseId: request.warehouseId,
|
||||
reasonCodeId: request.reasonCodeId,
|
||||
status: "Posted",
|
||||
createdBy: 17,
|
||||
createdAt: new Date().toISOString(),
|
||||
lines,
|
||||
ledgerRefs,
|
||||
}
|
||||
mockPurchaseReturns.push(purchaseReturn)
|
||||
return mockDelay(purchaseReturn)
|
||||
return apiRequest<PurchaseReturn>("/purchase-returns", { method: "POST", body: request })
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
// One typed client method for reference data (docs/11-BACKEND-PHASE1.md §6).
|
||||
// In-memory sample data (lib/api/mock-data.ts) — no backend API calls.
|
||||
import { apiRequest, buildQuery } from "@/lib/api-client"
|
||||
import { PagedResponse } from "@/types/common"
|
||||
import { ReasonCode, ReasonCodeContext } from "@/types/stock"
|
||||
import { mockDelay, mockReasonCodes } from "@/lib/api/mock-data"
|
||||
|
||||
export interface ListReasonCodesParams {
|
||||
context?: ReasonCodeContext
|
||||
page?: number
|
||||
pageSize?: number
|
||||
q?: string
|
||||
}
|
||||
|
||||
export const reasonCodesApi = {
|
||||
list(context?: ReasonCodeContext): Promise<PagedResponse<ReasonCode>> {
|
||||
const items = mockReasonCodes.filter((r) => !context || r.context === context)
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page: 1, pageSize: 20, totalItems: items.length, totalPages: 1 },
|
||||
})
|
||||
list(context?: ReasonCodeContext, params: Omit<ListReasonCodesParams, "context"> = {}): Promise<PagedResponse<ReasonCode>> {
|
||||
return apiRequest<PagedResponse<ReasonCode>>(`/reason-codes${buildQuery({ context, ...params })}`)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,74 +1,31 @@
|
||||
// One typed client method per Requisition endpoint (docs/11-BACKEND-PHASE1.md §3.1, FR-PROC-01).
|
||||
// In-memory sample data (lib/api/mock-data.ts) — no backend API calls.
|
||||
import { apiRequest, buildQuery } from "@/lib/api-client"
|
||||
import { PagedResponse } from "@/types/common"
|
||||
import { CreateRequisitionRequest, Requisition, RequisitionStatus, RequisitionSummary } from "@/types/procurement"
|
||||
import { allocateReqLineId, allocateRequisitionId, mockDelay, mockRequisitions } from "@/lib/api/mock-data"
|
||||
|
||||
export interface ListRequisitionsParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
q?: string
|
||||
status?: RequisitionStatus
|
||||
}
|
||||
|
||||
function toSummary(r: Requisition): RequisitionSummary {
|
||||
return {
|
||||
requisitionId: r.requisitionId,
|
||||
docNo: r.docNo,
|
||||
status: r.status,
|
||||
requestedBy: r.requestedBy,
|
||||
createdAt: r.createdAt,
|
||||
lineCount: r.lines.length,
|
||||
}
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export const requisitionsApi = {
|
||||
list(params: ListRequisitionsParams = {}): Promise<PagedResponse<RequisitionSummary>> {
|
||||
const filtered = mockRequisitions
|
||||
.filter((r) => !params.status || r.status === params.status)
|
||||
.map(toSummary)
|
||||
.sort((a, b) => b.requisitionId - a.requisitionId)
|
||||
|
||||
const page = params.page ?? 1
|
||||
const pageSize = params.pageSize ?? 20
|
||||
const start = (page - 1) * pageSize
|
||||
const items = filtered.slice(start, start + pageSize)
|
||||
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page, pageSize, totalItems: filtered.length, totalPages: Math.ceil(filtered.length / pageSize) || 0 },
|
||||
})
|
||||
return apiRequest<PagedResponse<RequisitionSummary>>(`/requisitions${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
get(requisitionId: number): Promise<Requisition> {
|
||||
const r = mockRequisitions.find((x) => x.requisitionId === requisitionId)
|
||||
if (!r) return Promise.reject(new Error(`Mock requisition ${requisitionId} not found`))
|
||||
return mockDelay(r)
|
||||
return apiRequest<Requisition>(`/requisitions/${requisitionId}`)
|
||||
},
|
||||
|
||||
/** `requestedBy` is stamped from the session, never posted. */
|
||||
create(request: CreateRequisitionRequest): Promise<Requisition> {
|
||||
if (request.lines.length === 0) {
|
||||
return Promise.reject(new Error("A requisition needs at least one line."))
|
||||
}
|
||||
const requisition: Requisition = {
|
||||
requisitionId: allocateRequisitionId(),
|
||||
docNo: "",
|
||||
status: "Draft",
|
||||
requestedBy: 17,
|
||||
createdAt: new Date().toISOString(),
|
||||
lines: request.lines.map((l) => ({ reqLineId: allocateReqLineId(), itemId: l.itemId, qty: l.qty, requiredBy: l.requiredBy })),
|
||||
}
|
||||
requisition.docNo = `PR-2026-${String(requisition.requisitionId).padStart(5, "0")}`
|
||||
mockRequisitions.push(requisition)
|
||||
return mockDelay(requisition)
|
||||
return apiRequest<Requisition>("/requisitions", { method: "POST", body: request })
|
||||
},
|
||||
|
||||
submit(requisitionId: number): Promise<Requisition> {
|
||||
const r = mockRequisitions.find((x) => x.requisitionId === requisitionId)
|
||||
if (!r) return Promise.reject(new Error(`Mock requisition ${requisitionId} not found`))
|
||||
if (r.status !== "Draft") {
|
||||
return Promise.reject(new Error(`${r.docNo} has already been submitted.`))
|
||||
}
|
||||
r.status = "Submitted"
|
||||
return mockDelay(r)
|
||||
return apiRequest<Requisition>(`/requisitions/${requisitionId}/submit`, { method: "POST" })
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
// One typed client method per RFQ/Quotation endpoint (docs/11-BACKEND-PHASE1.md §3.2, FR-PROC-02).
|
||||
// In-memory sample data (lib/api/mock-data.ts) — no backend API calls.
|
||||
//
|
||||
// Note `vendorIds` is validated on create but not persisted — there is no RFQ↔vendor link
|
||||
// in the model, so an Rfq comes back without them; quotations reference vendors directly.
|
||||
import { apiRequest, buildQuery } from "@/lib/api-client"
|
||||
import { PagedResponse } from "@/types/common"
|
||||
import {
|
||||
CreateQuotationRequest,
|
||||
@@ -7,94 +10,38 @@ import {
|
||||
Quotation,
|
||||
Rfq,
|
||||
RfqComparison,
|
||||
RfqComparisonLine,
|
||||
RfqStatus,
|
||||
RfqSummary,
|
||||
} from "@/types/procurement"
|
||||
import {
|
||||
allocateQuotationId,
|
||||
allocateRfqId,
|
||||
allocateRfqLineId,
|
||||
mockDelay,
|
||||
mockQuotations,
|
||||
mockRfqs,
|
||||
} from "@/lib/api/mock-data"
|
||||
|
||||
function toSummary(r: Rfq): RfqSummary {
|
||||
return {
|
||||
rfqId: r.rfqId,
|
||||
docNo: r.docNo,
|
||||
requisitionId: r.requisitionId,
|
||||
status: r.status,
|
||||
vendorIds: r.vendorIds,
|
||||
createdAt: r.createdAt,
|
||||
}
|
||||
export interface ListRfqsParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
q?: string
|
||||
status?: RfqStatus
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export const rfqsApi = {
|
||||
list(): Promise<PagedResponse<RfqSummary>> {
|
||||
const items = [...mockRfqs].sort((a, b) => b.rfqId - a.rfqId).map(toSummary)
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page: 1, pageSize: 20, totalItems: items.length, totalPages: 1 },
|
||||
})
|
||||
list(params: ListRfqsParams = {}): Promise<PagedResponse<RfqSummary>> {
|
||||
return apiRequest<PagedResponse<RfqSummary>>(`/rfqs${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
get(rfqId: number): Promise<Rfq> {
|
||||
const r = mockRfqs.find((x) => x.rfqId === rfqId)
|
||||
if (!r) return Promise.reject(new Error(`Mock RFQ ${rfqId} not found`))
|
||||
return mockDelay(r)
|
||||
return apiRequest<Rfq>(`/rfqs/${rfqId}`)
|
||||
},
|
||||
|
||||
create(request: CreateRfqRequest): Promise<Rfq> {
|
||||
if (request.vendorIds.length === 0) return Promise.reject(new Error("Select at least one vendor."))
|
||||
if (request.lines.length === 0) return Promise.reject(new Error("Add at least one line."))
|
||||
const rfq: Rfq = {
|
||||
rfqId: allocateRfqId(),
|
||||
docNo: "",
|
||||
requisitionId: request.requisitionId ?? null,
|
||||
status: "Open",
|
||||
vendorIds: request.vendorIds,
|
||||
createdAt: new Date().toISOString(),
|
||||
lines: request.lines.map((l) => ({ rfqLineId: allocateRfqLineId(), itemId: l.itemId, qty: l.qty })),
|
||||
}
|
||||
rfq.docNo = `RFQ-2026-${String(rfq.rfqId).padStart(5, "0")}`
|
||||
mockRfqs.push(rfq)
|
||||
return mockDelay(rfq)
|
||||
return apiRequest<Rfq>("/rfqs", { method: "POST", body: request })
|
||||
},
|
||||
|
||||
/** One quotation per vendor per RFQ; a duplicate returns 409. */
|
||||
addQuotation(rfqId: number, request: CreateQuotationRequest): Promise<Quotation> {
|
||||
const rfq = mockRfqs.find((x) => x.rfqId === rfqId)
|
||||
if (!rfq) return Promise.reject(new Error(`Mock RFQ ${rfqId} not found`))
|
||||
if (!rfq.vendorIds.includes(request.vendorId)) {
|
||||
return Promise.reject(new Error("This vendor was not invited to the RFQ."))
|
||||
}
|
||||
const quotation: Quotation = {
|
||||
quotationId: allocateQuotationId(),
|
||||
rfqId,
|
||||
vendorId: request.vendorId,
|
||||
createdAt: new Date().toISOString(),
|
||||
lines: request.lines,
|
||||
}
|
||||
mockQuotations.push(quotation)
|
||||
return mockDelay(quotation)
|
||||
return apiRequest<Quotation>(`/rfqs/${rfqId}/quotations`, { method: "POST", body: request })
|
||||
},
|
||||
|
||||
/** Server-computed vendor-by-line price matrix. */
|
||||
comparison(rfqId: number): Promise<RfqComparison> {
|
||||
const rfq = mockRfqs.find((x) => x.rfqId === rfqId)
|
||||
if (!rfq) return Promise.reject(new Error(`Mock RFQ ${rfqId} not found`))
|
||||
const quotations = mockQuotations.filter((q) => q.rfqId === rfqId)
|
||||
|
||||
const lines: RfqComparisonLine[] = rfq.lines.map((rfqLine) => ({
|
||||
itemId: rfqLine.itemId,
|
||||
qty: rfqLine.qty,
|
||||
cells: quotations
|
||||
.map((q) => {
|
||||
const line = q.lines.find((l) => l.itemId === rfqLine.itemId)
|
||||
return line ? { vendorId: q.vendorId, unitPrice: line.unitPrice, leadDays: line.leadDays } : null
|
||||
})
|
||||
.filter((c): c is { vendorId: number; unitPrice: number; leadDays: number } => c !== null),
|
||||
}))
|
||||
|
||||
return mockDelay({ rfqId, vendorIds: rfq.vendorIds, lines })
|
||||
return apiRequest<RfqComparison>(`/rfqs/${rfqId}/comparison`)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,124 +1,32 @@
|
||||
// One typed client method per adjustment endpoint (docs/11-BACKEND-PHASE1.md §5.5).
|
||||
// In-memory Stock Core (lib/api/mock-data.ts) — no backend API calls.
|
||||
// Auto-posts on create with a mandatory reason code (FR-STK-07).
|
||||
import { apiRequest, buildQuery } from "@/lib/api-client"
|
||||
import { PagedResponse } from "@/types/common"
|
||||
import { AdjustmentStatus, CreateAdjustmentRequest, StockAdjustment, StockAdjustmentSummary } from "@/types/stock"
|
||||
import {
|
||||
allocateAdjLineId,
|
||||
allocateAdjustmentId,
|
||||
consumeFifo,
|
||||
lastKnownCost,
|
||||
mockDelay,
|
||||
mockStockAdjustments,
|
||||
postLedgerEntry,
|
||||
receiveLayer,
|
||||
} from "@/lib/api/mock-data"
|
||||
import { CreateAdjustmentRequest, StockAdjustment, StockAdjustmentSummary } from "@/types/stock"
|
||||
|
||||
export interface ListAdjustmentsParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
q?: string
|
||||
warehouseId?: number
|
||||
}
|
||||
|
||||
function toSummary(a: (typeof mockStockAdjustments)[number]): StockAdjustmentSummary {
|
||||
return {
|
||||
adjustmentId: a.adjustmentId,
|
||||
docNo: a.docNo,
|
||||
warehouseId: a.warehouseId,
|
||||
reasonCodeId: a.reasonCodeId,
|
||||
status: a.status,
|
||||
createdAt: a.createdAt,
|
||||
}
|
||||
reasonCodeId?: number
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export const stockAdjustmentsApi = {
|
||||
list(params: ListAdjustmentsParams = {}): Promise<PagedResponse<StockAdjustmentSummary>> {
|
||||
const filtered = mockStockAdjustments
|
||||
.filter((a) => !params.warehouseId || a.warehouseId === params.warehouseId)
|
||||
.map(toSummary)
|
||||
.sort((a, b) => b.adjustmentId - a.adjustmentId)
|
||||
|
||||
const page = params.page ?? 1
|
||||
const pageSize = params.pageSize ?? 20
|
||||
const start = (page - 1) * pageSize
|
||||
const items = filtered.slice(start, start + pageSize)
|
||||
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page, pageSize, totalItems: filtered.length, totalPages: Math.ceil(filtered.length / pageSize) || 0 },
|
||||
})
|
||||
return apiRequest<PagedResponse<StockAdjustmentSummary>>(`/stock-adjustments${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
get(adjustmentId: number): Promise<StockAdjustment> {
|
||||
const a = mockStockAdjustments.find((x) => x.adjustmentId === adjustmentId)
|
||||
if (!a) return Promise.reject(new Error(`Mock adjustment ${adjustmentId} not found`))
|
||||
return mockDelay(a)
|
||||
return apiRequest<StockAdjustment>(`/stock-adjustments/${adjustmentId}`)
|
||||
},
|
||||
|
||||
/**
|
||||
* 400 REASON_CODE_REQUIRED without a reason; 422 if it is not an Adjustment-context
|
||||
* reason or qtyDelta is 0; 409 STOCK_NEGATIVE_BLOCKED if a decrease exceeds available.
|
||||
*/
|
||||
create(request: CreateAdjustmentRequest): Promise<StockAdjustment> {
|
||||
if (!request.reasonCodeId) {
|
||||
return Promise.reject(Object.assign(new Error("A reason code is required for adjustments."), { code: "REASON_CODE_REQUIRED" }))
|
||||
}
|
||||
|
||||
const adjustmentId = allocateAdjustmentId()
|
||||
const ledgerRefs: number[] = []
|
||||
const lines: StockAdjustment["lines"] = []
|
||||
|
||||
try {
|
||||
for (const line of request.lines) {
|
||||
const adjLineId = allocateAdjLineId()
|
||||
lines.push({ adjLineId, itemId: line.itemId, binId: line.binId ?? null, batchId: line.batchId ?? null, qtyDelta: line.qtyDelta })
|
||||
|
||||
if (line.qtyDelta > 0) {
|
||||
// FR-STK-07: increase creates a layer at the last known cost.
|
||||
const unitCost = lastKnownCost(line.itemId, request.warehouseId)
|
||||
const { ledger } = receiveLayer({
|
||||
itemId: line.itemId,
|
||||
warehouseId: request.warehouseId,
|
||||
binId: line.binId,
|
||||
batchId: line.batchId,
|
||||
qty: line.qtyDelta,
|
||||
unitCost,
|
||||
userId: 17,
|
||||
sourceDocType: "Adjustment",
|
||||
sourceDocId: adjustmentId,
|
||||
})
|
||||
ledgerRefs.push(ledger.ledgerId)
|
||||
} else if (line.qtyDelta < 0) {
|
||||
// Decrease consumes FIFO layers (409 STOCK_NEGATIVE_BLOCKED if insufficient).
|
||||
const chunks = consumeFifo(line.itemId, request.warehouseId, Math.abs(line.qtyDelta))
|
||||
for (const chunk of chunks) {
|
||||
const ledger = postLedgerEntry({
|
||||
itemId: line.itemId,
|
||||
warehouseId: request.warehouseId,
|
||||
binId: line.binId,
|
||||
batchId: line.batchId,
|
||||
userId: 17,
|
||||
direction: "Out",
|
||||
qtyBase: chunk.qtyConsumed,
|
||||
unitCost: chunk.unitCost,
|
||||
sourceDocType: "Adjustment",
|
||||
sourceDocId: adjustmentId,
|
||||
})
|
||||
ledgerRefs.push(ledger.ledgerId)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
return Promise.reject(err)
|
||||
}
|
||||
|
||||
const adjustment = {
|
||||
adjustmentId,
|
||||
docNo: `ADJ-2026-${String(adjustmentId).padStart(5, "0")}`,
|
||||
warehouseId: request.warehouseId,
|
||||
reasonCodeId: request.reasonCodeId,
|
||||
status: "Posted" as AdjustmentStatus,
|
||||
createdBy: 17,
|
||||
createdAt: new Date().toISOString(),
|
||||
lines,
|
||||
ledgerRefs,
|
||||
}
|
||||
mockStockAdjustments.push(adjustment)
|
||||
return mockDelay(adjustment)
|
||||
return apiRequest<StockAdjustment>("/stock-adjustments", { method: "POST", body: request })
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,164 +1,45 @@
|
||||
// One typed client method per count endpoint (docs/11-BACKEND-PHASE1.md §5.6).
|
||||
// In-memory Stock Core (lib/api/mock-data.ts) — no backend API calls.
|
||||
// create (snapshots system qty) -> enterCounts -> post (creates the variance adjustment).
|
||||
import { apiRequest, buildQuery } from "@/lib/api-client"
|
||||
import { PagedResponse } from "@/types/common"
|
||||
import {
|
||||
CountStatus,
|
||||
CreateCountRequest,
|
||||
EnterCountsRequest,
|
||||
EnterCountsResponse,
|
||||
PostCountResponse,
|
||||
StockCount,
|
||||
StockCountSummary,
|
||||
} from "@/types/stock"
|
||||
import {
|
||||
allocateAdjLineId,
|
||||
allocateAdjustmentId,
|
||||
allocateCountId,
|
||||
allocateCountLineId,
|
||||
computeOnHand,
|
||||
consumeFifo,
|
||||
lastKnownCost,
|
||||
mockDelay,
|
||||
mockStockAdjustments,
|
||||
mockStockCounts,
|
||||
postLedgerEntry,
|
||||
receiveLayer,
|
||||
} from "@/lib/api/mock-data"
|
||||
|
||||
export interface ListCountsParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
q?: string
|
||||
status?: CountStatus
|
||||
warehouseId?: number
|
||||
}
|
||||
|
||||
function toSummary(c: (typeof mockStockCounts)[number]): StockCountSummary {
|
||||
return {
|
||||
countId: c.countId,
|
||||
docNo: c.docNo,
|
||||
warehouseId: c.warehouseId,
|
||||
countType: c.countType,
|
||||
status: c.status,
|
||||
createdAt: c.createdAt,
|
||||
}
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export const stockCountsApi = {
|
||||
list(params: ListCountsParams = {}): Promise<PagedResponse<StockCountSummary>> {
|
||||
const filtered = mockStockCounts
|
||||
.filter((c) => !params.warehouseId || c.warehouseId === params.warehouseId)
|
||||
.map(toSummary)
|
||||
.sort((a, b) => b.countId - a.countId)
|
||||
|
||||
const page = params.page ?? 1
|
||||
const pageSize = params.pageSize ?? 20
|
||||
const start = (page - 1) * pageSize
|
||||
const items = filtered.slice(start, start + pageSize)
|
||||
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page, pageSize, totalItems: filtered.length, totalPages: Math.ceil(filtered.length / pageSize) || 0 },
|
||||
})
|
||||
return apiRequest<PagedResponse<StockCountSummary>>(`/stock-counts${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
get(countId: number): Promise<StockCount> {
|
||||
const c = mockStockCounts.find((x) => x.countId === countId)
|
||||
if (!c) return Promise.reject(new Error(`Mock count ${countId} not found`))
|
||||
return mockDelay(c)
|
||||
return apiRequest<StockCount>(`/stock-counts/${countId}`)
|
||||
},
|
||||
|
||||
create(request: CreateCountRequest): Promise<StockCount> {
|
||||
const countId = allocateCountId()
|
||||
const count = {
|
||||
countId,
|
||||
docNo: `CNT-2026-${String(countId).padStart(5, "0")}`,
|
||||
warehouseId: request.warehouseId,
|
||||
countType: request.countType,
|
||||
status: "Draft" as CountStatus,
|
||||
createdBy: 17,
|
||||
createdAt: new Date().toISOString(),
|
||||
lines: request.itemIds.map((itemId) => ({
|
||||
countLineId: allocateCountLineId(),
|
||||
itemId,
|
||||
binId: null,
|
||||
systemQty: computeOnHand(itemId, request.warehouseId).onHand,
|
||||
countedQty: null,
|
||||
variance: null,
|
||||
})),
|
||||
}
|
||||
mockStockCounts.push(count)
|
||||
return mockDelay(count)
|
||||
return apiRequest<StockCount>("/stock-counts", { method: "POST", body: request })
|
||||
},
|
||||
|
||||
enterCounts(countId: number, request: EnterCountsRequest): Promise<EnterCountsResponse> {
|
||||
const c = mockStockCounts.find((x) => x.countId === countId)
|
||||
if (!c) return Promise.reject(new Error(`Mock count ${countId} not found`))
|
||||
|
||||
for (const input of request.lines) {
|
||||
const line = c.lines.find((l) => l.countLineId === input.countLineId)
|
||||
if (!line) continue
|
||||
line.countedQty = input.countedQty
|
||||
line.variance = Math.round((input.countedQty - line.systemQty) * 100) / 100
|
||||
}
|
||||
return mockDelay({ lines: c.lines })
|
||||
/** Returns the whole count (with server-computed variance), not just the lines. */
|
||||
enterCounts(countId: number, request: EnterCountsRequest): Promise<StockCount> {
|
||||
return apiRequest<StockCount>(`/stock-counts/${countId}/counts`, { method: "PUT", body: request })
|
||||
},
|
||||
|
||||
/** Posts the variance adjustment and closes the count. `adjustmentId` is null if there was no variance. */
|
||||
post(countId: number): Promise<PostCountResponse> {
|
||||
const c = mockStockCounts.find((x) => x.countId === countId)
|
||||
if (!c) return Promise.reject(new Error(`Mock count ${countId} not found`))
|
||||
if (c.status === "Posted") return Promise.reject(new Error(`${c.docNo} has already been posted.`))
|
||||
|
||||
const adjustmentId = allocateAdjustmentId()
|
||||
const ledgerRefs: number[] = []
|
||||
const adjLines: { adjLineId: number; itemId: number; binId: number | null; batchId: number | null; qtyDelta: number }[] = []
|
||||
|
||||
for (const line of c.lines) {
|
||||
if (!line.variance) continue
|
||||
adjLines.push({ adjLineId: allocateAdjLineId(), itemId: line.itemId, binId: line.binId, batchId: null, qtyDelta: line.variance })
|
||||
|
||||
if (line.variance > 0) {
|
||||
const unitCost = lastKnownCost(line.itemId, c.warehouseId)
|
||||
const { ledger } = receiveLayer({
|
||||
itemId: line.itemId,
|
||||
warehouseId: c.warehouseId,
|
||||
qty: line.variance,
|
||||
unitCost,
|
||||
userId: 17,
|
||||
sourceDocType: "Count",
|
||||
sourceDocId: c.countId,
|
||||
})
|
||||
ledgerRefs.push(ledger.ledgerId)
|
||||
} else {
|
||||
const chunks = consumeFifo(line.itemId, c.warehouseId, Math.abs(line.variance))
|
||||
for (const chunk of chunks) {
|
||||
const ledger = postLedgerEntry({
|
||||
itemId: line.itemId,
|
||||
warehouseId: c.warehouseId,
|
||||
userId: 17,
|
||||
direction: "Out",
|
||||
qtyBase: chunk.qtyConsumed,
|
||||
unitCost: chunk.unitCost,
|
||||
sourceDocType: "Count",
|
||||
sourceDocId: c.countId,
|
||||
})
|
||||
ledgerRefs.push(ledger.ledgerId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Count Variance reason code (docs/8.3 seed list) — posted as its own adjustment record.
|
||||
mockStockAdjustments.push({
|
||||
adjustmentId,
|
||||
docNo: `ADJ-2026-${String(adjustmentId).padStart(5, "0")}`,
|
||||
warehouseId: c.warehouseId,
|
||||
reasonCodeId: 3,
|
||||
status: "Posted",
|
||||
createdBy: 17,
|
||||
createdAt: new Date().toISOString(),
|
||||
lines: adjLines,
|
||||
ledgerRefs,
|
||||
})
|
||||
|
||||
c.status = "Posted"
|
||||
return mockDelay({ countId: c.countId, status: c.status, adjustmentId, ledgerRefs })
|
||||
return apiRequest<PostCountResponse>(`/stock-counts/${countId}/post`, { method: "POST" })
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
// One typed client method per transfer endpoint (docs/11-BACKEND-PHASE1.md §5.4).
|
||||
// In-memory Stock Core (lib/api/mock-data.ts) — no backend API calls.
|
||||
// create -> dispatch -> receive. The server consumes source FIFO layers on dispatch and
|
||||
// creates the destination layer at the inherited cost on receive (cost-preserving, FR-STK-06).
|
||||
import { apiRequest, buildQuery } from "@/lib/api-client"
|
||||
import { PagedResponse } from "@/types/common"
|
||||
import {
|
||||
CreateTransferRequest,
|
||||
@@ -10,173 +12,39 @@ import {
|
||||
StockTransferSummary,
|
||||
TransferStatus,
|
||||
} from "@/types/stock"
|
||||
import {
|
||||
MockTransferLine,
|
||||
allocateTransferId,
|
||||
allocateTransferLineId,
|
||||
consumeFifo,
|
||||
mockDelay,
|
||||
mockStockTransfers,
|
||||
postLedgerEntry,
|
||||
receiveLayer,
|
||||
} from "@/lib/api/mock-data"
|
||||
|
||||
export interface ListTransfersParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
q?: string
|
||||
status?: TransferStatus
|
||||
srcWarehouseId?: number
|
||||
destWarehouseId?: number
|
||||
}
|
||||
|
||||
function toPublicLine(line: MockTransferLine) {
|
||||
return {
|
||||
transferLineId: line.transferLineId,
|
||||
itemId: line.itemId,
|
||||
srcBinId: line.srcBinId,
|
||||
destBinId: line.destBinId,
|
||||
batchId: line.batchId,
|
||||
qty: line.qty,
|
||||
}
|
||||
}
|
||||
|
||||
function toSummary(t: (typeof mockStockTransfers)[number]): StockTransferSummary {
|
||||
return {
|
||||
transferId: t.transferId,
|
||||
docNo: t.docNo,
|
||||
srcWarehouseId: t.srcWarehouseId,
|
||||
destWarehouseId: t.destWarehouseId,
|
||||
status: t.status,
|
||||
createdAt: t.createdAt,
|
||||
}
|
||||
}
|
||||
|
||||
function toPublic(t: (typeof mockStockTransfers)[number]): StockTransfer {
|
||||
return { ...toSummary(t), createdBy: t.createdBy, lines: t.lines.map(toPublicLine) }
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export const stockTransfersApi = {
|
||||
list(params: ListTransfersParams = {}): Promise<PagedResponse<StockTransferSummary>> {
|
||||
const filtered = mockStockTransfers
|
||||
.filter((t) => !params.status || t.status === params.status)
|
||||
.filter((t) => !params.srcWarehouseId || t.srcWarehouseId === params.srcWarehouseId)
|
||||
.filter((t) => !params.destWarehouseId || t.destWarehouseId === params.destWarehouseId)
|
||||
.map(toSummary)
|
||||
.sort((a, b) => b.transferId - a.transferId)
|
||||
|
||||
const page = params.page ?? 1
|
||||
const pageSize = params.pageSize ?? 20
|
||||
const start = (page - 1) * pageSize
|
||||
const items = filtered.slice(start, start + pageSize)
|
||||
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page, pageSize, totalItems: filtered.length, totalPages: Math.ceil(filtered.length / pageSize) || 0 },
|
||||
})
|
||||
return apiRequest<PagedResponse<StockTransferSummary>>(`/stock-transfers${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
get(transferId: number): Promise<StockTransfer> {
|
||||
const t = mockStockTransfers.find((x) => x.transferId === transferId)
|
||||
if (!t) return Promise.reject(new Error(`Mock transfer ${transferId} not found`))
|
||||
return mockDelay(toPublic(t))
|
||||
return apiRequest<StockTransfer>(`/stock-transfers/${transferId}`)
|
||||
},
|
||||
|
||||
create(request: CreateTransferRequest): Promise<StockTransfer> {
|
||||
const transferId = allocateTransferId()
|
||||
const t = {
|
||||
transferId,
|
||||
docNo: `TRF-2026-${String(transferId).padStart(5, "0")}`,
|
||||
srcWarehouseId: request.srcWarehouseId,
|
||||
destWarehouseId: request.destWarehouseId,
|
||||
status: "Draft" as TransferStatus,
|
||||
createdBy: 17,
|
||||
createdAt: new Date().toISOString(),
|
||||
lines: request.lines.map((l) => ({
|
||||
transferLineId: allocateTransferLineId(),
|
||||
itemId: l.itemId,
|
||||
srcBinId: l.srcBinId ?? null,
|
||||
destBinId: l.destBinId ?? null,
|
||||
batchId: l.batchId ?? null,
|
||||
qty: l.qty,
|
||||
dispatchedChunks: [],
|
||||
})),
|
||||
}
|
||||
mockStockTransfers.push(t)
|
||||
return mockDelay(toPublic(t))
|
||||
return apiRequest<StockTransfer>("/stock-transfers", { method: "POST", body: request })
|
||||
},
|
||||
|
||||
/** 409 STOCK_NEGATIVE_BLOCKED if source available < requested. */
|
||||
dispatch(transferId: number): Promise<DispatchTransferResponse> {
|
||||
const t = mockStockTransfers.find((x) => x.transferId === transferId)
|
||||
if (!t) return Promise.reject(new Error(`Mock transfer ${transferId} not found`))
|
||||
if (t.status !== "Draft") return Promise.reject(new Error(`${t.docNo} has already been dispatched.`))
|
||||
|
||||
const consumedLayers: { layerId: number; qtyConsumed: number; unitCost: number }[] = []
|
||||
const ledgerRefs: number[] = []
|
||||
|
||||
try {
|
||||
for (const line of t.lines) {
|
||||
const chunks = consumeFifo(line.itemId, t.srcWarehouseId, line.qty)
|
||||
line.dispatchedChunks = chunks
|
||||
for (const chunk of chunks) {
|
||||
const ledger = postLedgerEntry({
|
||||
itemId: line.itemId,
|
||||
warehouseId: t.srcWarehouseId,
|
||||
binId: line.srcBinId,
|
||||
batchId: line.batchId,
|
||||
userId: t.createdBy,
|
||||
direction: "Out",
|
||||
qtyBase: chunk.qtyConsumed,
|
||||
unitCost: chunk.unitCost,
|
||||
sourceDocType: "Transfer",
|
||||
sourceDocId: t.transferId,
|
||||
})
|
||||
consumedLayers.push(chunk)
|
||||
ledgerRefs.push(ledger.ledgerId)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
return Promise.reject(err)
|
||||
}
|
||||
|
||||
t.status = "InTransit"
|
||||
return mockDelay({ transferId: t.transferId, status: t.status, consumedLayers, ledgerRefs })
|
||||
return apiRequest<DispatchTransferResponse>(`/stock-transfers/${transferId}/dispatch`, { method: "POST" })
|
||||
},
|
||||
|
||||
receive(transferId: number, lines: ReceiveTransferLineInput[]): Promise<ReceiveTransferResponse> {
|
||||
const t = mockStockTransfers.find((x) => x.transferId === transferId)
|
||||
if (!t) return Promise.reject(new Error(`Mock transfer ${transferId} not found`))
|
||||
if (t.status !== "InTransit") return Promise.reject(new Error(`${t.docNo} is not in transit.`))
|
||||
|
||||
const createdLayers: { layerId: number; warehouseId: number; qtyReceived: number; unitCost: number }[] = []
|
||||
const ledgerRefs: number[] = []
|
||||
|
||||
for (const input of lines) {
|
||||
const line = t.lines.find((l) => l.transferLineId === input.transferLineId)
|
||||
if (!line) continue
|
||||
// Cost-preserving (FR-STK-06): one destination layer per dispatched chunk, at its exact source cost.
|
||||
for (const chunk of line.dispatchedChunks) {
|
||||
const { layer, ledger } = receiveLayer({
|
||||
itemId: line.itemId,
|
||||
warehouseId: t.destWarehouseId,
|
||||
binId: line.destBinId,
|
||||
batchId: line.batchId,
|
||||
qty: chunk.qtyConsumed,
|
||||
unitCost: chunk.unitCost,
|
||||
userId: t.createdBy,
|
||||
sourceDocType: "Transfer",
|
||||
sourceDocId: t.transferId,
|
||||
})
|
||||
createdLayers.push({
|
||||
layerId: layer.layerId,
|
||||
warehouseId: layer.warehouseId,
|
||||
qtyReceived: layer.qtyReceived,
|
||||
unitCost: layer.unitCost,
|
||||
})
|
||||
ledgerRefs.push(ledger.ledgerId)
|
||||
}
|
||||
}
|
||||
|
||||
t.status = "Received"
|
||||
return mockDelay({ transferId: t.transferId, status: t.status, createdLayers, ledgerRefs })
|
||||
return apiRequest<ReceiveTransferResponse>(`/stock-transfers/${transferId}/receive`, {
|
||||
method: "POST",
|
||||
body: { lines },
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,147 +1,60 @@
|
||||
// One typed client method per stock-enquiry endpoint (docs/11-BACKEND-PHASE1.md §5.1-5.3, §5.7).
|
||||
// In-memory Stock Core (lib/api/mock-data.ts) — no backend API calls.
|
||||
import { apiRequest, buildQuery } from "@/lib/api-client"
|
||||
import { PagedResponse } from "@/types/common"
|
||||
import { LedgerEntry, OnHand, ReorderAlert, ReorderRequisitionResponse, Valuation } from "@/types/stock"
|
||||
import {
|
||||
allocateReqLineId,
|
||||
allocateRequisitionId,
|
||||
computeOnHand,
|
||||
knownStockKeys,
|
||||
mockDelay,
|
||||
mockItemReorders,
|
||||
mockRequisitions,
|
||||
mockStockLayers,
|
||||
mockStockLedger,
|
||||
} from "@/lib/api/mock-data"
|
||||
|
||||
export interface LedgerQuery {
|
||||
itemId?: number
|
||||
warehouseId?: number
|
||||
/** `YYYY-MM-DD`. */
|
||||
from?: string
|
||||
to?: string
|
||||
/**
|
||||
* Document-type prefix as stored on the ledger: "GRN", "ADJ", "TRF", "PRET", "CNT"
|
||||
* (Domain/DocumentTypes.cs) — not the friendly name. Pair with sourceDocId to ask
|
||||
* "what movements did this document post?".
|
||||
*/
|
||||
sourceDocType?: string
|
||||
sourceDocId?: number
|
||||
page?: number
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export interface OnHandListParams {
|
||||
itemId?: number
|
||||
warehouseId?: number
|
||||
page?: number
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const stockApi = {
|
||||
onHand(itemId: number, warehouseId: number): Promise<OnHand> {
|
||||
const computed = computeOnHand(itemId, warehouseId)
|
||||
return mockDelay({
|
||||
itemId,
|
||||
warehouseId,
|
||||
...computed,
|
||||
asOf: new Date().toISOString(),
|
||||
})
|
||||
return apiRequest<OnHand>(`/stock/on-hand${buildQuery({ itemId, warehouseId })}`)
|
||||
},
|
||||
|
||||
/** Every item/warehouse pair currently on record — the Enquiry screen's row source. */
|
||||
onHandList(): Promise<OnHand[]> {
|
||||
const rows = knownStockKeys().map(({ itemId, warehouseId }) => ({
|
||||
itemId,
|
||||
warehouseId,
|
||||
...computeOnHand(itemId, warehouseId),
|
||||
asOf: new Date().toISOString(),
|
||||
}))
|
||||
return mockDelay(rows)
|
||||
/** Every (item, warehouse) pair holding stock. Paged, unlike the old client-side version. */
|
||||
onHandList(params: OnHandListParams = {}): Promise<PagedResponse<OnHand>> {
|
||||
return apiRequest<PagedResponse<OnHand>>(`/stock/on-hand/list${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
ledger(params: LedgerQuery): Promise<PagedResponse<LedgerEntry>> {
|
||||
const from = params.from ? new Date(params.from).getTime() : null
|
||||
const to = params.to ? new Date(params.to).getTime() : null
|
||||
|
||||
const filtered = mockStockLedger
|
||||
.filter((e) => !params.itemId || e.itemId === params.itemId)
|
||||
.filter((e) => !params.warehouseId || e.warehouseId === params.warehouseId)
|
||||
.filter((e) => from === null || new Date(e.createdAt).getTime() >= from)
|
||||
.filter((e) => to === null || new Date(e.createdAt).getTime() <= to)
|
||||
.sort((a, b) => b.ledgerId - a.ledgerId)
|
||||
|
||||
const page = params.page ?? 1
|
||||
const pageSize = params.pageSize ?? 20
|
||||
const start = (page - 1) * pageSize
|
||||
const items = filtered.slice(start, start + pageSize)
|
||||
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: {
|
||||
page,
|
||||
pageSize,
|
||||
totalItems: filtered.length,
|
||||
totalPages: pageSize <= 0 ? 0 : Math.ceil(filtered.length / pageSize),
|
||||
},
|
||||
})
|
||||
return apiRequest<PagedResponse<LedgerEntry>>(`/stock/ledger${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
valuation(itemId: number, warehouseId: number): Promise<Valuation> {
|
||||
const layers = mockStockLayers
|
||||
.filter((l) => l.itemId === itemId && l.warehouseId === warehouseId && l.qtyRemaining > 0)
|
||||
.sort((a, b) => new Date(a.receiptDate).getTime() - new Date(b.receiptDate).getTime())
|
||||
.map((l) => ({
|
||||
layerId: l.layerId,
|
||||
qtyRemaining: l.qtyRemaining,
|
||||
unitCost: l.unitCost,
|
||||
value: Math.round(l.qtyRemaining * l.unitCost * 100) / 100,
|
||||
receiptDate: l.receiptDate,
|
||||
}))
|
||||
|
||||
const totalQty = layers.reduce((sum, l) => sum + l.qtyRemaining, 0)
|
||||
const totalValue = Math.round(layers.reduce((sum, l) => sum + l.value, 0) * 100) / 100
|
||||
|
||||
return mockDelay({
|
||||
itemId,
|
||||
warehouseId,
|
||||
layers,
|
||||
totalQty,
|
||||
totalValue,
|
||||
currency: "LKR",
|
||||
costingMethod: "FIFO",
|
||||
})
|
||||
return apiRequest<Valuation>(`/stock/valuation${buildQuery({ itemId, warehouseId })}`)
|
||||
},
|
||||
|
||||
reorderAlerts(warehouseId?: number): Promise<PagedResponse<ReorderAlert>> {
|
||||
const items = mockItemReorders
|
||||
.filter((r) => !warehouseId || r.warehouseId === warehouseId)
|
||||
.map((r) => ({ ...r, available: computeOnHand(r.itemId, r.warehouseId).available }))
|
||||
.filter((r) => r.available <= r.reorderPoint)
|
||||
.map((r) => ({
|
||||
itemId: r.itemId,
|
||||
warehouseId: r.warehouseId,
|
||||
available: r.available,
|
||||
reorderPoint: r.reorderPoint,
|
||||
reorderQty: r.reorderQty,
|
||||
suggestedRequisitionQty: r.reorderQty,
|
||||
}))
|
||||
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page: 1, pageSize: 20, totalItems: items.length, totalPages: 1 },
|
||||
})
|
||||
/** Items at or below their reorder point — computed on read, no stored entity (FR-STK-10). */
|
||||
reorderAlerts(warehouseId?: number, params: { page?: number; pageSize?: number } = {}): Promise<PagedResponse<ReorderAlert>> {
|
||||
return apiRequest<PagedResponse<ReorderAlert>>(`/stock/reorder-alerts${buildQuery({ warehouseId, ...params })}`)
|
||||
},
|
||||
|
||||
/** Creates a draft requisition for the suggested qty; returns the full requisition. */
|
||||
createReorderRequisition(itemId: number, warehouseId: number): Promise<ReorderRequisitionResponse> {
|
||||
const setting = mockItemReorders.find((r) => r.itemId === itemId && r.warehouseId === warehouseId)
|
||||
const qty = setting?.reorderQty ?? 0
|
||||
const requisitionId = allocateRequisitionId()
|
||||
const requiredBy = new Date(Date.now() + 14 * 24 * 60 * 60 * 1000).toISOString().slice(0, 10)
|
||||
|
||||
// Genuinely lands in the Requisitions list (§3), not a fabricated response —
|
||||
// same "wire mock modules together" posture as GRN confirm → Stock Core.
|
||||
mockRequisitions.push({
|
||||
requisitionId,
|
||||
docNo: `PR-2026-${String(requisitionId).padStart(5, "0")}`,
|
||||
status: "Draft",
|
||||
requestedBy: 17,
|
||||
createdAt: new Date().toISOString(),
|
||||
lines: [{ reqLineId: allocateReqLineId(), itemId, qty, requiredBy }],
|
||||
})
|
||||
|
||||
return mockDelay({
|
||||
requisitionId,
|
||||
docNo: `PR-2026-${String(requisitionId).padStart(5, "0")}`,
|
||||
itemId,
|
||||
warehouseId,
|
||||
qty,
|
||||
status: "Draft",
|
||||
})
|
||||
return apiRequest<ReorderRequisitionResponse>(
|
||||
`/stock/reorder-alerts/${itemId}/requisition${buildQuery({ warehouseId })}`,
|
||||
{ method: "POST" },
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,26 +1,22 @@
|
||||
// One typed client method per UOM endpoint (docs/11-BACKEND-PHASE1.md §2.2, FR-MD-02).
|
||||
// `list` also backs the GRN/PO line UOM picker built in earlier sessions.
|
||||
// In-memory sample data (lib/api/mock-data.ts) — no backend API calls.
|
||||
// `list` also backs the GRN/PO line UOM picker.
|
||||
import { apiRequest, buildQuery } from "@/lib/api-client"
|
||||
import { PagedResponse } from "@/types/common"
|
||||
import { CreateUomRequest, Uom } from "@/types/master-data"
|
||||
import { allocateUomId, mockDelay, mockUoms } from "@/lib/api/mock-data"
|
||||
|
||||
export interface ListUomsParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
q?: string
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export const uomsApi = {
|
||||
list(): Promise<PagedResponse<Uom>> {
|
||||
return mockDelay({
|
||||
items: mockUoms,
|
||||
pagination: { page: 1, pageSize: 20, totalItems: mockUoms.length, totalPages: 1 },
|
||||
})
|
||||
list(params: ListUomsParams = {}): Promise<PagedResponse<Uom>> {
|
||||
return apiRequest<PagedResponse<Uom>>(`/uoms${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
create(request: CreateUomRequest): Promise<Uom> {
|
||||
const name = request.name.trim()
|
||||
if (!name) return Promise.reject(new Error("UOM name is required."))
|
||||
if (mockUoms.some((u) => u.name.toLowerCase() === name.toLowerCase())) {
|
||||
return Promise.reject(Object.assign(new Error(`UOM "${name}" already exists.`), { code: "SKU_DUPLICATE" }))
|
||||
}
|
||||
const uom: Uom = { uomId: allocateUomId(), name }
|
||||
mockUoms.push(uom)
|
||||
return mockDelay(uom)
|
||||
return apiRequest<Uom>("/uoms", { method: "POST", body: request })
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
// One typed client method per Variant Category endpoint, mirroring lib/api/brands.ts.
|
||||
// In-memory sample data (lib/api/mock-data.ts) — no backend API calls.
|
||||
import { PagedResponse } from "@/types/common"
|
||||
import { CreateVariantCategoryRequest, UpdateVariantCategoryRequest, VariantCategory } from "@/types/master-data"
|
||||
import { allocateVariantCategoryId, mockVariantCategories, mockDelay } from "@/lib/api/mock-data"
|
||||
|
||||
export const variantCategoriesApi = {
|
||||
list(): Promise<PagedResponse<VariantCategory>> {
|
||||
const items = [...mockVariantCategories].sort((a, b) => a.name.localeCompare(b.name))
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page: 1, pageSize: items.length || 1, totalItems: items.length, totalPages: 1 },
|
||||
})
|
||||
},
|
||||
|
||||
create(request: CreateVariantCategoryRequest): Promise<VariantCategory> {
|
||||
const name = request.name.trim()
|
||||
if (!name) return Promise.reject(new Error("Category name is required."))
|
||||
if (mockVariantCategories.some((c) => c.name.toLowerCase() === name.toLowerCase())) {
|
||||
return Promise.reject(new Error(`Variant category "${name}" already exists.`))
|
||||
}
|
||||
const category: VariantCategory = {
|
||||
variantCategoryId: allocateVariantCategoryId(),
|
||||
name,
|
||||
createdAt: new Date().toISOString(),
|
||||
}
|
||||
mockVariantCategories.push(category)
|
||||
return mockDelay(category)
|
||||
},
|
||||
|
||||
update(variantCategoryId: number, request: UpdateVariantCategoryRequest): Promise<VariantCategory> {
|
||||
const name = request.name.trim()
|
||||
if (!name) return Promise.reject(new Error("Category name is required."))
|
||||
const category = mockVariantCategories.find((c) => c.variantCategoryId === variantCategoryId)
|
||||
if (!category) return Promise.reject(new Error("Variant category not found."))
|
||||
if (mockVariantCategories.some((c) => c.variantCategoryId !== variantCategoryId && c.name.toLowerCase() === name.toLowerCase())) {
|
||||
return Promise.reject(new Error(`Variant category "${name}" already exists.`))
|
||||
}
|
||||
category.name = name
|
||||
return mockDelay(category)
|
||||
},
|
||||
|
||||
remove(variantCategoryId: number): Promise<void> {
|
||||
const index = mockVariantCategories.findIndex((c) => c.variantCategoryId === variantCategoryId)
|
||||
if (index === -1) return Promise.reject(new Error("Variant category not found."))
|
||||
mockVariantCategories.splice(index, 1)
|
||||
return mockDelay(undefined)
|
||||
},
|
||||
}
|
||||
@@ -1,120 +1,36 @@
|
||||
// One typed client method per vendor (supplier) endpoint (docs/11-BACKEND-PHASE1.md
|
||||
// §2.4, FR-MD-06). ETag/If-Match on update, PATCH status for deactivate (masters
|
||||
// are deactivated, not hard-deleted, FR-MD-08). In-memory sample data
|
||||
// (lib/api/mock-data.ts) — no backend API calls.
|
||||
// §2.4, FR-MD-06). ETag/If-Match on update, PATCH status for deactivate (masters are
|
||||
// deactivated, not hard-deleted, FR-MD-08).
|
||||
import { apiRequest, apiRequestWithETag, buildQuery } from "@/lib/api-client"
|
||||
import { ApiResult, EntityStatus, PagedResponse } from "@/types/common"
|
||||
import { Vendor } from "@/types/master-data"
|
||||
import {
|
||||
allocateVendorId,
|
||||
bumpVendorVersion,
|
||||
getVendorVersion,
|
||||
initVendorVersion,
|
||||
mockDelay,
|
||||
mockVendors,
|
||||
} from "@/lib/api/mock-data"
|
||||
import { CreateVendorRequest, UpdateVendorRequest, Vendor } from "@/types/master-data"
|
||||
|
||||
export interface ListVendorsParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
q?: string
|
||||
status?: EntityStatus
|
||||
}
|
||||
|
||||
export interface CreateVendorRequest {
|
||||
code: string
|
||||
name: string
|
||||
terms?: string | null
|
||||
taxReg?: string | null
|
||||
currency: string
|
||||
}
|
||||
|
||||
export type UpdateVendorRequest = CreateVendorRequest
|
||||
|
||||
function codeTaken(code: string, excludeVendorId?: number) {
|
||||
return mockVendors.some((v) => v.vendorId !== excludeVendorId && v.code.toLowerCase() === code.toLowerCase())
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export const vendorsApi = {
|
||||
list(params: ListVendorsParams = {}): Promise<PagedResponse<Vendor>> {
|
||||
const term = params.q?.trim().toLowerCase()
|
||||
const filtered = mockVendors
|
||||
.filter((v) => !params.status || v.status === params.status)
|
||||
.filter((v) => !term || `${v.code} ${v.name}`.toLowerCase().includes(term))
|
||||
|
||||
const page = params.page ?? 1
|
||||
const pageSize = params.pageSize ?? 20
|
||||
const start = (page - 1) * pageSize
|
||||
const items = filtered.slice(start, start + pageSize)
|
||||
const totalItems = filtered.length
|
||||
const totalPages = pageSize <= 0 ? 0 : Math.ceil(totalItems / pageSize)
|
||||
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page, pageSize, totalItems, totalPages },
|
||||
})
|
||||
return apiRequest<PagedResponse<Vendor>>(`/vendors${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
get(vendorId: number): Promise<ApiResult<Vendor>> {
|
||||
const v = mockVendors.find((x) => x.vendorId === vendorId)
|
||||
if (!v) return Promise.reject(new Error(`Mock vendor ${vendorId} not found`))
|
||||
return mockDelay({ data: v, etag: String(getVendorVersion(vendorId)) })
|
||||
return apiRequestWithETag<Vendor>(`/vendors/${vendorId}`)
|
||||
},
|
||||
|
||||
create(request: CreateVendorRequest): Promise<ApiResult<Vendor>> {
|
||||
const code = request.code.trim()
|
||||
if (!code) return Promise.reject(new Error("Vendor code is required."))
|
||||
if (codeTaken(code)) {
|
||||
return Promise.reject(Object.assign(new Error(`Vendor code "${code}" already exists.`), { code: "SKU_DUPLICATE" }))
|
||||
}
|
||||
const vendor: Vendor = {
|
||||
vendorId: allocateVendorId(),
|
||||
code,
|
||||
name: request.name.trim(),
|
||||
terms: request.terms?.trim() || null,
|
||||
taxReg: request.taxReg?.trim() || null,
|
||||
currency: request.currency.trim().toUpperCase(),
|
||||
status: "Active",
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: null,
|
||||
}
|
||||
mockVendors.push(vendor)
|
||||
initVendorVersion(vendor.vendorId)
|
||||
return mockDelay({ data: vendor, etag: "1" })
|
||||
return apiRequestWithETag<Vendor>("/vendors", { method: "POST", body: request })
|
||||
},
|
||||
|
||||
update(vendorId: number, request: UpdateVendorRequest, ifMatch: string): Promise<ApiResult<Vendor>> {
|
||||
const v = mockVendors.find((x) => x.vendorId === vendorId)
|
||||
if (!v) return Promise.reject(new Error(`Mock vendor ${vendorId} not found`))
|
||||
|
||||
if (String(getVendorVersion(vendorId)) !== ifMatch) {
|
||||
return Promise.reject(
|
||||
Object.assign(new Error("The vendor was modified by another request."), { code: "CONCURRENCY_CONFLICT" })
|
||||
)
|
||||
}
|
||||
|
||||
const code = request.code.trim()
|
||||
if (!code) return Promise.reject(new Error("Vendor code is required."))
|
||||
if (codeTaken(code, vendorId)) {
|
||||
return Promise.reject(Object.assign(new Error(`Vendor code "${code}" already exists.`), { code: "SKU_DUPLICATE" }))
|
||||
}
|
||||
|
||||
v.code = code
|
||||
v.name = request.name.trim()
|
||||
v.terms = request.terms?.trim() || null
|
||||
v.taxReg = request.taxReg?.trim() || null
|
||||
v.currency = request.currency.trim().toUpperCase()
|
||||
v.updatedAt = new Date().toISOString()
|
||||
|
||||
const next = bumpVendorVersion(vendorId)
|
||||
return mockDelay({ data: v, etag: String(next) })
|
||||
return apiRequestWithETag<Vendor>(`/vendors/${vendorId}`, { method: "PUT", body: request, ifMatch })
|
||||
},
|
||||
|
||||
updateStatus(vendorId: number, status: EntityStatus): Promise<void> {
|
||||
const v = mockVendors.find((x) => x.vendorId === vendorId)
|
||||
if (!v) return Promise.reject(new Error(`Mock vendor ${vendorId} not found`))
|
||||
v.status = status
|
||||
v.updatedAt = new Date().toISOString()
|
||||
bumpVendorVersion(vendorId)
|
||||
return mockDelay(undefined)
|
||||
return apiRequest<void>(`/vendors/${vendorId}/status`, { method: "PATCH", body: { status } })
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,60 +1,39 @@
|
||||
// One typed client method per warehouse/bin endpoint (docs/11-BACKEND-PHASE1.md §2.5,
|
||||
// FR-MD-07/FR-WH-01). In-memory sample data (lib/api/mock-data.ts) — no backend API calls.
|
||||
// FR-MD-07/FR-WH-01).
|
||||
//
|
||||
// Warehouses are create-and-list only: there is no PUT, no status, and no ETag on them.
|
||||
import { apiRequest, buildQuery } from "@/lib/api-client"
|
||||
import { PagedResponse } from "@/types/common"
|
||||
import { Bin, Warehouse } from "@/types/master-data"
|
||||
import { allocateBinId, allocateWarehouseId, mockBins, mockDelay, mockWarehouses } from "@/lib/api/mock-data"
|
||||
import { Bin, CreateBinRequest, CreateWarehouseRequest, Warehouse } from "@/types/master-data"
|
||||
|
||||
export interface CreateWarehouseRequest {
|
||||
code: string
|
||||
name: string
|
||||
}
|
||||
export type { CreateBinRequest, CreateWarehouseRequest }
|
||||
|
||||
export interface CreateBinRequest {
|
||||
code: string
|
||||
binType?: string | null
|
||||
export interface ListWarehousesParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
q?: string
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export const warehousesApi = {
|
||||
list(): Promise<PagedResponse<Warehouse>> {
|
||||
return mockDelay({
|
||||
items: mockWarehouses,
|
||||
pagination: { page: 1, pageSize: 20, totalItems: mockWarehouses.length, totalPages: 1 },
|
||||
})
|
||||
list(params: ListWarehousesParams = {}): Promise<PagedResponse<Warehouse>> {
|
||||
return apiRequest<PagedResponse<Warehouse>>(`/warehouses${buildQuery(params)}`)
|
||||
},
|
||||
|
||||
get(warehouseId: number): Promise<Warehouse> {
|
||||
const wh = mockWarehouses.find((w) => w.warehouseId === warehouseId)
|
||||
if (!wh) return Promise.reject(new Error(`Mock warehouse ${warehouseId} not found`))
|
||||
return mockDelay(wh)
|
||||
return apiRequest<Warehouse>(`/warehouses/${warehouseId}`)
|
||||
},
|
||||
|
||||
create(request: CreateWarehouseRequest): Promise<Warehouse> {
|
||||
const code = request.code.trim()
|
||||
if (!code) return Promise.reject(new Error("Warehouse code is required."))
|
||||
if (mockWarehouses.some((w) => w.code.toLowerCase() === code.toLowerCase())) {
|
||||
return Promise.reject(Object.assign(new Error(`Warehouse code "${code}" already exists.`), { code: "SKU_DUPLICATE" }))
|
||||
}
|
||||
const warehouse: Warehouse = { warehouseId: allocateWarehouseId(), code, name: request.name.trim() }
|
||||
mockWarehouses.push(warehouse)
|
||||
return mockDelay(warehouse)
|
||||
return apiRequest<Warehouse>("/warehouses", { method: "POST", body: request })
|
||||
},
|
||||
|
||||
listBins(warehouseId: number): Promise<PagedResponse<Bin>> {
|
||||
const items = mockBins.filter((b) => b.warehouseId === warehouseId)
|
||||
return mockDelay({
|
||||
items,
|
||||
pagination: { page: 1, pageSize: 20, totalItems: items.length, totalPages: 1 },
|
||||
})
|
||||
/** Returns a bare array, not a paged envelope — the server does not page bins. */
|
||||
listBins(warehouseId: number): Promise<Bin[]> {
|
||||
return apiRequest<Bin[]>(`/warehouses/${warehouseId}/bins`)
|
||||
},
|
||||
|
||||
createBin(warehouseId: number, request: CreateBinRequest): Promise<Bin> {
|
||||
const code = request.code.trim()
|
||||
if (!code) return Promise.reject(new Error("Bin code is required."))
|
||||
if (mockBins.some((b) => b.warehouseId === warehouseId && b.code.toLowerCase() === code.toLowerCase())) {
|
||||
return Promise.reject(Object.assign(new Error(`Bin code "${code}" already exists in this warehouse.`), { code: "SKU_DUPLICATE" }))
|
||||
}
|
||||
const bin: Bin = { binId: allocateBinId(), warehouseId, code, binType: request.binType?.trim() || null }
|
||||
mockBins.push(bin)
|
||||
return mockDelay(bin)
|
||||
return apiRequest<Bin>(`/warehouses/${warehouseId}/bins`, { method: "POST", body: request })
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,20 +1,29 @@
|
||||
// "Wastage" is not a distinct document type in docs/10-BACKEND-PHASE1.md or the
|
||||
// SRS — stock write-offs (damage, theft/loss, expiry) are modeled as Stock
|
||||
// Adjustments with a mandatory reason code (FR-STK-07), and the reason-code seed
|
||||
// list (docs/8.3 / docs/11 §6) already includes Damage / Theft-Loss / Expiry
|
||||
// Write-off. This module is a frontend-only lens: it reuses stockAdjustmentsApi
|
||||
// and the shared mock Stock Core, filtered to loss-type reason codes and
|
||||
// flattened to per-line records for a focused "record wastage" flow and report.
|
||||
// No new backend concept, no new mock store.
|
||||
// "Wastage" is not a distinct document type in docs/10-BACKEND-PHASE1.md or the SRS —
|
||||
// stock write-offs (damage, theft/loss, expiry) are modelled as Stock Adjustments with a
|
||||
// mandatory reason code (FR-STK-07), and the seeded reason codes (docs/10 §B.8.3) already
|
||||
// include Damage / Theft-Loss / Expiry Write-off. This module is a frontend-only lens over
|
||||
// stockAdjustmentsApi: filtered to loss-type reasons and flattened to per-line records for
|
||||
// a focused "record wastage" flow and report. No new backend concept.
|
||||
import { reasonCodesApi } from "@/lib/api/reason-codes"
|
||||
import { stockAdjustmentsApi } from "@/lib/api/stock-adjustments"
|
||||
import { mockDelay, mockReasonCodes, mockStockAdjustments, mockStockLedger } from "@/lib/api/mock-data"
|
||||
import { stockApi } from "@/lib/api/stock"
|
||||
import { StockAdjustment } from "@/types/stock"
|
||||
|
||||
/** Reason-code strings treated as "wastage" (loss-type) causes, per docs/8.3. */
|
||||
const WASTAGE_CODES = new Set(["DMG", "LOSS", "EXPWO"])
|
||||
/**
|
||||
* Reason codes treated as "wastage" (loss-type) causes. These match the seeded codes in
|
||||
* Infra/Persistence/DataSeeder.cs — DMG/THEFT/EXP. (`VAR` count-variance and `SYS`
|
||||
* corrections are adjustments too, but they are not losses, so they stay out.)
|
||||
*/
|
||||
const WASTAGE_CODES = new Set(["DMG", "THEFT", "EXP"])
|
||||
|
||||
export function wastageReasonCodeIds(): number[] {
|
||||
return mockReasonCodes.filter((r) => WASTAGE_CODES.has(r.code)).map((r) => r.reasonCodeId)
|
||||
/** Sync predicate for screens that already hold the reason-code list. */
|
||||
export function isWastageReasonCode(code: string): boolean {
|
||||
return WASTAGE_CODES.has(code)
|
||||
}
|
||||
|
||||
export async function wastageReasonCodeIds(): Promise<number[]> {
|
||||
const res = await reasonCodesApi.list("Adjustment", { pageSize: 200 })
|
||||
return res.items.filter((r) => isWastageReasonCode(r.code)).map((r) => r.reasonCodeId)
|
||||
}
|
||||
|
||||
export interface WastageRecord {
|
||||
@@ -28,13 +37,15 @@ export interface WastageRecord {
|
||||
reasonCodeId: number
|
||||
/** Positive quantity wasted (the underlying adjustment line is a negative qtyDelta). */
|
||||
qty: number
|
||||
/** FIFO cost of the wasted quantity, summed from the matching outbound ledger entries. */
|
||||
/** FIFO cost of the wasted quantity, summed from the ledger entries this adjustment posted. */
|
||||
value: number
|
||||
}
|
||||
|
||||
export interface ListWastageParams {
|
||||
warehouseId?: number
|
||||
reasonCodeId?: number
|
||||
page?: number
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export interface RecordWastageInput {
|
||||
@@ -46,45 +57,58 @@ export interface RecordWastageInput {
|
||||
}
|
||||
|
||||
export const wastageApi = {
|
||||
list(params: ListWastageParams = {}): Promise<WastageRecord[]> {
|
||||
const wastageIds = new Set(wastageReasonCodeIds())
|
||||
const records: WastageRecord[] = []
|
||||
/**
|
||||
* Flattens loss-type adjustments to per-item records.
|
||||
*
|
||||
* Costs come from the ledger rather than being recomputed here: FIFO consumption is the
|
||||
* server's job, and the browser cannot see the layers (docs/20 §3.3). One ledger call per
|
||||
* adjustment is the price of the ledger's polymorphic source reference — acceptable
|
||||
* because the adjustment page is already bounded.
|
||||
*/
|
||||
async list(params: ListWastageParams = {}): Promise<WastageRecord[]> {
|
||||
const wastageIds = new Set(await wastageReasonCodeIds())
|
||||
if (wastageIds.size === 0) return []
|
||||
|
||||
for (const adj of mockStockAdjustments) {
|
||||
if (!wastageIds.has(adj.reasonCodeId)) continue
|
||||
if (params.warehouseId && adj.warehouseId !== params.warehouseId) continue
|
||||
if (params.reasonCodeId && adj.reasonCodeId !== params.reasonCodeId) continue
|
||||
// The API filters by a single reasonCodeId, so a specific pick can be pushed down;
|
||||
// otherwise fetch the page and keep the loss-type rows.
|
||||
const res = await stockAdjustmentsApi.list({
|
||||
warehouseId: params.warehouseId,
|
||||
reasonCodeId: params.reasonCodeId,
|
||||
page: params.page,
|
||||
pageSize: params.pageSize ?? 50,
|
||||
})
|
||||
const summaries = res.items.filter((a) => wastageIds.has(a.reasonCodeId))
|
||||
|
||||
for (const line of adj.lines) {
|
||||
if (line.qtyDelta >= 0) continue // wastage is always a decrease
|
||||
const detailed = await Promise.all(
|
||||
summaries.map(async (summary) => {
|
||||
const [adjustment, ledger] = await Promise.all([
|
||||
stockAdjustmentsApi.get(summary.adjustmentId),
|
||||
stockApi.ledger({ sourceDocType: "ADJ", sourceDocId: summary.adjustmentId, pageSize: 200 }),
|
||||
])
|
||||
|
||||
const value = mockStockLedger
|
||||
.filter(
|
||||
(l) =>
|
||||
l.sourceDocType === "Adjustment" &&
|
||||
l.sourceDocId === adj.adjustmentId &&
|
||||
l.itemId === line.itemId &&
|
||||
l.direction === "Out"
|
||||
)
|
||||
.reduce((sum, l) => sum + l.value, 0)
|
||||
return adjustment.lines
|
||||
.filter((line) => line.qtyDelta < 0) // wastage is always a decrease
|
||||
.map<WastageRecord>((line) => {
|
||||
const value = ledger.items
|
||||
.filter((l) => l.itemId === line.itemId && l.direction === "Out")
|
||||
.reduce((sum, l) => sum + l.value, 0)
|
||||
return {
|
||||
adjustmentId: adjustment.adjustmentId,
|
||||
adjLineId: line.adjLineId,
|
||||
docNo: adjustment.docNo,
|
||||
createdAt: adjustment.createdAt,
|
||||
warehouseId: adjustment.warehouseId,
|
||||
itemId: line.itemId,
|
||||
binId: line.binId,
|
||||
reasonCodeId: adjustment.reasonCodeId,
|
||||
qty: Math.abs(line.qtyDelta),
|
||||
value: Math.round(value * 100) / 100,
|
||||
}
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
records.push({
|
||||
adjustmentId: adj.adjustmentId,
|
||||
adjLineId: line.adjLineId,
|
||||
docNo: adj.docNo,
|
||||
createdAt: adj.createdAt,
|
||||
warehouseId: adj.warehouseId,
|
||||
itemId: line.itemId,
|
||||
binId: line.binId,
|
||||
reasonCodeId: adj.reasonCodeId,
|
||||
qty: Math.abs(line.qtyDelta),
|
||||
value: Math.round(value * 100) / 100,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
records.sort((a, b) => b.adjustmentId - a.adjustmentId)
|
||||
return mockDelay(records)
|
||||
return detailed.flat().sort((a, b) => b.adjustmentId - a.adjustmentId)
|
||||
},
|
||||
|
||||
/** Records wastage as a single-line, negative-qtyDelta stock adjustment (FR-STK-07). */
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
// Client-side cache of the signed-in user's PROFILE, for display only.
|
||||
//
|
||||
// This is not an auth mechanism and holds no credentials: the session is the httpOnly
|
||||
// `erp_at` cookie, which JS cannot read and which the API validates on every call. This
|
||||
// exists only because there is no `GET /auth/me` endpoint — the user object arrives once,
|
||||
// in the login/register response (docs/11 §2.0) — and the Header needs a name to show.
|
||||
//
|
||||
// Treat it as untrusted display data. Clearing it does not log anyone out; only the
|
||||
// server clearing the cookie does that.
|
||||
import { AuthUser } from "@/types/auth"
|
||||
|
||||
const KEY = "erpcore.user"
|
||||
|
||||
export function setStoredUser(user: AuthUser | null): void {
|
||||
if (typeof window === "undefined") return
|
||||
if (!user) {
|
||||
window.localStorage.removeItem(KEY)
|
||||
return
|
||||
}
|
||||
window.localStorage.setItem(KEY, JSON.stringify(user))
|
||||
}
|
||||
|
||||
export function getStoredUser(): AuthUser | null {
|
||||
if (typeof window === "undefined") return null
|
||||
const raw = window.localStorage.getItem(KEY)
|
||||
if (!raw) return null
|
||||
try {
|
||||
return JSON.parse(raw) as AuthUser
|
||||
} catch {
|
||||
// Corrupt/legacy value — drop it rather than crash the shell.
|
||||
window.localStorage.removeItem(KEY)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function clearStoredUser(): void {
|
||||
setStoredUser(null)
|
||||
}
|
||||
|
||||
/** Best display name available, falling back through the fields AuthHex may leave null. */
|
||||
export function displayName(user: AuthUser | null): string {
|
||||
if (!user) return "Signed in"
|
||||
return user.fullname?.trim() || user.userName?.trim() || user.email?.trim() || "Signed in"
|
||||
}
|
||||
@@ -8,6 +8,14 @@ interface ApiErrorLike {
|
||||
errors?: Record<string, string[]>
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic framework codes. Unlike the domain codes below, these say nothing on their own —
|
||||
* the server's `detail` ("A brand named 'bosch' already exists.") is always more useful
|
||||
* than "This action conflicts…", so for these the detail wins and the text here is only a
|
||||
* last resort.
|
||||
*/
|
||||
const GENERIC_CODES = new Set(["validation_error", "not_found", "conflict"])
|
||||
|
||||
const CODE_MESSAGES: Record<string, string> = {
|
||||
OVER_RECEIPT_TOLERANCE: "This quantity exceeds the purchase order's open quantity beyond the allowed tolerance.",
|
||||
STOCK_NEGATIVE_BLOCKED: "Not enough available stock for this action.",
|
||||
@@ -28,8 +36,10 @@ const CODE_MESSAGES: Record<string, string> = {
|
||||
export function errorMessage(error: unknown): string {
|
||||
if (error && typeof error === "object") {
|
||||
const e = error as ApiErrorLike
|
||||
if (e.code && CODE_MESSAGES[e.code]) return CODE_MESSAGES[e.code]
|
||||
// A specific domain code beats the server's prose; a generic one loses to it.
|
||||
if (e.code && !GENERIC_CODES.has(e.code) && CODE_MESSAGES[e.code]) return CODE_MESSAGES[e.code]
|
||||
if (e.detail) return e.detail
|
||||
if (e.code && CODE_MESSAGES[e.code]) return CODE_MESSAGES[e.code]
|
||||
}
|
||||
if (error instanceof Error) return error.message
|
||||
return "Something went wrong."
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
import { z } from "zod"
|
||||
|
||||
// Treats null/undefined as missing so validation surfaces one clear
|
||||
// "required" message instead of a generic type error.
|
||||
// Plain z.string(), not z.preprocess(): preprocess widens the schema's INPUT type to
|
||||
// `unknown`, so zodResolver produced a Resolver<{email: unknown, …}> that could not be
|
||||
// assigned to useForm<LoginValues> — the long-standing type error on the login page.
|
||||
// Form fields always yield strings (RHF defaults them to ""), so the null/undefined
|
||||
// coercion it was guarding against cannot occur here.
|
||||
function requiredString(message: string) {
|
||||
return z.preprocess(
|
||||
(val) => (val === null || val === undefined ? "" : val),
|
||||
z.string().min(1, message)
|
||||
)
|
||||
return z.string().min(1, message)
|
||||
}
|
||||
|
||||
// Email validation schema
|
||||
export const emailSchema = z.preprocess(
|
||||
(val) => (val === null || val === undefined ? "" : val),
|
||||
z.string().min(1, "Email is required").email("Enter a valid email")
|
||||
)
|
||||
export const emailSchema = z.string().min(1, "Email is required").email("Enter a valid email")
|
||||
|
||||
// Login schema (email + password) for reuse across the app
|
||||
export const loginSchema = z.object({
|
||||
|
||||
@@ -57,9 +57,10 @@ export function validateBrandName(name: string): Record<string, string> {
|
||||
return errors
|
||||
}
|
||||
|
||||
export function validateVariantCategoryName(name: string): Record<string, string> {
|
||||
/** Renamed from validateVariantCategoryName — "variant categories" are Item Types now. */
|
||||
export function validateItemTypeName(name: string): Record<string, string> {
|
||||
const errors: Record<string, string> = {}
|
||||
if (!name.trim()) errors.name = "Category name is required"
|
||||
if (!name.trim()) errors.name = "Item type name is required"
|
||||
return errors
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user