def create_order(request, id): cart = ShoppingCart.objects.get(user=request.user) item = SellItem.objects.get(pk=id) #create new order for this item # use sell_item.price to present the total price for order new_order = Order(user=request.user, sell_item=item) new_order.save() #remove the item from the shopping cart cart.sellItem.remove(item) cartNum = getCartNum(request) return render(request, 'create_order.html', { "order": new_order, "cartNum": cartNum })
def create_order_from_cart(request): """ 从购物车中创建订单 param参数:openid —— restaurant的openid wp_openid —— member的openid --- serializer: trade.serializers.OrderDetailSerializer omit_serializer: false responseMessages: - code: 200 message: OK - code: 400 message: Bad Request """ cart_id = request.GET.get('cart_id') try: cart = Cart.objects.get(pk=cart_id) except Cart.DoesNotExist: return Response('cart not found', status=status.HTTP_400_BAD_REQUEST) restaurant = cart.restaurant member = cart.member table = cart.table order = Order(restaurant=restaurant, member=member, table_name=table.name) order.save() price_total = 0 for cart_item in cart.cart_items.all(): order_item = OrderItem( order=order, category=cart_item.product.category.name, name=cart_item.product.name, img_key=cart_item.product.img_key, price=cart_item.product.price, unit=cart_item.product.unit, description=cart_item.product.description, count=cart_item.count, ) order_item.save() price_total += cart_item.product.price * cart_item.count # cart_item.delete() order.price = price_total order.save() table.order = order table.save() # cart.table = None # cart.save() serializer = OrderDetailSerializer(order) return Response(serializer.data, status=status.HTTP_200_OK)
def create_bundle_order(request): cart = ShoppingCart.objects.get(user=request.user) items = cart.buyItem.all() new_order = Order(user=request.user) new_order.save() totalprice = 0.00 for item in items: new_order.buy_item.add(item) totalprice += float(item.totalprice) if UserLocation.objects.filter(user=request.user): address = UserLocation.objects.filter(user=request.user)[0] else: address = [] cartNum = getCartNum(request) return render( request, 'create_order.html', { "order": new_order, "items": items, "totalprice": str(totalprice), "address": address, "cartNum": cartNum })
def create_order_from_cart(request): """ 从购物车中创建订单 param参数:openid —— restaurant的openid wp_openid —— member的openid --- serializer: trade.serializers.OrderDetailSerializer omit_serializer: false responseMessages: - code: 200 message: OK - code: 400 message: Bad Request """ cart_id = request.GET.get('cart_id') try: cart = Cart.objects.get(pk=cart_id) except Cart.DoesNotExist: return Response('cart not found', status=status.HTTP_400_BAD_REQUEST) restaurant = cart.restaurant member = cart.member table = cart.table order = Order(restaurant=restaurant, member=member, table_name=table.name) order.save() price_total = 0 for cart_item in cart.cart_items.all(): order_item = OrderItem(order=order, category=cart_item.product.category.name, name=cart_item.product.name, img_key=cart_item.product.img_key, price=cart_item.product.price, unit=cart_item.product.unit, description=cart_item.product.description, count=cart_item.count, ) order_item.save() price_total += cart_item.product.price * cart_item.count # cart_item.delete() order.price = price_total order.save() table.order = order table.save() # cart.table = None # cart.save() serializer = OrderDetailSerializer(order) return Response(serializer.data, status=status.HTTP_200_OK)
def testCompletion(self): # place an order order = Order(supplier=self.john, customer=self.jane, date=datetime.now(), code='1', value=0) order.save() o_wrench = OrderItem.objects.create(order=order, info=self.wrench, quantity=10, price=10) o_screwdriver = OrderItem.objects.create(order=order, info=self.screwdriver, quantity=20, price=20) o_pliers = OrderItem.objects.create(order=order, info=self.pliers, quantity=30, price=30) transfer = StockTransfer.objects.create(origin=self.john_warehouse, destination=self.jane_warehouse, date=datetime.now(), code='1') StockTransferItem.objects.create(order=o_wrench, product=self.wrench, transfer=transfer, quantity=10) StockTransferItem.objects.create(order=o_screwdriver, product=self.screwdriver, transfer=transfer, quantity=20) StockTransferItem.objects.create(order=o_pliers, product=self.pliers, transfer=transfer, quantity=30) order.evaluate() self.assertTrue(order.has_label(Order.CLOSED))
def save_order(out_trade_no, total): data = parsing_data(decode_order(out_trade_no)) u = User.objects.filter(id=data['u_id']).first() login_name = u.login_name t = TradeType.objects.filter(id=data['trade_group_type']).first() # 添加订单 o = Order() o.id = out_trade_no o.login_name = login_name o.trade_type = data['pay_type'] o.trade_group = data['trade_group_type'] o.total = total o.days = t.days o.desc = data['concurrent'] o.add_time = datetime.datetime.now() o.save() u = User.objects.filter(login_name=login_name).first() u.enable = int(data['concurrent']) # 判断当前用户所属分组,如果不是未付费会员、并且没有到过期时间。则该操作是一个升级或者续费的操作 if u.group_id > 1 and u.end_time > datetime.datetime.now(): u_trade_type = u.group.t.all()[0] t_price = t.price * 11 if t.days == 31 else t.price u_price = u_trade_type.price * 11 if u_trade_type.days == 31 else u_trade_type.price y_days = (u.end_time - datetime.datetime.now()).days u_unit_price = u_price / 366 t_unit_price = t_price / 366 y_price = y_days * u_unit_price n_days = y_price / t_unit_price # 升级或者续费,都是进行一个当前时间 + 余下时间 + 订单购买时间 u.end_time = datetime.datetime.now() + datetime.timedelta(days=n_days) + datetime.timedelta(days=o.days) else: # 过期时间 = 支付订单时间 + 过期天数 u.end_time = o.add_time + datetime.timedelta(days=o.days) u.group = TradeType.objects.filter(id=o.trade_group).first().group u.save()
def public_transfer(request): """对公转账""" if request.method == 'POST': # 该项记录由管理员进行添加财务信息,添加后,改变用户的权限 login_name = request.POST.get('login_name') # 获取当前用户 u = User.objects.filter(login_name=login_name).first() if not u: return resp(400, '没有该用户') trade_type_id = request.POST.get('trade_type') enable = int(request.POST.get('enable')) # 直接获取当前交易类型的用户分组,获取分组、获取交易天数、交易价格。 t = TradeType.objects.filter(id=trade_type_id).first() g = t.group d = t.days p = t.price m_type = 4 # 对公转账 o = Order() o.id = encode_order(f'{login_name}{g}{d}{p}') o.login_name = login_name o.trade_type = m_type o.trade_group = g.id o.total = p o.days = d o.desc = 1 o.save() # 改变用户行为 u.group = g u.enable = enable u.end_time = datetime.datetime.now() + datetime.timedelta(days=d) u.save() return resp()
def card_recharge(request): """卡号充值""" if request.method == 'POST': # 清除字符串两边空格 card_id = request.POST.get('card_id') card_pwd = request.POST.get('card_pwd') if card_id and card_pwd: card_id = card_id.strip() card_pwd = card_pwd.strip() else: return resp(205, '参数不正确') # 判断卡号是否存在 c = CardRechargeList.objects.filter(card_id=card_id).first() if not c: return resp(202, '资源不存在') if c.is_use: return resp(201, '资源已被使用') if c.card_pwd != card_pwd: return resp(203, '密码错误,请联系商家') # 通过校验,开始进行充值 token = request.META.get('HTTP_AUTHENTICATION') obj = check_token(token) if not obj: return resp(204, '用户信息过期') u_id = obj['id'] u = User.objects.filter(id=u_id).first() # 获取当前用户是否过期,如果用户存在会员并且没有过期,产生升级、续费的操作。 if u.group_id != 1 and u.end_time > datetime.datetime.now(): # 当前用户存在会员并且没有过期 # 获取到交易类型分辨vip等级 u_trade_type = u.group.t.all()[0] re_name = re.findall(r'(VI.*?\d)', u_trade_type.name)[0] if re_name in c.trade_type.name: # 当前会员进行续费,续费也可以是对月、年续费 return resp(300, '是否续费当前会员') else: # 当前用户进行升级 return resp(301, '是否升级会员') # 过期时间 = 支付订单时间 + 过期天数 u.end_time = datetime.datetime.now() + datetime.timedelta(days=c.trade_type.days) u.group = c.trade_type.group u.save() c.is_use = 1 c.save() # 创建订单 o = Order() o.id = encode_order(str(time.time())) o.login_name = u.login_name o.trade_type = 3 o.card_id = card_id o.trade_group = c.trade_type_id o.days = c.trade_type.days o.total = float(c.trade_type.price) o.desc = '1' o.save() return resp()
def filldb(self, admin): company = admin.get_profile().company # contacts c = Contact.objects.create(name='John') ContactDetail.objects.create(owner=c, type=ContactDetail.NUMBER, label='home', value='123-4567') ContactDetail.objects.create(owner=c, type=ContactDetail.ADDRESS, label='home', value='12 Sunshine St') c = Contact.objects.create(name='Jane') ContactDetail.objects.create(owner=c, type=ContactDetail.NUMBER, label='home', value='123-4567') ContactDetail.objects.create(owner=c, type=ContactDetail.ADDRESS, label='home', value='12 Sunset St') c = Contact.objects.create(name='Joe') ContactDetail.objects.create(owner=c, type=ContactDetail.NUMBER, label='home', value='123-4567') ContactDetail.objects.create(owner=c, type=ContactDetail.ADDRESS, label='home', value='12 Moon St.') def create_product(brand, model, summary): product = Product.objects.create(brand=brand, model=model, summary=summary) product.log(Product.REGISTER, admin) # catalog create_product( 'ANLY', 'AH3NC', 'TIMER', ) create_product( 'ANLY', 'AH3NB', 'TIMER', ) create_product( 'ANLY', 'AFR-1', 'FLOATLESS RELAY', ) create_product( 'ANLY', 'TRD-N', 'WYE-DELTA TIMER', ) create_product( 'FUJI', 'SC-0', 'CONTACTOR', ) create_product( 'FUJI', 'SC-4-1', 'CONTACTOR', ) create_product( 'FUJI', 'SC-5-1', 'CONTACTOR', ) create_product( 'FUJI', 'SC-N1', 'CONTACTOR', ) create_product( 'BROYCE', 'B8PRC-220', 'OVER-UNDER VOLTAGE RELAY', ) create_product( 'BROYCE', 'B8PRC-440', 'OVER-UNDER VOLTAGE RELAY', ) def create_service(name): service = Service.objects.create(name=name) service.log(Service.REGISTER, admin) create_service('Control Assembly') create_service('Panelboard Assembly') create_service('Delivery') # inventory loc = Location.objects.create(owner=company, name='Location A', address='Top of the world') loc.log(Location.REGISTER, admin) loc = Location.objects.create(owner=company, name='Location B', address='Bottom of the world') loc.log(Location.REGISTER, admin) # orders customer = Contact.objects.get(pk=2) product1 = Product.objects.get(pk=1) product2 = Product.objects.get(pk=2) product3 = Product.objects.get(pk=3) def init_stock(location_id, product_id): stock = Stock.objects.get(location__id=1, product__id=product_id) stock.quantity += 100 stock.save() init_stock(1, 1) init_stock(1, 2) init_stock(1, 3) order = Order(customer=customer, supplier=company, date=datetime.today(), code='1234') order.save() item = OrderItem() #item 1 item.document = order item.price = 99 item.quantity = 2 item.info = product1 item.order = order item.save() item = OrderItem() #item 2 item.document = order item.price = 16 item.quantity = 3 item.info = product2 item.order = order item.save() item = OrderItem() #item 2 item.document = order item.price = 33 item.quantity = 25 item.info = product3 item.order = order item.save() order.log(Order.REGISTER, admin) pass