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

44 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-07-17 12:07:45 +00:00
const UserError = require('vn-loopback/util/user-error');
2021-02-10 14:03:11 +00:00
module.exports = Self => {
require('../methods/invoice-in/filter')(Self);
require('../methods/invoice-in/summary')(Self);
2021-07-23 12:06:48 +00:00
require('../methods/invoice-in/clone')(Self);
2021-09-24 06:24:34 +00:00
require('../methods/invoice-in/toBook')(Self);
2021-09-28 08:57:51 +00:00
require('../methods/invoice-in/getTotals')(Self);
2022-10-17 13:13:27 +00:00
require('../methods/invoice-in/invoiceInPdf')(Self);
2022-10-24 13:15:23 +00:00
require('../methods/invoice-in/invoiceInEmail')(Self);
2023-03-27 12:29:22 +00:00
require('../methods/invoice-in/getSerial')(Self);
2023-12-28 14:55:11 +00:00
require('../methods/invoice-in/corrective')(Self);
2024-04-22 09:45:58 +00:00
require('../methods/invoice-in/exchangeRateUpdate')(Self);
2024-03-05 15:14:05 +00:00
require('../methods/invoice-in/toUnbook')(Self);
2024-05-27 08:48:32 +00:00
require('../methods/invoice-in/updateInvoiceIn')(Self);
2023-12-28 14:55:11 +00:00
2023-07-17 12:07:45 +00:00
Self.rewriteDbError(function(err) {
2023-07-17 12:11:05 +00:00
if (err.code === 'ER_ROW_IS_REFERENCED_2' && err.sqlMessage.includes('vehicleInvoiceIn'))
2023-07-17 12:07:45 +00:00
return new UserError(`This invoice has a linked vehicle.`);
return err;
});
2024-11-29 10:53:07 +00:00
Self.observe('before save', async function(ctx) {
if (ctx.newInstance) 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);
if (invoiceIn.isBooked) throw new UserError('InvoiceIn is already booked');
});
2021-02-10 14:03:11 +00:00
};