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
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
173
174
175
176
177
178
import * as sql from "mssql";
import { default as BaseController } from "../baseController";
import { Post } from "egg-shell-decorators";
import { PurchaseReturn } from "../../entity/inbound/purchase/purchaseReturn";
import { PurchaseReturnList } from "../../entity/inbound/purchase/purchaseReturnList";
import { PurchaseReturnStatusHistory } from "../../entity/inbound/purchase/purchaseReturnStatusHistory";
 
/**
 * 收货 - 入库单
 */
export default class EnterController extends BaseController {
  //#region getByCode
  /**
   * 根据采购退货单Code获取采购退货单信息,参数需要拼接在地址后,例如:/api/PurchaseReturn/GetByCode?returnCode=xxxxxx
   */
  @Post()
  public async getByCode() {
    let { ctx } = this;
    let body = ctx.request.body;
    if (!body.orderCode) {
      this.info.result = false;
      this.info.msg = "查询单号不能为空";
      return;
    }
    var model = await this.dbRead.findOne(PurchaseReturn, {
      where: [{ returnCode: body.orderCode }, { orderCode: body.orderCode }],
      relations: ["PurchaseReturnList"]
    });
    this.info.data = model;
    this.info.result = true;
    return this.info;
  }
  //#endregion
 
  //#region 复制单据
  @Post()
  public async copySaleOrder() {
    let { ctx } = this;
    let body = ctx.request.body;
 
    let return_Id = body.return_Id;
    let returnCode = await ctx.service.common.getCodeRegular(104); //得到自动编号
 
    try {
      var returnInfo = await this.dbRead.findOne(PurchaseReturn, return_Id);
      let returnNew = Object.assign({}, returnInfo);
 
      returnNew.return_Id = 0;
      returnNew.returnCode = returnCode;
      returnNew.sortingStatus = 1;
      returnNew.sortingDate = null;
      returnNew.statusID = 1;
      returnNew.statusText = "新建";
      returnNew.auditing = 0;
      returnNew.auditor = null;
      returnNew.auditDate = null;
 
      await this.dbWrite.insert(PurchaseReturn, returnNew);
 
      //如果订单主表新增成功,在复制明细
      if (returnNew.return_Id > 0) {
        let returnDetails = await this.dbRead.find(PurchaseReturnList, {
          return_Id: returnInfo.return_Id
        });
        for (var detail of returnDetails) {
          let detailNew = Object.assign({}, detail);
          detailNew.orderList_Id = 0;
          detailNew.return_Id = returnNew.return_Id;
          await this.dbWrite.insert(PurchaseReturnList, detailNew);
        }
        this.info.msg = "复制成功!";
        this.info.result = true;
      } else {
        this.info.msg = "错误信息:复制失败!";
        this.info.result = false;
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
 
    ctx.body = this.info;
  }
 
  //#endregion
 
  //#region 转到出库单
  @Post()
  public async toSaleOrder() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    try {
      const connection: any = await this.dbWrite.connection;
      let request = new sql.Request(connection.driver.master);
      request.input("return_Id", body.return_Id);
      request.input("user_Id", userInfo.user_Id);
      request.input("userTrueName", userInfo.userTrueName);
      request.output("outMsg", sql.NVarChar(2000));
      let result = await request.execute("sp_Purchase_Return_ToSaleOrder");
      let outMsg = result.output.outMsg;
 
      if (outMsg != null && outMsg.ToString()) {
        this.info.msg = outMsg.ToString();
        this.info.result = false;
        return this.info;
      }
      this.info.result = true;
      this.info.msg = "转到出库单成功!";
    } catch (error) {
      this.info.result = false;
      this.info.msg = error.message;
    }
 
    ctx.body = this.info;
  }
 
  //#endregion
 
  //#region  退货货单审核
  /**
   * 退货货单审核
   */
  @Post()
  public async auditingReturn() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let return_Ids = body.return_Ids;
    if (!Array.isArray(return_Ids)) {
      this.info.result = false;
      this.info.msg = "没有可执行的数据";
      ctx.body = this.info;
      return;
    }
 
    try {
      for (let return_Id of return_Ids) {
        // 更新审核状态
        let returnInfo = await this.dbRead.findOne(PurchaseReturn, return_Id);
        await this.dbWrite.update(
          PurchaseReturn,
          {
            return_Id: return_Id
          },
          {
            statusID: 2,
            auditor: userInfo.userTrueName,
            auditing: 2,
            auditDate: new Date(),
            statusText: "审核成功"
          }
        );
 
        // 记录审核退货单轨迹
        await this.dbWrite.save(PurchaseReturnStatusHistory, {
          return_Id: return_Id,
          statusType: "单据状态",
          operationType: "订单状态",
          fromStatus: returnInfo.statusText,
          toStatus: "审核成功",
          createID: userInfo.user_Id,
          creator: userInfo.userTrueName,
          createDate: new Date()
        });
 
        this.info.result = true;
        this.info.msg = "审核成功";
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
}