/// <summary>
        /// Function used to Find a TreeItem with the specified id
        /// </summary>
        /// <param name="item">Item to check if has the correct id</param>
        /// <param name="data">DataRow containing the value of the group (contains id, dgrp_id, and so on).</param>
        /// <returns>true of item has the correctid</returns>
        protected bool FindItemById(UnorderedTree.TreeItem item, object data)
        {
            CmpStatus.StatusTreeItem stitem = (CmpStatus.StatusTreeItem)item.Data;
            DataRow group = (DataRow)data;
            bool    bType = Convert.ToBoolean(group["TipoAgrupacion"]);
            int     id    = Convert.ToInt32(group["Agrupacion"]);
            bool    bRet  = bType ? stitem.IdType == id : stitem.Id == id;

            return(bRet);
        }
        /// <summary>
        /// Get the rules for a Article given one group/or group type (GROUP or GROUPS_DEF item)
        /// </summary>
        /// <param name="art">Article to use</param>
        /// <param name="item">Group or Group type to use</param>
        /// <returns>An ArticleRules object with data</returns>
        public static ArticleRules GetArticleRules(Article art, int artDefId, CmpStatus.StatusTreeItem item, DateTime time)
        {
            string swhere = null;

            object[] whereValues = null;
            if (item.IsGroup)
            {
                swhere      = "RUL_GRP_ID = @ARTICLES_RULES.RUL_GRP_ID@";
                whereValues = new object[] { item.Id, null, null, null };
            }
            else if (item.IsType)
            {
                swhere      = "RUL_DGRP_ID = @ARTICLES_RULES.RUL_DGRP_ID@";
                whereValues = new object[] { item.IdType, null, null, null };
            }
            else
            {
                // We have only ARTICLES_RULES entries for GROUPS or GROUPS_DEF but not for UNITS!
                // TODO: Log the error.
                return(null);
            }
            swhere         = swhere + " AND (RUL_ENDDATE >= @ARTICLES_RULES.RUL_ENDDATE@ OR RUL_ENDDATE IS NULL) AND (RUL_INIDATE <= @ARTICLES_RULES.RUL_INIDATE@ OR RUL_INIDATE IS NULL)";
            whereValues[1] = time;
            whereValues[2] = time;

            // Now we add the filter by article type
            swhere        += " AND (RUL_DART_ID = @ARTICLES_RULES.RUL_DART_ID@)";
            whereValues[3] = (art != null ? art.DartId : artDefId);

            CmpArticlesRulesDB cmp = new CmpArticlesRulesDB();
            DataTable          dt  = cmp.GetData(null, swhere, whereValues);

            if (dt.Rows.Count > 1)
            {
                // TODO: Log the error [We must find at most one register]
                return(null);
            }
            else if (dt.Rows.Count == 0)
            {
                // This is no error
                return(null);
            }
            // Return the first row of the DataTable
            return(new ArticleRules(dt.Rows[0], art));
        }
