Last active
October 21, 2019 12:16
-
-
Save wmakeev/e718961a11a3916360830ac5c4728694 to your computer and use it in GitHub Desktop.
Создаем заказы поставщику для заказов на производство #moysklad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const _ = require('lodash') | |
const fetch = require('node-fetch') | |
const Moysklad = require('moysklad') | |
const ms = Moysklad({ apiVersion: '1.2', fetch }) | |
const href = ref => ms.buildUrl(ref) | |
const PROCESSING_ORDER_IDS = ['710a1333-f3ee-11e9-0a80-012300049f97'] | |
;(async () => { | |
const { rows: processingOrders } = await ms.GET('entity/processingorder', { | |
filter: { | |
id: PROCESSING_ORDER_IDS | |
}, | |
expand: 'positions.assortment', | |
limit: 100 | |
}) | |
let purchaseOrders = _(processingOrders) | |
// собираем все позиции из всех заказов на пр-во в один массив | |
.flatMap(order => order.positions.rows) | |
// группировка позиций по поставщику | |
.groupBy(pos => pos.assortment.supplier.meta.href) | |
// объединяем одинаковые позиции | |
.mapValues(positions => | |
_(positions) | |
// группируем позиции по товару | |
.groupBy(pos => pos.assortment.meta.href) | |
// получаем позицию для заказа поставщику (кол-во и цена) | |
.mapValues(positions => ({ | |
quantity: positions.reduce((total, pos) => total + pos.quantity, 0), | |
price: positions[0].assortment.buyPrice.value | |
})) | |
.value() | |
) | |
// создаем объекты заказов поставщику | |
.map((positions, supHref) => { | |
return { | |
organization: { | |
meta: processingOrders[0].organization.meta | |
}, | |
agent: { | |
meta: { | |
type: 'counterparty', | |
href: supHref | |
} | |
}, | |
// устанавливаем пользовательские атрибуты если нужно | |
attributes: [ | |
{ | |
meta: { | |
type: 'attributemetadata', | |
href: href( | |
'entity/purchaseorder/metadata/attributes/4efebea9-04d2-11e3-8960-7054d21a8d1e' | |
) | |
}, | |
value: { | |
name: 'В работе' | |
} | |
} | |
], | |
positions: _(positions) | |
.map((posInfo, asmtHref) => ({ | |
assortment: { | |
meta: { | |
type: 'product', | |
href: asmtHref | |
} | |
}, | |
...posInfo | |
})) | |
.value() | |
} | |
}) | |
.value() | |
purchaseOrders = await ms.POST('entity/purchaseorder', purchaseOrders) | |
console.log('Созданы заказы поставщикам:', purchaseOrders.map(po => po.name)) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment