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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { default as BaseController } from "../baseController";
import { Post } from "egg-shell-decorators";
import * as sql from "mssql";
import { vPurchaseEnterListCreateShelve } from "../../entity/inbound/purchase/vPurchaseEnterListCreateShelve";
import { In } from "typeorm";
import { PurchaseEnter } from "../../entity/inbound/purchase/purchaseEnter";
import { PurchaseShelve } from "../../entity/inbound/purchase/purchaseShelve";
import { PurchaseShelveList } from "../../entity/inbound/purchase/purchaseShelveList";
import moment = require("moment");
import * as mssql from "mssql";
 
/**
 * 收货 - 入库单
 */
export default class EnterController extends BaseController {
  //#region 撤销入库
  @Post()
  public async cancelEnter() {
    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("enter_Ids", body.enter_Ids);
      request.input("userProduct_Id", userInfo.userProduct_Id);
      request.output("outMsg", sql.NVarChar(2000));
      let result = await request.execute("sp_Purchase_Enter_Cancel");
      let outMsg = result.output.outMsg;
 
      if (outMsg != null && outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
      } else {
        this.info.msg = "入库撤销成功";
        this.info.result = true;
      }
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误信息:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 生成上架单
  @Post()
  public async createShelve() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let enterList_Ids = body.enterList_Ids;
    if (!Array.isArray(enterList_Ids)) {
      this.info.result = false;
      this.info.msg = "没有选择可执行的数据";
      return;
    }
    try {
      let msg = "";
      let shelveList = await this.dbRead.find(vPurchaseEnterListCreateShelve, {
        enterList_Id: In(enterList_Ids)
      });
      if (!shelveList.length) {
        this.info.result = false;
        this.info.msg = "没有可执行的数据";
        return;
      }
      // 每个入库单生成一个上架单
      var shelveGroup = shelveList.reduce(
        (all: Array<any>, next) => (all.some(item => item.enter_Id == next.enter_Id && item.enterCode == next.enterCode) ? all : [...all, next]),
        []
      );
      for (let a of shelveGroup) {
        let repOrder = await this.dbRead.findOne(PurchaseEnter, {
          enterCode: a.enterCode,
          userProduct_Id: userInfo.userProduct_Id
        });
 
        var enterList = shelveList.filter(w => w.enter_Id == a.enter_Id);
        let num = body.num;
        // num>0进行分页操作
        let shelveOrderCount = num > 0 ? Math.ceil((1.0 * enterList.length) / body.num) : enterList.length;
        for (
          let i = 0;
          i < shelveOrderCount;
          i++ //分单
        ) {
          //#region  上架主表信息
          let shelveCode = await ctx.service.common.getCodeRegular(269);
          if (msg) msg += ", ";
          msg += shelveCode;
 
          let newShelveInfo = new PurchaseShelve();
          newShelveInfo.shelveCode = shelveCode;
          newShelveInfo.enter_Id = repOrder.enter_Id;
          newShelveInfo.enterCode = repOrder.enterCode;
          newShelveInfo.consignor_Id = repOrder.consignor_Id;
          newShelveInfo.consignorCode = repOrder.consignorCode;
          newShelveInfo.consignorName = repOrder.consignorName;
          newShelveInfo.storage_Id = repOrder.storage_Id;
          newShelveInfo.storageName = repOrder.storageName;
          //newShelveInfo.User_Id = userInfo.user_Id;
          //newShelveInfo.UserTrueName = UserHelper.UserInfo.UserTrueName;
          newShelveInfo.onShelveStatus = "待上架";
          newShelveInfo.totalQuantity = 0;
          newShelveInfo.auditing = 0;
          newShelveInfo.enable = 1;
          newShelveInfo.createID = userInfo.user_Id;
          newShelveInfo.creator = userInfo.userTrueName;
          newShelveInfo.createDate = new Date();
 
          newShelveInfo.platCorpName = userInfo.platCorpName;
          newShelveInfo.platUserCode = userInfo.platUserCode;
          newShelveInfo.platUserName = userInfo.platUserName;
          newShelveInfo.platUser_Id = userInfo.platUser_Id;
          newShelveInfo.userProductAlias = userInfo.userProductAlias;
          newShelveInfo.userProductCode = userInfo.userProductCode;
          newShelveInfo.userProduct_Id = userInfo.userProduct_Id;
 
          // 保存主表数据
          await this.dbWrite.insert(PurchaseShelve, newShelveInfo);
          //#endregion
 
          //#region  上架明细信息
          let maxIndex = (i + 1) * body.num;
          if (maxIndex > enterList.length || !body.num) maxIndex = enterList.length;
          let pageEnterList = enterList.filter((item, index) => {
            return item && index >= i * body.num && index < maxIndex;
          });
          for (var enterDetailInfo of pageEnterList) {
            let modellist = new PurchaseShelveList();
            modellist.shelve_Id = newShelveInfo.shelve_Id;
            modellist.enter_Id = enterDetailInfo.enter_Id;
            modellist.enterList_Id = enterDetailInfo.enterList_Id;
            modellist.product_Id = enterDetailInfo.product_Id;
            modellist.productCode = enterDetailInfo.productCode;
            modellist.productName = enterDetailInfo.productName;
            modellist.productModel = enterDetailInfo.productModel;
            modellist.productSpec = enterDetailInfo.productSpec;
 
            modellist.quantity = enterDetailInfo.quantity;
            modellist.onShelveQuantity = enterDetailInfo.quantity;
            modellist.weight = enterDetailInfo.weight;
            modellist.totalWeight = enterDetailInfo.totalWeight;
 
            modellist.positionName = enterDetailInfo.shelvePositionName;
            modellist.positionNo = enterDetailInfo.positionName;
            modellist.enable = 1;
            modellist.createID = userInfo.user_Id;
            modellist.creator = userInfo.userTrueName;
            modellist.createDate = new Date();
            modellist.batchNumber = enterDetailInfo.batchNumber;
            let productDate = enterDetailInfo.produceDate;
            let _productDate;
            if (productDate) {
              _productDate = moment(productDate).format("YYYY-MM-DD");
            }
            modellist.produceDate = _productDate;
            modellist.shelveStatus = "待上架";
            await this.dbWrite.insert(PurchaseShelveList, modellist);
          }
          //#endregion
 
          //#region 更新合计数量
          // 主表求和字段计算
          let total: any = pageEnterList.reduce(
            (totalItem: any, currItem) => {
              totalItem.totalQuantity += currItem.quantity;
              totalItem.totalWeight += currItem.totalWeight;
 
              return totalItem;
            },
            { totalQuantity: 0, totalWeight: 0 }
          );
          await this.dbWrite.update(PurchaseShelve, newShelveInfo.shelve_Id, {
            totalQuantity: total.totalQuantity,
            onShelveQuantity: total.totalQuantity,
            totalWeight: total.totalWeight
          });
          //#endregion
        }
      }
      this.info.result = true;
      this.info.msg = msg + "上架单生成成功!";
    } catch (error) {
      this.info.result = false;
      this.info.msg = "生成错误:" + error.message;
    }
 
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 生成一次性费用
  /**
   * 生成一次性费用
   */
  @Post()
  public async getFeeItem() {
    let { ctx } = this;
    let body = ctx.request.body;
    if (!Array.isArray(body.ids) || !body.ids.length) {
      this.info.result = false;
      this.info.msg = "数据不存在";
      ctx.body = this.info;
      return;
    }
    try {
      const connection: any = await this.dbWrite.connection;
      let request = new mssql.Request(connection.driver.master);
      let msg = [];
 
      for (let enter_Id of body.ids) {
        request.input("enter_Id", enter_Id);
        request.output("outMsg", mssql.NVarChar(2000));
        let result = await request.execute("sp_Fee_Base_OneCharge_In");
        let outMsg = result.output.outMsg;
 
        if (outMsg) {
          msg.push(outMsg);
        } else {
          let enterInfo = await this.dbRead.findOne(PurchaseEnter, enter_Id);
          let expandFields = enterInfo.expandFields || "{}";
          expandFields = JSON.parse(expandFields);
          expandFields["isCreateFee"] = "已生成"; // 生成费用单
          enterInfo.expandFields = JSON.stringify(expandFields);
 
          await this.dbWrite.save(enterInfo);
          msg.push(enterInfo.enterCode + "执行完成");
        }
      }
      this.info.msg = msg.join(",");
      this.info.result = true;
    } catch (error) {
      this.info.msg = error.message;
      this.info.result = false;
    }
    this.ctx.body = this.info;
  }
  //#endregion
}