Skip to content
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

Pre-fill quantity on part ordering form #3395

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion InvenTree/InvenTree/api_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@


# InvenTree API version
INVENTREE_API_VERSION = 66
INVENTREE_API_VERSION = 67

"""
Increment this API version number whenever there is a significant change to the API that any clients need to know about

v67 -> 2022-07-25 : https://github.com/inventree/InvenTree/pull/3395
- Adds a 'requirements' endpoint for Part instance
- Provides information on outstanding order requirements for a given part

v66 -> 2022-07-24 : https://github.com/inventree/InvenTree/pull/3393
- Part images can now be downloaded from a remote URL via the API
- Company images can now be downloaded from a remote URL via the API
Expand Down
36 changes: 36 additions & 0 deletions InvenTree/part/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,40 @@ def add_schedule_entry(date, quantity, title, label, url):
return Response(schedule)


class PartRequirements(RetrieveAPI):
"""API endpoint detailing 'requirements' information for aa particular part.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo


This endpoint returns information on upcoming requirements for:

- Sales Orders
- Build Orders
- Total requirements

As this data is somewhat complex to calculate, is it not included in the default API
"""

queryset = Part.objects.all()

def retrieve(self, request, *args, **kwargs):
"""Construct a response detailing Part requirements"""

part = self.get_object()

data = {
"available_stock": part.available_stock,
"on_order": part.on_order,
"required_build_order_quantity": part.required_build_order_quantity(),
"allocated_build_order_quantity": part.build_order_allocation_count(),
"required_sales_order_quantity": part.required_sales_order_quantity(),
"allocated_sales_order_quantity": part.sales_order_allocation_count(pending=True),
}

data["allocated"] = data["allocated_build_order_quantity"] + data["allocated_sales_order_quantity"]
data["required"] = data["required_build_order_quantity"] + data["required_sales_order_quantity"]

return Response(data)


class PartMetadata(RetrieveUpdateAPI):
"""API endpoint for viewing / updating Part metadata."""

Expand Down Expand Up @@ -1997,6 +2031,8 @@ class BomItemSubstituteDetail(RetrieveUpdateDestroyAPI):
# Endpoint for future scheduling information
re_path(r'^scheduling/', PartScheduling.as_view(), name='api-part-scheduling'),

re_path(r'^requirements/', PartRequirements.as_view(), name='api-part-requirements'),

# Endpoint for duplicating a BOM for the specific Part
re_path(r'^bom-copy/', PartCopyBOM.as_view(), name='api-part-bom-copy'),

Expand Down
25 changes: 24 additions & 1 deletion InvenTree/templates/js/translated/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ function orderParts(parts_list, options={}) {

var thumb = thumbnailImage(part.thumbnail || part.image);

// The "quantity" field should have been provided for each part
// Default quantity value
var quantity = part.quantity || 1;

if (quantity < 0) {
Expand Down Expand Up @@ -1017,6 +1017,29 @@ function orderParts(parts_list, options={}) {
return '{% trans "No matching purchase orders" %}';
}
}, null, opts);

// Request 'requirements' information for each part
inventreeGet(`/api/part/${part.pk}/requirements/`, {}, {
success: function(response) {
var required = response.required || 0;
var allocated = response.allocated || 0;
var available = response.available_stock || 0;

// Based on what we currently 'have' on hand, what do we need to order?
var deficit = Math.max(required - allocated, 0);

if (available < deficit) {
var q = deficit - available;

updateFieldValue(
`quantity_${part.pk}`,
q,
{},
opts
);
}
}
});
});

// Add callback for "add to purchase order" button
Expand Down