public int Upload(string fileName, out string message) { message = string.Empty; if (String.IsNullOrEmpty(fileName)) { return(-2); } var fi = new FileInfo(fileName); using (var package = new ExcelPackage(fi)) { var listOfRequests = new List <Request>(); var workbook = package.Workbook; foreach (var worksheet in workbook.Worksheets) { RequestHeader headerObj; IList <Request> readData = ReadRequestRecords(worksheet, out headerObj); //Read Request Records form Excels file listOfRequests.AddRange(readData); _requestHeaderRepository.Add(headerObj); _requestRepository.AddRange(listOfRequests); } try { _requestHeaderRepository.Save(); _requestRepository.Save(); return(1); } catch (Exception e) { return(0); } } }
public void CreateApplication(IList <RequestViewModel> requestViewModels, string poNumber, string temp_ApplicationNumber) { foreach (var requestViewModel in requestViewModels.Where(x => x.ToApplyTotal > 0)) { var currentRequestHeaderNumber = requestViewModel.RequestNumber; var purchaseApplicationHeader = _purchaseApplicationHeaderRepository.FindBy(x => x.RequestNumber == currentRequestHeaderNumber).FirstOrDefault(); if (purchaseApplicationHeader == null) //创建一个新的采购申请Header { var requestHeader = _requestHeaderRepository.FindBy(x => x.RequestHeaderNumber == currentRequestHeaderNumber).FirstOrDefault(); //TO DO mapping var newPurchaseApplicationHeader = _mapper.Map <PurchaseApplicationHeader>(requestHeader); //Create a new purchase application header. var prefix = newPurchaseApplicationHeader.RequestCategory == RequestCategoriesEnum.采购退货 ? "THSQ" : "SQ"; var lastSerialNumber = _purchaseApplicationHeaderRepository.GetLatestSerialNumber(DateTime.Now); newPurchaseApplicationHeader.SerialNo = ++lastSerialNumber; newPurchaseApplicationHeader.PurchaseApplicationNumber = string.IsNullOrEmpty(temp_ApplicationNumber) ? ServiceHelper.GenerateCodeNumber(prefix, newPurchaseApplicationHeader.SerialNo) : temp_ApplicationNumber; _purchaseApplicationHeaderRepository.Add(newPurchaseApplicationHeader); try { _purchaseApplicationHeaderRepository.Save(); purchaseApplicationHeader = newPurchaseApplicationHeader; //Update PO Number of contract in RequestHeader. if (!string.IsNullOrEmpty(poNumber)) { var poNumberFromDb = _managementService.GetPoNumberIfNotExisting(poNumber); if (requestHeader != null) { requestHeader.Contract.PoId = poNumberFromDb; requestHeader.UpdateDate = DateTime.Now; } _requestHeaderRepository.Save(); } } catch (Exception e) { // ignored return; } } var newPurchaseApplication = _mapper.Map <PurchaseApplication>(requestViewModel); newPurchaseApplication.CurrentPurchasePrice = _managementService.GetItemById(requestViewModel.ItemId).Price; if (purchaseApplicationHeader != null) { newPurchaseApplication.PurchaseApplicationNumber = purchaseApplicationHeader.PurchaseApplicationNumber; newPurchaseApplication.ProcessStatus = ProcessStatusEnum.申请审核中; if (purchaseApplicationHeader.RequestCategory == RequestCategoriesEnum.采购退货) { var latestpurchase = _purchaseRepository .FindBy(x => x.PurchaseApplication.ItemId == requestViewModel.ItemId) .OrderByDescending(x => x.CreateDate).FirstOrDefault(); if (latestpurchase != null) { newPurchaseApplication.SupplierId = latestpurchase.PurchaseApplication.SupplierId; newPurchaseApplication.CurrentPurchasePrice = latestpurchase.CurrentPurchasePrice; } } } _purchaseApplicationRepository.Add(newPurchaseApplication); _requestService.UpdateRequestProcessStatus(requestViewModel.RequestId, ProcessStatusEnum.申请审核中); } _purchaseApplicationRepository.Save(); }