Ejemplo n.º 1
0
        public Loan UpdateContractStatus(Loan credit, Project project, IClient client, bool undoValidation)
        {
            CheckOperationDate(credit.CreditCommiteeDate.Value);
            using (SqlConnection conn = _loanManager.GetConnection())
            using (SqlTransaction sqlTransaction = conn.BeginTransaction())
            {

                try
                {
                    if (credit.StartDate.Date < ((DateTime) credit.CreditCommiteeDate).Date)
                        throw new OpenCbsContractSaveException(
                            OpenCbsContractSaveExceptionEnum.LoanWasValidatedLaterThanDisbursed);

                    Loan tempLoan = credit.Copy();

                    LoanValidationEvent lve = null;
                    if (undoValidation)
                    {
                        if (tempLoan.Events.GetLastLoanNonDeletedEvent != null &&
                            tempLoan.Events.GetLastLoanNonDeletedEvent is LoanValidationEvent)
                        {
                            ((LoanValidationEvent) tempLoan.Events.GetLastLoanNonDeletedEvent).Amount = credit.Amount;
                            _ePs.CancelFireEvent(tempLoan.Events.GetLastLoanNonDeletedEvent, sqlTransaction, tempLoan,
                                                 tempLoan.Product.Currency.Id);
                            tempLoan.Events.GetLastLoanNonDeletedEvent.Deleted = true;
                        }
                    }
                    else if (tempLoan.ContractStatus == OContractStatus.Validated)
                    {
                        lve = new LoanValidationEvent();
                        lve.Amount = tempLoan.Amount;
                        lve.Date = tempLoan.CreditCommiteeDate.Value;
                        lve.Cancelable = true;
                        lve.User = _user;
                        lve.Deleted = false;

                        if (Teller.CurrentTeller != null && Teller.CurrentTeller.Id != 0)
                            lve.TellerId = Teller.CurrentTeller.Id;

                        _ePs.FireEvent(lve, tempLoan, sqlTransaction);
                        tempLoan.Events.Add(lve);
                    }
                    _loanManager.UpdateLoanStatus(tempLoan, sqlTransaction);
                    project.SetStatus();
                    new ProjectServices(_user).UpdateProjectStatus(project, sqlTransaction);
                    client.SetStatus();
                    new ClientServices(_user).UpdateClientStatus(client, sqlTransaction);

                    FundingLineEvent pendingFundingLineEvent = new FundingLineEvent
                        {
                            Code =
                                string.Concat(
                                    OFundingLineEventTypes.Commitment.
                                                           ToString(), "/", tempLoan.Code),
                            Type = OFundingLineEventTypes.Commitment,
                            FundingLine =
                                _fundingLineServices.SelectFundingLineById(
                                    tempLoan.FundingLine.Id, sqlTransaction),
                            Movement = OBookingDirections.Debit,
                            CreationDate = TimeProvider.Now,
                            Amount = tempLoan.Amount
                            ,
                            AttachTo = lve
                        };
                    //if this is a new validate event, register it, otherwise delete previous validate event
                    if (tempLoan.ContractStatus == OContractStatus.Validated)
                        tempLoan.FundingLine.AddEvent(_fundingLineServices.AddFundingLineEvent(pendingFundingLineEvent,
                                                                                               sqlTransaction));
                    else if (undoValidation)
                    {
                        DeleteFundingLineEvent(ref tempLoan, pendingFundingLineEvent, sqlTransaction);
                    }
                    sqlTransaction.Commit();
                    return tempLoan;
                }
                catch (Exception ex)
                {
                    sqlTransaction.Rollback();
                    throw ex;
                }
            }
        }
Ejemplo n.º 2
0
        public int SaveProject(Project pProject, IClient pClient)
        {
            if (pClient.Id == 0)
                throw new OpenCbsProjectSaveException(OpenCbsProjectSaveExceptionEnum.ClientIsEmpty);

            if (string.IsNullOrEmpty(pProject.Name))
                throw new OpenCbsProjectSaveException(OpenCbsProjectSaveExceptionEnum.NameIsEmpty);

            if (string.IsNullOrEmpty(pProject.Aim))
                throw new OpenCbsProjectSaveException(OpenCbsProjectSaveExceptionEnum.AimIsEmpty);

            if (pProject.BeginDate == DateTime.MinValue)
                throw new OpenCbsProjectSaveException(OpenCbsProjectSaveExceptionEnum.BeginDateEmpty);

            if (pProject.CorporateCA.HasValue && pProject.CorporateCA.Value == -1)
                throw new OpenCbsProjectSaveException(OpenCbsProjectSaveExceptionEnum.CAIsBad);

            if (pProject.CorporateFinancialPlanAmount.HasValue && pProject.CorporateFinancialPlanAmount.Value == -1)
                throw new OpenCbsProjectSaveException(OpenCbsProjectSaveExceptionEnum.FinancialPlanAmountIsBad);

            if (pProject.CorporateFinancialPlanTotalAmount.HasValue && pProject.CorporateFinancialPlanTotalAmount.Value == -1)
                throw new OpenCbsProjectSaveException(OpenCbsProjectSaveExceptionEnum.FinancialTotalPlanAmountIsBad);

            foreach (FollowUp up in pProject.FollowUps)
            {
                if(!up.CA.HasValue)
                    throw new OpenCbsProjectSaveException(OpenCbsProjectSaveExceptionEnum.CACannotBeNullInFollowUp);
            }

            using (SqlConnection conn = _ProjectManager.GetConnection())
            using (SqlTransaction t = conn.BeginTransaction())
            {
                try
                {
                    if (String.IsNullOrEmpty(pClient.Branch.Code))
                    {
                        pClient.Branch.Code = _branchService.FindBranchCodeByClientId(pClient.Id, t);
                    }
                    if (string.IsNullOrEmpty(pProject.Code))
                        pProject.Code = pProject.GenerateProjectCode(pClient.Branch.Code);
                    int id = 0;
                    if (pProject.Id == 0)
                    {
                        pProject.SetStatus();
                        pClient.SetStatus();

                        _ClientServices.UpdateClientStatus(pClient, t);
                        id = _ProjectManager.Add(pProject, pClient.Id, t);
                    }
                    else
                    {
                        _ProjectManager.UpdateProject(pProject, t);
                        id = pProject.Id;
                    }
                    t.Commit();
                    return id;
                }
                catch (Exception)
                {
                    t.Rollback();
                    throw;
                }
            }
        }