Пример #1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            //get the value from purchase form first
            transactionBLL transaction = new transactionBLL();

            transaction.type = lblTop.Text;

            //get the ID of dealer or customer here.
            //let get the name of dealer first
            string     DeaCustName = txtName.Text;
            DeaCustBLL dc          = dcDAL.GetDeaCustIDFromName(DeaCustName);

            transaction.dea_cus_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 username of logged in user
            string  username = frmLogin.loggedIn;
            userBLL u        = uDAL.GetIDFromUsername(username);

            transaction.added_by           = u.id;
            transaction.transactionDetails = transactionDT;


            //lets create boolean variable and set it value to false/
            bool success = false;

            //Actual Code to insert transaction and transaction detail
            using (TransactionScope scope = new TransactionScope())
            {
                int transactionID = -1;
                //create a boolean Value and insert transaction
                bool w = tDAL.InsertTransaction(transaction, out transactionID);

                //use for loop to insert transaction Detail
                for (int i = 0; i < transactionDT.Rows.Count; i++)
                {
                    //Get all the detail of products
                    transactionDetailBLL transactionDetail = new transactionDetailBLL();
                    //Get the products name and convert 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 Increase or Decrease product Quantity based on Pyurchase or Sales
                    string transactionType = lblTop.Text;

                    //let check weather we are in purchase or sales
                    bool x = false;
                    if (transactionType == "PURCHASE")
                    {
                        x = pDAL.Increaseproduct(transactionDetail.product_id, transactionDetail.qty);
                        //Increase the product
                    }
                    else if (transactionType == "SALES")
                    {
                        //decrease the product
                        x = pDAL.DecreaseProduct(transactionDetail.product_id, transactionDetail.qty);
                    }
                    //Insert transaction Detail in to DataBase
                    bool y = tdDAL.InsertTransactionDetail(transactionDetail);
                    success = w && x && y;
                }
                if (success == true)
                {
                    //Transaction Completed
                    scope.Complete();

                    //Code to Print the Bill of products
                    DGVPrinter printer = new DGVPrinter();

                    printer.Title               = "\r\n\r\n\r\n ANYSTORE PVT.LTD. \r\n\r\n";
                    printer.SubTitle            = "Highfrezh, Ajoke \r\n phone: 01-41XXXXX \r\n\r\n";
                    printer.SubTitleFormatFlags = StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
                    printer.PageNumbers         = true;
                    printer.PageNumberInHeader  = false;
                    printer.PorportionalColumns = true;
                    printer.HeaderCellAlignment = StringAlignment.Near;
                    printer.Footer              = "Discount: " + txtDiscount.Text + "% \r\n" + "VAT: " + txtVat.Text + "% \r\n" + "Grand Total: " + txtGrandTotal.Text + "\r\n\r\n" + "Thanks for doing business with us";
                    printer.FooterSpacing       = 15;
                    printer.PrintDataGridView(dgvAddedProducts);



                    MessageBox.Show("Transaction Complete successfully");
                    //Clear datagrid veiw and all the TextBoxs
                    dgvAddedProducts.DataSource = null;
                    dgvAddedProducts.Rows.Clear();

                    txtSearch.Text        = "";
                    txtName.Text          = "";
                    txtEmail.Text         = "";
                    txtContact.Text       = "";
                    txtAddress.Text       = "";
                    txtProductSearch.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
                {
                    //Transaction Failed
                    MessageBox.Show("Transaction Failed");
                }
            }
        }