/// <summary> /// 修改配件 /// </summary> /// <param name="model"></param> public override void UpdateModel(Product model) { using (var context = DbContext.Open()) { var temp = context.Single <Product>(model.Id); var stockDiff = model.Stock - temp.Stock; if (stockDiff != 0) { // 修改配件库存,增加出入库记录 var record = new ProductStockRecord { Id = Guid.NewGuid(), ProductId = model.Id, Quantity = Math.Abs(stockDiff), QuantityBefore = temp.Stock, RelationId = model.Id, Type = stockDiff > 0 ? EnumStockRecordType.ModifyStockIn : EnumStockRecordType.ModifyStockOut, RecordOn = DateTime.Now, CreatedOn = DateTime.Now, Remark = "修改配件", }; context.Insert(record); } context.Update(model); } }
/// <summary> /// 设置已完工 /// </summary> /// <param name="model"></param> public void Complete(RepairOrder model) { if (model == null || model.Id == Guid.Empty) { return; } using (var context = DbContext.Open()) { using (var tran = context.BeginTransaction()) { // 修改维修单状态 model.Status = EnumRepairOrderStatus.Completed; model.CompleteOn = DateTime.Now; context.Update(model); // 扣配件库存,并增加出库记录 foreach (var item in model.ProductList) { var product = context.SingleById <Product>(item.ProductId); if (product == null) { throw new Exception(string.Format("获取配件信息失败,配件ID:{0}", item.ProductId)); } // 插入配件出库记录 var record = new ProductStockRecord { Id = Guid.NewGuid(), RelationId = model.Id, ProductId = item.ProductId, Quantity = item.Quantity, Type = EnumStockRecordType.Repair, QuantityBefore = product.Stock, CreatedOn = DateTime.Now, RecordOn = model.CompleteOn, Remark = string.Format("维修单:{0} 完工", model.Code), }; context.Insert(record); // 扣该配件的库存 product.Stock = product.Stock - item.Quantity; context.Update(product); } tran.Commit(); } } }
/// <summary> /// 设置已到货 /// </summary> /// <param name="model"></param> public void SetArrival(PurchaseOrder model) { using (var context = DbContext.Open()) { using (var tran = context.BeginTransaction()) { // 修改采购单状态 model.Status = EnumPurchaseOrderStatus.Completed; model.ArrivalOn = DateTime.Now; context.Update(model); // 库存入库 foreach (var item in model.ProductList) { var product = ProductRepository.Get(item.ProductId); if (product == null) { throw new Exception(string.Format("获取配件信息失败,配件ID:{0}", item.ProductId)); } // 插入配件出库记录 var record = new ProductStockRecord { Id = Guid.NewGuid(), RelationId = model.Id, ProductId = item.ProductId, Quantity = item.Quantity, Type = EnumStockRecordType.Purchase, QuantityBefore = product.Stock, CreatedOn = DateTime.Now, RecordOn = model.ArrivalOn, Remark = string.Format("采购单:{0} 到货", model.Code), }; context.Insert(record); // 加该配件的库存 product.Stock = product.Stock + item.Quantity; context.Update(product); } tran.Commit(); } } }
/// <summary> /// 销售单出库 /// </summary> /// <param name="model"></param> public void Outbound(SaleOrder model) { using (var context = DbContext.Open()) { using (var tran = context.BeginTransaction()) { // 修改状态为已出库 model.Status = EnumSaleOrderStatus.Outbound; model.OutboundOn = DateTime.Now; context.Update(model); // 扣产品库存 foreach (var item in model.ProductList) { var product = context.SingleById <Product>(item.ProductId); if (product == null) { throw new Exception(string.Format("获取配件信息失败,配件ID:{0}", item.ProductId)); } // 插入配件出库记录 var record = new ProductStockRecord { Id = Guid.NewGuid(), RelationId = model.Id, ProductId = item.ProductId, Quantity = item.Quantity, Type = EnumStockRecordType.Sale, QuantityBefore = product.Stock, CreatedOn = DateTime.Now, RecordOn = model.OutboundOn, Remark = string.Format("销售单:{0} 出库", model.Code), }; context.Insert(record); // 扣该配件的库存 product.Stock = product.Stock - item.Quantity; context.Update(product); } tran.Commit(); } } }
/// <summary> /// 取消采购单 /// </summary> /// <param name="model"></param> /// <param name="order"></param> public void Cancel(PurchaseOrder model, FinanceOrder order) { using (var context = DbContext.Open()) { using (var tran = context.BeginTransaction()) { #region 调整库存 foreach (var item in model.ProductList) { var product = item.Product; if (product == null) { continue; } if (model.Status == EnumPurchaseOrderStatus.Completed) { // 如果是已到货的采购单,需要回滚配件库存 // 插入配件出入库记录 var record = new ProductStockRecord { Id = Guid.NewGuid(), CreatedOn = DateTime.Now, ProductId = product.Id, Quantity = item.Quantity, QuantityBefore = product.Stock, RecordOn = DateTime.Now, RelationId = model.Id, Type = EnumStockRecordType.CancelPurchase, Remark = string.Format("取消采购单:{0}", model.Code), }; context.Insert(record); // 减去配件库存 product.Stock = product.Stock - item.Quantity; context.Update(product); } } #endregion #region 增加财务单 if (model.SettlementAmount > 0) { // 如果已经结算过,增加取消采购单的财务单 order.Id = Guid.NewGuid(); order.Amount = model.SettlementAmount; order.Type = EnumFinanceOrderType.CancelPurchase; order.CreatedOn = DateTime.Now; order.RelationId = model.Id; order.Remark = order.Remark ?? string.Format("取消采购单:{0}", model.Code); context.Insert(order); } #endregion // 删除采购单配件 context.Delete <PurchaseOrderProduct>(rop => rop.PurchaseOrderId == model.Id); // 删除采购单 context.Delete <PurchaseOrder>(ro => ro.Id == model.Id); tran.Commit(); } } }