Simple Client Model for FinancialBatch
Beispiel #1
0
 /// <summary>
 /// Copies the base properties from a source FinancialBatch object
 /// </summary>
 /// <param name="source">The source.</param>
 public void CopyPropertiesFrom( FinancialBatch source )
 {
     this.Id = source.Id;
     this.AccountingSystemCode = source.AccountingSystemCode;
     this.BatchEndDateTime = source.BatchEndDateTime;
     this.BatchStartDateTime = source.BatchStartDateTime;
     this.CampusId = source.CampusId;
     this.ControlAmount = source.ControlAmount;
     this.ForeignGuid = source.ForeignGuid;
     this.ForeignKey = source.ForeignKey;
     this.ModifiedAuditValuesAlreadyUpdated = source.ModifiedAuditValuesAlreadyUpdated;
     this.Name = source.Name;
     this.Note = source.Note;
     this.Status = source.Status;
     this.CreatedDateTime = source.CreatedDateTime;
     this.ModifiedDateTime = source.ModifiedDateTime;
     this.CreatedByPersonAliasId = source.CreatedByPersonAliasId;
     this.ModifiedByPersonAliasId = source.ModifiedByPersonAliasId;
     this.Guid = source.Guid;
     this.ForeignId = source.ForeignId;
 }
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnSave_Click( object sender, RoutedEventArgs e )
        {
            try
            {
                RockConfig rockConfig = RockConfig.Load();
                RockRestClient client = new RockRestClient( rockConfig.RockBaseUrl );
                client.Login( rockConfig.Username, rockConfig.Password );

                FinancialBatch financialBatch = null;
                if ( SelectedFinancialBatch == null || SelectedFinancialBatch.Id == 0 )
                {
                    financialBatch = new FinancialBatch { Id = 0, Guid = Guid.NewGuid(), Status = BatchStatus.Pending, CreatedByPersonAliasId = LoggedInPerson.PrimaryAliasId };
                }
                else
                {
                    financialBatch = client.GetData<FinancialBatch>( string.Format( "api/FinancialBatches/{0}", SelectedFinancialBatch.Id ) );
                }

                txtBatchName.Text = txtBatchName.Text.Trim();
                if ( string.IsNullOrWhiteSpace( txtBatchName.Text ) )
                {
                    txtBatchName.Style = this.FindResource( "textboxStyleError" ) as Style;
                    return;
                }
                else
                {
                    txtBatchName.Style = this.FindResource( "textboxStyle" ) as Style;
                }

                financialBatch.Name = txtBatchName.Text;
                Campus selectedCampus = cbCampus.SelectedItem as Campus;
                if ( selectedCampus.Id > 0 )
                {
                    financialBatch.CampusId = selectedCampus.Id;
                }
                else
                {
                    financialBatch.CampusId = null;
                }

                financialBatch.BatchStartDateTime = dpBatchDate.SelectedDate;

                if ( !string.IsNullOrWhiteSpace( txtControlAmount.Text ) )
                {
                    financialBatch.ControlAmount = decimal.Parse( txtControlAmount.Text.Replace( "$", string.Empty ) );
                }
                else
                {
                    financialBatch.ControlAmount = 0.00M;
                }

                txtNote.Text = txtNote.Text.Trim();
                financialBatch.Note = txtNote.Text;

                if ( financialBatch.Id == 0 )
                {
                    client.PostData<FinancialBatch>( "api/FinancialBatches/", financialBatch );
                }
                else
                {
                    client.PutData<FinancialBatch>( "api/FinancialBatches/", financialBatch, financialBatch.Id );
                }

                if ( SelectedFinancialBatch == null || SelectedFinancialBatch.Id == 0 )
                {
                    // refetch the batch to get the Id if it was just Inserted
                    financialBatch = client.GetDataByGuid<FinancialBatch>( "api/FinancialBatches", financialBatch.Guid );

                    SelectedFinancialBatch = financialBatch;
                }

                LoadFinancialBatchesGrid();
                UpdateBatchUI( financialBatch );

                ShowBatch( false );
            }
            catch ( Exception ex )
            {
                ShowException( ex );
            }
        }
        /// <summary>
        /// Updates the batch UI.
        /// </summary>
        /// <param name="selectedBatch">The selected batch.</param>
        private void UpdateBatchUI( FinancialBatch selectedBatch )
        {
            if ( selectedBatch == null )
            {
                grdBatchItems.DataContext = null;
                DisplayTransactionCount();
                HideBatch();
                return;
            }
            else
            {
                ShowBatch( false );
            }

            RockConfig rockConfig = RockConfig.Load();
            RockRestClient client = new RockRestClient( rockConfig.RockBaseUrl );
            client.Login( rockConfig.Username, rockConfig.Password );
            SelectedFinancialBatch = selectedBatch;
            lblBatchNameReadOnly.Content = selectedBatch.Name;
            lblBatchIdReadOnly.Content = string.Format( "Batch Id: {0}", selectedBatch.Id );

            lblBatchCampusReadOnly.Content = selectedBatch.CampusId.HasValue ? client.GetData<Campus>( string.Format( "api/Campuses/{0}", selectedBatch.CampusId ?? 0 ) ).Name : string.Empty;
            lblBatchDateReadOnly.Content = selectedBatch.BatchStartDateTime.Value.ToString( "d" );
            var createdByPerson = client.GetData<Person>( string.Format( "api/People/GetByPersonAliasId/{0}", selectedBatch.CreatedByPersonAliasId ?? 0 ) );
            if ( createdByPerson != null )
            {
                lblBatchCreatedByReadOnly.Content = string.Format("{0} {1}", createdByPerson.NickName, createdByPerson.LastName);
            }
            else
            {
                lblBatchCreatedByReadOnly.Content = string.Empty;
            }

            lblBatchControlAmountReadOnly.Content = selectedBatch.ControlAmount.ToString( "F" );

            txtBatchName.Text = selectedBatch.Name;
            if ( selectedBatch.CampusId.HasValue )
            {
                cbCampus.SelectedValue = selectedBatch.CampusId;
            }
            else
            {
                cbCampus.SelectedValue = 0;
            }

            dpBatchDate.SelectedDate = selectedBatch.BatchStartDateTime;
            lblCreatedBy.Content = lblBatchCreatedByReadOnly.Content as string;
            txtControlAmount.Text = selectedBatch.ControlAmount.ToString( "F" );
            txtNote.Text = selectedBatch.Note;

            // start a background thread to download transactions since this could take a little while and we want a Wait cursor
            BackgroundWorker bw = new BackgroundWorker();
            Rock.Wpf.WpfHelper.FadeOut( lblCount, 0 );
            lblCount.Content = "Loading...";
            Rock.Wpf.WpfHelper.FadeIn( lblCount, 300 );
            List<FinancialTransaction> transactions = null;
            grdBatchItems.DataContext = null;
            bw.DoWork += delegate( object s, DoWorkEventArgs ee )
            {
                ee.Result = null;

                transactions = client.GetData<List<FinancialTransaction>>( "api/FinancialTransactions/", string.Format( "BatchId eq {0}", selectedBatch.Id ) );
            };

            bw.RunWorkerCompleted += delegate( object s, RunWorkerCompletedEventArgs ee )
            {
                this.Cursor = null;
                foreach ( var transaction in transactions )
                {
                    if ( transaction.FinancialPaymentDetailId.HasValue )
                    {
                        transaction.FinancialPaymentDetail = transaction.FinancialPaymentDetail ?? client.GetData<FinancialPaymentDetail>( string.Format( "api/FinancialPaymentDetails/{0}", transaction.FinancialPaymentDetailId ?? 0 ) );
                        if ( transaction.FinancialPaymentDetail != null )
                        {
                            transaction.FinancialPaymentDetail.CurrencyTypeValue = this.CurrencyValueList.FirstOrDefault( a => a.Id == transaction.FinancialPaymentDetail.CurrencyTypeValueId );
                        }
                    }
                }

                // sort starting with most recent first
                var bindingList = new BindingList<FinancialTransaction>( transactions.OrderByDescending( a => a.CreatedDateTime ).ToList() );
                bindingList.RaiseListChangedEvents = true;
                bindingList.ListChanged += bindingList_ListChanged;

                grdBatchItems.DataContext = bindingList;
                DisplayTransactionCount();
            };

            this.Cursor = Cursors.Wait;
            bw.RunWorkerAsync();
        }
 /// <summary>
 /// Handles the Click event of the btnAddBatch control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
 private void btnAddBatch_Click( object sender, RoutedEventArgs e )
 {
     var financialBatch = new FinancialBatch { Id = 0, BatchStartDateTime = DateTime.Now.Date, CreatedByPersonAliasId = LoggedInPerson.PrimaryAliasId };
     UpdateBatchUI( financialBatch );
     ShowBatch( true );
 }