Ejemplo n.º 1
0
        private JObject GetErplyOrders(EAPI api, int pageSize, int pageNo, int dateLastRun)
        {
            var dict = new Dictionary <string, object>();

            dict.Add("changedSince", dateLastRun);
            dict.Add("pageNo", pageNo);
            dict.Add("recordsOnPage", pageSize);
            dict.Add("getRowsForAllInvoices", 1);
            dict.Add("getAddedTimestamp", 1);

            return(api.sendRequest("getSalesDocuments", dict));
        }
Ejemplo n.º 2
0
        public int ProcessErply(EAPI api)
        {
            // Get Stores data from Erply
            var error = GetStores(api);

            if (error.IsError)
            {
                LogService.WriteLog(TransType.Erply, null, null, error.Message);
            }

            // Get SalesOrders from Erply
            int pageSize = 100;
            int pageNo   = 1;

            var dateLastRun = db.GetLastOrderExtractTime();

            LogService.WriteLog(TransType.Erply, null, null, "Date Last Run: " + dateLastRun.ToString());

            JObject erplyOrders = null;

            erplyOrders = GetErplyOrders(api, pageSize, pageNo, dateLastRun);
            var dateThisRun = SaveOrdersToTempTables(erplyOrders);

            // Check if number of orders exceeds the pageSize of the ErplyAPI
            // If so, loop through and process the remaining orders
            int numberOfErplyOrders = erplyOrders["records"].Count();
            int totalRecs           = (int)erplyOrders["status"]["recordsTotal"];

            if (numberOfErplyOrders > 0)
            {
                do
                {
                    pageNo++;
                    erplyOrders = GetErplyOrders(api, pageSize, pageNo, dateLastRun);
                    SaveOrdersToTempTables(erplyOrders);

                    numberOfErplyOrders = erplyOrders["records"].Count();
                } while (numberOfErplyOrders > 0);
            }

            db.UpdateLastOrderExtractTime(dateThisRun);

            LogService.WriteLog(TransType.Erply, null, null, "Received " + totalRecs + " Erply orders");

            return(dateThisRun);
        }
        public Error GetStockOnHandForAllStores(EAPI api, int unixDate, int storeId)
        {
            // We have to make stock on hand requests for each store from Erply because
            // although its API does enable a blank warehouseID (meaning 'all warehouses') to be
            // supplied, the returned data contains no warehouseId so we can't work out which
            // warehouse each record belongs to.
            var error = new Error();

            // We only process stores with a range configured as we can't
            // order anything for them without the range being set
            foreach (var store in db.FindStores(true)
                     .Where(s => storeId < 1 || s.Id == storeId)
                     .ToList())
            {
                error = GetStockOnHand(api, store, unixDate);
                if (error.IsError)
                {
                    break;
                }
            }
            return(error);
        }
        public Error GetStockOnHand(EAPI api,
                                    Store store,
                                    int unixDate)           // Changes since last run date
        {
            var error = new Error();

            try {
                int pageSize       = 100,
                    pageNo         = 1,
                    numStockOnHand = 0;
                string request     = "getProductStock";

                if (store.RangeId == null)
                {
                    LogService.WriteLog(TransType.StockOnHand, store.Id, null, "Warning: Not updating Stock on Hand for Erply store: " + store.Name + " because it has no Range set");
                }
                else
                {
                    LogService.WriteLog(TransType.StockOnHand, store.Id, null, "Updating Stock on Hand for Erply store: " + store.Name);

                    var dict = createGetStockOnHandParameters(store, pageNo, pageSize, unixDate);

                    JObject json = api.sendRequest(request, dict);
                    if (json != null)
                    {
                        error = GetError(json["status"]["errorCode"].ToString(), json["status"]["responseStatus"].ToString());

                        int totalRecs = (int)json["status"]["recordsTotal"];
                        int numPages  = totalRecs / pageSize;
                        if (numPages * pageSize < totalRecs)
                        {
                            numPages++;
                        }

                        LogService.WriteLog(TransType.StockOnHand, store.Id, null, $"{totalRecs} records(s) in {numPages} page(s) found");

                        while (!error.IsError && pageNo <= numPages)
                        {
                            int numRecs = (int)json["status"]["recordsInResponse"];

                            for (int i = 0; i < numRecs; i++)
                            {
                                int productId = Convert.ToInt32((int)json["records"][i]["productID"]);
                                var product   = db.FindProduct(productId);
                                if (product == null)
                                {
                                    LogService.WriteLog(TransType.StockOnHand, store.Id, productId, $"Product Id #{productId} does not exist - creating it");

                                    product = new Product {
                                        Id          = productId,
                                        ProductCode = productId.ToString(),
                                        ProductName = "Erply Product Id:" + productId.ToString(),
                                        PackSize    = 1,
                                        Mpl         = 0
                                    };

                                    var oopsProduct = ropesDb.FindProduct(product.ProductCode);
                                    if (oopsProduct != null)
                                    {
                                        product.PackSize = oopsProduct.MSQ;
                                    }

                                    db.InsertOrUpdateProduct(product, true);
                                }

                                var storeStock = db.FindStoreStock(store.Id, productId);
                                if (storeStock == null)
                                {
                                    storeStock = new StoreStock {
                                        StoreId = store.Id, ProductId = productId, StockinTransit = 0
                                    }
                                }
                                ;

                                var stockOnHand = (int)Convert.ToDecimal(json["records"][i]["amountInStock"]);
                                //if (stockOnHand < 0) stockOnHand = 0;   // Erply can return negative values
                                storeStock.StockOnHand = stockOnHand;
                                db.InsertOrUpdateStoreStock(storeStock);
                                numStockOnHand++;

                                LogService.WriteLog(TransType.StockOnHand, store.Id, productId, $"  {store.Name} / {product.ProductCode} {product.ProductName}: {storeStock.StockOnHand}");
                            }

                            pageNo++;
                            if (pageNo <= numPages)
                            {
                                // Get the next page
                                dict = createGetStockOnHandParameters(store, pageNo, pageSize, unixDate);

                                json = api.sendRequest(request, dict);
                                if (json != null)
                                {
                                    error     = GetError(json["status"]["errorCode"].ToString(), json["status"]["responseStatus"].ToString());
                                    totalRecs = (int)json["status"]["recordsTotal"];
                                    numPages  = totalRecs / pageSize;
                                    if (numPages * pageSize < totalRecs)
                                    {
                                        numPages++;
                                    }
                                }
                                else
                                {
                                    error.SetError($"Error: A NULL JSON object was returned by {request}!");
                                }
                            }
                        }
                        LogService.WriteLog(TransType.StockOnHand, store.Id, null, $"{numStockOnHand} Stock on Hand record(s) retrieved");
                    }
                    else
                    {
                        error.SetError($"Error: A NULL JSON object was returned by {request}!");
                    }
                }
            } catch (Exception e) {
                error.SetError(e);
            }
            return(error);
        }
