def api_remain(request): # 进出库 if request.method == 'GET': goods_id = request.GET.get('goods_id') shop_id = request.GET.get('shop_id') goodsshop = GoodsShop.objects.get(goods=Goods.objects.get(id=goods_id), shop=Shop.objects.get(id=shop_id)) return render_to_response('meizu/modal_change.html', {'request': request, 'goodsshop': goodsshop}) elif request.method == 'POST': user = request.user if not user: return HttpResponse("false") shop_id = request.POST['shop_id'] goods_id = request.POST['goods_id'] action = request.POST.get('action') number = request.POST.get('number') remark = request.POST.get('remark') if number is not None and (not user.is_superuser): return HttpResponse('stop') goodsshop = GoodsShop.objects.get(goods__id=goods_id, shop__id=shop_id) goods = Goods.objects.get(id=goods_id) shop = Shop.objects.get(id=shop_id) if action == 'sub': goodsshop.remain -= 1 if goodsshop.remain < 0: return HttpResponse('chaoguo') goodsshop.save() # recent_record = GoodsRecord.objects.order_by('-date')[0] # if recent_record.shop.id == int(shop_id) and recent_record.goods.id == int( # goods_id) and recent_record.updater == user: # recent_record.change_num -= 1 # recent_record.save() # # # 如果是0就删除 # if recent_record.change_num == 0: # recent_record.delete() # else: goods_record = GoodsRecord(goods=goods, shop=shop, change_num=-1, updater=user) sell_record = SellRecord(shop=shop, goods=goods, amount=1, updater=user) sell_record.save() goods_record.sell_record = sell_record goods_record.save() elif action == 'add': goodsshop.remain += 1 goodsshop.save() # recent_record = GoodsRecord.objects.order_by('-date')[0] # if recent_record.shop.id == int(shop_id) and recent_record.goods.id == int( # goods_id) and recent_record.updater == user: # recent_record.change_num += 1 # recent_record.save() # # # 如果是0就删除 # if recent_record.change_num == 0: # recent_record.delete() # else: goods_record = GoodsRecord(goods=goods, shop=shop, change_num=1, updater=user) goods_record.save() elif number: goodsshop.remain += int(number) if goodsshop.remain < 0: return HttpResponse('chaoguo') goodsshop.save() # recent_record = GoodsRecord.objects.order_by('-date')[0] # if recent_record.shop.id == int(shop_id) and recent_record.goods.id == int( # goods_id) and recent_record.updater == user: # recent_record.change_num += int(number) # recent_record.save() # # # 如果是0就删除 # if recent_record.change_num == 0: # recent_record.delete() # else: if number < 0: sell_record = SellRecord(shop=shop, goods=goods, amount=number, updater=user) sell_record.save() goods_record = GoodsRecord(goods=goods, shop=shop, change_num=number, updater=user, remark=remark) goods_record.sell_record = sell_record goods_record.save() else: goods_record = GoodsRecord(goods=goods, shop=shop, change_num=number, updater=user, remark=remark) goods_record.save() goods.recent_sell = datetime.date.today() goods.save() return HttpResponse(goodsshop.remain)
def api_remain(request): # 进出库 if request.method == 'GET': goods_id = request.GET.get('goods_id') shop_id = request.GET.get('shop_id') goodsshop = GoodsShop.objects.get(goods=Goods.objects.get(id=goods_id), shop=Shop.objects.get(id=shop_id)) return render_to_response('modal_change.html', { 'request': request, 'goodsshop': goodsshop }) elif request.method == 'POST': user = request.user if not user: return HttpResponse("false") shop_id = request.POST['shop_id'] goods_id = request.POST['goods_id'] action = request.POST.get('action') number = request.POST.get('number') remark = request.POST.get('remark') if number is not None and (not user.is_superuser): return HttpResponse('stop') goodsshop = GoodsShop.objects.get(goods__id=goods_id, shop__id=shop_id) goods = Goods.objects.get(id=goods_id) shop = Shop.objects.get(id=shop_id) if action == 'sub': goodsshop.remain -= 1 if goodsshop.remain < 0: return HttpResponse('chaoguo') goodsshop.save() # recent_record = GoodsRecord.objects.order_by('-date')[0] # if recent_record.shop.id == int(shop_id) and recent_record.goods.id == int( # goods_id) and recent_record.updater == user: # recent_record.change_num -= 1 # recent_record.save() # # # 如果是0就删除 # if recent_record.change_num == 0: # recent_record.delete() # else: goods_record = GoodsRecord(goods=goods, shop=shop, change_num=-1, updater=user) sell_record = SellRecord(shop=shop, goods=goods, amount=1, updater=user) sell_record.save() goods_record.sell_record = sell_record goods_record.save() elif action == 'add': goodsshop.remain += 1 goodsshop.save() # recent_record = GoodsRecord.objects.order_by('-date')[0] # if recent_record.shop.id == int(shop_id) and recent_record.goods.id == int( # goods_id) and recent_record.updater == user: # recent_record.change_num += 1 # recent_record.save() # # # 如果是0就删除 # if recent_record.change_num == 0: # recent_record.delete() # else: goods_record = GoodsRecord(goods=goods, shop=shop, change_num=1, updater=user) goods_record.save() elif number: goodsshop.remain += int(number) if goodsshop.remain < 0: return HttpResponse('chaoguo') goodsshop.save() recent_record = GoodsRecord.objects.order_by('-date')[0] # if recent_record.shop.id == int(shop_id) and recent_record.goods.id == int( # goods_id) and recent_record.updater == user: # recent_record.change_num += int(number) # recent_record.save() # # # 如果是0就删除 # if recent_record.change_num == 0: # recent_record.delete() # else: if number < 0: sell_record = SellRecord(shop=shop, goods=goods, amount=number, updater=user) sell_record.save() goods_record = GoodsRecord(goods=goods, shop=shop, change_num=number, updater=user, remark=remark) goods_record.sell_record = sell_record goods_record.save() else: goods_record = GoodsRecord(goods=goods, shop=shop, change_num=number, updater=user, remark=remark) goods_record.save() goods.recent_sell = datetime.date.today() goods.save() return HttpResponse(goodsshop.remain)