internal static bool Approve(Approval approval, ISINAvailability availability)
 {
     if (approval == null || availability == null || Math.Abs(approval.Amount) > availability.Available)
     {
         return false;
     }
     return true;
 }
        void requestButton_Click(object sender, EventArgs e) {
            if (!OrchestradeCommon.Util.AccessHelper.HasAccess(ACCESS_NAME)) {
                GUIHelper.Display(this, "Access " + ACCESS_NAME + " denied for user " + OrchestradeUtils.CurrentUser());
                return;
            }

            Bond bond = productEdit.Properties.Tag as Bond;
            if (bond == null) {
                GUIHelper.Display(this, "Please select an instrument.");
                return;
            }
            OrchestradeParty portfolio = portfolioEdit.Properties.Tag as OrchestradeParty;
            if (portfolio == null) {
                GUIHelper.Display(this, "Please select a portfolio.");
                return;
            }
            decimal nominal = 0;
            if (sizeTextEdit.EditValue != null) {
                nominal = Convert.ToDecimal(sizeTextEdit.EditValue);
            }
            if (nominal == 0) {
                GUIHelper.Display(this, "Please enter a valid nominal.");
                return;
            }

            DateTime settleDate = DateTime.Parse(settleDateEdit.Text);

            Approval approval = new Approval();
            approval.Amount = -1 * Math.Abs(nominal);
            approval.ISIN = bond.Isin;
            approval.Portfolio = portfolio.Code;
            approval.Comments = commentEdit.Text;
            approval.Requester = OrchestradeUtils.CurrentUser();
            approval.ApprovalStatusUpdateTime = DateTime.Now;
            approval.SettlementDate = settleDate;

            setControlsEditable(false);
            String originalRequestButtonText = requestButton.Text;
            requestButton.Text = "Please Wait";

            Task<String> task1 = new Task<String>(() => manager.ProcessApprovalRequest(approval));

            task1.GetAwaiter().OnCompleted(() => 
                {
                    GUIHelper.Display(this, task1.Result);
                    refreshApprovals();
                    requestButton.Text = originalRequestButtonText;
                });

            task1.Start();
       } 
        internal String ProcessApprovalRequest(Approval approval)//, DateTime settleDate)
        {
            String isin = approval.ISIN;
            if(String.IsNullOrEmpty(isin))
            {
                throw new ApplicationException("Must provide ISIN before requesting short sale approval.");
            }

            DateTime settleDate = approval.SettlementDate;
            List<ISINAvailability> isinAvailabilityList = GetISINAvailability(settleDate);

            bool approved = ShortSaleRuleEngine.Approve(approval, isinAvailabilityList.FirstOrDefault(o => o.ISIN == approval.ISIN));
            ApprovalStatus approvalStatus = approved ? ApprovalStatus.Approved : ApprovalStatus.Rejected;
            approval.ApprovalStatus = approvalStatus;
            ShortSaleSchemaSession.GetContext().Approvals.InsertOnSubmit(approval);
            ShortSaleSchemaSession.CommitChanges();
            return approvalStatus.Name + ": " + approval.ID;
        }