Esempio n. 1
0
        }//end of event handler

        //transfer button click event to transfer money from one account to aother
        private void btnTransfer_Click(object sender, EventArgs e)
        {
            //Note that we are creatinf a transaction here in order to make sure that if something goes
            //wrong in a middle of transfering money our database stays in consistent state.
            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    BLLTransactionManager manager = new BLLTransactionManager();

                    //Dealing with getting monney from one acc first
                    int transferAmmount = MoneyTransformer.TransformMoney(txtAmmount.Text);

                    int debitedAccNewBal = SourceAccount.CurrentBalance - transferAmmount;

                    // new balance for credited account
                    int creditedAccNewBal = DestinationAccount.CurrentBalance + transferAmmount;

                    bool transferPossible = false;

                    // setting the possibility of transaction to true
                    if (SourceAccount.CurrentBalance >= transferAmmount)
                    {
                        transferPossible = true;
                    }
                    else
                    {
                        MessageBox.Show("Not Enough funds");
                    }


                    if (transferPossible)
                    {
                        //testing to make sure saving account doesnt transfer to external
                        if (SourceAccount.AccountType.Equals("saving"))
                        {
                            if (!ExternalTransfer)
                            {
                                manager.WithdrawMonies(SourceAccount.AccountNo, debitedAccNewBal, SourceAccount.AccountType);


                                //This code can be uncommented in order to show that database doesnt change if there was an error or exceprion somewhere
                                //Exception ex = new Exception();
                                //if (ex != null)
                                //{
                                //    MessageBox.Show("Opps SOMETHING WENT WRONG HERE !!!");
                                //}
                                //throw ex;

                                manager.DepositMonies(DestinationAccount.AccountNo, creditedAccNewBal, DestinationAccount.AccountType);

                                int debitTransNo  = 0;
                                int creditTransNo = 0;

                                //setting and creating transaction for debit
                                TransactionRecord debitTrans = new TransactionRecord();
                                debitTrans.AccountNumber      = SourceAccount.AccountNo;
                                debitTrans.TransactionType    = TransactionType.debit;
                                debitTrans.Date               = DateTime.Now;
                                debitTrans.TransactionAmmount = transferAmmount;
                                debitTrans.Balance            = debitedAccNewBal;
                                //creating a transaction for debited account
                                manager.CreateTransaction(debitTrans, out debitTransNo);


                                //setting and creating transaction for credit
                                TransactionRecord creditTrans = new TransactionRecord();
                                creditTrans.AccountNumber      = DestinationAccount.AccountNo;
                                creditTrans.TransactionType    = TransactionType.credit;
                                creditTrans.Date               = DateTime.Now;
                                creditTrans.TransactionAmmount = transferAmmount;
                                creditTrans.Balance            = creditedAccNewBal;
                                //creating a transaction for credit account
                                manager.CreateTransaction(creditTrans, out creditTransNo);


                                //Create Transfer Record object first
                                TransferRecord record = new TransferRecord();
                                record.RecepientName  = txtRecepientName.Text;
                                record.DebitedAccount = int.Parse(txtAccountNumber.Text);
                                //NOTE here I am setting the record transaction number to the number created for debited account
                                record.TransactionNumber   = debitTransNo;
                                record.DestinationSortCode = txtDestinationSortCode.Text;
                                record.DestinationAccNo    = int.Parse(txtAccOutNumber.Text);
                                record.TransferAmmount     = transferAmmount;
                                record.TransferDate        = DateTime.Now;
                                record.TransferDescription = rtfDescription.Text;


                                bool success = false;
                                //creating new transfer record
                                success = manager.CreateTransfer(record);
                                transaction.Complete();
                                transaction.Dispose();

                                if (success)
                                {
                                    MessageBox.Show("Transfer Successfull");
                                }
                            }
                            else
                            {
                                MessageBox.Show("Cant transfer to an external account from saving account\nOnly internal transfers are allowed");
                            }
                        }
                        else
                        {
                            manager.WithdrawMonies(SourceAccount.AccountNo, debitedAccNewBal, SourceAccount.AccountType);


                            //This code can be uncommented in order to show that database doesnt change if there was an error or exceprion somewhere
                            //Exception ex = new Exception();
                            //if (ex != null)
                            //{
                            //    MessageBox.Show("Opps SOMETHING WENT WRONG HERE !!!");
                            //}
                            //throw ex;

                            manager.DepositMonies(DestinationAccount.AccountNo, creditedAccNewBal, DestinationAccount.AccountType);

                            int debitTransNo  = 0;
                            int creditTransNo = 0;

                            //setting and creating transaction for debit
                            TransactionRecord debitTrans = new TransactionRecord();
                            debitTrans.AccountNumber      = SourceAccount.AccountNo;
                            debitTrans.TransactionType    = TransactionType.debit;
                            debitTrans.Date               = DateTime.Now;
                            debitTrans.TransactionAmmount = transferAmmount;
                            debitTrans.Balance            = debitedAccNewBal;
                            //creating a transaction for debited account
                            manager.CreateTransaction(debitTrans, out debitTransNo);


                            //setting and creating transaction for credit
                            TransactionRecord creditTrans = new TransactionRecord();
                            creditTrans.AccountNumber      = DestinationAccount.AccountNo;
                            creditTrans.TransactionType    = TransactionType.credit;
                            creditTrans.Date               = DateTime.Now;
                            creditTrans.TransactionAmmount = transferAmmount;
                            creditTrans.Balance            = creditedAccNewBal;
                            //creating a transaction for credit account
                            manager.CreateTransaction(creditTrans, out creditTransNo);


                            //Create Transfer Record object first
                            TransferRecord record = new TransferRecord();
                            record.RecepientName  = txtRecepientName.Text;
                            record.DebitedAccount = int.Parse(txtAccountNumber.Text);
                            //NOTE here I am setting the record transaction number to the number created for debited account
                            record.TransactionNumber   = debitTransNo;
                            record.DestinationSortCode = txtDestinationSortCode.Text;
                            record.DestinationAccNo    = int.Parse(txtAccOutNumber.Text);
                            record.TransferAmmount     = transferAmmount;
                            record.TransferDate        = DateTime.Now;
                            record.TransferDescription = rtfDescription.Text;


                            bool success = false;
                            //creating new transfer record
                            success = manager.CreateTransfer(record);
                            transaction.Complete();
                            transaction.Dispose();

                            if (success)
                            {
                                MessageBox.Show("Transfer Successfull");
                            }
                        }
                    }
                }
                catch (TransactionException ex)
                {
                    transaction.Dispose();
                    MessageBox.Show("OOpppps somethinge went WRONG there");
                    throw ex;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("No money were transfered");
                }
            }
        }//End of TransferClick