def post(self, request, order_id, product_id): """ 주문을 생성합니다. order_id : 입력받은 order_id를 이용하여 검증된 유저의 장바구니 여부를 확인합니다. product_id : 개별적으로 주문을하고 장바구니에 담길 상품의 id입니다. """ data = json.loads(request.body) user_id = request.userid try: if Order.objects.filter(username_id=user_id).exists(): user_address = User.objects.get(id=user_id).address if user_address: if Order.objects.filter(id=order_id).exists(): if Product.objects.filter(id=product_id).exists(): order_id = Order.objects.get(id=order_id) product_id = Product.objects.get(id=product_id) new_order = OrderProduct(order=order_id, product=product_id, quantity=data['quantity']) new_order.save() return JsonResponse({'message': "장바구니에 담겼습니다"}, status=200) return JsonResponse({'message': '해당 상품은 존재하지 않습니다'}, status=400) return JsonResponse({'message': '주소를 등록해주세요'}, status=400) else: new_order = Order(username_id=user_id) new_order.save() return JsonResponse({'message': '장바구니가 생성됬습니다'}, status=200) except KeyError: return JsonResponse({'error': '올바르지 않은 키 값'}, status=400)
def orderproduct(request): category = Category.objects.all() current_user = request.user schopcart = ShopCart.objects.filter(user_id=current_user) total = 0 for rs in schopcart: total += rs.product.price * rs.quantity if request.method == 'POST': form = OrderForm(request.POST) if form.is_valid(): data = Order() data.first_name = form.cleaned_data['first_name'] data.last_name = form.cleaned_data['last_name'] data.address = form.cleaned_data['address'] data.city = form.cleaned_data['city'] data.country=form.cleaned_data['country'] data.phone = form.cleaned_data['phone'] data.user_id = current_user.id data.total = total data.ip = request.META.get('REMOTE_ADDR') ordercode = get_random_string(5).upper() data.code = ordercode data.save() schopcart = ShopCart.objects.filter(user_id = current_user.id) for rs in schopcart: detail = OrderProduct() detail.order_id =data.id detail.product_id =rs.product_id detail.user_id = current_user.id detail.quantity = rs.quantity product = Product.objects.get(id = rs.product_id) product.amount = rs.quantity product.save() detail.price = rs.product.price detail.amount = rs.amount detail.save() ShopCart.objects.filter(user_id=current_user.id).delete() request.session['cart_items'] =0 messages.success(request,"Siparişiniz verildi, Teşekkürler") return render(request,'order_Completed.html',{'ordercode':ordercode,'category':category}) else: messages.error(request,form.errors) return HttpResponseRedirect("/order/orderproduct") form = OrderForm() profile = UserProfile.objects.get(user_id = current_user.id) context = {'schopcart':schopcart, 'category':category, 'total':total, 'form':form, 'profile':profile,} return render(request,'Order_Form.html',context)
def orderproduct(request): category = Category.objects.all() current_user = request.user shopcart = ShopCart.objects.filter(user_id=current_user) total = 0 for rs in shopcart: total += rs.product.price * rs.quantity if request.method == 'POST': form = OrderForm() if form.is_valid(): data = Order() data.first_name = form.cleaned_data['first_name'] data.last_name = form.cleaned_data['last_name'] data.phone = form.cleaned_data['phone'] data.address = form.cleaned_data['address'] data.city = form.cleaned_data['city'] data.country = form.cleaned_data['country'] data.user_id = current_user.id data.ip = request.META('REMOTE_ADDR') ordercode = get_random_string(12).upper() data.code = ordercode data.save() shopcart = ShopCart.objects.filter(user_id=current_user.id) for rs in shopcart: detail = OrderProduct() detail.order_id = data.id detail.product = rs.product_id detail.user_id = rs.user_id detail.quantity = rs.quantity detail.price = rs.product.price detail.amount = rs.product.amount detail.save() product = Product.objects.get(rs.product_id) product.amount -= rs.quantity product.save() ShopCart.objects.filter(user_id=current_user.id).delete() request.session['cart_item'] = 0 messages.success(request, "SIzning buyurtmangiz qabul qilindi!") return render(request, 'ordercomplete.html', { 'ordercode': ordercode, 'category': category}) else: messages.warning(request, form.errors) return HttpResponseRedirect('/order/orderproduct') form = OrderForm shopcart = ShopCart.objects.filter(user_id=current_user.id) profile = UserProfile.objects.filter(user_id=current_user.id) context = { 'shopcart': shopcart, 'category': category, 'total': total, 'profile': profile, 'form': form, } return HttpResponseRedirect(request, 'orderproduct.html', context)
def siparis(request, id): setting = Setting.objects.get(pk=1) category = Category.objects.all() current_user = request.user # #Access User Session information, şu anki userin bilgilerini aliyor schopcart = ShopCart.objects.get(pk=id) total = schopcart.ay * schopcart.urun.price if id: #shopcartdan id gelmisse data = Order() # order modeline baglaniyor data.user_id = current_user.id #bilgileri Order veritabanina aktariyor data.status = 'New' data.total = total data.ip = request.META.get('REMOTE_ADDR') data.save() #aktarilan verileri kaydediyor schopcart = ShopCart.objects.get( pk=id) #Shopcartdan gelen idli veriyi OrderProducta ekliyor #orderproducta ekleme detail = OrderProduct() #orderproductta baglaniiyor detail.order_id = data.id detail.urun_id = schopcart.urun_id detail.user_id = current_user.id detail.ay = schopcart.ay detail.price = schopcart.urun.price detail.amount = schopcart.amount detail.status = 'New' detail.save() ShopCart.objects.get( pk=id).delete() #shopcartdan alinan veriyi siliyor prodata = Property.objects.get( pk=schopcart.urun_id) #shopcartdan siparis verilen urunu aliyor prodata.status = "False" #statusunu false yapiyor prodata.save() messages.success( request, 'Siparisiniz Basariyla Alinmistir') #basarili islem mesaji context = { 'setting': setting, 'category': category, 'shopcart': schopcart, 'total': total, } return HttpResponseRedirect('/user/orders') #user orderse gonderiyor else: messages.warning(request, "Siparis Alinamadi") #basarisiz islem return HttpResponseRedirect('/user/shopcart') #shopcartda kaliyor
def checkout(request): current_user = request.user shopcart = ShopCart.objects.filter(user_id = current_user.id) total = 0 q = 0 for s in shopcart: total += s.product.price * s.quantity q += s.quantity setting = Setting.objects.all() catagories = ProductCategories.objects.all() data = Order() if request.method == "POST": data.full_name = request.POST.get("name") data.address = request.POST.get("address") data.city = request.POST.get("city") data.country = request.POST.get("country") data.phone = request.POST.get("tel") data.user_id = current_user.id data.ip = request.META.get('REMOTE_ADDR') data.total = total orderCode = get_random_string(5).upper() data.code = orderCode data.save() for rs in shopcart: detail = OrderProduct() detail.order_id = data.id detail.product_id = rs.product_id detail.user_id = current_user.id detail.quantity = rs.quantity detail.price = rs.product.price detail.amount = total detail.save() ShopCart.objects.filter(user_id=current_user.id).delete()#clear and delete shopcart messages.success(request,"Your order has been completed. Thank you.") return render(request, 'order_Completed.html',{'orderCode':orderCode,'category':catagories}) context = { "catagories":catagories, "setting":setting, 'total':total, 'q':q, } return render(request, 'checkout.html',context=context)
def create(self, validated_data): carts = validated_data['carts'] product_ids = [cart.product_option.product_id for cart in carts] request_user = self.context['request'].user with transaction.atomic(): order = Order.objects.create( user=request_user, order_uid=self.create_order_uid(), shipping_price=self.get_calculated_products_shipping_price( product_ids), shipping_address=validated_data['shipping_address'], shipping_request_note=validated_data['shipping_request_note'], is_paid=False, ) order_products = [ OrderProduct( user=request_user, cart=cart, product_option=cart.product_option, order=order, product_price=cart.product_option.product.price, ordered_quantity=cart.quantity, ordered_price=cart.product_option.product.price * cart.quantity, status=OrderProduct.STATUS_CHOICE.PENDING, ) for cart in carts ] OrderProduct.objects.bulk_create(order_products) Payment.objects.create(order=order, pay_price=order.shipping_price + sum([ order_product.ordered_price for order_product in order_products ]), pay_method=getattr( Payment.PAY_METHOD_CHOICE, validated_data['pay_method'])) return order
def create(self, validated_data): validated_data['creator_id'] = self.context['request'].user order_positions_data = validated_data.pop('order_positions') total_price = 0 for order_position_data in order_positions_data: product = Product.objects.filter( id=order_position_data['product'].id).first() total_price += product.price * order_position_data['quantity'] validated_data['total_price'] = total_price order = super().create(validated_data) raw_order_positions = [] for order_position_data in order_positions_data: order_position = OrderProduct( order=order, product=order_position_data['product'], quantity=order_position_data['quantity']) raw_order_positions.append(order_position) OrderProduct.objects.bulk_create(raw_order_positions) return order
def orderproduct(request): category = Category.objects.all() current_user = request.user shopcart = ShopCart.objects.filter(user_id=current_user.id) total = 0 for rs in shopcart: total += rs.product.fiyat * rs.quantity if request.method == 'POST': form = OrderForm(request.POST) if form.is_valid(): data = Order() data.first_name = form.cleaned_data['first_name'] data.last_name = form.cleaned_data['last_name'] data.address = form.cleaned_data['address'] data.city = form.cleaned_data['city'] data.phone = form.cleaned_data['phone'] data.user_id = current_user.id data.total = total data.ip = request.META.get('REMOTE_ADDR') ordercode = get_random_string(5).upper() data.code = ordercode data.save() #move shopcrt items for order products items shopcart = ShopCart.objects.filter(user_id=current_user.id) for rs in shopcart: #schopcart detail = OrderProduct() detail.order_id = data.id detail.product_id = rs.product_id detail.user_id = current_user.id detail.quantity = rs.quantity detail.fiyat = rs.product.fiyat detail.stok_durum = rs.stok_durum detail.save() # ***************< Reduce >***************# product = Kitap.objects.get(id=rs.product_id) product.stok_durum -= rs.quantity product.save() #***************<^^^^^^^^^^>***************# ShopCart.objects.filter(user_id=current_user.id).delete() request.session['cart_items'] = 0 messages.success(request, "Your order has been completed , thank you!") return render(request, "Order_Complated.html", { 'ordercode': ordercode, 'category': category }) else: messages.warning(request, form.errors) return HttpResponseRedirect("/order/orderproduct") form = OrderForm() profile = UserProfile.objects.get(user_id=current_user.id) context = { 'shopcart': shopcart, # hoca Schopcart yazmis 'category': category, 'total': total, 'profile': profile, 'form': form, } return render(request, 'Order_Form.html', context)
def orderproduct(request): category = Category.objects.all() current_user = request.user shopcart = ShopCart.objects.filter(user_id=current_user.id) total = 0 for rs in shopcart: if rs.product.variant == 'None': total += rs.product.price * rs.quantity else: total += rs.variant.price * rs.quantity transport = 0 for rs in shopcart: transport += rs.product.transportation * rs.quantity final_total = total + transport amount = 0 for rs in shopcart: amount += rs.quantity if request.method == 'POST': # if there is a post form = OrderForm(request.POST) # return HttpResponse(request.POST.items()) if form.is_valid(): # Send Credit card to bank, If the bank responds ok, continue, if not, show the error req_data = { "merchant_id": MERCHANT, "amount": amount, "callback_url": CallbackURL, "description": description, "metadata": {"mobile": mobile, "email": email} } req_header = {"accept": "application/json", "content-type": "application/json'"} req = requests.post(url=ZP_API_REQUEST, data=json.dumps( req_data), headers=req_header) authority = req.json()['data']['authority'] if len(req.json()['errors']) == 0: return redirect(ZP_API_STARTPAY.format(authority=authority)) else: e_code = req.json()['errors']['code'] e_message = req.json()['errors']['message'] return HttpResponse(f"Error code: {e_code}, Error Message: {e_message}") # .............. data = Order() data.first_name = form.cleaned_data['first_name'] # get product quantity from form data.last_name = form.cleaned_data['last_name'] data.address = form.cleaned_data['address'] data.city = form.cleaned_data['city'] data.phone = form.cleaned_data['phone'] data.user_id = current_user.id data.total = total data.ip = request.META.get('REMOTE_ADDR') ordercode = get_random_string(5).upper() # random cod data.code = ordercode data.save() # for rs in shopcart: detail = OrderProduct() detail.order_id = data.id detail.product_id = rs.product_id detail.user_id = current_user.id detail.quantity = rs.quantity if rs.product.variant == 'None': detail.price = rs.product.price else: detail.price = rs.variant.price detail.variant_id = rs.variant_id detail.amount = rs.amount detail.save() # ***Reduce quantity of sold product from Amount of Product if rs.product.variant == 'None': product = Product.objects.get(id=rs.product_id) product.amount -= rs.quantity product.save() else: variant = Variants.objects.get(id=rs.product_id) variant.quantity -= rs.quantity variant.save() # ************ <> ***************** setting = Setting.objects.get(pk=1) ShopCart.objects.filter(user_id=current_user.id).delete() # Clear & Delete shopcart request.session['cart_items'] = 0 messages.success(request, "خرید شما با موفقیت انجام شد") return render(request, 'Order_Completed.html', {'ordercode': ordercode, 'category': category, 'setting': setting, 'amount':amount}) else: messages.warning(request, form.errors) return HttpResponseRedirect("/order/orderproduct") form = OrderForm() profile = UserProfile.objects.get(user_id=current_user.id) setting = Setting.objects.get(pk=1) context = {'shopcart': shopcart, 'category': category, 'total': total, 'form': form, 'profile': profile, 'transport': transport, 'final_total': final_total, 'setting': setting, 'amount':amount } return render(request, 'Order_Form.html', context)
def orderproduct(request): category = Category.objects.all() current_user = request.user schopcart = Shopcart.objects.filter(user_id=current_user.id) total = 0 for rs in schopcart: total += rs.quantity if request.method == 'POST': form = OrderForm(request.POST) if form.is_valid(): data = Order() data.first_name = form.cleaned_data['first_name'] data.last_name = form.cleaned_data['last_name'] data.address = form.cleaned_data['address'] data.city = form.cleaned_data['city'] data.country = form.cleaned_data['country'] data.phone = form.cleaned_data['phone'] data.user_id = current_user.id data.total = total data.ip = request.META.get('REMOTE_ADDR') ordercode = get_random_string(5).upper() tarih = datetime.now() + timedelta(days=15) data.teslim_tarih = tarih data.code = ordercode data.save() # Move Shopcart items to order Product items schopcart = Shopcart.objects.filter(user_id=current_user.id) for rs in schopcart: detail = OrderProduct() detail.order_id = data.id #order id detail.product_id = rs.product_id detail.user_id = current_user.id detail.quantity = rs.quantity #reduce quantity of sold product product = Product.objects.get(id=rs.product_id) product.amount -= rs.quantity product.save() #************* <> ********* detail.save() Shopcart.objects.filter( user_id=current_user.id).delete() #clear and delete shop cart request.session['cart_items'] = 0 messages.success(request, "Kitabı Ödünç Aldınız, Teşekkür Ederiz") return render(request, 'Order_Completed.html', { 'ordercode': ordercode, 'category': category, 'tarih': tarih }) else: messages.warning(request, form.errors) return HttpResponseRedirect("/order/orderproduct") form = OrderForm() profile = UserProfile.objects.get(user_id=current_user.id) context = { 'schopcart': schopcart, 'category': category, 'total': total, 'form': form, 'profile': profile } return render(request, 'Order_Form.html', context)
def getProductItem(self, item, orderId, pets): response = None if item['type'] == "product": product = Product.objects.get( pk=item['id'] ) orderProduct = OrderProduct( pType = item['type'], product_id = item['id'], order_id = orderId, pet_id = item['pet'], unit_price = product.price, quantity = item['quantity'] ) totalPrice = product.price*item['quantity'] tmpProductPrices = None # Recurring Purchases # if 'is_recurring' in item and item['is_recurring'] == True: product.is_recurring = item['is_recurring'] orderProduct.order_every = item['orderEvery'] if 'orderEvery' in item else '' orderProduct.date = item['scheduleDate'] tmpProductPrices = self.getRecurringProdPrice(item, product)[0] orderProduct.unit_price = tmpProductPrices.price totalPrice = orderProduct.unit_price * item['quantity'] else: product.is_recurring = False totalPrice = orderProduct.unit_price * item['quantity'] response = { 'item': orderProduct, 'source': product, 'pet': next((x for x in pets if x.id == item['pet']), None), 'totalPrice': totalPrice, 'productPrice': tmpProductPrices if tmpProductPrices is not None else None } else: service = Service.objects.get( pk=item['id'] ) orderProduct = OrderProduct( pType = item['type'], service_id = item['id'], order_id = orderId, pet_id = item['pet'], unit_price = service.price, quantity = item['quantity'], timeOption = item['timeOption'], date = item['scheduleDate'], time = item['time'] if 'time' in item else '', notes = item['notes'] if 'notes' in item else '', ) totalPrice = service.price*item['quantity'] # Recurring Purchases if service.is_recurring: orderProduct.order_every = item['orderEvery'] if 'orderEvery' in item else '' orderProduct.scheduleDate = item['scheduleDate'] response = { 'item': orderProduct, 'source': service, 'pet': next((x for x in pets if x.id == item['pet']), None), 'totalPrice': totalPrice } return response