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
import BaseController from "../baseController";
import { Post } from "egg-shell-decorators";
import { vBaseProductPosition } from "../../entity/storage/product/vBaseProductPosition";
import { SaleOrderPicking } from "../../entity/outbound/manufacture/saleOrderPicking";
import { SaleOrderPickingList } from "../../entity/outbound/manufacture/saleOrderPickingList";
import { BaseProductPosition } from "../../entity/storage/product/baseProductPosition";
import { MoreThan } from "typeorm";
 
export default class OrderJxController extends BaseController {
  //#region 扫描获取数据
  @Post()
  public async getScanData() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    try {
      let datalist = await this.dbRead.find(vBaseProductPosition, {
        userProduct_Id: userInfo.userProduct_Id,
        positionName: "分拣区",
        plateCode: body.plateCode,
        productStorage: MoreThan(0)
      });
      if (datalist.length) {
        this.info.data = datalist;
        this.info.result = true;
      } else {
        this.info.result = false;
        this.info.msg = `${body.plateCode}不存在`;
      }
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#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 SaleOrderPicking(); // 主表数据
    // 必须做这边操作,编辑时才执行更新操作
    mainInfo = Object.assign(mainInfo, masterData);
 
    try {
      // 新建时,生成单据编号
      if (!mainInfo.orderPicking_Id) {
        let orderPickingCode = await ctx.service.common.getCodeRegular(116);
        mainInfo.orderPickingCode = orderPickingCode;
      }
 
      // 主表求和字段计算
      let total: any = detailList.reduce(
        (totalItem: any, currItem) => {
          totalItem.totalQuanity += currItem.quantity;
          return totalItem;
        },
        { totalQuanity: 0 }
      );
 
      // 状态默认为新建
      if (!mainInfo.statusText) {
        mainInfo.statusText = "待上架";
      }
      mainInfo.orderType = body.orderType;
      mainInfo.storage_Id = masterData.storage_Id;
      mainInfo.storageName = masterData.storageName;
      mainInfo.plateType = masterData.plateType;
      mainInfo.plateCode = masterData.plateCode;
      mainInfo.createDate = new Date();
      mainInfo.createID = userInfo.user_Id;
      mainInfo.creator = userInfo.userTrueName;
      mainInfo.totalQuanity = total.totalQuanity;
      await this.setAccountInfo(mainInfo);
      await this.dbWrite.save(mainInfo); // 保存主表信息
 
      let detailInfo = new SaleOrderPickingList();
      for (let item of detailList) {
        // 必须做这边操作,编辑时才执行更新操作
        detailInfo = Object.assign(detailInfo, item);
 
        detailInfo.orderPicking_Id = mainInfo.orderPicking_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.quantityOrder;
        detailInfo.validQty = item.validQty;
 
        await this.setAccountInfo(detailInfo);
        await this.dbWrite.save(detailInfo); // 保存明细数据
        details.push(detailInfo);
      }
      // 库存校验
      for (let detail of details) {
        let ppInfo = await this.dbRead.findOne(BaseProductPosition, {
          plateCode: masterData.plateCode,
          product_Id: detail.product_Id,
          productStorage: MoreThan(0)
        });
        if (!ppInfo || ppInfo.productStorage < detail.quantity) {
          this.info.result = false;
          this.info.msg = `${masterData.plateCode}器具没有库存`;
          ctx.body = this.info;
          return;
        }
      }
 
      // 扣减库存
      for (let detail of details) {
        let ppInfo = await this.dbRead.findOne(BaseProductPosition, {
          plateCode: mainInfo.plateCode,
          product_Id: detail.product_Id,
          productStorage: MoreThan(0)
        });
 
        // 将库存设置为0
        await this.dbWrite.update(
          BaseProductPosition,
          {
            productPosition_Id: ppInfo.productPosition_Id
          },
          {
            productStorage: ppInfo.productStorage - detail.quantity,
            storageStatus: "余料"
          }
        );
      }
 
      // 将单据状态修改为确认完成
      mainInfo.statusText = "已出库";
      await this.dbWrite.save(mainInfo);
      this.info.result = true;
      this.info.msg = "确认出库成功";
 
      this.info.data = mainInfo;
      this.info.data2 = details;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region save 确认出库
  @Post()
  public async confirm() {
    let { ctx } = this;
    let body = ctx.request.body;
    let id = body.id;
    let masterData = await this.dbRead.findOne(SaleOrderPicking, id);
 
    try {
      let details = await this.dbRead.find(SaleOrderPickingList, {
        orderPicking_Id: id
      });
      // 库存校验
      for (let detail of details) {
        let ppInfo = await this.dbRead.findOne(BaseProductPosition, {
          plateCode: masterData.plateCode,
          product_Id: detail.product_Id,
          productStorage: MoreThan(0)
        });
        if (!ppInfo || ppInfo.productStorage < detail.quantity) {
          this.info.result = false;
          this.info.msg = `${masterData.plateCode}器具没有库存`;
          ctx.body = this.info;
          return;
        }
      }
 
      // 扣减库存
      for (let detail of details) {
        let ppInfo = await this.dbRead.findOne(BaseProductPosition, {
          plateCode: masterData.plateCode,
          product_Id: detail.product_Id,
          productStorage: MoreThan(0)
        });
 
        // 将库存设置为0
        await this.dbWrite.update(
          BaseProductPosition,
          {
            productPosition_Id: ppInfo.productPosition_Id
          },
          {
            productStorage: ppInfo.productStorage - detail.quantity,
            storageStatus: "余料"
          }
        );
      }
 
      // 将单据状态修改为确认完成
      masterData.statusText = "已出库";
      await this.dbWrite.save(masterData);
 
      this.info.result = true;
      this.info.msg = "确认出库成功";
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
}