From bf5c366b5db0b9d03047688ab0d56992be6c02aa Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 21 Feb 2023 00:56:10 +1100 Subject: [PATCH] Remove "calculate price" button from SalesOrderLineItem - Existing method is extremely expensive - Will be replaced with an API driven approach --- InvenTree/order/urls.py | 2 - InvenTree/order/views.py | 109 +-------------------- InvenTree/templates/js/translated/order.js | 27 ----- 3 files changed, 1 insertion(+), 137 deletions(-) diff --git a/InvenTree/order/urls.py b/InvenTree/order/urls.py index 278914bd75..6fca35e2a1 100644 --- a/InvenTree/order/urls.py +++ b/InvenTree/order/urls.py @@ -18,8 +18,6 @@ purchase_order_detail_urls = [ purchase_order_urls = [ - re_path(r'^pricing/', views.LineItemPricing.as_view(), name='line-pricing'), - # Display detail view for a single purchase order re_path(r'^(?P\d+)/', include(purchase_order_detail_urls)), diff --git a/InvenTree/order/views.py b/InvenTree/order/views.py index b3909971e0..35947f7299 100644 --- a/InvenTree/order/views.py +++ b/InvenTree/order/views.py @@ -4,9 +4,7 @@ import logging from decimal import Decimal, InvalidOperation from django.db.utils import IntegrityError -from django.forms import HiddenInput, IntegerField from django.http import HttpResponseRedirect -from django.http.response import JsonResponse from django.shortcuts import get_object_or_404 from django.urls import reverse from django.utils.translation import gettext_lazy as _ @@ -18,14 +16,11 @@ from common.views import FileManagementFormView from company.models import SupplierPart # ManufacturerPart from InvenTree.helpers import DownloadFile from InvenTree.views import AjaxView, InvenTreeRoleMixin -from part.models import Part -from part.views import PartPricing from plugin.views import InvenTreePluginViewMixin from . import forms as order_forms from .admin import PurchaseOrderLineItemResource, SalesOrderLineItemResource -from .models import (PurchaseOrder, PurchaseOrderLineItem, SalesOrder, - SalesOrderLineItem) +from .models import PurchaseOrder, PurchaseOrderLineItem, SalesOrder logger = logging.getLogger("inventree") @@ -301,105 +296,3 @@ class PurchaseOrderExport(AjaxView): filedata = dataset.export(format=export_format) return DownloadFile(filedata, filename) - - -class LineItemPricing(PartPricing): - """View for inspecting part pricing information.""" - - class EnhancedForm(PartPricing.form_class): - """Extra form options""" - pk = IntegerField(widget=HiddenInput()) - so_line = IntegerField(widget=HiddenInput()) - - form_class = EnhancedForm - - def get_part(self, id=False): - """Return the Part instance associated with this view""" - if 'line_item' in self.request.GET: - try: - part_id = self.request.GET.get('line_item') - part = SalesOrderLineItem.objects.get(id=part_id).part - except Part.DoesNotExist: - return None - elif 'pk' in self.request.POST: - try: - part_id = self.request.POST.get('pk') - part = Part.objects.get(id=part_id) - except Part.DoesNotExist: - return None - else: - return None - - if id: - return part.id - return part - - def get_so(self, pk=False): - """Return the SalesOrderLineItem associated with this view""" - so_line = self.request.GET.get('line_item', None) - if not so_line: - so_line = self.request.POST.get('so_line', None) - - if so_line: - try: - sales_order = SalesOrderLineItem.objects.get(pk=so_line) - if pk: - return sales_order.pk - return sales_order - except Part.DoesNotExist: - return None - return None - - def get_quantity(self): - """Return set quantity in decimal format.""" - qty = Decimal(self.request.GET.get('quantity', 1)) - if qty == 1: - return Decimal(self.request.POST.get('quantity', 1)) - return qty - - def get_initials(self): - """Return initial context values for this view""" - initials = super().get_initials() - initials['pk'] = self.get_part(id=True) - initials['so_line'] = self.get_so(pk=True) - return initials - - def post(self, request, *args, **kwargs): - """Respond to a POST request to get particular pricing information""" - REF = 'act-btn_' - act_btn = [a.replace(REF, '') for a in self.request.POST if REF in a] - - # check if extra action was passed - if act_btn and act_btn[0] == 'update_price': - # get sales order - so_line = self.get_so() - if not so_line: - self.data = {'non_field_errors': [_('Sales order not found')]} - else: - quantity = self.get_quantity() - price = self.get_pricing(quantity).get('unit_part_price', None) - - if not price: - self.data = {'non_field_errors': [_('Price not found')]} - else: - # set normal update note - note = _('Updated {part} unit-price to {price}') - - # check qunatity and update if different - if so_line.quantity != quantity: - so_line.quantity = quantity - note = _('Updated {part} unit-price to {price} and quantity to {qty}') - - # update sale_price - so_line.sale_price = price - so_line.save() - - # parse response - data = { - 'form_valid': True, - 'success': note.format(part=str(so_line.part), price=str(so_line.sale_price), qty=quantity) - } - return JsonResponse(data=data) - - # let the normal pricing view run - return super().post(request, *args, **kwargs) diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js index f1044c020d..4afd8fda0a 100644 --- a/InvenTree/templates/js/translated/order.js +++ b/InvenTree/templates/js/translated/order.js @@ -4058,8 +4058,6 @@ function loadSalesOrderLineItemTable(table, options={}) { if (part.assembly) { html += makeIconButton('fa-tools', 'button-build', row.part, '{% trans "Build stock" %}'); } - - html += makeIconButton('fa-dollar-sign icon-green', 'button-price', pk, '{% trans "Calculate price" %}'); } html += makeIconButton('fa-clone', 'button-duplicate', pk, '{% trans "Duplicate line item" %}'); @@ -4247,31 +4245,6 @@ function loadSalesOrderLineItemTable(table, options={}) { } ); }); - - // Callback for displaying price - $(table).find('.button-price').click(function() { - var pk = $(this).attr('pk'); - var idx = $(this).closest('tr').attr('data-index'); - var row = $(table).bootstrapTable('getData')[idx]; - - launchModalForm( - '{% url "line-pricing" %}', - { - submit_text: '{% trans "Calculate price" %}', - data: { - line_item: pk, - quantity: row.quantity, - }, - buttons: [ - { - name: 'update_price', - title: '{% trans "Update Unit Price" %}' - }, - ], - success: reloadTable, - } - ); - }); } $(table).inventreeTable({