Import entry buys from lastest buy #861
|
@ -52,23 +52,59 @@ module.exports = Self => {
|
|||
}
|
||||
|
||||
try {
|
||||
const entry = await models.Entry.findById(id, null, myOptions);
|
||||
// const stmts = [];
|
||||
|
||||
const entry = await models.Entry.findById(id, {
|
||||
include: [{
|
||||
relation: 'travel',
|
||||
scope: {
|
||||
fields: ['warehouseInFk', 'landed'],
|
||||
}
|
||||
}]
|
||||
}, myOptions);
|
||||
|
||||
await entry.updateAttributes({
|
||||
observation: args.observation,
|
||||
ref: args.ref
|
||||
}, myOptions);
|
||||
|
||||
const travel = entry.travel();
|
||||
await Self.rawSql('CALL buyUltimate(?, ?)', [
|
||||
travel.warehouseInFk,
|
||||
travel.landed
|
||||
], myOptions);
|
||||
|
||||
const buyUltimate = await Self.rawSql(`
|
||||
SELECT *
|
||||
FROM tmp.buyUltimate
|
||||
WHERE warehouseFk = ?
|
||||
`, [travel.warehouseInFk], myOptions);
|
||||
|
||||
const lastItemBuys = new Map();
|
||||
for (const buy of buyUltimate)
|
||||
lastItemBuys.set(buy.itemFk, buy);
|
||||
|
||||
const buys = [];
|
||||
for (let buy of args.buys) {
|
||||
if (!buy.itemFk)
|
||||
throw new Error('The itemFk is required');
|
||||
|
||||
const lastItemBuy = lastItemBuys.get(buy.itemFk);
|
||||
const lastBuy = await models.Buy.findById(lastItemBuy.buyFk, null, myOptions);
|
||||
|
||||
const stickers = 1;
|
||||
const quantity = (stickers * buy.packing);
|
||||
buys.push({
|
||||
entryFk: entry.id,
|
||||
itemFk: buy.itemFk,
|
||||
stickers: 1,
|
||||
quantity: 1,
|
||||
stickers: stickers,
|
||||
quantity: quantity,
|
||||
packing: buy.packing,
|
||||
grouping: buy.grouping,
|
||||
buyingValue: buy.buyingValue,
|
||||
packageFk: buy.packageFk
|
||||
packageFk: buy.packageFk,
|
||||
groupingMode: lastBuy.groupingMode,
|
||||
weight: lastBuy.weight
|
||||
});
|
||||
|
||||
await models.ItemMatchProperties.upsert({
|
||||
|
|
Loading…
Reference in New Issue