| import BaseService from "../baseService"; | 
| import { ResultInfo } from "../../public/commonInterface"; | 
| import { FinancePayout } from "../../entity/finance/pay/financePayout"; | 
| import { FinanceFlow } from "../../entity/finance/base/financeFlow"; | 
| import { FinancePayoutList } from "../../entity/finance/pay/financePayoutList"; | 
| import { Raw } from "typeorm"; | 
| import { PurchaseOrder } from "../../entity/inbound/purchase/purchaseOrder"; | 
| import { FinanceBankAccount } from '../../entity/finance/base/financeBankAccount'; | 
|   | 
| /** | 
|  * 财务管理 | 
|  */ | 
| export default class PayoutService extends BaseService { | 
|   //#region 审核后生成账户流水 | 
|   public async batchAuditing(): Promise<ResultInfo> { | 
|     let { ctx } = this; | 
|     let body = ctx.request.body; | 
|     let userInfo = await ctx.helper.userInfo(); | 
|     try { | 
|       let idValues = body.idValues; | 
|       if (!Array.isArray(idValues)) { | 
|         this.info.result = false; | 
|         this.info.msg = "没有可执行的数据"; | 
|         return this.info; | 
|       } | 
|       for (var idValue of idValues) { | 
|         let dataInfo = await this.dbRead.findOne(FinancePayout, idValue); | 
|         if (!dataInfo) { | 
|           this.info.result = false; | 
|           this.info.msg = "单据不存在"; | 
|           return this.info; | 
|         } | 
|         if (dataInfo.billStatus != "新建") { | 
|           this.info.result = false; | 
|           this.info.msg = "只有新建的单子才允许审核"; | 
|           return this.info; | 
|         } | 
|         //账户流水 | 
|         let lastFlow = await this.dbRead.findOne(FinanceFlow, { | 
|           where: { | 
|             bankAccount_Id: dataInfo.bankAccount_Id | 
|           }, | 
|           order: { | 
|             flow_Id: "DESC" | 
|           } | 
|         }); | 
|         // 最后金额 | 
|         let balanceRMB = lastFlow ? lastFlow.balanceRMB : 0; | 
|         let outcomeRMB = dataInfo.totalSettle; | 
|   | 
|         //生成账户流水 | 
|         let newFlow = new FinanceFlow(); | 
|         newFlow.flowCode = await this.ctx.service.common.getCodeRegular(1696); | 
|         newFlow.bankAccount_Id = dataInfo.bankAccount_Id; | 
|         newFlow.flowName = "付款单付款"; | 
|         newFlow.bankAccountCode = dataInfo.bankAccountCode; | 
|         newFlow.bankAccountName = dataInfo.bankAccountName; | 
|         newFlow.outcomeRMB = outcomeRMB; | 
|         newFlow.incomeRMB = 0; | 
|         newFlow.balanceRMB = balanceRMB - outcomeRMB; | 
|         newFlow.userTrueName = userInfo.userTrueName; | 
|         newFlow.deptName = userInfo.deptName; | 
|         newFlow.sourceType = "付款单"; | 
|   | 
|         newFlow.consignor_Id = dataInfo.consignor_Id; | 
|         newFlow.consignorCode = dataInfo.consignorCode; | 
|         newFlow.consignorName = dataInfo.consignorName; | 
|         newFlow.mainID = dataInfo.payout_Id; | 
|         newFlow.detailID = dataInfo.payout_Id; | 
|         newFlow.billCode = dataInfo.payoutCode; | 
|   | 
|         await this.setAccountInfo(newFlow); | 
|         await this.dbWrite.save(newFlow); | 
|   | 
|         await this.dbWrite.update(FinanceBankAccount, { | 
|           bankAccount_Id: dataInfo.bankAccount_Id | 
|         }, { | 
|           currentRMB: newFlow.balanceRMB | 
|         }); | 
|   | 
|         // 修改出库单状态 | 
|         let receiveDetails = await this.dbRead.find(FinancePayoutList, { | 
|           payout_Id: dataInfo.payout_Id | 
|         }); | 
|         // 修改单据状态 | 
|         await this.dbWrite.update( | 
|           FinancePayout, | 
|           { | 
|             payout_Id: dataInfo.payout_Id, | 
|             userProduct_Id: userInfo.userProduct_Id | 
|           }, | 
|           { | 
|             billStatus: "审核成功", | 
|             auditing: 2, | 
|             auditor: userInfo.userTrueName, | 
|             auditDate: new Date() | 
|           } | 
|         ); | 
|   | 
|         for (let detail of receiveDetails) { | 
|           let orderInfo = await this.dbRead.findOne(PurchaseOrder, detail.bill_Id); | 
|   | 
|           if (orderInfo) { | 
|             // 获取出库单所有已经结算的明细 | 
|             let orderSettiles = await this.dbWrite.find(FinancePayoutList, { | 
|               bill_Id: detail.bill_Id, | 
|               payout_Id: Raw(() => { | 
|                 return "payout_Id in(Select payout_Id from Finance_Payout Where billStatus='审核成功')"; | 
|               }) | 
|             }); | 
|             // 主表求和字段计算 | 
|             let total: any = orderSettiles.reduce( | 
|               (totalItem: any, currItem) => { | 
|                 totalItem.totalSettle += currItem.totalSettle; | 
|                 return totalItem; | 
|               }, | 
|               { totalSettle: 0 } | 
|             ); | 
|   | 
|             // 重新计算出库单已结算和剩余未结算金额 | 
|             let unpaid = orderInfo.totalMoney - total.totalSettle; | 
|             if (unpaid < 0) unpaid = 0; | 
|             await this.dbWrite.update( | 
|               PurchaseOrder, | 
|               { | 
|                 order_Id: orderInfo.order_Id | 
|               }, | 
|               { | 
|                 totalPaid: total.totalSettle, | 
|                 unpaid: unpaid | 
|               } | 
|             ); | 
|           } | 
|         } | 
|       } | 
|       this.info.result = true; | 
|       this.info.msg = "审核成功"; | 
|     } catch (error) { | 
|       this.info.result = false; | 
|       this.info.msg = "审核失败" + error.message; | 
|     } | 
|   | 
|     return this.info; | 
|   } | 
|   //#endregion | 
| } |