/// <summary> /// Obtains the tariff associated to the ID and day (or day_type) specified. /// </summary> /// <param name="cmpDaysDef">CmpDaysDef with the info about days_def assigned to the current Date.</param> /// <returns>ArrayList of Tarifs objects</returns> public ArrayList ObtainTariff(CmpDay cmpday) { ArrayList tariffs = new ArrayList(); CmpDaysDef cmpdaysdef = cmpday.DaysDef; // Filter the TARIFF table by TAR_ID. As a result we will found a set of registers // Foreach row that the TAR_DAY_ID is NOT NULL check if TAR_DAY_ID is a DAY_ID of the pInDate day. // If it is we have found a tariff. // If none of the registers have TAR_DAY_ID not NULL or we have not found the tariff yet, we get all // registers with a TAR_DDAY_ID NOT NULL, and find the 1st that his DDAY_ID has the current day. CmpTariffsDB cmp = new CmpTariffsDB(); string swhere = "TAR_ID = @TARIFFS.TAR_ID@ AND (TAR_INIDATE <[email protected]_INIDATE@ OR TAR_INIDATE IS NULL) AND (TAR_ENDDATE>= @TARIFFS.TAR_ENDDATE@ OR TAR_ENDDATE IS NULL)"; DataTable dtTariffs = cmp.GetData(null, swhere, new object[] { _tarId, _pInDate, _pInDate }); // Step 1: Check all TAR_DAY_ID not null DataRow [] tarDayId = dtTariffs.Select("TAR_DAY_ID IS NOT NULL"); if (tarDayId.Length > 0) { // Get all DAY_ID that correspond to _pInDate (can be more than one). if (cmpday.Count > 0) { // Finally checks every DAY_ID of the TARIFFS found to IDs stored in daysid array foreach (DataRow drTariff in tarDayId) { int dayid = Convert.ToInt32(drTariff["TAR_DAY_ID"]); // Check if the DAY with day_id is the same day of _pInDate. If it is we have found the tariff (a part // of the tarif, because remember that we can have more than one register). if (dayid == cmpday.Id) { // We have found it!!! We have found it!! tariffs.Add(new Tariff(drTariff)); } } } } // Search now by day_def and acumulate the results. DataRow[] tarDdayId = dtTariffs.Select("TAR_DDAY_ID IS NOT NULL"); if (tarDdayId.Length > 0) { // Get al DDAY_ID that contains the _pInDate (that is, have a 1 in the mask for the _pInDate day) and store // them all (CmpDaysDef will store them for us ;)) if (!cmpdaysdef.IsLoaded) { cmpdaysdef.Load(); } if (cmpdaysdef.Count > 0) { // If we have found at least one DDAY_ID that contains the pInDate process the tariffs... foreach (DataRow drTariff in tarDdayId) { int ddayid = Convert.ToInt32(drTariff["TAR_DDAY_ID"]); // Check if DDAY_ID is a day_def including the day _pInDate. If it is we have found de tariff if (cmpdaysdef.ContainsId(ddayid)) { // We have found it!!! We have found it!! tariffs.Add(new Tariff(drTariff)); } } } } return(tariffs); // If no tariff was found will have Count == 0 }
/// <summary> /// Validates the status of the SISTEM. /// Foreach element of the list of parents, searches a record in table STATUS (filtering by DAY_ID or DDAY_ID). /// Returns when the 1st element of the table STATUS was found. /// </summary> /// <param name="statusTreeList">List of parents (aka Physical trees). Obtained by CmpStatus::GetUnitTree()</param> /// <param name="cmpDay">CmpDay with info about the Day AND DaysDef</param> /// <param name="timid">TIM_ID (timetable) for the status is searched</param> /// <returns>Status of the sistem (STA_DSTA_ID of the register found) or -1 if no register is found in STATUS</returns> public int ValidateStatus(ArrayList statusTreeList, CmpDay cmpDay, int timid) { CmpStatusDB cmpdb = new CmpStatusDB(); string swhere0 = null; string swhere1 = "[email protected]_TIM_ID@ "; string sformat = "SELECT STA_DSTA_ID from STATUS WHERE {0} and {1} and {2} and STA_DAY_ID = @STATUS.STA_DAY_ID@"; DataTable dtStatus = null; object[] whereValues = new object[] { null, timid, cmpDay.Id }; // null will be filled later bool compareByDayId = cmpDay.Count > 0; StringBuilder sb = null; string [] fields = new string[] { "STA_DSTA_ID", "STA_DDAY_ID" }; foreach (object o in statusTreeList) { sb = new StringBuilder(); StatusTreeItem item = ((StatusTreeItem)((UnorderedTree.TreeItem)o).Data); // Builds specific sql string depending upon item is unit, or group or group_type if (item.IsUnit) { swhere0 = "[email protected]_UNI_ID@"; whereValues[0] = item.Id; } else if (item.IsGroup) { swhere0 = "[email protected]_GRP_ID@"; whereValues[0] = item.Id; } else // item.IsType { swhere0 = "[email protected]_DGRP_ID@"; whereValues[0] = item.IdType; } if (compareByDayId) { sb.AppendFormat(sformat, swhere0, swhere1, "STA_DAY_ID", "STA_DAY_ID"); // Get rows from STATUS for current unit/group/group_day, timetable and DAY dtStatus = cmpdb.GetData(sb.ToString(), whereValues); if (dtStatus.Rows.Count > 1) { // Log the incoherence and return the 1st register // TODO: Log return(Convert.ToInt32(dtStatus.Rows[0]["STA_DSTA_ID"])); } else if (dtStatus.Rows.Count == 1) { // We have found it. return(Convert.ToInt32(dtStatus.Rows[0]["STA_DSTA_ID"])); } } // If we reached that point means one of the following: // No STATUS register found when searching by day_id // There was no DAY corresponding to the currend date // in both cases we have to do the same thing: search by dday_id sb.AppendFormat("{0} AND {1}", swhere0, swhere1); dtStatus = cmpdb.GetData(fields, sb.ToString(), whereValues); // Although whereValues has 3 values, only 2 first will be used // In that point we have all STATUS registers that are for current unit/group/group_type and // for all days/days_def. Check if one of the registers are contained in DAYS_DEF of current day foreach (DataRow dr in dtStatus.Rows) { if (dr["STA_DDAY_ID"] != DBNull.Value) { int daydef = Convert.ToInt32(dr["STA_DDAY_ID"]); if (cmpDay.DaysDef.ContainsId(daydef)) { // We have found the register. return(Convert.ToInt32(dr["STA_DSTA_ID"])); } } } // No register was found, searching for day or day_def for the current item... go to next item } // We have no found any register... return(-1); }