Esempio n. 1
0
        /// <summary>
        /// Update H2H table based on flight information
        /// </summary>
        public void EngineUpdatePTMH2H(OSUSR_UUK_FLT_INFO flightInfoI, DateTime stampPTM)
        {
            BagTMLog.LogDebug("BagTM Engine Update PTM H2H Start", this);
            int i = 0;

            BaggageEntities db = new BaggageEntities();

            try
            {
                BagTMLog.LogDebug("BagTM Engine Update PTM H2H FlightInfoI", flightInfoI);
                BagTMLog.LogDebug("BagTM Engine Update PTM H2H Hub", hub);

                BagTMLog.LogDebug("BagTM Engine Update Search PTM H2H", this);

                String iFLTNR = (flightInfoI != null) ?
                                CommonFunctions.FormatFlightNumber(flightInfoI.OPERATOR + flightInfoI.FLT_NR) : null;
                System.Nullable <DateTime> iDATE = (flightInfoI != null) ? flightInfoI.STD : null;
                String iORIGIN = (flightInfoI != null) ? flightInfoI.FROM_IATA : null;
                String iDEST   = (flightInfoI != null) ? flightInfoI.TO_IATA : null;
                System.Nullable <DateTime> stamp = stampPTM;

                List <OSUSR_UUK_PTM_H2H> h2hList = this.SearchPTMH2HInformation(
                    iFLTNR, iDATE, iORIGIN);

                List <OSUSR_UUK_PTMREFS2G>        refStandGateTimes = this.SearchRefStandGateTimes();
                List <OSUSR_UUK_PTM_REF_SEC_TIME> secTimes          = this.SearchSecTimes();


                BagTMLog.LogDebug("BagTM Engine Update PTM H2H List", h2hList);


                var ptmQuery = from s in db.OSUSR_UUK_PAXMSGS
                               where s.TIMESTAMP == stamp
                               select s;

                var ptmList = ptmQuery.Where(s => s.IFLTNR == iFLTNR &&
                                             s.IDATE == ((DateTime)iDATE).Date &&
                                             s.IORIGIN == iORIGIN &&
                                             s.TIMESTAMP == stamp)
                              .Select(x => new
                {
                    x.IFLTNR,
                    x.IDATE,
                    x.IORIGIN,
                    x.OFLTNR,
                    x.ODATE,
                    x.ODEST,
                    x.RESERVATIONSTATUS
                })
                              .Distinct()
                              .ToList();

                BagTMLog.LogDebug("BagTM Engine Update Obtain PTM TTY list", ptmList);

                OSUSR_UUK_FLT_INFO flightInfoF;
                foreach (OSUSR_UUK_PTM_H2H h2h in h2hList)
                {
                    flightInfoF = null;

                    BagTMLog.LogDebug("BagTM Engine Update PTM H2H Flight Process", h2h);
                    i++;
                    var ptmListH2H = ptmList.Where(s => s.IFLTNR == h2h.IFLTNR &&
                                                   s.IDATE == h2h.IDATE &&
                                                   s.IORIGIN == h2h.IORIGIN &&
                                                   s.OFLTNR == h2h.OFLTNR &&
                                                   s.ODATE == h2h.ODATE &&
                                                   s.ODEST == h2h.ODEST &&
                                                   s.RESERVATIONSTATUS == h2h.STATUS)
                                     .ToList();

                    var h2hExists = ptmListH2H.FirstOrDefault();

                    BagTMLog.LogDebug("BagTM Engine Update Calculate PTMH2H PAX Count", ptmListH2H);

                    var PAXCalculate = ptmQuery.Where(s => s.IFLTNR == h2h.IFLTNR &&
                                                      s.IDATE == h2h.IDATE &&
                                                      s.IORIGIN == h2h.IORIGIN &&
                                                      s.OFLTNR == h2h.OFLTNR &&
                                                      s.ODATE == h2h.ODATE &&
                                                      s.ODEST == h2h.ODEST &&
                                                      s.RESERVATIONSTATUS == h2h.STATUS)
                                       .GroupBy(x => new
                    {
                        x.IFLTNR,
                        x.IDATE,
                        x.IORIGIN,
                        x.OFLTNR,
                        x.ODATE,
                        x.ODEST,
                        x.RESERVATIONSTATUS
                    })
                                       .Select(x => new
                    {
                        IFLTNR            = x.Key.IFLTNR,
                        IDATE             = x.Key.IDATE,
                        IORIGIN           = x.Key.IORIGIN,
                        OFLTNR            = x.Key.OFLTNR,
                        ODATE             = x.Key.ODATE,
                        ODEST             = x.Key.ODEST,
                        RESERVATIONSTATUS = x.Key.RESERVATIONSTATUS,
                        SumFIRST          = x.Sum(z => z.FIRST),
                        SumBUSINESS       = x.Sum(z => z.BUSINESS),
                        SumECONOMY        = x.Sum(z => z.ECONOMY)
                    }
                                               ).FirstOrDefault();

                    BagTMLog.LogDebug("BagTM Engine Update H2H Exist", PAXCalculate);

                    if (h2h.OFLTNR != null && h2h.ODATE != null)
                    {
                        flightInfoF = this.SearchFlightInfo(h2h.OFLTNR, h2h.ODATE, hub, h2h.ODEST);
                    }
                    else
                    {
                        flightInfoF = new OSUSR_UUK_FLT_INFO();
                    }
                    BagTMLog.LogDebug("BagTM Engine Search FlightInfoF", flightInfoF);

                    if (PAXCalculate != null && PAXCalculate.IFLTNR != null)
                    {
                        // To update inbound NRBAGS count for Hub messages
                        h2h.FIRST    = PAXCalculate.SumFIRST;
                        h2h.BUSINESS = PAXCalculate.SumBUSINESS;
                        h2h.ECONOMY  = PAXCalculate.SumECONOMY;

                        h2h.ETA = (flightInfoI != null && flightInfoI.DOOROPEN != null && flightInfoI.DOOROPEN.Value.Year != 1900) ? flightInfoI.DOOROPEN :
                                  (flightInfoI != null && flightInfoI.ATA != null && flightInfoI.ATA.Value.Year != 1900) ? flightInfoI.ATA :
                                  (flightInfoI != null && flightInfoI.ETA != null && flightInfoI.ETA.Value.Year != 1900) ? flightInfoI.ETA : null;
                        h2h.GATE = (flightInfoF != null && flightInfoF.FROM_GATE != null) ? flightInfoF.FROM_GATE : null;
                        h2h.ETD  = (flightInfoF != null && flightInfoF.DOORCLOSED != null && flightInfoF.DOORCLOSED.Value.Year != 1900) ? flightInfoF.DOORCLOSED :
                                   (flightInfoF != null && flightInfoF.ATD != null && flightInfoF.ATD.Value.Year != 1900) ? flightInfoF.ATD :
                                   (flightInfoF != null && flightInfoF.ETD != null && flightInfoF.ETD.Value.Year != 1900) ? flightInfoF.ETD :
                                   (flightInfoF != null && flightInfoF.STD != null && flightInfoF.STD.Value.Year != 1900) ? flightInfoF.STD : null;
                        h2h.STAND      = (flightInfoI != null && flightInfoI.TO_STAND != null) ? flightInfoI.TO_STAND : null;
                        h2h.STATUS     = PAXCalculate.RESERVATIONSTATUS;
                        h2h.IFLTINFOID = (flightInfoI != null) ? (System.Nullable <int>)flightInfoI.ID : null;
                        h2h.OFLTINFOID = (flightInfoF != null) ? (System.Nullable <int>)flightInfoF.ID : null;

                        System.Nullable <DateTime> etg = null;
                        if (flightInfoI != null)
                        {
                            if (flightInfoI.DOOROPEN != null)
                            {
                                etg = flightInfoI.DOOROPEN;
                            }
                            else if (flightInfoI.ATA != null)
                            {
                                etg = flightInfoI.ATA;
                            }
                            else if (flightInfoI.ETA != null)
                            {
                                etg = flightInfoI.ETA;
                            }
                        }

                        // Obtain security times from table
                        OSUSR_UUK_PTMREFS2G refStandGateTime = refStandGateTimes.Where(
                            s => s.GATE == h2h.GATE &&
                            s.STAND == h2h.STAND).FirstOrDefault();

                        int standGateMinutes = (refStandGateTime != null && refStandGateTime.MINUTES != null) ? (int)refStandGateTime.MINUTES / 2 : 0;

                        if (refStandGateTime != null && SEC_Y.Equals(refStandGateTime.SEC))
                        {
                            OSUSR_UUK_PTM_REF_SEC_TIME secTime = secTimes.Where(
                                s => s.WEEK == ((DateTime)h2h.IDATE).DayOfWeek.ToString() &&
                                s.FROM_TIME > ((DateTime)etg).AddMinutes(standGateMinutes))
                                                                 .FirstOrDefault();
                            h2h.SEC = (secTime != null && secTime.TIME != null) ? standGateMinutes + (int)secTime.TIME : standGateMinutes;
                        }
                        else
                        {
                            h2h.SEC = standGateMinutes;
                        }
                        if (h2h.ETD != null)
                        {
                            h2h.ETCG = h2h.ETD;
                            h2h.ETCG.Value.AddMinutes(-1 * etcg);
                        }
                        h2h.ETG = h2h.ETA;
                        h2h.ETG = (h2h.ETG != null) ? (System.Nullable <DateTime>)((DateTime)h2h.ETG).AddMinutes(
                            Math.Min((h2h.SEC != null) ? (int)h2h.SEC : 0, this.maxPaxTurnaround)) : null;
                        h2h.HUB = (h2h.ETD != null && h2h.ETG != null) ? (System.Nullable <int>)((TimeSpan)(h2h.ETD - h2h.ETG)).TotalMinutes : null;

                        BagTMLog.LogInfo("BagTM Engine H2H Update: " + h2h.ID, this);
                    }
                    else
                    {
                        // To update inbound NRBAGS count for Hub messages
                        h2h.FIRST    = 0;
                        h2h.BUSINESS = 0;
                        h2h.ECONOMY  = 0;

                        h2h.ETA    = (flightInfoI != null && flightInfoI.ETA != null) ? flightInfoI.ETA : null;
                        h2h.GATE   = (flightInfoF != null && flightInfoF.FROM_GATE != null) ? flightInfoF.FROM_GATE : null;
                        h2h.ETD    = (flightInfoF != null && flightInfoF.ETD != null) ? flightInfoF.ETD : null;
                        h2h.STAND  = (flightInfoI != null && flightInfoI.TO_STAND != null) ? flightInfoI.TO_STAND : null;
                        h2h.STATUS = null;

                        h2h.SEC  = 0;
                        h2h.ETG  = null;
                        h2h.ETCG = null;
                        h2h.HUB  = null;
                    }

                    db.OSUSR_UUK_PTM_H2H.Attach(h2h);
                    db.Entry(h2h).State = EntityState.Modified;
                    BagTMLog.LogInfo("BagTM Engine PTM H2H Update: " + h2h.ID, this);

                    ptmList.Remove(h2hExists);
                }

                OSUSR_UUK_PTM_H2H newH2H = null;
                foreach (var ptmNotIn in ptmList)
                {
                    flightInfoF = null;

                    // Verify if passengers in connection with a FI flight
                    if (ptmNotIn.OFLTNR != null && !ptmNotIn.OFLTNR.StartsWith(this.airline))
                    {
                        continue;
                    }

                    newH2H = new OSUSR_UUK_PTM_H2H();

                    var PAXCalculate = ptmQuery.Where(s => s.IFLTNR == ptmNotIn.IFLTNR &&
                                                      s.IDATE == ptmNotIn.IDATE &&
                                                      s.IORIGIN == ptmNotIn.IORIGIN &&
                                                      s.OFLTNR == ptmNotIn.OFLTNR &&
                                                      s.ODATE == ptmNotIn.ODATE &&
                                                      s.ODEST == ptmNotIn.ODEST &&
                                                      s.RESERVATIONSTATUS == ptmNotIn.RESERVATIONSTATUS)
                                       .GroupBy(x => new
                    {
                        x.IFLTNR,
                        x.IDATE,
                        x.IORIGIN,
                        x.OFLTNR,
                        x.ODATE,
                        x.ODEST,
                        x.RESERVATIONSTATUS
                    })
                                       .Select(x => new
                    {
                        IFLTNR            = x.Key.IFLTNR,
                        IDATE             = x.Key.IDATE,
                        IORIGIN           = x.Key.IORIGIN,
                        OFLTNR            = x.Key.OFLTNR,
                        ODATE             = x.Key.ODATE,
                        ODEST             = x.Key.ODEST,
                        RESERVATIONSTATUS = x.Key.RESERVATIONSTATUS,
                        SumFIRST          = x.Sum(z => z.FIRST),
                        SumBUSINESS       = x.Sum(z => z.BUSINESS),
                        SumECONOMY        = x.Sum(z => z.ECONOMY)
                    }
                                               ).FirstOrDefault();

                    BagTMLog.LogDebug("BagTM Engine Update H2H Don't Exist", PAXCalculate);

                    if (PAXCalculate != null && PAXCalculate.IFLTNR != null)
                    {
                        // To update inbound NRBAGS count for Hub messages
                        newH2H.FIRST    = PAXCalculate.SumFIRST;
                        newH2H.BUSINESS = PAXCalculate.SumBUSINESS;
                        newH2H.ECONOMY  = PAXCalculate.SumECONOMY;
                    }
                    else
                    {
                        newH2H.FIRST    = 0;
                        newH2H.BUSINESS = 0;
                        newH2H.ECONOMY  = 0;
                    }

                    if (ptmNotIn.OFLTNR != null && ptmNotIn.ODATE != null)
                    {
                        flightInfoF = this.SearchFlightInfo(ptmNotIn.OFLTNR, ptmNotIn.ODATE, hub, ptmNotIn.ODEST);
                    }
                    else
                    {
                        flightInfoF = new OSUSR_UUK_FLT_INFO();
                    }
                    BagTMLog.LogDebug("BagTM Engine Search FlightInfoF", flightInfoF);

                    newH2H.IFLTNR  = ptmNotIn.IFLTNR;
                    newH2H.IDATE   = ptmNotIn.IDATE;
                    newH2H.IORIGIN = ptmNotIn.IORIGIN;
                    newH2H.OFLTNR  = ptmNotIn.OFLTNR;
                    newH2H.ODATE   = ptmNotIn.ODATE;
                    newH2H.ODEST   = ptmNotIn.ODEST;

                    newH2H.ETA = (flightInfoI != null && flightInfoI.DOOROPEN != null && flightInfoI.DOOROPEN.Value.Year != 1900) ? flightInfoI.DOOROPEN :
                                 (flightInfoI != null && flightInfoI.ATA != null && flightInfoI.ATA.Value.Year != 1900) ? flightInfoI.ATA :
                                 (flightInfoI != null && flightInfoI.ETA != null && flightInfoI.ETA.Value.Year != 1900) ? flightInfoI.ETA : null;
                    newH2H.GATE = (flightInfoF != null && flightInfoF.FROM_GATE != null) ? flightInfoF.FROM_GATE : null;
                    newH2H.ETD  = (flightInfoF != null && flightInfoF.DOORCLOSED != null && flightInfoF.DOORCLOSED.Value.Year != 1900) ? flightInfoF.DOORCLOSED :
                                  (flightInfoF != null && flightInfoF.ATD != null && flightInfoF.ATD.Value.Year != 1900) ? flightInfoF.ATD :
                                  (flightInfoF != null && flightInfoF.ETD != null && flightInfoF.ETD.Value.Year != 1900) ? flightInfoF.ETD :
                                  (flightInfoF != null && flightInfoF.STD != null && flightInfoF.STD.Value.Year != 1900) ? flightInfoF.STD : null;
                    newH2H.STAND      = (flightInfoI != null && flightInfoI.TO_STAND != null) ? flightInfoI.TO_STAND : null;
                    newH2H.STATUS     = ptmNotIn.RESERVATIONSTATUS;
                    newH2H.IFLTINFOID = (flightInfoI != null) ? (System.Nullable <int>)flightInfoI.ID : null;
                    newH2H.OFLTINFOID = (flightInfoF != null) ? (System.Nullable <int>)flightInfoF.ID : null;

                    System.Nullable <DateTime> etcg = null;
                    if (flightInfoI != null)
                    {
                        if (flightInfoI.DOOROPEN != null && flightInfoI.DOOROPEN.Value.Year != 1900)
                        {
                            etcg = flightInfoI.DOOROPEN;
                        }
                        else if (flightInfoI.ATA != null && flightInfoI.ATA.Value.Year != 1900)
                        {
                            etcg = flightInfoI.ATA;
                        }
                        else if (flightInfoI.ETA != null && flightInfoI.ETA.Value.Year != 1900)
                        {
                            etcg = flightInfoI.ETA;
                        }
                    }

                    // Obtain security times from table
                    OSUSR_UUK_PTMREFS2G refStandGateTime = refStandGateTimes.Where(
                        s => s.GATE == newH2H.GATE &&
                        s.STAND == newH2H.STAND).FirstOrDefault();

                    int standGateMinutes = (refStandGateTime != null && refStandGateTime.MINUTES != null) ? (int)refStandGateTime.MINUTES / 2 : 0;

                    if (refStandGateTime != null && SEC_Y.Equals(refStandGateTime.SEC))
                    {
                        OSUSR_UUK_PTM_REF_SEC_TIME secTime = secTimes.Where(
                            s => s.WEEK == ((DateTime)newH2H.IDATE).DayOfWeek.ToString() &&
                            s.FROM_TIME > ((DateTime)flightInfoF.ETA).AddMinutes(standGateMinutes)).FirstOrDefault();
                        newH2H.SEC = (secTime != null && secTime.TIME != null) ? standGateMinutes + (int)secTime.TIME : standGateMinutes;
                    }
                    else
                    {
                        newH2H.SEC = standGateMinutes;
                    }
                    newH2H.ETG = newH2H.ETA;
                    newH2H.ETG = (newH2H.ETG != null) ? (System.Nullable <DateTime>)((DateTime)newH2H.ETG).AddMinutes(
                        Math.Min((newH2H.SEC != null) ? (int)newH2H.SEC : 0, this.maxPaxTurnaround)) : null;
                    newH2H.HUB = (newH2H.ETD != null && newH2H.ETG != null) ? (System.Nullable <int>)((TimeSpan)(newH2H.ETD - newH2H.ETG)).TotalMinutes : null;

                    try
                    {
                        db.OSUSR_UUK_PTM_H2H.Add(newH2H);
                        BagTMLog.LogInfo("BagTM Engine PTM H2H Create: " + newH2H.ID, this);
                    }
                    catch (DbEntityValidationException ex)
                    {
                        db.OSUSR_UUK_PTM_H2H.Remove(newH2H);
                        // Retrieve the error messages as a list of strings.
                        var errorMessages = ex.EntityValidationErrors
                                            .SelectMany(x => x.ValidationErrors)
                                            .Select(x => x.ErrorMessage);

                        // Join the list to a single string.
                        var fullErrorMessage = string.Join("; ", errorMessages);

                        // Combine the original exception message with the new one.
                        var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                        // Throw a new DbEntityValidationException with the improved exception message.
                        throw new EngineProcessingException(exceptionMessage);
                    }
                }

                try
                {
                    db.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    // Retrieve the error messages as a list of strings.
                    var errorMessages = ex.EntityValidationErrors
                                        .SelectMany(x => x.ValidationErrors)
                                        .Select(x => x.ErrorMessage);

                    // Join the list to a single string.
                    var fullErrorMessage = string.Join("; ", errorMessages);

                    // Combine the original exception message with the new one.
                    var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                    // Throw a new DbEntityValidationException with the improved exception message.
                    throw new EngineProcessingException(exceptionMessage);
                }
            }
            catch (Exception e)
            {
                BagTMLog.LogError("BagTM Engine Error updating PTM H2H Table", this, e);

                throw e;
            }

            BagTMLog.LogDebug("BagTM Engine Update PTM H2H End", this);
        }