예제 #3
0
        /// <summary>
        /// Check if exists one amplied zone that has the PDM passed and our group associated.
        /// </summary>
        /// <param name="pdm">PDM to search for amplied zone</param>
        /// <returns>ID of the amplied zone or -1 if no amplied zone is found.</returns>
        protected int CheckForAmpliedZone(int pdm)
        {
            CmpStatus     cmps = new CmpStatus();
            UnorderedTree tree = null;
            // Build the logical tree of the pdm
            ArrayList ar = cmps.GetUnitTree(pdm, true, false, out tree);

            // Now search for ar, searching for our group. If we found and is a amplied zone that means that exists
            // an amplied zone containing both: our group, and the pdm.
            foreach (object o in ar)
            {
                CmpStatus.StatusTreeItem titem = ((CmpStatus.StatusTreeItem)((UnorderedTree.TreeItem)o).Data);
                if (titem.IsGroup && titem.Id == _grpid && titem.IdType == _ampliedZoneTypeId)
                {
                    return(titem.Id);
                }
            }
            return(-1);
        }
 /// <summary>
 /// Method used to evaluate a TreeItem, and returning the id of the group contained by that item.
 /// </summary>
 /// <param name="item">Item to evaluate</param>
 /// <returns>Int32 with the Id or null if item is not a group (is Unit or Type)</returns>
 private object TreeItemToId(UnorderedTree.TreeItem item)
 {
     CmpStatus.StatusTreeItem stitem = (CmpStatus.StatusTreeItem)item.Data;
     return(stitem.IsGroup ? (object)stitem.Id : (object)null);
 }
        /// <summary>
        /// Get all the values of:
        ///		One Constraint
        ///			for
        ///		A list of group/group types
        /// </summary>
        /// <param name="tipoRestricciones">Array of 5 int with the values of table CON_DEF.
        /// tipoRestricciones[0] must be the DCON_ID of MAX_ESTANCIA (o MAX_PERMANENCIA).
        /// tipoRestricciones[1] must be the DCON_ID of MIN_REENTRADA.
        /// tipoRestricciones[2] must be the DCON_ID of TIEMPO_CORTESIA.
        /// tipoRestricciones[3] must be the DCON_ID of MAX_IMPORT.
        /// tipoRestricciones[4] must be the DCON_ID of MIN_IMPORT.
        /// </param>
        /// <returns>DataTable with all constraints for a con_id and a list of group/group_types</returns>
        public DataTable GetConstraints(int[] tipoRestricciones)
        {
            // Builds the DataTable that we will return
            DataTable dtReturn = new DataTable();

            dtReturn.Columns.Add("Agrupacion", typeof(int));                            // ID del grupo (GRP_ID o DGRP_ID)
            dtReturn.Columns.Add("TipoAgrupacion", typeof(bool));                       // Indica si el campo anterior es DGRP_ID (true) o GRP_ID
            dtReturn.Columns.Add("MP", typeof(int));                                    // Tiempo maximo de permanencia
            dtReturn.Columns.Add("DGRP_ID", typeof(int));                               // DGRP_ID de la agrupación (tanto so es group o group_type).
            dtReturn.Columns.Add("CalculoMP", typeof(int));                             //MP Calculado para la agrupación y sus hijos
            dtReturn.Columns.Add("TR", typeof(int));                                    // Tiempo de reentrada
            dtReturn.Columns.Add("TC", typeof(int));                                    // Tiempo de cortesia
            dtReturn.Columns.Add("PagoTC", typeof(bool));                               // Si TC se paga o no
            dtReturn.Columns.Add("Imax", typeof(double));                               // Importe max
            dtReturn.Columns.Add("Imin", typeof(double));                               // Importe min

            // Get ALL the constraints from the current CON_ID
            CmpConstraintsDB cmp = new CmpConstraintsDB();
            DataTable        dt  = cmp.GetData(null, "CON_ID = @CONSTRAINTS.CON_ID@", new object[] { _conid });

            // Filter and stores only the constraints that exists in groups
            foreach (object o in _lstGroups)
            {
                CmpStatus.StatusTreeItem item = (CmpStatus.StatusTreeItem)((UnorderedTree.TreeItem)o).Data;
                string sfilter = null;
                if (item.IsUnit)
                {
                    continue;
                }
                else if (item.IsGroup)
                {
                    sfilter = "CON_GRP_ID=" + item.Id;
                }
                else                            // item.IsType
                {
                    sfilter = "CON_DGRP_ID=" + item.IdType;
                }
                // Get all the constraints to be applied to that group or group type.
                // Will add a single row containing all the constraints.
                DataRow[] rows = dt.Select(sfilter);
                if (rows.Length > 0)
                {
                    DataRow rowToAdd = dtReturn.NewRow();
                    rowToAdd["TipoAgrupacion"] = item.IsType;
                    rowToAdd["Agrupacion"]     = item.IsType ? item.IdType : item.Id;
                    rowToAdd["DGRP_ID"]        = item.IdType;
                    rowToAdd["CalculoMP"]      = DBNull.Value;
                    foreach (DataRow row in rows)
                    {
                        // foereach constraint add it in the DataTable that we have to return...
                        rowToAdd["PagoTC"] = false;                                             // TODO: Read from row object (is not in BBDD now)
                        int    tipoRestriccion = Convert.ToInt32(row["CON_DCON_ID"]);
                        string scol            = null;
                        if (tipoRestriccion == tipoRestricciones[0])
                        {
                            scol = "MP";
                        }                                                                                                               // MAX_PERMANENCIA
                        else if (tipoRestriccion == tipoRestricciones[1])
                        {
                            scol = "TR";
                        }                                                                                                               // TIEMPO_REENTRADA
                        else if (tipoRestriccion == tipoRestricciones[2])
                        {
                            scol = "TC";
                        }                                                                                                               // TIEMPO_CORTESIA
                        else if (tipoRestriccion == tipoRestricciones[3])
                        {
                            scol = "Imax";
                        }                                                                                                       // MAX_IMPORT
                        else if (tipoRestriccion == tipoRestricciones[4])
                        {
                            scol = "Imin";
                        }                                                                                                       // MIN_IMPORT

                        if (scol != null)
                        {
                            rowToAdd[scol] = row["CON_VALUE"];
                        }
                    }
                    dtReturn.Rows.Add(rowToAdd);
                }
            }
            dtReturn.AcceptChanges();
            return(dtReturn);
        }
        /// <summary>
        /// Validates the TR (tiempo de reentrada) constraints for a given table of constraints and groups
        /// That method is called after get the constraints (CmpConstraints::GetConstraints) and
        /// after filtering the constraints obtained (CmpConstraints::FilterConstraintsTable)
        /// </summary>
        /// <param name="dtConstraints">DataTable with constraints and groups (not groups_types) to process.</param>
        /// <param name="pInDate">DateTime when te operation was done (usually MOV_OPDATE of M01 message)</param>
        /// <param name="artid">ID of the ARTICLE (-1 for NULL)</param>
        /// <param name="vehicleid">ID of the VEHICLE (CANNOT be null)</param>
        /// <param name="operationId">OUT parameter with value of DATE of previous Operation (only used if return code is PreviousDate)</param>
        /// <returns>A ValidateTRReturnCodes specifying the result of validation (non valid, ok with previous date, ok)</returns>
        public ValidateTRReturnCodes ValidateTR(DataTable dtConstraints, DateTime pInDate,
                                                int artid, string vehicleid, out int operationId)
        {
            //previousDate = DateTime.MinValue;
            operationId = -1;
            CmpOperationsDB cmp = new CmpOperationsDB();

            foreach (DataRow group in dtConstraints.Rows)
            {
                int       tr    = group["TR"] != DBNull.Value ? Convert.ToInt32(group["TR"]) : 0;
                int       tc    = group["TC"] != DBNull.Value ? Convert.ToInt32(group["TC"]) : 0;
                DateTime  opend = pInDate.Subtract(new TimeSpan(0, tr, 0));
                DataTable dtOperations;
                if (artid != -1)
                {
                    dtOperations = cmp.GetAllData(null, "OPE_ENDDATE >= @OPERATIONS.OPE_ENDDATE@ "
                                                  + "AND OPE_VEHICLEID = @OPERATIONS.OPE_VEHICLEID@ "
                                                  + "AND OPE_ART_ID = @OPERATIONS.OPE_ART_ID@",
                                                  "OPE_ENDDATE DESC", new object[] { opend, vehicleid, artid });
                }
                else
                {
                    dtOperations = cmp.GetAllData(null, "OPE_ENDDATE >= @OPERATIONS.OPE_ENDDATE@ "
                                                  + "AND OPE_VEHICLEID = @OPERATIONS.OPE_VEHICLEID@",
                                                  "OPE_ENDDATE DESC", new object[] { opend, vehicleid });
                }
                if (dtOperations.Rows.Count > 0)
                {
                    // Ok.. At that point we have:
                    //		ALL operations of the current vehicle and current article for ALL zones in the TR period of time.
                    //		We must filter that table for having only the operations for the CURRENT zone and ALL HIS CHILDS.
                    //		The rest of operations are discarded at that point (but, of course, we could find them again when
                    //		processing another zone
                    UnorderedTree.TreeItem titem = _treeGroups.FindItem(new UnorderedTree.FindItemDelegate(FindItemById), group);
                    // Delete from dtOperations table ALL operations that are not associated by titem or any childs of titem.
                    FilterOperationsTableByGroup(dtOperations, titem);
                }
                if (dtOperations.Rows.Count > 0)
                {
//					return ValidateTRReturnCodes.OperationNonValid;
                    // At that point we have only the operations related with the current group (or any of his childs).
                    DataRow  operation = dtOperations.Rows[0];
                    DateTime opendtc   = Convert.ToDateTime(operation["OPE_ENDDATE"]).AddMinutes(tc);
                    if (pInDate > opendtc)
                    {
                        // Operation not valid. At the first non-valid operation we return
                        return(ValidateTRReturnCodes.OperationNonValid);
                    }
                    else                     // An ampliation of previous operation is possible
                    {
                        // Find the group of the current operation
                        int currentGroup = -1;
                        foreach (object o in _lstGroups)
                        {
                            CmpStatus.StatusTreeItem item = (CmpStatus.StatusTreeItem)((UnorderedTree.TreeItem)o).Data;
                            if (item.IsUnit)
                            {
                                continue;
                            }
                            else if (item.IsGroup)
                            {
                                currentGroup = item.Id;
                                break;
                            }
                        }

                        // Let's check if the groups are the same.
                        // If not, exit as an extension is not possible.
                        int operationGroup = Convert.ToInt32(operation["OPE_GRP_ID"]);
                        if (currentGroup != operationGroup)
                        {
                            return(ValidateTRReturnCodes.OperationNonValid);
                        }

                        operationId = Convert.ToInt32(operation["OPE_ID"]);
                        return(ValidateTRReturnCodes.Extension);
                        /////////////////////////////////////////////////////////
                        //int groupid = Convert.ToInt32 (group["Agrupacion"]);
                        //if (opeid == groupid && Convert.ToBoolean (group["PagoTC"]))
                        //{
                        //	// An ampliation is allowed.
                        //	previousDate = Convert.ToDateTime (operation["OPE_ENDDATE"]);
                        //	return ValidateTRReturnCodes.PreviousDate;
                        //}
                    }
                }
            }
            return(ValidateTRReturnCodes.OnlyOk);
        }