コード例 #1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            //Get the values from form purchase Sales form First
            // we will create an objetct of Transactions Class

            transactionsBLL transaction = new transactionsBLL();

            transaction.type = lblTop.Text;

            // Get the ID of Dealer or Customer Here
            // Lets get name of the dealer or customer first

            string deaCustName = txtName.Text;

            // here we have first written method in DeaCustDAL and now forwarding as

            DeaCustBLL dc = dcDAL.GetDeaCustIDFromName(deaCustName);

            //Now we will refer the transaction

            transaction.dea_cust_id = dc.id;

            transaction.grandTotal       = Math.Round(decimal.Parse(txtGrandTotal.Text), 2);
            transaction.transaction_date = DateTime.Now;
            transaction.tax      = decimal.Parse(txtVat.Text);
            transaction.discount = decimal.Parse(txtDiscount.Text);

            // Get the user name,for added by, get the user name

            string username = frmLogin.loggedIn;

            userBLL u = uDAL.GetIDFromUsername(username);

            // we need to passthistransaction
            transaction.added_by = u.id;

            transaction.transactionDetails = transactionDT;

            //Lets create a Boolean variable and set its value to False

            bool success = false;

            //Actual Code to Insert Transaction and Transaction Details

            using (TransactionScope scope = new TransactionScope())
            {
                int transactionID = 1;

                //Create a Boolean value and Insert Transaction
                bool w = tDAL.Insert_Transcation(transaction, out transactionID);

                //Use for loop to insert transaction details

                for (int i = 0; i < transactionDT.Rows.Count; i++)
                {
                    // create all details of products

                    transactionDetailBLL transactionDetail = new transactionDetailBLL();

                    // Get the product name and convert it to ID

                    string productName = transactionDT.Rows[i][0].ToString();

                    productsBLL p = pDAL.GetproductIDFromName(ProductName);

                    transactionDetail.product_id  = p.id;
                    transactionDetail.rate        = decimal.Parse(transactionDT.Rows[i][1].ToString());
                    transactionDetail.qty         = decimal.Parse(transactionDT.Rows[i][2].ToString());
                    transactionDetail.total       = Math.Round(decimal.Parse(transactionDT.Rows[i][3].ToString()), 2);
                    transactionDetail.dea_cust_id = dc.id;
                    transactionDetail.added_date  = DateTime.Now;
                    transactionDetail.added_by    = u.id;

                    // Here we will increase or decrease the quantity based on purchase or sales
                    // we have created Methods to Increase and decrease Qty.
                    // Here Increase or Decrease Product Qty based on Purchase or Sales
                    // we wil lspecify it is Sales or Purchase. we wil lcreate a string variable

                    string transactionType = lblTop.Text;

                    //Lets Check we are on Purchase or Sales

                    bool x = false;

                    if (transactionType == "Purchase")
                    {
                        //Increase the product
                        x = pDAL.IncreaseProduct(transactionDetail.product_id, transactionDetail.qty);
                    }
                    else if (transactionType == "Sales")
                    {
                        //Decrease the Product Qty
                        x = pDAL.DecreaseProduct(transactionDetail.product_id, transactionDetail.qty);
                    }


                    //Insert Transaction Details Inside the Database
                    bool y = tdDAL.InsertTranscationDetail(transactionDetail);
                    success = w && y && x;
                }
                if (success == true)
                {
                    // transaction complete
                    scope.Complete();

                    MessageBox.Show("Transaction completed Successfully");
                    dgvAddedProducts.DataSource = null;
                    dgvAddedProducts.Rows.Clear();

                    txtSearch.Text  = "";
                    txtName.Text    = "";
                    txtEmail.Text   = "";
                    txtContact.Text = "";
                    txtAddress.Text = "";

                    txtSearchProduct.Text = "";
                    txtProductName.Text   = "";
                    txtInventory.Text     = "0";
                    txtRate.Text          = "0";
                    txtQty.Text           = "0";

                    txtSubTotal.Text     = "0";
                    txtDiscount.Text     = "0";
                    txtVat.Text          = "0";
                    txtGrandTotal.Text   = "0";
                    txtPaidAmount.Text   = "0";
                    txtReturnAmount.Text = "0";
                }
                else
                {
                    MessageBox.Show("This Transaction Has Not Been Completed and Failed");
                }
            }
        }