Restart count when changing year

Asked

Viewed 69 times

1

I have a View that returns data that the taxpayer paid the pension in the current year, where I return the monthly payment of the same. I need to perform the sum of the months, creating a monthly subtotal. With help I managed to work, leaving the result as the example:
Jsfiddle example

But I need the sum to restart at the turn of the year. This logic that I am not able to elaborate. I need the end result to turn something like:
Example Expected result

My controller is like this:

public ActionResult Index()
        {
           //realizo a busca no BD
            var usuario =
                previdenciaRepository.Previdencias.Where(p => p.CdMatricula == matricula && p.SqContrato == contrato).ToList();       

            return View(usuario);
        }

And in the view I do the calculation to add the returned data, so:

<table border="1">
        <thead>
            <tr>
                <th rowspan="2">
                    Mês
                </th>
                <th colspan="4">
                    <p align="center">
                        Contribuinte
                    </p>
                </th>
            </tr>
           <tr>
                <th>
                    %
                </th>

                <th>
                    R$ Ano
                </th>
                <th>
                    Acumulado R$
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.Previdencia.GroupBy(g => g.NmPessoa))
            {
                double subtotal = 0;
                foreach (var contribuicoes in item.ToList())
                {
                    subtotal += contribuicoes.Contribuinte;
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => contribuicoes.dtCompetencia)
                        </td>
                        <td>
                            11
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => contribuicoes.Contribuinte)
                        </td>
                        <td>
                            @subtotal.ToString("c")
                        </td>
                    </tr>
                }
            }
        </tbody>
    </table>

My Model is like this:

public class Previdencia
    {
        [Key]
        public Int64 Cod_Previdencia { get; set; }
        public int CdMatricula { get; set; }
        public Int16 SqContrato { get; set; }

        public string NmPessoa { get; set; }

        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? dtCompetencia { get; set; }

    }

1 answer

1


Quite simple. Just change your GroupBy().

By comment he was asked about the totalization of all records. This can be done by initializing a variable outside the loop and incrementing it within the loop:

        @{ var total = 0; }

        @foreach (var item in Model.Previdencia.GroupBy(g => new { g.NmPessoa, g.dtcompetencia.Year }))
        {
            double subtotal = 0;
            foreach (var contribuicoes in item.ToList())
            {
                subtotal += contribuicoes.Contribuinte;
                total += contribuicoes.Contribuinte;

                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => contribuicoes.dtCompetencia)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => contribuicoes.dtcompetencia)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => contribuicoes.Contribuinte)
                    </td>
                    <td>
                        @subtotal.ToString("c")
                    </td>
                </tr>
            }
        }
  • As always, you helped me again. Just a doubt. To contain the two fields, how should I proceed? The total and counting per year? Thus: http://jsfiddle.net/randrade/9qj04130/1/

  • @Renilsonandrade It can be by month too. Or it can be by a grouping assembled by you. Creativity is the limit.

  • I understood this part. However I need to save the total sum (without zeroing per year) in another field (as was done with the variable subtotal). Would you be able to do this by creating another variable? Or would you rather I open another question?

  • @Renilsonandrade No need. I already put in the answer. See if it meets you.

  • 1

    Thank you so much again, it was just that. Sometimes I even get puzzled at the ease of my problems. But that’s the way it is. rsrs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.