%PDF- %PDF-
Direktori : /home/opti/workflow/src/routes/ |
Current File : /home/opti/workflow/src/routes/human_resources.js |
var express = require('express'); var router = express.Router(); var formidable = require('formidable'); var fs = require('fs'); var path = require('path'); var Op = require('sequelize').Op; var ejs = require('ejs'); var pdf = require('html-pdf'); // var moment = require('moment'); // const email = require('../config/mail'); var secured = require('../middleware/login'); var db = require('../models'); //import models // const db = require('../models'); // new require for db object //TODO::list all contacts, invoices etc... //get method/route for job, category list/view, router.get('/jobs', secured, async(req, res, next)=> { const [titles, categories, salaryComponents, employmentStatuses, workShifts, payGrades] = await Promise.all([ db.JobTitle.findAll({where:{orgId: req.user.orgId}}), db.JobCategory.findAll({where:{orgId: req.user.orgId}}), db.SalaryComponent.findAll({where:{orgId: req.user.orgId}}), db.EmploymentStatus.findAll({where:{orgId: req.user.orgId}}), db.WorkShift.findAll({where:{orgId: req.user.orgId}}), db.PayGrade.findAll({where:{orgId: req.user.orgId}}), ]) if(salaryComponents.length == 0){ //this company doesn't have any salary component yet, //let's create a basic salary for them so they don't spoil our work await db.SalaryComponent.create({title:"Basic Salary", type:"Earning", partOfTotal:1, valueType:"Amount", orgId: req.user.orgId}) } res.render('hr/job/title', {titles, categories, salaryComponents, employmentStatuses, workShifts, payGrades}); }); router.get('/jobs/view/:uuid', secured, async(req, res, next)=> { const [category, categories] = await Promise.all([ db.JobCategory.findOne({where:{orgId: req.user.orgId, uuid:req.params.uuid}, include:'titles'}), db.JobCategory.findAll({where:{orgId: req.user.orgId}}), ]) const titles = await db.JobTitle.findAll({where:{orgId: req.user.orgId, categoryId:category.id}}); res.render('hr/job/categoryView', {category, categories, titles}); }); router.get('/salary-component', secured, async(req, res, next)=> { const [salaryComponents] = await Promise.all([ db.SalaryComponent.findAll({where:{orgId: req.user.orgId}}), ]) if(salaryComponents.length == 0){ //this company doesn't have any salary component yet, //let's create a basic salary for them so they don't spoil our work await db.SalaryComponent.create({title:"Basic Salary", type:"Earning", partOfTotal:1, valueType:"Amount", orgId: req.user.orgId}) } res.render('hr/job/salaryComponent', {salaryComponents}); }); router.get('/employment-status', secured, async(req, res, next)=> { const [employmentStatuses] = await Promise.all([ db.EmploymentStatus.findAll({where:{orgId: req.user.orgId}}), ]) res.render('hr/job/employmentStatus', {employmentStatuses}); }); router.get('/work-shift', secured, async(req, res, next)=> { const [workShifts] = await Promise.all([ db.WorkShift.findAll({where:{orgId: req.user.orgId}}), ]) res.render('hr/job/workShift', {workShifts}); }); router.get('/pay-grade', secured, async(req, res, next)=> { const [payGrades] = await Promise.all([ db.PayGrade.findAll({where:{orgId: req.user.orgId}}), ]) res.render('hr/job/payGrade', {payGrades}); }); router.get('/leave', secured, async(req, res, next)=> { const [leaves] = await Promise.all([ db.Leave.findAll({where:{orgId: req.user.orgId}}), ]) res.render('hr/job/leave', {leaves}); }); //post method/route for job, category list/view, router.post('/jobs/add-title', secured, function(req, res, next) { const body = req.body // console.log(body); db.JobCategory.findOne({where:{id:body.categoryId, orgId:req.user.orgId}}) .then(category=>{ if(!category) return res.json({status:"error", msg: "Category not found!"}); if(body.id){ db.JobTitle.findOne({where:{id:body.id, orgId:req.user.orgId}}) .then(jobTitle=>{ if(!jobTitle) return res.json({status:"error", msg: "Job not found!"}); else{ jobTitle.update({title:body.title, description: body.description, categoryId:body.categoryId}) .then(job => { return res.json({status:"success", msg: "Job Title updated!", job:{id:job.id, title:job.title, description:job.description, categoryId:job.categoryId, }}); }) } }) } else{ db.JobTitle.create({title:body.title, description:body.description, categoryId:body.categoryId, orgId: req.user.orgId}) .then(jobTitle=>{ if(!jobTitle) return res.json({status:"error", msg: "Unable to add Job title!"}); else return res.json({status:"success", msg: "Job Title added!", job:{id:jobTitle.id, title:jobTitle.title, description:jobTitle.description, categoryId:jobTitle.categoryId, }}); }) } }) }); router.post('/jobs/add-category', secured, function(req, res, next) { const body = req.body // console.log(body); if(body.id){ db.JobCategory.findOne({where:{id:body.id, orgId:req.user.orgId}}) .then(category=>{ if(!category) return res.json({status:"error", msg: "Category not found!"}); else{ category.update({title:body.title, description: body.description}) .then(c => { return res.json({status:"success", msg: "Job Category updated!", category:{id: c.id, title: c.title, description: c.description}}); }) } }) }else{ db.JobCategory.create({title:body.title, description:body.description, orgId: req.user.orgId}) .then(category=>{ if(!category) return res.json({status:"error", msg: "Unable to add category"}); else return res.json({status:"success", msg: "Job Category added!", category:{id: category.id, title: category.title, description: category.description}}); }) } }); router.post('/jobs/add-salary-component', secured, function(req, res, next) { const body = req.body // console.log(body); var type = body.type == 1 ? "Earning" : "Deduction"; var partOfTotal = body.partOfTotal == 1 ? 1 : 0; var valueType = body.valueType == 1 ? "Amount" : "Percentage"; if(body.id){ // console.log("fffff") db.SalaryComponent.findOne({where:{id:body.id, orgId:req.user.orgId}}) .then(salary=>{ if(!salary) return res.json({status:"error", msg: "Salary Component not found!"}); else{ salary.update({title:body.title, type, partOfTotal, valueType}) .then(s => { return res.json({status:"success", msg: "Salary Component updated!", salary:{id:s.id, title:s.title, type:s.type, partOfTotal:s.partOfTotal, valueType:s.valueType, }}); }) } }) }else{ // console.log("dddddd") db.SalaryComponent.create({title:body.title, type, partOfTotal, valueType, orgId: req.user.orgId}) .then(salary=>{ if(!salary) return res.json({status:"error", msg: "Unable to add Salary Component"}); else return res.json({status:"success", msg: "Salary Component added!", salary:{id:salary.id, title:salary.title, type:salary.type, partOfTotal:salary.partOfTotal, valueType:salary.valueType, }}); }) } }); router.post('/jobs/add-employment-status', secured, function(req, res, next) { const body = req.body // console.log(body); if(body.id){ db.EmploymentStatus.findOne({where:{id:body.id, orgId:req.user.orgId}}) .then(status=>{ if(!status) return res.json({status:"error", msg: "Employment Status not found!"}); else{ status.update({title:body.title, description: body.description}) .then(s => { return res.json({status:"success", msg: "Employment Status updated!", employmentStatus:{id:s.id, title:s.title, description:s.description, }}); }) } }) }else{ db.EmploymentStatus.create({title:body.title, description:body.description, orgId: req.user.orgId}) .then(status=>{ if(!status) return res.json({status:"error", msg: "Unable to add Employment Status"}); else return res.json({status:"success", msg: "Employment Status added!", employmentStatus:{id:status.id, title:status.title, description:status.description, }}); }) } }); router.post('/jobs/add-work-shift', secured, function(req, res, next) { const body = req.body // console.log(body); if(body.id){ db.WorkShift.findOne({where:{id:body.id, orgId:req.user.orgId}}) .then(shift=>{ if(!shift) return res.json({status:"error", msg: "Work Shift not found!"}); else{ shift.update({title:body.title, description: body.description, from:body.from, to:body.to}) .then(s => { return res.json({status:"success", msg: "Work Shift updated!", workShift:{id:s.id, title:s.title, from:s.from, to:s.to, description:s.description, }}); }) } }) }else{ db.WorkShift.create({title:body.title, description:body.description, from:body.from, to:body.to, orgId: req.user.orgId}) .then(shift=>{ if(!shift) return res.json({status:"error", msg: "Unable to add Work Shift"}); else return res.json({status:"success", msg: "Work Shift added!", workShift:{id:shift.id, title: shift.title, from:shift.from, to:shift.to, description:shift.description, }}); }) } }); router.post('/jobs/add-pay-grade', secured, function(req, res, next) { const body = req.body // console.log(body); if(body.id){ db.PayGrade.findOne({where:{id:body.id, orgId:req.user.orgId}}) .then(grade=>{ if(!grade) return res.json({status:"error", msg: "Pay Grade not found!"}); else{ grade.update({title:body.title, description: body.description, currencyId:body.currencyId, minSalary:body.minSalary, maxSalary:body.maxSalary}) .then(g => { return res.json({status:"success", msg: "Work Shift updated!", payGrade:{id:g.id, title:g.title, currencyId:g.currencyId, minSalary:g.minSalary, maxSalary:g.maxSalary, description:g.description, }}); }) } }) }else{ db.PayGrade.create({title:body.title, description:body.description, currencyId:body.currencyId, maxSalary:body.maxSalary, minSalary:body.minSalary, orgId: req.user.orgId}) .then(grade=>{ if(!grade) return res.json({status:"error", msg: "Unable to add Pay Grade"}); else return res.json({status:"success", msg: "Pay Grade added!", payGrade:{id:grade.id, title: grade.title, currencyId:grade.currencyId, minSalary:grade.minSalary, maxSalary:grade.maxSalary, description:grade.description, }}); }) } }); //json //get list of jobTitles in respect to jobCategory router.post('/jobs/get-jobTitles', secured, function(req, res, next) { const body = req.body // console.log(body); db.JobTitle.findAll({where:{categoryId : body.jobCategoryId, orgId : req.user.orgId}}) .then(jobTitles=>{ if(!jobTitles) return res.json({status:"error", msg: "Category not found!"}); return res.json({status:"success", msg: "", jobTitles}); }) }); //json //get list of jobTitles in respect to jobCategory router.post('/jobs/get-salaryComponents', secured, function(req, res, next) { // const body = req.body // console.log(body); db.SalaryComponent.findAll({where:{orgId : req.user.orgId}, order:[['createdAt', 'ASC']]}) .then(salaryComponents=>{ if(!salaryComponents) return res.json({status:"error", msg: "salaryComponents not found!"}); return res.json({status:"success", msg: "", salaryComponents}); }) }); //handle new employeee job detils, salary components, router.post('/employee/add', secured, function(req, res, next) { const new_employee = req.session.new_employee; var form = new formidable.IncomingForm(); form.parse(req, async (err, fields, files) => { const components = JSON.parse(fields.salaryComponents) let earnings = 0; let deduction = 0; let annualPayment = 0; const salaryComponents = await db.SalaryComponent.findAll({where:{orgId: req.user.orgId}}); const basicSalary = components[0]; salaryComponents.forEach((component, index) => { if(component.type == "Earning"){ if(component.valueType == "Amount") earnings += parseFloat(components[index]); else{ var a = (basicSalary * components[index])/ 100; //its percentage now earnings += a; } // console.log(components[index], index) } else{ if(component.valueType == "Amount") deduction += parseFloat(components[index]); else{ var a = (basicSalary * components[index])/ 100; //its percentage now deduction += a; } } }); annualPayment = earnings - deduction; //create new user db.User.create({...new_employee, roleId : 'isEmployee'}) .then(function(newStaff){ if(!newStaff){ req.flash('info', 'Error occurred while creating employee'); return res.redirect('/employee/all'); } db.EmployeeJob.create({orgId:req.user.orgId, jobTitleId: fields.jobTitleId, workShiftId: fields.workShiftId, employmentStatusId: fields.employmentStatusId, dateOfEmployment: fields.dateOfEmployment, dateOfPermanency: fields.dateOfPermanency, contractStarts: fields.contractStarts, contractEnds: fields.contractEnds, userId: newStaff.id, createdBy: req.user.id, active: 1}) // .then(employee) db.EmployeeSalary.create({ orgId: req.user.orgId, userId: newStaff.id, payGradeId: fields.payGradeId, currencyId:fields.currencyId, effectiveFrom: fields.effectiveFrom, effectiveTo: fields.effectiveTo, earnings, deduction, annualPayment, active:1}) .then(salary => { salaryComponents.forEach((component, index) => { var type = 2; if(salaryComponents.type == "Earning"){ type = 1; } db.EmployeeSalaryComponent.create({orgId: req.user.orgId, employeeSalaryId: salary.id, salaryComponentId: component.id, value: components[index], createdBy:req.user.id, userId: newStaff.id, type}) }); res.redirect('/employee/all') }) }) }) }); //handle edit employee's job router.post('/jobs/employee/job-edit', secured, function(req, res, next) { // const new_employee = req.session.new_employee; var form = new formidable.IncomingForm(); form.parse(req, async (err, fields, files) => { console.log(fields) // check e if(fields.jobId){ //find current job record and make it inactive db.EmployeeJob.findOne({where:{userId: fields.userId, id :fields.jobId, active:1}}) .then(async job => { await job.update({active: 0}); // console.log(job) }) } //create new record either record exist or not db.EmployeeJob.create({orgId:req.user.orgId, jobTitleId: fields.jobTitleId, workShiftId: fields.workShiftId, employmentStatusId: fields.employmentStatusId, dateOfEmployment: fields.dateOfEmployment, dateOfPermanency: fields.dateOfPermanency, contractStarts: fields.contractStarts, contractEnds: fields.contractEnds, userId: fields.userId, createdBy: req.user.id, active: 1}) .then( newJob => { req.flash('success', 'Employee Job Updated') res.redirect('/employee/view/'+fields.userId) }) // req.flash('info', 'Something went wrong') // res.redirect('employee/view/'+job.userId) }) }); //handle edit employee's salary router.post('/jobs/employee/salary-edit', secured, function(req, res, next) { var form = new formidable.IncomingForm(); form.parse(req, async (err, fields, files) => { // console.log(fields) const components = JSON.parse(fields.salaryComponents) let earnings = 0; let deduction = 0; let annualPayment = 0; const salaryComponents = await db.SalaryComponent.findAll({where:{orgId: req.user.orgId}}); const basicSalary = components[0]; salaryComponents.forEach((component, index) => { if(component.type == "Earning"){ if(component.valueType == "Amount") earnings += parseFloat(components[index]); else{ var a = (basicSalary * components[index])/ 100; //its percentage now earnings += a; } // console.log(components[index], index) } else{ if(component.valueType == "Amount") deduction += parseFloat(components[index]); else{ var a = (basicSalary * components[index])/ 100; //its percentage now deduction += a; } } }); annualPayment = earnings - deduction; if(fields.salaryId) //find current salary record and make inactive await db.EmployeeSalary.findOne({where:{userId: fields.userId, id:fields.salaryId}}) .then( async salary => { await salary.update({active:0, effectiveTo:fields.effectiveFrom});//this becomes non-effective when new becomes effective }) ///create new record even if no record exist or record set to inactive await db.EmployeeSalary.create({ orgId: req.user.orgId, userId: fields.userId, payGradeId: fields.payGradeId, currencyId:fields.currencyId, effectiveFrom: fields.effectiveFrom, effectiveTo: fields.effectiveTo, earnings, deduction, annualPayment, active:1}) .then(async (newSalary) => { await salaryComponents.forEach(async(component, index) => { var type = 2; if(salaryComponents.type == "Earning"){ type = 1; } await db.EmployeeSalaryComponent.create({orgId: req.user.orgId, employeeSalaryId: newSalary.id, salaryComponentId: component.id, value: components[index], createdBy:req.user.id, userId: fields.userId, type }) // .then( com => console.log(com)) console.log("inside") }); // for(var i = 0; i < salaryComponents.length; i++){ // await db.EmployeeSalaryComponent.create({orgId: req.user.orgId, employeeSalaryId: newSalary.id, salaryComponentId: salaryComponents[i].id, value: components[i], createdBy:req.user.id, userId: fields.userId,}); // console.log("inside") // } // console.log("outside") req.flash('success', "Employee's Salary updated") res.redirect('/employee/view/'+fields.userId) }) // req.flash('info', 'Something went wrong') // res.redirect('employee/view/'+job.userId) }) }); module.exports = router;