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
|
}
|