Exemple #1
0
        public int Add(FinancialTransaction aTransaction)
        {
            int           returnValue = -1;
            SqlConnection con         = new ConnectionString().GetConnection();

            con.Open();

            using (con)
            {
                SqlTransaction tran = con.BeginTransaction();

                try
                {
                    returnValue = new FinancialTransactionDataHelper().Insert(aTransaction, tran);

                    if (aTransaction.CascadeChanges)
                    {
                        if (aTransaction.WalletType == WalletType.Investment)
                        {
                            #region Insert in Investment
                            Investment anInvestment = new Investment();
                            Investor   anInvestor   = new Investor();

                            anInvestment.ID = aTransaction.WalletReference;
                            anInvestor.ID   = aTransaction.ContactID;
                            anInvestment.InvestorDetails = anInvestor;
                            anInvestment.InHand          = (aTransaction.FlowType == FlowType.Debit) ? aTransaction.Amount * -1 : aTransaction.Amount;

                            new InvestmentDataHelper().Update(anInvestment.ID, anInvestment, tran);
                            #endregion Insert in Investment
                        }
                        else if (aTransaction.WalletType == WalletType.InvestmentGroup)
                        {
                            InvestmentGroupDetailDataHelper anInvestmentGroupDetailDH = new InvestmentGroupDetailDataHelper();
                            InvestmentGroupDetail           aInvestmentGroupDetail    = new InvestmentGroupDetail();
                            int ainvestmentID = anInvestmentGroupDetailDH.SelectByID(aTransaction.WalletReference, tran).InvestmentID;

                            aInvestmentGroupDetail.AmountDeducted = true;
                            aInvestmentGroupDetail.ID             = aTransaction.WalletReference;
                            aInvestmentGroupDetail.InHand         = (aTransaction.FlowType == FlowType.Debit) ? aTransaction.Amount * -1 : aTransaction.Amount;

                            //Update Investment/Group Tables
                            anInvestmentGroupDetailDH.Update(aInvestmentGroupDetail.ID, aInvestmentGroupDetail, tran);

                            aTransaction.WalletType = WalletType.Investment;
                            //aInvestmentGroupDetail = anInvestmentGroupDetailDH.SelectByID(aTransaction.WalletReference,tran).InvestmentID;
                            aTransaction.WalletReference = ainvestmentID; //aInvestmentGroupDetail.InvestmentID;
                            aTransaction.FlowType        = (aTransaction.FlowType == FlowType.Debit) ? FlowType.Credit : FlowType.Debit;

                            //double entry rule, 2nd entry in Investment Account
                            new FinancialTransactionDataHelper().Insert(aTransaction, tran);
                        }
                    }

                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }

            return(returnValue);
        }