public Boolean UpgradeOrderLinesStatus(Dictionary <int, int?> orderLines, OrderLineStatus status, bool useStatusOnNonAssortmentItems = false) { using (var db = GetUnitOfWork()) { try { foreach (var orderLineID in orderLines) { var id = orderLineID; var orderLine = db .Scope .Repository <OrderLine>() .GetAll(x => x.OrderLineID == id.Key) .FirstOrDefault(); if (orderLine == null) { return(false); } orderLine.SetStatus(status, db.Scope.Repository <OrderLedger>(), orderLineID.Value, useStatusOnNonAssortmentItems); } db.Save(); return(true); } catch (Exception) { return(false); } } }
public Boolean UpgradeOrderLineStatus(int orderLineID, OrderLineStatus status, int?quantity = null, bool useStatusOnNonAssortmentItems = false) { using (var db = GetUnitOfWork()) { var orderLine = db .Scope .Repository <OrderLine>() .GetAll(x => x.OrderLineID == orderLineID) .FirstOrDefault(); if (orderLine == null) { return(false); } orderLine.SetStatus(status, db.Scope.Repository <OrderLedger>(), quantity, useStatusOnNonAssortmentItems); return(true); } }
public void SetStatus(OrderLineStatus status, IRepository <OrderLedger> repo, int?quantity = null, bool useStatusOnNonAssortmentItems = false) { OrderLedger ledger = null; if (this.Product.IsNonAssortmentItem.HasValue && this.Product.IsNonAssortmentItem.Value && !this.OrderLedgers.Any(x => x.Status == (int)status)) { int statusNA = (int)OrderLineStatus.ReadyToOrder; if (useStatusOnNonAssortmentItems) { statusNA = (int)status; } ledger = new OrderLedger() { LedgerDate = DateTime.Now, OrderLineID = this.OrderLineID, Status = statusNA, }; } else if (!this.OrderLedgers.Any(x => x.Status == (int)status)) { ledger = new OrderLedger() { LedgerDate = DateTime.Now, OrderLineID = this.OrderLineID, Status = (int)status, }; } if (ledger != null) { if (quantity.HasValue) { ledger.Quantity = quantity.Value; } this.OrderLedgers.Add(ledger); repo.Add(ledger); } }
/// <summary> /// Retrieves a grouped collection of orderlines per order /// </summary> /// <param name="orderLines"></param> /// <returns></returns> protected Dictionary <Concentrator.Objects.Models.Orders.Order, List <OrderLine> > GetGroupedOrders(List <OrderLine> orderLines, OrderLineStatus status) { return((from m in orderLines #if !DEBUG where m.CurrentState() == (int)status #endif group m by m.OrderID into g select new { Order = g.Key, OrderLines = g.ToList() }).ToDictionary(c => orderLines.Select(d => d.Order).Where(d => d.OrderID == c.Order).FirstOrDefault(), c => c.OrderLines)); }
private void WriteOrders(IEnumerable <OrderLine> orderLines, WebOrderProcessType processType, OrderLineStatus newStatus, string ilnClientNumber) { using (var engine = new MultiRecordEngine(typeof(DatColEnvelope), typeof(DatColHeader), typeof(DatColDate), typeof(DatColOrderLine), typeof(DatColCount))) { orderLines.GroupBy(c => c.OrderID).ToList().ForEach(orderLinesCollection => { using (Stream memoryStream = new MemoryStream()) using (var streamWriter = new StreamWriter(memoryStream)) { var saveOrder = true; _magentoOrderNumber = orderLinesCollection.First().Order.WebSiteOrderNumber; engine.BeginWriteStream(streamWriter); var magentoOrderNumber = orderLinesCollection.First().Order.WebSiteOrderNumber; var envelope = new DatColEnvelope { ILNClientNumber = ilnClientNumber, ILNSapphNumber = ILNSapphNumber, MagentoOrderNumber = magentoOrderNumber }; engine.WriteNext(envelope); var header = new DatColHeader { MagentoOrderNumber = magentoOrderNumber, ILNSapphNumber = ILNSapphNumber, ILNClientNumber = ilnClientNumber, ILNClientNumber2 = ilnClientNumber }; engine.WriteNext(header); var date = new DatColDate(); engine.WriteNext(date); var orderLineCounter = 0; foreach (var line in orderLinesCollection) { if (line.Product == null) { saveOrder = false; continue; } var barcode = line.Product.ProductBarcodes.Where(x => x.BarcodeType.HasValue && x.BarcodeType.Value == 0).Select(x => x).FirstOrDefault(); if (barcode == null) { saveOrder = false; continue; } decimal totalUnitPriceToProcess; var quantityToProcess = 0; switch (processType) { case WebOrderProcessType.WebOrder: if (!line.Price.HasValue) { throw new ArgumentNullException(); } var discount = line.LineDiscount.HasValue ? line.LineDiscount.Value : 0; if (line.ProductID == _shipmentCostProductID) { totalUnitPriceToProcess = (decimal)(line.Price); } else { totalUnitPriceToProcess = (decimal)(line.Price.Value - discount); } if (line.OrderLedgers.Any(c => c.Status == (int)OrderLineStatus.ProcessedKasmut)) { var ledg = line.OrderLedgers.FirstOrDefault(c => c.Status == (int)OrderLineStatus.ProcessedKasmut); if (ledg != null) { if (ledg.Quantity != null) { quantityToProcess = line.Quantity - ledg.Quantity.Value; } } } break; case WebOrderProcessType.CancellationWebOrder: if (line.ProductID == _returnCostProductID) { quantityToProcess = line.Quantity; totalUnitPriceToProcess = (decimal)(line.UnitPrice.HasValue ? line.UnitPrice.Value : 0); } else { var returnLedger = line.OrderLedgers.FirstOrDefault(x => x.Status == CancelledStatus); quantityToProcess = -((returnLedger != null && returnLedger.Quantity.HasValue) ? returnLedger.Quantity.Value : line.Quantity); totalUnitPriceToProcess = (decimal)(Math.Abs(quantityToProcess) * line.UnitPrice); } break; case WebOrderProcessType.ReturnedWebOrder: if (line.ProductID == _returnCostProductID) { quantityToProcess = line.Quantity; totalUnitPriceToProcess = (decimal)(line.BasePrice.HasValue ? line.BasePrice.Value : 0); } else { // if product not ReturnCost // Quantity = Returned Quantity (if this not exists) // Shipped Quantity // Price = Paid price per Product (with discount) * Quantity var returnLedger = line.OrderLedgers.FirstOrDefault(x => x.Status == ReturnStatus); quantityToProcess = -((returnLedger != null && returnLedger.Quantity.HasValue) ? returnLedger.Quantity.Value : line.Quantity); totalUnitPriceToProcess = (decimal)(Math.Abs(quantityToProcess) * (line.Price / line.Quantity)); } break; default: throw new NotImplementedException(); } var orderLine = new DatColOrderLine { OrderLineNumber = ++orderLineCounter, Barcode = barcode.Barcode, Quantity = quantityToProcess == 0 ? line.Quantity : quantityToProcess, TotalUnitPrice = totalUnitPriceToProcess }; engine.WriteNext(orderLine); } var count = new DatColCount { TotalOrderLine = orderLinesCollection.Count(), TotalQuantity = 0 }; engine.WriteNext(count); engine.Flush(); streamWriter.Flush(); string webOrderFile; switch (processType) { case WebOrderProcessType.WebOrder: webOrderFile = WebOrderFileName; break; case WebOrderProcessType.CancellationWebOrder: case WebOrderProcessType.ReturnedWebOrder: webOrderFile = ReturnWebOrderFileName; break; default: throw new NotImplementedException(); } if (saveOrder) { var listOfOrderLines = orderLinesCollection.ToDictionary <OrderLine, int, int?>(line => line.OrderLineID, line => null); if (!_orderRepo.UpgradeOrderLinesStatus(listOfOrderLines, newStatus, true)) { _log.Info(string.Format("The system can not upgrade the status of order line {0} to {1}", listOfOrderLines.Keys, newStatus)); } else { var ftpManager = new FtpManager(_ftpSetting.FtpUri, null, false, true); ftpManager.Upload(memoryStream.Reset(), webOrderFile); } } else { //todo: log corrupt lines } } }); } }