Пример #1
0
def inventory(request):
	if request.method == 'POST':
		#get the pending invoice for this user.
		# if one doesn't exist then create a new one
		user_invoice = Invoice.get_cart_invoice(request.user)

		if not user_invoice:
			response = HttpResponse()
			response.status_code = 303
			response['location'] = 'login'
			return response
				

		inventoryItem = InventoryType.objects.get(product_name=request.POST['item'])
		
		itemCount = int(request.POST['quantity'])
		#add invoice items to the invoice with the drone
		for x in range(itemCount):
			inv_item = InvoiceItem(invoice=user_invoice, inventory_type=inventoryItem)
			inv_item.save()
		
		#update the inventory count
		inventoryItem.stock_count = inventoryItem.stock_count - itemCount
		inventoryItem.save()
		
		response = HttpResponse()
		response.status_code = 303
		if request.POST['submit'] == 'add and go to checkout':
			response['location'] = 'checkout'
		else:
			response['location'] = 'inventory#body'
		return response
	
	inventory_items = InventoryType.objects.all()
	context = {'inventory_items': inventory_items}
	return render(request, 'app/inventory.html', context)
Пример #2
0
def checkout(request):
	userid = request.user
	try:
		cart_invoice = Invoice.objects.filter(status=Invoice.STATUS_PENDING).get(user=userid)
	except Invoice.DoesNotExist:
		context = {'cart_items': [], 'subtotal': 0, 'tax':0, 'total':0}
		return render(request, 'app/checkout.html', context)
	
	cart_items = cart_invoice.get_item_type_counts()

	if request.method == 'POST':
		response = HttpResponse()
		response.status_code = 303
		response['location'] = 'checkout'
		if request.POST['submit'] == 'confirm order':
			#change this to the delivering state
			cart_invoice.confirm_order()
			#redirect away from the checkout page
			response['location'] = 'status'
		elif request.POST['submit'] == 'update quantities':
			#for each item update the count
			for intype, count in cart_items.iteritems():
				#The POST has a field 'product-name-quantity=###' for each type to be removed
				# construct the string to lookup and get it from the POST. If it is not present
				# in the POST then assume this type is not to be removed
				quantity_str = (intype.product_name).lower().replace(' ','-') + "-quantity"
				new_count = int(request.POST[quantity_str])
				
				if new_count > count:

					#adding items of this type
					for i in range(new_count - count):
						inv_item = InvoiceItem(invoice=cart_invoice, inventory_type=intype)
						inv_item.save()
					intype.stock_count -= (new_count - count)
					intype.save()
					pass
				elif new_count < count:
					#removing items of this type
					cart_invoice.remove_type(intype, (count-new_count))
				else:
					#count is not changed
					pass
		elif request.POST['submit'] == 'remove selected':
			#for each item, if it is marked then remove from invoice
			invoice_types = InvoiceItem.objects.filter(invoice=cart_invoice).distinct('inventory_type')
			for intype in invoice_types:
				#The POST has a field 'product-name-marked="on"' for each type to be removed
				# construct the string to lookup and get it from the POST. If it is not present
				# in the POST then assume this type is not to be removed
				mark_str = (intype.inventory_type.product_name).lower().replace(' ','-') + "-marked"
				removal = request.POST.get(mark_str, 'off')
				if removal == 'on':
					cart_invoice.remove_type(intype.inventory_type, cart_items[intype.inventory_type])
		else:
			#unknown post
			print('Unknown POST submit: ' + request.POST['submit'])
			pass

		return response
	
	subtotal = Decimal(0.0)
	cart = []
	for itype, count in cart_items.iteritems():
		cart.append(type('',(object,),{'type': itype,'count': count, 'max':itype.stock_count + count})())
		#get the item description
		inventory_obj = InventoryType.objects.get(id=itype.id)
		#add price to subtotal
		subtotal += Decimal(count) * inventory_obj.price
	
	tax = (subtotal * Decimal(0.09)).quantize(Decimal('0.01'), rounding=ROUND_UP)
	total = subtotal + tax
	context = {'cart_items': cart, 'subtotal': subtotal, 'tax':tax, 'total':total}
	return render(request, 'app/checkout.html', context)