Ejemplo n.º 1
0
        public static async Task AttacheResponseSender()
        {
            string          token          = ZudelloLogin.Login();
            ResponseZudello ResponseStatus = new ResponseZudello();

            using (var db = new ZudelloContext())
            {
                var queueList = db.Zqueue.Where(i => i.Status == "Success" && (i.ResponseSent == 0)).ToList();

                try
                {
                    foreach (var l in queueList)
                    {
                        ResponseObject successList = new ResponseObject();
                        dynamic        Body        = JObject.Parse(l.Body);
                        successList.body    = "Success";
                        successList.uuid    = Body.uuid;
                        successList.queueid = l.Id;
                        successList.status  = 200;
                        ResponseStatus.data.Add(successList);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine("Master Data");
                }

                foreach (var data in ResponseStatus.data)
                {
                    string responseBody = JsonConvert.SerializeObject(data);

                    HttpStatusCode Response = ZudelloLogin.SendProcessResponse(token, responseBody);

                    if (Response == HttpStatusCode.OK)
                    {
                        var updateSent = db.Zqueue.Where(i => i.Id == data.queueid).FirstOrDefault();
                        updateSent.ResponseSent = 1;
                        db.SaveChanges();
                    }
                }

                db.Dispose();
            }
        }
Ejemplo n.º 2
0
        public static bool ProcessMethod(dynamic queueData)
        {
            string econnectDocument;
            string sXsdSchema;
            string sConnectionString;


            try
            {
                dynamic zudelloObject = JsonConvert.DeserializeObject <ExpandoObject>(queueData.queue.Body);
                string  order         = queueData.map.ProcessOrder.ToString();

                //Created at
                string obj     = queueData.map.DocType.ToString();
                string uuid    = "";//zudelloObject.invoiceUUID.ToString();
                string queueID = queueData.queue.Id.ToString();

                //Generate the eConnectXml
                string xmlRendered = GpTools.RenderXml(queueData.map.Body, zudelloObject);
                using (eConnectMethods e = new eConnectMethods())
                {
                    try
                    {
                        XmlDocument xmldoc = new XmlDocument();
                        xmldoc.LoadXml(xmlRendered);
                        econnectDocument = xmldoc.OuterXml;

                        //User ID=sa;Password=sa
                        using (var db = new ZudelloContext())
                        {
                            var Connection = db.Zconnections.Where(i => i.Id == 1).FirstOrDefault();

                            sConnectionString = String.Format(@"Data Source={0};Integrated Security = SSPI; Persist Security Info = false ; Initial Catalog ={1};", Connection.DataSource, Connection.InitialCatalog);
                            db.Dispose();
                        }
                        // sConnectionString = @"Data Source=LAPTOP-BUDQ9SBN\DYNAMICGP;Integrated Security = SSPI; Persist Security Info = false ; Initial Catalog = GP_DE;";
                        // Create an XML Document object for the schema
                        XmlDocument XsdDoc = new XmlDocument();

                        // Create a string representing the eConnect schema
                        sXsdSchema = XsdDoc.OuterXml;

                        // Pass in xsdSchema to validate against.
                        bool created = e.CreateEntity(sConnectionString, econnectDocument);
                        if (created == true)
                        {
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }

                    // The eConnectException class will catch eConnect business logic errors.
                    // display the error message on the console
                    catch (eConnectException exc)
                    {
                        Console.Write(exc.ToString());
                        e.Dispose();
                        return(false);
                    }
                    // Catch any system error that might occurr.
                    // display the error message on the console
                    catch (System.Exception ex)
                    {
                        Console.Write(ex.ToString());
                        e.Dispose();
                        return(false);
                    }
                }
            }


            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(false);
            }
        }
        public static async Task AddtoQueue()
        {
            //Check Mappings
            ZudelloLogin.GetZudelloMappings().Wait();
            string Token = ZudelloLogin.Login();
            List <Zconnections> teams = new List <Zconnections>();

            using (var db = new ZudelloContext())
            {
                //Maybe hash the records

                teams = db.Zconnections.ToList();
                await db.DisposeAsync();
            }

            int           numberOfLogicalCores = Environment.ProcessorCount;
            List <Thread> threads        = new List <Thread>(numberOfLogicalCores);
            int           sizeOfOneChunk = (teams.Count / numberOfLogicalCores) + 1;
            ConcurrentDictionary <int, dynamic>      cb          = new ConcurrentDictionary <int, dynamic>();
            ConcurrentDictionary <dynamic, LastSync> lastSyncBag = new ConcurrentDictionary <dynamic, LastSync>();

            for (int i = 0; i < numberOfLogicalCores; i++)
            {
                int ab  = i;
                var thr = new Thread(

                    () =>
                {
                    int count = 0;
                    List <Zconnections> teamChunked = teams.Skip(ab * sizeOfOneChunk)
                                                      .Take(sizeOfOneChunk).ToList();


                    foreach (var team in teamChunked)
                    {
                        try
                        {
                            dynamic ZudelloTeam = JsonConvert.DeserializeObject <ExpandoObject>(team.ZudelloCredentials);
                            string InvoiceList  = ZudelloLogin.CallZudelloDocs(Token, ZudelloTeam.team, team.Id);
                            dynamic ToSync      = JObject.Parse(InvoiceList);

                            foreach (var data in ToSync.data)
                            {
                                try
                                {
                                    string myDataString = data.ToString();
                                    // Console.WriteLine(Id.uuid + "," + Id.items);
                                    Console.WriteLine(data);


                                    ExoMappingAndDB mapId = GetMappingAndDatabase(data.document.docType.ToString(),
                                                                                  data.document.connectionUuid.ToString());
                                    int?rowId = null;
                                    if (mapId != null)
                                    {
                                        rowId = SaveToDBQueue(myDataString, mapId.mappingID, "Waiting", mapId);
                                    }



                                    var validate = Validations.ExoSupplierInvoice(myDataString, mapId);

                                    if (validate.CreateSupplier == true)
                                    {
                                        //HAVE to hard code type for the supplier and inventory?
                                        mapId = GetMappingAndDatabase(data.document.supplier.docType.ToString(), data.document.connectionUuid.ToString());

                                        // Check if already in the queue
                                        if (InQueue(mapId.mappingID, data.document.supplier.code.ToString()) == false)
                                        {
                                            SaveToDBQueue(data.document.supplier.ToString(), mapId.mappingID, "Waiting", mapId, rowId);
                                        }
                                    }

                                    if (validate.CreateInventory == true)
                                    {
                                        foreach (var itemData in data.document.lines)
                                        {
                                            mapId = GetMappingAndDatabase(itemData.docType.ToString(), data.document.connectionUuid.ToString());

                                            if (validate.InventoryToCreate.Contains(itemData.item.sku.ToString()))
                                            {
                                                // Check if already in the queue
                                                if (InQueue(mapId.mappingID, itemData.item.sku.ToString()) == false)
                                                {
                                                    SaveToDBQueue(itemData.ToString(), mapId.mappingID, "Waiting", mapId, rowId);
                                                }
                                            }
                                        }
                                    }

                                    LastSync lastSync    = new LastSync();
                                    lastSync.teamId      = team.Id;
                                    lastSync.lastUpdated = data.created_at.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");

                                    lastSyncBag.TryAdd(data.uuid, lastSync);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            //Write to loggings tables
                            Console.WriteLine(ex.Message);
                        }
                    }
                }

                    );

                threads.Add(thr);
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }


            var dataList = lastSyncBag.Values.ToList();

            foreach (var lastSync in dataList)
            {
                using (var db = new ZudelloContext())
                {
                    var updateLastSync = (from a in db.Zlastsync
                                          join c in db.Zmapping on a.MappingId equals c.Id
                                          where c.DocType == "CallZudelloDocs" && c.connection_id == lastSync.teamId
                                          select a).FirstOrDefault();

                    if (DateTime.Parse(updateLastSync.LastSync) > DateTime.Parse(lastSync.lastUpdated))
                    {
                    }
                    else
                    {
                        updateLastSync.LastSync = lastSync.lastUpdated;


                        db.SaveChanges();
                    }

                    db.Dispose();
                }
            }
        }