Beispiel #1
0
        protected void GVOcConfirmadasNoEntregadas_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null)
            {
                AlertPurchaseOrder ar = (e.Row.DataItem as AlertPurchaseOrder);
                if (ar != null)
                {
                    if (ar.WayOfDelivery == WayOfDelivery.Aereo)
                        e.Row.Cells[8].Text = "Aéreo";
                    else if (ar.WayOfDelivery == WayOfDelivery.Maritimo)
                        e.Row.Cells[8].Text = "Marítimo";
                    
                    if (ar.Type == AlertPurchaseOrderType.Confirmada)
                        e.Row.Cells[9].Text = "Confirmada";
                    else if (ar.Type == AlertPurchaseOrderType.NoConfirmada)
                        e.Row.Cells[9].Text = "No Confirmada";
                    else e.Row.Cells[9].Text = "En Tránsito";

                    if(ar.WayOfDelivery == WayOfDelivery.ND)
                        e.Row.Cells[8].Text = "N/D";
                }
            }
        }
Beispiel #2
0
        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");
            }
Beispiel #3
0
        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 <PartnerNet.Domain.Product> products = ControllerManager.Product.GetAll() as List <PartnerNet.Domain.Product>;
                List <PurchaseOrderItem>         purchaseOrderItemList = Grundfos.ScalaConnector.ControllerManager.PurchaseOrderItem.GetAlerts();

                # region Alerta de Ordenes de Compras Confirmadas y no Despachadas
                try
                {
                    List <StockMovementPending> stockMovementPendings = Grundfos.ScalaConnector.ControllerManager.StockMovementPending.GetPurchaseOrders() as List <StockMovementPending>;
                    List <PurchaseOrder>        purchaseOrderList     = Grundfos.ScalaConnector.ControllerManager.PurchaseOrder.GetAll() as List <PurchaseOrder>;

                    ControllerManager.AlertPurchaseOrder.CleanAlertPurchaseOrder();

                    foreach (StockMovementPending item in stockMovementPendings)
                    {
                        PurchaseOrder po = purchaseOrderList.Find(delegate(PurchaseOrder record)
                        {
                            if (record.Id == item.OrderNumber)
                            {
                                return(true);
                            }
                            return(false);
                        });
                        PurchaseOrderItem poi = purchaseOrderItemList.Find(delegate(PurchaseOrderItem record)
                        {
                            if ((record.Product.Id == item.ProductCode) && (record.PurchaseOrder.Id == item.OrderNumber))
                            {
                                return(true);
                            }
                            return(false);
                        });
                        AlertPurchaseOrder alert = new AlertPurchaseOrder();

                        alert.Destination       = AlertPurchaseOrderDestination.Stock;
                        alert.PurchaseOrderCode = item.OrderNumber;
                        alert.Product           = products.Find(delegate(PartnerNet.Domain.Product record)
                        {
                            if (record.ProductCode == item.ProductCode)
                            {
                                return(true);
                            }
                            return(false);
                        });
                        alert.Quantity = item.Quantity;
                        alert.Type     = AlertPurchaseOrderType.NoConfirmada;
                        if (poi.Confirmed == "1")
                        {
                            alert.Type = AlertPurchaseOrderType.Confirmada;
                        }
                        alert.GAP           = (item.DeliveryCommitt - po.Date).Days;
                        alert.WayOfDelivery = (WayOfDelivery)po.WayOfDelivery;
                        alert.ArrivalDate   = item.DeliveryCommitt;
                        if (item.CustSupCode == null)
                        {
                            alert.PurchaseOrderProviderCode = ControllerManager.Provider.GetProvider("999999").ProviderCode;
                        }
                        else if (!string.IsNullOrEmpty(item.CustSupCode.Trim()))
                        {
                            alert.PurchaseOrderProviderCode = item.CustSupCode;
                        }
                        else
                        {
                            alert.PurchaseOrderProviderCode = string.Empty;
                        }

                        alert.PurchaseOrderDate = po.Date;

                        if (item.CustSupName.EndsWith("#"))
                        {
                            alert.Type = AlertPurchaseOrderType.Transito;
                        }

                        if (alert.Product == null)
                        {
                            continue;
                        }

                        AlertOCGapAverage += alert.GAP;
                        AlertOCGapCount++;

                        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
                //Se reemplazo y unio todo con la alerta 1

                #endregion

                #region Alerta de Stock Negativos
                try
                {
                    List <ProductDetail> productDetails = Grundfos.ScalaConnector.ControllerManager.ProductDetail.GetAlert3();

                    ControllerManager.AlertProduct.CleanAlertProduct();

                    foreach (ProductDetail productDetail in productDetails)
                    {
                        AlertProduct alert = new AlertProduct();
                        alert.StandardCost = productDetail.Product.StandardCost;
                        alert.SubTotal     = productDetail.Product.StandardCost * Convert.ToDouble(productDetail.Stock);
                        alert.Quantity     = productDetail.Stock;
                        alert.Type         = 1;
                        alert.NegativeDate = DateTime.Today;
                        alert.Location     = productDetail.Location;
                        alert.Product      = products.Find(delegate(PartnerNet.Domain.Product record)
                        {
                            if (record.ProductCode == productDetail.Product.Id)
                            {
                                return(true);
                            }
                            return(false);
                        });

                        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 = purchaseOrderItemList.FindAll(delegate(PurchaseOrderItem record)
                    {
                        if ((record.Confirmed == "1") &&
                            (record.PurchaseOrder.Location ==
                             "01") &&
                            (record.PurchaseOrder.Id.
                             StartsWith("00000")))
                        {
                            return(true);
                        }
                        return(false);
                    });

                    ControllerManager.AlertSaleOrder.CleanAlertSaleOrder();

                    List <Product> scalaProducts = new List <Product>();
                    foreach (SaleOrderItem saleOrderItem in saleorderitems)
                    {
                        CreateAlert(products, saleOrderItem, POListAlert3, scalaProducts);
                    }
                    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.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;
                //            alert.Product = product;

                //            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;

                            GetAlertRepositionResult(ar, null);

                            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;

                        GetAlertRepositionResult(ar, pastproduct);

                        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:
                        alerttotal.Total = AlertOCGapAverage / AlertOCGapCount;
                        if (alerttotal.Total < 0)
                        {
                            alerttotal.Total = -alerttotal.Total;
                        }
                        break;

                    case 2:
                        alerttotal.Total = AlertOCGapAverage / AlertOCGapCount;
                        if (alerttotal.Total < 0)
                        {
                            alerttotal.Total = -alerttotal.Total;
                        }
                        break;

                    case 3:
                        alerttotal.Total = ControllerManager.TransactionHistoryWeekly.GetActiveProducts(Config.CurrentWeek, Config.CurrentDate.Year);
                        break;

                    case 4:
                        alerttotal.Total = AlertOVGapAverage / AlertOVGapCount;
                        if (alerttotal.Total < 0)
                        {
                            alerttotal.Total = -alerttotal.Total;
                        }
                        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");
            }