payrollBatch.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import request from '@/utils/request';
  2. import { getProcessInstance } from '@/api/bpm/processInstance';
  3. function unwrap(response) {
  4. const result = (response && response.data) || {};
  5. if (Number(result.code) === 0) return result.data;
  6. const error = new Error(result.message || '服务调用失败');
  7. if (response && response.__toastShown) error.__toastShown = true;
  8. return Promise.reject(error);
  9. }
  10. function hasId(value) {
  11. return value !== undefined && value !== null && String(value).trim() !== '';
  12. }
  13. /**
  14. * 薪资核算工作台汇总:批次状态、同步状态、人数、完成率、异常计数、可用动作。
  15. * GET /hr/payroll/batches/{batchId}/dashboard
  16. * 不含工资金额与来源快照正文。
  17. */
  18. export async function getPayrollBatchDashboard(batchId) {
  19. if (!hasId(batchId)) return null;
  20. const response = await request({
  21. url: `/hr/payroll/batches/${encodeURIComponent(String(batchId))}/dashboard`,
  22. method: 'get'
  23. });
  24. return unwrap(response);
  25. }
  26. /**
  27. * 批次未关闭异常:GET /hr/payroll/batches/{batchId}/exceptions
  28. */
  29. export async function listPayrollBatchExceptions(batchId) {
  30. if (!hasId(batchId)) return [];
  31. const response = await request({
  32. url: `/hr/payroll/batches/${encodeURIComponent(String(batchId))}/exceptions`,
  33. method: 'get'
  34. });
  35. return unwrap(response) || [];
  36. }
  37. /**
  38. * 法人内批次摘要:GET /hr/payroll/batches?companyId=xxx
  39. * 含审批单据编码、模板版本、发起人、创建时间,不含金额。
  40. */
  41. export async function listPayrollBatches(companyId) {
  42. if (!hasId(companyId)) return [];
  43. const response = await request({
  44. url: '/hr/payroll/batches',
  45. method: 'get',
  46. params: { companyId }
  47. });
  48. return unwrap(response) || [];
  49. }
  50. /**
  51. * 法人可用薪资模板摘要:GET /hr/payroll/templates?companyId=xxx
  52. * 批次摘要只返回模板主键和版本号,名称需另取。
  53. */
  54. export async function listPayrollTemplates(companyId) {
  55. if (!hasId(companyId)) return [];
  56. const response = await request({
  57. url: '/hr/payroll/templates',
  58. method: 'get',
  59. params: { companyId }
  60. });
  61. return unwrap(response) || [];
  62. }
  63. /**
  64. * 按模板主键查模板名称,取不到时返回空字符串(不阻断审批页渲染)。
  65. */
  66. export async function resolvePayrollTemplateName(companyId, templateId) {
  67. if (!hasId(companyId) || !hasId(templateId)) return '';
  68. try {
  69. const list = await listPayrollTemplates(companyId);
  70. const records = Array.isArray(list) ? list : [];
  71. const matched = records.find((item) => String(item.id) === String(templateId));
  72. return (matched && matched.templateName) || '';
  73. } catch (error) {
  74. return '';
  75. }
  76. }
  77. /**
  78. * 在法人批次摘要列表中查找当前批次。
  79. */
  80. export async function getPayrollBatchSummary(companyId, batchId) {
  81. if (!hasId(companyId) || !hasId(batchId)) return {};
  82. const list = await listPayrollBatches(companyId);
  83. const records = Array.isArray(list) ? list : [];
  84. return (
  85. records.find((item) => String(item.id) === String(batchId)) || {}
  86. );
  87. }
  88. /**
  89. * 审批详情取数(审批页用)
  90. *
  91. * 1. 待办传入的 businessId 即薪资批次主键,直接取工作台汇总;
  92. * 2. 汇总取不到时回退流程实例 formVariables(发起审批时透传的批次字段);
  93. * 3. 汇总可用时再并行取未关闭异常与批次摘要(单据编码/模板版本)。
  94. *
  95. * 不经过薪资明细接口,审批页不取个人薪资数据。
  96. */
  97. export async function loadPayrollBatchApprovalDetail(
  98. businessId,
  99. processInstanceId = ''
  100. ) {
  101. const result = {
  102. dashboard: {},
  103. summary: {},
  104. exceptions: [],
  105. formVariables: {},
  106. errorMessage: ''
  107. };
  108. let batchId = hasId(businessId) ? businessId : '';
  109. let dashboardError = '';
  110. if (hasId(batchId)) {
  111. try {
  112. result.dashboard = (await getPayrollBatchDashboard(batchId)) || {};
  113. } catch (error) {
  114. dashboardError = error.message || '';
  115. }
  116. }
  117. if (
  118. (!result.dashboard || !Object.keys(result.dashboard).length) &&
  119. hasId(processInstanceId)
  120. ) {
  121. try {
  122. const instance = await getProcessInstance(processInstanceId);
  123. result.formVariables = (instance && instance.formVariables) || {};
  124. const fallbackId = result.formVariables.businessId;
  125. if (hasId(fallbackId) && String(fallbackId) !== String(batchId)) {
  126. batchId = fallbackId;
  127. try {
  128. result.dashboard = (await getPayrollBatchDashboard(batchId)) || {};
  129. } catch (error) {
  130. dashboardError = error.message || dashboardError;
  131. }
  132. }
  133. } catch (error) {
  134. /* 流程实例也取不到时保留流程表单摘要 */
  135. }
  136. }
  137. if (result.dashboard && Object.keys(result.dashboard).length > 0) {
  138. const companyId = result.dashboard.companyId;
  139. await Promise.all([
  140. listPayrollBatchExceptions(batchId)
  141. .then((list) => {
  142. result.exceptions = Array.isArray(list) ? list : [];
  143. })
  144. .catch(() => {
  145. result.exceptions = [];
  146. }),
  147. getPayrollBatchSummary(companyId, batchId)
  148. .then(async (summary) => {
  149. result.summary = summary || {};
  150. if (hasId(result.summary.templateId)) {
  151. const templateName = await resolvePayrollTemplateName(
  152. companyId,
  153. result.summary.templateId
  154. );
  155. if (templateName) result.summary.templateName = templateName;
  156. }
  157. })
  158. .catch(() => {
  159. result.summary = {};
  160. })
  161. ]);
  162. }
  163. if (dashboardError) {
  164. result.errorMessage = dashboardError;
  165. }
  166. return result;
  167. }