Esempio n. 1
0
        private void DeleteLoan(LoanViewModel loan)
        {
            if (loan == null)
            {
                return;
            }

            loan.IsDeleted = true;
            _model.Remove(loan);
            RegisterUniquePendingChange(new DeleteLoanCommand(loan.Model));
        }
        void LoanDetails_ChangeContainerSet()
        {
            txtLoanNumber.BindUser(User, "LoanNumber", "tblLoan", "vchrLoanNumber");
            txtTransferMethod.BindUser(User, PickListType.Phrase, "Transfer Method", TraitCategoryType.Contact);
            txtReturnType.BindUser(User, PickListType.Phrase, "Return Type", TraitCategoryType.Contact);

            txtBorrower.BindUser(User, LookupType.Contact);
            txtReceiver.BindUser(User, LookupType.Contact);
            txtAuthorizedBy.BindUser(User, LookupType.Contact);

            chkBorrowerIsReceiver.Checked   += new RoutedEventHandler(chkBorrowerIsReceiver_Checked);
            chkBorrowerIsReceiver.Unchecked += new RoutedEventHandler(chkBorrowerIsReceiver_Unchecked);

            Loan model = null;

            if (LoanID >= 0)
            {
                var service = new LoanService(User);
                model = service.GetLoan(LoanID);
            }
            else
            {
                model = new Loan {
                    LoanClosed = false
                };
                RegisterUniquePendingChange(new InsertLoanCommand(model));
            }

            if (model != null)
            {
                _viewModel = new LoanViewModel(model);
                if (model.ReceiverID != 0 && model.ReceiverID == model.RequestorID)
                {
                    _viewModel.BorrowerIsReceiver = true;
                }

                this.DataContext        = _viewModel;
                _viewModel.DataChanged += new DataChangedHandler(viewModel_DataChanged);
            }

            var materialControl = new OneToManyControl(new LoanMaterialControl(User, model));

            var lbl = new Label {
                Content = ""
            };

            materialControl.AddButtonRHS(lbl);


            // Count the total number of specimens currently attached to this loan. Sometimes specimen counts are represented as "1 x adult" etc,
            // so we be a bit liberal in our interpretation.
            var specimenCountRegex = new Regex(@"[^\d]*(\d+)[^\d]*");

            materialControl.ModelChanged += list => {
                var dblTotal = list.Sum(vm => {
                    var loanMaterial = vm as LoanMaterialViewModel;
                    decimal subtotal = 0;
                    var matcher      = specimenCountRegex.Match(loanMaterial.NumSpecimens);
                    while (matcher.Success)
                    {
                        decimal d;
                        if (Decimal.TryParse(matcher.Groups[1].Value, out d))
                        {
                            subtotal += d;
                        }
                        matcher = matcher.NextMatch();
                    }

                    return(subtotal);
                });

                lbl.Content = String.Format("Total specimen count: {0}", (long)dblTotal);
            };

            tabLoan.AddTabItem("_Material", materialControl);
            tabLoan.AddTabItem("_Correspondence", new OneToManyControl(new LoanCorrespondenceControl(User, model)));
            _reminders = new OneToManyControl(new LoanRemindersControl(User, model));
            tabLoan.AddTabItem("_Reminders", _reminders);

            tabLoan.AddTabItem("_Traits", new TraitControl(User, TraitCategoryType.Loan, _viewModel));
            tabLoan.AddTabItem("_Notes", new NotesControl(User, TraitCategoryType.Loan, _viewModel));

            var window = this.FindParentWindow() as ControlHostWindow;

            if (window != null)
            {
                var button = new Button {
                    Width = 130, Height = 23, Content = "_Generate Loan Form..."
                };
                window.AddCustomButton(button);
                button.Click += new RoutedEventHandler((source, e) => {
                    ShowLoanForms();
                });
            }
        }