646 lines
17 KiB
JavaScript
646 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 Excise_duty_registrationsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const excise_duty_registrations = await db.excise_duty_registrations.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
duty_amount: data.duty_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
currency_code: data.currency_code
|
|
||
|
|
null
|
|
,
|
|
|
|
billing_basis: data.billing_basis
|
|
||
|
|
null
|
|
,
|
|
|
|
registration_reference: data.registration_reference
|
|
||
|
|
null
|
|
,
|
|
|
|
registered_at: data.registered_at
|
|
||
|
|
null
|
|
,
|
|
|
|
effective_from: data.effective_from
|
|
||
|
|
null
|
|
,
|
|
|
|
effective_to: data.effective_to
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await excise_duty_registrations.setAgreement_vehicle( data.agreement_vehicle || null, {
|
|
transaction,
|
|
});
|
|
|
|
await excise_duty_registrations.setCountry( data.country || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return excise_duty_registrations;
|
|
}
|
|
|
|
|
|
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 excise_duty_registrationsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
duty_amount: item.duty_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
currency_code: item.currency_code
|
|
||
|
|
null
|
|
,
|
|
|
|
billing_basis: item.billing_basis
|
|
||
|
|
null
|
|
,
|
|
|
|
registration_reference: item.registration_reference
|
|
||
|
|
null
|
|
,
|
|
|
|
registered_at: item.registered_at
|
|
||
|
|
null
|
|
,
|
|
|
|
effective_from: item.effective_from
|
|
||
|
|
null
|
|
,
|
|
|
|
effective_to: item.effective_to
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
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 excise_duty_registrations = await db.excise_duty_registrations.bulkCreate(excise_duty_registrationsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return excise_duty_registrations;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const excise_duty_registrations = await db.excise_duty_registrations.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.duty_amount !== undefined) updatePayload.duty_amount = data.duty_amount;
|
|
|
|
|
|
if (data.currency_code !== undefined) updatePayload.currency_code = data.currency_code;
|
|
|
|
|
|
if (data.billing_basis !== undefined) updatePayload.billing_basis = data.billing_basis;
|
|
|
|
|
|
if (data.registration_reference !== undefined) updatePayload.registration_reference = data.registration_reference;
|
|
|
|
|
|
if (data.registered_at !== undefined) updatePayload.registered_at = data.registered_at;
|
|
|
|
|
|
if (data.effective_from !== undefined) updatePayload.effective_from = data.effective_from;
|
|
|
|
|
|
if (data.effective_to !== undefined) updatePayload.effective_to = data.effective_to;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await excise_duty_registrations.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.agreement_vehicle !== undefined) {
|
|
await excise_duty_registrations.setAgreement_vehicle(
|
|
|
|
data.agreement_vehicle,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.country !== undefined) {
|
|
await excise_duty_registrations.setCountry(
|
|
|
|
data.country,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return excise_duty_registrations;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const excise_duty_registrations = await db.excise_duty_registrations.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of excise_duty_registrations) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of excise_duty_registrations) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return excise_duty_registrations;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const excise_duty_registrations = await db.excise_duty_registrations.findByPk(id, options);
|
|
|
|
await excise_duty_registrations.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await excise_duty_registrations.destroy({
|
|
transaction
|
|
});
|
|
|
|
return excise_duty_registrations;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const excise_duty_registrations = await db.excise_duty_registrations.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!excise_duty_registrations) {
|
|
return excise_duty_registrations;
|
|
}
|
|
|
|
const output = excise_duty_registrations.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.agreement_vehicle = await excise_duty_registrations.getAgreement_vehicle({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.country = await excise_duty_registrations.getCountry({
|
|
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.agreement_vehicles,
|
|
as: 'agreement_vehicle',
|
|
|
|
where: filter.agreement_vehicle ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.agreement_vehicle.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
notes: {
|
|
[Op.or]: filter.agreement_vehicle.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.countries,
|
|
as: 'country',
|
|
|
|
where: filter.country ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.country.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.country.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.currency_code) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'excise_duty_registrations',
|
|
'currency_code',
|
|
filter.currency_code,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.registration_reference) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'excise_duty_registrations',
|
|
'registration_reference',
|
|
filter.registration_reference,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'excise_duty_registrations',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.duty_amountRange) {
|
|
const [start, end] = filter.duty_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
duty_amount: {
|
|
...where.duty_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
duty_amount: {
|
|
...where.duty_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.registered_atRange) {
|
|
const [start, end] = filter.registered_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
registered_at: {
|
|
...where.registered_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
registered_at: {
|
|
...where.registered_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.effective_fromRange) {
|
|
const [start, end] = filter.effective_fromRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
effective_from: {
|
|
...where.effective_from,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
effective_from: {
|
|
...where.effective_from,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.effective_toRange) {
|
|
const [start, end] = filter.effective_toRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
effective_to: {
|
|
...where.effective_to,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
effective_to: {
|
|
...where.effective_to,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.billing_basis) {
|
|
where = {
|
|
...where,
|
|
billing_basis: filter.billing_basis,
|
|
};
|
|
}
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.excise_duty_registrations.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(
|
|
'excise_duty_registrations',
|
|
'registration_reference',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.excise_duty_registrations.findAll({
|
|
attributes: [ 'id', 'registration_reference' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['registration_reference', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.registration_reference,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|