import BaseService from "../baseService";
|
import { StorageCheck } from "../../entity/storage/check/storageCheck";
|
import { StorageCheckList } from "../../entity/storage/check/storageCheckList";
|
|
export default class CheckService extends BaseService {
|
//#region 盘点扫描获取明细
|
public async getCheckInfoList(checkCode: string) {
|
let { ctx } = this;
|
let userInfo = await ctx.helper.userInfo();
|
this.info.result = false;
|
if (!checkCode) {
|
this.info.msg = "出库单号不能为空!";
|
return this.info;
|
}
|
// 查询盘点单主表
|
let dataInfo = await this.dbRead
|
.createQueryBuilder(StorageCheck, "t")
|
.where("checkCode=:checkCode and userProduct_Id=:userProduct_Id", {
|
checkCode: checkCode,
|
userProduct_Id: userInfo.userProduct_Id
|
})
|
.getOne();
|
if (!dataInfo) {
|
this.info.msg = "未找到盘点数据!";
|
this.info.result = false;
|
return this.info;
|
}
|
if (dataInfo.statusText !== "新建") {
|
this.info.msg = "盘点单不是新建状态,不能盘点!";
|
this.info.result = false;
|
return this.info;
|
}
|
// 获取盘点明细数据
|
let checkDetailList = await this.dbRead
|
.createQueryBuilder(StorageCheckList, "t")
|
.where("check_Id=:check_Id and checkQuantity is null", {
|
check_Id: dataInfo.check_Id
|
})
|
.getMany();
|
if (checkDetailList.length == 0) {
|
this.info.msg = "盘点单号下没有可以盘点的明细!";
|
this.info.result = false;
|
return this.info;
|
}
|
let info = {
|
checkDetailList: checkDetailList,
|
storageCheckInfo: {
|
check_Id: dataInfo.check_Id,
|
checkCode: dataInfo.checkCode
|
}
|
};
|
this.info.result = true;
|
this.info.msg = null;
|
this.info.data = info;
|
return this.info;
|
}
|
//#endregion
|
|
//#region 盘点扫描提交
|
public async saveCheckByCheckList(checkCode: string, dataArray: Array<any>) {
|
let { ctx } = this;
|
let userInfo = await ctx.helper.userInfo();
|
this.info.result = false;
|
if (!checkCode) {
|
this.info.result = false;
|
this.info.msg = "盘点单号不能为空!";
|
return this.info;
|
}
|
// 查询盘点单主表
|
let dataInfo = await this.dbRead
|
.createQueryBuilder(StorageCheck, "t")
|
.where("checkCode=:checkCode And userProduct_Id=:userProduct_Id", {
|
checkCode: checkCode,
|
userProduct_Id: userInfo.userProduct_Id
|
})
|
.getOne();
|
if (!dataInfo) {
|
this.info.msg = "未找到盘点数据!";
|
this.info.result = false;
|
return this.info;
|
}
|
if (dataInfo.statusText !== "新建") {
|
this.info.result = false;
|
this.info.msg = "盘点单[" + dataInfo.checkCode + "]不是新建状态的,不能再修改!";
|
return this.info;
|
}
|
try {
|
//#region 盘点货位 判断货位是否存在
|
let positionArray: Array<any> = [];
|
for (var model of dataArray) {
|
if (!model.productCode) {
|
this.info.msg = "物料编号为空,请检查数据!";
|
this.info.result = false;
|
return this.info;
|
}
|
if (!model.positionName) {
|
this.info.msg = "物料[" + model.productCode + "]的货位是空的!";
|
this.info.result = false;
|
return this.info;
|
}
|
positionArray.push(model.positionName);
|
}
|
let positionStr = positionArray.join(",");
|
let sql = `SELECT Col AS PositionName FROM Func_SplitStrToTable(@0)
|
WHERE Col NOT IN(
|
SELECT PositionName FROM dbo.Base_Position WHERE storage_Id=@1
|
)`;
|
let dataPosition = await this.dbRead.query(sql, [positionStr, dataInfo.storage_Id]);
|
if (dataPosition.length) {
|
let _msg = dataPosition.map(item => item.positionName).join(",");
|
this.info.result = false;
|
this.info.msg = "货位[" + _msg + "]不存在";
|
return this.info;
|
}
|
//#endregion
|
|
//#region 验证物料在某货位的拣货数量是否正确
|
for (var modelOne of dataArray) {
|
let sqlOne = `SELECT COUNT(*) FROM dbo.Storage_CheckList WHERE Check_Id=@0 AND ProductModel=@1 AND PositionName=@2`;
|
let _count = await this.dbRead.query(sqlOne, [dataInfo.check_Id, modelOne.productCode, modelOne.positionName]);
|
if (_count == 0) {
|
this.info.result = false;
|
this.info.msg =
|
"盘点单[" +
|
dataInfo.checkCode +
|
"]中不存在物料[" +
|
modelOne.productCode +
|
"]货位[" +
|
modelOne.positionName +
|
"]的明细数据,请盘点其他物料或者货位数据!";
|
return this.info;
|
}
|
}
|
//#endregion
|
|
//#region 更新最后操作时间
|
let sqlTwo = `UPDATE dbo.Sys_User SET PDAOperateDate=GETDATE() WHERE User_Id=@0`;
|
await this.dbWrite.query(sqlTwo, [userInfo.user_Id]);
|
//#endregion
|
|
//#region 插入到盘点记录明细
|
// 得到当前单据的所有可用上架明细
|
let modelList = await this.dbRead.find(StorageCheckList, {
|
check_Id: dataInfo.check_Id
|
});
|
// 插入到明细记录
|
// var deleteSql = null;
|
// var insertSql = null;
|
for (var scanItem of dataArray) {
|
// 把原来盘点的这个物料货位的数据删掉
|
let deleteSql = `DELETE dbo.Storage_CheckListDetail WHERE Check_Id=@0 AND checkList_Id=@1`;
|
await this.dbWrite.query(deleteSql, [dataInfo.check_Id, scanItem.checkList_Id]);
|
}
|
for (var scanItem of dataArray) {
|
// 从可用上架明细中 ,得到当前物料编号的数据
|
let currentModel = modelList.find(item => {
|
let result =
|
item.productCode === scanItem.productCode && item.positionName === scanItem.positionName && item.checkList_Id === scanItem.checkList_Id;
|
return result;
|
});
|
if (currentModel) {
|
let insertSql = `INSERT INTO Storage_CheckListDetail
|
(CheckList_Id, Check_Id, Product_Id, ProductCode, ProductName, ProductModel, ProductSpec, PositionName,ScanQuantity, CreateID, Creator, CreateDate)
|
SELECT CheckList_Id, Check_Id, Product_Id, ProductCode, ProductName, ProductModel, ProductSpec, PositionName, @0 AS ScanQuantity,
|
@1 AS CreateID,'PDA' AS Creator, GETDATE() AS CreateDate
|
FROM dbo.Storage_CheckList WHERE CheckList_Id=@2`;
|
await this.dbWrite.query(insertSql, [scanItem.finishedQuantity, userInfo.user_Id, currentModel.checkList_Id]);
|
}
|
}
|
//#endregion
|
|
//#region 更新盘点单明细和主表数据
|
let sqlend = `--盘点数量
|
UPDATE dbo.Storage_CheckList SET CheckQuantity=(
|
SELECT SUM(ScanQuantity) FROM dbo.Storage_CheckListDetail WHERE CheckList_Id=Storage_CheckList.CheckList_Id
|
),ProfitQuantity=0,ProfitMoney=0,LossQuantity=0,LossMoney=0 WHERE Check_Id=@0;
|
--盘盈数量
|
UPDATE dbo.Storage_CheckList SET ProfitQuantity=CheckQuantity-ProductStorage,LossQuantity=0 WHERE CheckQuantity-ProductStorage>0 AND Check_Id=@0;
|
--盘亏数量
|
UPDATE dbo.Storage_CheckList SET LossQuantity=ProductStorage-CheckQuantity,ProfitQuantity=0 WHERE ProductStorage-CheckQuantity>0 AND Check_Id=@0;
|
--盘盈金额,盘亏金额
|
UPDATE dbo.Storage_CheckList SET ProfitMoney=PurchaseMoney*ProfitQuantity,LossMoney=PurchaseMoney*LossQuantity WHERE Check_Id=@0;
|
--主表
|
UPDATE dbo.Storage_Check SET
|
TotalCheckQuantity=(SELECT SUM(CheckQuantity) FROM dbo.Storage_CheckList WHERE Check_Id=dbo.Storage_Check.Check_Id),
|
TotalProfitQuantity=(SELECT SUM(ProfitQuantity) FROM dbo.Storage_CheckList WHERE Check_Id=dbo.Storage_Check.Check_Id),
|
TotalLossQuantity=(SELECT SUM(LossQuantity) FROM dbo.Storage_CheckList WHERE Check_Id=dbo.Storage_Check.Check_Id),
|
TotalProfitMoney=(SELECT SUM(ProfitQuantity) FROM dbo.Storage_CheckList WHERE Check_Id=dbo.Storage_Check.Check_Id),
|
TotalLossMoney=(SELECT SUM(LossMoney) FROM dbo.Storage_CheckList WHERE Check_Id=dbo.Storage_Check.Check_Id)
|
WHERE dbo.Storage_Check.Check_Id=@0`;
|
await this.dbWrite.query(sqlend, [dataInfo.check_Id]);
|
//#endregion
|
this.info.result = true;
|
this.info.msg = "盘点记录保存成功!";
|
return this.info;
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = error.message;
|
return this.info;
|
}
|
}
|
//#endregion
|
}
|