public void TestMethod2()
        {
            OpaExternal          opaSvc = new OpaExternal("uIndex", "Index0707Ws", "https://sws.export.com.gt/Desa/WS_OPA_2015/WSOPA.asmx");
            clsRespuestaDescargo r1 = new clsRespuestaDescargo(), r2 = new clsRespuestaDescargo(), r3 = new clsRespuestaDescargo();

            r1 = opaSvc.Validate("8851821-3", "274-6500634", 1, 9.578800, "915-6690865", 1);
        }
        public void TestMethod1()
        {
            OpaExternal          opaSvc = new OpaExternal("uIndex", "Index0707Ws", "https://sws.export.com.gt/WS_OPA_2015/WSOPA.asmx");
            clsRespuestaDescargo r1 = new clsRespuestaDescargo(), r2 = new clsRespuestaDescargo(), r3 = new clsRespuestaDescargo();

            r1 = opaSvc.Validate("4908811-4", "274-6504971", 1, 9.578800, "9157397314", 1);
            r2 = opaSvc.Validate("4908811-4", "274-6506473", 1, 16.800000, "9157397314", 1);
            r2 = opaSvc.Validate("4908811-4", "274-6507238", 1, 1713.600000, "9157397314", 1);
        }
        public clsRespuestaDescargo Transmit(String Nit, String ImportDocumentNumber, Int32 LineImportDocument, Double Quantity, String ExportDocumentNumber,
                                             Int32 LineExportDocument)
        {
            ImportDocumentNumber = ImportDocumentNumber.Replace("-", "");
            ExportDocumentNumber = ExportDocumentNumber.Replace("-", "");
            Nit = Nit.Replace("-", "");

            WSOPASoapClient opaservice = new WSOPASoapClient();

            opaservice.Endpoint.Address = new System.ServiceModel.EndpointAddress(new Uri(this.ServiceUrl));
            clsRespuestaDescargo result = opaservice.wmEnviarDescargo(this.UserName, this.Password, Nit, ImportDocumentNumber, LineImportDocument, Quantity,
                                                                      ExportDocumentNumber, LineExportDocument);

            return(result);
        }
        public static void Proccess(OpaExternal svc, ref Boolean BlockProccess, Int32 DelayValue, spg_Queue_Result document)
        {
            List <spg_QueueItems_Result> items  = Opa.GetDocumentItems(document.IdOpaHeader);
            List <spg_States_Result>     states = Opa.GetStates();

            Int32 IdStateTransmited = states.Where(y => y.Name == "Transmitido").FirstOrDefault().Id;
            Int32 IdStateQueue      = states.Where(y => y.Name == "Cola").FirstOrDefault().Id;
            Int32 IdStateProccess   = states.Where(y => y.Name == "Procesando").FirstOrDefault().Id;
            Int32 IdStateError      = states.Where(y => y.Name == "Error Transmisión").FirstOrDefault().Id;

            Int32 TotalItems      = items.Count;
            Int32 TotalTransmited = items.Where(x => x.IdState == IdStateTransmited).ToList().Count;
            Int32 TotalQueue      = items.Where(x => x.IdState == IdStateQueue).ToList().Count;
            Int32 TotalError      = items.Where(x => x.IdState == IdStateError).ToList().Count;
            Int32 ItemCounter     = 0;

            BlockProccess = true;
            Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + " " + items.Count + " registros a procesar.");

            if (TotalItems == TotalTransmited)
            {
                Opa.SetDocumentStateChange(document, IdStateTransmited);
                BlockProccess = false;
            }
            else if (TotalItems == TotalError)
            {
                Opa.SetDocumentStateChange(document, IdStateError);
                BlockProccess = false;
            }
            else
            {
                Opa.DocumentStartEnd(document, true);
                items.ForEach(x =>
                {
                    ItemCounter = ItemCounter + 1;
                    if (x.IdState == IdStateProccess)
                    {
                        clsRespuestaDescargo result = new clsRespuestaDescargo();
                        OpaResponse response        = new OpaResponse();
                        Opa.ItemStartEnd(x, true);

                        try
                        {
                            result = svc.Validate(x.Nit, x.IdDocumentStock, x.TransactionLineStock, (double)x.QuantitySubstract, x.IdDocumentSubstract, x.TransactionLineSubstract);

                            if (result.sCodRespuesta == "1")
                            {
                                result = svc.Transmit(x.Nit, x.IdDocumentStock, x.TransactionLineStock, (double)x.QuantitySubstract, x.IdDocumentSubstract, x.TransactionLineSubstract);
                            }

                            response = new OpaResponse()
                            {
                                Cif                         = (Decimal)result.dCifQ,
                                CustomDuties                = (Decimal)result.dDaiQ,
                                ErrorMessage                = result.sMensajeError,
                                ErrorType                   = result.sCodRespuesta,
                                ExtemporaneusMulct          = result.sRazonMultaExt,
                                ExtemporaneusMulctAmmount   = (Decimal)result.dMontoMultaExt,
                                IdOpaDetail                 = x.IdOpaDetail,
                                Iva                         = (Decimal)result.dIvaQ,
                                NationalizationMulct        = result.sRazonMultaNac,
                                NationalizationMulctAmmount = (Decimal)result.dMontoMultaNac,
                                TransactionNumber           = Convert.ToInt32(result.lOperacion),
                                ResponseDate                = DateTime.Now
                            };

                            String logMsg = null;
                            if (Convert.ToBoolean(ConfigurationManager.AppSettings["AlwaysTrue"].ToString()))
                            {
                                x.IdState = IdStateTransmited;
                                logMsg    = " transmitido";

                                response.Cif                         = x.CifSubstract;
                                response.CustomDuties                = x.CustomDutiesSubstract;
                                response.ErrorMessage                = "Transmisión exitosa.";
                                response.ErrorType                   = "1";
                                response.ExtemporaneusMulct          = result.sRazonMultaExt;
                                response.ExtemporaneusMulctAmmount   = (Decimal)result.dMontoMultaExt;
                                response.IdOpaDetail                 = x.IdOpaDetail;
                                response.Iva                         = x.IvaSubstract;
                                response.NationalizationMulct        = result.sRazonMultaNac;
                                response.NationalizationMulctAmmount = (Decimal)result.dMontoMultaNac;
                                response.TransactionNumber           = x.TransactionLineSubstract;
                                response.ResponseDate                = DateTime.Now;
                            }
                            else
                            {
                                x.IdState = (response.ErrorType == "1") ? IdStateTransmited : IdStateError;
                                logMsg    = (response.ErrorType == "1") ? " transmitido" : " con error";
                            }
                            Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + " Registro de detalle número " + x.IdOpaDetail + logMsg);
                            Opa.SetItemStateChange(x);
                            Opa.SetResponse(response);
                            Opa.ItemStartEnd(x, false);
                        }
                        catch (Exception ex)
                        {
                            String errorMsg = (ex.InnerException == null) ? ex.Message : ex.InnerException.Message;
                            response        = new OpaResponse()
                            {
                                Cif                         = x.CifSubstract,
                                CustomDuties                = x.CustomDutiesSubstract,
                                ErrorMessage                = errorMsg,
                                ErrorType                   = "0",
                                ExtemporaneusMulct          = result.sRazonMultaExt,
                                ExtemporaneusMulctAmmount   = (Decimal)result.dMontoMultaExt,
                                IdOpaDetail                 = x.IdOpaDetail,
                                Iva                         = x.IvaSubstract,
                                NationalizationMulct        = result.sRazonMultaNac,
                                NationalizationMulctAmmount = (Decimal)result.dMontoMultaNac,
                                TransactionNumber           = 0,
                                ResponseDate                = DateTime.Now
                            };

                            if (Convert.ToBoolean(ConfigurationManager.AppSettings["AlwaysTrue"].ToString()))
                            {
                                x.IdState                            = IdStateTransmited;
                                response.Cif                         = x.CifSubstract;
                                response.CustomDuties                = x.CustomDutiesSubstract;
                                response.ErrorMessage                = "Transmisión exitosa.";
                                response.ErrorType                   = "1";
                                response.ExtemporaneusMulct          = result.sRazonMultaExt;
                                response.ExtemporaneusMulctAmmount   = (Decimal)result.dMontoMultaExt;
                                response.IdOpaDetail                 = x.IdOpaDetail;
                                response.Iva                         = x.IvaSubstract;
                                response.NationalizationMulct        = result.sRazonMultaNac;
                                response.NationalizationMulctAmmount = (Decimal)result.dMontoMultaNac;
                                response.TransactionNumber           = 999;
                                response.ResponseDate                = DateTime.Now;
                                Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + " Registro de detalle número " + x.IdOpaDetail + " transmitido.");
                            }
                            else
                            {
                                x.IdState = IdStateError;
                                Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + " Registro de detalle número " + x.IdOpaDetail + " con error.");
                            }
                            Opa.SetItemStateChange(x);
                            Opa.SetResponse(response);
                            Opa.ItemStartEnd(x, false);
                        }

                        Thread.Sleep(DelayValue);
                    }
                });

                if (ItemCounter == TotalItems)
                {
                    items           = Opa.GetDocumentItems(document.IdOpaHeader);
                    TotalItems      = items.Count;
                    TotalTransmited = items.Where(x => x.IdState == IdStateTransmited).ToList().Count;
                    TotalQueue      = items.Where(x => x.IdState == IdStateQueue).ToList().Count;
                    TotalError      = items.Where(x => x.IdState == IdStateError).ToList().Count;

                    if (TotalItems == TotalTransmited)
                    {
                        Opa.SetDocumentStateChange(document, IdStateTransmited);
                    }
                    else if (TotalItems == TotalError)
                    {
                        Opa.SetDocumentStateChange(document, IdStateError);
                    }
                    else
                    {
                        Opa.SetDocumentStateChange(document, IdStateError);
                    }
                }

                try
                {
                    Opa.DocumentStartEnd(document, false);
                    Opa.SendNotification(document);
                    BlockProccess = false;
                }
                catch (Exception)
                {
                    BlockProccess = false;
                }
            }
        }