def process_stock_adjustment(request, supplier_id, product_id): try: if request.method != "POST": return JsonResponse({}, status=405) product = Product.objects.select_related("sales_unit").filter( pk=product_id).first() form = StockAdjustmentForm(data=request.POST, sales_unit=product.sales_unit) if form.is_valid(): return _process_stock_adjustment(form, request, supplier_id, product_id) errors = form.non_field_errors() if form.errors: for field, field_errors in form.errors.items(): errors.append(f"{field}: {','.join(field_errors)}") error_message = ugettext( "Please check submitted values and try again ({}).").format( ", ".join(errors)) return JsonResponse({"message": error_message}, status=400) except Exception as exc: error_message = ugettext( "Please check submitted values and try again (%(error)s).") % { "error": exc } return JsonResponse({"message": error_message}, status=400)
def process_stock_adjustment(request, supplier_id, product_id): try: if request.method != "POST": raise Exception(_("Not allowed")) form = StockAdjustmentForm(request.POST) if form.is_valid(): data = form.cleaned_data supplier = Supplier.objects.get(id=supplier_id) stock_adjustment = supplier.module.adjust_stock( product_id, delta=data.get("delta"), purchase_price=data.get("purchase_price"), created_by=request.user ) success_message = { "stockInformationDiv": "#%s" % get_stock_information_div_id( stock_adjustment.supplier, stock_adjustment.product), "updatedStockInformation": get_stock_information_html( stock_adjustment.supplier, stock_adjustment.product), "message": get_adjustment_success_message(stock_adjustment) } return JsonResponse(success_message, status=200) error_message = ugettext("Error, please check submitted values and try again.") return JsonResponse({"message": error_message}, status=400) except Exception as exc: error_message = ugettext( "Error, please check submitted values and try again (%(error)s).") % {"error": exc} return JsonResponse({"message": error_message}, status=400)
def process_stock_adjustment(request, supplier_id, product_id): try: if request.method != "POST": raise Exception(_("Not allowed")) form = StockAdjustmentForm(request.POST) if form.is_valid(): data = form.cleaned_data supplier = Supplier.objects.get(id=supplier_id) stock_adjustment = supplier.module.adjust_stock( product_id, delta=data.get("delta"), purchase_price=data.get("purchase_price"), created_by=request.user) success_message = { "stockInformationDiv": "#%s" % get_stock_information_div_id(stock_adjustment.supplier, stock_adjustment.product), "updatedStockInformation": get_stock_information_html(stock_adjustment.supplier, stock_adjustment.product), "message": get_adjustment_success_message(stock_adjustment) } return JsonResponse(success_message, status=200) error_message = ugettext( "Error, please check submitted values and try again.") return JsonResponse({"message": error_message}, status=400) except Exception as exc: error_message = ugettext( "Error, please check submitted values and try again (%(error)s)." ) % { "error": exc } return JsonResponse({"message": error_message}, status=400)
def get_stock_adjustment_div(request, supplier, product): """ Get html string to adjust stock values Contains inputs for purchase_price_value and delta :param request: HTTP request :type request: django.http.HttpRequest :param supplier: shuup Supplier :type supplier: shuup.core.models.Supplier :param product: shuup Product :type product: shuup.core.models.Product :return: html div as a string :rtype: str """ latest_adjustment = StockAdjustment.objects.filter( product=product, supplier=supplier, type=StockAdjustmentType.INVENTORY).last() purchase_price = (latest_adjustment.purchase_price_value if latest_adjustment else Decimal("0.00")) context = { "product": product, "supplier": supplier, "delta_step": pow(0.1, product.sales_unit.decimals), "adjustment_form": StockAdjustmentForm(initial={"purchase_price": purchase_price, "delta": None}), } if "shuup.notify" in settings.INSTALLED_APPS: from shuup.notify.models import Notification context["alert_limit_form"] = AlertLimitForm(initial={"alert_limit": 0}) if not get_missing_permissions(request.user, get_default_model_permissions(Notification)): context["notify_url"] = reverse("shuup_admin:notify.script.list") else: context["notify_url"] = "" return render_to_string("shuup/simple_supplier/admin/add_stock_form.jinja", context=context, request=request)
def get_stock_adjustment_div(request, supplier, product): """ Get html string to adjust stock values Contains inputs for purchase_price_value and delta :param request: HTTP request :type request: django.http.HttpRequest :param supplier: shuup Supplier :type supplier: shuup.core.models.Supplier :param product: shuup Product :type product: shuup.core.models.Product :return: html div as a string :rtype: str """ stock = StockCount.objects.get_or_create(product=product, supplier=supplier)[0] latest_adjustment = StockAdjustment.objects.filter( product=product, supplier=supplier, type=StockAdjustmentType.INVENTORY).last() purchase_price = (latest_adjustment.purchase_price.as_rounded().value if latest_adjustment else Decimal()) context = { "product": product, "supplier": supplier, "delta_step": pow(0.1, product.sales_unit.decimals) if product.sales_unit.decimals else 0, "adjustment_form": StockAdjustmentForm(initial={"purchase_price": purchase_price, "delta": None}), "stock": stock, "stock_managed_form": StockManagedForm(initial={"stock_managed": not stock.stock_managed}) } if "shuup.notify" in settings.INSTALLED_APPS: context["alert_limit_form"] = AlertLimitForm(initial={"alert_limit": stock.alert_limit or Decimal()}) if not get_missing_permissions(request.user, ("notify.script.list",)): context["notify_url"] = reverse("shuup_admin:notify.script.list") else: context["notify_url"] = "" return render_to_string("shuup/simple_supplier/admin/add_stock_form.jinja", context=context, request=request)
def get_stock_adjustment_div(request, supplier, product): """ Get html string to adjust stock values Contains inputs for purchase_price_value and delta :param request: HTTP request :type request: django.http.HttpRequest :param supplier: shuup Supplier :type supplier: shuup.core.models.Supplier :param product: shuup Product :type product: shuup.core.models.Product :return: html div as a string :rtype: str """ latest_adjustment = StockAdjustment.objects.filter( product=product, supplier=supplier).order_by("-created_on").first() purchase_price = (latest_adjustment.purchase_price_value if latest_adjustment else Decimal("0.00")) context = { "product": product, "supplier": supplier, "delta_step": pow(0.1, product.sales_unit.decimals), "form": StockAdjustmentForm(initial={"purchase_price": purchase_price, "delta": None}) } return render_to_string("shuup/simple_supplier/admin/add_stock_form.jinja", context=context, request=request)
def test_adjustment_form(rf, admin_user): """ Test StockAdjustmentForm. """ partial_sales_unit = SalesUnit.objects.create( identifier="test-sales-partial", decimals=2, name="Partial unit") form = StockAdjustmentForm( data={ "purchase_price": 10, "delta": 1.2, }, sales_unit=partial_sales_unit, ) assert form.is_valid() integer_sales_unit = SalesUnit.objects.create( identifier="test-sales-integer", decimals=0, name="Integer unit") form = StockAdjustmentForm( data={ "purchase_price": 10, "delta": 1.2, }, sales_unit=integer_sales_unit, ) assert not form.is_valid()
def test_alet_form(rf, admin_user): """ Test AlertLimitForm. """ partial_sales_unit = SalesUnit.objects.create( identifier="test-sales-partial", decimals=2, name="Partial unit") integer_sales_unit = SalesUnit.objects.create( identifier="test-sales-integer", decimals=0, name="Integer unit") form = AlertLimitForm(data={"alert_limit": Decimal("10.43")}, sales_unit=partial_sales_unit) assert form.is_valid() form = StockAdjustmentForm(data={"alert_limit": 1.2}, sales_unit=integer_sales_unit) assert not form.is_valid()