FPSMS-frontend
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

142 righe
4.9 KiB

  1. "use client";
  2. import { NEXT_PUBLIC_API_URL } from '@/config/api';
  3. import { clientAuthFetch } from '@/app/utils/clientAuthFetch';
  4. export interface ItemCodeWithName {
  5. code: string;
  6. name: string;
  7. }
  8. export interface ItemCodeWithCategory {
  9. code: string;
  10. category: string;
  11. name?: string;
  12. }
  13. /**
  14. * Fetch item codes for SemiFG Production Analysis Report
  15. * @param stockCategory - Comma-separated stock categories (e.g., "FG,WIP") or empty string for all
  16. * @returns Array of item codes with names
  17. */
  18. export const fetchSemiFGItemCodes = async (
  19. stockCategory: string = ''
  20. ): Promise<ItemCodeWithName[]> => {
  21. let url = `${NEXT_PUBLIC_API_URL}/report/semi-fg-item-codes`;
  22. if (stockCategory && stockCategory !== 'All' && !stockCategory.includes('All')) {
  23. url = `${url}?stockCategory=${stockCategory}`;
  24. }
  25. const response = await clientAuthFetch(url, {
  26. method: 'GET',
  27. headers: { 'Content-Type': 'application/json' },
  28. });
  29. if (response.status === 401 || response.status === 403) throw new Error("Unauthorized");
  30. if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
  31. return await response.json();
  32. };
  33. /**
  34. * Fetch item codes with category information for SemiFG Production Analysis Report
  35. * @param stockCategory - Comma-separated stock categories (e.g., "FG,WIP") or empty string for all
  36. * @returns Array of item codes with category and name
  37. */
  38. export const fetchSemiFGItemCodesWithCategory = async (
  39. stockCategory: string = ''
  40. ): Promise<ItemCodeWithCategory[]> => {
  41. let url = `${NEXT_PUBLIC_API_URL}/report/semi-fg-item-codes-with-category`;
  42. if (stockCategory && stockCategory !== 'All' && !stockCategory.includes('All')) {
  43. url = `${url}?stockCategory=${stockCategory}`;
  44. }
  45. const response = await clientAuthFetch(url, {
  46. method: 'GET',
  47. headers: { 'Content-Type': 'application/json' },
  48. });
  49. if (response.status === 401 || response.status === 403) throw new Error("Unauthorized");
  50. if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
  51. return await response.json();
  52. };
  53. /**
  54. * Generate and download the SemiFG Production Analysis Report PDF
  55. * @param criteria - Report criteria parameters
  56. * @param reportTitle - Title of the report for filename
  57. * @returns Promise that resolves when download is complete
  58. */
  59. export const generateSemiFGProductionAnalysisReport = async (
  60. criteria: Record<string, string>,
  61. reportTitle: string = '成品/半成品生產分析報告'
  62. ): Promise<void> => {
  63. const queryParams = new URLSearchParams(criteria).toString();
  64. const url = `${NEXT_PUBLIC_API_URL}/report/print-semi-fg-production-analysis?${queryParams}`;
  65. const response = await clientAuthFetch(url, {
  66. method: 'GET',
  67. headers: { Accept: 'application/pdf' },
  68. });
  69. if (response.status === 401 || response.status === 403) throw new Error("Unauthorized");
  70. if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
  71. const blob = await response.blob();
  72. const downloadUrl = window.URL.createObjectURL(blob);
  73. const link = document.createElement('a');
  74. link.href = downloadUrl;
  75. const contentDisposition = response.headers.get('Content-Disposition');
  76. let fileName = `${reportTitle}.pdf`;
  77. if (contentDisposition?.includes('filename=')) {
  78. fileName = contentDisposition.split('filename=')[1].split(';')[0].replace(/"/g, '');
  79. }
  80. link.setAttribute('download', fileName);
  81. document.body.appendChild(link);
  82. link.click();
  83. link.remove();
  84. window.URL.revokeObjectURL(downloadUrl);
  85. };
  86. /**
  87. * Generate and download the SemiFG Production Analysis Report as Excel
  88. * @param criteria - Report criteria parameters
  89. * @param reportTitle - Title of the report for filename
  90. * @returns Promise that resolves when download is complete
  91. */
  92. export const generateSemiFGProductionAnalysisReportExcel = async (
  93. criteria: Record<string, string>,
  94. reportTitle: string = '成品/半成品生產分析報告'
  95. ): Promise<void> => {
  96. const queryParams = new URLSearchParams(criteria).toString();
  97. const url = `${NEXT_PUBLIC_API_URL}/report/print-semi-fg-production-analysis-excel?${queryParams}`;
  98. const response = await clientAuthFetch(url, {
  99. method: 'GET',
  100. headers: { Accept: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' },
  101. });
  102. if (response.status === 401 || response.status === 403) throw new Error('Unauthorized');
  103. if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
  104. const blob = await response.blob();
  105. const downloadUrl = window.URL.createObjectURL(blob);
  106. const link = document.createElement('a');
  107. link.href = downloadUrl;
  108. const contentDisposition = response.headers.get('Content-Disposition');
  109. let fileName = `${reportTitle}.xlsx`;
  110. if (contentDisposition?.includes('filename=')) {
  111. fileName = contentDisposition.split('filename=')[1].split(';')[0].replace(/"/g, '');
  112. }
  113. link.setAttribute('download', fileName);
  114. document.body.appendChild(link);
  115. link.click();
  116. link.remove();
  117. window.URL.revokeObjectURL(downloadUrl);
  118. };