333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import BaseService from "../baseService";
import { ResultInfo } from "../../public/commonInterface";
import { FinanceReceive } from "../../entity/finance/receive/financeReceive";
import { FinanceFlow } from "../../entity/finance/base/financeFlow";
import { FinanceReceiveList } from "../../entity/finance/receive/financeReceiveList";
import { SaleOrder } from "../../entity/outbound/sale/saleOrder";
import { Raw } from "typeorm";
 
/**
 * 财务管理
 */
export default class ReceiveService 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(FinanceReceive, 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 incomeRMB = dataInfo.totalSettle;
 
        //生成账户流水
        let newFlow = new FinanceFlow();
        newFlow.flowCode = await this.ctx.service.common.getCodeRegular(1696);
        newFlow.flowName = "收款单收款";
        newFlow.bankAccountCode = dataInfo.bankAccountCode;
        newFlow.bankAccountName = dataInfo.bankAccountName;
        newFlow.outcomeRMB = 0;
        newFlow.incomeRMB = incomeRMB;
        newFlow.balanceRMB = balanceRMB + incomeRMB;
        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.receive_Id;
        newFlow.detailID = dataInfo.receive_Id;
        newFlow.billCode = dataInfo.receiveCode;
 
        await this.setAccountInfo(newFlow);
        await this.dbWrite.save(newFlow);
 
        // 修改出库单状态
        let receiveDetails = await this.dbRead.find(FinanceReceiveList, {
          receive_Id: dataInfo.receive_Id
        });
        // 修改单据状态
        await this.dbWrite.update(
          FinanceReceive,
          {
            receive_Id: dataInfo.receive_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(SaleOrder, detail.bill_Id);
 
          if (orderInfo) {
            // 获取出库单所有已经结算的明细
            let orderSettiles = await this.dbWrite.find(FinanceReceiveList, {
              bill_Id: detail.bill_Id,
              receive_Id: Raw(() => {
                return "receive_Id in(Select receive_Id from Finance_Receive Where billStatus='审核成功')";
              })
            });
            // 主表求和字段计算
            let total: any = orderSettiles.reduce(
              (totalItem: any, currItem) => {
                totalItem.totalSettle += currItem.totalSettle;
                return totalItem;
              },
              { totalSettle: 0 }
            );
 
            // 重新计算出库单已结算和剩余未结算金额
            let unpaid = orderInfo.subTotal - total.totalSettle;
            if (unpaid < 0) unpaid = 0;
            await this.dbWrite.update(
              SaleOrder,
              {
                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
}