diff --git a/app.js b/app.js index e83646d..316e807 100644 --- a/app.js +++ b/app.js @@ -1756,15 +1756,21 @@ function printWork(id,callback) // Napi műhely jelentések nyomtatása // Get Single Work -app.get('/print_workreport/:id',utils.ensureAuthenticated,async function(req,res){ +app.get('/print_workreport/:id/:date',utils.ensureAuthenticated,async function(req,res){ var PdfPrinter=require('pdfmake'); res.setHeader('Content-Type', 'application/json'); try{ var wtSum=0; - const reports=await WorkReport.find({workId: mongoose.Types.ObjectId(req.params.id)}) + var dr_firstDay= moment(req.params.date,"YYYY.MM").startOf('month').toDate(); + var dr_lastDay = moment(req.params.date,"YYYY.MM").endOf('month').toDate(); + const reports=await WorkReport.find({workId: mongoose.Types.ObjectId(req.params.id), + date: { + $gte: dr_firstDay, + $lte: dr_lastDay + }}) const workHeader=await Work.findOne({"_id":mongoose.Types.ObjectId(req.params.id)},{poNumber:1,workNumber:1,megrendelo:1}) - const workTimesSum=await WorkReport.aggregate( + /* const workTimesSum=await WorkReport.aggregate( [ { $match: { 'workId': mongoose.Types.ObjectId(req.params.id)} }, {$unwind: '$workFlow'}, @@ -1775,10 +1781,35 @@ app.get('/print_workreport/:id',utils.ensureAuthenticated,async function(req,res "count": 1,} } ] - ) + )*/ + const workTimesSum=await WorkReport.aggregate( + [ + { $match: { 'workId': mongoose.Types.ObjectId(req.params.id), + date: { + $gte: dr_firstDay, + $lte: dr_lastDay + }} }, + {$unwind: '$workFlow'}, + {$unwind: '$workFlow.employeeList'}, + {$group: {_id: '$workFlow.employeeList.company', + count: { + $sum: 8 + }, + employees:{ + $push:{ + name: "$workFlow.employeeList.name", + id: "$_id" + } + } + }, + + }, + + ] +) - if (workTimesSum.length>0) wtSum=workTimesSum[0].count*8 - var data=await printWorkReport(reports,workHeader) + //if (workTimesSum.length>0) wtSum=workTimesSum[0].count*8 + var data=await printWorkReport(reports,workHeader,workTimesSum) //console.log(JSON.stringify(data)); // fss.writeFile(path.normalize('d:\\test.json'), JSON.stringify(data)); res.send(data); @@ -1790,10 +1821,11 @@ app.get('/print_workreport/:id',utils.ensureAuthenticated,async function(req,res }) // Műhely Napi jelentés Nyomtatási kép generálás -async function printWorkReport(reports, workHeader) +async function printWorkReport(reports, workHeader, workTimeSum) { var item=[]; + var wtsum=[]; var wName=workHeader; var i=0; var emps=""; @@ -1832,7 +1864,13 @@ async function printWorkReport(reports, workHeader) }) }) - + wtsum.push([{text:'Cég név'}, {text:'Munkaórák'}]), + workTimeSum.forEach((ws,ind)=>{ + wtsum.push([ + {text: ws._id, style:'tableData'}, + {text: ws.count, style:'tableData'}, + ]) + }) // playground requires you to assign document definition to a variable called dd @@ -1906,13 +1944,22 @@ async function printWorkReport(reports, workHeader) body: item,//[], }, + layout: { fillColor: function (rowIndex, node, columnIndex) { return (rowIndex % 2 === 0) ? '#CCCCCC' : null; } }, - } + }, + + { + style: 'tableExample', + table: { + widths: [200, '*'], + body: wtsum + } + }, ], styles: { header: { @@ -2148,10 +2195,10 @@ app.get('/media/:file', function(req, res) { }); -app.route('/workreport').post(apiSaveReport); +app.route('/workreportSave').post(apiSaveReport); app.route('/workreportUpdate').post(apiUpdateReport); app.route('/workreportUserList').get(apiGetUsers); -app.route('/workreport/:id').get(apiGetReport); +app.route('/workreportGet/:id/:date').get(apiGetReport); app.route('/workreportList/:id').get(apiGetReportList); app.route('/workreportDates/:id').get(apiGetReportDates); // Route Files diff --git a/js/workReport.js b/js/workReport.js index d95cf57..8fb70cb 100644 --- a/js/workReport.js +++ b/js/workReport.js @@ -83,10 +83,22 @@ exports.apiGetReport=asyncHandler(async (req,res)=>{ res.setHeader('Content-Type', 'application/json'); try{ var wtSum=0; - const reports=await WorkReport.find({workId: mongoose.Types.ObjectId(req.params.id)}) + var dr_firstDay= moment(req.params.date,"YYYY.MM").startOf('month').toDate(); + var dr_lastDay = moment(req.params.date,"YYYY.MM").endOf('month').toDate(); + const reports=await WorkReport.find({workId: mongoose.Types.ObjectId(req.params.id), + date: { + $gte: dr_firstDay, + $lte: dr_lastDay + } + }) const workTimesSum=await WorkReport.aggregate( [ - { $match: { 'workId': mongoose.Types.ObjectId(req.params.id)} }, + { $match: { 'workId': mongoose.Types.ObjectId(req.params.id), + date: { + $gte: dr_firstDay, + $lte: dr_lastDay + } + } }, {$unwind: '$workFlow'}, {$unwind: '$workFlow.employeeList'}, {$group: {_id: '$workFlow.EmployeeList', count:{$sum:1}}}, diff --git a/public/js/work_utils.js b/public/js/work_utils.js index 91d1134..d0c4523 100644 --- a/public/js/work_utils.js +++ b/public/js/work_utils.js @@ -30,8 +30,9 @@ var workId = $(this).attr('data-Id'); if (workId) { + var selectedDate = $('#workReportDate').data("DateTimePicker").date().format('YYYY.MM'); //$.get('/invoices/print2' , function(data) { console.log(data); - $.get('/print_workreport/'+workId , function(data) { + $.get('/print_workreport/'+workId+'/'+selectedDate , function(data) { if (!isLoaded) { isLoaded=true; @@ -74,6 +75,90 @@ .catch(() => alert('Fájl letöltés sikertelen!')); } + + function updateWorkReport() + { + var selectedDate = $('#workReportDate').data("DateTimePicker").date().format('YYYY.MM'); + + //console.log("Selected Date:", selectedDate ? selectedDate.format('YYYY-MM-DD') : "No date selected"); + document.getElementById ('workTimesSum').textContent=0; + //Fetch workReport + var wid = document.getElementById ('wn').textContent; + fetch('/workreportGet/'+wid+'/'+selectedDate , { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer YOUR_ACCESS_TOKEN' + } + }) + .then(response => response.json()) + .then(data => { + $('#workReportTable2 > tbody').empty(); + //console.log(data) + //var t=data.reports[0]; + var newRow= "" + + $.each(data.reports,function(ix,t){ + newRow= `