Ejemplo n.º 5
0
        public Error GetStockInTransit(EAPI api, int unixDate, int warehouseId)           // Changes since last run date
        {
            var error = new Error();

            try {
                int pageSize          = 100,
                    pageNo            = 1,
                    numStockInTransit = 0;
                string request        = "getPurchaseDocuments";

                LogService.WriteLog(TransType.StockInTransit, null, null, "Updating Stock in Transit from Erply");

                var dict = createGetStockInTransitParameters(pageNo, pageSize, unixDate, warehouseId);

                JObject json = api.sendRequest(request, dict);
                if (json != null)
                {
                    error = GetError(json["status"]["errorCode"].ToString(), json["status"]["responseStatus"].ToString());

                    int totalRecs = (int)json["status"]["recordsTotal"];
                    int numPages  = totalRecs / pageSize;
                    if (numPages * pageSize < totalRecs)
                    {
                        numPages++;
                    }

                    LogService.WriteLog(TransType.StockInTransit, null, null, $"{totalRecs} records(s) in {numPages} page(s) found");

                    while (!error.IsError && pageNo <= numPages)
                    {
                        int numRecs = (int)json["status"]["recordsInResponse"];

                        for (int i = 0; i < numRecs; i++)
                        {
                            int row       = 0;
                            int productId = 1;
                            while (productId > 0)
                            {
                                try {
                                    productId = Convert.ToInt32((int)json["records"][i]["rows"][row]["productID"]);
                                } catch {
                                    productId = -1;
                                }
                                if (productId > 0)
                                {
                                    int storeId = Convert.ToInt32((int)json["records"][i]["warehouseID"]);
                                    var store   = db.FindStore(storeId);

                                    var product = db.FindProduct(productId);
                                    if (product == null)
                                    {
                                        product = new Product {
                                            Id          = productId,
                                            ProductCode = json["records"][i]["rows"][row]["code"].ToString(),
                                            ProductName = json["records"][i]["rows"][row]["itemName"].ToString(),
                                            PackSize    = 1,
                                            Mpl         = 0
                                        };

                                        var oopsProduct = ropesDb.FindProduct(product.ProductCode);
                                        if (oopsProduct != null)
                                        {
                                            product.PackSize = oopsProduct.MSQ;
                                        }

                                        db.InsertOrUpdateProduct(product, true);
                                    }

                                    var storeStock = db.FindStoreStock(storeId, productId);
                                    if (storeStock == null)
                                    {
                                        storeStock = new StoreStock {
                                            StoreId = storeId, ProductId = productId, StockOnHand = 0
                                        }
                                    }
                                    ;

                                    var stockInTransit = Convert.ToInt32(json["records"][i]["rows"][row]["amount"]);
                                    if (stockInTransit < 0)
                                    {
                                        stockInTransit = 0;
                                    }
                                    storeStock.StockinTransit = stockInTransit;
                                    db.InsertOrUpdateStoreStock(storeStock);
                                    numStockInTransit++;

                                    LogService.WriteLog(TransType.StockInTransit, storeId, productId, $"  {(store != null ? store.Name : "[Unknown Store]")} / {product.ProductCode} {product.ProductName}: {storeStock.StockinTransit}");
                                }
                                row++;
                            }
                        }

                        pageNo++;
                        if (pageNo <= numPages)
                        {
                            // Get the next page
                            dict = createGetStockInTransitParameters(pageNo, pageSize, unixDate, warehouseId);

                            json = api.sendRequest(request, dict);
                            if (json != null)
                            {
                                error     = GetError(json["status"]["errorCode"].ToString(), json["status"]["responseStatus"].ToString());
                                totalRecs = (int)json["status"]["recordsTotal"];
                                numPages  = totalRecs / pageSize;
                                if (numPages * pageSize < totalRecs)
                                {
                                    numPages++;
                                }
                            }
                            else
                            {
                                error.SetError($"Error: A NULL JSON object was returned by {request}!");
                            }
                        }
                    }
                    LogService.WriteLog(TransType.StockInTransit, null, null, $"{numStockInTransit} Stock in Transit record(s) retrieved");
                }
                else
                {
                    error.SetError($"Error: A NULL JSON object was returned by {request}!");
                }
            } catch (Exception e) {
                error.SetError(e);
            }
            return(error);
        }
        public Error GetStores(EAPI api)
        {
            var error = new Error();

            try {
                int pageSize   = 100,
                    pageNo     = 1;
                string request = "getWarehouses";

                LogService.WriteLog(TransType.StoreUpdate, null, null, "Updating Store list");

                var dict = createGetWarehousesParameters(pageNo, pageSize);

                JObject json = api.sendRequest(request, dict);
                if (json != null)
                {
                    error = GetError(json["status"]["errorCode"].ToString(), json["status"]["responseStatus"].ToString());

                    int totalRecs = (int)json["status"]["recordsTotal"];
                    int numPages  = totalRecs / pageSize;
                    if (numPages * pageSize < totalRecs)
                    {
                        numPages++;
                    }

                    while (!error.IsError && pageNo <= numPages)
                    {
                        int numRecs = (int)json["status"]["recordsInResponse"];

                        for (int i = 0; i < numRecs; i++)
                        {
                            int storeId = Convert.ToInt32((int)json["records"][i]["warehouseID"]);
                            var store   = db.FindStore(storeId);

                            if (store == null)
                            {
                                store = new Store {
                                    Id = storeId, ForecastFactor = 1, Priority = 999
                                }
                            }
                            ;
                            store.Name     = json["records"][i]["name"].ToString();
                            store.Address1 = json["records"][i]["street"].ToString();
                            store.Address2 = json["records"][i]["address2"].ToString();
                            store.Suburb   = json["records"][i]["city"].ToString();
                            store.State    = json["records"][i]["state"].ToString();
                            store.Postcode = json["records"][i]["ZIPcode"].ToString();
                            store.Country  = json["records"][i]["country"].ToString();

                            if (json["records"][i]["storeGroups"].ToString().ToLower().IndexOf("closed down") != -1 ||
                                store.Name.ToLower().IndexOf("closed") == 0)
                            {
                                store.IsActive = false;
                            }
                            else
                            {
                                store.IsActive = true;
                            }
                            db.InsertOrUpdateStore(store);

                            LogService.WriteLog(TransType.StoreUpdate, store.Id, null, $"Updated store: #{store.Id} Name:{store.Name} Addrs1:{store.Address1} Addrs2:{store.Address2} Suburb:{store.Suburb} State:{store.State} Postcode:{store.Postcode} Country:{store.Country}");
                        }

                        pageNo++;
                        if (pageNo <= numPages)
                        {
                            // Get the next page
                            dict = createGetWarehousesParameters(pageNo, pageSize);

                            json = api.sendRequest(request, dict);
                            if (json != null)
                            {
                                error     = GetError(json["status"]["errorCode"].ToString(), json["status"]["responseStatus"].ToString());
                                totalRecs = (int)json["status"]["recordsTotal"];
                                numPages  = totalRecs / pageSize;
                                if (numPages * pageSize < totalRecs)
                                {
                                    numPages++;
                                }
                            }
                            else
                            {
                                error.SetError($"Error: A NULL JSON object was returned by {request}!");
                            }
                        }
                    }
                }
                else
                {
                    error.SetError($"Error: A NULL JSON object was returned by {request}!");
                }
            } catch (Exception e) {
                error.SetError(e);
            }
            return(error);
        }