Beispiel #1
0
        public void RetrieveChannelAdvisorIds(KCStore store)
        {
            KCDataExchangeMaint      graph      = PXGraph.CreateInstance <KCDataExchangeMaint>();
            KCSiteMaster             connection = graph.Connection.Select().RowCast <KCSiteMaster>().First(x => x.SiteMasterCD.Equals(store.SiteMasterCD));
            KCARestClient            client     = new KCARestClient(connection);
            KCInventoryItemAPIHelper helper     = new KCInventoryItemAPIHelper(client);

            foreach (KCAPIIdSkuJuxtaposion juxtaposion in helper.GetAllIdSkuJuxtaposions())
            {
                InventoryItem item = graph.ItemByCd.SelectSingle(juxtaposion.Sku);
                if (item == null)
                {
                    continue;
                }
                KNSIKCInventoryItem cItem = graph.KCInventoryItem.SelectSingle(item.InventoryID);
                if (cItem != null && cItem.UsrKCActiveOnCa.GetValueOrDefault())
                {
                    cItem.UsrKCCAID       = juxtaposion.ID;
                    cItem.UsrKCCAParentID = juxtaposion.ParentProductID.ToString();
                    graph.KCInventoryItem.Update(cItem);
                }
            }

            graph.Actions.PressSave();

            if (logger != null)
            {
                logger.ClearLoggingIds();
                logger.Information(KCMessages.ProductIdsRetrievalSuccess);
            }
        }
        public ODataShipment GetAPIShipment(SOShipment shipment, SOPackageDetail package, PXResultset <SOLine> lines)
        {
            var wrapper     = new ODataShipment();
            var apiShipment = new KCAPIShipment()
            {
                ShippedDateUtc  = shipment.ShipDate,
                TrackingNumber  = package?.TrackNumber,
                ShippingCarrier = shipment.ShipVia,
                ShippingClass   = shipment.ShipVia,
                DeliveryStatus  = "Complete"
            };
            var apiItems = new List <KCAPIShipmentItem>();

            foreach (PXResult <SOLine> line in lines)
            {
                SOLine     soLine   = line.GetItem <SOLine>();
                SOShipLine shipLine = line.GetItem <SOShipLine>();
                DAC.KNSIKCInventoryItem kcInventoryItem    = line.GetItem <DAC.KNSIKCInventoryItem>();
                InventoryItem           inventoryItem      = line.GetItem <InventoryItem>();
                InventoryItemPCExt      inventoryItemPcExt = inventoryItem.GetExtension <InventoryItemPCExt>();
                SOLinePCExt             soLinePcExt        = soLine.GetExtension <SOLinePCExt>();

                int quantity = Convert.ToInt32(shipLine.ShippedQty.GetValueOrDefault());

                if ((soLine.LineNbr != soLinePcExt?.UsrKNMasterLineNbr && ParentIsGrouped(lines, soLinePcExt)) ||
                    (inventoryItemPcExt?.UsrKNCompositeType == KCConstants.ConfigurableProduct && soLine.LineNbr == soLinePcExt?.UsrKNMasterLineNbr))
                {
                    continue;
                }

                if (inventoryItemPcExt?.UsrKNCompositeType == KCConstants.GroupedProduct && quantity == 0)
                {
                    SOShipLine childItem = lines.Where(x => x.GetItem <SOLine>().GetExtension <SOLinePCExt>().UsrKNMasterLineNbr == soLine.LineNbr &&
                                                       x.GetItem <SOLine>().LineNbr != soLine.LineNbr).RowCast <SOShipLine>().FirstOrDefault();

                    if (childItem != null)
                    {
                        KCDataExchangeMaint     graph          = PXGraph.CreateInstance <KCDataExchangeMaint>();
                        List <KNSIGroupedItems> origChildItems = graph.GroupedChildItems.Select(inventoryItem.InventoryID).RowCast <KNSIGroupedItems>().ToList();
                        quantity = Convert.ToInt32(childItem.ShippedQty / origChildItems.Where(x => x.MappedInventoryID == childItem.InventoryID).FirstOrDefault().Quantity);
                    }
                    else
                    {
                        throw new KCCorruptedShipmentException();
                    }
                }

                apiItems.Add(new KCAPIShipmentItem
                {
                    ProductID   = kcInventoryItem?.UsrKCCAID.ToString(),
                    OrderItemID = soLine?.GetExtension <KCSOLineExt>()?.UsrKCOrderItemID.ToString(),
                    Quantity    = shipLine == null ? 0 : Convert.ToInt32(quantity)
                });
            }

            apiShipment.Items = apiItems;
            wrapper.Value     = apiShipment;

            return(wrapper);
        }
