明树Git Lab

Commit 1eca7605 authored by zfp1's avatar zfp1

update

parent 2aa06d51
......@@ -285,6 +285,17 @@ async function updateMat(req, res, next) {
}
async function importExcel(req, res, next) {
let nginxpath = _.last(String(req.file.path).split('/mnt/vdb1'));
let fileInfo = await DB.File.create({
originalname: req.file.originalname,
mimetype: req.file.mimetype,
filename: req.file.filename,
path: req.file.path,
nginxpath,
size: req.file.size,
});
let workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(req.file.path);
......@@ -302,7 +313,10 @@ async function importExcel(req, res, next) {
arr.push(rowData);
});
res.sendData(arr)
res.sendData({
fileInfo,
materials: arr,
})
}
async function inMat(req, res, next) {
......@@ -310,25 +324,52 @@ async function inMat(req, res, next) {
let body = req.body;
let creator = req.user._id;
let materials = body.materials || [];
let fileInfo = body.fileInfo;
let inApprovalId = new mongoose.Types.ObjectId();
let inApproval = {
_id: inApprovalId,
type: 1,
name: "入库单",
creator,
file: fileInfo && fileInfo._id,
}
let m_arr = [];
//创建单据
await DB.AgrMatApproval.create(inApproval);
// let m_arr = [];
for (let index = 0; index < materials.length; index++) {
const element = materials[index];
if(element.count > 0) {
m_arr.push({
...element,
approval: inApprovalId,
});
if (element.count > 0 && element.name) {
//1 . 对数据库库存进行加减
let check = await DB.AgrMat.findOne({ name: element.name }); // 根据名称查询
if (check && check._id) {
await DB.AgrMat.updateOne({ _id: check._id }, { $inc: { totalCount: element.count } });
//2. 创建记录
await DB.AgrMatInOut.create({
...element,
approval: inApprovalId,
status: 1,
currentTotalCount: check.totalCount,
actualCount: element.count,
});
} else {
await DB.AgrMat.create({
type: element.type,
name: element.name,
totalCount: element.count,
//TODO: 补充字段
});
//2. 创建记录
await DB.AgrMatInOut.create({
...element,
approval: inApprovalId,
status: 2,
currentTotalCount: 0,
actualCount: element.count,
});
}
}
}
//创建单据
//创建列表
} catch (error) {
next(error);
}
......@@ -336,7 +377,55 @@ async function inMat(req, res, next) {
async function outMat(req, res, next) {
try {
let body = req.body;
let creator = req.user._id;
let materials = body.materials || [];
// let fileInfo = body.fileInfo;
let inApprovalId = new mongoose.Types.ObjectId();
let inApproval = {
_id: inApprovalId,
type: 2,
name: "出库单",
creator,
// file: fileInfo && fileInfo._id,
}
//创建单据
await DB.AgrMatApproval.create(inApproval);
// let m_arr = [];
for (let index = 0; index < materials.length; index++) {
const element = materials[index];
if (element.count > 0 && element.name) {
//1 . 对数据库库存进行加减
let check = await DB.AgrMat.findOne({ name: element.name }); // 根据名称查询
if (check && check._id ) {
let actualCount = element.count;
let acstatus = 3;
if(element.count > check.totalCount) {
// 库存不够了 全部取出
actualCount = check.totalCount;
acstatus = 5
}
await DB.AgrMat.updateOne({ _id: check._id }, { $inc: { totalCount: -actualCount } });
//2. 创建记录
await DB.AgrMatInOut.create({
...element,
approval: inApprovalId,
status: acstatus, // 顺利出库 or 不足出库
currentTotalCount: check.totalCount,
actualCount: actualCount,
});
} else {
//2. 创建记录
await DB.AgrMatInOut.create({
...element,
approval: inApprovalId,
status: 4, //出库失败
currentTotalCount: check && check.totalCount || 0, //
});
}
}
}
} catch (error) {
next(error);
}
......
......@@ -26,7 +26,8 @@ const AgrMaterialSchema = new Schema({
},
totalCount: {
type: Number,
comment: "农资库存数量"
comment: "农资库存数量",
default: 0
},
unit: {
type: String,
......
......@@ -42,7 +42,13 @@ const AgrMaterialApprovalSchema = new Schema({
// type: [mongoose.Types.ObjectId],
// comment: "审批流程记录 不通过后再提交即为一次新的审批流程"
// },
file: {
type: mongoose.Types.ObjectId,
ref: 'File',
comment: "excel文件",
default: null,
set: v => {if(!v) {return null;} else {return v;}}
},
createdAt: {
type: Date,
default: Date.now,
......
......@@ -20,9 +20,18 @@ const AgrMaterialInOutSchema = new Schema({
},
count: {
type: Number,
comment: "出入库数量",
comment: "预计出入库数量",
default: 0,
},
currentTotalCount: {
type: Number,
comment: "当前库存数量"
},
actualCount: {
type: Number,
comment: "实际出入库数量--计算依据",
},
approval: {
type: mongoose.Types.ObjectId,
comment: "单据id",
......@@ -30,7 +39,7 @@ const AgrMaterialInOutSchema = new Schema({
},
status: {
type: Number,
comment: "入库状态 1 新增入库 2 追加入库 ",
comment: "出入库状态 1 新增入库 2 追加入库 3.顺利出库 4.出库失败 5. 出库数量不足 ",
default: 1,
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment