public void RemoveSpecialSparepart(int specialSparepartDetailId, int userId) { DateTime serverTime = DateTime.Now; SpecialSparepartDetail sspdEntity = _specialSparepartDetailRepository.GetById(specialSparepartDetailId); sspdEntity.ModifyDate = serverTime; sspdEntity.ModifyUserId = userId; sspdEntity.Status = (int)DbConstant.WheelDetailStatus.Deleted; _specialSparepartDetailRepository.AttachNavigation(sspdEntity.Sparepart); _specialSparepartDetailRepository.AttachNavigation(sspdEntity.CreateUser); _specialSparepartDetailRepository.AttachNavigation(sspdEntity.ModifyUser); _specialSparepartDetailRepository.Update(sspdEntity); _unitOfWork.SaveChanges(); _unitOfWork.SaveChanges(); Sparepart spEntity = _sparepartRepository.GetById(sspdEntity.SparepartId); spEntity.ModifyDate = serverTime; spEntity.ModifyUserId = userId; if (spEntity.StockQty > 0) { spEntity.StockQty = spEntity.StockQty - 1; } _sparepartRepository.AttachNavigation(spEntity.UnitReference); _sparepartRepository.AttachNavigation(spEntity.CategoryReference); _sparepartRepository.AttachNavigation(spEntity.CreateUser); _sparepartRepository.AttachNavigation(spEntity.ModifyUser); _sparepartRepository.Update(spEntity); _unitOfWork.SaveChanges(); SparepartStockCard stockCard = new SparepartStockCard(); Reference transactionReferenceTable = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_TRANSTBL_MANUAL).FirstOrDefault(); stockCard.CreateUserId = userId; stockCard.PurchaseDate = serverTime; stockCard.PrimaryKeyValue = sspdEntity.Id; stockCard.ReferenceTableId = transactionReferenceTable.Id; stockCard.SparepartId = spEntity.Id; stockCard.Description = "Remove Special Sparepart Detail"; stockCard.QtyOut = 1; stockCard.QtyOutPrice = Convert.ToDouble(sspdEntity.PurchasingDetail != null ? sspdEntity.PurchasingDetail.Price : sspdEntity.SparepartManualTransaction != null ? sspdEntity.SparepartManualTransaction.Price : 0); SparepartStockCard lastStockCard = _sparepartStockCardRepository.RetrieveLastCard(spEntity.Id); double lastStock = 0; double lastStockPrice = 0; if (lastStockCard != null) { lastStock = lastStockCard.QtyLast; lastStockPrice = lastStockCard.QtyLastPrice; } stockCard.QtyFirst = lastStock; stockCard.QtyFirstPrice = lastStockPrice; stockCard.QtyLast = lastStock - stockCard.QtyOut; stockCard.QtyLastPrice = lastStockPrice - stockCard.QtyOutPrice; _sparepartStockCardRepository.AttachNavigation(stockCard.CreateUser); _sparepartStockCardRepository.AttachNavigation(stockCard.Sparepart); _sparepartStockCardRepository.AttachNavigation(stockCard.ReferenceTable); stockCard = _sparepartStockCardRepository.Add(stockCard); _unitOfWork.SaveChanges(); if (sspdEntity.PurchasingDetail != null) { SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(sspdEntity.PurchasingDetail.Price); stockCardDtail.QtyOut = 1; stockCardDtail.QtyOutPrice = Convert.ToDouble(sspdEntity.PurchasingDetail.Price); SparepartStockCardDetail lastStockCardDetail = _sparepartStockCardDetailRepository.RetrieveLastCardDetailByPurchasingId(sspdEntity.Sparepart.Id, sspdEntity.PurchasingDetail.PurchasingId); double lastStockDetail = 0; double lastStockDetailPrice = 0; if (lastStockCardDetail != null) { lastStockDetail = lastStockCardDetail.QtyLast; lastStockDetailPrice = lastStockCardDetail.QtyLastPrice; } stockCardDtail.QtyFirst = lastStockDetail; stockCardDtail.QtyFirstPrice = lastStockDetailPrice; stockCardDtail.QtyLast = lastStockDetail - stockCardDtail.QtyOut; stockCardDtail.QtyLastPrice = lastStockDetailPrice - stockCardDtail.QtyOutPrice; stockCardDtail.PurchasingId = sspdEntity.PurchasingDetail.PurchasingId; _sparepartStockCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStockCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); PurchasingDetail pDetail = _purchasingDetailRepository.GetById(sspdEntity.PurchasingDetailId); pDetail.ModifyDate = serverTime; pDetail.ModifyUserId = userId; pDetail.QtyRemaining = pDetail.QtyRemaining - 1; _purchasingDetailRepository.AttachNavigation(pDetail.Purchasing); _purchasingDetailRepository.AttachNavigation(pDetail.Sparepart); _purchasingDetailRepository.AttachNavigation(pDetail.CreateUser); _purchasingDetailRepository.AttachNavigation(pDetail.ModifyUser); _purchasingDetailRepository.Update(pDetail); _unitOfWork.SaveChanges(); } if (sspdEntity.SparepartManualTransaction != null) { SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(sspdEntity.SparepartManualTransaction.Price); stockCardDtail.QtyOut = 1; stockCardDtail.QtyOutPrice = Convert.ToDouble(1 * sspdEntity.SparepartManualTransaction.Price); SparepartStockCardDetail lastStockCardDetail = _sparepartStockCardDetailRepository.RetrieveLastCardDetailByTransactionManualId(sspdEntity.Sparepart.Id, sspdEntity.SparepartManualTransactionId.Value); double lastStockDetail = 0; double lastStockDetailPrice = 0; if (lastStockCardDetail != null) { lastStockDetail = lastStockCardDetail.QtyLast; lastStockDetailPrice = lastStockCardDetail.QtyLastPrice; } stockCardDtail.QtyFirst = lastStockDetail; stockCardDtail.QtyFirstPrice = lastStockDetailPrice; stockCardDtail.QtyLast = lastStockDetail - stockCardDtail.QtyOut; stockCardDtail.QtyLastPrice = lastStockDetailPrice - stockCardDtail.QtyOutPrice; stockCardDtail.SparepartManualTransactionId = sspdEntity.SparepartManualTransactionId; _sparepartStockCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStockCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); SparepartManualTransaction spManual = _sparepartManualTransactionRepository.GetById(sspdEntity.SparepartManualTransactionId); spManual.ModifyDate = serverTime; spManual.ModifyUserId = userId; spManual.QtyRemaining = spManual.QtyRemaining - 1; _sparepartManualTransactionRepository.AttachNavigation(spManual.UpdateType); _sparepartManualTransactionRepository.AttachNavigation(spManual.Sparepart); _sparepartManualTransactionRepository.AttachNavigation(spManual.CreateUser); _sparepartManualTransactionRepository.AttachNavigation(spManual.ModifyUser); _sparepartManualTransactionRepository.Update(spManual); _unitOfWork.SaveChanges(); } _unitOfWork.SaveChanges(); }
public void DeleteVehicle(VehicleViewModel vehicle, int userId) { using (var trans = _unitOfWork.BeginTransaction()) { try { DateTime serverTime = DateTime.Now; vehicle.ModifyDate = serverTime; vehicle.ModifyUserId = userId; vehicle.Status = (int)DbConstant.DefaultDataStatus.Deleted; Vehicle entity = _vehicleRepository.GetById(vehicle.Id); Map(vehicle, entity); _vehicleRepository.AttachNavigation(entity.Brand); _vehicleRepository.AttachNavigation(entity.Type); _vehicleRepository.AttachNavigation(entity.VehicleGroup); _vehicleRepository.AttachNavigation(entity.Customer); _vehicleRepository.AttachNavigation(entity.CreateUser); _vehicleRepository.AttachNavigation(entity.ModifyUser); _vehicleRepository.Update(entity); _unitOfWork.SaveChanges(); foreach (var item in _vehicleWheelRepository.GetMany(vw => vw.VehicleId == vehicle.Id && vw.Status == (int)DbConstant.DefaultDataStatus.Active)) { item.ModifyDate = serverTime; item.ModifyUserId = userId; item.Status = (int)DbConstant.DefaultDataStatus.Deleted; _vehicleWheelRepository.AttachNavigation(item.Vehicle); _vehicleWheelRepository.AttachNavigation(item.WheelDetail); _vehicleWheelRepository.AttachNavigation(item.CreateUser); _vehicleWheelRepository.AttachNavigation(item.ModifyUser); _vehicleWheelRepository.Update(item); _unitOfWork.SaveChanges(); SpecialSparepartDetail wdEntity = _specialSparepartDetailRepository.GetById(item.WheelDetailId); wdEntity.ModifyDate = serverTime; wdEntity.ModifyUserId = userId; wdEntity.Status = (int)DbConstant.WheelDetailStatus.Ready; _specialSparepartDetailRepository.AttachNavigation(wdEntity.Sparepart); _specialSparepartDetailRepository.AttachNavigation(wdEntity.PurchasingDetail); _specialSparepartDetailRepository.AttachNavigation(wdEntity.SparepartManualTransaction); _specialSparepartDetailRepository.AttachNavigation(wdEntity.CreateUser); _specialSparepartDetailRepository.AttachNavigation(wdEntity.ModifyUser); _specialSparepartDetailRepository.Update(wdEntity); _unitOfWork.SaveChanges(); Sparepart spEntity = _sparepartRepository.GetById(wdEntity.SparepartId); spEntity.ModifyDate = serverTime; spEntity.ModifyUserId = userId; spEntity.StockQty = spEntity.StockQty + 1; _sparepartRepository.AttachNavigation(spEntity.UnitReference); _sparepartRepository.AttachNavigation(spEntity.CategoryReference); _sparepartRepository.AttachNavigation(spEntity.CreateUser); _sparepartRepository.AttachNavigation(spEntity.ModifyUser); _sparepartRepository.Update(spEntity); _unitOfWork.SaveChanges(); SparepartStockCard stockCard = new SparepartStockCard(); Reference transactionReferenceTable = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_TRANSTBL_VEHICLE).FirstOrDefault(); stockCard.CreateUserId = userId; stockCard.PurchaseDate = serverTime; stockCard.PrimaryKeyValue = vehicle.Id; stockCard.ReferenceTableId = transactionReferenceTable.Id; stockCard.SparepartId = spEntity.Id; stockCard.Description = "Delete Vehicle"; stockCard.QtyIn = 1; stockCard.QtyInPrice = Convert.ToDouble(wdEntity.PurchasingDetail != null ? wdEntity.PurchasingDetail.Price : wdEntity.SparepartManualTransaction != null ? wdEntity.SparepartManualTransaction.Price : 0); SparepartStockCard lastStockCard = _sparepartStokCardRepository.RetrieveLastCard(spEntity.Id); double lastStock = 0; double lastStockPrice = 0; if (lastStockCard != null) { lastStock = lastStockCard.QtyLast; lastStockPrice = lastStockCard.QtyLastPrice; } stockCard.QtyFirst = lastStock; stockCard.QtyFirstPrice = lastStockPrice; stockCard.QtyLast = lastStock + stockCard.QtyIn; stockCard.QtyLastPrice = lastStockPrice + stockCard.QtyInPrice; _sparepartStokCardRepository.AttachNavigation(stockCard.CreateUser); _sparepartStokCardRepository.AttachNavigation(stockCard.Sparepart); _sparepartStokCardRepository.AttachNavigation(stockCard.ReferenceTable); stockCard = _sparepartStokCardRepository.Add(stockCard); _unitOfWork.SaveChanges(); if (wdEntity.PurchasingDetail != null) { SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(wdEntity.PurchasingDetail.Price); stockCardDtail.QtyIn = 1; stockCardDtail.QtyInPrice = Convert.ToDouble(wdEntity.PurchasingDetail.Price); SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByPurchasingId(wdEntity.Sparepart.Id, wdEntity.PurchasingDetail.PurchasingId); double lastStockDetail = 0; double lastStockDetailPrice = 0; if (lastStockCardDetail != null) { lastStockDetail = lastStockCardDetail.QtyLast; lastStockDetailPrice = lastStockCardDetail.QtyLastPrice; } stockCardDtail.QtyFirst = lastStockDetail; stockCardDtail.QtyFirstPrice = lastStockDetailPrice; stockCardDtail.QtyLast = lastStockDetail + stockCardDtail.QtyIn; stockCardDtail.QtyLastPrice = lastStockDetailPrice + stockCardDtail.QtyInPrice; stockCardDtail.PurchasingId = wdEntity.PurchasingDetail.PurchasingId; _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStokCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); PurchasingDetail pDetail = _purchasingDetailRepository.GetById(wdEntity.PurchasingDetailId); pDetail.ModifyDate = serverTime; pDetail.ModifyUserId = userId; pDetail.QtyRemaining = pDetail.QtyRemaining + 1; _purchasingDetailRepository.AttachNavigation(pDetail.Purchasing); _purchasingDetailRepository.AttachNavigation(pDetail.Sparepart); _purchasingDetailRepository.AttachNavigation(pDetail.CreateUser); _purchasingDetailRepository.AttachNavigation(pDetail.ModifyUser); _purchasingDetailRepository.Update(pDetail); _unitOfWork.SaveChanges(); } if (wdEntity.SparepartManualTransaction != null) { SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(wdEntity.SparepartManualTransaction.Price); stockCardDtail.QtyIn = 1; stockCardDtail.QtyInPrice = Convert.ToDouble(1 * wdEntity.SparepartManualTransaction.Price); SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByTransactionManualId(wdEntity.Sparepart.Id, wdEntity.SparepartManualTransactionId.Value); double lastStockDetail = 0; double lastStockDetailPrice = 0; if (lastStockCardDetail != null) { lastStockDetail = lastStockCardDetail.QtyLast; lastStockDetailPrice = lastStockCardDetail.QtyLastPrice; } stockCardDtail.QtyFirst = lastStockDetail; stockCardDtail.QtyFirstPrice = lastStockDetailPrice; stockCardDtail.QtyLast = lastStockDetail + stockCardDtail.QtyIn; stockCardDtail.QtyLastPrice = lastStockDetailPrice + stockCardDtail.QtyInPrice; stockCardDtail.SparepartManualTransactionId = wdEntity.SparepartManualTransactionId; _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStokCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); SparepartManualTransaction spManual = _sparepartManualTransactionRepository.GetById(wdEntity.SparepartManualTransactionId); spManual.ModifyDate = serverTime; spManual.ModifyUserId = userId; spManual.QtyRemaining = spManual.QtyRemaining + 1; _sparepartManualTransactionRepository.AttachNavigation(spManual.UpdateType); _sparepartManualTransactionRepository.AttachNavigation(spManual.Sparepart); _sparepartManualTransactionRepository.AttachNavigation(spManual.CreateUser); _sparepartManualTransactionRepository.AttachNavigation(spManual.ModifyUser); _sparepartManualTransactionRepository.Update(spManual); _unitOfWork.SaveChanges(); } } trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw; } } }
public void RevertVehicleWheel(int vehicleWheelId, int userId) { using (var trans = _unitOfWork.BeginTransaction()) { try { DateTime serverTime = DateTime.Now; VehicleWheel vwEntity = _vehicleWheelRepository.GetById(vehicleWheelId); _vehicleWheelRepository.AttachNavigation(vwEntity.Vehicle); _vehicleWheelRepository.AttachNavigation(vwEntity.WheelDetail); _vehicleWheelRepository.AttachNavigation(vwEntity.CreateUser); _vehicleWheelRepository.AttachNavigation(vwEntity.ModifyUser); _vehicleWheelRepository.Delete(vwEntity); _unitOfWork.SaveChanges(); SpecialSparepartDetail wdEntity = _specialSparepartDetailRepository.GetById(vwEntity.WheelDetailId); wdEntity.ModifyDate = serverTime; wdEntity.ModifyUserId = userId; wdEntity.Status = (int)DbConstant.WheelDetailStatus.Ready; _specialSparepartDetailRepository.AttachNavigation(wdEntity.Sparepart); _specialSparepartDetailRepository.AttachNavigation(wdEntity.CreateUser); _specialSparepartDetailRepository.AttachNavigation(wdEntity.ModifyUser); _specialSparepartDetailRepository.Update(wdEntity); _unitOfWork.SaveChanges(); Sparepart spEntity = _sparepartRepository.GetById(wdEntity.SparepartId); spEntity.ModifyDate = serverTime; spEntity.ModifyUserId = userId; spEntity.StockQty = spEntity.StockQty + 1; _sparepartRepository.AttachNavigation(spEntity.UnitReference); _sparepartRepository.AttachNavigation(spEntity.CategoryReference); _sparepartRepository.AttachNavigation(spEntity.CreateUser); _sparepartRepository.AttachNavigation(spEntity.ModifyUser); _sparepartRepository.Update(spEntity); _unitOfWork.SaveChanges(); SparepartStockCard stockCard = new SparepartStockCard(); Reference transactionReferenceTable = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_TRANSTBL_VEHICLE).FirstOrDefault(); stockCard.CreateUserId = userId; stockCard.PurchaseDate = serverTime; stockCard.PrimaryKeyValue = wdEntity.Id; stockCard.ReferenceTableId = transactionReferenceTable.Id; stockCard.SparepartId = spEntity.Id; stockCard.Description = "Lepas Ban Kendaraan"; stockCard.QtyIn = 1; stockCard.QtyInPrice = Convert.ToDouble(wdEntity.PurchasingDetail != null ? wdEntity.PurchasingDetail.Price : wdEntity.SparepartManualTransaction != null ? wdEntity.SparepartManualTransaction.Price : 0); SparepartStockCard lastStockCard = _sparepartStokCardRepository.RetrieveLastCard(spEntity.Id); double lastStock = 0; double lastStockPrice = 0; if (lastStockCard != null) { lastStock = lastStockCard.QtyLast; lastStockPrice = lastStockCard.QtyLastPrice; } stockCard.QtyFirst = lastStock; stockCard.QtyFirstPrice = lastStockPrice; stockCard.QtyLast = lastStock + stockCard.QtyIn; stockCard.QtyLastPrice = lastStockPrice + stockCard.QtyInPrice; _sparepartStokCardRepository.AttachNavigation(stockCard.CreateUser); _sparepartStokCardRepository.AttachNavigation(stockCard.Sparepart); _sparepartStokCardRepository.AttachNavigation(stockCard.ReferenceTable); stockCard = _sparepartStokCardRepository.Add(stockCard); _unitOfWork.SaveChanges(); if (wdEntity.PurchasingDetail != null) { SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(wdEntity.PurchasingDetail.Price); stockCardDtail.QtyIn = 1; stockCardDtail.QtyInPrice = Convert.ToDouble(wdEntity.PurchasingDetail.Price); SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByPurchasingId(wdEntity.Sparepart.Id, wdEntity.PurchasingDetail.PurchasingId); double lastStockDetail = 0; double lastStockDetailPrice = 0; if (lastStockCardDetail != null) { lastStockDetail = lastStockCardDetail.QtyLast; lastStockDetailPrice = lastStockCardDetail.QtyLastPrice; } stockCardDtail.QtyFirst = lastStockDetail; stockCardDtail.QtyFirstPrice = lastStockDetailPrice; stockCardDtail.QtyLast = lastStockDetail + stockCardDtail.QtyIn; stockCardDtail.QtyLastPrice = lastStockDetailPrice + stockCardDtail.QtyInPrice; stockCardDtail.PurchasingId = wdEntity.PurchasingDetail.PurchasingId; _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStokCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); } if (wdEntity.SparepartManualTransaction != null) { SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(wdEntity.SparepartManualTransaction.Price); stockCardDtail.QtyIn = 1; stockCardDtail.QtyInPrice = Convert.ToDouble(1 * wdEntity.SparepartManualTransaction.Price); SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByTransactionManualId(wdEntity.Sparepart.Id, wdEntity.SparepartManualTransactionId.Value); double lastStockDetail = 0; double lastStockDetailPrice = 0; if (lastStockCardDetail != null) { lastStockDetail = lastStockCardDetail.QtyLast; lastStockDetailPrice = lastStockCardDetail.QtyLastPrice; } stockCardDtail.QtyFirst = lastStockDetail; stockCardDtail.QtyFirstPrice = lastStockDetailPrice; stockCardDtail.QtyLast = lastStockDetail + stockCardDtail.QtyIn; stockCardDtail.QtyLastPrice = lastStockDetailPrice + stockCardDtail.QtyInPrice; stockCardDtail.SparepartManualTransactionId = wdEntity.SparepartManualTransactionId; _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStokCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); } trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw; } } }