You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

175 lines
4.1 KiB

  1. "use server"
  2. import { serverFetchJson, serverFetchString, serverFetchWithNoContent } from "@/app/utils/fetchUtil";
  3. import { BASE_API_URL } from "@/config/api";
  4. import { revalidatePath, revalidateTag } from "next/cache";
  5. import { cache } from "react";
  6. export interface InvoiceResult {
  7. id: number;
  8. projectCode: string;
  9. projectName: string;
  10. stage: string;
  11. comingPaymentMileStone: string;
  12. paymentMilestoneDate: string;
  13. resourceUsage: number;
  14. unbilledHours: number;
  15. reminder: string;
  16. }
  17. type InvoiceListError = {
  18. [field in keyof NewInvoice]?: string;
  19. } & {
  20. message?: string;
  21. };
  22. export type NewInvoice = {
  23. invoiceNo: string | undefined,
  24. projectCode: string | undefined,
  25. issuedDate: Date
  26. issuedAmount: number,
  27. receiptDate: Date
  28. receivedAmount: number
  29. } & {
  30. _isNew: boolean;
  31. _error: InvoiceListError
  32. }
  33. export type InvoiceType = {
  34. data: NewInvoice[]
  35. }
  36. export type PostInvoiceData = {
  37. invoiceNo: string
  38. projectId: number
  39. projectCode: string | undefined,
  40. issuedAmount: number
  41. issueDate: string
  42. receiptDate?: string
  43. receivedAmount?: number
  44. }
  45. export interface CreateInvoiceInputs {
  46. id: number;
  47. // Project Details
  48. projectCode: string;
  49. projectName: string;
  50. stage: string;
  51. comingPaymentMileStone: string;
  52. paymentMilestoneDate: string;
  53. resourceUsage: number;
  54. unbilledHours: number;
  55. // Invoice Info
  56. client: string;
  57. address: string;
  58. attention: string;
  59. invoiceDate: string;
  60. dueDate: string;
  61. projectRefNo: string;
  62. // Invoice related Info
  63. reminder: string;
  64. amount: number;
  65. billHours: number;
  66. }
  67. export interface InvoiceInformation{
  68. id: number;
  69. client: string;
  70. address: string;
  71. attention: string;
  72. invoiceDate: string;
  73. dueDate: string;
  74. projectRefNo: string;
  75. amount: number;
  76. }
  77. export const fetchProjectInvoiceById = cache(async (id: number) => {
  78. return serverFetchJson<InvoiceResult[]>(`${BASE_API_URL}/invoices/getProjectDetail/${id}`, {
  79. next: { tags: ["projectDetailById"] },
  80. });
  81. })
  82. export const fetchInvoiceInfoById = cache(async (id: number) => {
  83. return serverFetchJson<InvoiceInformation[]>(`${BASE_API_URL}/invoices/getInvoiceInfo/${id}`, {
  84. next: { tags: ["invoiceInfoById"] },
  85. });
  86. })
  87. export const importIssuedInovice = async (data: FormData) => {
  88. // console.log("----------------",data)
  89. const importIssuedInovice = await serverFetchJson<any>(
  90. `${BASE_API_URL}/invoices/import/issued`,
  91. {
  92. method: "POST",
  93. body: data,
  94. // headers: { "Content-Type": "multipart/form-data" },
  95. },
  96. );
  97. return importIssuedInovice;
  98. };
  99. export const importReceivedInovice = async (data: FormData) => {
  100. // console.log("----------------",data)
  101. const importReceivedInovice = await serverFetchJson<any>(
  102. `${BASE_API_URL}/invoices/import/received`,
  103. {
  104. method: "POST",
  105. body: data,
  106. // headers: { "Content-Type": "multipart/form-data" },
  107. },
  108. );
  109. return importReceivedInovice;
  110. };
  111. export const importInvoices = async (data: FormData) => {
  112. const importInvoices = await serverFetchJson<any>(
  113. `${BASE_API_URL}/invoices/import/v2`,
  114. {
  115. method: "POST",
  116. body: data,
  117. },
  118. );
  119. return importInvoices;
  120. };
  121. export const updateInvoice = async (data: any) => {
  122. console.log(data)
  123. const updateInvoice = await serverFetchJson<any>(`${BASE_API_URL}/invoices/update`, {
  124. method: "Post",
  125. body: JSON.stringify(data),
  126. headers: { "Content-Type": "application/json" },
  127. });
  128. revalidateTag("invoices")
  129. revalidatePath("/(main)/invoice")
  130. return updateInvoice;
  131. }
  132. export const deleteInvoice = async (id: number) => {
  133. const invoice = await serverFetchWithNoContent(
  134. `${BASE_API_URL}/invoices/${id}`,
  135. {
  136. method: "DELETE",
  137. headers: { "Content-Type": "application/json" },
  138. },
  139. );
  140. revalidateTag("invoices");
  141. return invoice;
  142. };
  143. export const createInvoices = async (data: any) => {
  144. // console.log(data)
  145. const createInvoices = serverFetchString<any>(`${BASE_API_URL}/invoices/create`, {
  146. method: "Post",
  147. body: JSON.stringify(data),
  148. headers: { "Content-Type": "application/json" },
  149. });
  150. revalidateTag("invoices")
  151. return createInvoices;
  152. }