private bool IsVehicleIdVIP(CDatM1 m1, ref bool isVIP, long cpmArticleDef = GlobalDefs.DEF_UNDEFINED_VALUE)
        {
            trace.Write(TraceLevel.Info, ">>IsVehicleIdVIP");
            bool fnResult = true;

            try
            {
                isVIP = false;

                COPSPlate strPlate    = m1.GetInVehicleID();
                COPSDate  odDate      = m1.GetInDate().Copy();
                long      lGroup      = m1.GetInGroup();
                long      lArticleDef = GlobalDefs.DEF_UNDEFINED_VALUE;

                tariffCalculator.FillTree();
                bool isVehicleIdVIP = database.IsVehicleIdVIP(tariffCalculator.GetTree(), ref strPlate, odDate, lGroup, ref lArticleDef, ref isVIP);
                if (!isVehicleIdVIP)
                {
                    throw new InvalidOperationException("Error getting last operation group");
                }

                if (isVIP && lArticleDef != GlobalDefs.DEF_UNDEFINED_VALUE)
                {
                    m1.SetInArticleDef((int)lArticleDef);
                }
                else if (isVIP && lArticleDef == GlobalDefs.DEF_UNDEFINED_VALUE)
                {
                    isVIP = isVIP && lArticleDef == cpmArticleDef;
                }

                m1.SetOutIsVIP(isVIP);
            }
            catch (Exception error)
            {
                trace.Write(TraceLevel.Error, error.ToLogString());
                fnResult = false;
            }


            trace.Write(TraceLevel.Info, "<<IsVehicleIdVIP");
            return(fnResult);
        }
        private bool ComputeReturn(CDatM1 m1Return, CDatM1 m1)
        {
            trace.Write(TraceLevel.Debug, "ComputeReturn");
            bool   fnResult                 = true;
            CDatM1 M1ComputeTime            = null;
            CDatM1 M1ReturnWithCompleteHist = null;

            try
            {
                bool     bM1Plus      = false;
                int      nMaxQuantity = 0;
                COPSDate dt;
                COPSDate dt2;
                bool     bWholeOperationWithChipCard;
                bool     bWholeOperationWithMobile;

                M1ReturnWithCompleteHist = new CDatM1(loggerManager);
                m1Return.SetInOperType(OperationDat.DEF_OPERTYPE_PARK);
                M1ReturnWithCompleteHist.Copy(m1Return);

                m1Return.SetInHistOnlyWithSamePaymentType(true);

                if (!tariffCalculator.GetM1(m1Return, bM1Plus, nMaxQuantity))
                {
                    throw new InvalidOperationException("Error computing M1");
                }

                if (!tariffCalculator.GetM1(M1ReturnWithCompleteHist, bM1Plus, nMaxQuantity))
                {
                    throw new InvalidOperationException("Error computing M1");
                }

                if ((!m1Return.GetOutHistPostPay()) && (!M1ReturnWithCompleteHist.GetOutHistPostPay()))
                {
                    dt = m1Return.GetOutOperDateRealIni();
                    m1.SetOutOperDateIni0(dt);
                    m1.SetOutOperDateIni(dt);

                    dt2 = m1Return.GetOutOperDateIni();
                    if (dt2.GetStatus() == COPSDateStatus.Valid)
                    {
                        m1.SetOutOperDateEnd(dt2);
                    }
                    else
                    {
                        m1.SetOutOperDateEnd(dt);
                    }

                    m1.SetOutAccumulateMoney(m1Return.GetOutRealAccumulateMoney());
                    m1.SetOutAccumulateTime(m1Return.GetOutRealAccumulateTime());

                    m1.SetInIntervalOffsetMoney(M1ReturnWithCompleteHist.GetOutRealAccumulateMoney() - m1Return.GetOutRealAccumulateMoney());
                    m1.SetInIntervalOffsetMinutes(M1ReturnWithCompleteHist.GetOutRealAccumulateTime() - m1Return.GetOutRealAccumulateTime());

                    bWholeOperationWithChipCard = m1Return.GetOutWholeOperationWithChipCard() == 1;
                    bWholeOperationWithMobile   = m1Return.GetOutWholeOperationWithMobile() == 1;

                    if (!tariffCalculator.GetM1(m1, bM1Plus, nMaxQuantity))
                    {
                        throw new InvalidOperationException("Error computing Return M1");
                    }

                    if (m1.GetOutResult() <= 0)
                    {
                        m1.SetOutResult(TariffCalculator.M1_NO_RETURN);
                    }
                    else if ((m1.GetInPaymentType() != OperationDat.DEF_OPERPAY_CHIPCARD) && (m1.GetInPaymentType() != OperationDat.DEF_OPERPAY_MOBILE))
                    {
                        m1.SetOutResult(TariffCalculator.M1_NO_RETURN);
                    }
                    else if (m1.GetOutRetImport() <= 0)
                    {
                        m1.SetOutResult(TariffCalculator.M1_NO_RETURN);
                    }

                    if (m1.GetOutResult() > 0)
                    {
                        string    strUnit;
                        COPSPlate strVehicleId;

                        M1ComputeTime = new CDatM1(loggerManager);
                        M1ComputeTime.SetInUnit(m1.GetInUnit());
                        strUnit = m1.GetInUnit().ToString();
                        M1ComputeTime.SetSrc(strUnit);
                        dt = m1.GetDate();
                        M1ComputeTime.SetDate(dt);

                        M1ComputeTime.SetInOperType(OperationDat.DEF_OPERTYPE_PARK);        // DEFINES en operdat.h
                        M1ComputeTime.SetInDate(dt);

                        M1ComputeTime.SetInArticleDef(m1.GetInArticleDef());
                        M1ComputeTime.SetInGroup(m1.GetInGroup());
                        strVehicleId = m1.GetInVehicleID();
                        M1ComputeTime.SetInVehicleID(strVehicleId);

                        dt = m1Return.GetOutOperDateRealIni();

                        M1ComputeTime.SetInDate(dt);
                        M1ComputeTime.SetOutOperDateIni0(dt);
                        M1ComputeTime.SetOutOperDateIni(dt);

                        M1ComputeTime.SetOutMaxImport(m1.GetOutAccumulateMoney());
                        M1ComputeTime.SetInIntervalOffsetMoney(M1ReturnWithCompleteHist.GetOutRealAccumulateMoney() - m1Return.GetOutRealAccumulateMoney());
                        M1ComputeTime.SetInIntervalOffsetMinutes(M1ReturnWithCompleteHist.GetOutRealAccumulateTime() - m1Return.GetOutRealAccumulateTime());

                        if (!tariffCalculator.GetM1(M1ComputeTime, bM1Plus, nMaxQuantity, false))
                        {
                            throw new InvalidOperationException("Error computing Compute Time M1");
                        }

                        if (M1ComputeTime.GetOutResult() <= 0)
                        {
                            m1.SetOutResult(TariffCalculator.M1_NO_RETURN);
                        }
                        else
                        {
                            dt = M1ComputeTime.GetOutOperDateEnd();
                            m1.SetOutOperDateEnd(dt);

                            // Total parking time
                            m1.SetOutEfMaxTime(M1ComputeTime.GetOutEfMaxTime());
                        }
                    }
                }
                else
                {
                    m1.SetOutResult(TariffCalculator.M1_NO_RETURN);
                }
            }
            catch (Exception error)
            {
                trace.Write(TraceLevel.Error, error.ToLogString());
                fnResult = false;
            }

            return(fnResult);
        }
        private bool ComputeAmpliation(CDatM1 pM1, ref bool pExistCurrentOperationInOtherGroup)
        {
            trace.Write(TraceLevel.Debug, "ComputeAmpliation");
            bool fnResult = true;

            try
            {
                bool      bM1Plus      = false;
                int       nMaxQuantity = 0;
                COPSPlate strPlate;
                COPSDate  oDate;
                long      lCurrentArticleDef;
                long      lCurrentGroup;
                long      lAmpArticleDef = GlobalDefs.DEF_UNDEFINED_VALUE;
                long      lAmpGroup      = GlobalDefs.DEF_UNDEFINED_VALUE;
                bool      bIsVIP         = false;
                bool      bIsResident    = false;

                pExistCurrentOperationInOtherGroup = false;

                lCurrentArticleDef = pM1.GetInArticleDef();
                lCurrentGroup      = pM1.GetInGroup();
                strPlate           = pM1.GetInVehicleID();
                oDate = pM1.GetInDate().Copy();


                if (!database.GetVehicleLastOperationInfo(strPlate, oDate, ref lAmpArticleDef, ref lAmpGroup))
                {
                    throw new InvalidOperationException("Error getting last operation group");
                }

                if ((lAmpGroup != lCurrentGroup && lAmpGroup != GlobalDefs.DEF_UNDEFINED_VALUE) ||
                    (lAmpArticleDef != lCurrentArticleDef && lAmpArticleDef != GlobalDefs.DEF_UNDEFINED_VALUE)
                    )
                {
                    if (lAmpGroup != GlobalDefs.DEF_UNDEFINED_VALUE)
                    {
                        pM1.SetInGroup((int)lAmpGroup);
                    }

                    if (lAmpArticleDef != GlobalDefs.DEF_UNDEFINED_VALUE)
                    {
                        pM1.SetInArticleDef((int)lAmpArticleDef);
                    }

                    nMaxQuantity = (int)pM1.GetOutIntAcumul();

                    if (!tariffCalculator.GetM1(pM1, bM1Plus, nMaxQuantity))
                    {
                        throw new InvalidOperationException("Error computing Ampliation M1");
                    }

                    if (pM1.GetOutRealAccumulateMoney() > 0 && pM1.GetOutResult() > 0 && pM1.GetOutPostPay() == 0)
                    {
                        pExistCurrentOperationInOtherGroup = true;

                        if (!IsVehicleIdVIP(pM1, ref bIsVIP, pM1.GetInArticleDef()))
                        {
                            trace.Write(TraceLevel.Info, "ERROR : IsVehicleIdVIP");
                        }

                        if (!bIsVIP)
                        {
                            if (!IsVehicleIdResident(pM1, ref bIsResident, pM1.GetInArticleDef()))
                            {
                                trace.Write(TraceLevel.Info, "ERROR : IsVehicleIdResident");
                            }
                        }
                    }
                }
            }
            catch (Exception error)
            {
                trace.Write(TraceLevel.Error, error.ToLogString());
                fnResult = false;
            }

            return(fnResult);
        }
        public int fnM1(string pStrIn, string pRegEntry, ref string pStrOut, ref string pStrOutM50, int nSize, bool bApplyHistory, bool bUseDefaultArticleDef)
        {
            int nRes = 1;

            try
            {
                Guard.IsNullOrEmptyString(pStrIn, nameof(pStrIn));

                LoadParams(pRegEntry);
                trace.Write(TraceLevel.Debug, $"GeneralParams.UnitId loaded  {GeneralParams.UnitId}");

                CDatM1     m1        = new CDatM1(loggerManager);
                CDatM1     m1Return  = new CDatM1(loggerManager);
                CDatM1     m1Amp     = new CDatM1(loggerManager);
                COPSPacket opsPacket = new COPSPacket(loggerManager);
                bool       exitCurrentOperationInOtherGroup = false;

                trace.Write(TraceLevel.Info, "Waiting For Unlock .....");

                //TODO : Añadir en lock toda la seccion
                trace.Write(TraceLevel.Info, "Inside Lock .....");

                if (!OpenDatabase())
                {
                    throw new InvalidOperationException("Open database has failed");
                }

                if (!InitComputeM1())
                {
                    throw new InvalidOperationException("Compute1 initialization has failed");
                }

                tariffCalculator.SetDBB(database);

                opsPacket.Parse(pStrIn);
                if (opsPacket.GetMsg(0) == null)
                {
                    throw new InvalidOperationException("OPSPacket doesn't conatin any message");
                }

                if (!m1.SetData(opsPacket.GetMsg(0)))
                {
                    throw new InvalidOperationException("M1 Extracting data from OPSPacket");
                }

                if (m1.GetInArticleDef() == GlobalDefs.DEF_UNDEFINED_VALUE)
                {
                    trace.Write(TraceLevel.Debug, $"Set M1 InArticleDef from parameters");
                    m1.SetInArticleDef(defaultArticleDef);
                }

                if (tariffCalculator == null)
                {
                    throw new InvalidOperationException("TariffCalculator is NULL");
                }

                trace.Write(TraceLevel.Info, "FnM1 ");
                m1.Trace();

                if (!database.IsOpened())
                {
                    trace.Write(TraceLevel.Info, "Database is not connected");

                    if (!database.Close())
                    {
                        trace.Write(TraceLevel.Error, "ERROR Closing Database");
                    }

                    if (!OpenDatabase())
                    {
                        throw new InvalidOperationException("Open database has failed");
                    }
                }

                if (m1.GetInOperType() == OperationDat.DEF_OPERTYPE_RETN)
                {
                    if (!m1Return.SetData(opsPacket.GetMsg(0)))
                    {
                        throw new InvalidOperationException("M1RETURN Extracting data from OPSPacket");
                    }

                    if (m1Return.GetInArticleDef() == GlobalDefs.DEF_UNDEFINED_VALUE)
                    {
                        trace.Write(TraceLevel.Debug, $"Set M1RETURN InArticleDef from parameters");
                        m1.SetInArticleDef(defaultArticleDef);
                    }

                    if (!ComputeReturn(m1Return, m1))
                    {
                        trace.Write(TraceLevel.Info, $"ERROR: Computing Return");
                    }
                }
                else
                {
                    bool isVIP      = false;
                    bool isResident = false;

                    if (!bUseDefaultArticleDef)
                    {
                        if (!IsVehicleIdVIP(m1, ref isVIP))
                        {
                            trace.Write(TraceLevel.Error, "ERROR : IsVehicleIdVIP");
                        }

                        if (!isVIP)
                        {
                            if (!IsVehicleIdResident(m1, ref isResident))
                            {
                                trace.Write(TraceLevel.Error, "ERROR : IsVehicleIdResident");
                            }
                        }
                    }

                    // Modify tariffs for abonos in Zumarraga
                    trace.Write(TraceLevel.Info, "System ID %1", GeneralParams.SystemId.ToString());
                    if (GeneralParams.SystemId == SYSTEM_IDENTIFIER_ZUMARRAGA)
                    {
                        if (m1.GetInArticleDef() >= 102 && m1.GetInArticleDef() <= 113)
                        {
                            trace.Write(TraceLevel.Info, "Modifying tariffs for Zumarraga");
                            COPSPlate strVehicleId = m1.GetInVehicleID();

                            if (!database.SetTariffDates(tariffCalculator.GetTree(), m1.GetInArticleDef(), strVehicleId))
                            {
                                throw new InvalidOperationException("Setting tariff dates before calling M1");
                            }
                        }
                    }

                    bool bM1Plus      = false;
                    int  nMaxQuantity = (int)m1.GetOutIntAcumul();

                    if (!tariffCalculator.GetM1(m1, bM1Plus, nMaxQuantity, bApplyHistory))
                    {
                        trace.Write(TraceLevel.Info, "ERROR : GetM1");
                    }

                    m1AppliedHistory  = (tariffCalculator.GetApplyVehicleHistory()) ? 1: 0;
                    m1RealCurrMinutes = (int)tariffCalculator.GetRealCurrMinutes();

                    if (lookOtherGroups)
                    {
                        m1Amp = new CDatM1(loggerManager);
                        if (m1Amp == null)
                        {
                            throw new InvalidOperationException("ERROR : pM1 is NULL");
                        }

                        if (!m1Amp.SetData(opsPacket.GetMsg(0)))
                        {
                            throw new InvalidOperationException("ERROR : pM1 Extracting data from OPSPacket");
                        }

                        if (m1Amp.GetInArticleDef() == GlobalDefs.DEF_UNDEFINED_VALUE)
                        {
                            m1Amp.SetInArticleDef(defaultArticleDef);
                        }


                        if (!ComputeAmpliation(m1Amp, ref exitCurrentOperationInOtherGroup))
                        {
                            trace.Write(TraceLevel.Info, "ERROR : Computing Ampliation");
                        }
                    }
                }

                m1.Trace();

                string szOutMessage;
                string szOutMessageM50;

                //// Format Result Message
                szOutMessage = FormatOutMessage(m1, true, "A");
                if (exitCurrentOperationInOtherGroup && m1Amp != null)
                {
                    szOutMessage = FormatOutMessage(m1Amp, false, "B");
                }

                szOutMessageM50 = FormatOutMessageM50(m1);

                trace.Write(TraceLevel.Info, $"Message {szOutMessage}");
                trace.Write(TraceLevel.Info, $"Message 50 {szOutMessageM50}");

                pStrOut    = szOutMessage;
                pStrOutM50 = szOutMessageM50;

                ////// Format Result Message
                //XmlDocument xmlMessage = FormatOutMessage(m1, true, "A");
                //if (exitCurrentOperationInOtherGroup && m1Amp != null)
                //{
                //    xmlMessage = FormatOutMessage(m1Amp, false, "B", xmlMessage);
                //}

                //szOutMessage = xmlMessage.InnerXml;
                //xmlMessage.RemoveAll();

                //xmlMessage = FormatOutMessageM50(m1);
                //szOutMessageM50 = xmlMessage.InnerXml;

                //trace.Write(TraceLevel.Info, $"Message {szOutMessage}");
                //trace.Write(TraceLevel.Info, $"Message 50 {szOutMessageM50}");

                //pStrOut = szOutMessage;
                //pStrOutM50 = szOutMessageM50;
            }
            catch (LoadParametersException error)
            {
                trace.Write(TraceLevel.Error, error.ToLogString());
                nRes = -3;
            }
            catch (Exception error)
            {
                trace.Write(TraceLevel.Error, error.ToLogString());
                nRes = -1;
            }
            finally
            {
                if (!database.Close())
                {
                    trace.Write(TraceLevel.Error, "ERROR Closing Database");
                }
            }

            return(nRes);
        }