def create(self, user_id, sku_id, warehouse_id, quantity): # 查找对应库存是否存在, inventory = None try: inventory = SkuWarehouse.objects.\ get(sku_id=sku_id, warehouse_id=warehouse_id, is_deleted=False) inventory.quantity = inventory.quantity + quantity inventory.available_quantity =\ inventory.available_quantity + quantity except SkuWarehouse.DoesNotExist: # 获取仓库信息 warehouse = get_warehouse_by_id(warehouse_id, user_id) inventory = SkuWarehouse(sku_id=sku_id, warehouse_id=warehouse_id, user_id=user_id, quantity=quantity, available_quantity=quantity, warehouse_name=warehouse.name, warehosue_province=warehouse.province, warehouse_city=warehouse.city, warehouse_area=warehouse.area, warehouse_detail=warehouse.detail, warehouse_longitude=warehouse.longitude, warehouse_latitude=warehouse.latitude) inventory.save() return inventory
def create(self, user_id, token, data, user_name=None): s = ';' user_info = get_user_info(user_id) express_number = s.join(data.get('express_number', [])) stock_in = StockIn(stock_in_type=int(data.get('stock_in_type', 1)), warehouse_id=data['warehouse_id'], express=data.get('express'), express_number=express_number, warehouse_code=data.get('warehouse_code'), warehouse_name=data.get('warehouse_name'), wms_app_key=data.get('wms_app_key'), user_note=data.get('user_note'), estimated_to_arrival=datetime.fromtimestamp( int(data.get('estimated_to_arrival'))), user_id=user_id, user_name=user_name, user_mobile=user_info['phone'], id=IdGenerationService.generate_stock_in_id()) # 获取仓库信息,修改入库单的发货和收货信息 # 可能存在不能获取到信息的情况 warehouse = get_warehouse_by_id(data['warehouse_id'], token) stock_in.sender_phone = data.get('sender_phone', '') stock_in.send_name = data.get('sender_name', '') stock_in.recipient_phone = warehouse['warehouse_recipient_contact'] stock_in.recipient_name = warehouse['warehouse_recipient_name'] # 如果是退货入库,必须关联退货订单 if int(data['stock_in_type']) == 2: try: order = Order.objects.get(id=data['order_id']) stock_in.order_code = order.order_code except AttributeError: raise CustomException('10003', 'order_id not exist') except ObjectDoesNotExist: raise CustomException('10009') if not data.get('skues'): raise CustomException('10020', '请填写商品信息') create_stock_in_details = [] for sku_info in data['skues']: sku = Sku.objects.get(id=sku_info['sku_id']) stock_in_detail = StockInDetail(quantity=sku_info['quantity'], sku_id=sku_info['sku_id'], stock_in_id=stock_in.id, sku_name=sku.sku_name, sku_spec=sku.specification, bar_code=sku.bar_code, item_code=sku.item_code, user_id=user_id) create_stock_in_details.append(stock_in_detail) stock_in.save() StockInDetail.objects.bulk_create(create_stock_in_details) stock_in.stock_in_details = create_stock_in_details # data['warehouse_id'] # EntryOrder(custom_id='xiaobanma').\ # sync_to_cop(user_id, stock_in) EntryOrder(custom_id=data['warehouse_id']).\ sync_to_cop(user_id, stock_in) return stock_in
def get_warehouse(self, warehouse_id, token): # TODO 获取仓库信息 warehouse = get_warehouse_by_id(warehouse_id=warehouse_id, token=token) destination = self.order.consignee_province +\ self.order.consignee_city + self.order.consignee_area +\ self.order.consignee_detail source = warehouse['warehouse_province'] + warehouse['warehouse_city'] +\ warehouse['warehouse_area'] + warehouse['warehouse_detail'] distance = map_utils.get_distance(source, destination) warehouse['distance'] = distance return warehouse
def set_warehouse_info(cls, order, token=None): warehouse_id = order.warehouse_id warehouse = get_warehouse_by_id(warehouse_id, token) order.warehouse_name = warehouse['warehouse_name'] order.warehouse_code = warehouse['warehouse_code'] order.warehouse_province = warehouse['warehouse_province'] order.warehouse_city = warehouse['warehouse_city'] order.warehouse_area = warehouse['warehouse_area'] order.warehouse_detail = warehouse['warehouse_detail'] order.warehouse_recipient_name = warehouse['warehouse_recipient_name'] order.warehouse_recipient_contact = \ warehouse['warehouse_recipient_contact'] express = cls.get_recommand_express(warehouse['express']) order.logistics_name = express['express_name'] order.logistics_code = express['express_code'] order.save() return order
def warehouses(request, warehouse_id): token = request.user['token'] result = get_warehouse_by_id(warehouse_id=warehouse_id, token=token) return JsonResponse(data=result)
def order_billing(self, order, order_expresses): ''' 订单计费 - 产生快递费用 - 产生订单处理费用 - 生成用仓用户账单 - 扣减余额 - 生成共享仓用户收入 - 增加余额 ''' # 获取仓库信息(携带快递价格表) warehouse = get_warehouse_by_id(order.warehouse_id) express_amount = 0 express_sheet_amount = 0 express_fees = [] sub_user = order.user_id # 分仓用户id share_user = warehouse['owner_id'] # 共享仓用户id service = None # 订单处理服务价格表 # province = order.consignee_province for order_express in order_expresses: express = None try: express = [ e for e in warehouse['express'] if e['express_code'] == order_express.logistics_code and order.consignee_province in e['province'] ][0] except Exception as e: raise CustomException(60004, '没有找到对应的快递') # 快递费用 express_fee = self.fee_ser.\ create_express_fee(order, express, order_express) express_amount += express_fee.amount express_sheet_amount += express_fee.sheet_amount express_fees.append(express_fee) # 订单处理费用 try: service = [s for s in warehouse['service'] if s['code'] == 20][0] except Exception as e: print(e) raise CustomException(60005, '没有找到订单处理费用') order_process_fee = self.fee_ser.\ create_order_process_fee(order, service) # 用仓用户账单 order_bill = self.bill_ser.\ create_order_bill(express_amount=express_amount, express_sheet_amount=express_sheet_amount, order_process_amount=order_process_fee.amount, order_process_fee=order_process_fee, user_id=sub_user, order=order) # 在用仓用户余额扣减面单费 sub_balance = self.balance_ser.\ deduct_balance(sub_user, express_sheet_amount) # 记录用仓用户流水 disbursement = self.rd_ser.\ disbursement(share_user, 12, express_sheet_amount) # 共享仓用户收入 order_receipt = self.receipt_ser.\ create_order_receipt(user_id=order.user_id, express_amount=express_amount, order_process_fee=order_process_fee, order=order) # 共享仓用户余额增加一笔订单账单费用 share_balance = self.balance_ser.\ add_balance(user_id=warehouse['owner_id'], amount=order_receipt.amount) # 记录共享仓用户流水 receipt = self.rd_ser.\ receipt(user_id=share_user, statement_type=4, amount=order_bill.amount) # 快递费用设置 账单 和 收入 关联 for fee in express_fees: fee.order_bill = order_bill fee.order_receipt = order_receipt fee.sub_user_id = sub_user fee.share_user_id = share_user fee.save() order_process_fee.sub_user_id = sub_user order_process_fee.share_user_id = share_user order_process_fee.save() logger.info(""" 处理一笔订单发货确认申请:\n 用仓用户{0}:\n 产生一笔账单:{1}\n 流水:{2}\n 余额:{3}\n 共享仓用户{4}:\n 产生一笔收入:{5} 流水:{6} 余额:{7}\n """.format(sub_user, order_bill, disbursement, sub_balance, share_user, order_receipt, receipt, share_balance)) return True
def confirm(self, params, data): try: # if 'entryOrderId' in data['entryOrder']: # stock_in = StockIn.objects.get( # entry_order_id=data['entryOrder']['entryOrderId'], # wms_app_key=params['app_key'], # warehouse_code=data['entryOrder']['warehouseCode'] # ) # else: # stock_in = StockIn.objects.get( # entry_order_code=data['entryOrder']['entryOrderCode'] # ) stock_in = StockIn.objects.get( id=data['entryOrder']['entryOrderCode']) warehouse = get_warehouse_by_id(stock_in.warehouse_id) # TODO 收到重复的outBizCode应该怎么处理 # if stock_in.out_biz_code == data['entryOrder']['outBizCode']: # return { # 'flag': 'success', # 'code': '0', # 'message': '' # } if StockInConfirm.objects.filter( out_biz_code=data['entryOrder']['outBizCode']): return {'flag': 'success', 'code': '0', 'message': ''} stock_in.stock_in_status = data['entryOrder']['status'] # stock_in.out_biz_code = data['entryOrder']['outBizCode'] stock_in.save() except Exception as e: print(e) return { 'flag': 'failure', 'code': '10010', 'message': 'no this entryorder info' } # TODO 多行处理 # for order_line in data['orderLines']: if isinstance(data['orderLines']['orderLine'], list): order_lines = data['orderLines']['orderLine'] else: order_lines = [data['orderLines']['orderLine']] for order_line in order_lines: # TODO need to store in db(new table?) to trace and show? try: # if 'itemId' in order_line: # stock_in_detail = StockInDetail.objects.get( # stock_in=stock_in, # item_id=order_line['itemId'] # ) # else: # stock_in_detail = StockInDetail.objects.get( # stock_in=stock_in, # item_code=order_line['itemCode'] # ) stock_in_detail = StockInDetail.objects.get( stock_in=stock_in, item_code=order_line['itemCode']) stock_in_detail.sku.quantity += int(order_line['actualQty']) stock_in_detail.sku.available_quantity += int( order_line['actualQty']) stock_in_detail.sku.save() except Exception as e: print(e) return { 'flag': 'failure', 'code': '10011', 'message': 'no this sku info' } try: sku_warehouse = SkuWarehouse.objects.get( sku=stock_in_detail.sku, warehouse_id=stock_in.warehouse_id) sku_warehouse.quantity += int(order_line['actualQty']) sku_warehouse.available_quantity += int( order_line['actualQty']) sku_warehouse.save() except Exception as e: print(e) sku_warehouse = SkuWarehouse( sku=stock_in_detail.sku, warehouse_id=stock_in.warehouse_id, quantity=int(order_line['actualQty']), available_quantity=int(order_line['actualQty']), user_id=stock_in.user_id, warehouse_name=warehouse['warehouse_name'], warehouse_province=warehouse['warehouse_province'], warehouse_city=warehouse['warehouse_city'], warehouse_area=warehouse['warehouse_area'], warehouse_detail=warehouse['warehouse_detail'], warehouse_longitude=warehouse['warehouse_longitude'], warehouse_latitude=warehouse['warehouse_latitude']) sku_warehouse.save() stock_in_confirm = StockInConfirm( plan_quantity=order_line['planQty'], actual_quantity=order_line['actualQty'], inventory_type=order_line.get('inventoryType', 'ZP'), # sku_name=models.CharField(max_length=128, verbose_name='商品名称'), # sku_spec=models.CharField(max_length=128, verbose_name='商品规格'), item_code=order_line['itemCode'], item_id=order_line.get('itemId', ''), out_biz_code=data['entryOrder']['outBizCode'], # bar_code=, sku=stock_in_detail.sku, user_id=stock_in_detail.sku.user_id, stock_in=stock_in, ) stock_in_confirm.save() return {'flag': 'success', 'code': '0', 'message': ''}