452 lines
11 KiB
JavaScript
452 lines
11 KiB
JavaScript
const db = require('../models');
|
|
const FileDBApi = require('./file');
|
|
const crypto = require('crypto');
|
|
const Utils = require('../utils');
|
|
|
|
const Sequelize = db.Sequelize;
|
|
const Op = Sequelize.Op;
|
|
|
|
module.exports = class PayrollsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const payrolls = await db.payrolls.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
basic_salary: data.basic_salary || null,
|
|
allowances: data.allowances || null,
|
|
deductions: data.deductions || null,
|
|
net_salary: data.net_salary || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await payrolls.setEmployee(data.employee || null, {
|
|
transaction,
|
|
});
|
|
|
|
await payrolls.setCompanies(data.companies || null, {
|
|
transaction,
|
|
});
|
|
|
|
return payrolls;
|
|
}
|
|
|
|
static async bulkImport(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
// Prepare data - wrapping individual data transformations in a map() method
|
|
const payrollsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
basic_salary: item.basic_salary || null,
|
|
allowances: item.allowances || null,
|
|
deductions: item.deductions || null,
|
|
net_salary: item.net_salary || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const payrolls = await db.payrolls.bulkCreate(payrollsData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return payrolls;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const payrolls = await db.payrolls.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.basic_salary !== undefined)
|
|
updatePayload.basic_salary = data.basic_salary;
|
|
|
|
if (data.allowances !== undefined)
|
|
updatePayload.allowances = data.allowances;
|
|
|
|
if (data.deductions !== undefined)
|
|
updatePayload.deductions = data.deductions;
|
|
|
|
if (data.net_salary !== undefined)
|
|
updatePayload.net_salary = data.net_salary;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await payrolls.update(updatePayload, { transaction });
|
|
|
|
if (data.employee !== undefined) {
|
|
await payrolls.setEmployee(
|
|
data.employee,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.companies !== undefined) {
|
|
await payrolls.setCompanies(
|
|
data.companies,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return payrolls;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const payrolls = await db.payrolls.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of payrolls) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of payrolls) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return payrolls;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const payrolls = await db.payrolls.findByPk(id, options);
|
|
|
|
await payrolls.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await payrolls.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return payrolls;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const payrolls = await db.payrolls.findOne({ where }, { transaction });
|
|
|
|
if (!payrolls) {
|
|
return payrolls;
|
|
}
|
|
|
|
const output = payrolls.get({ plain: true });
|
|
|
|
output.employee = await payrolls.getEmployee({
|
|
transaction,
|
|
});
|
|
|
|
output.companies = await payrolls.getCompanies({
|
|
transaction,
|
|
});
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(filter, globalAccess, options) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userCompanies = (user && user.companies?.id) || null;
|
|
|
|
if (userCompanies) {
|
|
if (options?.currentUser?.companiesId) {
|
|
where.companiesId = options.currentUser.companiesId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.employees,
|
|
as: 'employee',
|
|
|
|
where: filter.employee
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.employee
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
first_name: {
|
|
[Op.or]: filter.employee
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.companies,
|
|
as: 'companies',
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.basic_salaryRange) {
|
|
const [start, end] = filter.basic_salaryRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
basic_salary: {
|
|
...where.basic_salary,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
basic_salary: {
|
|
...where.basic_salary,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.allowancesRange) {
|
|
const [start, end] = filter.allowancesRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
allowances: {
|
|
...where.allowances,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
allowances: {
|
|
...where.allowances,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.deductionsRange) {
|
|
const [start, end] = filter.deductionsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
deductions: {
|
|
...where.deductions,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
deductions: {
|
|
...where.deductions,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.net_salaryRange) {
|
|
const [start, end] = filter.net_salaryRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
net_salary: {
|
|
...where.net_salary,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
net_salary: {
|
|
...where.net_salary,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.companies) {
|
|
const listItems = filter.companies.split('|').map((item) => {
|
|
return Utils.uuid(item);
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
companiesId: { [Op.or]: listItems },
|
|
};
|
|
}
|
|
|
|
if (filter.createdAtRange) {
|
|
const [start, end] = filter.createdAtRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
if (globalAccess) {
|
|
delete where.companiesId;
|
|
}
|
|
|
|
const queryOptions = {
|
|
where,
|
|
include,
|
|
distinct: true,
|
|
order:
|
|
filter.field && filter.sort
|
|
? [[filter.field, filter.sort]]
|
|
: [['createdAt', 'desc']],
|
|
transaction: options?.transaction,
|
|
logging: console.log,
|
|
};
|
|
|
|
if (!options?.countOnly) {
|
|
queryOptions.limit = limit ? Number(limit) : undefined;
|
|
queryOptions.offset = offset ? Number(offset) : undefined;
|
|
}
|
|
|
|
try {
|
|
const { rows, count } = await db.payrolls.findAndCountAll(queryOptions);
|
|
|
|
return {
|
|
rows: options?.countOnly ? [] : rows,
|
|
count: count,
|
|
};
|
|
} catch (error) {
|
|
console.error('Error executing query:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async findAllAutocomplete(
|
|
query,
|
|
limit,
|
|
offset,
|
|
globalAccess,
|
|
organizationId,
|
|
) {
|
|
let where = {};
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike('payrolls', 'net_salary', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.payrolls.findAll({
|
|
attributes: ['id', 'net_salary'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['net_salary', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.net_salary,
|
|
}));
|
|
}
|
|
};
|