// ===============
        // Send Collection
        // ===============
        public void SendCollection(String apiUrlHost, String json, Int32 collectionId)
        {
            try
            {
                // ============
                // Http Request
                // ============
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

                var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://" + apiUrlHost + "/api/add/POSIntegration/salesInvoice");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method      = "POST";

                // ====
                // Data
                // ====
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    Entities.ISPOSTrnCollection collection = new JavaScriptSerializer().Deserialize <Entities.ISPOSTrnCollection>(json);
                    streamWriter.Write(new JavaScriptSerializer().Serialize(collection));
                }

                // ================
                // Process Response
                // ================
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    if (result != null)
                    {
                        var currentCollection = from d in posdb.TrnCollections
                                                where d.Id == collectionId
                                                select d;

                        if (currentCollection.Any())
                        {
                            var updateCollection = currentCollection.FirstOrDefault();
                            updateCollection.PostCode = result.Replace("\"", "");
                            posdb.SubmitChanges();
                        }

                        trnIntegrationForm.salesIntegrationLogMessages("Send Succesful!" + "\r\n\n");
                        trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                        trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");
                    }
                }
            }
            catch (WebException we)
            {
                var resp = new StreamReader(we.Response.GetResponseStream()).ReadToEnd();

                trnIntegrationForm.salesIntegrationLogMessages(resp.Replace("\"", "") + "\r\n\n");
                trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");
            }
        }
Ejemplo n.º 2
0
        private void btnSaveSettings_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Save changes?", "Save Settings", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (dialogResult == DialogResult.Yes)
            {
                if (!isFolderMonitoringOnly)
                {
                    var settings = from d in posdb.SysSettings select d;
                    if (settings.Any())
                    {
                        var updateSettings = settings.FirstOrDefault();
                        updateSettings.BranchCode     = txtBranchCode.Text;
                        updateSettings.UserCode       = txtUserCode.Text;
                        updateSettings.UseItemPrice   = cbxUseItemPrice.Checked;
                        updateSettings.PostUserId     = Convert.ToInt32(cboPostUser.SelectedValue);
                        updateSettings.PostSupplierId = Convert.ToInt32(cboPostSupplier.SelectedValue);
                        posdb.SubmitChanges();
                    }
                }

                Entities.SysSettings newSysSettings = new Entities.SysSettings()
                {
                    ConnectionString         = txtConnectionString.Text,
                    Domain                   = txtDomain.Text,
                    LogFileLocation          = txtLogFileLocation.Text,
                    FolderToMonitor          = txtFolderToMonitor.Text,
                    IsFolderMonitoringOnly   = isFolderMonitoringOnly,
                    FolderMonitoringUserCode = txtFMUserCode.Text,
                    FolderForSentFiles       = txtFolderForSentFiles.Text,
                    ManualSalesIntegration   = cbxManualSalesIntegration.Checked
                };

                String settingsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Settings.json");
                String newJson      = new JavaScriptSerializer().Serialize(newSysSettings);

                File.WriteAllText(settingsPath, newJson);

                trnInnosoftPOSIntegrationForm.getSettings();

                Close();
            }
        }
