protected override void Process() { _monitoring.Notify(Name, 0); using (var db = new Database(Environments.Current.Connection, "System.Data.SqlClient")) { foreach (var vendorID in ConfigurationHelper.VendorIdsToDownload) { _monitoring.Notify(Name, vendorID); DateTime lastRun; var runStart = DateTime.Now; if (!DateTime.TryParse(db.SingleOrDefault <string>(string.Format("SELECT Value FROM VendorSetting WHERE VendorID='{0}' AND SettingKey='{1}'", vendorID, VendorSettingKey)), out lastRun)) { log.Info(string.Format("VendorSetting for \"{1}\" does not exist or has invalid data for vendorID {0} , doing a media export from the beginning of time", vendorID, VendorSettingKey)); } // if setting not present if (lastRun == DateTime.MinValue) { lastRun = SqlDateTime.MinValue.Value; // new DateTime(1753, 1, 1, 0, 0, 0); // tsql minimum date value db.Execute(string.Format("INSERT INTO VendorSetting (VendorID, SettingKey, Value) Values ({0}, '{1}', '{2}')", vendorID, VendorSettingKey, runStart.ToString(MessageHelper.ISO8601, CultureInfo.InvariantCulture))); } else { lastRun = lastRun.ToUniversalTime(); } const string query = "SELECT MediaID, BrandID, ProductID, ManufacturerID, ImagePath, LastChanged FROM ImageView WHERE ConnectorID=@0 AND (@1 IS NULL OR LastChanged > @1)"; var images = db.Fetch <ProductMedia>(query, VendorSettingsHelper.GetConnectorIDByVendorID(vendorID), lastRun); var processed = ProcessProductImages(db, images, vendorID); // finished if (processed) { db.Execute(string.Format("UPDATE VendorSetting SET Value='{2}' WHERE VendorID={0} AND SettingKey='{1}'", vendorID, VendorSettingKey, runStart.ToString(MessageHelper.ISO8601, CultureInfo.InvariantCulture))); } } } _monitoring.Notify(Name, 1); }
protected override void Process() { _monitoring.Notify(Name, 0); log.InfoFormat("Start processing Sales Order Import"); log.InfoFormat("Get messages to process"); var salesOrderFiles = MessageHelper.GetMessagesByStatusAndType(WehkampMessageStatus.Created, MessageHelper.WehkampMessageType.SalesOrder); log.InfoFormat("Found {0} messages to process", salesOrderFiles.Count); foreach (var file in salesOrderFiles) { log.Info(string.Format("{0} - Loading file: {1}", Name, file.Filename)); kassaInformatie salesOrder; var result = kassaInformatie.LoadFromFile(Path.Combine(file.Path, file.Filename), out salesOrder); //Set message status MessageHelper.UpdateMessageStatus(file.MessageID, result ? WehkampMessageStatus.InProgress : WehkampMessageStatus.Error); var salesOrdersImported = false; if (result) { var vendorID = file.VendorID; var connectorID = VendorSettingsHelper.GetConnectorIDByVendorID(vendorID); var orderItems = new List <SalesOrderObject>(); #region foreach (var so in salesOrder.kassabon) log.InfoFormat("Found {0} orders to process", salesOrder.kassabon.Count); var counter = 0; foreach (var so in salesOrder.kassabon) { var document = so.Serialize(); var receivedDate = DateTime.ParseExact(string.Format("{0} {1}", so.zendingDatum.ToString("yyyy-MM-dd"), DateTime.Now.ToString("HH:mm:ss")), "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); var salesOrderItem = new SalesOrderObject { SalesOrderID = -1, Document = document, ConnectorID = connectorID, IsDispatched = 1, ReceivedDate = receivedDate.ToUniversalTime(), IsDropShipment = 1, HoldOrder = 0, WebsiteOrderNumber = so.kassabonNummer, OrderType = (int)OrderTypes.SalesOrder, IsSalesOrder = so.klantMutatie.ToLower(CultureInfo.InvariantCulture) == "verkoop" }; foreach (var r in so.kassabonregel) { var quantity = int.Parse(r.verkoopAantal, CultureInfo.InvariantCulture); var productID = ProductHelper.GetProductIDByWehkampData(r.artikelNummer, r.kleurNummer, r.maat, vendorID); if (productID != 0) //ProductHelper returns FirstOrDefault of the productid, which means it will have the value 0 when it can't be found in VendorAssortment. If we don't check for this here it will break when inserting an orderline. { var soLine = new SalesOrderLineObject { ProductID = productID, Quantity = quantity, BasePrice = -1, UnitPrice = r.factuurBedrag + r.kortingBedrag, // + korting bedrag per item Price = (r.factuurBedrag + r.kortingBedrag) * quantity, // + korting bedrag per item LineDiscount = r.kortingBedrag * quantity, //totaal korting bedrag (dus voor alle items totaal) IsDispatched = 1, }; salesOrderItem.SalesOrderLines.Add(soLine); } else { log.ErrorFormat("Unable to find ProductID in VendorAssortment for artikelNummer: {0} with kleurNummer: {1}, maat: {2} and vendorID: {3}. Aborting the processing of this Kassabon.", r.artikelNummer, r.kleurNummer, r.maat, vendorID); MessageHelper.UpdateMessageStatus(file.MessageID, WehkampMessageStatus.Error); return; } } orderItems.Add(salesOrderItem); counter++; if (counter % 25 == 0) { log.Debug(string.Format("Loaded {0} orders from file", counter)); } } log.Debug(string.Format("Loaded {0} orders from file", counter)); #endregion salesOrdersImported = ProcessSalesOrders(orderItems, vendorID); } else { log.Error(string.Format("Error loading file {0}", file.Filename)); MessageHelper.Error(file.MessageID); } if (salesOrdersImported) { MessageHelper.Archive(file.MessageID); } else { MessageHelper.UpdateMessageStatus(file.MessageID, WehkampMessageStatus.Error); } } log.InfoFormat("Finished processing Sales Order Import"); _monitoring.Notify(Name, 1); }
protected override void Process() { _monitoring.Notify(Name, 0); log.InfoFormat("Start processing Stock Return Request Confirmation Import"); log.InfoFormat("Get messages to process"); var files = MessageHelper.GetMessagesByStatusAndType(WehkampMessageStatus.Created, MessageHelper.WehkampMessageType.StockReturnRequestConfirmation); log.InfoFormat("Found {0} messages to process", files.Count); foreach (var file in files) { var wehkampOrderID = -1; try { log.Info(string.Format("{0} - Loading file: {1}", Name, file.Filename)); retourUitslag stockReturnRequestConfirmation; var result = retourUitslag.LoadFromFile(Path.Combine(file.Path, file.Filename), out stockReturnRequestConfirmation); //Set message status MessageHelper.UpdateMessageStatus(file.MessageID, result ? WehkampMessageStatus.InProgress : WehkampMessageStatus.Error); if (result) { var vendorID = file.VendorID; var defaultWarehouseCode = VendorHelper.GetReturnDifferenceShopNumber(vendorID); var confirmationItems = new List <StockReturnRequestConfirmationLineObject>(); #region foreach (var confirmation in stockReturnRequestConfirmation.bevestiging) log.InfoFormat("Found {0} confirmations to process", stockReturnRequestConfirmation.uitslag.Count); var counter = 0; var sendingTime = DateTime.ParseExact(string.Format("{0} {1}", stockReturnRequestConfirmation.header.berichtDatumTijd.ToString("yyyy-MM-dd"), DateTime.Now.ToString("HH:mm:ss")), "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); var groupedUitslag = from u in stockReturnRequestConfirmation.uitslag group u by new { u.artikelNummer, u.kleurNummer, u.maat } into gu select new retourUitslagUitslag { artikelNummer = gu.Key.artikelNummer, kleurNummer = gu.Key.kleurNummer, maat = gu.Key.maat, locusStatus = "not used", verzondenAantal = gu.Sum(u => int.Parse(u.verzondenAantal)).ToString(CultureInfo.InvariantCulture) }; //foreach (var confirmation in stockReturnRequestConfirmation.uitslag) foreach (var confirmation in groupedUitslag) { int orderID; int orderLineID; //create object var productID = Helpers.ProductHelper.GetProductIDByWehkampData(confirmation.artikelNummer, confirmation.kleurNummer, confirmation.maat, vendorID); if (StockReturnHelper.OrderLineExistForProductAndOrderType((int)OrderTypes.ReturnOrder, productID)) { orderLineID = StockReturnHelper.GetLastOrderLineIDByOrderTypeAndProductID((int)OrderTypes.ReturnOrder, productID); orderID = StockReturnHelper.GetOrderIDByOrderLineID(orderLineID); } else { //Order doesn't exists in the database. //Create order and orderline for this product. //Check if we already created an order for this file if (wehkampOrderID == -1) { var receivedDate = DateTime.ParseExact(string.Format("{0} {1}", stockReturnRequestConfirmation.header.berichtDatumTijd.ToString("yyyy-MM-dd"), DateTime.Now.ToString("HH:mm:ss")), "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); wehkampOrderID = StockReturnHelper.CreateOrderRowForWehkampReturns(VendorSettingsHelper.GetConnectorIDByVendorID(vendorID), Path.GetFileNameWithoutExtension(file.Filename), file.Filename, receivedDate.ToUniversalTime()); } orderLineID = StockReturnHelper.CreateOrderLineRow(wehkampOrderID, productID, vendorID, defaultWarehouseCode.ToString(CultureInfo.InvariantCulture)); orderID = wehkampOrderID; } var c = new StockReturnRequestConfirmationLineObject { OrderID = orderID, OrderLineID = orderLineID, ProductID = productID, Quantity = int.Parse(confirmation.verzondenAantal) }; confirmationItems.Add(c); if (counter % 25 == 0) { log.Debug(string.Format("Loaded {0} confirmations from file", counter)); } counter++; } counter = 0; foreach (var item in confirmationItems) { StockReturnHelper.CreateOrderLedgerRow(item.OrderLineID, item.Quantity, sendingTime.ToUniversalTime()); if (counter % 25 == 0) { log.Debug(string.Format("Inserted {0} confirmations into database", counter)); } counter++; } #endregion MessageHelper.Archive(file.MessageID); } } catch (Exception exFile) { log.Fatal(string.Format("Error processing file {0} ", file.Filename), exFile); MessageHelper.Error(file.MessageID); } } log.InfoFormat("Finished processing Stock Return Request Confirmation Import"); _monitoring.Notify(Name, 1); }