39023-vm/backend/src/db/api/ir_financial_statement_summaries.js
2026-03-06 04:22:38 +00:00

660 lines
17 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 Ir_financial_statement_summariesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const ir_financial_statement_summaries = await db.ir_financial_statement_summaries.create(
{
id: data.id || undefined,
statement_type: data.statement_type
||
null
,
period_type: data.period_type
||
null
,
period_end_at: data.period_end_at
||
null
,
total_revenue: data.total_revenue
||
null
,
net_income: data.net_income
||
null
,
total_assets: data.total_assets
||
null
,
total_liabilities: data.total_liabilities
||
null
,
free_cash_flow: data.free_cash_flow
||
null
,
notes: data.notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await ir_financial_statement_summaries.setIr_company( data.ir_company || null, {
transaction,
});
return ir_financial_statement_summaries;
}
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 ir_financial_statement_summariesData = data.map((item, index) => ({
id: item.id || undefined,
statement_type: item.statement_type
||
null
,
period_type: item.period_type
||
null
,
period_end_at: item.period_end_at
||
null
,
total_revenue: item.total_revenue
||
null
,
net_income: item.net_income
||
null
,
total_assets: item.total_assets
||
null
,
total_liabilities: item.total_liabilities
||
null
,
free_cash_flow: item.free_cash_flow
||
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 ir_financial_statement_summaries = await db.ir_financial_statement_summaries.bulkCreate(ir_financial_statement_summariesData, { transaction });
// For each item created, replace relation files
return ir_financial_statement_summaries;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const ir_financial_statement_summaries = await db.ir_financial_statement_summaries.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.statement_type !== undefined) updatePayload.statement_type = data.statement_type;
if (data.period_type !== undefined) updatePayload.period_type = data.period_type;
if (data.period_end_at !== undefined) updatePayload.period_end_at = data.period_end_at;
if (data.total_revenue !== undefined) updatePayload.total_revenue = data.total_revenue;
if (data.net_income !== undefined) updatePayload.net_income = data.net_income;
if (data.total_assets !== undefined) updatePayload.total_assets = data.total_assets;
if (data.total_liabilities !== undefined) updatePayload.total_liabilities = data.total_liabilities;
if (data.free_cash_flow !== undefined) updatePayload.free_cash_flow = data.free_cash_flow;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await ir_financial_statement_summaries.update(updatePayload, {transaction});
if (data.ir_company !== undefined) {
await ir_financial_statement_summaries.setIr_company(
data.ir_company,
{ transaction }
);
}
return ir_financial_statement_summaries;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const ir_financial_statement_summaries = await db.ir_financial_statement_summaries.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of ir_financial_statement_summaries) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of ir_financial_statement_summaries) {
await record.destroy({transaction});
}
});
return ir_financial_statement_summaries;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const ir_financial_statement_summaries = await db.ir_financial_statement_summaries.findByPk(id, options);
await ir_financial_statement_summaries.update({
deletedBy: currentUser.id
}, {
transaction,
});
await ir_financial_statement_summaries.destroy({
transaction
});
return ir_financial_statement_summaries;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const ir_financial_statement_summaries = await db.ir_financial_statement_summaries.findOne(
{ where },
{ transaction },
);
if (!ir_financial_statement_summaries) {
return ir_financial_statement_summaries;
}
const output = ir_financial_statement_summaries.get({plain: true});
output.ir_company = await ir_financial_statement_summaries.getIr_company({
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.ir_companies,
as: 'ir_company',
where: filter.ir_company ? {
[Op.or]: [
{ id: { [Op.in]: filter.ir_company.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.ir_company.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(
'ir_financial_statement_summaries',
'notes',
filter.notes,
),
};
}
if (filter.period_end_atRange) {
const [start, end] = filter.period_end_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
period_end_at: {
...where.period_end_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
period_end_at: {
...where.period_end_at,
[Op.lte]: end,
},
};
}
}
if (filter.total_revenueRange) {
const [start, end] = filter.total_revenueRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
total_revenue: {
...where.total_revenue,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
total_revenue: {
...where.total_revenue,
[Op.lte]: end,
},
};
}
}
if (filter.net_incomeRange) {
const [start, end] = filter.net_incomeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
net_income: {
...where.net_income,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
net_income: {
...where.net_income,
[Op.lte]: end,
},
};
}
}
if (filter.total_assetsRange) {
const [start, end] = filter.total_assetsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
total_assets: {
...where.total_assets,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
total_assets: {
...where.total_assets,
[Op.lte]: end,
},
};
}
}
if (filter.total_liabilitiesRange) {
const [start, end] = filter.total_liabilitiesRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
total_liabilities: {
...where.total_liabilities,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
total_liabilities: {
...where.total_liabilities,
[Op.lte]: end,
},
};
}
}
if (filter.free_cash_flowRange) {
const [start, end] = filter.free_cash_flowRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
free_cash_flow: {
...where.free_cash_flow,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
free_cash_flow: {
...where.free_cash_flow,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.statement_type) {
where = {
...where,
statement_type: filter.statement_type,
};
}
if (filter.period_type) {
where = {
...where,
period_type: filter.period_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.ir_financial_statement_summaries.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(
'ir_financial_statement_summaries',
'statement_type',
query,
),
],
};
}
const records = await db.ir_financial_statement_summaries.findAll({
attributes: [ 'id', 'statement_type' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['statement_type', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.statement_type,
}));
}
};