803 lines
21 KiB
JavaScript
803 lines
21 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 Income_entriesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const income_entries = await db.income_entries.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
income_type: data.income_type
|
|
||
|
|
null
|
|
,
|
|
|
|
gross_amount_eur: data.gross_amount_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
wage_tax_eur: data.wage_tax_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
solidarity_tax_eur: data.solidarity_tax_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
church_tax_eur: data.church_tax_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
health_insurance_eur: data.health_insurance_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
pension_insurance_eur: data.pension_insurance_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
unemployment_insurance_eur: data.unemployment_insurance_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
long_term_care_insurance_eur: data.long_term_care_insurance_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
statement_date: data.statement_date
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await income_entries.setTax_profile( data.tax_profile || null, {
|
|
transaction,
|
|
});
|
|
|
|
await income_entries.setHousehold_member( data.household_member || null, {
|
|
transaction,
|
|
});
|
|
|
|
await income_entries.setEmployer( data.employer || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return income_entries;
|
|
}
|
|
|
|
|
|
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 income_entriesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
income_type: item.income_type
|
|
||
|
|
null
|
|
,
|
|
|
|
gross_amount_eur: item.gross_amount_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
wage_tax_eur: item.wage_tax_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
solidarity_tax_eur: item.solidarity_tax_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
church_tax_eur: item.church_tax_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
health_insurance_eur: item.health_insurance_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
pension_insurance_eur: item.pension_insurance_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
unemployment_insurance_eur: item.unemployment_insurance_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
long_term_care_insurance_eur: item.long_term_care_insurance_eur
|
|
||
|
|
null
|
|
,
|
|
|
|
statement_date: item.statement_date
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: item.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const income_entries = await db.income_entries.bulkCreate(income_entriesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return income_entries;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const income_entries = await db.income_entries.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.income_type !== undefined) updatePayload.income_type = data.income_type;
|
|
|
|
|
|
if (data.gross_amount_eur !== undefined) updatePayload.gross_amount_eur = data.gross_amount_eur;
|
|
|
|
|
|
if (data.wage_tax_eur !== undefined) updatePayload.wage_tax_eur = data.wage_tax_eur;
|
|
|
|
|
|
if (data.solidarity_tax_eur !== undefined) updatePayload.solidarity_tax_eur = data.solidarity_tax_eur;
|
|
|
|
|
|
if (data.church_tax_eur !== undefined) updatePayload.church_tax_eur = data.church_tax_eur;
|
|
|
|
|
|
if (data.health_insurance_eur !== undefined) updatePayload.health_insurance_eur = data.health_insurance_eur;
|
|
|
|
|
|
if (data.pension_insurance_eur !== undefined) updatePayload.pension_insurance_eur = data.pension_insurance_eur;
|
|
|
|
|
|
if (data.unemployment_insurance_eur !== undefined) updatePayload.unemployment_insurance_eur = data.unemployment_insurance_eur;
|
|
|
|
|
|
if (data.long_term_care_insurance_eur !== undefined) updatePayload.long_term_care_insurance_eur = data.long_term_care_insurance_eur;
|
|
|
|
|
|
if (data.statement_date !== undefined) updatePayload.statement_date = data.statement_date;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await income_entries.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.tax_profile !== undefined) {
|
|
await income_entries.setTax_profile(
|
|
|
|
data.tax_profile,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.household_member !== undefined) {
|
|
await income_entries.setHousehold_member(
|
|
|
|
data.household_member,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.employer !== undefined) {
|
|
await income_entries.setEmployer(
|
|
|
|
data.employer,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return income_entries;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const income_entries = await db.income_entries.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of income_entries) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of income_entries) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return income_entries;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const income_entries = await db.income_entries.findByPk(id, options);
|
|
|
|
await income_entries.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await income_entries.destroy({
|
|
transaction
|
|
});
|
|
|
|
return income_entries;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const income_entries = await db.income_entries.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!income_entries) {
|
|
return income_entries;
|
|
}
|
|
|
|
const output = income_entries.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.tax_profile = await income_entries.getTax_profile({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.household_member = await income_entries.getHousehold_member({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.employer = await income_entries.getEmployer({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.tax_profiles,
|
|
as: 'tax_profile',
|
|
|
|
where: filter.tax_profile ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.tax_profile.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
profile_title: {
|
|
[Op.or]: filter.tax_profile.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.household_members,
|
|
as: 'household_member',
|
|
|
|
where: filter.household_member ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.household_member.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
full_name: {
|
|
[Op.or]: filter.household_member.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.employers,
|
|
as: 'employer',
|
|
|
|
where: filter.employer ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.employer.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
employer_name: {
|
|
[Op.or]: filter.employer.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'income_entries',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.gross_amount_eurRange) {
|
|
const [start, end] = filter.gross_amount_eurRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
gross_amount_eur: {
|
|
...where.gross_amount_eur,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
gross_amount_eur: {
|
|
...where.gross_amount_eur,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.wage_tax_eurRange) {
|
|
const [start, end] = filter.wage_tax_eurRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
wage_tax_eur: {
|
|
...where.wage_tax_eur,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
wage_tax_eur: {
|
|
...where.wage_tax_eur,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.solidarity_tax_eurRange) {
|
|
const [start, end] = filter.solidarity_tax_eurRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
solidarity_tax_eur: {
|
|
...where.solidarity_tax_eur,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
solidarity_tax_eur: {
|
|
...where.solidarity_tax_eur,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.church_tax_eurRange) {
|
|
const [start, end] = filter.church_tax_eurRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
church_tax_eur: {
|
|
...where.church_tax_eur,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
church_tax_eur: {
|
|
...where.church_tax_eur,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.health_insurance_eurRange) {
|
|
const [start, end] = filter.health_insurance_eurRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
health_insurance_eur: {
|
|
...where.health_insurance_eur,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
health_insurance_eur: {
|
|
...where.health_insurance_eur,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.pension_insurance_eurRange) {
|
|
const [start, end] = filter.pension_insurance_eurRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
pension_insurance_eur: {
|
|
...where.pension_insurance_eur,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
pension_insurance_eur: {
|
|
...where.pension_insurance_eur,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.unemployment_insurance_eurRange) {
|
|
const [start, end] = filter.unemployment_insurance_eurRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
unemployment_insurance_eur: {
|
|
...where.unemployment_insurance_eur,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
unemployment_insurance_eur: {
|
|
...where.unemployment_insurance_eur,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.long_term_care_insurance_eurRange) {
|
|
const [start, end] = filter.long_term_care_insurance_eurRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
long_term_care_insurance_eur: {
|
|
...where.long_term_care_insurance_eur,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
long_term_care_insurance_eur: {
|
|
...where.long_term_care_insurance_eur,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.statement_dateRange) {
|
|
const [start, end] = filter.statement_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
statement_date: {
|
|
...where.statement_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
statement_date: {
|
|
...where.statement_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.income_type) {
|
|
where = {
|
|
...where,
|
|
income_type: filter.income_type,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
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.income_entries.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, ) {
|
|
let where = {};
|
|
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'income_entries',
|
|
'notes',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.income_entries.findAll({
|
|
attributes: [ 'id', 'notes' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['notes', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.notes,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|