public bool insertAndUpdate(DealerAndCustomerBLL d)
        {
            bool isSuccess = false;

            try
            {
                SqlCommand cmd = new SqlCommand("USP_InsertDealerAndCustomerDetails", AppManager.ConnectionManager);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("id", SqlDbType.Int).Value              = d.id;
                cmd.Parameters.Add("type", SqlDbType.VarChar).Value        = d.type;
                cmd.Parameters.Add("name", SqlDbType.VarChar).Value        = d.name;
                cmd.Parameters.Add("email", SqlDbType.VarChar).Value       = d.email;
                cmd.Parameters.Add("contact", SqlDbType.VarChar).Value     = d.contact;
                cmd.Parameters.Add("address", SqlDbType.VarChar).Value     = d.address;
                cmd.Parameters.Add("added_date", SqlDbType.DateTime).Value = d.added_date;
                cmd.Parameters.Add("added_by", SqlDbType.Int).Value        = d.added_by;
                int row = cmd.ExecuteNonQuery();
                if (row > 0)
                {
                    isSuccess = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return(isSuccess);
        }
        public DealerAndCustomerBLL SearchDealerAndCustomerForTransaction(string keyword)
        {
            DataTable            dt = new DataTable();
            DealerAndCustomerBLL dc = new DealerAndCustomerBLL();

            try
            {
                SqlCommand cmd = new SqlCommand("USP_SearchDealersAndCustomersForTransaction", AppManager.ConnectionManager);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("keyword", SqlDbType.VarChar).Value = keyword;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet        ds      = new DataSet();
                adapter.Fill(ds);
                adapter.Dispose();
                dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    dc.name    = Common.ConvertToString(dt.Rows[0]["name"]);
                    dc.email   = Common.ConvertToString(dt.Rows[0]["email"]);
                    dc.contact = Common.ConvertToString(dt.Rows[0]["contact"]);
                    dc.address = Common.ConvertToString(dt.Rows[0]["address"]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return(dc);
        }
        public bool delete(DealerAndCustomerBLL d)
        {
            bool isSuccess = false;

            try
            {
                SqlCommand cmd = new SqlCommand("USP_DeleteDealersAndCustomers", AppManager.ConnectionManager);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("id", SqlDbType.Int).Value = d.id;
                int row = cmd.ExecuteNonQuery();
                if (row > 0)
                {
                    isSuccess = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return(isSuccess);
        }
        public DealerAndCustomerBLL getDealerOrCustomerIdFromName(string name)
        {
            DealerAndCustomerBLL dc = new DealerAndCustomerBLL();

            try
            {
                DataTable  dt  = new DataTable();
                SqlCommand cmd = new SqlCommand("USP_GetDealerOrCustomerIdFromName", AppManager.ConnectionManager);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("dealerOrCustomerName", SqlDbType.VarChar).Value = name;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dt);
                adapter.Dispose();
                if (dt.Rows.Count > 0)
                {
                    dc.id = Common.ConvertToInt(dt.Rows[0]["id"].ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return(dc);
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            TransactionsBLL transaction = new TransactionsBLL();

            transaction.type = lblPurchaseAndSalesTop.Text;

            string dealerOrCustomerName = Common.ConvertToString(txtDealerAndCustomerName.Text.Trim());

            //if (!String.IsNullOrEmpty(dealerOrCustomerName))
            //{
            DealerAndCustomerBLL dc = dcDAL.getDealerOrCustomerIdFromName(dealerOrCustomerName);

            transaction.dealer_customer_id = dc.id;
            //}
            //else
            //{
            //    transaction.dealer_customer_id = 0;
            //}

            transaction.grandTotal = Common.ConvertToDouble(txtGrandTotal.Text.Trim());
            //transaction.transaction_date = DateTime.Now;
            transaction.transaction_date = dtpBillDate.Value;
            transaction.tax      = Common.ConvertToDouble(txtGstPercentage.Text.Trim());
            transaction.discount = Common.ConvertToDouble(txtDiscountPercentage.Text.Trim());
            string  loggedInUser = frmLogin.loggedInUser;
            UserBLL usr          = uDAL.getUserIdFromUserName(loggedInUser);

            transaction.added_by           = usr.id;
            transaction.transactionDetails = dtTransaction;

            bool isSuccess = false;

            //insert transaction details

            using (TransactionScope scope = new TransactionScope())
            {
                int    transactionId   = -1;
                string transactionType = lblPurchaseAndSalesTop.Text;
                bool   x = tDAL.insertTransactions(transaction, out transactionId);

                for (int i = 0; i < grdAddedProducts.Rows.Count; i++)
                {
                    TransactionDetailBLL transactionDetails = new TransactionDetailBLL();

                    string productName = Common.ConvertToString(grdAddedProducts.Rows[i].Cells["ProductName"].Value);

                    ProductsBLL p = pDAL.getProductIdFromName(productName);
                    transactionDetails.product_id         = p.id;
                    transactionDetails.rate               = Common.ConvertToDouble(grdAddedProducts.Rows[i].Cells["Rate"].Value);
                    transactionDetails.qty                = Common.ConvertToDouble(grdAddedProducts.Rows[i].Cells["Quantity"].Value);
                    transactionDetails.total              = Common.ConvertToDouble(grdAddedProducts.Rows[i].Cells["Total"].Value);
                    transactionDetails.dealer_customer_id = dc.id;
                    transactionDetails.added_date         = dtpBillDate.Value;
                    transactionDetails.added_by           = usr.id;
                    transactionDetails.tranId             = transactionId;

                    //increase or decrease the product quantity based on purchase and sales

                    bool isPSIncreaseDecrease = false;
                    if (transactionType == "PURCHASE")
                    {
                        //increase the product quantity
                        isPSIncreaseDecrease = pDAL.increaseProductQuantity(transactionDetails.product_id, transactionDetails.qty);
                    }
                    else if (transactionType == "SALES")
                    {
                        //decrease the product quantity
                        isPSIncreaseDecrease = pDAL.decreaseProductQuantity(transactionDetails.product_id, transactionDetails.qty);
                    }

                    bool y = tdDAL.insertTransactionDetail(transactionDetails);
                    isSuccess = x && y && isPSIncreaseDecrease;
                }

                if (isSuccess)
                {
                    scope.Complete();
                    printTransactionFromGrid(grdAddedProducts);
                    if (transactionType == "PURCHASE")
                    {
                        MessageBox.Show("Purchase successfull");
                    }
                    else if (transactionType == "SALES")
                    {
                        MessageBox.Show("Sales successfull");
                    }
                    dtTransaction.Clear();
                    grdAddedProducts.DataSource = null;
                    clearDealerAndCustomerDetails();
                    clearProductDetails();
                    clearCalculationDetails();
                }
                else
                {
                    MessageBox.Show("Transaction failed");
                }
            }
        }