public bool Execute(out string errors) { errors = ""; try { #region Check if needs to be executed if (Config.CurrentDate.DayOfWeek == (DayOfWeek)Convert.ToInt32(ConfigurationManager.AppSettings["ExecuteDayOfWeek"]) && DateTime.Now.Hour >= Convert.ToInt32(ConfigurationManager.AppSettings["ExecuteHour"])) { //Review if the forecast is executing in the log //If not start the process, otherwise returns true with no errors. if (ControllerManager.Log.IsExecuting(Name, ExecutionStatus.Start)) { return(true); } else { ControllerManager.Log.Add(Name, ExecutionStatus.Start, string.Empty); } } else { return(true); } #endregion DateTime endDate = Config.CurrentDate; DateTime startDate = endDate.AddDays(-7); #region Provider Refresh IList <Grundfos.ScalaConnector.Provider> proveedoresScala = Grundfos.ScalaConnector.ControllerManager.Provider.GetProviderList(); List <PartnerNet.Domain.Provider> proveedores = ControllerManager.Provider.GetFullProviderList(); try { foreach (Grundfos.ScalaConnector.Provider provider in proveedoresScala) { bool updated = false; PartnerNet.Domain.Provider prov = null; prov = proveedores.Find(delegate(PartnerNet.Domain.Provider record) { if (record.ProviderCode != provider.Id) { return(false); } return(true); }); if (prov != null) { prov.Name = provider.Name; prov.CountryCode = provider.CountryCode; updated = true; } if (updated == false) { PartnerNet.Domain.Provider nuevo = new PartnerNet.Domain.Provider(); nuevo.Name = provider.Name; nuevo.ProviderCode = provider.Id; //cambiar y resubir nuevo.CountryCode = provider.CountryCode; ControllerManager.Provider.SaveOrUpdate(nuevo); updated = true; } } ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Provider refresh successfully"); } catch (Exception ex) { errors = ex.ToString(); try { string process = "Provider Refresh"; ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); SendErrorEmail(process, errors); } catch { } return(false); } #endregion #region Product Refresh IList <Grundfos.ScalaConnector.Product> productosScala = Grundfos.ScalaConnector.ControllerManager.Product.GetProductList(); List <PartnerNet.Domain.Product> productos = ControllerManager.Product.GetFullProductList(); try { foreach (Grundfos.ScalaConnector.Product product in productosScala) { PartnerNet.Domain.Product producto = productos.Find(delegate(PartnerNet.Domain.Product record) { if (record.ProductCode != product.Id) { return(false); } return(true); }); if (producto == null) { producto = new PartnerNet.Domain.Product(); producto.ProductCode = product.Id; producto.Safety = 6; } producto.Description = product.Description; producto.Group = product.Group; producto.LeadTime = product.Detail[0].Leadtime; producto.CountryCode = product.CountryCode; if (producto.RepositionLevel != product.Detail[0].RepPoint) { producto.RepositionLevel = product.Detail[0].RepPoint; //ProductRepositionLevelHistory prlh = new ProductRepositionLevelHistory(); //prlh.Product = producto; //prlh.RepositionLevel = producto.RepositionLevel; //ControllerManager.Product.GenericSave(prlh); } producto.RepositionPoint = product.Detail[0].PurchaseMod; producto.AlternativeProduct = product.AlternativeProduct; producto.AlternativeDate = product.AlternativeDate; PartnerNet.Domain.Provider provtemp = proveedores.Find(delegate(PartnerNet.Domain.Provider record) { if (record.ProviderCode != product.Provider.Id) { return(false); } return(true); }); if (provtemp != null) { producto.Provider = provtemp; } else if (provtemp == null) { producto.Provider = proveedores.Find(delegate(PartnerNet.Domain.Provider record) { if (record.ProviderCode != "999999") { return(false); } return(true); }); } foreach (ProductDetail productDetail in product.Detail) { if (productDetail.RepPoint == 0) { producto.ScalaRep0 = true; } } ControllerManager.Product.Save(producto); } ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Product refresh successfully"); } catch (Exception ex) { errors = ex.ToString(); try { string process = "Product Refresh"; ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); SendErrorEmail(process, errors); } catch { } return(false); } #endregion #region Generate History #region Limpieza de datos procesador de esta semana (si los hubiera) ControllerManager.TransactionHistoryWeekly.CleanData(Config.CurrentWeek, Config.CurrentDate.Year); #endregion IList <Product> prodlist = Grundfos.ScalaConnector.ControllerManager.Product.GetProductList(); List <PartnerNet.Domain.Product> productlist = ControllerManager.Product.GetProductListAlt(); try { List <Transactions> transcomp = Grundfos.ScalaConnector.ControllerManager.Transactions.GetTransaction(startDate, endDate, 0); List <Transactions> transvent = Grundfos.ScalaConnector.ControllerManager.Transactions.GetTransaction(startDate, endDate, 1); List <PurchaseOrderItem> poil = Grundfos.ScalaConnector.ControllerManager.PurchaseOrderItem.GetWeeklyTransaction(startDate, endDate); foreach (Product prod in prodlist) { PartnerNet.Domain.Product producto = null; producto = productlist.Find(delegate(PartnerNet.Domain.Product record) { if (record.ProductCode != prod.Id) { return(false); } return(true); }); if (producto != null) { #region Generate TransactionHistoryWeekly TransactionHistoryWeekly transax = new TransactionHistoryWeekly(); int stock = 0; int purchases = 0; int sales = 0; int purchaseorders = 0; stock = prod.StockQ; Transactions subList = null; subList = transcomp.Find(delegate(Transactions record) { if (record.Product != prod) { return(false); } return(true); }); if (subList != null) { purchases = subList.Quantity; } subList = transvent.Find(delegate(Transactions record) { if (record.Product != prod) { return(false); } return(true); }); if (subList != null) { sales = -subList.Quantity; } PurchaseOrderItem subList2 = null; subList2 = poil.Find(delegate(PurchaseOrderItem record) { if (record.Product != prod) { return(false); } return(true); }); if (subList2 != null) { purchaseorders = subList2.QuantityOrdered; } transax.ProductID = producto; transax.Purchase = purchases; transax.Sale = sales; transax.PurchaseOrders = purchaseorders; transax.Stock = stock; transax.Year = endDate.Year; transax.Week = Thread.CurrentThread.CurrentCulture.Calendar.GetWeekOfYear(endDate, CalendarWeekRule. FirstFourDayWeek, DayOfWeek.Sunday); transax.ProductCode = prod.Id; ControllerManager.TransactionHistoryWeekly.Save(transax); #endregion } } ControllerManager.TransactionHistoryWeekly.Copy(); ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Weekly history created successfully"); } catch (Exception ex) { errors = ex.ToString(); try { string process = "Weekly History"; ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); SendErrorEmail(process, errors); } catch { } return(false); } #endregion #region Generate Weekly Statitics try { ControllerManager.TransactionHistoryWeekly.CalculateFullStatistic(Config.CurrentWeek, Config.CurrentDate.Year); ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Weekly statistics created successfully"); } catch (Exception ex) { errors = ex.ToString(); try { string process = "Weekly Statistics"; ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); SendErrorEmail(process, errors); } catch { } return(false); } #endregion if (Config.CurrentDate.Day <= 7) { #region Limpieza de datos procesador de este mes (si los hubiera) ControllerManager.TransactionHistoryMonthly.CleanData(Config.CurrentDate.Month, Config.CurrentDate.Year); #endregion #region Generate Monthly History try { DateTime endDateM = GetLastDayOfMonth(Config.CurrentDate.AddMonths(-1)); DateTime startDateM = GetFirstDayOfMonth(Config.CurrentDate.AddMonths(-1)); List <Transactions> transcompm = Grundfos.ScalaConnector.ControllerManager.Transactions.GetTransaction(startDateM, endDateM, 0); List <Transactions> transventm = Grundfos.ScalaConnector.ControllerManager.Transactions.GetTransaction(startDateM, endDateM, 1); List <PurchaseOrderItem> poilm = Grundfos.ScalaConnector.ControllerManager.PurchaseOrderItem.GetWeeklyTransaction(startDateM, endDateM); foreach (Product prod in prodlist) { PartnerNet.Domain.Product producto = null; producto = productlist.Find(delegate(PartnerNet.Domain.Product record) { if (record.ProductCode != prod.Id) { return(false); } return(true); }); if (producto != null) { #region Generate TransactionHistoryMonthly TransactionHistoryMonthly transax = new TransactionHistoryMonthly(); int stock = 0; int purchases = 0; int sales = 0; int purchaseorders = 0; stock = prod.StockQ; Transactions subList = null; subList = transcompm.Find(delegate(Transactions record) { if (record.Product != prod) { return(false); } return(true); }); if (subList != null) { purchases = subList.Quantity; } subList = transventm.Find(delegate(Transactions record) { if (record.Product != prod) { return(false); } return(true); }); if (subList != null) { sales = -subList.Quantity; } PurchaseOrderItem subList2 = null; subList2 = poilm.Find(delegate(PurchaseOrderItem record) { if (record.Product != prod) { return(false); } return(true); }); if (subList2 != null) { purchaseorders = subList2.QuantityOrdered; } transax.ProductID = producto; transax.Purchase = purchases; transax.Sale = sales; transax.PurchaseOrders = purchaseorders; transax.Stock = stock; transax.Year = endDateM.Year; transax.Month = Thread.CurrentThread.CurrentCulture.Calendar.GetMonth(endDateM); transax.ProductCode = prod.Id; ControllerManager.TransactionHistoryMonthly.Save(transax); #endregion } } ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Montly history created successfully"); } catch (Exception ex) { errors = ex.ToString(); try { string process = "Monthly History"; ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); SendErrorEmail(process, errors); } catch { } return(false); } #endregion #region Generate Monthly Statitics //try //{ // ControllerManager.TransactionHistoryMonthly.CalculateFullStatistic(Config.CurrentDate.Month - 1, Config.CurrentDate.Year); // ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Monthly statistics created successfully"); //} //catch (Exception ex) //{ // errors = ex.ToString(); // try // { // string process = "Monthly Statistics"; // ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); // SendErrorEmail(process, errors); // } // catch // { // } // return false; //} #endregion } #region Generate Forecast try { IList <PartnerNet.Domain.Product> tempprodlist = new List <PartnerNet.Domain.Product>(); ControllerManager.Forecast.CalculateFullForecast(Config.CurrentWeek, Config.CurrentDate.Year, tempprodlist, true); ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Forecast created successfully"); } catch (Exception ex) { errors = ex.ToString(); try { string process = "Forecast"; ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); SendErrorEmail(process, errors); } catch { } return(false); } #endregion #region Generate Purchase Orders #region Limpieza de datos procesador de esta semana (si los hubiera) ControllerManager.PurchaseOrder.CleanData(Config.CurrentDate); #endregion try { ControllerManager.PurchaseOrder.GenerateFullPO(Config.CurrentWeek, Config.CurrentDate.Year, PurchaseOrderType.Forecast); ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Purchase orders created successfully"); } catch (Exception ex) { errors = ex.ToString(); try { string process = "Purchase Orders"; ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); SendErrorEmail(process, errors); } catch { } return(false); } #endregion #region Proceso de Forecast a Productos Faltantes //List<string> prodlistfalt = PartnerNet.Business.ControllerManager.Product.GetLeftProductList(); //List<PartnerNet.Domain.Product> prodlisttemp = PartnerNet.Business.ControllerManager.Product.GetFullProductList(); //List<string> leftproducts = new List<string>(); //foreach (PartnerNet.Domain.Product product in prodlisttemp) //{ // string temp = prodlistfalt.Find(delegate(string record) // { // if (record == product.ProductCode) // { // return true; // } // return false; // }); // if (temp == null) // { // leftproducts.Add(product.ProductCode); // } //} //foreach (string product in leftproducts) //{ // PartnerNet.Domain.Product producto = PartnerNet.Business.ControllerManager.Product.GetProductInfo(product); // PartnerNet.Business.ControllerManager.Forecast.CalculateIndividualForecast(Config.CurrentWeek, Config.CurrentDate.Year, producto); //} #endregion ControllerManager.Log.Add(Name, ExecutionStatus.Finished, "Process finished successfully"); } catch (Exception ex) { errors = ex.ToString(); try { string process = "Proceso de Forecast"; ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); SendErrorEmail(process, errors); } catch { } return(false); } return(true); }
public bool Execute(out string errors) { errors = ""; try { #region Check if needs to be executed if (DateTime.Now.Hour >= Convert.ToInt32(ConfigurationManager.AppSettings["ExecuteHourAlerts"])) { // Review if the forecast is executing in the log // If not start the process, otherwise returns true with no errors. if (ControllerManager.Log.IsExecuting(Name, ExecutionStatus.Start)) { return(true); } else { ControllerManager.Log.Add(Name, ExecutionStatus.Start, string.Empty); } } else { return(true); } #endregion //------------------------------------------------------------------------------------------------------------------ List <PurchaseOrderItem> POList = Grundfos.ScalaConnector.ControllerManager.PurchaseOrderItem.GetAlerts(); List <SaleOrderItem> saleorderitemsgrouped = Grundfos.ScalaConnector.ControllerManager.SaleOrderItem.PendingSaleOrderQuantity(Config.CurrentDate.AddDays(-70)); # region Alerta de Ordenes de Compras Confirmadas y no Despachadas try { List <PurchaseOrderItem> POListAlert1 = POList.FindAll(delegate(PurchaseOrderItem record) { double daysOfWOF = 0; switch (record.PurchaseOrder.WayOfDelivery) { case 1: daysOfWOF = 44; break; case 2: daysOfWOF = 12; break; case 3: daysOfWOF = 4; break; } if ((record.Confirmed == "1") && (record.QuantityOrdered > record.Quantity) && (record.PurchaseOrder.Location == "01") && (record.PurchaseOrder.Id.StartsWith("00000"))) { if (record.PurchaseOrder.PurchaseOrderDelivery.Count > 0) { if ((record.PurchaseOrder.PurchaseOrderDelivery[0].Provider.Id == "900900" || record.PurchaseOrder.PurchaseOrderDelivery[0].Provider.Id == "900928")) { if (!record.PurchaseOrder.PurchaseOrderDelivery[0].ProviderStatus.EndsWith("#") && record.PurchaseOrder.PurchaseOrderDelivery[0].DeliveryDate.AddDays(-daysOfWOF) > Config.CurrentDate) { return(true); } } else { return(true); } } else { return(true); } } return(false); }); ControllerManager.AlertPurchaseOrder.CleanAlertPurchaseOrder(); foreach (PurchaseOrderItem item in POListAlert1) { AlertPurchaseOrder alert = new AlertPurchaseOrder(); alert.Destination = SearchSaleOrderDestination(saleorderitemsgrouped, item); alert.PurchaseOrderCode = item.PurchaseOrder.Id; alert.PurchaseOrderItemCode = item.Product.Id; alert.Quantity = item.QuantityOrdered - item.Quantity; alert.Type = AlertPurchaseOrderType.Alert1; alert.GAP = 0; alert.WayOfDelivery = (WayOfDelivery)item.PurchaseOrder.WayOfDelivery; alert.ArrivalDate = item.ArrivalDate; if (item.Product.Provider == null) { alert.PurchaseOrderProviderCode = ControllerManager.Provider.GetProvider("999999").ProviderCode; alert.PurchaseOrderProviderName = ControllerManager.Provider.GetProvider("999999").Name; } else if (!string.IsNullOrEmpty(item.Product.Provider.Id.Trim())) { alert.PurchaseOrderProviderCode = item.Product.Provider.Id; alert.PurchaseOrderProviderName = item.Product.Provider.Name; } else { alert.PurchaseOrderProviderCode = string.Empty; alert.PurchaseOrderProviderName = string.Empty; } double days = 0; switch (item.PurchaseOrder.WayOfDelivery) { case 1: days = 44; break; case 2: days = 12; break; case 3: days = 4; break; } if (item.PurchaseOrder.PurchaseOrderDelivery.Count > 0) { alert.CalculatedArrivalDate = item.PurchaseOrder.PurchaseOrderDelivery[0].DeliveryDate.AddDays(-days); } else { alert.CalculatedArrivalDate = DateTime.MaxValue; } ControllerManager.AlertPurchaseOrder.Save(alert); } ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Alerta de Ordenes de Compras Confirmadas y no Despachadas: Completadas"); } catch (Exception ex) { errors = ex.ToString(); try { string process = "Alerta de Ordenes de Compras Confirmadas y no Despachadas"; ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); SendErrorEmail(process, errors); } catch { } return(false); } #endregion #region Alerta de Ordenes de Compras No Confirmadas try { List <PurchaseOrderItem> POListAlert2 = POList.FindAll(delegate(PurchaseOrderItem record) { if ((record.Confirmed != "1") && (record.PurchaseOrder.Date < Config.CurrentDate.AddDays(-4)) && (record.PurchaseOrder.Location == "01") && (record.QuantityOrdered > record.Quantity) && (record.PurchaseOrder.Id. StartsWith("00000"))) { return(true); } return(false); }); foreach (PurchaseOrderItem item in POListAlert2) { AlertPurchaseOrder alert = new AlertPurchaseOrder(); alert.Destination = SearchSaleOrderDestination(saleorderitemsgrouped, item); alert.PurchaseOrderCode = item.PurchaseOrder.Id; alert.PurchaseOrderItemCode = item.Product.Id; alert.Quantity = item.QuantityOrdered - item.Quantity; alert.Type = AlertPurchaseOrderType.Alert2; alert.GAP = Convert.ToInt32((Config.CurrentDate - item.PurchaseOrder.Date).TotalDays); alert.WayOfDelivery = (WayOfDelivery)item.PurchaseOrder.WayOfDelivery; alert.ArrivalDate = item.ArrivalDate; if ((item.Product.Provider == null) || (item.Product.Provider.Id == " ")) { alert.PurchaseOrderProviderCode = ControllerManager.Provider.GetProvider("999999").ProviderCode; alert.PurchaseOrderProviderName = ControllerManager.Provider.GetProvider("999999").Name; } else if (!string.IsNullOrEmpty(item.Product.Provider.Id.Trim())) { alert.PurchaseOrderProviderCode = item.Product.Provider.Id; alert.PurchaseOrderProviderName = item.Product.Provider.Name; } else { alert.PurchaseOrderProviderCode = string.Empty; alert.PurchaseOrderProviderName = string.Empty; } double days = 0; switch (item.PurchaseOrder.WayOfDelivery) { case 1: days = 44; break; case 2: days = 12; break; case 3: days = 4; break; } if (item.PurchaseOrder.PurchaseOrderDelivery.Count > 0) { alert.CalculatedArrivalDate = item.PurchaseOrder.PurchaseOrderDelivery[0].DeliveryDate.AddDays(-days); } else { alert.CalculatedArrivalDate = DateTime.MaxValue; } ControllerManager.AlertPurchaseOrder.Save(alert); } ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Alerta de Ordenes de Compras No Confirmadas: Completadas"); } catch (Exception ex) { errors = ex.ToString(); try { string process = "Alerta de Ordenes de Compras No Confirmadas"; ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); SendErrorEmail(process, errors); } catch { } return(false); } #endregion #region Alerta de Stock Negativos try { List <Product> ProdList = Grundfos.ScalaConnector.ControllerManager.Product.GetAlert3(); ControllerManager.AlertProduct.CleanAlertProduct(); foreach (Product product in ProdList) { AlertProduct alert = new AlertProduct(); alert.ProductCode = product.Id; alert.StandardCost = product.StandardCost; alert.SubTotal = product.StandardCost * Convert.ToDouble(product.StockQ); alert.Quantity = product.StockQ; alert.Type = 1; alert.NegativeDate = DateTime.Today; ControllerManager.AlertProduct.Save(alert); } ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Alertas de Stock Negativo: Completadas"); } catch (Exception ex) { errors = ex.ToString(); try { string process = "Alertas de Stock Negativo"; ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); SendErrorEmail(process, errors); } catch { } return(false); } #endregion #region Alerta de Ventas no Cumplimentadas try { List <SaleOrderItem> saleorderitems = Grundfos.ScalaConnector.ControllerManager.SaleOrderItem.PendingSaleOrder( Config.CurrentDate.AddDays(-44)); List <PurchaseOrderItem> POListAlert3 = POList.FindAll(delegate(PurchaseOrderItem record) { if ((record.Confirmed == "1") && (record.PurchaseOrder.Location == "01") && (record.PurchaseOrder.Id. StartsWith("00000"))) { return(true); } return(false); }); ControllerManager.AlertSaleOrder.CleanAlertSaleOrder(); foreach (PurchaseOrderItem item in POListAlert3) { AlertSaleOrder alert = new AlertSaleOrder(); AlertPurchaseOrderDestination destination = SearchSaleOrderDestination(saleorderitemsgrouped, item); if (destination == AlertPurchaseOrderDestination.Venta) { List <SaleOrderItem> saleorderitem = SearchSaleOrder(saleorderitems, item); foreach (SaleOrderItem orderItem in saleorderitem) { if (item.ArrivalDate > orderItem.DeliveryDate) { alert.PurchaseOrderCode = item.PurchaseOrder.Id; alert.PurchaseOrderItemCode = item.Product.Id; alert.SaleOrderCode = orderItem.SaleOrder.Id; alert.CustomerCode = orderItem.SaleOrder.CustomerCode; alert.Quantity = (int)orderItem.Quantity; alert.GAP = (item.ArrivalDate - orderItem.DeliveryDate).Days; alert.WayOfDelivery = (WayOfDelivery)item.PurchaseOrder.WayOfDelivery; alert.PurchaseOrderArrivalDate = item.ArrivalDate; alert.SaleOrderDeliveryDate = orderItem.DeliveryDate; alert.OrderDate = orderItem.SaleOrder.Date; ControllerManager.AlertSaleOrder.Save(alert); } } } } ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Alertas de Ventas no Cumplimentadas: Completadas"); } catch (Exception ex) { errors = ex.ToString(); try { string process = "Alertas de Ventas no Cumplimentadas"; ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); SendErrorEmail(process, errors); } catch { } return(false); } #endregion #region Alerta de Stock Futuro Menor al Safety //PENDIENTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! try { List <PartnerNet.Domain.Product> ProdList = ControllerManager.Product.GetAlertNegativeFutureStock(); PartnerNet.Domain.Product prodtemp = null; foreach (PartnerNet.Domain.Product product in ProdList) { if (prodtemp != product) { AlertProduct alert = new AlertProduct(); alert.ProductCode = product.ProductCode; alert.StandardCost = 0; alert.SubTotal = 0; alert.Quantity = product.Forecasts[0].FinalStock; DateTime fechatemp = Convert.ToDateTime("01/01/" + Config.CurrentDate.Year.ToString()); fechatemp = Thread.CurrentThread.CurrentCulture.Calendar.AddWeeks(fechatemp, product.Forecasts[0].Week); alert.NegativeDate = fechatemp.AddDays(-7 + (int)fechatemp.DayOfWeek); alert.Type = 2; ControllerManager.AlertProduct.Save(alert); prodtemp = product; } } ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Stock Futuro Menor al Safety: Completadas"); } catch (Exception ex) { errors = ex.ToString(); try { string process = "Stock Futuro Menor al Safety"; ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); SendErrorEmail(process, errors); } catch { } return(false); } //PENDIENTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #endregion #region Alerta de Nivel de Reposición try { List <AlertReposition> lstrepotmp = ControllerManager.TransactionHistoryWeekly.GetAlert6(Config.CurrentWeek, Config.CurrentDate.Year) as List <AlertReposition>; ControllerManager.AlertReposition.CleanAlertReposition(); List <AlertReposition> aRLst = lstrepotmp.FindAll(delegate(AlertReposition record) { if (record.Product.AlternativeProduct == " " && record.Product.AlternativeDate == new DateTime(9999, 12, 31) ) { return(true); } else { return(false); } }); List <AlertReposition> aRLst2 = lstrepotmp.FindAll(delegate(AlertReposition record) { if (record.Product.AlternativeProduct != " " && record.Product.AlternativeDate != new DateTime(9999, 12, 31) ) { return(true); } else { return(false); } }); List <string> alternativeProducts = new List <string>(); foreach (AlertReposition alertReposition in aRLst2) { alternativeProducts.Add(alertReposition.Product.AlternativeProduct); } List <AlertReposition> lstprodalt = ControllerManager.TransactionHistoryWeekly.GetAlert6(Config.CurrentWeek, Config.CurrentDate.Year, alternativeProducts) as List <AlertReposition>; List <AlertReposition> aRLst3 = lstprodalt.FindAll(delegate(AlertReposition record) { if (record.Product.AlternativeProduct == " " && record.Product.AlternativeDate == new DateTime(9999, 12, 31) ) { return(true); } else { return(false); } }); List <SaleOrderItem> soFullLst = Grundfos.ScalaConnector.ControllerManager.SaleOrderItem.SaleOrdersByProduct(); foreach (AlertReposition ar in aRLst) { List <SaleOrderItem> soLst = soFullLst.FindAll(delegate(SaleOrderItem record) { if (record.Product.Id == ar.Product.ProductCode) { return(true); } else { return(false); } }); double maximumQuantity = 0; double total = 0; bool haveParts = false; string maximumOrderCode = "Sin Orden"; foreach (SaleOrderItem sOI in soLst) { if (sOI.Quantity > maximumQuantity) { maximumQuantity = sOI.Quantity; maximumOrderCode = sOI.SaleOrderId; } total = total + sOI.Quantity; if (sOI.Product.Parts.Count > 0 && sOI.Product.Parts[0].Type == "B") { haveParts = true; break; } } if (!haveParts) { if (((maximumQuantity * total) / 100) > 40) { ar.IsConflicted = true; } else { ar.IsConflicted = false; } ar.OrderInfo = soLst.Count + "/" + ((maximumQuantity * total) / 100) + "/" + maximumOrderCode; if (!aRLst3.Exists(delegate(AlertReposition record) { if (record.ProductCode == ar.ProductCode) { return(true); } else { return(false); } })) { ControllerManager.AlertReposition.Save(ar); } } } foreach (AlertReposition ar in aRLst3) { AlertReposition pastproduct = aRLst2.Find(delegate(AlertReposition record) { if (record.Product.AlternativeProduct == ar.Product.ProductCode) { return(true); } else { return(false); } }); List <SaleOrderItem> soLst = soFullLst.FindAll(delegate(SaleOrderItem record) { if (record.Product.Id == ar.Product.ProductCode) { return(true); } else { return(false); } }); List <SaleOrderItem> soLstAlt = soFullLst.FindAll(delegate(SaleOrderItem record) { if (record.Product.Id == pastproduct.Product.ProductCode) { return(true); } else { return(false); } }); ar.Sales += pastproduct.Sales; int saleMonts = Convert.ToInt32(Math.Round(Convert.ToDecimal(ar.ProductSaleLife + pastproduct.ProductSaleLife) / 4, MidpointRounding.AwayFromZero)); if (saleMonts > 12) { saleMonts = 12; } if (saleMonts == 0) { saleMonts = 1; } decimal cuatrimestralSales; decimal divider = Convert.ToDecimal((saleMonts * 3)) / 12; if (divider > 1) { cuatrimestralSales = Math.Round(ar.Sales / divider); } else { cuatrimestralSales = ar.Sales; } if (cuatrimestralSales > 0) { ar.Result = (ar.Product.RepositionLevel * 100 / cuatrimestralSales) - 100; } double maximumQuantity = 0; double total = 0; bool haveParts = false; string maximumOrderCode = "Sin Orden"; foreach (SaleOrderItem sOI in soLst) { if (sOI.Quantity > maximumQuantity) { maximumQuantity = sOI.Quantity; maximumOrderCode = sOI.SaleOrderId; } total = total + sOI.Quantity; if (sOI.Product.Parts.Count > 0 && sOI.Product.Parts[0].Type == "B") { haveParts = true; break; } } foreach (SaleOrderItem sOI in soLstAlt) { if (sOI.Quantity > maximumQuantity) { maximumQuantity = sOI.Quantity; maximumOrderCode = sOI.SaleOrderId; } total = total + sOI.Quantity; if (sOI.Product.Parts.Count > 0 && sOI.Product.Parts[0].Type == "B") { haveParts = true; break; } } if (!haveParts) { if (((maximumQuantity * total) / 100) > 40) { ar.IsConflicted = true; } else { ar.IsConflicted = false; } ar.OrderInfo = soLst.Count + "/" + ((maximumQuantity * total) / 100) + "/" + maximumOrderCode; ControllerManager.AlertReposition.Save(ar); } } ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Alertas de Nivel de Reposición: Completadas"); } catch (Exception ex) { errors = ex.ToString(); try { string process = "Alertas de Nivel de Reposición"; ControllerManager.Log.Add(Name, ExecutionStatus.Running, process + ": " + errors); SendErrorEmail(process, errors); } catch { } return(false); } #endregion #region Obtener Totales AlertTotal alerttotal; for (int i = 1; i <= 6; i++) { alerttotal = ControllerManager.AlertTotal.GetAlertTotal(i); switch (i) { case 1: List <PurchaseOrderItem> POListAlertTotal1 = POList.FindAll(delegate(PurchaseOrderItem record) { if ((record.QuantityOrdered > record.Quantity) && (record.PurchaseOrder.Location == "01") && (record.PurchaseOrder.Id. StartsWith("00000"))) { return(true); } return(false); }); alerttotal.Total = POListAlertTotal1.Count; break; case 2: List <PurchaseOrderItem> POListAlertTotal2 = POList.FindAll(delegate(PurchaseOrderItem record) { if ((record.QuantityOrdered > record.Quantity) && (record.PurchaseOrder.Location == "01") && (record.PurchaseOrder.Id. StartsWith("00000"))) { return(true); } return(false); }); alerttotal.Total = POListAlertTotal2.Count; break; case 3: alerttotal.Total = ControllerManager.TransactionHistoryWeekly.GetActiveProducts(Config.CurrentWeek, Config.CurrentDate.Year); break; case 4: List <PurchaseOrderItem> POListAlertTotal3 = POList.FindAll(delegate(PurchaseOrderItem record) { if ((record.Confirmed == "1") && (record.PurchaseOrder.Location == "01")) { return(true); } return(false); }); alerttotal.Total = POListAlertTotal3.Count; break; case 5: alerttotal.Total = ControllerManager.TransactionHistoryWeekly.GetActiveProducts(Config.CurrentWeek, Config.CurrentDate.Year); break; case 6: alerttotal.Total = ControllerManager.TransactionHistoryWeekly.GetActiveProducts(Config.CurrentWeek, Config.CurrentDate.Year); break; } } ControllerManager.Log.Add(Name, ExecutionStatus.Running, "Totales obtenidos para las alertas."); #endregion ControllerManager.Log.Add(Name, ExecutionStatus.Finished, "Alert process finished successfully"); }