222
schangxiang@126.com
2025-06-13 6a8393408d8cefcea02b7a598967de8dc1e565c2
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import BaseService from "../../service/baseService";
import * as mssql from "mssql";
import { Post } from "egg-shell-decorators";
import { FeePayableBill } from "../../entity/finance/fee/feePayableBill";
import { In } from "typeorm";
import { FeeStatement } from "../../entity/finance/fee/feeStatement";
import { FeePayableBillDetail } from "../../entity/finance/fee/feePayableBillDetail";
import { FeeStatementDetail } from "../../entity/finance/fee/feeStatementDetail";
 
export default class PayableBillController extends BaseService {
  //#region
  @Post()
  public async storageSettle() {
    let { ctx } = this;
    let userInfo = await ctx.helper.userInfo();
 
    try {
      const connection: any = await this.dbWrite.connection;
      let request = new mssql.Request(connection.driver.master);
 
      request.input("UserProduct_Id", userInfo.userProduct_Id);
      request.output("outMsg", mssql.NVarChar(2000));
      let result = await request.execute("sp_Fee_Base_Storagefee");
      let outMsg = result.output.outMsg;
 
      if (outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
      } else {
        this.info.msg = "执行完成";
        this.info.result = true;
      }
    } catch (error) {
      this.info.msg = error.message;
      this.info.result = false;
    }
    this.ctx.body = this.info;
  }
  //#endregion
  //#region 生成对账单
  @Post()
  public async billGenerat() {
    let { ctx } = this;
    let userInfo = await this.userInfo;
    let body = ctx.body;
    let ids = body.ids;
    if (!Array.isArray(ids)) {
      this.info.result = false;
      this.info.msg = "没有选择可执行的数据";
      return;
    }
    try {
      // 获取主表信息 Fee_PayableBill
      let payableBillList = await this.dbRead.find(FeePayableBill, {
        where: {
          payableBill_Id: In(ids)
        }
      });
      // 根据仓库和商家进行分组
      let groupList: Array<FeePayableBill> = payableBillList.reduce(
        (all: Array<any>, next) =>
          all.some(item => item.storage_Id == next.storage_Id && item.consignor_Id == next.consignor_Id) ? all : [...all, next],
        []
      );
      if (groupList.length > 1) {
        this.info.result = false;
        this.info.msg = "仓库和商家必须一致才可以生成对账单";
        ctx.body = this.info;
        return;
      }
 
      // 分组后的数据进行保存
      for (let groupInfo of groupList) {
        var totalReceivable = 0;
        for (let payList of payableBillList) {
          totalReceivable += payList.totalReceivable;
        }
        // 保存主表
        let code = await this.ctx.service.common.getCodeRegular(1786); // 获取对账单编号
        let statementInfo = new FeeStatement();
        statementInfo.statementCode = code;
        statementInfo.billCompany = null;
        statementInfo.consignorName = groupInfo.consignorName;
        statementInfo.consignorCode = groupInfo.consignorCode;
        statementInfo.consignor_Id = groupInfo.consignor_Id;
        statementInfo.createDate = new Date();
        statementInfo.createID = userInfo.user_Id;
        statementInfo.creator = userInfo.userTrueName;
        statementInfo.endData = groupInfo.storageName;
        statementInfo.invoiceNo = groupInfo.storageName;
        statementInfo.isBilling = 0;
        statementInfo.isSettlement = 0;
        statementInfo.remark = groupInfo.storageName;
        statementInfo.settlementRemark = groupInfo.storageName;
        statementInfo.starData = null;
        statementInfo.statType = groupInfo.billType;
        statementInfo.statement_Id = 0;
        statementInfo.statusText = "新建";
        statementInfo.storageName = groupInfo.storageName;
        statementInfo.storage_Id = groupInfo.storage_Id;
        statementInfo.totalMoney = totalReceivable;
        // aaa = groupInfo.totalReceivable;
        // statementInfo.totalMoney = groupInfo.totalReceivable + aaa;
 
        // 设置账套信息
        await this.setAccountInfo(statementInfo);
        await this.dbWrite.save(statementInfo);
 
        let groupIds = payableBillList
          .filter(item => item.storage_Id == groupInfo.storage_Id && item.consignor_Id == groupInfo.consignor_Id)
          .map(item => item.payableBill_Id); // [1,3,4]
        // 处理明细
        let detailList = await this.dbRead.find(FeePayableBillDetail, {
          payableBill_Id: In(groupIds)
        });
        for (let detailInfo of detailList) {
          let statementDetailInfo = new FeeStatementDetail();
          statementDetailInfo.statement_Id = statementInfo.statement_Id;
          statementDetailInfo.billType = groupInfo.billType;
          statementDetailInfo.chargeMode = null;
          statementDetailInfo.consignorCode = groupInfo.consignorCode;
          statementDetailInfo.consignorName = groupInfo.consignorName;
          statementDetailInfo.consignor_Id = groupInfo.consignor_Id;
 
          statementDetailInfo.endTime = detailInfo.endDate;
          statementDetailInfo.feeItemCode = detailInfo.feeItemCode;
          statementDetailInfo.feeItemName = detailInfo.feeItemName;
          statementDetailInfo.feeItem_Id = detailInfo.feeItem_Id;
          statementDetailInfo.freeDays = detailInfo.freeDays;
          statementDetailInfo.freeMoney = detailInfo.freeMoney;
 
          statementDetailInfo.paid = null;
          statementDetailInfo.payMode = detailInfo.payMode;
          statementDetailInfo.payStatus = detailInfo.payStatus;
          statementDetailInfo.payableBillCode = groupInfo.payableBillCode;
          statementDetailInfo.payableBillCreateDate = new Date();
          statementDetailInfo.payableBillDetail_Id = detailInfo.payableBillDetail_Id;
          statementDetailInfo.payableBill_Id = detailInfo.payableBill_Id;
          statementDetailInfo.receivable = detailInfo.receivable;
          statementDetailInfo.remark = detailInfo.remark;
          statementDetailInfo.settlementMode = detailInfo.payType;
          statementDetailInfo.startTime = detailInfo.beginDate;
          statementDetailInfo.storageName = groupInfo.storageName;
          statementDetailInfo.storage_Id = groupInfo.storage_Id;
          statementDetailInfo.storagefee_Id = null;
          statementDetailInfo.tagWeight_T = null;
          statementDetailInfo.typeName = groupInfo.typeName;
          statementDetailInfo.type_Id = null;
          statementDetailInfo.unitPrice = detailInfo.feeRate;
          statementDetailInfo.weight = detailInfo.weight;
          statementDetailInfo.qty = detailInfo.qty;
 
          // 保存明细
          await this.dbWrite.save(statementDetailInfo);
        }
      }
      if (!payableBillList.length) {
        this.info.result = false;
        this.info.msg = "数据不存在";
        ctx.body = this.info;
        return;
      }
      this.info.result = true;
      this.info.msg = `账单生成成功`;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误信息:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
}