2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-18 13:05:42 +00:00

Scheduling information is now calculated on the server, and provided via a new API endpoint

- Much simpler than sequencing multiple API calls
This commit is contained in:
Oliver
2022-03-01 22:54:49 +11:00
parent dfd6684699
commit 776dffe779
3 changed files with 161 additions and 152 deletions

View File

@ -2002,158 +2002,27 @@ function loadPartSchedulingChart(canvas_id, part_id) {
}
];
function addScheduleEntry(date, delta, label, url) {
// Adds a new entry to the schedule
// First, iterate through to find an insertion index (based on date)
var found = false;
for (var idx = 0; idx < stock_schedule.length; idx++) {
var entry = stock_schedule[idx];
if (date < entry.date) {
stock_schedule.splice(idx, 0, {
date: date,
delta: delta,
label: label,
url: url,
/* Request scheduling information for the part.
* Note that this information has already been 'curated' by the server,
* and arranged in increasing chronological order
*/
inventreeGet(
`/api/part/${part_id}/scheduling/`,
{},
{
async: false,
success: function(response) {
response.forEach(function(entry) {
stock_schedule.push({
date: moment(entry.date),
delta: entry.quantity,
label: entry.label,
url: entry.url,
})
});
found = true;
break;
}
}
if (!found) {
stock_schedule.push({
date: date,
delta: delta,
label: label,
url: url,
});
}
}
// Extract purchase order information from the server
if (part_info.purchaseable) {
inventreeGet(
`/api/order/po-line/`,
{
pending: true,
base_part: part_id,
order_detail: true,
},
{
async: false,
success: function(line_items) {
line_items.forEach(function(line_item) {
// Extract target_date information from the response.
// If the line_item does not have an individual target date, maybe the parent order does?
var target_date = line_item.target_date || line_item.order_detail.target_date;
// How many to receive?
var delta = Math.max(line_item.quantity - line_item.received, 0);
// TODO: What do we do if there is no target_date set for a PO line item?
// TODO: Do we just ignore it??
if (target_date && delta > 0) {
var td = moment(target_date);
if (td >= today) {
// TODO: Improve labels for purchase order lines
addScheduleEntry(td, delta, "Purchase Order", '/index/');
} else {
// Ignore any entries that are in the "past"
// TODO: Can we better handle this case?
}
}
});
}
}
);
}
// Extract sales order information from the server
if (part_info.salable) {
inventreeGet(
`/api/order/so-line/`,
{
part: part_id,
pending: true,
order_detail: true,
},
{
async: false,
success: function(line_items) {
line_items.forEach(function(line_item) {
// Extract target_date information from the response.
// If the line_item does not have an individual target date, maybe the parent order does?
var target_date = line_item.target_date || line_item.order_detail.target_date;
var delta = Math.max(line_item.quantity - line_item.shipped, 0);
// TODO: What do we do if there is no target_date set for a PO line item?
// TODO: Do we just ignore it??
if (target_date && delta > 0) {
var td = moment(target_date);
if (td >= today) {
// TODO: Improve labels for sales order items
addScheduleEntry(td, -delta, "Sales Order", '/index/');
} else {
// Ignore any entries that are in the "past"
// TODO: Can we better handle this case?
}
}
});
}
}
);
}
// Request build orders for this part
if (part_info.assembly) {
inventreeGet(
`/api/build/`,
{
part: part_id,
active: true,
},
{
async: false,
success: function(build_orders) {
build_orders.forEach(function(build_order) {
var target_date = build_order.target_date;
var delta = Math.max(build_order.quantity - build_order.completed, 0);
// TODO: How do we handle the case where the build order does not have a target date??
// TODO: Do we just ignore it?
if (target_date && delta > 0) {
var td = moment(target_date);
if (td >= today) {
addScheduleEntry(td, delta, "Build Order", "");
} else {
// TODO: Handle case where the build order is in the "past"
}
}
});
}
}
)
}
);
// Iterate through future "events" to calculate expected quantity