def post(self, request): try: json_data = request.body data = json.loads(json_data) except Exception: return JsonError("解析失败") record = data.get('list') if not record: return JsonError("无效数据") batch_num = batch_number() user = request.user # 权限判断 if not user or not user.shop: return JsonError("没有入库权限") for item in record: form = CreateStockRecordForm(item) if form.is_valid(): stock_record = form.save(commit=False) stock_record.batch_number = batch_num stock_record.operator = user.username stock_record.shop_id = user.shop.id stock_record.good_id = item.get('good_id') stock_record.save() else: print(form.errors) model.ExamineStockRecord.objects.create( batch_number=batch_num, total_price=data.get('total_price'), shop_id=user.shop.id, stock_genre=data.get('stock_genre') or 1, operator=user.username) notice_manager("sys_message", "你有审批任务啦") return JsonSuccess("入库提交成功")
def post(self, request): user = request.user if not user.shop: return JsonError("无对应门店数据") self.shop_id = user.shop.id goods_data = self.get_arg('goods_data') order_data = self.get_arg('order_data') serial_number = 'XY' + batch_number() buy_price_total = 0 for data in goods_data: stock_instance = GoodStock.objects.filter( good_id=data['good_id'], shop_id=self.shop_id).first() good_instance = Good.objects.filter(id=data['good_id']).first() if not good_instance: return JsonError("无效数据") if not stock_instance: stock_instance = self.pre_sale(data, good_instance) stock_instance.number = stock_instance.number - data[ 'number'] * data['package_number'] stock_instance.save() buy_price_total += stock_instance.stock_buy_price * data['number'] form = GoodOperateRecordForm(data) one_profit = data['sale_price'] - stock_instance.stock_buy_price profit = one_profit * data['number'] if form.is_valid(): instance = form.save(commit=False) instance.serial_number = serial_number instance.good_id = data['good_id'] instance.quantify = good_instance.quantify or '-' instance.stock_buy_price = stock_instance.stock_buy_price instance.profit = profit instance.discount_profit = round( one_profit / data['sale_price'] * 100, 2) instance.operator = user instance.shop = user.shop instance.save() print(order_data) discount_profit = round((order_data['discMoney'] - buy_price_total) / order_data['discMoney'] * 100, 2) GoodOrder.objects.create(serial_number=serial_number, number=order_data['totalNumber'], buy_price_total=round(buy_price_total, 2), pre_sale_price=order_data['sumMoney'], discount_price=order_data['discMoney'], discount=order_data['discount'], profit=round( order_data['discMoney'] - buy_price_total, 2), discount_profit=discount_profit, operator=user, shop=user.shop) return JsonSuccess("结算成功")