FPSMS-frontend
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 

189 rader
4.8 KiB

  1. "use server";
  2. import {
  3. ServerFetchError,
  4. serverFetchJson,
  5. serverFetchWithNoContent,
  6. } from "@/app/utils/fetchUtil";
  7. import { revalidateTag, revalidatePath } from "next/cache";
  8. import { BASE_API_URL } from "@/config/api";
  9. import { CreateItemResponse, RecordsRes } from "../../utils";
  10. import { ItemQc, ItemsResult } from ".";
  11. import { QcChecksInputs } from "../qcCheck/actions";
  12. import { cache } from "react";
  13. // export type TypeInputs = {
  14. // id: number;
  15. // name: string
  16. // }
  17. // export type UomInputs = {
  18. // uom: string
  19. // }
  20. // export type WeightUnitInputs = {
  21. // weightUnit: string
  22. // conversion: number
  23. // }
  24. export type CreateItemInputs = {
  25. id?: string | number;
  26. code: string;
  27. name: string;
  28. description?: string | undefined;
  29. remarks?: string | undefined;
  30. shelfLife?: number | undefined;
  31. countryOfOrigin?: string | undefined;
  32. maxQty: number;
  33. type: string;
  34. qcChecks: QcChecksInputs[];
  35. qcChecks_active: number[];
  36. qcCategoryId: number | undefined;
  37. store_id?: string | undefined;
  38. warehouse?: string | undefined;
  39. area?: string | undefined;
  40. slot?: string | undefined;
  41. LocationCode?: string | undefined;
  42. isEgg?: boolean | undefined;
  43. isFee?: boolean | undefined;
  44. isBag?: boolean | undefined;
  45. qcType?: string | undefined;
  46. };
  47. export const saveItem = async (data: CreateItemInputs) => {
  48. const item = await serverFetchJson<CreateItemResponse<CreateItemInputs>>(
  49. `${BASE_API_URL}/items/new`,
  50. {
  51. method: "POST",
  52. body: JSON.stringify(data),
  53. headers: { "Content-Type": "application/json" },
  54. },
  55. );
  56. revalidateTag("items");
  57. return item;
  58. };
  59. export const deleteItem = async (id: number) => {
  60. const response = await serverFetchJson<ItemsResult>(
  61. `${BASE_API_URL}/items/${id}`,
  62. {
  63. method: "DELETE",
  64. headers: { "Content-Type": "application/json" },
  65. },
  66. );
  67. revalidateTag("items");
  68. revalidatePath("/(main)/settings/items");
  69. return response;
  70. };
  71. export interface ItemCombo {
  72. id: number,
  73. label: string,
  74. uomId: number,
  75. uom: string,
  76. uomDesc: string,
  77. group?: string,
  78. currentStockBalance?: number,
  79. }
  80. export interface ItemWithDetails {
  81. id: number;
  82. code: string;
  83. name: string;
  84. description?: string;
  85. uomId: number;
  86. uom: string;
  87. uomDesc: string;
  88. currentStockBalance: number;
  89. }
  90. export interface PickUomOption {
  91. uomId: number;
  92. uomCode: string;
  93. uomDesc: string;
  94. }
  95. export interface AvailablePickUomsResponse {
  96. itemId: number;
  97. stockUom: PickUomOption | null;
  98. options: PickUomOption[];
  99. }
  100. /** FP-MTMS Version Checklist | Functions Ref. No. 35 | v1.0.1 | 2026-07-22 */
  101. export const fetchAvailablePickUoms = cache(async (itemId: number) => {
  102. return serverFetchJson<AvailablePickUomsResponse>(
  103. `${BASE_API_URL}/items/${itemId}/available-pick-uoms`,
  104. {
  105. method: "GET",
  106. next: { tags: ["items"] },
  107. },
  108. );
  109. });
  110. export const fetchItemsWithDetails = cache(async (searchParams?: Record<string, any>) => {
  111. if (searchParams) {
  112. const queryString = new URLSearchParams(searchParams).toString();
  113. return serverFetchJson<RecordsRes<ItemWithDetails>>(
  114. `${BASE_API_URL}/items/itemsWithDetails?${queryString}`,
  115. {
  116. next: { tags: ["items"] },
  117. }
  118. );
  119. } else {
  120. return serverFetchJson<RecordsRes<ItemWithDetails>>(
  121. `${BASE_API_URL}/items/itemsWithDetails`,
  122. {
  123. next: { tags: ["items"] },
  124. }
  125. );
  126. }
  127. });
  128. /** Paged item lookup for Inventory Search (code / name). */
  129. export const fetchItemsByPage = cache(async (queryParams?: Record<string, string | number>) => {
  130. const searchParams = new URLSearchParams();
  131. if (queryParams) {
  132. Object.entries(queryParams).forEach(([key, value]) => {
  133. if (value !== undefined && value !== null && `${value}` !== "") {
  134. searchParams.set(key, String(value));
  135. }
  136. });
  137. }
  138. const queryString = searchParams.toString();
  139. return serverFetchJson<RecordsRes<ItemsResult>>(
  140. queryString
  141. ? `${BASE_API_URL}/items/getRecordByPage?${queryString}`
  142. : `${BASE_API_URL}/items/getRecordByPage`,
  143. {
  144. method: "GET",
  145. next: { tags: ["items"] },
  146. },
  147. );
  148. });
  149. export const fetchAllItemsInClient = cache(async () => {
  150. return serverFetchJson<ItemCombo[]>(`${BASE_API_URL}/items/consumables`, {
  151. next: { tags: ["items"] },
  152. });
  153. });
  154. export const fetchPickOrderItemsByPageClient = cache(
  155. async (queryParams?: Record<string, any>) => {
  156. if (queryParams) {
  157. const queryString = new URLSearchParams(queryParams).toString();
  158. return serverFetchJson<RecordsRes<any>>(
  159. `${BASE_API_URL}/items/pickOrderItems?${queryString}`,
  160. {
  161. method: "GET",
  162. next: { tags: ["pickorder"] },
  163. },
  164. );
  165. } else {
  166. return serverFetchJson<RecordsRes<any>>(
  167. `${BASE_API_URL}/items/pickOrderItems`,
  168. {
  169. method: "GET",
  170. next: { tags: ["pickorder"] },
  171. },
  172. );
  173. }
  174. },
  175. );