Beispiel #3
0
        public void DeleteObsoleteBundleComponents(List <KCAPIBundleComponent> bundleComponents, InventoryItem product)
        {
            var caid = BulkGraph.KCInventoryItem.SelectSingle(product.InventoryID)?.UsrKCCAID;

            if (caid.GetValueOrDefault() < 1)
            {
                return;
            }
            KCDataExchangeMaint      graph                 = PXGraph.CreateInstance <KCDataExchangeMaint>();
            KCSiteMaster             connection            = graph.Connection.SelectSingle();
            KCARestClient            client                = new KCARestClient(connection);
            KCInventoryItemAPIHelper helper                = new KCInventoryItemAPIHelper(client, LoggerProperties);
            KCODataWrapper <KCAPIBundleComponent> response = helper.GetBundleComponents(caid);

            if (response == null)
            {
                return;
            }
            List <KCAPIBundleComponent> cBundleComponents = helper.GetBundleComponents(caid).Value.ToList();

            foreach (KCAPIBundleComponent component in cBundleComponents.Where(x => bundleComponents.All(y => y.ComponentSku != x.ComponentSku)))
            {
                helper.DeleteBundleComponent(caid, component.ComponentID, product.InventoryCD, component.ComponentID.ToString());
            }
        }
        private void UpdateSiteQuantity(KCDataExchangeMaint masterGraph, KCMSMQInventoryQuantity quantityProduct, KCInventoryItemAPIHelper helper)
        {
            InventoryItem inventoryItem = masterGraph.ProductByInvCd.Select(quantityProduct.InventoryID.Trim());

            if (inventoryItem != null)
            {
                KNSIKCInventoryItem kcProduct = masterGraph.KCInventoryItem.SelectSingle(inventoryItem.InventoryID);
                if (kcProduct.UsrKCCAID != null)
                {
                    logger.SetParentAndEntityIds(null, inventoryItem.InventoryCD);

                    try
                    {
                        APIQuantityValue apiQuantity = new APIQuantityValue
                        {
                            Value = new APIUpdates()
                            {
                                UpdateType = "InStock",
                                Updates    = quantityProduct.Updates
                            }
                        };

                        helper.UpdateQuantity(apiQuantity, kcProduct.UsrKCCAID);

                        logger.Information(KCMessages.MSMQSyncAPI);
                    }
                    catch (Exception e)
                    {
                        logger.Information(e.Message);
                    }
                }
            }
        }
        private void UpdatePrice(KCDataExchangeMaint masterGraph, KCMSMQInventoryPrice priceProduct, KCInventoryItemAPIHelper helper)
        {
            InventoryItem inventoryItem = masterGraph.ProductByInvCd.Select(priceProduct.InventoryID.Trim());

            if (inventoryItem != null)
            {
                KNSIKCInventoryItem kcProduct          = masterGraph.KCInventoryItem.SelectSingle(inventoryItem.InventoryID);
                InventoryItemPCExt  inventoryItemPCExt = inventoryItem.GetExtension <InventoryItemPCExt>();
                InventoryItem       parent             = KCGeneralDataHelper.GetInventoryItemByInventoryId(PXGraph.CreateInstance <KCDataExchangeMaint>(), inventoryItemPCExt.UsrKNCompositeID);

                logger.SetParentAndEntityIds(parent?.InventoryCD, inventoryItem.InventoryCD);

                try
                {
                    if (kcProduct.UsrKCCAID != null)
                    {
                        helper.EditProduct(KCMapInventoryItem.GetAPIMSMQInventoryPrice(priceProduct), kcProduct.UsrKCCAID);
                    }

                    logger.Information(KCMessages.MSMQSyncAPI);
                }
                catch (Exception e)
                {
                    logger.Information(e.Message);
                }
            }
        }
        public static int?GetCAIDByInventoryId(KCDataExchangeMaint graph, KCInventoryItemAPIHelper helper, int?id)
        {
            InventoryItem         product      = graph.ProductByInvId.Select(id);
            KCAPIInventoryItemIDs existingItem = GetExistingCAProductByInventoryItemCd(helper, product.InventoryCD);

            return(existingItem?.ID);
        }
Beispiel #7
0
        public static KCAPIBundleComponent GetAPIGroupedBundleComponent(KCInventoryItemAPIHelper helper, int?productId, KNSIGroupedItems component)
        {
            KCDataExchangeMaint graph = PXGraph.CreateInstance <KCDataExchangeMaint>();

            return(new KCAPIBundleComponent()
            {
                ProductID = productId.GetValueOrDefault(),
                ComponentID = KCGeneralDataHelper.GetCAIDByInventoryId(graph, helper, component.MappedInventoryID).GetValueOrDefault(),
                Quantity = Convert.ToInt32(component.Quantity)
            });
        }
        private void Export(KCDataExchangeMaint graph, List <KeyValuePair <string, InventoryItem> > products, CancellationToken cancellationToken,
                            Dictionary <string, KCAPIInventoryItem> MSMQPrices = null, Dictionary <string, List <KCAPIQuantity> > MSMQQuantityUpdates = null)
        {
            KCBulkProductMaint bulkGraph    = PXGraph.CreateInstance <KCBulkProductMaint>();
            KCSiteMaster       connection   = graph.Connection.SelectSingle();
            KCARestClient      client       = new KCARestClient(connection);
            KCBulkUploader     bulkUploader = new KCBulkUploader(bulkGraph, logger.LoggerProperties, cancellationToken)
            {
                ApiHelper = new KCInventoryItemAPIHelper(client, logger.LoggerProperties),
                _strategy = new KCFullSync(bulkGraph)
            };
            List <KCBulkProduct> dtos = bulkUploader.HandleItems(products, MSMQPrices, MSMQQuantityUpdates);

            if (dtos.Count > 0)
            {
                string bulkFile        = bulkUploader.PrepareItemBulkFile(dtos);
                string productFilePath = bulkUploader.GenerateProductUploadPath(connection);
                bulkUploader.UploadFileToFTP(connection, bulkFile, productFilePath);
            }
        }
