private void SendMessage(SyncType syncType, string inventoryId, string message, string warehouse)
        {
            LazyInitializer.EnsureInitialized(ref this._msmq, ref this._msmqInitialized, ref this._msmqLock, () =>
                                              new MessageQueue(this.Address)
            {
                Formatter = new StringMessageFormatter(),
                DefaultPropertiesToSend = new DefaultPropertiesToSend
                {
                    Recoverable        = true,
                    UseDeadLetterQueue = true,
                    TimeToReachQueue   = TimeSpan.FromMinutes(1.0)
                }
            });

            Message previousMessage = null;

            int count = KCMSMQueueReader.GetMessagesCount(_msmq);

            if (count > 0)
            {
                Cursor cursor = _msmq.CreateCursor();
                try
                {
                    Message msg = _msmq.Peek(TimeSpan.FromSeconds(3), cursor, PeekAction.Current);
                    if (IsPreviousMessage(syncType, inventoryId, msg, warehouse))
                    {
                        previousMessage = msg;
                    }

                    if (previousMessage == null)
                    {
                        while (count > 0)
                        {
                            msg = _msmq.Peek(TimeSpan.FromSeconds(3), cursor, PeekAction.Next);
                            if (IsPreviousMessage(syncType, inventoryId, msg, warehouse))
                            {
                                previousMessage = msg;
                            }

                            count--;
                        }
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    cursor.Dispose();
                }
            }

            _msmq.Send(message);

            if (previousMessage != null)
            {
                _msmq.ReceiveById(previousMessage.Id);
            }
        }
        public void ProcessMessageAPI(KCPriceAndInventoryMessage message)
        {
            KCDataExchangeMaint      masterGraph = PXGraph.CreateInstance <KCDataExchangeMaint>();
            KCSiteMaster             connection  = masterGraph.Connection.SelectSingle();
            KCARestClient            client      = new KCARestClient(connection);
            KCInventoryItemAPIHelper helper      = new KCInventoryItemAPIHelper(client, logger.LoggerProperties);
            KCPriceAndInventoryMaint graph       = PXGraph.CreateInstance <KCPriceAndInventoryMaint>();


            var syncType = KCMSMQQueueHelper.ParseSyncQueueName(message.Address);
            var syncName = KCMSMQQueueHelper.GetSyncName(syncType);
            PushNotificationsHook pnHook = graph.PushNotification.SelectSingle(syncName);

            if (syncType == SyncType.InventoryQuantity)
            {
                KCMSMQueueReader quantity = null;
                try
                {
                    quantity = new KCMSMQueueReader(pnHook.Address);
                    if (quantity.TryReceiveMessage(message.MessageID, out var msg))
                    {
                        foreach (object product in msg.Inserted)
                        {
                            KCMSMQInventoryQuantity quantityProduct = JsonConvert.DeserializeObject <KCMSMQInventoryQuantity>(product.ToString());
                            UpdateSiteQuantity(masterGraph, quantityProduct, helper);
                        }
                    }
                }
                finally
                {
                    quantity?.Dispose();
                }
            }
            else if (syncType == SyncType.InventoryPrice)
            {
                KCMSMQueueReader price = null;
                try
                {
                    price = new KCMSMQueueReader(pnHook.Address);
                    if (price.TryReceiveMessage(message.MessageID, out var msg))
                    {
                        foreach (object product in msg.Inserted)
                        {
                            KCMSMQInventoryPrice priceProduct = JsonConvert.DeserializeObject <KCMSMQInventoryPrice>(product?.ToString());
                            UpdatePrice(masterGraph, priceProduct, helper);
                        }
                    }
                }
                finally
                {
                    price?.Dispose();
                }
            }
            else if (syncType == SyncType.VendorQuantity)
            {
                KCMSMQueueReader vendor = null;
                try
                {
                    vendor = new KCMSMQueueReader(pnHook.Address);
                    if (vendor.TryReceiveMessage(message.MessageID, out var msg))
                    {
                        foreach (object product in msg.Inserted)
                        {
                            KCMSMQInventoryQuantity quantityProduct = JsonConvert.DeserializeObject <KCMSMQInventoryQuantity>(product.ToString());
                            UpdateSiteQuantity(masterGraph, quantityProduct, helper);
                        }
                    }
                }
                finally
                {
                    vendor?.Dispose();
                }
            }
        }
        public void ProcessMessageFTP(List <KCPriceAndInventoryMessage> messages, CancellationToken cancellationToken)
        {
            KCDataExchangeMaint      masterGraph = PXGraph.CreateInstance <KCDataExchangeMaint>();
            KCPriceAndInventoryMaint graph       = PXGraph.CreateInstance <KCPriceAndInventoryMaint>();


            var MSMQQuantityUpdates = new Dictionary <string, List <KCAPIQuantity> >();
            var MSMQPrices          = new Dictionary <string, KCAPIInventoryItem>();

            var productsForExportPrice    = new List <KeyValuePair <string, InventoryItem> >();
            var productsForExportQuantity = new Dictionary <string, KeyValuePair <string, InventoryItem> >();

            PushNotificationsHook pricePN    = graph.PushNotification.SelectSingle(KCMSMQQueueHelper.GetSyncName(SyncType.InventoryPrice));
            PushNotificationsHook quantityPN = graph.PushNotification.SelectSingle(KCMSMQQueueHelper.GetSyncName(SyncType.InventoryQuantity));
            PushNotificationsHook vendorPN   = graph.PushNotification.SelectSingle(KCMSMQQueueHelper.GetSyncName(SyncType.VendorQuantity));

            KCMSMQueueReader price    = null;
            KCMSMQueueReader quantity = null;
            KCMSMQueueReader vendor   = null;

            try
            {
                price    = new KCMSMQueueReader(pricePN.Address);
                quantity = new KCMSMQueueReader(quantityPN.Address);
                vendor   = new KCMSMQueueReader(vendorPN.Address);

                foreach (KCPriceAndInventoryMessage msg in messages)
                {
                    var syncType = KCMSMQQueueHelper.ParseSyncQueueName(msg.Address);
                    if (syncType == SyncType.InventoryQuantity || syncType == SyncType.VendorQuantity)
                    {
                        List <object> insertedMessages = null;
                        if (syncType == SyncType.InventoryQuantity && quantity.TryReceiveMessage(msg.MessageID, out var invMessage))
                        {
                            insertedMessages = invMessage.Inserted;
                        }
                        if (syncType == SyncType.VendorQuantity && vendor.TryReceiveMessage(msg.MessageID, out var vendorMessage))
                        {
                            insertedMessages = vendorMessage.Inserted;
                        }

                        if (insertedMessages != null)
                        {
                            foreach (object item in insertedMessages)
                            {
                                KCMSMQInventoryQuantity quantityProduct = JsonConvert.DeserializeObject <KCMSMQInventoryQuantity>(item.ToString());
                                InventoryItem           inventoryItem   = masterGraph.ProductByInvCd.Select(quantityProduct.InventoryID.Trim());
                                KNSIKCInventoryItem     kcProduct       = masterGraph.KCInventoryItem.SelectSingle(inventoryItem.InventoryID);

                                if (kcProduct.UsrKCCAID != null)
                                {
                                    var key = inventoryItem.InventoryCD;

                                    if (!MSMQQuantityUpdates.ContainsKey(key))
                                    {
                                        MSMQQuantityUpdates.Add(key, quantityProduct.Updates);
                                    }

                                    if (!productsForExportQuantity.ContainsKey(key))
                                    {
                                        InventoryItemPCExt inventoryItemPCExt = inventoryItem.GetExtension <InventoryItemPCExt>();

                                        productsForExportQuantity.Add(inventoryItem.InventoryCD, new KeyValuePair <string, InventoryItem>(inventoryItemPCExt.UsrKNCompositeType, inventoryItem));
                                    }
                                }
                            }
                        }
                    }
                    else if (syncType == SyncType.InventoryPrice)
                    {
                        if (price.TryReceiveMessage(msg.MessageID, out var message))
                        {
                            foreach (object item in message.Inserted)
                            {
                                KCMSMQInventoryPrice priceProduct       = JsonConvert.DeserializeObject <KCMSMQInventoryPrice>(item.ToString());
                                InventoryItem        inventoryItem      = masterGraph.ProductByInvCd.Select(priceProduct.InventoryID.Trim());
                                InventoryItemPCExt   inventoryItemPCExt = inventoryItem.GetExtension <InventoryItemPCExt>();

                                productsForExportPrice.Add(new KeyValuePair <string, InventoryItem>(inventoryItemPCExt.UsrKNCompositeType, inventoryItem));
                                MSMQPrices.Add(inventoryItem.InventoryCD, KCMapInventoryItem.GetAPIMSMQInventoryPrice(priceProduct));
                            }
                        }
                    }
                }
            }
            finally
            {
                price?.Dispose();
                quantity?.Dispose();
                vendor?.Dispose();
            }

            Export(masterGraph, productsForExportPrice, cancellationToken, MSMQPrices);
            Export(masterGraph, productsForExportQuantity.Values.ToList(), cancellationToken, null, MSMQQuantityUpdates);
        }