public static List <EstabEstabTransaction> getAllTransactions()
    {
        List <EstabEstabTransaction> transactions = new List <EstabEstabTransaction>();

        try
        {
            SqlCommand command = new SqlCommand("Select * from BplTransactionEstabToEstab");
            command.Connection = connection;
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                EstabEstabTransaction t = new EstabEstabTransaction();
                t.ID     = reader["bplEstabTrasactionID"].ToString();
                t.Match  = EstabEstabMatchDB.getMatchByID(reader["bpMatchEstabID"].ToString());
                t.Units  = Convert.ToInt32(reader["unitsPossible"]);
                t.Status = reader["status"].ToString();
                transactions.Add(t);
            }
            reader.Close();
        }
        finally
        {
            connection.Close();
        }
        return(transactions);
    }
    protected void lbtnContact_Click(object sender, EventArgs e)
    {
        Establishment          currentEstab     = (Establishment)Session["establishment"];
        LinkButton             lbtn             = (LinkButton)sender;
        GridViewRow            gvr              = (GridViewRow)lbtn.NamingContainer;
        int                    i                = Convert.ToInt32(gvr.RowIndex);
        List <EstabEstabMatch> allEstabRequests = EstabEstabMatchDB.getAllMatches();
        List <EstabEstabMatch> estabRequests    = new List <EstabEstabMatch>();

        foreach (EstabEstabMatch m in allEstabRequests)
        {
            if (m.Match.ID == currentEstab.ID && m.Status == "pending" && m.Request.MatchedUnits < m.Request.Units)
            {
                estabRequests.Add(m);
            }
        }
        EstabEstabMatch currentMatch = estabRequests[i];
        Establishment   contactEstab = currentMatch.Request.Establishment;

        Session["ldID"]  = null;
        Session["rwEst"] = null;
        Session["echat"] = contactEstab.ID;
        Server.Transfer("IndividualChatE.aspx");
        //store session for individual chat in next page
    }
    protected void gvEstabMatches_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        EstablishmentBPRequest       selectedRequest      = pendingRequests[gvRequests.PageSize * gvRequests.PageIndex + gvRequests.SelectedIndex];
        List <EstabEstabTransaction> allEstabTransactions = EstabEstabTransactionDB.getAllTransactions();

        foreach (EstabEstabTransaction m in allEstabTransactions)
        {
            if (m.Match.Request.ID == selectedRequest.ID && m.Status == "accepted")
            {
                estabTransactions.Add(m);
            }
        }

        EstabEstabTransaction selectedTransaction = estabTransactions[e.RowIndex];

        selectedTransaction.Status = "cancelled";
        EstabEstabTransactionDB.updateTransaction(selectedTransaction);


        selectedTransaction.Match.Status = "declined";
        EstabEstabMatchDB.updateMatch(selectedTransaction.Match);

        selectedRequest.MatchedUnits = selectedRequest.MatchedUnits - selectedTransaction.Units;
        EstablishmentBPRequestDB.updateEstablishmentRequest(selectedRequest);
        Server.Transfer("PendingRequests.aspx");
    }
    protected void gvRequests_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        EstablishmentBPRequest selectedRequest = pendingRequests[gvRequests.PageSize * gvRequests.PageIndex + e.RowIndex];
        int noTransactions = 0;
        List <EstabEstabTransaction> allEstabTransactions = EstabEstabTransactionDB.getAllTransactions();
        List <EstabUserTransaction>  allUserTransactions  = EstabUserTransactionDB.getAllTransactions();

        foreach (EstabEstabTransaction m in allEstabTransactions)
        {
            if (m.Match.Request.ID == selectedRequest.ID && m.Status == "complete")
            {
                noTransactions = 1;
            }
        }
        foreach (EstabUserTransaction m in allUserTransactions)
        {
            if (m.Match.Request.ID == selectedRequest.ID && m.Status == "complete")
            {
                noTransactions = 2;
            }
        }
        if (noTransactions == 1 || noTransactions == 2)
        {
            lblOutput.Text = "Sorry this request cannot be cancelled!";
        }
        else
        {
            foreach (EstabEstabTransaction m in allEstabTransactions)
            {
                if (m.Match.Request.ID == selectedRequest.ID)
                {
                    m.Status = "cancelled";
                    EstabEstabTransactionDB.updateTransaction(m);
                    m.Match.Status = "declined";
                    EstabEstabMatchDB.updateMatch(m.Match);
                }
            }
            foreach (EstabUserTransaction m in allUserTransactions)
            {
                if (m.Match.Request.ID == selectedRequest.ID)
                {
                    m.Status = "cancelled";
                    EstabUserTransactionDB.updateTransaction(m);
                    m.Match.Status = "declined";
                    EstabUserMatchDB.updateMatch(m.Match);
                }
            }

            selectedRequest.Status = "cancelled";
            EstablishmentBPRequestDB.updateEstablishmentRequest(selectedRequest);
            Server.Transfer("PendingRequests.aspx");
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["establishment"] == null)
        {
            Server.Transfer("Login.aspx");
        }
        else
        {
            Establishment          currentEstab     = (Establishment)Session["establishment"];
            List <EstabEstabMatch> allEstabRequests = EstabEstabMatchDB.getAllMatches();
            List <EstabEstabMatch> estabRequests    = new List <EstabEstabMatch>();
            foreach (EstabEstabMatch m in allEstabRequests)
            {
                if (m.Match.ID == currentEstab.ID && m.Status == "pending" && m.Request.MatchedUnits < m.Request.Units)
                {
                    estabRequests.Add(m);
                }
            }
            if (estabRequests.Count == 0)
            {
                lblOutput.Text           = "There are currently no requests from establishments";
                pnlEstabRequests.Visible = false;
            }
            else
            {
                gvEstabRequests.DataSource = estabRequests;
                gvEstabRequests.DataBind();
            }

            List <BPMatchUserToEstab> allUserRequests = BPMatchUserToEstabDB.getAllbpMatchUserToEsta();
            List <BPMatchUserToEstab> userRequests    = new List <BPMatchUserToEstab>();
            foreach (BPMatchUserToEstab m in allUserRequests)
            {
                if (m.matchID.ID == currentEstab.ID && m.status == "pending" && m.bpRequestID.unitMatched < m.bpRequestID.Units)
                {
                    userRequests.Add(m);
                }
            }
            if (userRequests.Count == 0)
            {
                lblOutput.Text          = "There are currently no requests from users";
                pnlUserRequests.Visible = false;
            }
            else
            {
                gvUserRequests.DataSource = userRequests;
                gvUserRequests.DataBind();
            }
        }
    }
    protected void gvEstabRequests_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Establishment          currentEstab     = (Establishment)Session["establishment"];
        List <EstabEstabMatch> allEstabRequests = EstabEstabMatchDB.getAllMatches();
        List <EstabEstabMatch> estabRequests    = new List <EstabEstabMatch>();

        foreach (EstabEstabMatch m in allEstabRequests)
        {
            if (m.Match.ID == currentEstab.ID && m.Status == "pending" && m.Request.MatchedUnits < m.Request.Units)
            {
                estabRequests.Add(m);
            }
        }
        gvEstabRequests.PageIndex  = e.NewPageIndex;
        gvEstabRequests.DataSource = estabRequests;
        gvEstabRequests.DataBind();
    }
    protected void btnEstabSubmit_Click(object sender, EventArgs e)
    {
        Establishment          currentEstab     = (Establishment)Session["establishment"];
        List <EstabEstabMatch> allEstabRequests = EstabEstabMatchDB.getAllMatches();
        List <EstabEstabMatch> estabRequests    = new List <EstabEstabMatch>();

        foreach (EstabEstabMatch m in allEstabRequests)
        {
            if (m.Match.ID == currentEstab.ID && m.Status == "pending" && m.Request.MatchedUnits < m.Request.Units)
            {
                estabRequests.Add(m);
            }
        }
        EstabEstabMatch        currentMatch   = estabRequests[gvEstabRequests.PageSize * gvEstabRequests.PageIndex + gvEstabRequests.SelectedIndex];
        EstablishmentBPRequest r              = currentMatch.Request;
        EstabEstabTransaction  newTransaction = new EstabEstabTransaction();

        if (Convert.ToInt32(tbxEstabUnits.Text) > 0 || Convert.ToInt32(tbxUserUnits.Text) > 0)
        {
            int donateUnits = Convert.ToInt32(tbxEstabUnits.Text);
            if (donateUnits > (currentMatch.Request.Units - currentMatch.Request.MatchedUnits))
            {
                lblEstabOutput.Text = "Please enter a valid amount";
            }
            else
            {
                currentMatch.Status = "accepted";
                EstabEstabMatchDB.updateMatch(currentMatch);
                newTransaction.Match  = currentMatch;
                newTransaction.Units  = donateUnits;
                newTransaction.Status = "accepted";
                EstabEstabTransactionDB.insertTransaction(newTransaction);
                r.MatchedUnits = r.MatchedUnits + donateUnits;
                EstablishmentBPRequestDB.updateEstablishmentRequest(r);
                lblOutput.Text         = "Donation successfully accepted!";
                pnlAcceptEstab.Visible = false;
                tbxEstabUnits.Text     = "";
                Server.Transfer("IncomingRequests.aspx");
            }
        }
        else
        {
            lblOutput.Text = "The Unit cannot be less than 0 or less";
            return;
        }
    }
    protected void gvEstabRequests_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Establishment          currentEstab     = (Establishment)Session["establishment"];
        List <EstabEstabMatch> allEstabRequests = EstabEstabMatchDB.getAllMatches();
        List <EstabEstabMatch> estabRequests    = new List <EstabEstabMatch>();

        foreach (EstabEstabMatch m in allEstabRequests)
        {
            if (m.Match.ID == currentEstab.ID && m.Status == "pending" && m.Request.MatchedUnits < m.Request.Units)
            {
                estabRequests.Add(m);
            }
        }
        EstabEstabMatch currentMatch = estabRequests[gvEstabRequests.PageSize * gvEstabRequests.PageIndex + e.RowIndex];

        currentMatch.Status = "declined";
        EstabEstabMatchDB.updateMatch(currentMatch);
        Server.Transfer("IncomingRequests.aspx");
    }
    protected void gvEstabMatches_SelectedIndexChanged(object sender, EventArgs e)
    {
        EstablishmentBPRequest selectedRequest = pendingRequests[gvRequests.PageSize * gvRequests.PageIndex + gvRequests.SelectedIndex];

        List <EstabEstabTransaction> allEstabTransactions = EstabEstabTransactionDB.getAllTransactions();

        foreach (EstabEstabTransaction m in allEstabTransactions)
        {
            if (m.Match.Request.ID == selectedRequest.ID && m.Status == "accepted")
            {
                estabTransactions.Add(m);
            }
        }
        EstabEstabTransaction selectedTransaction = estabTransactions[gvEstabMatches.SelectedIndex];

        selectedTransaction.Status = "complete";
        EstabEstabTransactionDB.updateTransaction(selectedTransaction);

        EstabEstabMatch currentMatch = selectedTransaction.Match;

        currentMatch.Status = "complete";
        EstabEstabMatchDB.updateMatch(currentMatch);



        if (selectedRequest.MatchedUnits == selectedRequest.Units)
        {
            selectedRequest.Status = "complete";
            EstablishmentBPRequestDB.updateEstablishmentRequest(selectedRequest);
            foreach (EstabEstabTransaction et in allEstabTransactions)
            {
                if (et.Match.Request.ID == selectedRequest.ID)
                {
                    et.Status = "complete";
                    EstabEstabTransactionDB.updateTransaction(et);
                    et.Match.Status = "complete";
                    EstabEstabMatchDB.updateMatch(et.Match);
                }
            }
        }
        Server.Transfer("PendingRequests.aspx");
    }