Ejemplo n.º 3
0
        // =======================
        // Get Stock Transfer - IN
        // =======================
        public Task GetStockTransferIN(String apiUrlHost, String toBranchCode)
        {
            try
            {
                DateTime dateTimeToday     = DateTime.Now;
                String   stockTransferDate = Convert.ToDateTime(activityDate).ToString("MM-dd-yyyy", CultureInfo.InvariantCulture);

                // ============
                // Http Request
                // ============
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://" + apiUrlHost + "/api/get/POSIntegration/stockTransferItems/IN/" + stockTransferDate + "/" + toBranchCode);
                httpWebRequest.Method = "GET";
                httpWebRequest.Accept = "application/json";

                // ================
                // Process Response
                // ================
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    List <Entities.ISPOSTrnStockTransfer> stockTransferLists = (List <Entities.ISPOSTrnStockTransfer>)js.Deserialize(result, typeof(List <Entities.ISPOSTrnStockTransfer>));

                    if (stockTransferLists.Any())
                    {
                        foreach (var stockTransfer in stockTransferLists)
                        {
                            var currentStockIn = from d in posdb.TrnStockIns where d.Remarks.Equals("ST-" + stockTransfer.BranchCode + "-" + stockTransfer.STNumber) && d.TrnStockInLines.Count() > 0 && d.IsLocked == true select d;
                            if (!currentStockIn.Any())
                            {
                                trnIntegrationForm.salesIntegrationLogMessages("Saving Stock Transfer (IN): ST-" + stockTransfer.BranchCode + "-" + stockTransfer.STNumber + "\r\n\n");

                                var defaultPeriod   = from d in posdb.MstPeriods select d;
                                var defaultSettings = from d in posdb.SysSettings select d;

                                var lastStockInNumber   = from d in posdb.TrnStockIns.OrderByDescending(d => d.Id) select d;
                                var stockInNumberResult = defaultPeriod.FirstOrDefault().Period + "-000001";

                                if (lastStockInNumber.Any())
                                {
                                    var   stockInNumberSplitStrings = lastStockInNumber.FirstOrDefault().StockInNumber;
                                    Int32 secondIndex = stockInNumberSplitStrings.IndexOf('-', stockInNumberSplitStrings.IndexOf('-'));
                                    var   stockInNumberSplitStringValue = stockInNumberSplitStrings.Substring(secondIndex + 1);
                                    var   stockInNumber = Convert.ToInt32(stockInNumberSplitStringValue) + 000001;
                                    stockInNumberResult = defaultPeriod.FirstOrDefault().Period + "-" + FillLeadingZeroes(stockInNumber, 6);
                                }

                                InnosoftPOSData.TrnStockIn newStockIn = new InnosoftPOSData.TrnStockIn
                                {
                                    PeriodId       = defaultPeriod.FirstOrDefault().Id,
                                    StockInDate    = Convert.ToDateTime(stockTransfer.STDate),
                                    StockInNumber  = stockInNumberResult,
                                    SupplierId     = defaultSettings.FirstOrDefault().PostSupplierId,
                                    Remarks        = "ST-" + stockTransfer.BranchCode + "-" + stockTransfer.STNumber,
                                    IsReturn       = false,
                                    PreparedBy     = defaultSettings.FirstOrDefault().PostUserId,
                                    CheckedBy      = defaultSettings.FirstOrDefault().PostUserId,
                                    ApprovedBy     = defaultSettings.FirstOrDefault().PostUserId,
                                    IsLocked       = true,
                                    EntryUserId    = defaultSettings.FirstOrDefault().PostUserId,
                                    EntryDateTime  = DateTime.Now,
                                    UpdateUserId   = defaultSettings.FirstOrDefault().PostUserId,
                                    UpdateDateTime = DateTime.Now
                                };

                                posdb.TrnStockIns.InsertOnSubmit(newStockIn);
                                posdb.SubmitChanges();

                                if (stockTransfer.ListPOSIntegrationTrnStockTransferItem.Any())
                                {
                                    foreach (var item in stockTransfer.ListPOSIntegrationTrnStockTransferItem.ToList())
                                    {
                                        var currentItem = from d in posdb.MstItems where d.BarCode.Equals(item.ItemCode) && d.MstUnit.Unit.Equals(item.Unit) select d;
                                        if (currentItem.Any())
                                        {
                                            InnosoftPOSData.TrnStockInLine newStockInLine = new InnosoftPOSData.TrnStockInLine
                                            {
                                                StockInId      = newStockIn.Id,
                                                ItemId         = currentItem.FirstOrDefault().Id,
                                                UnitId         = currentItem.FirstOrDefault().UnitId,
                                                Quantity       = item.Quantity,
                                                Cost           = item.Cost,
                                                Amount         = item.Amount,
                                                ExpiryDate     = currentItem.FirstOrDefault().ExpiryDate,
                                                LotNumber      = currentItem.FirstOrDefault().LotNumber,
                                                AssetAccountId = currentItem.FirstOrDefault().AssetAccountId,
                                                Price          = currentItem.FirstOrDefault().Price
                                            };

                                            posdb.TrnStockInLines.InsertOnSubmit(newStockInLine);

                                            var updateItem = currentItem.FirstOrDefault();
                                            updateItem.OnhandQuantity = currentItem.FirstOrDefault().OnhandQuantity + Convert.ToDecimal(item.Quantity);

                                            posdb.SubmitChanges();

                                            trnIntegrationForm.salesIntegrationLogMessages(" > " + currentItem.FirstOrDefault().ItemDescription + " * " + item.Quantity.ToString("#,##0.00") + "\r\n\n");
                                        }
                                    }
                                }

                                trnIntegrationForm.salesIntegrationLogMessages("Save Successful!" + "\r\n\n");
                                trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");
                            }
                        }
                    }
                }

                return(Task.FromResult(""));
            }
            catch (Exception e)
            {
                trnIntegrationForm.salesIntegrationLogMessages("Stock Transfer (In) Error: " + e.Message + "\r\n\n");
                trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");

                return(Task.FromResult(""));
            }
        }
        // ========
        // Get Item
        // ========
        public Task GetItem(String apiUrlHost)
        {
            try
            {
                DateTime dateTimeToday = DateTime.Now;
                String   currentDate   = Convert.ToDateTime(activityDate).ToString("MM-dd-yyyy", CultureInfo.InvariantCulture);

                // ============
                // Http Request
                // ============
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://" + apiUrlHost + "/api/get/POSIntegration/item/" + currentDate);
                httpWebRequest.Method = "GET";
                httpWebRequest.Accept = "application/json";

                // ================
                // Process Response
                // ================
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    JavaScriptSerializer         js        = new JavaScriptSerializer();
                    List <Entities.ISPOSMstItem> itemLists = (List <Entities.ISPOSMstItem>)js.Deserialize(result, typeof(List <Entities.ISPOSMstItem>));

                    if (itemLists.Any())
                    {
                        foreach (var item in itemLists)
                        {
                            var units = from d in posdb.MstUnits where d.Unit.Equals(item.Unit) select d;
                            if (units.Any())
                            {
                                var taxes = from d in posdb.MstTaxes where d.Tax.Equals(item.OutputTax) select d;
                                if (taxes.Any())
                                {
                                    var supplier = from d in posdb.MstSuppliers select d;
                                    if (supplier.Any())
                                    {
                                        var defaultSettings = from d in posdb.SysSettings select d;

                                        var currentItem = from d in posdb.MstItems where d.BarCode.Equals(item.ManualArticleCode) && d.MstUnit.Unit.Equals(item.Unit) && d.IsLocked == true select d;
                                        if (currentItem.Any())
                                        {
                                            Boolean foundChanges = false;

                                            if (!foundChanges)
                                            {
                                                if (!currentItem.FirstOrDefault().BarCode.Equals(item.ManualArticleCode))
                                                {
                                                    foundChanges = true;
                                                }
                                            }

                                            if (!foundChanges)
                                            {
                                                if (!currentItem.FirstOrDefault().ItemDescription.Equals(item.Article))
                                                {
                                                    foundChanges = true;
                                                }
                                            }

                                            if (!foundChanges)
                                            {
                                                if (!currentItem.FirstOrDefault().Category.Equals(item.Category))
                                                {
                                                    foundChanges = true;
                                                }
                                            }

                                            if (!foundChanges)
                                            {
                                                if (!currentItem.FirstOrDefault().MstUnit.Unit.Equals(item.Unit))
                                                {
                                                    foundChanges = true;
                                                }
                                            }

                                            if (!defaultSettings.FirstOrDefault().UseItemPrice)
                                            {
                                                if (!foundChanges)
                                                {
                                                    if (currentItem.FirstOrDefault().Price != item.Price)
                                                    {
                                                        foundChanges = true;
                                                    }
                                                }
                                            }

                                            if (!foundChanges)
                                            {
                                                if (currentItem.FirstOrDefault().Cost != item.Cost)
                                                {
                                                    foundChanges = true;
                                                }
                                            }

                                            if (!foundChanges)
                                            {
                                                if (currentItem.FirstOrDefault().IsInventory != item.IsInventory)
                                                {
                                                    foundChanges = true;
                                                }
                                            }

                                            if (!foundChanges)
                                            {
                                                if (currentItem.FirstOrDefault().Remarks != null)
                                                {
                                                    if (!currentItem.FirstOrDefault().Remarks.Equals(item.Particulars))
                                                    {
                                                        foundChanges = true;
                                                    }
                                                }
                                            }

                                            if (!foundChanges)
                                            {
                                                if (currentItem.FirstOrDefault().OutTaxId != taxes.FirstOrDefault().Id)
                                                {
                                                    foundChanges = true;
                                                }
                                            }

                                            if (!defaultSettings.FirstOrDefault().UseItemPrice)
                                            {
                                                if (!foundChanges)
                                                {
                                                    if (item.ListItemPrice.Any())
                                                    {
                                                        var posItemPrices = from d in posdb.MstItemPrices where d.MstItem.BarCode.Equals(item.ManualArticleCode) select d;
                                                        if (posItemPrices.Any())
                                                        {
                                                            if (item.ListItemPrice.Count() == posItemPrices.Count())
                                                            {
                                                                foreach (var itemPrice in item.ListItemPrice)
                                                                {
                                                                    if (!foundChanges)
                                                                    {
                                                                        var currentPOSItemPrices = from d in posItemPrices where d.PriceDescription.Equals(itemPrice.PriceDescription) && d.Price == itemPrice.Price select d;
                                                                        if (!currentPOSItemPrices.Any())
                                                                        {
                                                                            foundChanges = true;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                foundChanges = true;
                                                            }
                                                        }
                                                    }
                                                }
                                            }

                                            if (foundChanges)
                                            {
                                                trnIntegrationForm.salesIntegrationLogMessages("Updating Item: " + currentItem.FirstOrDefault().ItemDescription + "\r\n\n");
                                                trnIntegrationForm.salesIntegrationLogMessages("Barcode: " + currentItem.FirstOrDefault().BarCode + "\r\n\n");

                                                var updateItem = currentItem.FirstOrDefault();
                                                updateItem.BarCode         = item.ManualArticleCode;
                                                updateItem.ItemDescription = item.Article;
                                                updateItem.Alias           = item.Article;
                                                updateItem.GenericName     = item.Article;
                                                updateItem.Category        = item.Category;
                                                updateItem.UnitId          = units.FirstOrDefault().Id;
                                                updateItem.Price           = item.Price;
                                                updateItem.Cost            = item.Cost;
                                                updateItem.IsInventory     = item.IsInventory;
                                                updateItem.Remarks         = item.Particulars;
                                                updateItem.OutTaxId        = taxes.FirstOrDefault().Id;
                                                updateItem.UpdateUserId    = defaultSettings.FirstOrDefault().PostUserId;
                                                updateItem.UpdateDateTime  = DateTime.Now;
                                                posdb.SubmitChanges();

                                                if (!defaultSettings.FirstOrDefault().UseItemPrice)
                                                {
                                                    if (item.ListItemPrice.Any())
                                                    {
                                                        var posItemPrices = from d in posdb.MstItemPrices where d.ItemId == currentItem.FirstOrDefault().Id select d;
                                                        if (posItemPrices.Any())
                                                        {
                                                            posdb.MstItemPrices.DeleteAllOnSubmit(posItemPrices);
                                                            posdb.SubmitChanges();
                                                        }

                                                        List <InnosoftPOSData.MstItemPrice> newItemPrice = new List <InnosoftPOSData.MstItemPrice>();
                                                        foreach (var itemPrice in item.ListItemPrice)
                                                        {
                                                            newItemPrice.Add(new InnosoftPOSData.MstItemPrice
                                                            {
                                                                ItemId           = currentItem.FirstOrDefault().Id,
                                                                PriceDescription = itemPrice.PriceDescription,
                                                                Price            = itemPrice.Price,
                                                                TriggerQuantity  = 0
                                                            });
                                                        }

                                                        posdb.MstItemPrices.InsertAllOnSubmit(newItemPrice);
                                                        posdb.SubmitChanges();
                                                    }
                                                }

                                                trnIntegrationForm.salesIntegrationLogMessages("Update Successful!" + "\r\n\n");
                                                trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                                trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");
                                            }
                                        }
                                        else
                                        {
                                            trnIntegrationForm.salesIntegrationLogMessages("Saving Item: " + item.Article + "\r\n\n");
                                            trnIntegrationForm.salesIntegrationLogMessages("Barcode: " + item.ManualArticleCode + "\r\n\n");

                                            var defaultItemCode = "000001";
                                            var lastItem        = from d in posdb.MstItems.OrderByDescending(d => d.Id) select d;
                                            if (lastItem.Any())
                                            {
                                                var OTNumber = Convert.ToInt32(lastItem.FirstOrDefault().ItemCode) + 000001;
                                                defaultItemCode = FillLeadingZeroes(OTNumber, 6);
                                            }

                                            InnosoftPOSData.MstItem newItem = new InnosoftPOSData.MstItem
                                            {
                                                ItemCode          = defaultItemCode,
                                                BarCode           = item.ManualArticleCode,
                                                ItemDescription   = item.Article,
                                                Alias             = item.Article,
                                                GenericName       = item.Article,
                                                Category          = item.Category,
                                                SalesAccountId    = 159,
                                                AssetAccountId    = 74,
                                                CostAccountId     = 238,
                                                InTaxId           = 4,
                                                OutTaxId          = taxes.FirstOrDefault().Id,
                                                UnitId            = units.FirstOrDefault().Id,
                                                DefaultSupplierId = supplier.FirstOrDefault().Id,
                                                Cost                 = item.Cost,
                                                MarkUp               = 0,
                                                Price                = item.Price,
                                                ImagePath            = "NA",
                                                ReorderQuantity      = 0,
                                                OnhandQuantity       = 0,
                                                IsInventory          = item.IsInventory,
                                                ExpiryDate           = null,
                                                LotNumber            = " ",
                                                Remarks              = item.Particulars,
                                                EntryUserId          = defaultSettings.FirstOrDefault().PostUserId,
                                                EntryDateTime        = DateTime.Now,
                                                UpdateUserId         = defaultSettings.FirstOrDefault().PostUserId,
                                                UpdateDateTime       = DateTime.Now,
                                                IsLocked             = true,
                                                DefaultKitchenReport = " ",
                                                IsPackage            = false
                                            };

                                            posdb.MstItems.InsertOnSubmit(newItem);
                                            posdb.SubmitChanges();

                                            if (item.ListItemPrice.Any())
                                            {
                                                List <InnosoftPOSData.MstItemPrice> newItemPrice = new List <InnosoftPOSData.MstItemPrice>();
                                                foreach (var itemPrice in item.ListItemPrice)
                                                {
                                                    newItemPrice.Add(new InnosoftPOSData.MstItemPrice
                                                    {
                                                        ItemId           = newItem.Id,
                                                        PriceDescription = itemPrice.PriceDescription,
                                                        Price            = itemPrice.Price,
                                                        TriggerQuantity  = 0
                                                    });
                                                }

                                                posdb.MstItemPrices.InsertAllOnSubmit(newItemPrice);
                                                posdb.SubmitChanges();
                                            }

                                            trnIntegrationForm.salesIntegrationLogMessages("Save Successful!" + "\r\n\n");
                                            trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                            trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");
                                        }
                                    }
                                    else
                                    {
                                        trnIntegrationForm.salesIntegrationLogMessages("Cannot Save Item: " + item.Article + "\r\n\n");
                                        trnIntegrationForm.salesIntegrationLogMessages("Empty Supplier!" + "\r\n\n");
                                        trnIntegrationForm.salesIntegrationLogMessages("Save Failed!" + "\r\n\n");
                                        trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                        trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");
                                    }
                                }
                                else
                                {
                                    trnIntegrationForm.salesIntegrationLogMessages("Cannot Save Item: " + item.Article + "\r\n\n");
                                    trnIntegrationForm.salesIntegrationLogMessages("Output Tax Mismatch!" + "\r\n\n");
                                    trnIntegrationForm.salesIntegrationLogMessages("Save Failed!" + "\r\n\n");
                                    trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                    trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");
                                }
                            }
                            else
                            {
                                trnIntegrationForm.salesIntegrationLogMessages("Cannot Save Item: " + item.Article + "\r\n\n");
                                trnIntegrationForm.salesIntegrationLogMessages("Unit Mismatch!" + "\r\n\n");
                                trnIntegrationForm.salesIntegrationLogMessages("Save Failed!" + "\r\n\n");
                                trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");
                            }
                        }
                    }
                }

                return(Task.FromResult(""));
            }
            catch (Exception e)
            {
                trnIntegrationForm.salesIntegrationLogMessages("Item Error: " + e.Message + "\r\n\n");
                trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");

                return(Task.FromResult(""));
            }
        }
        // ============
        // Get Supplier
        // ============
        public Task GetSupplier(String apiUrlHost)
        {
            try
            {
                DateTime dateTimeToday = DateTime.Now;
                String   currentDate   = Convert.ToDateTime(activityDate).ToString("MM-dd-yyyy", CultureInfo.InvariantCulture);

                // ============
                // Http Request
                // ============
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://" + apiUrlHost + "/api/get/POSIntegration/supplier/" + currentDate);
                httpWebRequest.Method = "GET";
                httpWebRequest.Accept = "application/json";

                // ================
                // Process Response
                // ================
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    JavaScriptSerializer             js            = new JavaScriptSerializer();
                    List <Entities.ISPOSMstSupplier> supplierLists = (List <Entities.ISPOSMstSupplier>)js.Deserialize(result, typeof(List <Entities.ISPOSMstSupplier>));

                    if (supplierLists.Any())
                    {
                        foreach (var supplier in supplierLists)
                        {
                            var terms = from d in posdb.MstTerms where d.Term.Equals(supplier.Term) select d;
                            if (terms.Any())
                            {
                                var defaultSettings = from d in posdb.SysSettings select d;

                                var currentSupplier = from d in posdb.MstSuppliers where d.Supplier.Equals(supplier.Article) && d.IsLocked == true select d;
                                if (currentSupplier.Any())
                                {
                                    Boolean foundChanges = false;

                                    if (!foundChanges)
                                    {
                                        if (!currentSupplier.FirstOrDefault().Supplier.Equals(supplier.Article))
                                        {
                                            foundChanges = true;
                                        }
                                    }

                                    if (!foundChanges)
                                    {
                                        if (!currentSupplier.FirstOrDefault().Address.Equals(supplier.Address))
                                        {
                                            foundChanges = true;
                                        }
                                    }

                                    if (!foundChanges)
                                    {
                                        if (!currentSupplier.FirstOrDefault().CellphoneNumber.Equals(supplier.ContactNumber))
                                        {
                                            foundChanges = true;
                                        }
                                    }

                                    if (!foundChanges)
                                    {
                                        if (!currentSupplier.FirstOrDefault().MstTerm.Term.Equals(supplier.Term))
                                        {
                                            foundChanges = true;
                                        }
                                    }

                                    if (!foundChanges)
                                    {
                                        if (!currentSupplier.FirstOrDefault().TIN.Equals(supplier.TaxNumber))
                                        {
                                            foundChanges = true;
                                        }
                                    }

                                    if (foundChanges)
                                    {
                                        trnIntegrationForm.salesIntegrationLogMessages("Updating Supplier: " + currentSupplier.FirstOrDefault().Supplier + "\r\n\n");
                                        trnIntegrationForm.salesIntegrationLogMessages("Contact No.: " + currentSupplier.FirstOrDefault().CellphoneNumber + "\r\n\n");

                                        var updateSupplier = currentSupplier.FirstOrDefault();
                                        updateSupplier.Supplier        = supplier.Article;
                                        updateSupplier.Address         = supplier.Address;
                                        updateSupplier.CellphoneNumber = supplier.ContactNumber;
                                        updateSupplier.TermId          = terms.FirstOrDefault().Id;
                                        updateSupplier.TIN             = supplier.TaxNumber;
                                        updateSupplier.UpdateUserId    = defaultSettings.FirstOrDefault().PostUserId;
                                        updateSupplier.UpdateDateTime  = DateTime.Now;
                                        posdb.SubmitChanges();

                                        trnIntegrationForm.salesIntegrationLogMessages("Update Successful!" + "\r\n\n");
                                        trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                        trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");
                                    }
                                }
                                else
                                {
                                    trnIntegrationForm.salesIntegrationLogMessages("Saving Supplier: " + supplier.Article + "\r\n\n");
                                    trnIntegrationForm.salesIntegrationLogMessages("Contact No.: " + supplier.ContactNumber + "\r\n\n");

                                    InnosoftPOSData.MstSupplier newSupplier = new InnosoftPOSData.MstSupplier
                                    {
                                        Supplier        = supplier.Article,
                                        Address         = supplier.Address,
                                        TelephoneNumber = "NA",
                                        CellphoneNumber = supplier.ContactNumber,
                                        FaxNumber       = "NA",
                                        TermId          = terms.FirstOrDefault().Id,
                                        TIN             = supplier.TaxNumber,
                                        AccountId       = 254,
                                        EntryUserId     = defaultSettings.FirstOrDefault().PostUserId,
                                        EntryDateTime   = DateTime.Now,
                                        UpdateUserId    = defaultSettings.FirstOrDefault().PostUserId,
                                        UpdateDateTime  = DateTime.Now,
                                        IsLocked        = true,
                                    };

                                    posdb.MstSuppliers.InsertOnSubmit(newSupplier);
                                    posdb.SubmitChanges();

                                    trnIntegrationForm.salesIntegrationLogMessages("Save Successful!" + "\r\n\n");
                                    trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                    trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");
                                }
                            }
                            else
                            {
                                trnIntegrationForm.salesIntegrationLogMessages("Cannot Save Supplier: " + supplier.Article + "\r\n\n");
                                trnIntegrationForm.salesIntegrationLogMessages("Term Mismatch!" + "\r\n\n");
                                trnIntegrationForm.salesIntegrationLogMessages("Save Failed!" + "\r\n\n");
                                trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");
                            }
                        }
                    }
                }

                return(Task.FromResult(""));
            }
            catch (Exception e)
            {
                trnIntegrationForm.salesIntegrationLogMessages("Supplier Error: " + e.Message + "\r\n\n");
                trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");

                return(Task.FromResult(""));
            }
        }
Ejemplo n.º 6
0
        // ==================
        // Send Sales Invoice
        // ==================
        public async void SendSalesInvoice(Forms.TrnIntegrationForm trnIntegrationForm, String domain, String salesDate, Int32 terminalId)
        {
            List <Entities.FolderMonitoringTrnSalesInvoice> newSalesInvoices = new List <Entities.FolderMonitoringTrnSalesInvoice>();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            String jsonData = "";

            List <Int32> collectionIds = new List <Int32>();

            String defaultReferenceNumberTimeStamp = DateTime.Now.ToString("yyyyMMddHHmmss");
            String salesTimeStamp = Convert.ToDateTime(salesDate).ToLongDateString();

            // ========================
            // Get Integration Settings
            // ========================
            var settings = from d in posdb.SysSettings select d;

            if (settings.Any())
            {
                String branchCode = settings.FirstOrDefault().BranchCode;
                String userCode   = settings.FirstOrDefault().UserCode;

                // =============
                // Getting Sales
                // =============
                trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\nGetting Sales Data... (0%) \r\n\n");
                while (true)
                {
                    newSalesInvoices = new List <Entities.FolderMonitoringTrnSalesInvoice>();

                    try
                    {
                        var collections = from d in posdb.TrnCollections
                                          where d.CollectionDate == Convert.ToDateTime(salesDate) &&
                                          d.TerminalId == terminalId &&
                                          d.SalesId != null &&
                                          d.PostCode == null &&
                                          d.IsLocked == true &&
                                          d.IsCancelled == false
                                          select d;

                        if (collections.Any())
                        {
                            foreach (var collection in collections)
                            {
                                if (collectionIds.Contains(collection.Id) == false)
                                {
                                    collectionIds.Add(collection.Id);
                                }

                                var salesLines = from d in posdb.TrnSalesLines
                                                 where d.SalesId == collection.SalesId
                                                 select d;

                                if (salesLines.Any())
                                {
                                    String defaultManualSINumber = collection.MstTerminal.Terminal + "-" + defaultReferenceNumberTimeStamp;

                                    var groupedSalesLines = from d in salesLines
                                                            group d by new
                                    {
                                        d.TrnSale.MstCustomer.CustomerCode,
                                        d.MstItem.BarCode,
                                        d.MstItem.MstUnit.Unit,
                                        d.MstItem.Price,
                                        d.NetPrice,
                                        d.DiscountAmount,
                                    } into g
                                        select new
                                    {
                                        g.Key.CustomerCode,
                                        g.Key.BarCode,
                                        g.Key.Unit,
                                        Quantity = g.Sum(s => s.Quantity),
                                        g.Key.Price,
                                        g.Key.DiscountAmount,
                                        g.Key.NetPrice,
                                        Amount = g.Sum(s => s.Amount)
                                    };

                                    if (groupedSalesLines.ToList().Any())
                                    {
                                        Int32 count = 0;

                                        foreach (var groupedSalesLine in groupedSalesLines.ToList())
                                        {
                                            count += 1;

                                            newSalesInvoices.Add(new Entities.FolderMonitoringTrnSalesInvoice
                                            {
                                                BranchCode        = branchCode,
                                                SIDate            = salesDate,
                                                CustomerCode      = groupedSalesLine.CustomerCode,
                                                ManualSINumber    = defaultManualSINumber,
                                                DocumentReference = collection.MstTerminal.Terminal,
                                                Remarks           = "Sales for " + salesTimeStamp,
                                                UserCode          = userCode,
                                                CreatedDateTime   = salesDate,
                                                ItemCode          = groupedSalesLine.BarCode,
                                                Particulars       = defaultManualSINumber,
                                                Unit           = groupedSalesLine.Unit,
                                                Quantity       = groupedSalesLine.Quantity,
                                                Price          = groupedSalesLine.Price,
                                                DiscountAmount = groupedSalesLine.DiscountAmount,
                                                NetPrice       = groupedSalesLine.NetPrice,
                                                Amount         = groupedSalesLine.Amount,
                                                No             = count
                                            });
                                        }
                                    }
                                }
                            }

                            var groupedNewSalesInvoices = from d in newSalesInvoices
                                                          group d by new
                            {
                                d.ManualSINumber,
                                d.DocumentReference,
                                d.CustomerCode,
                                d.Remarks,
                                d.ItemCode,
                                d.Unit,
                                d.Price,
                                d.NetPrice,
                                d.DiscountAmount,
                            } into g
                                select new
                            {
                                g.Key.ManualSINumber,
                                g.Key.DocumentReference,
                                g.Key.CustomerCode,
                                g.Key.Remarks,
                                g.Key.ItemCode,
                                g.Key.Unit,
                                Quantity = g.Sum(s => s.Quantity),
                                g.Key.Price,
                                g.Key.DiscountAmount,
                                g.Key.NetPrice,
                                Amount = g.Sum(s => s.Amount)
                            };

                            if (groupedNewSalesInvoices.Any())
                            {
                                newSalesInvoices = new List <Entities.FolderMonitoringTrnSalesInvoice>();

                                Int32 count = 0;

                                foreach (var groupedNewSalesInvoice in groupedNewSalesInvoices.ToList())
                                {
                                    count += 1;

                                    newSalesInvoices.Add(new Entities.FolderMonitoringTrnSalesInvoice
                                    {
                                        BranchCode        = branchCode,
                                        SIDate            = salesDate,
                                        CustomerCode      = groupedNewSalesInvoice.CustomerCode,
                                        ManualSINumber    = groupedNewSalesInvoice.ManualSINumber,
                                        DocumentReference = groupedNewSalesInvoice.DocumentReference,
                                        Remarks           = groupedNewSalesInvoice.Remarks,
                                        UserCode          = userCode,
                                        CreatedDateTime   = salesDate,
                                        ItemCode          = groupedNewSalesInvoice.ItemCode,
                                        Particulars       = groupedNewSalesInvoice.ManualSINumber,
                                        Unit           = groupedNewSalesInvoice.Unit,
                                        Quantity       = groupedNewSalesInvoice.Quantity,
                                        Price          = groupedNewSalesInvoice.Price,
                                        DiscountAmount = groupedNewSalesInvoice.DiscountAmount,
                                        NetPrice       = groupedNewSalesInvoice.NetPrice,
                                        Amount         = groupedNewSalesInvoice.Amount,
                                        No             = count
                                    });
                                }
                            }
                        }

                        trnIntegrationForm.manualSalesIntegrationLogMessages("ManualSIIntegrationLogOnce");

                        trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\nGetting Sales Data... (100%) \r\n\n");
                        trnIntegrationForm.manualSalesIntegrationLogMessages("Get Successful!" + "\r\n\n");
                        trnIntegrationForm.manualSalesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                        trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\n");

                        break;
                    }
                    catch (Exception e)
                    {
                        trnIntegrationForm.manualSalesIntegrationLogMessages("Error: " + e.Message + "\r\n\n");
                        trnIntegrationForm.manualSalesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                        trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\n");

                        trnIntegrationForm.manualSalesIntegrationLogMessages("Retrying...\r\n\n");

                        Thread.Sleep(5000);
                    }
                }

                if (newSalesInvoices.Any())
                {
                    // ========
                    // Cleaning
                    // ========
                    trnIntegrationForm.manualSalesIntegrationLogMessages("Cleaning Temporary Sales... (0%) \r\n\n");
                    while (true)
                    {
                        try
                        {
                            String deleteTemporarySalesInvoiceTask = await DeleteTemporarySalesInvoice(domain);

                            if (!deleteTemporarySalesInvoiceTask.Equals("Clean Successful..."))
                            {
                                trnIntegrationForm.manualSalesIntegrationLogMessages(deleteTemporarySalesInvoiceTask);
                                trnIntegrationForm.manualSalesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\n");

                                trnIntegrationForm.manualSalesIntegrationLogMessages("Retrying...\r\n\n");

                                Thread.Sleep(5000);
                            }
                            else
                            {
                                trnIntegrationForm.manualSalesIntegrationLogMessages("ManualSIIntegrationLogOnce");

                                trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\nCleaning Temporary Sales... (100%) \r\n\n");
                                trnIntegrationForm.manualSalesIntegrationLogMessages("Clean Successful!" + "\r\n\n");
                                trnIntegrationForm.manualSalesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\n");

                                break;
                            }
                        }
                        catch (Exception e)
                        {
                            trnIntegrationForm.manualSalesIntegrationLogMessages("Error: " + e.Message + "\r\n\n");
                            trnIntegrationForm.manualSalesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                            trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\n");

                            trnIntegrationForm.manualSalesIntegrationLogMessages("Retrying...\r\n\n");

                            Thread.Sleep(5000);
                        }
                    }

                    Boolean post = false;

                    // =======
                    // Sending
                    // =======
                    trnIntegrationForm.manualSalesIntegrationLogMessages("Sending Sales... (0%) \r\n\n");
                    while (true)
                    {
                        try
                        {
                            Decimal percentage = 0;

                            Boolean send = false;
                            Int32   skip = 0;

                            for (Int32 i = 1; i <= newSalesInvoices.Count(); i++)
                            {
                                if (i % 100 == 0)
                                {
                                    jsonData = serializer.Serialize(newSalesInvoices.Skip(skip).Take(100));
                                    skip     = i;

                                    send       = true;
                                    percentage = Convert.ToDecimal((Convert.ToDecimal(skip) / Convert.ToDecimal(newSalesInvoices.Count())) * 100);
                                }
                                else
                                {
                                    if (i == newSalesInvoices.Count())
                                    {
                                        if (newSalesInvoices.Count() <= 100)
                                        {
                                            jsonData = serializer.Serialize(newSalesInvoices);
                                        }
                                        else
                                        {
                                            jsonData = serializer.Serialize(newSalesInvoices.Skip(skip).Take(i - skip));
                                        }

                                        send       = true;
                                        percentage = Convert.ToDecimal((Convert.ToDecimal(i) / Convert.ToDecimal(newSalesInvoices.Count())) * 100);
                                    }
                                }

                                if (send)
                                {
                                    while (true)
                                    {
                                        try
                                        {
                                            String insertTemporarySalesInvoiceTask = await InsertTemporarySalesInvoice(domain, jsonData);

                                            if (!insertTemporarySalesInvoiceTask.Equals("Send Successful..."))
                                            {
                                                trnIntegrationForm.manualSalesIntegrationLogMessages(insertTemporarySalesInvoiceTask);
                                                trnIntegrationForm.manualSalesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                                trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\n");

                                                trnIntegrationForm.manualSalesIntegrationLogMessages("Retrying...\r\n\n");

                                                Thread.Sleep(5000);
                                            }
                                            else
                                            {
                                                trnIntegrationForm.manualSalesIntegrationLogMessages("ManualSIIntegrationLogOnce");
                                                trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\nSending Sales... (" + Math.Round(percentage, 2) + "%) \r\n\n");

                                                if (i == newSalesInvoices.Count())
                                                {
                                                    trnIntegrationForm.manualSalesIntegrationLogMessages("Send Successful!" + "\r\n\n");
                                                    trnIntegrationForm.manualSalesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                                    trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\n");
                                                }

                                                break;
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            trnIntegrationForm.manualSalesIntegrationLogMessages("Sending Error: " + e.Message + "\r\n\n");
                                            trnIntegrationForm.manualSalesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                            trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\n");

                                            trnIntegrationForm.manualSalesIntegrationLogMessages("Retrying...\r\n\n");

                                            Thread.Sleep(5000);
                                        }
                                    }

                                    send = false;
                                }
                            }

                            post = true;
                            break;
                        }
                        catch (Exception e)
                        {
                            trnIntegrationForm.manualSalesIntegrationLogMessages("Error: " + e.Message + "\r\n\n");
                            trnIntegrationForm.manualSalesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                            trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\n");

                            trnIntegrationForm.manualSalesIntegrationLogMessages("Retrying...\r\n\n");

                            Thread.Sleep(5000);
                        }
                    }

                    // =======
                    // Posting
                    // =======
                    if (post)
                    {
                        trnIntegrationForm.manualSalesIntegrationLogMessages("Posting Sales... (0%) \r\n\n");
                        while (true)
                        {
                            try
                            {
                                var groupedSalesInvoices = from d in newSalesInvoices
                                                           group d by new
                                {
                                    d.BranchCode,
                                    d.ManualSINumber
                                } into g
                                select g.Key;

                                var salesInvoices = from d in groupedSalesInvoices.ToList() select d;
                                if (salesInvoices.Any())
                                {
                                    Decimal percentage = 0;
                                    Int32   count      = 0;

                                    foreach (var salesInvoice in salesInvoices.ToList())
                                    {
                                        count     += 1;
                                        percentage = Convert.ToDecimal((Convert.ToDecimal(count) / Convert.ToDecimal(salesInvoices.Count())) * 100);

                                        while (true)
                                        {
                                            try
                                            {
                                                String postSalesInvoiceTask = await PostSalesInvoice(domain, salesInvoice.BranchCode, salesInvoice.ManualSINumber);

                                                if (!postSalesInvoiceTask.Equals("Post Successful..."))
                                                {
                                                    trnIntegrationForm.manualSalesIntegrationLogMessages(postSalesInvoiceTask);
                                                    trnIntegrationForm.manualSalesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                                    trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\n");

                                                    trnIntegrationForm.manualSalesIntegrationLogMessages("Retrying...\r\n\n");

                                                    Thread.Sleep(5000);
                                                }
                                                else
                                                {
                                                    trnIntegrationForm.manualSalesIntegrationLogMessages("ManualSIIntegrationLogOnce");
                                                    trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\nPosting Sales... (" + Math.Round(percentage, 2) + "%) \r\n\n");

                                                    if (count == salesInvoices.Count())
                                                    {
                                                        trnIntegrationForm.manualSalesIntegrationLogMessages("Post Successful!" + "\r\n\n");
                                                        trnIntegrationForm.manualSalesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                                        trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\n");
                                                    }

                                                    break;
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                trnIntegrationForm.manualSalesIntegrationLogMessages("Posting Error: " + e.Message + "\r\n\n");
                                                trnIntegrationForm.manualSalesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                                trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\n");

                                                trnIntegrationForm.manualSalesIntegrationLogMessages("Retrying...\r\n\n");

                                                Thread.Sleep(5000);
                                            }
                                        }
                                    }
                                }

                                if (collectionIds.Any())
                                {
                                    foreach (var collectionId in collectionIds)
                                    {
                                        var collection = from d in posdb.TrnCollections
                                                         where d.Id == collectionId
                                                         select d;

                                        if (collection.Any())
                                        {
                                            String defaultManualSINumber = defaultReferenceNumberTimeStamp + "_" + collection.FirstOrDefault().MstCustomer.CustomerCode;

                                            var updateCollectionPostCode = collection.FirstOrDefault();
                                            updateCollectionPostCode.PostCode = defaultManualSINumber;

                                            posdb.SubmitChanges();
                                        }
                                    }
                                }

                                break;
                            }
                            catch (Exception e)
                            {
                                trnIntegrationForm.manualSalesIntegrationLogMessages("Error: " + e.Message + "\r\n\n");
                                trnIntegrationForm.manualSalesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\n");

                                trnIntegrationForm.manualSalesIntegrationLogMessages("Retrying...\r\n\n");

                                Thread.Sleep(5000);
                            }
                        }
                    }
                }
                else
                {
                    trnIntegrationForm.manualSalesIntegrationLogMessages("Data Source Empty \r\n\n");
                    trnIntegrationForm.manualSalesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                    trnIntegrationForm.manualSalesIntegrationLogMessages("\r\n\n");
                }
            }
        }
Ejemplo n.º 7
0
        // ==============
        // Get Item Price
        // ==============
        public Task GetItemPrice(String apiUrlHost, String branchCode)
        {
            try
            {
                DateTime dateTimeToday = DateTime.Now;
                String   currentDate   = Convert.ToDateTime(activityDate).ToString("MM-dd-yyyy", CultureInfo.InvariantCulture);

                // ============
                // Http Request
                // ============
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://" + apiUrlHost + "/api/get/POSIntegration/itemPrice/" + branchCode + "/" + currentDate);
                httpWebRequest.Method = "GET";
                httpWebRequest.Accept = "application/json";

                // ================
                // Process Response
                // ================
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    List <Entities.ISPOSTrnArticlePrice> itemPriceLists = (List <Entities.ISPOSTrnArticlePrice>)js.Deserialize(result, typeof(List <Entities.ISPOSTrnArticlePrice>));

                    if (itemPriceLists.Any())
                    {
                        foreach (var itemPrice in itemPriceLists)
                        {
                            var currentItem = from d in posdb.MstItems where d.BarCode.Equals(itemPrice.ItemCode) && d.IsLocked == true select d;
                            if (currentItem.Any())
                            {
                                var currentItemPrice = from d in posdb.MstItemPrices where d.ItemId == currentItem.FirstOrDefault().Id&& d.PriceDescription.Equals("IP-" + itemPrice.BranchCode + "-" + itemPrice.IPNumber + " (" + itemPrice.IPDate + ")") select d;
                                if (!currentItemPrice.Any())
                                {
                                    trnIntegrationForm.salesIntegrationLogMessages("Saving Item Price: IP-" + itemPrice.BranchCode + "-" + itemPrice.IPNumber + " (" + itemPrice.IPDate + ")" + "\r\n\n");
                                    trnIntegrationForm.salesIntegrationLogMessages("Current Item: " + currentItem.FirstOrDefault().ItemDescription + "\r\n\n");

                                    InnosoftPOSData.MstItemPrice newPrice = new InnosoftPOSData.MstItemPrice()
                                    {
                                        ItemId           = currentItem.FirstOrDefault().Id,
                                        PriceDescription = "IP-" + itemPrice.BranchCode + "-" + itemPrice.IPNumber + " (" + itemPrice.IPDate + ")",
                                        Price            = itemPrice.Price,
                                        TriggerQuantity  = itemPrice.TriggerQuantity
                                    };

                                    posdb.MstItemPrices.InsertOnSubmit(newPrice);

                                    var updateCurrentItem = currentItem.FirstOrDefault();
                                    updateCurrentItem.Price = itemPrice.Price;
                                    posdb.SubmitChanges();

                                    trnIntegrationForm.salesIntegrationLogMessages("Save Successful!" + "\r\n\n");
                                    trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                    trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");
                                }
                            }
                            else
                            {
                                trnIntegrationForm.salesIntegrationLogMessages("Cannot Save Item Price: IP-" + itemPrice.BranchCode + "-" + itemPrice.IPNumber + " (" + itemPrice.IPDate + ")" + "..." + "\r\n\n");
                                trnIntegrationForm.salesIntegrationLogMessages("Price: " + itemPrice.Price + "\r\n\n");
                                trnIntegrationForm.salesIntegrationLogMessages("Item Not Found!" + "\r\n\n");
                                trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                                trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");
                            }
                        }
                    }
                }

                return(Task.FromResult(""));
            }
            catch (Exception e)
            {
                trnIntegrationForm.salesIntegrationLogMessages("Item Price Error: " + e.Message + "\r\n\n");
                trnIntegrationForm.salesIntegrationLogMessages("Time Stamp: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "\r\n\n");
                trnIntegrationForm.salesIntegrationLogMessages("\r\n\n");

                return(Task.FromResult(""));
            }
        }