mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-20 22:06:28 +00:00
Merge branch 'master' of https://github.com/inventree/InvenTree into price-history
This commit is contained in:
@ -7,7 +7,7 @@ from __future__ import unicode_literals
|
||||
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from django.http import JsonResponse
|
||||
from django.db.models import Q, F, Count, Prefetch, Sum
|
||||
from django.db.models import Q, F, Count
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from rest_framework import status
|
||||
@ -635,29 +635,15 @@ class PartList(generics.ListCreateAPIView):
|
||||
# TODO: Need to figure out a cheaper way of making this filter query
|
||||
|
||||
if stock_to_build is not None:
|
||||
# Filter only active parts
|
||||
queryset = queryset.filter(active=True)
|
||||
# Prefetch current active builds
|
||||
build_active_queryset = Build.objects.filter(status__in=BuildStatus.ACTIVE_CODES)
|
||||
build_active_prefetch = Prefetch('builds',
|
||||
queryset=build_active_queryset,
|
||||
to_attr='current_builds')
|
||||
parts = queryset.prefetch_related(build_active_prefetch)
|
||||
|
||||
# Get active builds
|
||||
builds = Build.objects.filter(status__in=BuildStatus.ACTIVE_CODES)
|
||||
# Store parts with builds needing stock
|
||||
parts_need_stock = []
|
||||
parts_needed_to_complete_builds = []
|
||||
# Filter required parts
|
||||
for build in builds:
|
||||
parts_needed_to_complete_builds += [part.pk for part in build.required_parts_to_complete_build]
|
||||
|
||||
# Find parts with active builds
|
||||
# where any subpart's stock is lower than quantity being built
|
||||
for part in parts:
|
||||
if part.current_builds:
|
||||
builds_ids = [build.id for build in part.current_builds]
|
||||
total_build_quantity = build_active_queryset.filter(pk__in=builds_ids).aggregate(quantity=Sum('quantity'))['quantity']
|
||||
|
||||
if part.can_build < total_build_quantity:
|
||||
parts_need_stock.append(part.pk)
|
||||
|
||||
queryset = queryset.filter(pk__in=parts_need_stock)
|
||||
queryset = queryset.filter(pk__in=parts_needed_to_complete_builds)
|
||||
|
||||
# Optionally limit the maximum number of returned results
|
||||
# e.g. for displaying "recent part" list
|
||||
|
@ -116,6 +116,12 @@ def inventree_docs_url(*args, **kwargs):
|
||||
return "https://inventree.readthedocs.io/"
|
||||
|
||||
|
||||
@register.simple_tag()
|
||||
def inventree_credits_url(*args, **kwargs):
|
||||
""" Return URL for InvenTree credits site """
|
||||
return "https://inventree.readthedocs.io/en/latest/credits/"
|
||||
|
||||
|
||||
@register.simple_tag()
|
||||
def setting_object(key, *args, **kwargs):
|
||||
"""
|
||||
|
@ -1959,6 +1959,11 @@ class PartPricing(AjaxView):
|
||||
|
||||
role_required = ['sales_order.view', 'part.view']
|
||||
|
||||
def get_quantity(self):
|
||||
""" Return set quantity in decimal format """
|
||||
|
||||
return Decimal(self.request.POST.get('quantity', 1))
|
||||
|
||||
def get_part(self):
|
||||
try:
|
||||
return Part.objects.get(id=self.kwargs['pk'])
|
||||
@ -1967,12 +1972,12 @@ class PartPricing(AjaxView):
|
||||
|
||||
def get_pricing(self, quantity=1, currency=None):
|
||||
|
||||
try:
|
||||
quantity = int(quantity)
|
||||
except ValueError:
|
||||
quantity = 1
|
||||
# try:
|
||||
# quantity = int(quantity)
|
||||
# except ValueError:
|
||||
# quantity = 1
|
||||
|
||||
if quantity < 1:
|
||||
if quantity <= 0:
|
||||
quantity = 1
|
||||
|
||||
# TODO - Capacity for price comparison in different currencies
|
||||
@ -2002,16 +2007,19 @@ class PartPricing(AjaxView):
|
||||
min_buy_price /= scaler
|
||||
max_buy_price /= scaler
|
||||
|
||||
min_unit_buy_price = round(min_buy_price / quantity, 3)
|
||||
max_unit_buy_price = round(max_buy_price / quantity, 3)
|
||||
|
||||
min_buy_price = round(min_buy_price, 3)
|
||||
max_buy_price = round(max_buy_price, 3)
|
||||
|
||||
if min_buy_price:
|
||||
ctx['min_total_buy_price'] = min_buy_price
|
||||
ctx['min_unit_buy_price'] = min_buy_price / quantity
|
||||
ctx['min_unit_buy_price'] = min_unit_buy_price
|
||||
|
||||
if max_buy_price:
|
||||
ctx['max_total_buy_price'] = max_buy_price
|
||||
ctx['max_unit_buy_price'] = max_buy_price / quantity
|
||||
ctx['max_unit_buy_price'] = max_unit_buy_price
|
||||
|
||||
# BOM pricing information
|
||||
if part.bom_count > 0:
|
||||
@ -2024,16 +2032,19 @@ class PartPricing(AjaxView):
|
||||
min_bom_price /= scaler
|
||||
max_bom_price /= scaler
|
||||
|
||||
min_unit_bom_price = round(min_bom_price / quantity, 3)
|
||||
max_unit_bom_price = round(max_bom_price / quantity, 3)
|
||||
|
||||
min_bom_price = round(min_bom_price, 3)
|
||||
max_bom_price = round(max_bom_price, 3)
|
||||
|
||||
if min_bom_price:
|
||||
ctx['min_total_bom_price'] = min_bom_price
|
||||
ctx['min_unit_bom_price'] = min_bom_price / quantity
|
||||
ctx['min_unit_bom_price'] = min_unit_bom_price
|
||||
|
||||
if max_bom_price:
|
||||
ctx['max_total_bom_price'] = max_bom_price
|
||||
ctx['max_unit_bom_price'] = max_bom_price / quantity
|
||||
ctx['max_unit_bom_price'] = max_unit_bom_price
|
||||
|
||||
# Stock history
|
||||
if part_settings.part_show_graph and part.total_stock > 1:
|
||||
@ -2077,10 +2088,11 @@ class PartPricing(AjaxView):
|
||||
|
||||
currency = None
|
||||
|
||||
try:
|
||||
quantity = int(self.request.POST.get('quantity', 1))
|
||||
except ValueError:
|
||||
quantity = 1
|
||||
quantity = self.get_quantity()
|
||||
|
||||
# Retain quantity value set by user
|
||||
form = self.form_class()
|
||||
form.fields['quantity'].initial = quantity
|
||||
|
||||
# TODO - How to handle pricing in different currencies?
|
||||
currency = None
|
||||
@ -2090,7 +2102,7 @@ class PartPricing(AjaxView):
|
||||
'form_valid': False,
|
||||
}
|
||||
|
||||
return self.renderJsonResponse(request, self.form_class(), data=data, context=self.get_pricing(quantity, currency))
|
||||
return self.renderJsonResponse(request, form, data=data, context=self.get_pricing(quantity, currency))
|
||||
|
||||
|
||||
class PartParameterTemplateCreate(AjaxCreateView):
|
||||
|
Reference in New Issue
Block a user