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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//#region
import { default as BaseController } from "../baseController";
import { Post } from "egg-shell-decorators";
import { PurchaseOrder } from "../../entity/inbound/purchase/purchaseOrder";
import { PurchaseOrderList } from "../../entity/inbound/purchase/purchaseOrderList";
import { BasePlate } from "../../entity/basicInfo/base/basePlate";
//#endregion
 
/**
 * 余料回库
 */
export default class OrderHjController extends BaseController {
  //#region save 保存
  @Post()
  public async save() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let details = []; // 记录明细保存的数据
    let masterData = body.masterData;
    let detailList = body.detailList;
 
    let mainInfo = new PurchaseOrder(); // 主表数据
    // 必须做这边操作,编辑时才执行更新操作
    mainInfo = Object.assign(mainInfo, masterData);
 
    try {
      let plateInfo = await this.dbRead.findOne(BasePlate, {
        plateCode: masterData.plateCode
      });
      if (!plateInfo) {
        this.info.result = false;
        this.info.msg = `${masterData.plateCode}器具编号不存在`;
        ctx.body = this.info;
        return;
      }
 
      // 新建时,生成单据编号
      if (!mainInfo.order_Id) {
        let orderCode = await ctx.service.common.getCodeRegular(102);
        mainInfo.orderCode = orderCode;
      }
 
      // 主表求和字段计算
      let total: any = detailList.reduce(
        (totalItem: any, currItem) => {
          totalItem.totalQuantity += currItem.quantity;
          return totalItem;
        },
        { totalQuantity: 0 }
      );
 
      // 状态默认为新建
      if (!mainInfo.statusText) {
        mainInfo.statusText = "新建";
      }
      mainInfo.orderType = masterData.orderType;
      mainInfo.storage_Id = masterData.storage_Id;
      mainInfo.storageName = masterData.storageName;
      mainInfo.plateType = plateInfo.plateType;
      mainInfo.plateCode = masterData.plateCode;
      mainInfo.createDate = new Date();
      mainInfo.createID = userInfo.user_Id;
      mainInfo.creator = userInfo.userTrueName;
      mainInfo.totalQuantity = total.totalQuantity;
      mainInfo.applyDate = new Date();
      mainInfo.consignor_Id = 30;
      mainInfo.consignorCode = "GX30";
      mainInfo.consignorName = "广州西门子";
      await this.setAccountInfo(mainInfo);
      await this.dbWrite.save(mainInfo); // 保存主表信息
 
      for (let item of detailList) {
        let detailInfo = new PurchaseOrderList();
        // 必须做这边操作,编辑时才执行更新操作
        detailInfo = Object.assign(detailInfo, item);
 
        detailInfo.order_Id = mainInfo.order_Id;
        detailInfo.productCode = item.productCode;
        detailInfo.product_Id = item.product_Id;
        detailInfo.productName = item.productName;
        detailInfo.productModel = item.productModel;
        detailInfo.produceDate = item.produceDate;
        detailInfo.productSpec = item.productSpec;
        detailInfo.produceDate = item.produceDate;
        detailInfo.plateCode = item.plateCode;
        // detailInfo.plateType = item.plateType;
        detailInfo.quantity = item.quantity;
        // detailInfo.validQty = item.validQty;
 
        await this.setAccountInfo(detailInfo);
        await this.dbWrite.save(detailInfo); // 保存明细数据
        details.push(detailInfo);
      }
 
      this.info.data = mainInfo;
      this.info.data2 = details;
      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 confirm() {
  //   let { ctx } = this;
  //   try {
  //     let body = ctx.request.body;
  //     var order_Id = body.order_Id;
  //     var orderList_Id = body.orderList_Id;
  //     if (!order_Id) {
  //       this.info.result = false;
  //       this.info.msg = "order_Id不能为空";
  //       ctx.body = this.info;
  //       return;
  //     }
  //     var orderInfo = await this.dbRead.findOne(SaleOrder, {
  //       order_Id: order_Id
  //     });
  //     if (orderInfo.statusText !== "部分出库") {
  //       this.info.result = false;
  //       this.info.msg = "只有部分出库可以进行操作!";
  //       ctx.body = this.info;
  //       return;
  //     }
  //     var saleOrderList = await this.dbRead.findOne(SaleOrderList, {
  //       orderList_Id: orderList_Id
  //     });
  //     // 当前商品只会占一条库存明细
  //     var holderInfo = await this.dbRead.findOne(vBaseProductPlaceHolder, {
  //       mainID: order_Id,
  //       detailID: orderList_Id,
  //       className: "销售订单"
  //     });
  //     if (holderInfo) {
  //       var postionInfo = await this.dbRead.findOne(BaseProductPosition, {
  //         productPosition_Id: holderInfo.productPosition_Id
  //       });
  //       // 通过物料寻找器具种类,取器具种类的库区和仓库 寻找空货位
 
  //       var baseProductInfo = await this.dbRead.findOne(BaseProductInfo, {
  //         product_Id: saleOrderList.product_Id
  //       });
  //       if (!baseProductInfo) {
  //         this.info.result = false;
  //         this.info.msg = "获取商品信息异常";
  //         ctx.body = this.info;
  //         return;
  //       }
  //       var basePlateTypeInfo = await this.dbRead.findOne(BasePlateType, {
  //         plateType_Id: baseProductInfo.plateType_Id
  //       });
 
  //       if (!basePlateTypeInfo) {
  //         this.info.result = false;
  //         this.info.msg = "获取器具种类异常";
  //         ctx.body = this.info;
  //         return;
  //       }
  //       let basePositionInfo = await this.dbRead
  //         .createQueryBuilder(BasePosition, "t")
  //         .where(
  //           "storage_Id=:storage_Id and areaCode=:areaCode and   NOT EXISTS(SELECT 1 FROM dbo.vBase_ProductPosition where positionName=t.positionName  and validQty>0 )",
  //           { storage_Id: basePlateTypeInfo.storage_Id, areaCode: basePlateTypeInfo.areaCode }
  //         )
  //         .getOne();
  //       if (!basePositionInfo) {
  //         this.info.result = false;
  //         this.info.msg = basePlateTypeInfo.storageName + basePlateTypeInfo.areaCode + "没有空货位了";
  //         ctx.body = this.info;
  //         return;
  //       }
  //       postionInfo.positionName = basePositionInfo.positionName;
  //       postionInfo.areaCode = basePositionInfo.areaCode;
  //       postionInfo.storageName = basePositionInfo.storageName;
  //       postionInfo.storage_Id = basePositionInfo.storage_Id;
  //       await this.dbWrite.save(postionInfo);
 
  //       // 新建分拣区回库单
  //       var sortingCloutReturnInfo = new PurchaseOrder();
  //       sortingCloutReturnInfo.orderCode = await ctx.service.common.getCodeRegular(379);
 
  //       sortingCloutReturnInfo.consignor_Id = orderInfo.consignor_Id;
  //       sortingCloutReturnInfo.consignorCode = orderInfo.consignorCode;
  //       sortingCloutReturnInfo.consignorName = orderInfo.consignorName;
  //       sortingCloutReturnInfo.orderType = "分拣区回拣";
  //       sortingCloutReturnInfo.storage_Id = basePositionInfo.storage_Id;
  //       sortingCloutReturnInfo.storageName = basePositionInfo.storageName;
  //       sortingCloutReturnInfo.statusText = "全部收货";
  //       sortingCloutReturnInfo.totalQuantity = postionInfo.productStorage;
  //       sortingCloutReturnInfo.quantity = holderInfo.orignHolderStorage;
  //       sortingCloutReturnInfo.completeQuantity = holderInfo.orignHolderStorage - postionInfo.productStorage;
  //       await this.dbWrite.save(sortingCloutReturnInfo);
  //       var returnListInfo = new PurchaseOrderList();
  //       returnListInfo.orderPlan_Id = orderInfo.order_Id;
  //       returnListInfo.orderPlanCode = orderInfo.orderCode;
  //       returnListInfo.order_Id = sortingCloutReturnInfo.order_Id;
  //       returnListInfo.product_Id = postionInfo.product_Id;
  //       returnListInfo.productCode = postionInfo.productCode;
  //       returnListInfo.productName = postionInfo.productName;
  //       returnListInfo.totalQuantity = postionInfo.productStorage;
  //       returnListInfo.quantity = postionInfo.productStorage;
  //       returnListInfo.totalQuantity = holderInfo.orignHolderStorage;
  //       returnListInfo.completeQuantity = holderInfo.orignHolderStorage - postionInfo.productStorage;
  //       returnListInfo.plateCode = orderInfo.plateCode;
  //       returnListInfo.plateName = orderInfo.plateName;
  //       returnListInfo.shelfSpace = postionInfo.positionName;
  //       await this.dbWrite.save(returnListInfo);
 
  //       orderInfo.statusText = "已回拣";
  //       await this.dbWrite.save(orderInfo);
  //     } else {
  //       this.info.result = false;
  //       this.info.msg = "获取库存异常";
  //       ctx.body = this.info;
  //       return;
  //     }
  //   } catch (ex) {
  //     this.info.result = false;
  //     this.info.msg = "错误信息:" + ex.message;
  //   }
  //   ctx.body = this.info;
  // }
  //#endregion
 
  //#region 分拣区回拣PC
  // @Post()
  // public async backOrderPc() {
  //   let { ctx } = this;
  //   try {
  //     let body = ctx.request.body;
  //     var order_Id = body.order_Id;
  //     var jsonDetails = body.jsonDetails;
  //     if (!order_Id) {
  //       this.info.result = false;
  //       this.info.msg = "order_Id不能为空";
  //       ctx.body = this.info;
  //       return;
  //     }
  //     var orderInfo = await this.dbRead.findOne(SaleOrder, {
  //       order_Id: order_Id
  //     });
  //     if (orderInfo.statusText !== "部分出库") {
  //       this.info.result = false;
  //       this.info.msg = "只有部分出库可以进行操作!";
  //       ctx.body = this.info;
  //       return;
  //     }
  //     for (var info of jsonDetails) {
  //       var orderList_Id = info.orderList_Id;
  //       var saleOrderList = await this.dbRead.findOne(SaleOrderList, {
  //         orderList_Id: orderList_Id
  //       });
  //       // 当前商品只会占一条库存明细
  //       var holderInfo = await this.dbRead.findOne(vBaseProductPlaceHolder, {
  //         mainID: order_Id,
  //         detailID: orderList_Id,
  //         className: "销售订单"
  //       });
  //       if (holderInfo) {
  //         var postionInfo = await this.dbRead.findOne(BaseProductPosition, {
  //           productPosition_Id: holderInfo.productPosition_Id
  //         });
  //         // 通过物料寻找器具种类,取器具种类的库区和仓库 寻找空货位
 
  //         var baseProductInfo = await this.dbRead.findOne(BaseProductInfo, {
  //           product_Id: saleOrderList.product_Id
  //         });
  //         if (!baseProductInfo) {
  //           this.info.result = false;
  //           this.info.msg = "获取商品信息异常";
  //           ctx.body = this.info;
  //           return;
  //         }
  //         var basePlateTypeInfo = await this.dbRead.findOne(BasePlateType, {
  //           plateType_Id: baseProductInfo.plateType_Id
  //         });
 
  //         if (!basePlateTypeInfo) {
  //           this.info.result = false;
  //           this.info.msg = "获取器具种类异常";
  //           ctx.body = this.info;
  //           return;
  //         }
  //         let basePositionInfo = await this.dbRead
  //           .createQueryBuilder(BasePosition, "t")
  //           .where(
  //             "storage_Id=:storage_Id and areaCode=:areaCode and   NOT EXISTS(SELECT 1 FROM dbo.vBase_ProductPosition where positionName=t.positionName  and validQty>0 )",
  //             { storage_Id: basePlateTypeInfo.storage_Id, areaCode: basePlateTypeInfo.areaCode }
  //           )
  //           .getOne();
  //         if (!basePositionInfo) {
  //           this.info.result = false;
  //           this.info.msg = basePlateTypeInfo.storageName + basePlateTypeInfo.areaCode + "没有空货位了";
  //           ctx.body = this.info;
  //           return;
  //         }
  //         postionInfo.positionName = basePositionInfo.positionName;
  //         postionInfo.areaCode = basePositionInfo.areaCode;
  //         postionInfo.storageName = basePositionInfo.storageName;
  //         postionInfo.storage_Id = basePositionInfo.storage_Id;
  //         await this.dbWrite.save(postionInfo);
 
  //         // 新建分拣区回库单
  //         var sortingCloutReturnInfo = new PurchaseOrder();
  //         sortingCloutReturnInfo.orderCode = await ctx.service.common.getCodeRegular(379);
 
  //         sortingCloutReturnInfo.consignor_Id = orderInfo.consignor_Id;
  //         sortingCloutReturnInfo.consignorCode = orderInfo.consignorCode;
  //         sortingCloutReturnInfo.consignorName = orderInfo.consignorName;
  //         sortingCloutReturnInfo.orderType = "分拣区回拣";
  //         sortingCloutReturnInfo.storage_Id = basePositionInfo.storage_Id;
  //         sortingCloutReturnInfo.storageName = basePositionInfo.storageName;
  //         sortingCloutReturnInfo.statusText = "全部收货";
  //         sortingCloutReturnInfo.totalQuantity = postionInfo.productStorage;
  //         sortingCloutReturnInfo.quantity = holderInfo.orignHolderStorage;
  //         sortingCloutReturnInfo.completeQuantity = holderInfo.orignHolderStorage - postionInfo.productStorage;
  //         await this.dbWrite.save(sortingCloutReturnInfo);
  //         var returnListInfo = new PurchaseOrderList();
  //         returnListInfo.orderPlan_Id = orderInfo.order_Id;
  //         returnListInfo.orderPlanCode = orderInfo.orderCode;
  //         returnListInfo.order_Id = sortingCloutReturnInfo.order_Id;
  //         returnListInfo.product_Id = postionInfo.product_Id;
  //         returnListInfo.productCode = postionInfo.productCode;
  //         returnListInfo.productName = postionInfo.productName;
  //         returnListInfo.totalQuantity = postionInfo.productStorage;
  //         returnListInfo.quantity = postionInfo.productStorage;
  //         returnListInfo.totalQuantity = holderInfo.orignHolderStorage;
  //         returnListInfo.completeQuantity = holderInfo.orignHolderStorage - postionInfo.productStorage;
  //         returnListInfo.plateCode = orderInfo.plateCode;
  //         returnListInfo.plateName = orderInfo.plateName;
  //         returnListInfo.shelfSpace = postionInfo.positionName;
  //         await this.dbWrite.save(returnListInfo);
 
  //         orderInfo.statusText = "已回拣";
  //         await this.dbWrite.save(orderInfo);
  //         this.info.result = true;
  //         this.info.msg = "已回拣";
  //       } else {
  //         this.info.result = false;
  //         this.info.msg = "获取库存异常";
  //         ctx.body = this.info;
  //         return;
  //       }
  //     }
  //   } catch (ex) {
  //     this.info.result = false;
  //     this.info.msg = "错误信息:" + ex.message;
  //   }
  //   ctx.body = this.info;
  // }
  //#endregion
 
  //#region 页面加载
  // @Post()
  // public async initData() {
  //   let { ctx } = this;
  //   let body = ctx.request.body;
  //   this.info.result = true;
  //   let userInfo = await ctx.helper.userInfo();
  //   let datalist = await this.dbRead.find(PurchaseOrder, {
  //     userProduct_Id: userInfo.userProduct_Id,
  //     order_Id: body.id
  //   });
  //   let bodylist = await this.dbRead.find(PurchaseOrderList, {
  //     userProduct_Id: userInfo.userProduct_Id,
  //     order_Id: body.id
  //   });
  //   this.info.data = datalist;
  //   this.info.data2 = bodylist;
 
  //   this.ctx.body = this.info;
  // }
  //#endregion
}