Esempio n. 1
0
        public static Client ToHandyClient(this CustomerSupplier value)
        {
            Client client = new Client();

            client.code        = value.cCodigoCliente;
            client.accuracy    = 10;
            client.description = value.cRazonSocial;
            client.comments    = "RFC: " + value.cRFC;
            client.enabled     = (value.cEstatus == 1) ? true : false;
            client.isNew       = false;
            client.discount    = value.cDescuentoMovto;
            //Las direcciones en Compaq se guardan por separado, es necesario volver a conectarse para buscar el domicilio
            Logger log = new Logger();  //Inicializa logs

            try
            {
                Directory.SetCurrentDirectory(ConfigProxy.AdminPaqPath); //Cambia al directorio de AdminPAQ para acceder al SDK
                int iResult = CompacSDK.fInicializaSDK();                // Inicialización del SDK

                Address address = new Address();                         //nuevo objeto para almacenar la dirección del cliente
                if (iResult == 0)
                {
                    iResult = CompacSDK.fAbreEmpresa(ConfigProxy.AdminPaqCompanyPath); //Apertura de la empresa
                    if (iResult == 0)
                    {
                        string agent = "";
                        iResult = AdminPaqService.Instance.GetAgent(value.cCodigoCliente, ref agent);

                        if (iResult == 0)
                        {
                            client.zone_description = agent;
                            log.Debug(value.cRazonSocial + " Agente encontrado: " + agent);
                        }
                        else
                        {
                            client.zone_description = "Zona General";
                            log.Warn(value.cRazonSocial + "No se pudo obtener agente. Error: " + iResult + ", " + CompacSDK.GetError(iResult));
                        }

                        iResult = AdminPaqService.Instance.GetAddress(ref address, value.cCodigoCliente, 0); //búsqueda de la dirección fiscal del cliente
                        if (iResult == 0)
                        {
                            client.address     = address.cNombreCalle + " " + address.cNumeroExterior + " " + address.cNumeroInterior + " " + address.cColonia + ", " + address.cEstado + ", " + address.cPais;
                            client.postalCode  = address.cCodigoPostal;
                            client.city        = address.cCiudad;
                            client.phoneNumber = address.cTelefono1;
                        }
                        else
                        {
                            log.Warn("Dirección no encontrada para cliente " + value.cCodigoCliente);
                        }
                    }
                    else
                    {
                        log.Warn("No se pudo abrir la empresa para buscar dirección y agente del cliente. Error: " + AdminPaqService.Instance.GetError(iResult));
                    }
                }
                else
                {
                    log.Error("No se pudo inicializar el SDK para obtener direcciones de clientes");
                }
            }
            catch (Exception ex)
            {
                log.Error("Error al tratar de obtener la dirección de un cliente: " + client.code + ". Exepción: " + ex);
                throw ex;
            }
            return(client);
        }
        public static void MainProcess2()
        {
            process = Process.GetCurrentProcess();
            try
            {
                ConfigProxy.OpenConfigFile();
            }
            catch (Exception ex)
            {
                log.Error("Unable to open config file: " + ex.Message);
                Console.ReadKey();
                return;
            }
            int waitTime = ConfigProxy.SyncPeriodMinutes * 60 * 1000;

            log.Debug("Starting service.");
            log.Trace("Initializing...");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");
            //Adding a higher level Exception handling in case the finally block throws an exception
            try
            {
                try
                {
                    //Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");
                    Directory.SetCurrentDirectory(ConfigProxy.AdminPaqPath);
                    string initialDate = (ConfigProxy.FirstSync) ? "01/01/1990 00:00:00.00" : ConfigProxy.LastSync;
                    int    iResult     = 0;
                    if (ConfigProxy.FirstSync)
                    {
                        log.Trace("First Sync");
                        ConfigProxy.FirstSync = false;
                    }
                    log.Info("Initializing Compac SDK.");
                    iResult = CompacSDK.fInicializaSDK();
                    log.Info("Memory usage: " + process.WorkingSet64);
                    if (iResult != 0)
                    {
                        throw new HandySyncException("Unable to initialize SDK. Check AdminPAQ path specified in config file.");
                    }

                    log.Info("Opening company with Compac SDK in Path: " + ConfigProxy.AdminPaqCompanyPath);
                    iResult = CompacSDK.fAbreEmpresa(ConfigProxy.AdminPaqCompanyPath);
                    log.Info("Memory usage: " + process.WorkingSet64);
                    if (iResult != 0)
                    {
                        throw new HandySyncException("Unable to open company files. Check AdminPAQ path specified in config file.");
                    }

                    log.Trace("Company opened successfully: " + ConfigProxy.AdminPaqCompanyPath);

                    //Sync Products
                    if (ConfigProxy.SyncProducts)
                    {
                        SyncProducts(initialDate);
                    }
                    else
                    {
                        log.Info("Products didn't sync because the application is not configured to do so. ");
                    }
                    log.Info("Memory usage: " + process.WorkingSet64);
                    //Sync Clients
                    if (ConfigProxy.SyncClients)
                    {
                        SyncClients(initialDate);
                    }
                    else
                    {
                        log.Info("Clients didn't sync because the application is not configured to do so. ");
                    }
                    log.Info("Memory usage: " + process.WorkingSet64);
                    //Sync Orders
                    if (ConfigProxy.SyncOrders)
                    {
                        SyncOrders(initialDate);
                    }
                    else
                    {
                        log.Info("Orders didn't sync because the application is not configured to do so. ");
                    }
                    log.Info("Memory usage: " + process.WorkingSet64);
                    ConfigProxy.LastSync = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.ff");
                }
                catch (HandySyncException he)
                {
                    log.Fatal(he.Message);
                }
                catch (ExternalException eex)
                {
                    log.Error("External Exception ocurred. Code: " + eex.ErrorCode + " Message: " + eex.Message);
                }
                catch (Exception ex)
                {
                    log.Error("Error in main process: " + ex.Message);
                }
                finally
                {
                    try
                    {
                        log.Info("Closing company. [fCierraEmpresa]");
                        CompacSDK.fCierraEmpresa();
                        log.Info("Memory usage: " + process.WorkingSet64);
                        try
                        {
                            log.Info("Terminating Compac SDK. [fTerminaSDK]");
                            CompacSDK.fTerminaSDK();
                            log.Info("Memory usage: " + process.WorkingSet64);
                        }
                        catch (ExternalException eex)
                        {
                            log.Error("External Exception ocurred while trying to finalize Compac SDK. Code: " + eex.ErrorCode + " Message: " + eex.Message);
                        }
                        catch (Exception ex2)
                        {
                            log.Error("Error in finally block while trying to finalize Compac SDK: " + ex2.Message);
                        }
                    }
                    catch (ExternalException eex)
                    {
                        log.Error("External Exception ocurred while trying to close company. Code: " + eex.ErrorCode + " Message: " + eex.Message);
                    }
                    catch (Exception ex)
                    {
                        log.Error("Error in finally block while trying to close company: " + ex.Message);
                    }
                    finally
                    {
                        //Waits the specified time for next sync
                        log.Info("Iteration finished.  Waiting specified time for next cycle.");
                        GC.Collect();
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("Error in finally block while trying to finalize Compac SDK: " + ex.Message);
                try
                {
                    log.Info("Attempting to terminate Compac SDK. [fTerminaSDK]");
                    CompacSDK.fTerminaSDK();
                    log.Info("Memory usage: " + process.WorkingSet64);
                }
                catch (Exception ex1)
                {
                    log.Error("Error while attempting to terminate Compac SDK: " + ex1.Message);
                }
                finally
                {
                    GC.Collect();
                }
            }
        }