def __init__(self, pk, *args, **kwargs): super(BadDebtForm, self).__init__(*args, **kwargs) self.fields['staff'].queryset = UserProfile.objects.filter( user_type="Staff").filter(user__is_active=True).filter( is_active=True).filter(user__is_superuser=False) self.fields['staff'].empty_label = "Choose staff" self.fields['customer'].disabled = True self.fields['customer'].queryset = UserProfile.objects.filter( user__id=pk).filter(user__is_superuser=False) self.fields['customer'].empty_label = None self.fields['amount'].max_value = get_customer_debt(pk) self.fields['amount'].initial = get_customer_debt(pk)
def getCustomerInfo(request, *args, **kwargs): context = list() customer = UserProfile.objects.filter(id=kwargs.get("pk")).first().user print(customer) if customer.first_name and customer.last_name: name = customer.first_name + " " + customer.last_name else: name = customer.username userProfile = UserProfile.objects.filter(user=customer).first() if userProfile: credit_limit = userProfile.credit_limit balance = userProfile.balance else: credit_limit = 0.0 balance = 0.0 info = { "name": name, "credit_limit": credit_limit, 'balance': get_customer_debt(customer.pk), } context.append(info) return HttpResponse(json.dumps(context))
def customer_debt(pk): return get_customer_debt(pk)
def post(self, request, *args, **kwargs): context = list() form = SalesForm(request.POST) if form.is_valid(): new_form = form.save(commit=False) customer = form.cleaned_data.get("customer_name").user staff = form.cleaned_data.get("staff_name").user new_form.customer = customer new_form.staff = staff if form.cleaned_data.get("customer_name").credit_limit == "": credit_limit = 0 else: credit_limit = form.cleaned_data.get("customer_name").credit_limit new_form.created_by = request.user if form.cleaned_data.get("customer_name").credit_limit == '': credit_limit = 0 else: credit_limit = form.cleaned_data.get( "customer_name").credit_limit user_branch_obj = UserProfile.objects.filter( user=request.user).first().branch new_form.sale_branch = user_branch_obj try: new_form.sale_date = datetime.datetime.strptime( str(request.POST.get("sale_date")).strip(), "%d %B %Y").date() except: new_form.sale_date = datetime.datetime.strptime( str(request.POST.get("sale_date")).strip(), "%d %B, %Y").date() total_sale_dept = get_customer_debt( customer.pk) + float(request.POST.get("total_price")) if total_sale_dept > float(credit_limit) and not form.cleaned_data.get("sale_type"): new_form.waiting_approval = True products = request.POST.getlist("product_selected") quantities = request.POST.getlist("quantity_selected") prices = request.POST.getlist("price_selected") print(products) print(quantities) print(prices) if total_sale_dept > float(form.cleaned_data.get("customer_name").credit_limit) and not form.cleaned_data.get("sale_type"): new_form.save() for (product, quantity, price) in zip(products, quantities, prices): saleItem = SaleItem() saleItem.sale = new_form saleItem.product = Product.objects.filter( id=product).first() saleItem.quantity = quantity saleItem.price = price saleItem.save() info = { "status": True, "message": "Successfully Registered and Waiting Approval", "id": new_form.pk } context.append(info) return HttpResponse(json.dumps(context)) else: new_form.save() if form.cleaned_data.get("payment_method"): if not form.cleaned_data.get("payment_method") == "Cash Collections": account_transaction = AccountTransaction() account_transaction.account = Account.objects.filter( id=form.cleaned_data.get("payment_method")).first() account_transaction.amount = request.POST.get( "total_price") account_transaction.transanction_type = "deposit" account_transaction.created_by = request.user account_transaction.content_object = new_form account_transaction.save() for (product, quantity, price) in zip(products, quantities, prices): saleItem = SaleItem() saleItem.sale = new_form saleItem.product = Product.objects.filter(id=product).first() saleItem.quantity = quantity saleItem.price = price saleItem.save() info = { "status": True, "message": "Successfully Registered", "id": new_form.pk } context.append(info) return HttpResponse(json.dumps(context)) else: print(form.errors) context = { 'title': "Add Sales", "form": form } return render(request, 'pages/add_sales.html', context)