public async Task <ApiResponse> Handle(AddItemOrderCommand request, CancellationToken cancellationToken) { ApiResponse response = new ApiResponse(); try { if (request != null) { StorePurchaseOrder obj = _mapper.Map <StorePurchaseOrder>(request); obj.IsDeleted = false; await _dbContext.StorePurchaseOrders.AddAsync(obj); await _dbContext.SaveChangesAsync(); response.StatusCode = StaticResource.successStatusCode; response.Message = "Success"; } else { response.StatusCode = StaticResource.failStatusCode; response.Message = "Model value inappropriate"; } } catch (Exception ex) { response.StatusCode = StaticResource.failStatusCode; response.Message = StaticResource.SomethingWrong + ex.Message; } return(response); }
public void Create(CreateStorePurchaseOrder model) { var entity = new StorePurchaseOrder(); entity = model.MapTo <StorePurchaseOrder>(); entity.AddItems(model.ConvertJsonToItem()); var reason = "创建采购单"; var billIdentity = BillIdentity.StorePurchaseOrder; if (entity.OrderType == OrderType.Refund) { reason = "创建采购退单"; billIdentity = BillIdentity.StorePurchaseRefundOrder; } var entitys = _service.SplitOrderItem(entity); foreach (var order in entitys) { entity.Code = _sequenceService.GenerateNewCode(billIdentity); entity.SetItems(order.Items.ToList()); _db.Insert(entity); var history = new ProcessHistory(model.CreatedBy, model.CreatedByName, (int)entity.Status, entity.Id, billIdentity.ToString(), reason); _db.Command.AddExecute(history.CreateSql(entity.GetType().Name, entity.Code), history); _db.SaveChange(); } }
public void UpdateWithItem(StorePurchaseOrder model) { if (_db.Table.Exists <StorePurchaseOrderItem>(n => n.StorePurchaseOrderId == model.Id)) { _db.Delete <StorePurchaseOrderItem>(n => n.StorePurchaseOrderId == model.Id); } _db.Insert <StorePurchaseOrderItem>(model.Items.ToArray()); _db.Update(model); }
/// <summary> /// 采购入库 /// </summary> /// <param name="entity"></param> public void StockInProducts(StorePurchaseOrder entity) { if (entity == null) { throw new FriendlyException("单据不存在"); } if (entity.Items.Count() == 0) { throw new FriendlyException("单据明细为空"); } //记录库存批次 var productIdArray = entity.Items.Select(n => n.ProductId).ToArray(); var inventorys = _db.Table.FindAll <StoreInventory>("select * from storeinventory where productId in @ProductIds and StoreId=@StoreId", new { ProductIds = productIdArray, StoreId = entity.StoreId }); var inventoryBatchs = new List <StoreInventoryBatch>(); var inventoryHistorys = new List <StoreInventoryHistory>(); var batchNo = _sequenceService.GenerateBatchNo(entity.StoreId); foreach (var item in entity.Items) { // 采购单允许实收为0,这种商品需要跳过,不处理 if (item.ActualQuantity == 0) { continue; } item.BatchNo = batchNo; //更新单据明细批次 var inventory = inventorys.FirstOrDefault(n => n.ProductId == item.ProductId); if (inventory == null) { throw new FriendlyException(string.Format("商品{0}不存在", item.ProductId)); } var inventoryQuantity = inventory.Quantity; inventory.Quantity += item.ActualQuantity; inventory.SaleQuantity += item.ActualQuantity; inventory.AvgCostPrice = CalculatedAveragePrice(inventory.AvgCostPrice, inventoryQuantity, item.Price, item.ActualQuantity); // 修改库存均价 inventory.LastCostPrice = item.Price > 0 ? item.Price : inventory.LastCostPrice; //记录库存流水 var history = new StoreInventoryHistory(item.ProductId, entity.StoreId, inventoryQuantity, item.ActualQuantity, item.Price, item.BatchNo, entity.Id, entity.Code, BillIdentity.StorePurchaseOrder, entity.StoragedBy, entity.SupplierId); inventoryHistorys.Add(history); //记录库存批次 var batchQuantity = CalculatedBatchQuantity(inventoryQuantity, item.ActualQuantity); var batch = new StoreInventoryBatch(item.ProductId, entity.StoreId, entity.SupplierId, batchQuantity, item.ContractPrice, item.Price, item.BatchNo, item.ProductionDate, item.ShelfLife, entity.StoragedBy); inventoryBatchs.Add(batch); } _db.Update(entity.Items.ToArray()); _db.Update(inventorys.ToArray()); _db.Insert(inventoryHistorys.ToArray()); _db.Insert(inventoryBatchs.ToArray()); }
public List <StorePurchaseOrder> SplitOrderItem(StorePurchaseOrder model) { var splitNumber = 10; List <StorePurchaseOrderItem> items = new List <StorePurchaseOrderItem>(); List <StorePurchaseOrder> entitys = new List <StorePurchaseOrder>(); foreach (var item in model.Items) { if (items.Count == splitNumber) { StorePurchaseOrder entity = new StorePurchaseOrder(); entity.SetItems(items); entitys.Add(entity); items = new List <StorePurchaseOrderItem>(); // 重新分配一个 } var product = _db.Table.Find <Product>(item.ProductId); if (model.OrderType == ValueObject.OrderType.Refund) { //检查库存 var inventoryModel = _db.Table.Find <StoreInventory>(n => n.ProductId == item.ProductId && n.StoreId == model.StoreId); if (item.Quantity > inventoryModel.Quantity) { throw new FriendlyException(string.Format("商品{0}退货数量{1} > 库存数{2}", product.Code, item.Quantity, inventoryModel.Quantity)); } } else { if (item.Quantity <= 0) { throw new FriendlyException(string.Format("{0}:数量不能小于等于0", product.Code)); } } var itemProduct = items.FirstOrDefault(n => n.ProductId == item.ProductId); if (itemProduct == null) { items.Add(item); } else { itemProduct.Quantity += item.Quantity; } } if (items.Count > 0 && items.Count <= splitNumber) { StorePurchaseOrder entity = new StorePurchaseOrder(); entity.SetItems(items); entitys.Add(entity); } return(entitys); }
public List <StoreInventoryBatch> SaveBatch(StorePurchaseOrder entity) { var inventoryBatchs = new List <StoreInventoryBatch>(); var batchNo = _sequenceService.GenerateBatchNo(entity.StoreId); foreach (var item in entity.Items) { //批次 item.BatchNo = batchNo; var batch = new StoreInventoryBatch(item.ProductId, entity.StoreId, entity.SupplierId, item.ActualQuantity, item.ContractPrice, item.Price, item.BatchNo, item.ProductionDate, item.ShelfLife, entity.StoragedBy); inventoryBatchs.Add(batch); } return(inventoryBatchs); }
public IEnumerable <StoreInventory> CheckProductNotInInventory(StorePurchaseOrder entity) { if (entity == null) { throw new FriendlyException("单据不存在"); } if (entity.Items.Count() == 0) { throw new FriendlyException("单据明细为空"); } //查询门店库存中不存在商品 string sql = @"select i.ProductId ,o.StoreId from storepurchaseorderitem i inner join storepurchaseorder o on i.StorePurchaseOrderId = o.Id left join (select * from storeinventory si where si.StoreId = @StoreId ) s on i.ProductId = s.ProductId where s.Id is null and i.StorePurchaseOrderId = @StorePurchaseOrderId"; var items = _db.Table.FindAll <StoreInventory>(sql, new { StoreId = entity.StoreId, StorePurchaseOrderId = entity.Id }); return(items); }
/// <summary> /// 采购出库。 采购退单,数量为负 /// </summary> /// <param name="entity"></param> public void StockOutInventory(StorePurchaseOrder entity) { if (entity == null) { throw new FriendlyException("单据不存在"); } if (entity.Items.Count() == 0) { throw new FriendlyException("单据明细为空"); } var entityItems = entity.Items; var productIdArray = entity.Items.Select(n => n.ProductId).ToArray(); var inventorys = _db.Table.FindAll <StoreInventory>("select * from storeinventory where productId in @ProductIds and StoreId=@StoreId", new { ProductIds = productIdArray, StoreId = entity.StoreId }).ToList(); var inventoryBatchs = _db.Table.FindAll <StoreInventoryBatch>("select * from storeinventorybatch where storeId=@StoreId and productId in @ProductIds and Quantity>0", new { StoreId = entity.StoreId, ProductIds = productIdArray }).ToList(); var inventoryHistorys = new List <StoreInventoryHistory>(); var inventoryBatchUpdates = new List <StoreInventoryBatch>(); //批次更新 foreach (var item in entity.Items) { var inventory = inventorys.FirstOrDefault(n => n.ProductId == item.ProductId); if (inventory == null) { throw new FriendlyException(string.Format("商品{0}库存记录不存在", item.ProductId)); } // 先检查总库存是否够扣减 if (inventory.Quantity < item.ActualQuantity) { var product = _db.Table.Find <Product>(inventory.ProductId); throw new FriendlyException(string.Format("{0}库存不足!", product.Code)); } // 扣减总库存 var inventoryQuantity = inventory.Quantity; inventory.Quantity -= item.ActualQuantity; inventory.SaleQuantity -= item.ActualQuantity; //按照先进先出扣减批次库存 var productBatchs = SortBatchByFIFO(inventoryBatchs, item.ProductId, item.BatchNo); var leftQuantity = item.ActualQuantity; // 需要扣减的总数量 foreach (var batchItem in productBatchs) { if (batchItem.Quantity >= leftQuantity) { batchItem.Quantity -= leftQuantity; inventoryBatchUpdates.Add(batchItem); //记录修改历史 inventoryHistorys.Add(new StoreInventoryHistory(inventory.ProductId, entity.StoreId, inventoryQuantity, -leftQuantity, batchItem.Price, batchItem.BatchNo, entity.Id, entity.Code, BillIdentity.StorePurchaseRefundOrder, entity.CreatedBy, entity.SupplierId)); break; } else { inventoryHistorys.Add(new StoreInventoryHistory(inventory.ProductId, entity.StoreId, inventoryQuantity, -batchItem.Quantity, batchItem.Price, batchItem.BatchNo, entity.Id, entity.Code, BillIdentity.StorePurchaseRefundOrder, entity.CreatedBy, entity.SupplierId)); // 剩余扣减数 inventoryQuantity = inventoryQuantity - batchItem.Quantity; // 第1+N次扣减后总库存 leftQuantity = leftQuantity - batchItem.Quantity; batchItem.Quantity = 0; //扣完 inventoryBatchUpdates.Add(batchItem); } } } _db.Update(inventorys.ToArray()); if (inventoryBatchUpdates.Count > 0) { _db.Update(inventoryBatchUpdates.ToArray()); } _db.Insert(inventoryHistorys.ToArray()); }