const db = require('../models'); const crypto = require('crypto'); const Utils = require('../utils'); const Sequelize = db.Sequelize; const Op = Sequelize.Op; module.exports = class Ex_bond_registersDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const ex_bond_registers = await db.ex_bond_registers.create( { id: data.id || undefined, entry_date: data.entry_date || null , reference_number: data.reference_number || null , mushok_number: data.mushok_number || null , item_code: data.item_code || null , quantity: data.quantity || null , value: data.value || null , warehouse_location: data.warehouse_location || null , batch_number: data.batch_number || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); return ex_bond_registers; } 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 ex_bond_registersData = data.map((item, index) => ({ id: item.id || undefined, entry_date: item.entry_date || null , reference_number: item.reference_number || null , mushok_number: item.mushok_number || null , item_code: item.item_code || null , quantity: item.quantity || null , value: item.value || null , warehouse_location: item.warehouse_location || null , batch_number: item.batch_number || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const ex_bond_registers = await db.ex_bond_registers.bulkCreate(ex_bond_registersData, { transaction }); return ex_bond_registers; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const ex_bond_registers = await db.ex_bond_registers.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.entry_date !== undefined) updatePayload.entry_date = data.entry_date; if (data.reference_number !== undefined) updatePayload.reference_number = data.reference_number; if (data.mushok_number !== undefined) updatePayload.mushok_number = data.mushok_number; if (data.item_code !== undefined) updatePayload.item_code = data.item_code; if (data.quantity !== undefined) updatePayload.quantity = data.quantity; if (data.value !== undefined) updatePayload.value = data.value; if (data.warehouse_location !== undefined) updatePayload.warehouse_location = data.warehouse_location; if (data.batch_number !== undefined) updatePayload.batch_number = data.batch_number; updatePayload.updatedById = currentUser.id; await ex_bond_registers.update(updatePayload, {transaction}); return ex_bond_registers; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const ex_bond_registers = await db.ex_bond_registers.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of ex_bond_registers) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of ex_bond_registers) { await record.destroy({transaction}); } }); return ex_bond_registers; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const ex_bond_registers = await db.ex_bond_registers.findByPk(id, options); await ex_bond_registers.update({ deletedBy: currentUser.id }, { transaction, }); await ex_bond_registers.destroy({ transaction }); return ex_bond_registers; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const ex_bond_registers = await db.ex_bond_registers.findOne( { where }, { transaction }, ); if (!ex_bond_registers) { return ex_bond_registers; } const output = ex_bond_registers.get({plain: true}); return output; } static async findAll(filter, options) { const limit = filter.limit || 0; let offset = 0; let where = {}; const currentPage = +filter.page; const user = (options && options.currentUser) || null; offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = []; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.reference_number) { where = { ...where, [Op.and]: Utils.ilike( 'ex_bond_registers', 'reference_number', filter.reference_number, ), }; } if (filter.mushok_number) { where = { ...where, [Op.and]: Utils.ilike( 'ex_bond_registers', 'mushok_number', filter.mushok_number, ), }; } if (filter.item_code) { where = { ...where, [Op.and]: Utils.ilike( 'ex_bond_registers', 'item_code', filter.item_code, ), }; } if (filter.warehouse_location) { where = { ...where, [Op.and]: Utils.ilike( 'ex_bond_registers', 'warehouse_location', filter.warehouse_location, ), }; } if (filter.batch_number) { where = { ...where, [Op.and]: Utils.ilike( 'ex_bond_registers', 'batch_number', filter.batch_number, ), }; } if (filter.entry_dateRange) { const [start, end] = filter.entry_dateRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, entry_date: { ...where.entry_date, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, entry_date: { ...where.entry_date, [Op.lte]: end, }, }; } } if (filter.quantityRange) { const [start, end] = filter.quantityRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, quantity: { ...where.quantity, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, quantity: { ...where.quantity, [Op.lte]: end, }, }; } } if (filter.valueRange) { const [start, end] = filter.valueRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, value: { ...where.value, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, value: { ...where.value, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } 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.ex_bond_registers.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( 'ex_bond_registers', 'reference_number', query, ), ], }; } const records = await db.ex_bond_registers.findAll({ attributes: [ 'id', 'reference_number' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['reference_number', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.reference_number, })); } };