我正在努力在我的库存应用程序中获得当前的剩余库存。我写了代码来计算剩余的数量,但它只根据购买的数量计算。我想要添加总售出数量,并找出购买和总售出数量之间的差异。如何计算总售出数量和剩余库存?
models.py
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=100)
slug = models.CharField(max_length=100)
price = models.DecimalField(max_digits=6, decimal_places=2)
quantity = models.IntegerField(null=True, blank=True)
category = models.ForeignKey(
Category, on_delete=models.CASCADE, blank=True, null=True)
def __str__(self):
return self.name
class Stock(models.Model):
sold_quantity = models.IntegerField(null=True, blank=True, default=0)
product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True)
def __str__(self):
return self.product.name
@property
def get_difference(self):
total = self.product.quantity-self.sold_quantity
return totalViews.py
def add_sales(request):
form = AddSalesForm()
if request.method == 'POST':
form = AddSalesForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.product.quantity -= instance.sold_quantity
instance.save()
return redirect('stock_details')
contex = {'form': form}
return render(request, 'stockmgmt/add_sales.html', contex)模板
{% extends 'stockmgmt/base.html' %}
{% block content %}
<form method="GET">
<a href="{% url 'add_sales' %}" class="btn btn-primary">Add Sales</a>
<p>
<table class="table table-bordered">
<tr>
<th>Item</th>
<th>Category</th>
<th>Purchased Qty</th>
<th>Sold Qty</th>
<th>Remaining Qty</th>
<th>Price</th>
<th>Action</th>
</tr>
{% for item in stocks %}
<tr>
<td>{{item.product.name}}</td>
<td>{{item.product.category}}</td>
<td>{{item.product.quantity}}</td>
<td>{{item.sold_quantity}}</td>
<td>{{item.get_difference}}</td>
<td>{{item.product.price}}</td>
<td><a href="{% url 'delete_sales' item.id %}" class="btn btn-danger">Delete</a>
<a href="" class="btn btn-primary">Edit</a>
</td>
</tr>
{% endfor %}
</table>
</form>
{% endblock %}

发布于 2020-08-20 22:07:09
每次下单后,您都需要更新现有库存
对于ex。
期初库存= 100当前库存= 100
在第一个订单10个之后
期初库存= 100当前库存=当前库存-订单数量= 100 - 10 = 90
第二个订单数量为5个
期初库存= 100当前库存=当前库存-订单量= 90 -5= 85
如果我购买或添加新库存,则会更新当前库存
增加50个数量后
现存量=现存量+进货数量= 85 + 50 = 135
您可以有一个日志表(Model),用于跟踪股票的收入和流出
希望你能理解我试图解释的概念
https://stackoverflow.com/questions/63506647
复制相似问题