2024-11-29 11:22:01 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.observe('before save', async function(ctx) {
|
2024-11-29 13:32:12 +00:00
|
|
|
if (ctx.isNewInstance) return;
|
2024-11-29 11:22:01 +00:00
|
|
|
|
|
|
|
const models = Self.app.models;
|
2024-12-02 16:48:40 +00:00
|
|
|
const invoiceIn = await models.InvoiceIn.findById(ctx.currentInstance.invoiceInFk, null, ctx.options);
|
2024-11-29 11:22:01 +00:00
|
|
|
if (invoiceIn.isBooked) throw new UserError('InvoiceIn is already booked');
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.observe('before delete', async function(ctx) {
|
|
|
|
const models = Self.app.models;
|
2024-12-02 16:48:40 +00:00
|
|
|
const invoiceInTax = await Self.findById(ctx.where.id, null, ctx.options);
|
|
|
|
const invoiceIn = await models.InvoiceIn.findById(invoiceInTax.invoiceInFk, null, ctx.options);
|
2024-11-29 11:22:01 +00:00
|
|
|
if (invoiceIn.isBooked) throw new UserError('InvoiceIn is already booked');
|
|
|
|
});
|
|
|
|
};
|