Esempio n. 10
0
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //sending request if the amount is greater than 1
        if (Convert.ToInt32(tbxAmount.Text) > 0)
        {
            Establishment          currentEstab     = (Establishment)Session["establishment"];
            EstablishmentBPRequest currentRequestBP = new EstablishmentBPRequest();
            currentRequestBP.BloodGroup    = ddlBloodType.SelectedValue;
            currentRequestBP.Units         = Convert.ToInt32(tbxAmount.Text);
            currentRequestBP.MatchedUnits  = 0;
            currentRequestBP.Type          = ddlRequestType.SelectedValue;
            currentRequestBP.Establishment = currentEstab;
            currentRequestBP.Status        = "pending";
            currentRequestBP.RequestDate   = DateTime.Today;
            int num = EstablishmentBPRequestDB.insertEstablishmentRequest(currentRequestBP);
            if (num != 1)
            {
                lblOutput.Text = "Cannot";
            }
            else
            {
                List <EstablishmentBPRequest> establishmentBPRequest = EstablishmentBPRequestDB.getAllEstablishmentRequests();
                //get inserted request
                string x      = establishmentBPRequest[0].ID;
                int    tempId = Convert.ToInt32(x.Substring(4, x.Length - 4));
                foreach (EstablishmentBPRequest tempReq in establishmentBPRequest)
                {
                    int t = Convert.ToInt32(tempReq.ID.Substring(4, tempReq.ID.Length - 4));

                    if (tempId <= t && tempReq.Establishment.ID == currentEstab.ID)
                    {
                        tempId = Convert.ToInt32(tempReq.ID.Substring(4, tempReq.ID.Length - 4));
                    }
                }
                EstablishmentBPRequest request = EstablishmentBPRequestDB.getRequestByID("bper" + Convert.ToString(tempId));
                lblOutput.Text = "Your request ID is bper" + tempId;
                List <Establishment> establishments = EstablishmentDB.getAllEstablishments();
                foreach (Establishment est in establishments)
                {
                    EstabEstabMatch newMatch = new EstabEstabMatch();
                    if ((est.Type == "Hospital" || est.Type == "Blood Bank") && currentEstab.ID != est.ID)
                    {
                        int d = getDistance(est.Address, currentEstab.Address);
                        if (d < 3600)
                        {
                            newMatch.Request  = request;
                            newMatch.Match    = est;
                            newMatch.Distance = d;
                            newMatch.Status   = "pending";
                            EstabEstabMatchDB.insertMatch(newMatch);
                        }
                    }
                }
                List <Users>            users    = UsersDB.getallUsers();
                List <LastDonationDate> allDates = LastDonationDateDB.getAllLastDonations();

                foreach (Users tusr in users)
                {
                    EstabUserMatch newMatch     = new EstabUserMatch();
                    string         userGroup    = tusr.BloodType;
                    string         requestGroup = request.BloodGroup;
                    bool           f            = false;
                    //check blood to match with other user
                    if (requestGroup == "AB+")
                    {
                        f = true;
                    }
                    else if (requestGroup == "AB-" && (userGroup == "O-" || userGroup == "B-" || userGroup == "A-" || userGroup == "AB-"))
                    {
                        f = true;
                    }
                    else if (requestGroup == "A+" && (userGroup == "O-" || userGroup == "O+" || userGroup == "A-" || userGroup == "A+"))
                    {
                        f = true;
                    }
                    else if (requestGroup == "A-" && (userGroup == "O-" || userGroup == "A-"))
                    {
                        f = true;
                    }
                    else if (requestGroup == "B+" && (userGroup == "O-" || userGroup == "O+" || userGroup == "B-" || userGroup == "B+"))
                    {
                        f = true;
                    }
                    else if (requestGroup == "B-" && (userGroup == "O-" || userGroup == "B-"))
                    {
                        f = true;
                    }
                    else if (requestGroup == "O+" && (userGroup == "O-" || userGroup == "O+"))
                    {
                        f = true;
                    }
                    else if (requestGroup == "O-" && (userGroup == "O-"))
                    {
                        f = true;
                    }

                    //if match found check his medical status and status
                    if (f == true && tusr.medicalStatus.ToLower() == "can donate" && tusr.Status == "Allow")
                    {
                        foreach (LastDonationDate d in allDates)
                        {
                            if (d.User.userId == tusr.userId)
                            {
                                if ((d.LastDonation < System.DateTime.Now.AddDays(-90) && d.Type == "blood") || (d.LastDonation < System.DateTime.Now.AddDays(-14) && d.Type == "platelet") && d.Status == "Not in transaction")
                                {
                                    int d1 = getDistance(Convert.ToString(tusr.latitude) + "," + Convert.ToString(tusr.longtitude), currentEstab.Address);
                                    if (d1 < 3600)
                                    {
                                        newMatch.Match    = tusr;
                                        newMatch.Request  = request;
                                        newMatch.Status   = "pending";
                                        newMatch.Distance = d1;
                                        EstabUserMatchDB.insertMatch(newMatch);
                                    }
                                }
                            }
                        }
                    }
                }

                tbxAmount.Text               = "";
                ddlBloodType.SelectedIndex   = 0;
                ddlRequestType.SelectedIndex = 0;
                string MyAccountUrl = "RequestBlood.aspx";
                Page.Header.Controls.Add(new LiteralControl(string.Format(@" <META http-equiv='REFRESH' content=4;url={0}> ", MyAccountUrl)));
            }
        }
        else
        {
            lblOutput.Text = "Blood amount cannot be 0 or less than that";
            return;
        }
    }