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
import * as sql from "mssql";
import BaseService from "../baseService";
import { BasePosition } from "../../entity/basicInfo/base/basePosition";
import { Not, In } from "typeorm";
import { PurchaseShelve } from "../../entity/inbound/purchase/purchaseShelve";
import { PurchaseShelveList } from "../../entity/inbound/purchase/purchaseShelveList";
// import { vBaseProductPosition } from "../../entity/storage/product/vBaseProductPosition";
import moment = require("moment");
import { BaseProductInfo } from "../../entity/basicInfo/base/baseProductInfo";
import { SysUserLog } from "../../entity/sys/core/sysUserLog";
// import { BaseProductInfoHistory } from "../../entity/basicInfo/base/baseProductInfoHistory";
 
/**
 * 上架码盘Service
 */
export default class ShelveStackingScanService extends BaseService {
  //#region  上架码盘 - 获取数据
  public async getData(storage_Id: number, positionName: string, productCode: string) {
    try {
      let sKU_ProductToMultiBarcode = await this.ctx.service.common.getConfigBool("sKU_ProductToMultiBarcode"); //支持一品多码
      var where = "";
      if (sKU_ProductToMultiBarcode) {
        where = `(PI.productCode COLLATE Chinese_PRC_CS_AS=@2 
                    OR PI.relationCode COLLATE Chinese_PRC_CS_AS=@2 
                    OR PI.relationCode2 COLLATE Chinese_PRC_CS_AS=@2 
                    OR PI.relationCode3 COLLATE Chinese_PRC_CS_AS=@2
                    OR PI.relationCode4 COLLATE Chinese_PRC_CS_AS=@2
                    OR PI.relationCode5 COLLATE Chinese_PRC_CS_AS=@2
                    OR PI.middleBarcode COLLATE Chinese_PRC_CS_AS=@2
                    OR PI.bigBarcode COLLATE Chinese_PRC_CS_AS=@2)
                `;
      } else {
        where = "PI.productCode COLLATE Chinese_PRC_CS_AS=@2 ";
      }
 
      let sql = "";
      sql = `SELECT  PI.product_Id, PI.productCode, PI.productName, PI.productSpec, PI.purchasePrice, 
        0 AS shelveQuantity, PP.positionName, PP.plateCode,MAX( PP.produceDate) AS produceDate, PP.batchNumber,
        PI.relationCode, PI.relationCode2, PI.relationCode3, PI.relationCode4, PI.relationCode5,
        PI.middleBarcode, middleUnitConvert, PI.bigBarcode, PI.unitConvert,PI.smallUnit,PP.validQty,PP.inStorageDate,PP.productPosition_Id,PP.poCode,PP.trackingNumber,PP.saleCode
        FROM      dbo.vBase_ProductInfo_SKU AS PI INNER JOIN
          dbo.vBase_ProductPosition AS PP ON PI.product_Id = PP.product_Id
        WHERE (${where})
        AND PP.storage_Id=@0
        --AND PP.positionName=@1
        AND ISNULL(plateCode, '')=''
        AND PP.validQty>0 GROUP BY PI.product_Id, PI.productCode, PI.productName, PI.productSpec, PI.purchasePrice, PP.positionName, PP.plateCode, PP.batchNumber,
        PI.relationCode, PI.relationCode2, PI.relationCode3, PI.relationCode4, PI.relationCode5,
        PI.middleBarcode, middleUnitConvert, PI.bigBarcode, PI.unitConvert,PI.smallUnit,PP.validQty,PP.inStorageDate,PP.productPosition_Id,PP.poCode,PP.trackingNumber,PP.saleCode`;
 
      let orderList = await this.dbRead.query(sql, [storage_Id, positionName, productCode]);
      for (let item of orderList) {
        item.inStorageDate = moment(item.inStorageDate).format("YYYY-MM-DD");
      }
      if (orderList.length == 0) {
        this.info.result = false;
        this.info.msg = "未找到可以码盘的数据!";
        return this.info;
      } else {
        this.info.result = true;
        this.info.msg = "";
        this.info.data = orderList;
 
        return this.info;
      }
    } catch (e) {
      this.info.result = false;
      this.info.msg = e.message;
      return this.info;
    }
  }
  //#endregion
 