Esempio n. 2
0
        /// <summary>
        /// Update H2H table based on flight information
        /// </summary>
        public void EngineUpdateH2H(OSUSR_UUK_FLT_INFO flightInfoI, OSUSR_UUK_FLT_INFO flightInfoF, bool isHub)
        {
            BagTMLog.LogDebug("BagTM Engine Update H2H Start", this);

            try
            {
                BagTMLog.LogDebug("BagTM Engine Update H2H FlightInfoI", flightInfoI);
                BagTMLog.LogDebug("BagTM Engine Update H2H FlightInfoF", flightInfoF);
                BagTMLog.LogDebug("BagTM Engine Update H2H Hub", this.hub);

                BagTMLog.LogDebug("BagTM Engine Update Search H2H", this);

                String iFLTNR = null;
                System.Nullable <DateTime> iDATE = null;
                String iORIGIN = null;
                String iDEST   = null;
                String fFLTNR  = null;
                System.Nullable <DateTime> fDATE = null;
                String fORIGIN = null;
                String fDEST   = null;

                iFLTNR = (flightInfoI != null) ?
                         CommonFunctions.FormatFlightNumber(flightInfoI.OPERATOR + flightInfoI.FLT_NR) : null;
                iDATE   = (flightInfoI != null) ? flightInfoI.STD : null;
                iORIGIN = (flightInfoI != null) ? flightInfoI.FROM_IATA : null;
                iDEST   = (flightInfoI != null) ? flightInfoI.TO_IATA : null;
                fFLTNR  = (flightInfoF != null) ?
                          CommonFunctions.FormatFlightNumber(flightInfoF.OPERATOR + flightInfoF.FLT_NR) : null;
                fDATE   = (flightInfoF != null) ? flightInfoF.STD : null;
                fORIGIN = (flightInfoF != null) ? flightInfoF.FROM_IATA : null;
                fDEST   = (flightInfoF != null) ? flightInfoF.TO_IATA : null;

                OSUSR_UUK_H2H h2h = this.SearchH2HyByFlightPair(
                    iFLTNR, iDATE, iORIGIN, fFLTNR, fDATE, fORIGIN);

                BagTMLog.LogDebug("BagTM Engine Update H2H List", h2h);

                BaggageEntities dbinup = new BaggageEntities();

                var bagQuery = from s in dbinup.OSUSR_UUK_BAGINTEG
                               select s;

                var bagList = bagQuery.Where(s => s.IFLTNR == iFLTNR &&
                                             DbFunctions.TruncateTime(s.IDATE) == DbFunctions.TruncateTime(iDATE) &&
                                             s.IORIGIN == iORIGIN &&
                                             (s.IAUT != BagTMRulesActions.PAX_AUT_STATUS_CODE ||
                                              s.IAUT != BagTMRulesActions.BAG_GROUP_AUT_CODE))
                              .ToList();

                BagTMLog.LogDebug("BagTM Engine Update Calculate BagIntegrity Bag Count", bagList);

                var bagCalculate = (from bi in bagList
                                    // here I choose each field I want to group by
                                    group bi by new { bi.IFLTNR, bi.IDATE, bi.IORIGIN, bi.FFLTNR, bi.FDATE, bi.FDEST } into g
                                    select new
                {
                    IFLTNR = g.Key.IFLTNR,
                    IDATE = g.Key.IDATE,
                    IORIGIN = g.Key.IORIGIN,
                    OFLTNR = g.Key.FFLTNR,
                    ODATE = g.Key.FDATE,
                    ODEST = g.Key.FDEST,
                    Count = g.Count()
                }
                                    ).ToList();

                BagTMLog.LogDebug("BagTM Engine Update H2H Bag Calculate", bagCalculate);

                if (h2h != null)
                {
                    BagTMLog.LogDebug("BagTM Engine Update H2H Flight Process", h2h);

                    h2h.IDATE      = (flightInfoI != null) ? flightInfoI.STD : null;
                    h2h.IETA       = (flightInfoI != null) ? flightInfoI.ETA : null;
                    h2h.IATA       = (flightInfoI != null) ? flightInfoI.ATA : null;
                    h2h.ISTAND     = (flightInfoI != null) ? flightInfoI.TO_STAND : null;
                    h2h.HULLOPEN   = (flightInfoI != null) ? flightInfoI.DOOROPEN : null;
                    h2h.IFLTINFOID = (flightInfoI != null && flightInfoI.ID != 0) ? (System.Nullable <int>)flightInfoI.ID : null;
                    h2h.ODATE      = (flightInfoF != null) ? flightInfoF.STD : null;
                    h2h.ETD        = (flightInfoF != null) ? flightInfoF.ETD : null;
                    h2h.ATD        = (flightInfoF != null) ? flightInfoF.ATD : null;
                    h2h.STANDD     = (flightInfoF != null) ? flightInfoF.FROM_STAND : null;
                    h2h.HULLCLOSE  = (flightInfoF != null) ? flightInfoF.DOORCLOSED : null;
                    h2h.OFLTINFOID = (flightInfoF != null && flightInfoF.ID != 0) ? (System.Nullable <int>)flightInfoF.ID : null;

                    var calcBaggageFlightIF = bagCalculate.Where(s => s.IFLTNR == h2h.IFLTNR &&
                                                                 s.IDATE == ((h2h.IDATE != null) ? ((DateTime)h2h.IDATE).Date : h2h.IDATE) &&
                                                                 s.IORIGIN == h2h.IORIGIN &&
                                                                 s.OFLTNR == h2h.OFLTNR &&
                                                                 s.ODATE == ((h2h.ODATE != null) ? ((DateTime)h2h.ODATE).Date : h2h.ODATE))
                                              .FirstOrDefault();

                    var calcBaggageFlightI = bagCalculate.Where(s => s.IFLTNR == h2h.IFLTNR &&
                                                                s.IDATE == ((h2h.IDATE != null) ? ((DateTime)h2h.IDATE).Date : h2h.IDATE) &&
                                                                s.IFLTNR != null)
                                             .GroupBy(s => new { s.IFLTNR, s.IDATE })
                                             .Select(r => new
                    {
                        FLIGHT      = r.Key.IFLTNR,
                        DATE        = r.Key.IDATE,
                        SUMBAGGAGES = r.Sum(x => x.Count)
                    }).FirstOrDefault();

                    BagTMLog.LogDebug("BagTM Engine Update H2H Exist", calcBaggageFlightIF);
                    if (calcBaggageFlightIF != null && calcBaggageFlightIF.Count > 0)
                    {
                        // To update inbound NRBAGS count for Hub messages
                        if (h2h.IFLTNR != null)
                        {
                            h2h.INRBAGS  = calcBaggageFlightIF.Count;
                            h2h.OLNRBAGS = 0;
                        }
                        else
                        {
                            h2h.INRBAGS  = 0;
                            h2h.OLNRBAGS = calcBaggageFlightIF.Count;
                        }
                        if (h2h.OFLTNR == null)
                        {
                            h2h.OLNRBAGS = 0;
                        }


                        BagTMLog.LogInfo("BagTM Engine H2H Update: " + h2h.ID, this);
                    }
                    else
                    {
                        // To update inbound NRBAGS count for Hub messages
                        if (isHub)
                        {
                            h2h.INRBAGS = 0;
                        }
                        else
                        {
                            h2h.OLNRBAGS = 0;
                        }
                    }

                    System.Nullable <int> inboundFlightEquip = null;

                    if (flightInfoI != null && flightInfoI.AC_REGISTRATION != null)
                    {
                        OSUSR_UUK_REGISTRATIONS reg = registrationList.Where(x => x.ACREGISTRATION == flightInfoI.AC_REGISTRATION)
                                                      .FirstOrDefault <OSUSR_UUK_REGISTRATIONS>();

                        inboundFlightEquip = (reg != null) ? reg.EQUIPTYPEID : (System.Nullable <int>)Convert.ToInt32(this.defaultEquipment);
                    }

                    System.Nullable <int> outboundFlightEquip = null;

                    if (flightInfoF != null && flightInfoF.AC_REGISTRATION != null)
                    {
                        OSUSR_UUK_REGISTRATIONS reg = registrationList.Where(x => x.ACREGISTRATION == flightInfoF.AC_REGISTRATION)
                                                      .FirstOrDefault <OSUSR_UUK_REGISTRATIONS>();

                        outboundFlightEquip = (reg != null) ? reg.EQUIPTYPEID : (System.Nullable <int>)Convert.ToInt32(this.defaultEquipment);
                    }

                    Decimal[] timesInbound = this.CalculateTimes(
                        (inboundFlightEquip != null) ? inboundFlightEquip : (System.Nullable <int>)Convert.ToInt32(this.defaultEquipment),
                        (flightInfoI != null) ? flightInfoI.TO_STAND : this.defaultStandFrom,
                        (flightInfoI != null) ? this.baggageTerminalCode : this.defaultGateTo);
                    Decimal[] timesOutbound = this.CalculateTimes(
                        (outboundFlightEquip != null) ? outboundFlightEquip : (System.Nullable <int>)Convert.ToInt32(this.defaultEquipment),
                        (flightInfoF != null) ? this.baggageTerminalCode : this.defaultStandFrom,
                        (flightInfoF != null) ? flightInfoF.FROM_GATE : this.defaultGateTo);

                    Double calcETU = 0;
                    Double calcETL = 0;

                    // Calculate times and insert if flight exists
                    if (h2h.IFLTNR != null)
                    {
                        h2h.IUNLOAD = (calcBaggageFlightI != null) ? timesInbound[0] * (Decimal)calcBaggageFlightI.SUMBAGGAGES
                                                                       : 0;
                        h2h.IINJECT = (calcBaggageFlightI != null) ? timesInbound[1] * (Decimal)calcBaggageFlightI.SUMBAGGAGES
                                                                       : 0;
                        h2h.ITAXI = (calcBaggageFlightI != null) ? timesInbound[3] : 0;

                        // Calculate ETU
                        calcETU = Convert.ToDouble(h2h.IUNLOAD + h2h.IINJECT + h2h.ITAXI);

                        calcETU = Math.Max(this.minLoadUnloadTime * 60 + Convert.ToDouble(h2h.IINJECT) + Convert.ToDouble(h2h.ITAXI), calcETU);
                        calcETU = Math.Min(this.maxLoadUnloadTime * 60 + Convert.ToDouble(h2h.IINJECT) + Convert.ToDouble(h2h.ITAXI), calcETU);

                        h2h.ETU = (h2h.HULLOPEN != null && h2h.HULLOPEN.Value.Year != 1900) ? h2h.HULLOPEN :
                                  (h2h.IATA != null && h2h.IATA.Value.Year != 1900) ? h2h.IATA :
                                  (h2h.IETA != null && h2h.IETA.Value.Year != 1900) ? h2h.IETA : null;

                        if (h2h.ETU != null)
                        {
                            h2h.ETU = ((DateTime)h2h.ETU).AddSeconds(Math.Min(calcETU, (double)this.maxBaggageTurnaround * 60));
                        }
                    }

                    if (h2h.OFLTNR != null)
                    {
                        h2h.OLOAD = (calcBaggageFlightIF != null) ? timesOutbound[2] * (Decimal)calcBaggageFlightIF.Count
                                                                     : 0;
                        h2h.OINJECT = (calcBaggageFlightIF != null) ? timesOutbound[1] * (Decimal)calcBaggageFlightIF.Count
                                                                    : 0;
                        h2h.OTAXI = (calcBaggageFlightIF != null) ? timesOutbound[3] : 0;

                        // Calculate ETL
                        calcETL = Convert.ToDouble(h2h.OLOAD + h2h.OINJECT + h2h.OTAXI);

                        calcETL = Math.Max(this.minLoadUnloadTime * 60 + Convert.ToDouble(h2h.OINJECT) + Convert.ToDouble(h2h.OTAXI), calcETL);
                        calcETL = Math.Min(this.maxLoadUnloadTime * 60 + Convert.ToDouble(h2h.OINJECT) + Convert.ToDouble(h2h.OTAXI), calcETL);

                        h2h.ETL = (h2h.HULLOPEN != null && h2h.HULLOPEN.Value.Year != 1900) ? h2h.HULLOPEN :
                                  (h2h.IATA != null && h2h.IATA.Value.Year != 1900) ? h2h.IATA :
                                  (h2h.IETA != null && h2h.IETA.Value.Year != 1900) ? h2h.IETA : null;

                        if (h2h.ETL != null)
                        {
                            // Calc final ETL time
                            calcETL  = calcETL + this.processSorterProcessingTimes((DateTime)h2h.ETL) * 60;
                            h2h.ETL  = ((DateTime)h2h.ETL).AddSeconds(Math.Min(calcETU + calcETL, 2 * (double)this.maxBaggageTurnaround * 60));
                            h2h.ETLU = ((DateTime)h2h.ETL).AddSeconds(calcETU + calcETL);
                        }
                    }

                    try
                    {
                        // Calculate Hub time
                        h2h.HUB = (h2h.ETL != null && h2h.ETD != null &&
                                   ((DateTime)h2h.ETL).Year > 1900 && ((DateTime)h2h.ETD).Year > 1900) ?
                                  (System.Nullable <int>)((TimeSpan)(h2h.ETD - h2h.ETL)).TotalMinutes : null;

                        BagTMLog.LogDebug("BagTM Engine Update to ", h2h);

                        dbinup.OSUSR_UUK_H2H.Attach(h2h);
                        dbinup.Entry(h2h).State = EntityState.Modified;

                        BagTMLog.LogInfo("BagTM Engine H2H Update: " + h2h.ID, this);
                    }
                    catch (DbEntityValidationException ex)
                    {
                        dbinup.OSUSR_UUK_H2H.Remove(h2h);
                        // Retrieve the error messages as a list of strings.
                        var errorMessages = ex.EntityValidationErrors
                                            .SelectMany(x => x.ValidationErrors)
                                            .Select(x => x.ErrorMessage);

                        // Join the list to a single string.
                        var fullErrorMessage = string.Join("; ", errorMessages);

                        // Combine the original exception message with the new one.
                        var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                        // Throw a new DbEntityValidationException with the improved exception message.
                        throw new EngineProcessingException(exceptionMessage);
                    }
                    catch (Exception e)
                    {
                        dbinup.OSUSR_UUK_H2H.Remove(h2h);

                        throw e;
                    }
                }
                else
                {
                    OSUSR_UUK_H2H newH2H = new OSUSR_UUK_H2H();
                    newH2H.IFLTNR     = iFLTNR;
                    newH2H.IORIGIN    = iORIGIN;
                    newH2H.IDATE      = iDATE;
                    newH2H.IETA       = (flightInfoI != null) ? flightInfoI.ETA : null;
                    newH2H.IATA       = (flightInfoI != null) ? flightInfoI.ATA : null;
                    newH2H.ISTAND     = (flightInfoI != null) ? flightInfoI.TO_STAND : null;
                    newH2H.HULLOPEN   = (flightInfoI != null) ? flightInfoI.DOOROPEN : null;
                    newH2H.IFLTINFOID = (flightInfoI != null && flightInfoI.ID != 0) ? (System.Nullable <int>)flightInfoI.ID : null;
                    newH2H.ONTOFLIGHT = fFLTNR;
                    newH2H.OFLTNR     = fFLTNR;
                    newH2H.ODATE      = fDATE;
                    newH2H.ETD        = (flightInfoF != null) ? flightInfoF.ETD : null;
                    newH2H.ATD        = (flightInfoF != null) ? flightInfoF.ATD : null;
                    newH2H.STANDD     = (flightInfoF != null) ? flightInfoF.FROM_STAND : null;
                    newH2H.HULLCLOSE  = (flightInfoF != null) ? flightInfoF.DOORCLOSED : null;
                    newH2H.OFLTINFOID = (flightInfoF != null && flightInfoF.ID != 0) ? (System.Nullable <int>)flightInfoF.ID : null;
                    // To update inbound NRBAGS count for Hub messages
                    if (newH2H.IFLTNR != null)
                    {
                        newH2H.INRBAGS  = 1;
                        newH2H.OLNRBAGS = 0;
                    }
                    else
                    {
                        newH2H.INRBAGS  = 0;
                        newH2H.OLNRBAGS = 1;
                    }
                    if (newH2H.OFLTNR == null)
                    {
                        newH2H.OLNRBAGS = 0;
                    }

                    System.Nullable <int> inboundFlightEquip = null;

                    if (flightInfoI != null && flightInfoI.AC_REGISTRATION != null)
                    {
                        OSUSR_UUK_REGISTRATIONS reg = registrationList.Where(x => x.ACREGISTRATION == flightInfoI.AC_REGISTRATION)
                                                      .FirstOrDefault <OSUSR_UUK_REGISTRATIONS>();

                        inboundFlightEquip = (reg != null) ? reg.EQUIPTYPEID : (System.Nullable <int>)Convert.ToInt32(this.defaultEquipment);
                    }

                    System.Nullable <int> outboundFlightEquip = null;

                    if (flightInfoF != null && flightInfoF.AC_REGISTRATION != null)
                    {
                        OSUSR_UUK_REGISTRATIONS reg = registrationList.Where(x => x.ACREGISTRATION == flightInfoI.AC_REGISTRATION)
                                                      .FirstOrDefault <OSUSR_UUK_REGISTRATIONS>();

                        outboundFlightEquip = (reg != null) ? reg.EQUIPTYPEID : (System.Nullable <int>)Convert.ToInt32(this.defaultEquipment);
                    }
                    Decimal[] timesInbound = this.CalculateTimes(
                        (inboundFlightEquip != null) ? inboundFlightEquip : (System.Nullable <int>)Convert.ToInt32(this.defaultEquipment),
                        (flightInfoI != null) ? flightInfoI.TO_STAND : this.defaultStandFrom,
                        (flightInfoI != null) ? this.baggageTerminalCode : this.defaultGateTo);
                    Decimal[] timesOutbound = this.CalculateTimes(
                        (outboundFlightEquip != null) ? outboundFlightEquip : (System.Nullable <int>)Convert.ToInt32(this.defaultEquipment),
                        (flightInfoF != null) ? this.baggageTerminalCode : this.defaultStandFrom,
                        (flightInfoF != null) ? flightInfoF.FROM_GATE : this.defaultGateTo);

                    Double calcETU = 0;
                    Double calcETL = 0;

                    // Calculate times and insert if flight exists
                    if (newH2H.IFLTNR != null)
                    {
                        newH2H.IUNLOAD = timesInbound[0];
                        newH2H.IINJECT = timesInbound[1];
                        newH2H.ITAXI   = timesInbound[3];

                        // Calculate ETU
                        calcETU = Convert.ToDouble(newH2H.IUNLOAD + newH2H.IINJECT + newH2H.ITAXI);

                        calcETU = Math.Max(this.minLoadUnloadTime * 60 + Convert.ToDouble(newH2H.IINJECT) + Convert.ToDouble(newH2H.ITAXI), calcETU);
                        calcETU = Math.Min(this.maxLoadUnloadTime * 60 + Convert.ToDouble(newH2H.IINJECT) + Convert.ToDouble(newH2H.ITAXI), calcETU);

                        newH2H.ETU = (newH2H.HULLOPEN != null && newH2H.HULLOPEN.Value.Year != 1900) ? newH2H.HULLOPEN :
                                     (newH2H.IATA != null && newH2H.IATA.Value.Year != 1900) ? newH2H.IATA :
                                     (newH2H.IETA != null && newH2H.IETA.Value.Year != 1900) ? newH2H.IETA : null;

                        if (newH2H.ETU != null)
                        {
                            newH2H.ETU = ((DateTime)newH2H.ETU).AddSeconds(Math.Min(calcETU, (double)this.maxBaggageTurnaround * 60));
                        }
                    }

                    if (newH2H.OFLTNR != null)
                    {
                        newH2H.OLOAD   = timesOutbound[2];
                        newH2H.OINJECT = timesOutbound[1];
                        newH2H.OTAXI   = timesOutbound[3];

                        // Calculate ETL
                        calcETL = Convert.ToDouble(newH2H.OLOAD + newH2H.OINJECT + newH2H.OTAXI);


                        calcETL = Math.Max(this.minLoadUnloadTime * 60 + Convert.ToDouble(newH2H.OINJECT) + Convert.ToDouble(newH2H.OTAXI), calcETL);
                        calcETL = Math.Min(this.maxLoadUnloadTime * 60 + Convert.ToDouble(newH2H.OINJECT) + Convert.ToDouble(newH2H.OTAXI), calcETL);


                        newH2H.ETL = (newH2H.HULLOPEN != null && newH2H.HULLOPEN.Value.Year != 1900) ? newH2H.HULLOPEN :
                                     (newH2H.IATA != null && newH2H.IATA.Value.Year != 1900) ? newH2H.IATA :
                                     (newH2H.IETA != null && newH2H.IETA.Value.Year != 1900) ? newH2H.IETA : null;

                        if (newH2H.ETL != null)
                        {
                            // Calc final ETL time
                            calcETL     = calcETL + this.processSorterProcessingTimes((DateTime)newH2H.ETL) * 60;
                            newH2H.ETL  = ((DateTime)newH2H.ETL).AddSeconds(Math.Min(calcETU + calcETL, 2 * (double)this.maxBaggageTurnaround * 60));
                            newH2H.ETLU = ((DateTime)newH2H.ETL).AddSeconds(calcETU + calcETL);
                        }
                    }

                    try
                    {
                        // Calculate Hub time
                        newH2H.HUB = (newH2H.ETL != null && newH2H.ETD != null &&
                                      ((DateTime)newH2H.ETL).Year > 1900 && ((DateTime)newH2H.ETD).Year > 1900) ?
                                     (System.Nullable <int>)((TimeSpan)(newH2H.ETD - newH2H.ETL)).TotalMinutes : null;

                        OSUSR_UUK_H2H h2hExist = this.SearchH2HInformation(
                            newH2H.IFLTNR, newH2H.IDATE, newH2H.IORIGIN, newH2H.OFLTNR, newH2H.ODATE, "");

                        if (h2hExist != null && h2hExist.IFLTNR != null)
                        {
                            EngineUpdateH2H(flightInfoI, flightInfoF, isHub);
                            BagTMLog.LogDebug("BagTM Engine Update H2H Already Exist", h2hExist);
                            return;
                        }
                        dbinup.OSUSR_UUK_H2H.Add(newH2H);

                        BagTMLog.LogInfo("BagTM Engine H2H Create: " + newH2H.ID, this);
                        BagTMLog.LogDebug("BagTM Engine Update H2H Create", newH2H);
                    }
                    catch (DbEntityValidationException ex)
                    {
                        dbinup.OSUSR_UUK_H2H.Remove(newH2H);
                        // Retrieve the error messages as a list of strings.
                        var errorMessages = ex.EntityValidationErrors
                                            .SelectMany(x => x.ValidationErrors)
                                            .Select(x => x.ErrorMessage);

                        // Join the list to a single string.
                        var fullErrorMessage = string.Join("; ", errorMessages);

                        // Combine the original exception message with the new one.
                        var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                        // Throw a new DbEntityValidationException with the improved exception message.
                        throw new EngineProcessingException(exceptionMessage);
                    }
                    catch (Exception e)
                    {
                        dbinup.OSUSR_UUK_H2H.Remove(newH2H);

                        throw e;
                    }
                }


                dbinup.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                                    .SelectMany(x => x.ValidationErrors)
                                    .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                // Throw a new DbEntityValidationException with the improved exception message.
                throw new EngineProcessingException(exceptionMessage);
            }

            BagTMLog.LogDebug("BagTM Engine Update H2H End", this);
        }
        public void UpdateBagIntegrity(OSUSR_UUK_BAGMSGS bag, OSUSR_UUK_BAGINTEG bagIntegrity, String hub, bool isHub, Int32 TTYChange)
        {
            BagTMLog.LogDebug("BagTM Engine Upodate Bag Integrity Start", this);

            BagTMLog.LogDebug("BagTM Engine Upodate Bag Integrity", bag);
            BagTMLog.LogDebug("BagTM Engine Upodate Bag Integrity", bagIntegrity);
            BagTMLog.LogDebug("BagTM Engine Upodate Bag Integrity", hub);
            BagTMLog.LogDebug("BagTM Engine Upodate Bag Integrity", isHub);
            BagTMLog.LogDebug("BagTM Engine Upodate Bag Integrity", TTYChange);
            bool isLocalTTYforHub = false;

            if (isHub && BagTMRulesActions.localBagParameter.Equals(bag.VBAGSOURCIND))
            {
                isLocalTTYforHub = true;
            }

            if (bag.NBAGTAG == null || bagIntegrity.NUMBER != bag.NBAGTAG + bag.NNRTAGS)
            {
                throw new EngineProcessingException(
                          String.Format("Message TTY {0} and BAGTAG {1} not processed due to incosistences.",
                                        bag.SMI, bag.NBAGTAG));
            }

            // Have to be defined what to do
            if (null != bagIntegrity.CLOSE)
            {
                bagIntegrity.CLOSE = null;
            }

            // Create different BagIntegrity record creation for Hub and Local TTY messages
            if (isHub)
            {
                // The message is a local message for Hub transit
                if (isLocalTTYforHub)
                {
                    if (bag.FFLTNR != null && !CommonFunctions.FormatFlightNumber(bag.FFLTNR).Equals(bagIntegrity.IFLTNR))
                    {
                        bagIntegrity.IFLTNR = CommonFunctions.FormatFlightNumber(bag.FFLTNR);
                    }
                    if (bag.FDATE != null && !bag.FDATE.Equals(bagIntegrity.IDATE))
                    {
                        bagIntegrity.IDATE = bag.FDATE;
                    }
                    if (bag.VCITY != null && !bag.VCITY.Equals(bagIntegrity.IORIGIN))
                    {
                        bagIntegrity.IORIGIN = bag.VCITY;
                    }
                    if (bag.SSEAT != null && !bag.SSEAT.Equals(bagIntegrity.ISEAT))
                    {
                        bagIntegrity.ISEAT = bag.SSEAT;
                    }
                    if (bag.OFLTNR != null && !CommonFunctions.FormatFlightNumber(bag.OFLTNR).Equals(bagIntegrity.FFLTNR))
                    {
                        bagIntegrity.FFLTNR = CommonFunctions.FormatFlightNumber(bag.OFLTNR);
                    }
                    if (bag.ODATE != null && !bag.ODATE.Equals(bagIntegrity.FDATE))
                    {
                        bagIntegrity.FDATE = bag.ODATE;
                    }
                    if (bag.ODEST != null && !bag.ODEST.Equals(bagIntegrity.FDEST))
                    {
                        bagIntegrity.FDEST = bag.ODEST;
                    }
                    if (BagTMRulesActions.SMI_BPM.Equals(bag.SMI))
                    {
                        bagIntegrity.BPMIN = BagTMRulesActions.YES;
                    }
                    if (BagTMRulesActions.PAX_BOARDED_STATUS.Equals(bag.SPAXCK))
                    {
                        bagIntegrity.BSMBOARDI = BagTMRulesActions.PAX_BOARDED_STATUS;
                    }

                    switch (TTYChange)
                    {
                    case BagTMRulesActions.BAG_TTY_CHANGE_AUTH:
                        bagIntegrity.IAUT = bag.SAUTL;
                        break;

                    case BagTMRulesActions.BAG_TTY_CHANGE_PAX_BOARDED:
                        bagIntegrity.BSMBOARDI = BagTMRulesActions.PAX_BOARDED_STATUS;
                        break;

                    case BagTMRulesActions.BAG_TTY_CHANGE_LOAD_FAIL:
                        bagIntegrity.BPMIN = BagTMRulesActions.PAX_BOARDED_FAIL;
                        break;

                    case BagTMRulesActions.BAG_TTY_CHANGE_BAGGAGE_OFFLOAD:
                        if (bagIntegrity.IFLTNR != null)
                        {
                            bagIntegrity.IAUT  = BagTMRulesActions.BAG_OFF;
                            bagIntegrity.BPMIN = BagTMRulesActions.BAG_OFF;
                        }
                        if (bagIntegrity.FFLTNR != null)
                        {
                            bagIntegrity.FAUT   = BagTMRulesActions.BAG_OFF;
                            bagIntegrity.BPMHUB = BagTMRulesActions.BAG_OFF;
                        }
                        break;

                    case BagTMRulesActions.BAG_TTY_CHANGE_PAX_CANCEL:
                        if (bagIntegrity.IFLTNR != null)
                        {
                            bagIntegrity.IAUT  = BagTMRulesActions.PAX_AUT_STATUS_CODE;
                            bagIntegrity.BPMIN = BagTMRulesActions.PAX_AUT_STATUS_CODE;
                        }
                        if (bagIntegrity.FFLTNR != null)
                        {
                            bagIntegrity.FAUT   = BagTMRulesActions.PAX_AUT_STATUS_CODE;
                            bagIntegrity.BPMHUB = BagTMRulesActions.PAX_AUT_STATUS_CODE;
                        }
                        break;
                    }
                    if (bag.PPAXNAME.Equals(BagTMRulesActions.BAGGAGE_CREW))
                    {
                        bagIntegrity.FAUT      = BagTMRulesActions.BAG_CREW_VALUE;
                        bagIntegrity.BPMIN     = BagTMRulesActions.BAG_CREW_VALUE;
                        bagIntegrity.BSMBOARDI = BagTMRulesActions.BAG_CREW_VALUE;
                    }
                }
                else
                {
                    if (bag.IFLTNR != null && !CommonFunctions.FormatFlightNumber(bag.IFLTNR).Equals(bagIntegrity.IFLTNR))
                    {
                        bagIntegrity.IFLTNR = CommonFunctions.FormatFlightNumber(bag.IFLTNR);
                    }
                    if (bag.IDATE != null && !bag.IDATE.Equals(bagIntegrity.IDATE))
                    {
                        bagIntegrity.IDATE = bag.IDATE;
                    }
                    if (bag.IORIGIN != null && !bag.IORIGIN.Equals(bagIntegrity.IORIGIN))
                    {
                        bagIntegrity.IORIGIN = bag.IORIGIN;
                    }
                    if (bag.SSEAT != null && !bag.SSEAT.Equals(bagIntegrity.FSEAT))
                    {
                        bagIntegrity.FSEAT = bag.SSEAT;
                    }
                    if (bag.FFLTNR != null && !CommonFunctions.FormatFlightNumber(bag.FFLTNR).Equals(bagIntegrity.FFLTNR))
                    {
                        bagIntegrity.FFLTNR = CommonFunctions.FormatFlightNumber(bag.FFLTNR);
                    }
                    if (bag.FDATE != null && !bag.FDATE.Equals(bagIntegrity.FDATE))
                    {
                        bagIntegrity.FDATE = bag.FDATE;
                    }
                    if (bag.FDEST != null && !bag.FDEST.Equals(bagIntegrity.FDEST))
                    {
                        bagIntegrity.FDEST = bag.FDEST;
                    }

                    if (BagTMRulesActions.SMI_BPM.Equals(bag.SMI))
                    {
                        bagIntegrity.BPMHUB = BagTMRulesActions.YES;
                    }
                    if (BagTMRulesActions.BAGGAGE_RUSH.Equals(bag.EEXCEP) || this.IsRush(bagIntegrity))
                    {
                        bagIntegrity.ISRUSH = BagTMRulesActions.BAGGAGE_RUSH_VALUE;
                    }
                    if (BagTMRulesActions.BAGGAGE_PRIO.Equals(bag.EEXCEP))
                    {
                        bagIntegrity.ISRUSH = BagTMRulesActions.BAGGAGE_PRIO_VALUE;
                    }
                    if (BagTMRulesActions.PAX_BOARDED_STATUS.Equals(bag.SPAXCK))
                    {
                        bagIntegrity.BSMBOARD = BagTMRulesActions.PAX_BOARDED_STATUS;
                    }

                    switch (TTYChange)
                    {
                    case BagTMRulesActions.BAG_TTY_CHANGE_AUTH:
                        bagIntegrity.FAUT = bag.SAUTL;
                        break;

                    case BagTMRulesActions.BAG_TTY_CHANGE_PAX_BOARDED:
                        bagIntegrity.BSMBOARD = BagTMRulesActions.PAX_BOARDED_STATUS;
                        break;

                    case BagTMRulesActions.BAG_TTY_CHANGE_LOAD_FAIL:
                        bagIntegrity.BPMHUB = BagTMRulesActions.PAX_BOARDED_FAIL;
                        break;

                    case BagTMRulesActions.BAG_TTY_CHANGE_BAGGAGE_OFFLOAD:
                        bagIntegrity.FAUT   = BagTMRulesActions.BAG_OFF;
                        bagIntegrity.BPMHUB = BagTMRulesActions.BAG_OFF;
                        break;

                    case BagTMRulesActions.BAG_TTY_CHANGE_PAX_CANCEL:
                        bagIntegrity.FAUT   = BagTMRulesActions.PAX_AUT_STATUS_CODE;
                        bagIntegrity.BPMHUB = BagTMRulesActions.PAX_AUT_STATUS_CODE;
                        break;
                    }
                    if (bag.PPAXNAME.Equals(BagTMRulesActions.BAGGAGE_CREW))
                    {
                        bagIntegrity.FAUT     = BagTMRulesActions.BAG_CREW_VALUE;
                        bagIntegrity.BPMHUB   = BagTMRulesActions.BAG_CREW_VALUE;
                        bagIntegrity.BSMBOARD = BagTMRulesActions.BAG_CREW_VALUE;
                    }
                }
            }
            else
            {
                if (bag.FFLTNR != null && !CommonFunctions.FormatFlightNumber(bag.FFLTNR).Equals(bagIntegrity.FFLTNR))
                {
                    bagIntegrity.FFLTNR = CommonFunctions.FormatFlightNumber(bag.FFLTNR);
                }
                if (bag.FDATE != null && !bag.FDATE.Equals(bagIntegrity.FDATE))
                {
                    bagIntegrity.FDATE = bag.FDATE;
                }
                if (bag.FDEST != null && !bag.FDEST.Equals(bagIntegrity.FDEST))
                {
                    bagIntegrity.FDEST = bag.FDEST;
                }
                if (bag.SSEAT != null && !bag.SSEAT.Equals(bagIntegrity.FSEAT))
                {
                    bagIntegrity.FSEAT = bag.SSEAT;
                }

                if (BagTMRulesActions.SMI_BPM.Equals(bag.SMI))
                {
                    bagIntegrity.BPMHUB = BagTMRulesActions.YES;
                }
                if (BagTMRulesActions.BAGGAGE_RUSH.Equals(bag.EEXCEP) || this.IsRush(bagIntegrity))
                {
                    bagIntegrity.ISRUSH = BagTMRulesActions.BAGGAGE_RUSH_VALUE;
                }
                if (BagTMRulesActions.BAGGAGE_PRIO.Equals(bag.EEXCEP))
                {
                    bagIntegrity.ISRUSH = BagTMRulesActions.BAGGAGE_PRIO_VALUE;
                }
                if (BagTMRulesActions.PAX_BOARDED_STATUS.Equals(bag.SPAXCK))
                {
                    bagIntegrity.BSMBOARD = BagTMRulesActions.PAX_BOARDED_STATUS;
                }

                switch (TTYChange)
                {
                case BagTMRulesActions.BAG_TTY_CHANGE_AUTH:
                    bagIntegrity.FAUT = bag.SAUTL;
                    break;

                case BagTMRulesActions.BAG_TTY_CHANGE_PAX_BOARDED:
                    bagIntegrity.BSMBOARD = BagTMRulesActions.PAX_BOARDED_STATUS;
                    break;

                case BagTMRulesActions.BAG_TTY_CHANGE_LOAD_FAIL:
                    bagIntegrity.BPMHUB = BagTMRulesActions.PAX_BOARDED_FAIL;
                    break;

                case BagTMRulesActions.BAG_TTY_CHANGE_BAGGAGE_OFFLOAD:
                    bagIntegrity.FAUT   = BagTMRulesActions.BAG_OFF;
                    bagIntegrity.BPMHUB = BagTMRulesActions.BAG_OFF;
                    break;

                case BagTMRulesActions.BAG_TTY_CHANGE_PAX_CANCEL:
                    bagIntegrity.FAUT   = BagTMRulesActions.PAX_AUT_STATUS_CODE;
                    bagIntegrity.BPMHUB = BagTMRulesActions.PAX_AUT_STATUS_CODE;
                    break;
                }
                if (bag.PPAXNAME.Equals(BagTMRulesActions.BAGGAGE_CREW))
                {
                    bagIntegrity.FAUT     = BagTMRulesActions.BAG_CREW_VALUE;
                    bagIntegrity.BPMHUB   = BagTMRulesActions.BAG_CREW_VALUE;
                    bagIntegrity.BSMBOARD = BagTMRulesActions.BAG_CREW_VALUE;
                }
            }

            // Change register for baggage groups
            if (!bagIntegrity.NUMBER.EndsWith(BagTMRulesActions.BAGGAGE_NRTAG_001))
            {
                bagIntegrity.FAUT  = (bagIntegrity.FAUT != null) ? BagTMRulesActions.BAG_GROUP_AUT_CODE : null;
                bagIntegrity.IAUT  = (bagIntegrity.IAUT != null) ? BagTMRulesActions.BAG_GROUP_AUT_CODE : null;
                bagIntegrity.BPMIN = (bagIntegrity.BPMIN != null) ? BagTMRulesActions.BAG_GROUP_BPM_CODE : null;
                bagIntegrity.IAUT  = (bagIntegrity.BPMHUB != null) ? BagTMRulesActions.BAG_GROUP_BPM_CODE : null;
            }

            BaggageEntities dbup = new BaggageEntities();

            try
            {
                BagTMLog.LogDebug("BagTM Engine Upodate Bag Integrity Saving", bagIntegrity);

                // Verifies if updating last baggage integrity record, if more then one remove all
                OSUSR_UUK_BAGINTEG lastBaggageIntegraty = this.LastBaggageIntegraty(bagIntegrity);
                if (lastBaggageIntegraty != null && lastBaggageIntegraty.TIMESTAMP != null &&
                    lastBaggageIntegraty.TIMESTAMP > bagIntegrity.TIMESTAMP)
                {
                    this.UpdateBagIntegrity(bag, lastBaggageIntegraty, hub, isHub, TTYChange);
                }
                else
                {
                    if (lastBaggageIntegraty != null)
                    {
                        bagIntegrity = MergeBagIntegrity(bagIntegrity, lastBaggageIntegraty);
                    }

                    bagIntegrity.TIMESTAMP = DateTime.Now;

                    dbup.OSUSR_UUK_BAGINTEG.Attach(bagIntegrity);
                    dbup.Entry(bagIntegrity).State = EntityState.Modified;
                    dbup.SaveChanges();
                }

                BagTMLog.LogInfo("BagTM Engine Update Bag Integrity: " + bagIntegrity.NUMBER, this);
            }
            catch (DbEntityValidationException ex)
            {
                dbup.OSUSR_UUK_BAGINTEG.Remove(bagIntegrity);

                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors;

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                // Throw a new DbEntityValidationException with the improved exception message.
                throw new EngineProcessingException(exceptionMessage);
            }
            catch (Exception e)
            {
                dbup.OSUSR_UUK_BAGINTEG.Remove(bagIntegrity);

                throw e;
            }

            BagTMLog.LogDebug("BagTM Engine Upodate Bag Integrity End", this);
        }