73 lines
2.2 KiB
JavaScript
73 lines
2.2 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethodCtx('addFromPackaging', {
|
|
description: 'Create a receipt or return entry for a supplier with a specific travel',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'supplier',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The supplier id',
|
|
},
|
|
{
|
|
arg: 'isTravelReception',
|
|
type: 'boolean',
|
|
required: true,
|
|
description: 'Indicates if the travel associated with the entry is a return or receipt travel'
|
|
}],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/addFromPackaging`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.addFromPackaging = async(ctx, options) => {
|
|
const args = ctx.args;
|
|
const models = Self.app.models;
|
|
let tx;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const travelConfig = await models.TravelConfig.findOne({}, myOptions);
|
|
|
|
const today = new Date();
|
|
const yesterday = new Date(today);
|
|
yesterday.setDate(today.getDate() - 1);
|
|
const tomorrow = new Date(today);
|
|
tomorrow.setDate(today.getDate() + 1);
|
|
|
|
const travel = await models.Travel.create({
|
|
shipped: args.isTravelReception ? yesterday : today,
|
|
landed: args.isTravelReception ? today : tomorrow,
|
|
agencyModeFk: travelConfig.agencyFk,
|
|
warehouseInFk: travelConfig.warehouseOutFk,
|
|
warehouseOutFk: travelConfig.warehouseInFk
|
|
}, myOptions);
|
|
|
|
const entry = await models.Entry.create({
|
|
supplierFk: args.supplier,
|
|
travelFk: travel.id,
|
|
companyFk: travelConfig.companyFk
|
|
}, myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return entry;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|