def requestCurCart(request): cart = None user = None try: #gibt es schon einen warenkorb? cartid = request.session['cart_id'] cart = Cart.objects.get(id=cartid,isOrdered=False) if request.user.is_authenticated(): #soll der warenkorb einem benutzer gehoeren? if not cart.user: # warenkorb hat keine besitzer (vorm login der fall) cart.user = request.user cart.save() if cart.user == request.user: #warenkorb gehoert der email-addy -> return return cart elif not cart.user: return cart # warenkorb gehoert niemandem except (KeyError, Cart.DoesNotExist): # es gibt keinen warenkorb if request.user.is_authenticated(): user = request.user try: cart = Cart.objects.get(user=user,isActive=True,isOrdered=False) request.session['cart_id'] = cart.id return cart except: pass cart = Cart() # warenkorb erstellen if user: #soll korb einem benutzer gehoeren? cart.user = user cart.isActive = True cart.save() request.session['cart_id'] = cart.id # id in sesison speichern return cart
def orderOverview(request): cart = requestCurCart(request) if int(cart.get_item_count()) > 0: try: order = Order.objects.get(id=request.session['order_id'], user=request.user,order_date__isnull=True) # Wurde Bestellung abgeschickt? if request.method == 'POST': order.order_date = datetime.datetime.now() # copy the current shipping fee order.shipping_cost = decimal.Decimal('%s' % order.cart.get_shipping_fee()) order.save() order.billnumber() #Speichern, dass Cart bestellt wurde order.cart.isActive = False order.cart.isOrdered = True order.cart.save() # Kopie anlegen, wenn Cart einen Namen hat # ansonsten kann ein Cart doppelt bestellt werden if order.cart.name: # neuen Cart mit alten Cart-Daten erstellen newcart = Cart() newcart.name = order.cart.name newcart.user = order.cart.user newcart.save() # Produkte aus altem Cart in neuen legen for cp in CartProduct.objects.filter(cart=order.cart): newcp = CartProduct(cart=newcart) newcp.product = cp.product newcp.amount = cp.amount newcp.save() # MixedProdukte aus altem Cart in neuen legen for mcp in CartProduct.objects.filter(cart=order.cart): newmcp = CartMixProduct(cart=newcart) newmcp.product = mcp.product newmcp.amount = mcp.amount newmcp.save() # Die verwendeten Gutscheine entwerten ccs = CartCoupon.objects.filter(cart=order.cart) for cc in ccs: # ist der Gutschein ein einmaliger Gutschein? if cc.coupon.isgift == True: cc.coupon.isValid = False cc.coupon.valid_to = datetime.datetime.now() cc.coupon.save() # Restguthaben von Gutscheinen dem Guthabenkonto gutschreiben if decimal.Decimal(order.cart.get_total_with_fee()) < decimal.Decimal('0.00'): try: cacc = CouponAccount.objects.get(user=request.user) except CouponAccount.DoesNotExist: cacc = CouponAccount(user=request.user, balance=0 ) cacc.save() ctrans = CouponAccountTransaction(cacc=cacc) ctrans.amount = decimal.Decimal(order.cart.get_total_with_fee()) * -1 ctrans.message = 'Restguthaben von Bestellung %s' % order.billnumber() ctrans.isValid = False ctrans.save() cacc.balance += decimal.Decimal(order.cart.get_total_with_fee()) * -1 cacc.save() sendOrderMail(order) sendOrderMailAdmin(order) if order.paymenttype.code == 'bill': return HttpResponseRedirect(reverse('shoppy_order_thanks')) else: return HttpResponseRedirect(reverse('shoppy_custom_order_view',args=[order.id])) cart = order.cart cartitems = CartProduct.objects.all().filter(cart=cart) cartmixitems = CartMixProduct.objects.all().filter(cart=cart) cartcoupon = CartCoupon.objects.all().filter(cart=cart) couponmoney = decimal.Decimal('0.00') for cc in cartcoupon: couponmoney += cc.coupon.valueMoney couponmoney = moneyfmt(couponmoney, places=2, dp=',', sep='.') return render_to_response('shop/overview.html',{ 'order': order, 'couponmoney': couponmoney, 'cartitems': cartitems, 'cartmixitems': cartmixitems, 'hideRightNavi':True, 'hideLeftNavi':True },context_instance=RequestContext(request)) except Order.DoesNotExist, KeyError: return render_to_response('error.html', { 'errormsg': 'Oooops. Da lief wohl was falsch!', },context_instance=RequestContext(request))