private Dictionary <string, string> ExtractM1DataToM50Message(CDatM1 m1)
        {
            trace.Write(TraceLevel.Info, ">>ExtractM1DataToM50Message");

            Dictionary <string, string> elements = new Dictionary <string, string>();

            try
            {
                elements.Add($"r", m1.GetOutResult().ToString());
                elements.Add($"q1", m1.GetOutMinImport().ToString());
                elements.Add($"q2", m1.GetOutMaxImport().ToString());
                elements.Add($"t", m1.GetOutEfMaxTime().ToString());
                elements.Add($"o", m1.GetInOperType().ToString());

                if (m1.GetOutOperDateEnd().GetStatus() != COPSDateStatus.Null)
                {
                    elements.Add($"d", m1.GetOutOperDateEnd().CopyToChar());
                }

                if (m1.GetOutOperDateIni().GetStatus() != COPSDateStatus.Null)
                {
                    elements.Add($"di", m1.GetOutOperDateIni().CopyToChar());
                }

                elements.Add($"g", m1.GetInGroup().ToString());
                elements.Add($"ad", m1.GetInArticleDef().ToString());
                elements.Add($"aq", m1.GetOutAccumulateMoney().ToString());
                elements.Add($"at", m1.GetOutAccumulateTime().ToString());
                elements.Add($"aqag", m1.GetOutAccumulateMoneyAllGroup().ToString());
                elements.Add($"atag", m1.GetOutAccumulateTimeAllGroup().ToString());

                if (m1.GetOutOperDateIni0().GetStatus() != COPSDateStatus.Null)
                {
                    elements.Add($"d0", m1.GetOutOperDateIni0().CopyToChar());
                }

                elements.Add($"q", m1.GetOutRetImport().ToString());

                if (m1.GetOutOperDateRealIni().GetStatus() != COPSDateStatus.Null)
                {
                    elements.Add($"dr0", m1.GetOutOperDateRealIni().CopyToChar());
                }

                elements.Add($"raq", m1.GetOutRealAccumulateMoney().ToString());
                elements.Add($"rat", m1.GetOutRealAccumulateTime().ToString());

                if (m1.GetOutWholeOperationWithChipCard() != 0)
                {
                    elements.Add($"chca", m1.GetOutWholeOperationWithChipCard().ToString());
                }

                if (m1.GetOutWholeOperationWithMobile() != 0)
                {
                    elements.Add($"mobi", m1.GetOutWholeOperationWithMobile().ToString());
                }

                if (m1.GetOutPostPay() != 0)
                {
                    elements.Add($"pp", m1.GetOutPostPay().ToString());
                }

                if (m1.GetOutIsResident() != 0)
                {
                    elements.Add($"resi", m1.GetOutIsResident().ToString());
                }

                if (m1.GetOutIsVIP() != 0)
                {
                    elements.Add($"vip", m1.GetOutIsVIP().ToString());
                }

                elements.Add($"his", m1AppliedHistory.ToString());
                elements.Add($"rot", m1RealCurrMinutes.ToString());
            }
            catch (Exception error)
            {
                trace.Write(TraceLevel.Error, error.ToLogString());
            }

            trace.Write(TraceLevel.Info, "<<ExtractM1DataToM50Message");

            return(elements);
        }
        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);
        }
        private Dictionary <string, string> ExtractM1DataToMessage(CDatM1 m1, string m1ID)
        {
            trace.Write(TraceLevel.Debug, ">>ExtractM1DataToMessage");

            Dictionary <string, string> elements = new Dictionary <string, string>();

            try
            {
                elements.Add($"{m1ID}r", m1.GetOutResult().ToString());

                if (m1.GetOutResult() > 0)
                {
                    elements.Add($"{m1ID}q1", m1.GetOutMinImport().ToString());
                    elements.Add($"{m1ID}q2", m1.GetOutMaxImport().ToString());

                    if (m1.GetInComputeTimeLimits())
                    {
                        elements.Add($"{m1ID}t1", m1.GetOutMinTime().ToString());
                        elements.Add($"{m1ID}t2", m1.GetOutMaxTime().ToString());

                        if (m1.GetOutMinOperDate().GetStatus() != COPSDateStatus.Null)
                        {
                            elements.Add($"{m1ID}d1", m1.GetOutMinOperDate().CopyToChar());
                        }

                        if (m1.GetOutMaxOperDate().GetStatus() != COPSDateStatus.Null)
                        {
                            elements.Add($"{m1ID}d2", m1.GetOutMaxOperDate().CopyToChar());
                        }
                    }

                    elements.Add($"{m1ID}t", m1.GetOutEfMaxTime().ToString());
                    elements.Add($"{m1ID}o", m1.GetInOperType().ToString());

                    if (m1.GetOutOperDateEnd().GetStatus() != COPSDateStatus.Null)
                    {
                        elements.Add($"{m1ID}d", m1.GetOutOperDateEnd().CopyToChar());
                    }

                    if (m1.GetOutOperDateIni().GetStatus() != COPSDateStatus.Null)
                    {
                        elements.Add($"{m1ID}di", m1.GetOutOperDateIni().CopyToChar());
                    }

                    elements.Add($"{m1ID}g", m1.GetInGroup().ToString());
                    elements.Add($"{m1ID}ad", m1.GetInArticleDef().ToString());
                    elements.Add($"{m1ID}aq", m1.GetOutAccumulateMoney().ToString());
                    elements.Add($"{m1ID}at", m1.GetOutAccumulateTime().ToString());
                    elements.Add($"{m1ID}aqag", m1.GetOutAccumulateMoneyAllGroup().ToString());
                    elements.Add($"{m1ID}atag", m1.GetOutAccumulateTimeAllGroup().ToString());

                    if (m1.GetOutOperDateIni0().GetStatus() != COPSDateStatus.Null)
                    {
                        elements.Add($"{m1ID}d0", m1.GetOutOperDateIni0().CopyToChar());
                    }

                    elements.Add($"{m1ID}q", m1.GetOutRetImport().ToString());

                    if (m1.GetOutOperDateRealIni().GetStatus() != COPSDateStatus.Null)
                    {
                        elements.Add($"{m1ID}dr0", m1.GetOutOperDateRealIni().CopyToChar());
                    }

                    elements.Add($"{m1ID}raq", m1.GetOutRealAccumulateMoney().ToString());
                    elements.Add($"{m1ID}rat", m1.GetOutRealAccumulateTime().ToString());

                    if (m1.GetOutWholeOperationWithChipCard() != 0)
                    {
                        elements.Add($"{m1ID}chca", m1.GetOutWholeOperationWithChipCard().ToString());
                    }

                    if (m1.GetOutWholeOperationWithMobile() != 0)
                    {
                        elements.Add($"{m1ID}mobi", m1.GetOutWholeOperationWithMobile().ToString());
                    }

                    if (m1.GetOutPostPay() != 0)
                    {
                        elements.Add($"{m1ID}pp", m1.GetOutPostPay().ToString());
                    }

                    if (m1.GetOutIsResident() != 0)
                    {
                        elements.Add($"{m1ID}resi", m1.GetOutIsResident().ToString());
                    }

                    if (m1.GetOutIsVIP() != 0)
                    {
                        elements.Add($"{m1ID}vip", m1.GetOutIsVIP().ToString());
                    }

                    string strOutStepsCalculationString = m1.GetOutStepsCalculationString();
                    if (strOutStepsCalculationString.Length > 0)
                    {
                        elements.Add($"{m1ID}cst", m1.GetOutStepsCalculationString());
                    }
                }
            }
            catch (Exception error)
            {
                trace.Write(TraceLevel.Error, error.ToLogString());
            }

            trace.Write(TraceLevel.Debug, "<<ExtractM1DataToMessage");

            return(elements);
        }