public PrintLabelResult PrintLabel(long orderId, long companyId, long?by) { var printLabelResult = new PrintLabelResult() { IsPrintStarted = false }; var syncInfo = new DbSyncInformer(_dbFactory, _log, _time, SyncType.PostagePurchase, "", MarketType.None, String.Empty); using (var db = _dbFactory.GetRWDb()) { var company = db.Companies.GetByIdWithSettingsAsDto(companyId); var companyAddress = new CompanyAddressService(company); var shipmentProviders = _serviceFactory.GetShipmentProviders(_log, _time, _dbFactory, _weightService, company.ShipmentProviderInfoList, _defaultCustomType, _labelDirectory, _reserveDirectory, _templateDirectory); var labelService = new LabelService(shipmentProviders, _log, _time, _dbFactory, _emailService, _pdfMaker, _addressService); CompanyHelper.UpdateBalance(db, company, shipmentProviders, true, _time.GetAppNowTime()); var shippingList = db.OrderShippingInfos.GetOrderInfoWithItems(_weightService, new[] { orderId }.ToList(), SortMode.ByLocation, unmaskReferenceStyle: false, includeSourceItems: false).ToList(); shippingList = shippingList.Where(sh => !sh.OnHold).ToList(); //NOTE: Update from address var dropShipperList = db.DropShippers.GetAllAsDto().ToList(); foreach (var shipping in shippingList) { shipping.ReturnAddress = dropShipperList.FirstOrDefault(ds => ds.Id == shipping.DropShipperId)?.GetAddressDto(); } printLabelResult = labelService.PrintLabels(db, company, companyAddress, syncInfo, null, shippingList, true, null, null, null, _labelDirectory, _isSampleMode, _time.GetAppNowTime(), by); //Apply new balance to Session CompanyHelper.UpdateBalance(db, company, shipmentProviders, false, _time.GetAppNowTime()); if (printLabelResult.IsPrintStarted) { SaveBatchPrintResultToDb(db, _log, null, printLabelResult, shippingList, _time.GetAppNowTime(), by); if (printLabelResult.FailedIds.Any()) { printLabelResult.Messages.Add(new Message(printLabelResult.GetConcatFailedOrdersString(), MessageTypes.Error)); } } } return(printLabelResult); }
public PrintLabelResult PrintBatch(long batchId, long companyId, long?by) { var printLabelResult = new PrintLabelResult() { IsPrintStarted = false }; var syncInfo = new DbSyncInformer(_dbFactory, _log, _time, SyncType.PostagePurchase, "", MarketType.None, JsonConvert.SerializeObject(new { batchId = batchId })); using (var db = _dbFactory.GetRWDb()) { var when = _time.GetAppNowTime(); var company = db.Companies.GetByIdWithSettingsAsDto(companyId); var companyAddress = new CompanyAddressService(company); var labelProviders = _serviceFactory.GetShipmentProviders(_log, _time, _dbFactory, _weightService, company.ShipmentProviderInfoList, _defaultCustomType, _labelDirectory, _reserveDirectory, _templateDirectory); var labelService = new LabelService(labelProviders, _log, _time, _dbFactory, _emailService, _pdfMaker, _addressService); CompanyHelper.UpdateBalance(db, company, labelProviders, true, when); var orderIds = db.OrderBatches.GetOrderIdsForBatch( batchId, OrderStatusEnumEx.AllUnshippedWithShipped //for composing pdf file with all ); var batchDto = db.OrderBatches.GetAsDto(batchId); //Close batch db.OrderBatches.CloseBatch(batchId); SendBeforePrintNotificationMessage(batchDto.Name); var shippingList = db.OrderShippingInfos.GetOrderInfoWithItems(_weightService, orderIds.ToList(), SortMode.ByLocation, unmaskReferenceStyle: false, includeSourceItems: false).ToList(); //NOTE: Update from address var dropShipperList = db.DropShippers.GetAllAsDto().ToList(); foreach (var shipping in shippingList) { shipping.ReturnAddress = dropShipperList.FirstOrDefault(ds => ds.Id == shipping.DropShipperId)?.GetAddressDto(); } //NOTE: update phone if missing foreach (var shipping in shippingList) { if (shipping.ShippingMethod.CarrierName == ShippingServiceUtils.FedexCarrier && String.IsNullOrEmpty(shipping.ToAddress.FinalPhone)) { shipping.ToAddress.Phone = company.Phone; shipping.ToAddress.ManuallyPhone = company.Phone; } } //NOTE: Sort for setting # shippingList = SortHelper.Sort(shippingList, SortMode.ByShippingMethodThenLocation).ToList(); IList <long> removedOrderIds = new List <long>(); if (!syncInfo.IsSyncInProgress()) { UpdateLabelPrintStatus(db, orderIds, shippingList); removedOrderIds = RemoveOrdersWithIssue(db, orderIds, shippingList, printLabelResult); if (removedOrderIds.Any()) { var batchName = string.Format("Issues of {0} {1} orders", batchDto.Name, removedOrderIds.Count); db.OrderBatches.CreateBatch(batchName, BatchType.PrintLabel, removedOrderIds, when, by); //NOTE: keep to final email notification //shippingList = shippingList.Where(sh => !removedOrderIds.Contains(sh.OrderId)).ToList(); } //Removed list var removedList = shippingList.Where(sh => removedOrderIds.Contains(sh.OrderId)).ToList(); if (batchDto.LockDate.HasValue) { var historyRemovedOrderIds = db.OrderChangeHistories.GetAll().Where(ch => ch.FromValue == batchDto.Id.ToString() && ch.FieldName == OrderHistoryHelper.AddToBatchKey && ch.ChangeDate >= batchDto.LockDate) .Select(ch => ch.OrderId) .Distinct() .ToList(); historyRemovedOrderIds = historyRemovedOrderIds.Where(id => !orderIds.Contains(id)).ToList(); var historyShippingList = db.OrderShippingInfos.GetOrderInfoWithItems(_weightService, historyRemovedOrderIds.ToList(), SortMode.ByLocation, unmaskReferenceStyle: false, includeSourceItems: false).ToList(); removedList.AddRange(historyShippingList); } //Changed list var styleChangedList = new List <StyleChangeInfo>(); if (batchDto.LockDate.HasValue) { var historyChangeStyles = db.OrderChangeHistories.GetAll().Where(ch => orderIds.Contains(ch.OrderId) && ch.FieldName == OrderHistoryHelper.ReplaceItemKey && ch.ChangeDate >= batchDto.LockDate) .ToList(); foreach (var change in historyChangeStyles) { if (!String.IsNullOrEmpty(change.FromValue) && !String.IsNullOrEmpty(change.ToValue)) { var fromStyleItemId = StringHelper.ToInt(change.FromValue); var fromStyleItem = db.StyleItems.GetAll().FirstOrDefault(si => si.Id == fromStyleItemId); var toStyleItemId = StringHelper.ToInt(change.ToValue); var toStyleItem = db.StyleItems.GetAll().FirstOrDefault(si => si.Id == toStyleItemId); if (fromStyleItem != null && toStyleItem != null) { var fromStyle = db.Styles.Get(fromStyleItem.StyleId); var toStyle = db.Styles.Get(toStyleItem.StyleId); styleChangedList.Add(new StyleChangeInfo() { SourceStyleString = fromStyle.StyleID, SourceStyleSize = fromStyleItem.Size, DestStyleString = toStyle.StyleID, DestStyleSize = toStyleItem.Size, }); } } } } //Printing printLabelResult = labelService.PrintLabels(db, company, companyAddress, syncInfo, batchId, shippingList, false, removedList, styleChangedList, batchDto.ScanFormPath, _labelDirectory, _isSampleMode, when, by); printLabelResult.RemovedIds = removedOrderIds; long?failedBatchId = null; //Move orders with errors to a new batch if (printLabelResult.FailedIds.Count > 0) { var batchName = string.Format("Failed of {0} {1} orders", batchDto.Name, printLabelResult.FailedIds.Count); var failedOrderIds = printLabelResult.FailedIds.Select(s => s.OrderId).Distinct().ToList(); failedBatchId = db.OrderBatches.CreateBatch(batchName, BatchType.PrintLabel, failedOrderIds, when, by); printLabelResult.Messages.Add( new Message( String.Format("{0} unprinted orders was moved to new batch \"{1}\"", failedOrderIds.Count, batchName), MessageTypes.Error)); } if (printLabelResult.IsPrintStarted) { //Send notification to seller SendAfterPrintNotificationMessage(db, printLabelResult.Messages, batchDto.Id, batchDto.Name, company, shippingList.Where(sh => printLabelResult.RemovedIds.Any(f => f == sh.OrderId)).ToList(), shippingList.Where(sh => printLabelResult.FailedIds.Any(f => f.ShipmentId == sh.Id)).ToList()); //Apply new balance to Session CompanyHelper.UpdateBalance(db, company, labelProviders, false, _time.GetAppNowTime()); SaveBatchPrintResultToDb(db, _log, batchDto.Id, printLabelResult, shippingList, _time.GetAppNowTime(), by); if (printLabelResult.FailedIds.Any()) { printLabelResult.Messages.Add(new Message(printLabelResult.GetConcatFailedOrdersString(), MessageTypes.Error)); } } else { _emailService.SendSystemEmailToAdmin(String.Format("Batch \"{0}\" print wasn't started", batchDto.Name), String.Format("Ended at {0}", _time.GetAppNowTime())); } } else { printLabelResult.Messages.Add(new Message("Request rejected. The system is already buys postage", MessageTypes.Warning)); } return(printLabelResult); } }