import { useMessage } from '@eciol/ant-ui'; import { request } from '@eciol/request'; import { blobToJson, downloadByData, replaceVariables } from '@eciol/shared'; import type { FilterJson, PayApplyService } from '@/api'; import { useUserStoreWithOut } from '@/store/modules/user'; import { generateFileName, PaymentChannelEnum } from '@/utils'; import type { PayBillService } from './typing'; enum Api { GetPayBillList = '/api/Paybill/QueryPage', GetPayBillDetail = '/api/Paybill/{orgId}/{id}', DeletePayBill = '/api/Paybill/{orgId}/cancel?id={id}', JoinPayBill = '/api/Paybill/{orgId}/{id}/join', SubmitPayBill = '/api/Paybill/{orgId}/submit', CreatePayBill = '/api/Paybill/{orgId}/create', ApprovePayBill = '/api/Paybill/{orgId}/approve', DeApprovePayBill = '/api/Paybill/{orgId}/de-approve', PaymentPayBill = '/api/Paybill/{orgId}/payment', DeEndApprovePayBill = '/api/Paybill/{orgId}/de-final-approve', EditActualPayBill = '/api/Paybill/editactual', EndApprovePayBill = '/api/Paybill/{orgId}/final-approve', StatisticsPayBill = '/api/Paybill/{orgId}/statistics', ExportPayBill = '/api/Paybill/{orgId}/export-list', ExportPaypal = '/api/Paybill/{orgId}/export-paypal', ExportBank = '/api/Paybill/{orgId}/export-bank', AdjustmentPayBill = '/api/PaybillChange/{id}/{procInstId}/AdjustmentSubmit', CheckAdjustmentPayBill = '/api/PaybillChange/AdjustmentCreate', } /** * 获取付款单列表 */ function getPayBillList(data: PayBillService.GetPayApplyListParams) { const { getCompanyId } = useUserStoreWithOut(); return request.post( { url: Api.GetPayBillList, data: { ...data, orgId: getCompanyId }, }, { showLoading: true, }, ); } /** * 获取付款单详情 */ function getPayBillDetail(id: number) { const { getCompanyId } = useUserStoreWithOut(); return request.get( { url: replaceVariables(Api.GetPayBillDetail, { id, orgId: getCompanyId }), }, { showLoading: true, }, ); } /** * 删除付款单 */ function deletePayBill(id: number) { const { getCompanyId } = useUserStoreWithOut(); return request.put( { url: replaceVariables(Api.DeletePayBill, { id, orgId: getCompanyId }), }, { showLoading: true, successMessageMode: 'message', successMessage: '删除成功!', }, ); } /** * 付款单加入关联查询 */ function joinPayBill(id: number, orgId) { const { getCompanyId } = useUserStoreWithOut(); return request.get( { url: replaceVariables(Api.JoinPayBill, { id, orgId: orgId ?? getCompanyId }), }, { showLoading: true, }, ); } /** * 新建付款单 */ function createPayBill(data: PayBillService.PaybillCreateParams) { const { getCompanyId } = useUserStoreWithOut(); return request.post( { url: replaceVariables(Api.CreatePayBill, { orgId: getCompanyId }), data, }, { showLoading: true, loadingTip: 'ADD', successMessageMode: 'message', operationType: 'ADD', }, ); } /** * 提交付款单 */ function submitPayBill(ids: number[]) { const { getCompanyId } = useUserStoreWithOut(); return request.put( { url: replaceVariables(Api.SubmitPayBill, { orgId: getCompanyId }), data: ids, }, { showLoading: true, loadingTip: 'SUBMIT', operationType: 'SUBMIT', successMessageMode: 'message', }, ); } /** * 审核付款单 */ function approvePayBill(ids: number[]) { const { getCompanyId } = useUserStoreWithOut(); return request.put( { url: replaceVariables(Api.ApprovePayBill, { orgId: getCompanyId }), data: ids, }, { showLoading: true, successMessage: '审核成功', successMessageMode: 'message', }, ); } /** * 反审核付款单 */ function deApprovePayBill(ids: number[]) { const { getCompanyId } = useUserStoreWithOut(); return request.put( { url: replaceVariables(Api.DeApprovePayBill, { orgId: getCompanyId }), data: ids, }, { showLoading: true, successMessage: '反审核成功', successMessageMode: 'message', }, ); } /** * 确认付款付款单 */ function paymentPayBill(ids: number[]) { const { getCompanyId } = useUserStoreWithOut(); return request.put( { url: replaceVariables(Api.PaymentPayBill, { orgId: getCompanyId }), data: ids, }, { showLoading: true, successMessage: '确认付款成功', successMessageMode: 'message', }, ); } /** * 终审付款单 */ function endApprovePayBill(ids: number[]) { const { getCompanyId } = useUserStoreWithOut(); return request.put( { url: replaceVariables(Api.EndApprovePayBill, { orgId: getCompanyId }), data: ids, }, { showLoading: true, successMessage: '终审成功', successMessageMode: 'message', }, ); } /** * 反终审付款单 */ function deEndApprovePayBill(ids: number[]) { const { getCompanyId } = useUserStoreWithOut(); return request.put( { url: replaceVariables(Api.DeEndApprovePayBill, { orgId: getCompanyId }), data: ids, }, { showLoading: true, successMessage: '反终审成功', successMessageMode: 'message', }, ); } /** * 修改实付金额 */ function editActualPayBill(data: PayBillService.EditActualPayBillParams) { return request.post( { url: Api.EditActualPayBill, data, }, { showLoading: true, successMessage: '设置实际金额成功', successMessageMode: 'message', }, ); } /** * 获取支付统计信息 */ async function statisticsPayBill(data) { const { getCompanyId } = useUserStoreWithOut(); const result = await request.post( { url: replaceVariables(Api.StatisticsPayBill, { orgId: getCompanyId }), data, }, { showLoading: true, }, ); const _data = result.sort((a, b) => a.paymentChannel - b.paymentChannel); const mergedArray: PayApplyService.GetPayStatisticsResult = Object.values( _data.reduce((result, item) => { if (result[item.paymentChannel]) { Object.assign(result[item.paymentChannel].data ?? {}, item.data); } else { result[item.paymentChannel] = item; } return result; }, {}), ); return mergedArray.map((item) => { if (item.paymentChannel === PaymentChannelEnum.外币支付) { const paymentChannelFields = ['Paypal Account', 'Bank Account']; item.paymentChannelList = Object.keys(item.data) .filter((key) => paymentChannelFields.includes(key)) .map((key) => ({ label: item.data[key], value: key, })); item.currentList = Object.keys(item.data) .filter((key) => !paymentChannelFields.includes(key)) .map((key) => ({ label: item.data[key], value: key, })); } else { item.data = Object.keys(item.data).map((key) => ({ label: item.data[key], value: key, })); } return item; }); } /** * 导出付款单 */ async function exportPayBill(data: FilterJson['filtersConditions']) { const { createMessage } = useMessage(); const { getCompanyId } = useUserStoreWithOut(); const _data = await request.post( { url: replaceVariables(Api.ExportPayBill, { orgId: getCompanyId }), data, responseType: 'blob', }, { showLoading: true, successMessage: '导出成功', successMessageMode: 'message', isTransformResponse: false, }, ); if (_data.type === 'application/json') { const result = await blobToJson(_data); createMessage.error(result.message); return false; } downloadByData(_data, generateFileName({ fileName: 'Payment list', suffix: 'xlsx' })); createMessage.success('导出成功'); } /** * 导出Paypal文件(外币支付) */ async function exportPaypal(data: FilterJson['filtersConditions']) { const { createMessage } = useMessage(); const { getCompanyId } = useUserStoreWithOut(); const _data = await request.post( { url: replaceVariables(Api.ExportPaypal, { orgId: getCompanyId }), data, responseType: 'blob', }, { showLoading: true, successMessage: '导出成功', successMessageMode: 'message', isTransformResponse: false, }, ); if (_data.type === 'application/json') { const result = await blobToJson(_data); createMessage.error(result.message); return false; } downloadByData(_data, generateFileName({ fileName: 'Invoice', suffix: 'zip' })); createMessage.success('导出成功'); } /** * 导出Bank文件(外币支付) */ async function exportBank(data: FilterJson['filtersConditions']) { const { createMessage } = useMessage(); const { getCompanyId } = useUserStoreWithOut(); const _data = await request.post( { url: replaceVariables(Api.ExportBank, { orgId: getCompanyId }), data, responseType: 'blob', }, { showLoading: true, successMessage: '导出成功', successMessageMode: 'message', isTransformResponse: false, }, ); if (_data.type === 'application/json') { const result = await blobToJson(_data); createMessage.error(result.message); return false; } downloadByData(_data, generateFileName({ fileName: 'Invoice', suffix: 'zip' })); createMessage.success('导出成功'); } function adjustmentPayBill(data) { return request.put( { url: replaceVariables(Api.AdjustmentPayBill, { id: data.dataId, procInstId: data.procInstId, }), }, { showLoading: true, }, ); } function checkAdjustmentPayBill(data: { paybillId: number; reason: string }) { return request.post( { url: Api.CheckAdjustmentPayBill, data, }, { showLoading: true, }, ); } export { adjustmentPayBill, approvePayBill, checkAdjustmentPayBill, createPayBill, deApprovePayBill, deEndApprovePayBill, deletePayBill, editActualPayBill, endApprovePayBill, exportBank, exportPayBill, exportPaypal, getPayBillDetail, getPayBillList, joinPayBill, paymentPayBill, statisticsPayBill, submitPayBill, }; export type { PayBillService } from './typing';