Beispiel #9
0
        public void ExportShipments(KCStore store)
        {
            if (store.DateTo < store.DateFrom)
            {
                throw new PXException(KCMessages.DateToBiggerThanDateFrom);
            }
            if (store.DateTo.GetValueOrDefault() != default)
            {
                store.DateTo = store.DateTo.GetValueOrDefault().AddDays(1);
            }
            bool anyExported             = false;
            KCDataExchangeMaint    graph = PXGraph.CreateInstance <KCDataExchangeMaint>();
            SOOrderShipmentProcess orderShipmentGraph = PXGraph.CreateInstance <SOOrderShipmentProcess>();

            KCSiteMaster        connection     = graph.Connection.Select().RowCast <KCSiteMaster>().Where(x => x.SiteMasterCD.Equals(store.SiteMasterCD)).First();
            KCARestClient       client         = new KCARestClient(connection);
            KCOrderAPIHelper    helperOrder    = new KCOrderAPIHelper(client);
            KCShipmentAPIHelper helperShipment = new KCShipmentAPIHelper(client);

            List <SOOrder> orders = graph.Orders.Select().RowCast <SOOrder>().Where(x =>
                                                                                    x.GetExtension <KCSOOrderExt>().UsrKCSiteName?.EndsWith("/Non-FBA") == true).ToList();

            foreach (SOOrder order in orders)
            {
                IEnumerable <SOOrderShipment> orderShipments = KCGeneralDataHelper.GetOrderShipmentsByOrderNbr(graph, order.OrderNbr);
                if (orderShipments == null)
                {
                    continue;
                }

                foreach (SOOrderShipment orderShipment in orderShipments)
                {
                    if (!CheckData(orderShipment, store.DateFrom, store.DateTo))
                    {
                        continue;
                    }
                    PXResultset <SOLine> lines    = graph.OrderLines.Select(orderShipment.ShipmentNbr);
                    SOShipment           shipment = KCGeneralDataHelper.GetShipmentByShipmentNbr(orderShipmentGraph, orderShipment.ShipmentNbr);
                    if (!CheckShippingCarrier(helperShipment, shipment))
                    {
                        logger.Information(KCMessages.ShipViaDoesnotExist(shipment.ShipmentNbr));
                        continue;
                    }
                    KCSOShipmentExt shipmentKCExt  = shipment?.GetExtension <KCSOShipmentExt>();
                    SOPackageDetail package        = KCGeneralDataHelper.GetPackageByShipmentNbr(orderShipmentGraph, orderShipment.ShipmentNbr);
                    KCSOOrderExt    orderExt       = order.GetExtension <KCSOOrderExt>();
                    KCMapShipment   shipmentMapper = new KCMapShipment();

                    int?customerOrderNbr = Convert.ToInt32(order.CustomerOrderNbr);

                    if (shipment != null && orderShipment.Confirmed.GetValueOrDefault() &&
                        KCGeneralDataHelper.GetExistingCAOrderById(helperOrder, customerOrderNbr) != null && shipmentKCExt?.UsrKCExported != true)

                    {
                        string          log;
                        KCErrorResponse response = new KCErrorResponse();
                        logger.SetParentAndEntityIds(order.OrderNbr, shipment.ShipmentNbr);

                        try
                        {
                            response = helperShipment.MarkTheOrderAsShipped(shipmentMapper.GetAPIShipment(shipment, package, lines), customerOrderNbr);
                        }
                        catch (Exception ex)
                        {
                            log = KCMessages.CorruptedShipment(shipment.ShipmentNbr);
                            logger.Information(log);
                            continue;
                        }


                        if (response != null)
                        {
                            log = KCMessages.ShipmentExportFailure(shipment.ShipmentNbr, response.Error.Message);
                        }
                        else
                        {
                            shipmentKCExt.UsrKCExported = true;
                            orderShipmentGraph.Shipments.Update(shipment);
                            orderShipmentGraph.Save.Press();
                            anyExported = true;
                            log         = KCMessages.ShipmentExported(shipment.ShipmentNbr);
                        }

                        logger.Information(log);
                    }
                }
            }

            logger.ClearLoggingIds();
            logger.Information(anyExported ? KCMessages.ShipmentExportSuccess : KCMessages.NoShipmentsToExport);
        }
        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);
        }