import BaseController from "../baseController";
|
import { Post } from "egg-shell-decorators";
|
import * as sql from "mssql";
|
import { RequireAllocationList } from "../../entity/outbound/require/requireAllocationList";
|
import { RequireAllocation } from "../../entity/outbound/require/requireAllocation";
|
import { In } from "typeorm";
|
import { vBaseProductPlaceHolder } from "../../entity/storage/storage/vBaseProductPlaceHolder";
|
import * as XLSX from "xlsx";
|
import * as path from "path";
|
import { isNumber } from "util";
|
import { BaseStorage } from "../../entity/basicInfo/base/baseStorage";
|
import { BaseProductInfo } from "../../entity/basicInfo/base/baseProductInfo";
|
import { BasePosition } from "../../entity/basicInfo/base/basePosition";
|
// import { BaseProductPosition } from '../../entity/storage/product/baseProductPosition';
|
/**
|
* 调拨单管理
|
*/
|
export default class AllocationController extends BaseController {
|
//#region 批量分拣
|
@Post()
|
public async batchSort() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
let allocation_Ids = body.allocation_Ids;
|
let msg = [];
|
if (!allocation_Ids) {
|
this.info.result = false;
|
this.info.msg = "信息不存在";
|
this.ctx.body = this.info;
|
return;
|
}
|
try {
|
let allocations = await this.dbRead.find(RequireAllocation, {
|
allocation_Id: In(allocation_Ids)
|
});
|
for (let allocationInfo of allocations) {
|
let Allocationlist = await this.dbRead.find(RequireAllocationList, {
|
allocation_Id: allocationInfo.allocation_Id
|
});
|
if (allocationInfo.allocationText != "审核成功") {
|
this.info.msg = allocationInfo.allocationCode + "只有审核成功的单子才允许确认入库";
|
this.info.result = false;
|
this.ctx.body = this.info;
|
return;
|
}
|
if (!allocationInfo.storage_Id_In) {
|
this.info.msg = "调入仓库不能为空!";
|
this.info.result = false;
|
this.ctx.body = this.info;
|
return;
|
}
|
if (Allocationlist.length <= 0) {
|
this.info.msg = "明细为空不能分拣!";
|
this.info.result = false;
|
this.ctx.body = this.info;
|
return;
|
}
|
for (let detail of Allocationlist) {
|
if (!detail.positionName) {
|
this.info.msg = "明细货位不能为空!";
|
this.info.result = false;
|
this.ctx.body = this.info;
|
return;
|
}
|
}
|
|
const connection: any = await this.dbWrite.connection;
|
let request = new sql.Request(connection.driver.master);
|
request.input("User_Id", userInfo.user_Id);
|
request.input("allocation_Id", allocationInfo.allocation_Id);
|
request.output("outMsg", sql.NVarChar(2000));
|
let result = await request.execute("sp_Require_Allocation_Sorting");
|
let outMsg = result.output.outMsg;
|
|
if (outMsg) {
|
this.info.msg = outMsg;
|
this.info.result = false;
|
ctx.body = this.info;
|
return;
|
}
|
msg.push(allocationInfo.allocationCode + ",分拣完成!");
|
}
|
this.info.result = true;
|
this.info.msg = msg.join(",");
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 确认调拨
|
@Post()
|
public async confirmAllocation() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
let allocation_Ids = body.allocation_Ids;
|
let msg = [];
|
|
if (!allocation_Ids) {
|
this.info.result = false;
|
this.info.msg = "信息不存在";
|
this.ctx.body = this.info;
|
return;
|
}
|
try {
|
let allocations = await this.dbRead.find(RequireAllocation, {
|
allocation_Id: In(allocation_Ids)
|
});
|
for (let allocationInfo of allocations) {
|
let Allocationlist = await this.dbRead.find(RequireAllocationList, {
|
allocation_Id: allocationInfo.allocation_Id
|
});
|
if (allocationInfo.allocationText != "审核成功") {
|
this.info.msg = allocationInfo.allocationCode + "只有审核成功的单子才允许确认入库";
|
this.info.result = false;
|
this.ctx.body = this.info;
|
return;
|
}
|
if (!allocationInfo.storage_Id_In) {
|
this.info.msg = "调入仓库不能为空!";
|
this.info.result = false;
|
this.ctx.body = this.info;
|
return;
|
}
|
if (Allocationlist.length <= 0) {
|
this.info.msg = "明细为空不能分拣!";
|
this.info.result = false;
|
this.ctx.body = this.info;
|
return;
|
}
|
for (let detail of Allocationlist) {
|
if (!detail.positionName) {
|
this.info.msg = "明细货位不能为空!";
|
this.info.result = false;
|
this.ctx.body = this.info;
|
return;
|
}
|
}
|
|
//执行确认操作
|
const connection: any = await this.dbWrite.connection;
|
let request = new sql.Request(connection.driver.master);
|
request = new sql.Request(connection.driver.master);
|
request.input("modifier", userInfo.userTrueName);
|
request.input("allocation_Id", allocationInfo.allocation_Id);
|
request.output("outMsg", sql.NVarChar(2000));
|
try {
|
let result = await request.execute("sp_Require_Allocation_Check");
|
let outMsg = result.output.outMsg;
|
if (outMsg) {
|
msg.push(allocationInfo.allocationCode + "," + outMsg);
|
this.info.msg = outMsg;
|
this.info.result = false;
|
ctx.body = this.info;
|
continue;
|
}
|
} catch (e) {
|
msg.push(allocationInfo.allocationCode + "执行错误:" + e.message);
|
continue;
|
}
|
msg.push(allocationInfo.allocationCode + ",确认入库成功!");
|
}
|
this.info.result = true;
|
this.info.msg = msg.join(",");
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 获得打印拣配单数据
|
@Post()
|
public async pickBillPrint() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let allocation_Ids = body.allocation_Ids;
|
let userInfo = await ctx.helper.userInfo();
|
|
if (!allocation_Ids) {
|
this.info.result = false;
|
this.info.msg = "信息不存在";
|
this.ctx.body = this.info;
|
return;
|
}
|
try {
|
let allocations = await this.dbRead.find(RequireAllocation, {
|
allocation_Id: In(allocation_Ids)
|
});
|
let allocationlist = await this.dbRead.find(RequireAllocationList, {
|
allocation_Id: In(allocation_Ids)
|
});
|
let holderList = await this.dbRead.find(vBaseProductPlaceHolder, {
|
userProduct_Id: userInfo.userProduct_Id,
|
className: "要货调拨出库",
|
mainID: In(allocation_Ids)
|
});
|
this.info.result = true;
|
this.info.data = {
|
allocations: allocations,
|
allocationlist: allocationlist,
|
holderList: holderList
|
};
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 货主导入Excel
|
@Post()
|
public async importExcel() {
|
setTimeout(async () => {
|
await this.importExcelWork();
|
}, 0);
|
|
this.info.result = true;
|
this.info.msg = "开始上传...";
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region importExcelWork
|
private async importExcelWork() {
|
let { ctx } = this;
|
// let userInfo = await this.userInfo;
|
let userInfo = await ctx.helper.userInfo();
|
let body = ctx.request.body;
|
let redis = ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
|
let fileUrl = body.fileUrl;
|
redis.expire(body.key, 5 * 60);
|
if (!fileUrl) {
|
this.setMsg("上传文件不存在", "red");
|
return;
|
}
|
if (!body.key) {
|
this.setMsg("上传key不存在", "red");
|
return;
|
}
|
|
try {
|
let rootPath = path.resolve(); // 获得根目录
|
let filePath = rootPath + path.sep + fileUrl.replace(/\//gi, path.sep); // 上传文件路径
|
var workbook = XLSX.readFile(filePath); //整个 excel 文档
|
var sheetNames = workbook.SheetNames; //获取所有工作薄名
|
var sheet1 = workbook.Sheets[sheetNames[0]]; //根据工作薄名获取工作薄
|
let dataList = XLSX.utils.sheet_to_json(sheet1); // 获得当前sheet表单数据转为json格式
|
|
//#region 验证数据正确性
|
this.info.result = true;
|
if (!dataList.length) {
|
this.setMsg("没有可导入的数据", "red");
|
return;
|
}
|
//#endregion
|
let storageList = await this.dbRead.find(BaseStorage, {
|
userProduct_Id: userInfo.userProduct_Id
|
});
|
let productinfo = await this.dbRead.find(BaseProductInfo, {
|
productCode: In(dataList.map(item => "" + item["物料编号"])),
|
userProduct_Id: userInfo.userProduct_Id
|
});
|
let position = await this.dbRead.find(BasePosition, {
|
positionName: In(dataList.map(item => "" + item["入库货位"])),
|
userProduct_Id: userInfo.userProduct_Id
|
});
|
let i = 0;
|
// let dataRows = []; // 数据集合
|
for (let row of dataList) {
|
i += 1;
|
if (i % 20 === 1) {
|
this.setMsg(`第${i}行开始校验`, "blue");
|
}
|
let outstorage = storageList.find(item => item.storageName === row["调出仓库"]);
|
if (!outstorage) {
|
this.setMsg(`${i}、调出仓库不存在`, "red");
|
this.info.result = false;
|
}
|
let instorage = storageList.find(item => item.storageName === row["调入仓库"]);
|
if (!instorage) {
|
this.setMsg(`${i}、调入仓库不存在`, "red");
|
this.info.result = false;
|
}
|
if (!row["调拨日期"]) {
|
this.setMsg(`${i}、调出仓库不能为空`, "red");
|
this.info.result = false;
|
}
|
let productname = productinfo.find(item => item.productCode === row["物料编号"]);
|
if (!productname) {
|
this.setMsg(`${i}、产品编号不能为空`, "red");
|
this.info.result = false;
|
}
|
if (!row["产品名称"]) {
|
this.setMsg(`${i}、产品名称不能为空`, "red");
|
this.info.result = false;
|
}
|
if (!row["条形码"]) {
|
this.setMsg(`${i}、条形码不能为空`, "red");
|
this.info.result = false;
|
}
|
|
let quantity = row["要货数量"];
|
if (quantity && !isNumber(quantity)) {
|
this.setMsg(`${i}要货数量[${quantity}]数量请输入正整数!`, "red");
|
}
|
let inhuowei = position.find(item => item.positionName === row["入库货位"]);
|
if (!inhuowei) {
|
this.setMsg(`${i}、入库货位不存在`, "red");
|
this.info.result = false;
|
}
|
// if (!row["要货单号"]) {
|
// this.setMsg(`${i}、要货单号不能为空`, "red");
|
// this.info.result = false;
|
// }
|
}
|
if (this.isMsgError) {
|
this.setMsg(`导入数据有错误,请处理好重新导入`, "red");
|
this.setMsg("-1");
|
return;
|
}
|
|
// 分组
|
let groupList = dataList.reduce(
|
(all: Array<any>, next) =>
|
all.some(item => item["调出仓库"] == next["调出仓库"] && item["调入仓库"] == next["调入仓库"] && item["调拨日期"] == next["调拨日期"])
|
? all
|
: [...all, next],
|
[]
|
);
|
i = 0;
|
// 循环处理分组数据,groupList对应的是主表数据
|
for (let item of groupList) {
|
i++;
|
if (i % 20 === 1) {
|
this.setMsg(`第${i}行开始导入`, "blue");
|
}
|
let outstorage = storageList.find(row => row.storageName === item["调出仓库"]);
|
let instorage = storageList.find(row => row.storageName === item["调入仓库"]);
|
|
let Allocation = new RequireAllocation();
|
|
let allocationCode = await ctx.service.common.getCodeRegular(1742);
|
Allocation.allocationCode = allocationCode;
|
Allocation.allocationText = "新建";
|
Allocation.storageName = item["调出仓库"];
|
Allocation.storage_Id = outstorage.storage_Id;
|
Allocation.storageName_In = item["调入仓库"];
|
Allocation.storage_Id_In = instorage.storage_Id;
|
Allocation.sortingStatus = 1;
|
Allocation.sortingDate = null;
|
Allocation.allocationDate = new Date();
|
await this.setAccountInfo(Allocation);
|
// 明细数据
|
let detailList = dataList.filter(
|
row => row["调出仓库"] === item["调出仓库"] && row["调入仓库"] === item["调入仓库"] && row["调拨日期"] === item["调拨日期"]
|
);
|
// 主表求和字段计算
|
let total: any = detailList.reduce(
|
(totalItem: any, currItem) => {
|
totalItem.totalQuantity += currItem["要货数量"];
|
totalItem.ProductMoney += currItem["物料金额"];
|
return totalItem;
|
},
|
{ totalQuantity: 0, purchaseMoney: 0 }
|
);
|
Allocation.totalQuantity = total.totalQuantity;
|
Allocation.productMoney = total.purchaseMoney;
|
// 保存主表
|
await this.dbWrite.save(Allocation);
|
// let allocationInfo = await this.dbRead.findOne()
|
// 处理明细,将明细挂载到主表下
|
for (let detail of detailList) {
|
let productCode = detail["物料编号"];
|
let product = await this.dbRead.findOne(BaseProductInfo, {
|
productCode: productCode
|
});
|
let AllocationList = new RequireAllocationList();
|
AllocationList.productCode = productCode;
|
AllocationList.product_Id = product.product_Id;
|
AllocationList.productModel = product.productModel;
|
AllocationList.productName = product.productName;
|
AllocationList.productSpec = product.productSpec;
|
AllocationList.goodsQuantity = detail["要货数量"];
|
AllocationList.sortingQuantity = 0;
|
AllocationList.positionName = detail["入库货位"];
|
AllocationList.productPrice = product.purchasePrice;
|
AllocationList.productTotal = Number(detail["要货数量"]) * (product.purchasePrice || 0);
|
AllocationList.goodsCode = detail["要货单号"];
|
|
AllocationList.createDate = new Date();
|
AllocationList.creator = userInfo.userTrueName;
|
AllocationList.createID = userInfo.user_Id;
|
// //建立关系
|
AllocationList.requireAllocation = Allocation;
|
await this.dbWrite.save(AllocationList);
|
}
|
this.setMsg(`第${i}行导入成功`, "blue");
|
}
|
// redis.rpush(body.key, `${i}、第${i}行导入成功`);
|
// this.setMsg(`${i}、第${i}行导入成功`, "blue");
|
// this.setMsg(`第${i}行导入成功`, "blue");
|
} catch (ex) {
|
this.setMsg("出现异常:" + ex.message, "red");
|
this.info.result = false;
|
}
|
this.setMsg("-1");
|
}
|
//#endregion
|
}
|