Add basic stock handling
This commit is contained in:
parent
481575e968
commit
11d3b740aa
@ -366,6 +366,14 @@ class Order(models.Model):
|
|||||||
|
|
||||||
objects = OrderManager()
|
objects = OrderManager()
|
||||||
|
|
||||||
|
def minus_stock(self):
|
||||||
|
for line in self.lines.all():
|
||||||
|
line.minus_stock()
|
||||||
|
|
||||||
|
def add_stock(self):
|
||||||
|
for line in self.lines.all():
|
||||||
|
line.add_stock()
|
||||||
|
|
||||||
def get_total_quantity(self):
|
def get_total_quantity(self):
|
||||||
return sum([line.quantity for line in self])
|
return sum([line.quantity for line in self])
|
||||||
|
|
||||||
@ -443,6 +451,16 @@ class OrderLine(models.Model):
|
|||||||
def quantity_unfulfilled(self):
|
def quantity_unfulfilled(self):
|
||||||
return self.quantity - self.quantity_fulfilled
|
return self.quantity - self.quantity_fulfilled
|
||||||
|
|
||||||
|
def minus_stock(self):
|
||||||
|
if self.variant.track_inventory:
|
||||||
|
self.variant.stock -= self.quantity
|
||||||
|
self.variant.save()
|
||||||
|
|
||||||
|
def add_stock(self):
|
||||||
|
if self.variant.track_inventory:
|
||||||
|
self.variant.stock += self.quantity
|
||||||
|
self.variant.save()
|
||||||
|
|
||||||
|
|
||||||
class TrackingNumber(models.Model):
|
class TrackingNumber(models.Model):
|
||||||
order = models.ForeignKey(
|
order = models.ForeignKey(
|
||||||
|
|||||||
@ -11,7 +11,7 @@
|
|||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
{{form.as_p}}
|
{{form.as_p}}
|
||||||
<p class="form__submit">
|
<p class="form__submit">
|
||||||
<input class="action-button" type="submit" value="Create variant"> or <a href="{% url 'dashboard:product-detail' product.pk %}">cancel</a>
|
<input class="action-button" type="submit" value="Save changes"> or <a href="{% url 'dashboard:product-detail' product.pk %}">cancel</a>
|
||||||
</p>
|
</p>
|
||||||
</form>
|
</form>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
@ -234,6 +234,11 @@ class OrderCancelView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
|
|||||||
'status': OrderStatus.CANCELED
|
'status': OrderStatus.CANCELED
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def form_valid(self, form):
|
||||||
|
form.instance.add_stock()
|
||||||
|
form.instance.save()
|
||||||
|
return super().form_valid(form)
|
||||||
|
|
||||||
def get_success_url(self):
|
def get_success_url(self):
|
||||||
return reverse('dashboard:order-detail', kwargs={'pk': self.object.pk})
|
return reverse('dashboard:order-detail', kwargs={'pk': self.object.pk})
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,6 @@
|
|||||||
<h1>Welcome to our new website!</h1>
|
<h1>Welcome to our new website!</h1>
|
||||||
<h4>NEW COOL LOOK, SAME GREAT COFFEE</h4>
|
<h4>NEW COOL LOOK, SAME GREAT COFFEE</h4>
|
||||||
</div>
|
</div>
|
||||||
{# Home > Category > "Coffee/Merchandise" #}
|
|
||||||
<article>
|
<article>
|
||||||
<div class="breadcrumbs">
|
<div class="breadcrumbs">
|
||||||
<menu>
|
<menu>
|
||||||
|
|||||||
@ -35,7 +35,7 @@ from accounts.forms import (
|
|||||||
AddressForm as AccountAddressForm, CustomerUpdateForm
|
AddressForm as AccountAddressForm, CustomerUpdateForm
|
||||||
)
|
)
|
||||||
from core.models import (
|
from core.models import (
|
||||||
ProductCategory, Product, ProductOption,
|
ProductCategory, Product, ProductVariant, ProductOption,
|
||||||
Order, Transaction, OrderLine, Coupon, ShippingRate
|
Order, Transaction, OrderLine, Coupon, ShippingRate
|
||||||
)
|
)
|
||||||
from core.forms import ShippingRateForm
|
from core.forms import ShippingRateForm
|
||||||
@ -79,7 +79,12 @@ class CartAddProductView(SingleObjectMixin, FormView):
|
|||||||
return reverse('storefront:cart-detail')
|
return reverse('storefront:cart-detail')
|
||||||
|
|
||||||
def get_form(self, form_class=None):
|
def get_form(self, form_class=None):
|
||||||
variants = self.get_object().variants.all()
|
variants = self.get_object().variants.filter(
|
||||||
|
Q(track_inventory=False) | Q(
|
||||||
|
track_inventory=True,
|
||||||
|
stock__gt=0
|
||||||
|
)
|
||||||
|
)
|
||||||
options = ProductOption.objects.filter(products__pk=self.get_object().pk)
|
options = ProductOption.objects.filter(products__pk=self.get_object().pk)
|
||||||
if form_class is None:
|
if form_class is None:
|
||||||
form_class = self.get_form_class()
|
form_class = self.get_form_class()
|
||||||
@ -168,8 +173,10 @@ class ProductCategoryDetailView(DetailView):
|
|||||||
Prefetch(
|
Prefetch(
|
||||||
'product_set',
|
'product_set',
|
||||||
queryset=Product.objects.filter(
|
queryset=Product.objects.filter(
|
||||||
visible_in_listings=True
|
Q(visible_in_listings=True),
|
||||||
)
|
Q(variants__track_inventory=False) |
|
||||||
|
Q(variants__track_inventory=True) & Q(variants__stock__gt=0)
|
||||||
|
).distinct()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return object_list
|
return object_list
|
||||||
@ -192,7 +199,12 @@ class ProductDetailView(FormMixin, DetailView):
|
|||||||
form_class = AddToCartForm
|
form_class = AddToCartForm
|
||||||
|
|
||||||
def get_form(self, form_class=None):
|
def get_form(self, form_class=None):
|
||||||
variants = self.object.variants.all()
|
variants = self.object.variants.filter(
|
||||||
|
Q(track_inventory=False) | Q(
|
||||||
|
track_inventory=True,
|
||||||
|
stock__gt=0
|
||||||
|
)
|
||||||
|
)
|
||||||
options = ProductOption.objects.filter(products__pk=self.object.pk)
|
options = ProductOption.objects.filter(products__pk=self.object.pk)
|
||||||
if form_class is None:
|
if form_class is None:
|
||||||
form_class = self.get_form_class()
|
form_class = self.get_form_class()
|
||||||
@ -370,6 +382,7 @@ def paypal_order_transaction_capture(request, transaction_id):
|
|||||||
cart = Cart(request)
|
cart = Cart(request)
|
||||||
order = Order.objects.get(pk=request.session.get('order_id'))
|
order = Order.objects.get(pk=request.session.get('order_id'))
|
||||||
order.status = OrderStatus.UNFULFILLED
|
order.status = OrderStatus.UNFULFILLED
|
||||||
|
order.minus_stock()
|
||||||
try:
|
try:
|
||||||
coupon = Coupon.objects.get(
|
coupon = Coupon.objects.get(
|
||||||
code=request.session.get('coupon_code')
|
code=request.session.get('coupon_code')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user