private void PopulateGrid()
    {
        Dictionary <int, AdvanceDebt> debtLookup = new Dictionary <int, AdvanceDebt>();

        ExpenseClaims claims = ExpenseClaims.ForOrganization(Organization.PPSE);

        foreach (ExpenseClaim claim in claims)
        {
            // If ready for payout, add to list.

            if (claim.Open)
            {
                if (claim.Attested && claim.Validated && !claim.Repaid)
                {
                    // this should be added to the list. Check if we already have some open claims
                    // for this person:

                    if (debtLookup.ContainsKey(claim.ClaimingPersonId))
                    {
                        // Yes. Add claim to list.

                        debtLookup[claim.ClaimingPersonId].DebtCents -= claim.AmountCents;
                        debtLookup[claim.ClaimingPersonId].ClaimIds.Add(claim.Identity);
                    }
                    else
                    {
                        // No. Create a new debt for this person.

                        AdvanceDebt debt = new AdvanceDebt(claim.Claimer, -claim.AmountCents);
                        debt.ClaimIds.Add(claim.Identity);

                        debtLookup[claim.ClaimingPersonId] = debt;
                    }

                    if (debtLookup[claim.ClaimingPersonId].EarliestDate > claim.CreatedDateTime)
                    {
                        debtLookup[claim.ClaimingPersonId].EarliestDate = claim.CreatedDateTime;
                    }
                }
            }
        }

        // Now, we have grouped all ready but unsettled expenses per person. Let's add only those with a positive debt to the final list.

        List <AdvanceDebt> debtList = new List <AdvanceDebt>();

        foreach (int personId in debtLookup.Keys)
        {
            if (debtLookup[personId].DebtCents > 0)
            {
                debtList.Add(debtLookup[personId]);
            }
        }

        debtList.Sort(SortGridItems);

        this.GridDebts.DataSource = debtList;
    }
    private void PopulateGrid()
    {
        Dictionary<int, AdvanceDebt> debtLookup = new Dictionary<int, AdvanceDebt>();

        ExpenseClaims claims = ExpenseClaims.ForOrganization(Organization.PPSE);

        foreach (ExpenseClaim claim in claims)
        {
            // If ready for payout, add to list.

            if (claim.Open)
            {
                if (claim.Attested && claim.Validated && !claim.Repaid)
                {
                    // this should be added to the list. Check if we already have some open claims
                    // for this person:

                    if (debtLookup.ContainsKey(claim.ClaimingPersonId))
                    {
                        // Yes. Add claim to list.

                        debtLookup[claim.ClaimingPersonId].DebtCents -= claim.AmountCents;
                        debtLookup[claim.ClaimingPersonId].ClaimIds.Add(claim.Identity);
                    }
                    else
                    {
                        // No. Create a new debt for this person.

                        AdvanceDebt debt = new AdvanceDebt(claim.Claimer, -claim.AmountCents);
                        debt.ClaimIds.Add(claim.Identity);

                        debtLookup[claim.ClaimingPersonId] = debt;
                    }

                    if (debtLookup[claim.ClaimingPersonId].EarliestDate > claim.CreatedDateTime)
                    {
                        debtLookup[claim.ClaimingPersonId].EarliestDate = claim.CreatedDateTime;
                    }
                }
            }
        }

        // Now, we have grouped all ready but unsettled expenses per person. Let's add only those with a positive debt to the final list.

        List<AdvanceDebt> debtList = new List<AdvanceDebt>();

        foreach (int personId in debtLookup.Keys)
        {
            if (debtLookup[personId].DebtCents > 0)
            {
                debtList.Add(debtLookup[personId]);
            }
        }

        debtList.Sort(SortGridItems);

        this.GridDebts.DataSource = debtList;
    }
    private static int SortGridItems(AdvanceDebt debt1, AdvanceDebt debt2)
    {
        if (debt2.DebtCents == debt1.DebtCents)
        {
            return(0);
        }

        return(debt2.DebtCents - debt1.DebtCents > 0? 1: -1);
    }
    protected void GridPayouts_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            AdvanceDebt debt = (AdvanceDebt)e.Item.DataItem;

            if (debt == null)
            {
                return;
            }

            /*
             * Label labelDueDate = (Label)e.Item.FindControl("LabelDueDate");
             * if (payout.ExpectedTransactionDate < DateTime.Now)
             * {
             *  labelDueDate.Text = "ASAP";
             * }
             * else
             * {
             *  labelDueDate.Text = payout.ExpectedTransactionDate.ToString("yyyy-MM-dd");
             * }*/


            /*
             * Controls_v4_DocumentList docList = (Controls_v4_DocumentList) e.Item.FindControl("DocumentListClaim");
             *
             * if (docList != null)
             * {
             *  docList.Documents = Documents.ForObject(claim);
             * }*/

            HyperLink editLink = (HyperLink)e.Item.FindControl("LinkPartialInvoice");
            editLink.Attributes["href"] = "InvoiceAdvanceDebtsPartial.aspx?PersonId=" + debt.Person.Identity + "&OrganizationId=" + Organization.PPSEid;

            /*editLink.Attributes["onclick"] = String.Format("return ShowExpenseClaimForm('{0}','{1}');",
             *                                             claim.Identity, e.Item.ItemIndex);*/
        }
    }
    private static int SortGridItems (AdvanceDebt debt1, AdvanceDebt debt2)
    {
        if (debt2.DebtCents == debt1.DebtCents)
        {
            return 0;
        }

        return (debt2.DebtCents - debt1.DebtCents > 0? 1: -1);
    }