  //#region  码盘操作- 保存
  public async noBillEnterSave(orderDataList: Array<any>) {
    let { ctx } = this;
    let userInfo = await this.userInfo;
    let menu_Id = 103;
    if (orderDataList.length == 0) {
      this.info.result = false;
      this.info.msg = "已扫描数量不能全部为零";
      return this.info;
    }
    try {
      let excelDT = new sql.Table();
      excelDT.columns.add("storage_Id", sql.Int);
      excelDT.columns.add("storageName", sql.NVarChar(50));
      excelDT.columns.add("product_Id", sql.BigInt);
      excelDT.columns.add("finishedQuantity", sql.Int);
      excelDT.columns.add("positionName", sql.NVarChar(50));
      excelDT.columns.add("produceDate", sql.NVarChar(50));
 
      excelDT.columns.add("purchasePrice", sql.Decimal);
      excelDT.columns.add("batchNumber", sql.NVarChar(50));
      excelDT.columns.add("productSpec", sql.NVarChar(50));
      excelDT.columns.add("plateCode", sql.NVarChar(50));
      excelDT.columns.add("enterMode", sql.NVarChar(50));
 
      for (var dataItem of orderDataList) {
        excelDT.rows.add(
          dataItem.storage_Id,
          dataItem.storageName,
          dataItem.product_Id,
          dataItem.finishedQuantity,
          dataItem.positionName,
          dataItem.produceDate,
          dataItem.purchasePrice,
          dataItem.batchNumber,
          dataItem.productSpec,
          dataItem.plateCode,
          dataItem.enterMode
        );
      }
      let enterCode = await ctx.service.common.getCodeRegular(103);
      let shelveCode = await ctx.service.common.getCodeRegular(269);
 
      const connection: any = await this.dbWrite.connection;
      let request = new sql.Request(connection.driver.master);
      request.input("enterCode", enterCode);
      request.input("shelveCode", shelveCode);
      request.input("user_Id", userInfo.user_Id);
      request.input("menu_Id", menu_Id);
      request.input("excelDT", excelDT);
      request.output("outMsg", sql.NVarChar(2000));
      let result = await request.execute("sp_Purchase_Enter_NoBillEnterAndShelve");
      let outMsg = result.output.outMsg;
 
      if (outMsg != null && outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
        return this.info;
      } else {
        let positionNames = orderDataList.map(item => {
          return item.positionName;
        });
        var hasPosition = await this.dbRead
          .createQueryBuilder(BasePosition, "t")
          .where({
            positionType: Not(4),
            positionName: In(positionNames)
          })
          .getOne();
        if (hasPosition) {
          this.info.msg = "确认上架单成功";
        } else {
          if (ctx.service.common.getConfigBool("In_GenerateShelve")) {
            this.info.msg = "确认入库并自动生成上架单成功";
          } else {
            this.info.msg = "确认入库成功";
          }
        }
        this.info.result = true;
        return this.info;
      }
    } catch (error) {
      this.info.msg = error.message;
      this.info.result = false;
      return this.info;
    }
  }
  //#endregion
 
  //#region  新 码盘操作 - 保存
  public async billEnterSave(orderDataList: Array<any>, plateCode: string, percentAge: number, shelveType: string) {
    if (orderDataList.length == 0) {
      this.info.result = false;
      this.info.msg = "已扫描数量不能全部为零";
      return this.info;
    }
 
    let { ctx } = this;
    try {
      let userInfo = await ctx.helper.userInfo();
 
      // 创建主表
      let shelve = new PurchaseShelve();
      shelve.shelveCode = await ctx.service.common.getCodeRegular(269);
      let dataItem = orderDataList[0];
      shelve.storageName = dataItem.storageName;
      shelve.orderType = "入库上架";
      shelve.consignor_Id = 30;
      shelve.consignorCode = "GX30";
      shelve.consignorName = "广州西门子";
 
      // if (plateType.value === "1" || plateType.value === "2") {
      //   plateType.label = 1;
      // } else if (plateType.value === "3") {
      //   plateType.label = 2;
      // }
      // shelve.provider_Id = plateType.value;
      // shelve.providerCode = plateType.label;
      shelve.provider_Id = percentAge;
      shelve.storage_Id = dataItem.storage_Id;
      shelve.positionName = ""; // dataItem.positionName;
      shelve.plateCode = plateCode;
      shelve.onShelveStatus = "待上架";
      shelve.userProduct_Id = userInfo.userProduct_Id;
      await this.setAccountInfo(shelve);
      await this.dbWrite.save(shelve);
      // 合计重量
      let totalWeight = 0;
      // 合计数量
      let totalQuantity = 0;
 
      // 保存明细
      for (let dataItem of orderDataList) {
        let shelveList = new PurchaseShelveList();
        // let totalWeight = 0;
        // 获取物料信息
        let productPosition = await this.dbRead.findOne(BaseProductInfo, {
          productCode: dataItem.productCode
        });
        if (productPosition) {
          shelveList.product_Id = productPosition.product_Id;
          shelveList.productCode = productPosition.productCode;
          shelveList.productName = productPosition.productName;
          shelveList.productSpec = productPosition.productSpec;
          shelveList.smallUnit = productPosition.smallUnit;
        }
        // shelve.totalWeight = totalWeight;
        shelveList.quantity = dataItem.finishedQuantity;
        totalWeight += dataItem.totalWeight;
        shelveList.isBoosterArm = dataItem.isBoosterArm;
        totalQuantity += Number(dataItem.finishedQuantity);
 
        shelveList.poCode = dataItem.poCode;
        shelveList.trackingNumber = dataItem.extendField04;
        shelveList.saleCode = dataItem.saleCode;
        shelveList.extendField09 = dataItem.itemNumber;
        shelveList.extendField08 = dataItem.extendField08;
        shelveList.limitDate = dataItem.limitDate;
        shelveList.extendField06 = dataItem.extendField06;
 
        shelveList.shelve_Id = shelve.shelve_Id;
        shelveList.creator = userInfo.userTrueName;
        shelveList.createID = userInfo.user_Id;
        shelveList.sortingStatus = 1;
        await this.dbWrite.save(shelveList);
      }
 
      shelve.totalQuantity = totalQuantity;
      shelve.totalWeight = totalWeight;
      await this.setAccountInfo(shelve);
      await this.dbWrite.save(shelve);
      // 添加日志
      let userLog = new SysUserLog();
      userLog.operateType = "码盘上架";
      userLog.action = shelveType + "-" + shelve.shelveCode;
      userLog.iP = this.ctx.request.ip;
      userLog.userTrueName = userInfo.userTrueName;
 
      await this.setAccountInfo(userLog);
      await this.dbWrite.save(userLog);
 
      this.info.result = true;
      this.info.msg = "添加成功!";
      return this.info;
    } catch (error) {
      this.info.msg = error.message;
      this.info.result = false;
      return this.info;
    }
  }
  //#endregion
}