| 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"; | 
| import { SaleOrder } from "../../entity/outbound/sale/saleOrder"; | 
| import { BaseForklift } from "../../entity/basicInfo/workshop/baseForklift"; | 
|   | 
| 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 { | 
|       // 分拣根据托盘号 extendField02出库数量  extendField03为1出库完成待分拣的数据 | 
|       let datalist = null; | 
|       if (body.isall) { | 
|         datalist = await this.dbRead.find(vBaseProductPosition, { | 
|           userProduct_Id: userInfo.userProduct_Id, | 
|           plateCode: body.plateCode, | 
|           productStorage: MoreThan(0), | 
|           //  extendField02: MoreThan("0"), | 
|           extendField03: "1" | 
|         }); | 
|       } else { | 
|         datalist = await this.dbRead.find(vBaseProductPosition, { | 
|           userProduct_Id: userInfo.userProduct_Id, | 
|           plateCode: body.plateCode, | 
|           productStorage: MoreThan(0), | 
|           extendField02: MoreThan("0"), | 
|           extendField03: "1" | 
|         }); | 
|       } | 
|   | 
|       // 查到数据就返回  否则就不存在 | 
|       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 扫描获取数据 | 
|   @Post() | 
|   public async endOrder() { | 
|     let { ctx } = this; | 
|     let body = ctx.request.body; | 
|     let userInfo = await ctx.helper.userInfo(); | 
|     try { | 
|       if (!body.plateCode) { | 
|         this.info.result = false; | 
|         this.info.msg = `器具编号不能为空`; | 
|         ctx.body = this.info; | 
|         return; | 
|       } | 
|       let saleOrderInfo = await this.dbRead.findOne(SaleOrder, { | 
|         where: { | 
|           userProduct_Id: userInfo.userProduct_Id, | 
|           statusText: "已完成", | 
|           plateCode: body.plateCode | 
|         }, | 
|         order: { createDate: "DESC" } | 
|       }); | 
|       if (saleOrderInfo) { | 
|         // 修改叉车司机状态为空闲中 | 
|         await this.dbWrite.update( | 
|           BaseForklift, | 
|           { | 
|             relationDrivers: saleOrderInfo.relationDrivers | 
|           }, | 
|           { | 
|             forkliftStatus: "空闲中" | 
|           } | 
|         ); | 
|         saleOrderInfo.statusText = "出库口取货完成"; | 
|         await this.dbWrite.save(saleOrderInfo); // 保存主表信息 | 
|         this.info.data = saleOrderInfo; | 
|         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 | 
| } |