salix/modules/invoiceIn/back/models/invoice-in.js

44 lines
1.7 KiB
JavaScript

const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
require('../methods/invoice-in/filter')(Self);
require('../methods/invoice-in/summary')(Self);
require('../methods/invoice-in/clone')(Self);
require('../methods/invoice-in/toBook')(Self);
require('../methods/invoice-in/getTotals')(Self);
require('../methods/invoice-in/invoiceInPdf')(Self);
require('../methods/invoice-in/invoiceInEmail')(Self);
require('../methods/invoice-in/getSerial')(Self);
require('../methods/invoice-in/corrective')(Self);
require('../methods/invoice-in/exchangeRateUpdate')(Self);
require('../methods/invoice-in/toUnbook')(Self);
require('../methods/invoice-in/updateInvoiceIn')(Self);
Self.rewriteDbError(function(err) {
if (err.code === 'ER_ROW_IS_REFERENCED_2' && err.sqlMessage.includes('vehicleInvoiceIn'))
return new UserError(`This invoice has a linked vehicle.`);
return err;
});
Self.observe('before save', async function(ctx) {
if (ctx.isNewInstance) return;
const changes = ctx.data || ctx.instance;
const orgData = ctx.currentInstance;
let isNotEditable = orgData.isBooked || (!orgData.isBooked && changes.isBooked);
if (isNotEditable) {
for (const [key, value] of Object.entries(changes)) {
if (key !== 'isBooked' && value !== orgData[key])
throw new UserError('InvoiceIn is already booked');
}
}
});
Self.observe('before delete', async function(ctx) {
const invoiceIn = await Self.findById(ctx.where.id, null, ctx.options);
if (invoiceIn.isBooked) throw new UserError('InvoiceIn is already booked');
});
};