-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Regenerate allocations #661
Comments
This was quick and dirty solution by me, but might be helpful in the implementation: async recalculateAllocations(ctx: RequestContext): Promise<boolean> {
const candidateAdjustments = new Map();
const rawResults: { productVariantId: number, orderLineId: number | null, quantity: number, count: string }[] = await this.connection.getRepository(StockMovement).query(
`select *
from "order_item"
LEFT JOIN "order_line"
ON order_line.id = order_item."lineId"
LEFT JOIN "order"
ON "order".id = order_line."orderId"
LEFT JOIN "product_variant"
ON "product_variant".id = order_line."productVariantId"
where "lineId" in (select "id"
from "order_line"
where "orderId" in (
select "id"
from "order"
where "id" in (select "orderId"
from order_line
where "id" IN (
select "lineId" from "order_item"
LEFT JOIN "order_item_fulfillments_fulfillment"
ON order_item_fulfillments_fulfillment."orderItemId" = order_item.id
where "fulfillmentId" IS NULL and "cancelled" = false
)) AND "state" not in ('AddingItems', 'ArrangingPayment', 'Cancelled')
)
)`
);
rawResults.forEach(element => {
let candidateValue = 0;
if (candidateAdjustments.has(element.productVariantId)) {
candidateValue = candidateAdjustments.get(element.productVariantId);
}
candidateValue += 1;
candidateAdjustments.set(element.productVariantId, candidateValue);
});
await this.connection.getRepository(ProductVariant).update({}, {stockAllocated: 0});
const array = Array.from(candidateAdjustments, ([variantId, stockAllocated]) => ({ variantId, stockAllocated }));
await Promise.all(array.map(async({variantId, stockAllocated}) => {
try {
await this.connection.getRepository(ProductVariant).update({id: variantId, trackInventory: GlobalFlag.INHERIT}, {stockAllocated});
await this.connection.getRepository(ProductVariant).update({id: variantId, trackInventory: GlobalFlag.TRUE}, {stockAllocated});
} catch (error) {
console.log('error'+ error);
}
}));
return true;
} |
After giving this some thought, I have decided that this is best handled in conjunction with #1425. It makes sense to have a job like this run periodically and that also solves the problem of where the UI parts would go that controls this. So I'm deferring this to the next minor. |
Moving to v2.1 milestone because it goes together with the scheduled tasks feature |
Is there any update on this? |
@michaelbromley will this exact issue be included in core? As in, will we have If core will not include the actual |
Is your feature request related to a problem? Please describe.
After few weeks of implementation of stock movement feature we have quite a few product variants that because of an transition error, sometimes for unknown reason have unexpected stock allocation values - e.g. we have product variants that have negative allocations, or had some orders allocated multiple times.
The main issue to address here is ofc refinement of our project-specific order process and error handling to keep track of right allocations as much as possible, though I believe there will be edge cases/unpredictable errors that can result in wrong allocations that don't match to open orders. Currently when this happens there is no easy way how to get or update the current allocations.
Describe the solution you'd like
I propose a new function + button in AdminUI regenerate allocations, that when run would cycle through open orders and adjust allocations of all product variants that have invalid values. (and reset all allocations for product variants that are not in any of open order to 0).
The text was updated successfully, but these errors were encountered: