protected virtual void refreshDistributionCenters()
        {
            PXResultset <KCDistributionCenter> existingDistributionCenters = DistributionCenters.Select();
            List <string> existingNames = new List <string>();
            List <KCAPIDistributionCenter> newCenters = new List <KCAPIDistributionCenter>();

            KCSiteMaster             connection = Connection.SelectSingle();
            KCARestClient            client     = new KCARestClient(connection);
            KCInventoryItemAPIHelper helper     = new KCInventoryItemAPIHelper(client);

            FillDistributionCentersFromCA(helper, newCenters);

            existingDistributionCenters.RowCast <KCDistributionCenter>().ForEach(x => existingNames.Add(x.DistributionCenterName));

            foreach (KCAPIDistributionCenter newCenter in newCenters)
            {
                if (!existingNames.Contains(newCenter.Name))
                {
                    DistributionCenters.Insert(new KCDistributionCenter()
                    {
                        DistributionCenterID   = newCenter.ID,
                        DistributionCenterName = newCenter.Name,
                        Code = newCenter.Code
                    });
                }
            }

            this.Persist(typeof(KCDistributionCenter), PXDBOperation.Insert);

            foreach (string existingName in existingNames)
            {
                if (!newCenters.Any(x => x.Name == existingName))
                {
                    KCDistributionCenter item = DistributionCenterByName.SelectSingle(existingName);
                    DistributionCenters.Delete(item);
                    KCInventoryManagement deletedMapping = Mapping.Select().RowCast <KCInventoryManagement>().FirstOrDefault(x => x.DistributionCenterID == item.DistributionCenterID);
                    if (deletedMapping != null)
                    {
                        Mapping.Delete(deletedMapping);
                    }
                }
            }

            bool defaultDcDeleted = DistributionCenters.Cache.Deleted.RowCast <KCDistributionCenter>().Any(x => x.DistributionCenterID == InventoryTrackingRule.Current.DefaultDistributionCenterID);

            if (InventoryTrackingRule.Current.InventoryTrackingRule == null)
            {
                InventoryTrackingRule.Current.InventoryTrackingRule = KCInventoryTrackingRulesConstants.Consolidate;
            }

            if (InventoryTrackingRule.Current.DefaultDistributionCenterID == null || defaultDcDeleted)
            {
                InventoryTrackingRule.Current.DefaultDistributionCenterID = GetDefaultDistributionCenter(helper, newCenters);
            }

            InventoryTrackingRule.Update(InventoryTrackingRule.Current);
            InventoryTrackingRule.Cache.SetStatus(InventoryTrackingRule.Current, PXEntryStatus.Updated);
            Connection.Cache.SetStatus(connection, PXEntryStatus.Notchanged);
            Actions.PressSave();
        }
        protected virtual void KCInventoryManagement_RowPersisting(PXCache sender, PXRowPersistingEventArgs e)
        {
            if (e == null || e.Row == null)
            {
                return;
            }

            KCInventoryManagement row = e.Row as KCInventoryManagement;

            if (IsWarehouseEmpty(row) && row.IncludeVendor != true && Connection.SelectSingle().InventoryTrackingRule == KCInventoryTrackingRulesConstants.Manage)
            {
                sender.RaiseExceptionHandling <KCInventoryManagement.siteid>(e.Row, null, new Exception(KCMessages.WarehouseCanNotBeEmpty));
                throw new PXException(KCMessages.WarehouseCanNotBeEmpty);
            }
        }
 protected virtual bool IsWarehouseEmpty(KCInventoryManagement distributionCenterMapping)
 {
     return(distributionCenterMapping.IsMapped == true && distributionCenterMapping.Siteid == null);
 }