예제 #1
0
        public static GpValidations SupplierInvoiceValidate(string Supplierinvoice, GpMappingAndDatabase mapping)
        {
            dynamic       validator = JsonConvert.DeserializeObject <ExpandoObject>(Supplierinvoice);
            GpValidations validate  = new GpValidations();

            string ExoCode = validator.document.supplier.code.ToString().ToUpper();

            SQLCredentials           ConnectionString = new SQLCredentials();
            Dictionary <int, string> Connection       = ConnectionString.ConnectionStringBuilder();


            string SQLQuery = String.Format("SELECT Accno FROM CR_ACCS where Name = '{0}' or Alphacode = '{0}'", ExoCode);


            try
            {
                SqlConnection con = new SqlConnection(Connection[mapping.Id]);
                // SqlDataReader myreader;
                con.Open();


                using (SqlCommand command = new SqlCommand(SQLQuery, con))
                {
                    var reader = command.ExecuteReader();
                    if (reader.Read())
                    {
                        //Do nothing Item Exists in Exo
                    }
                    else
                    {
                        validate.CreateSupplier = true;
                    }
                    reader.Close();
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }


            List <string> SkuList = new List <string>();

            foreach (var line in validator.document.lines)
            {
                string stkItem = "";
                try { stkItem = line.item.sku.ToString().ToUpper(); }
                catch (Exception ex)
                {
                    // if the sku is null will thow runtime binding error, this is fine as it is a GL invoice.
                    Console.WriteLine(ex.Message + " GL invioce");
                    continue;
                }

                string selectItems = String.Format("Select stockcode from IV00101 where stockcode = '{0}'", stkItem);
                try
                {
                    SqlConnection con = new SqlConnection(Connection[mapping.Id]);
                    // SqlDataReader myreader;
                    con.Open();

                    //Get a list of all ther table names in the Database
                    using (SqlCommand command = new SqlCommand(selectItems, con))
                    {
                        var reader = command.ExecuteReader();
                        if (reader.Read())
                        {
                            //Do nothing Item Exists in Exo
                        }
                        else
                        {
                            validate.InventoryToCreate.Add(stkItem);
                        }
                    }
                }


                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            if (validate.InventoryToCreate.Count() > 0)
            {
                validate.CreateInventory = true;
            }

            return(validate);
        }
        public static void AddtoQueue()
        {
            //Check Mappings
            ZudelloLogin.GetZudelloMappings();


            string Token       = ZudelloLogin.Login();
            string InvoiceList = ZudelloLogin.CallZudelloDocs(Token);


            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);


                    GpMappingAndDatabase mapId = GetMappingAndDatabase(data.document.docType.ToString(), data.document.connectionUuid.ToString());

                    if (mapId != null)
                    {
                        SaveToDBQueue(myDataString, mapId.mappingID, "Waiting", mapId);
                    }



                    var validate = GpValidations.SupplierInvoiceValidate(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);
                        }
                    }

                    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);
                                }
                            }
                        }
                    }

                    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"
                                              select a).FirstOrDefault();

                        //var dateParse = DateTime.ParseExact(data.created_at.ToString(), "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'", CultureInfo.InvariantCulture);
                        //string zudelloDate = data.created_at;

                        //  dateParse = dateParse


                        string pr = data.created_at.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");


                        updateLastSync.LastSync = pr;
                        db.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }