Function to generate fixed day-to-day launch

Asked

Viewed 158 times

1

I have this function that when starting with T it will generate invoice for every day, example: if you have T10 will generate every day 10 as specified in the form of payment. Currently being done this way:

  if (vf.Substring(0, 1) == "T")
                {
                    var par = vf.Substring(1, 2);
                    for (int i = 1; i <= parcelas; i++)
                    {
                        var venc = new FaturaContasReceber
                        {
                            FaturaContasReceberPId = idfatura,
                            DataFatura = DateTime.Now,
                            DiasAtraso = totalDia,
                            Data = DateTime.Parse(par + "/" + DateTime.Now.AddMonths(i).ToString("MM") + "/" + DateTime.Now.ToString("yyyy")),
                            DataPagamento = datapagamento,
                            Valor = valor1,
                            Total = saldo,
                            Saldo = saldo,
                            Quitado = quitado,
                            Caixa = caixa,
                        };
                        db.Add(venc);
                        db.SaveChanges();
                        saldo = 0;
                    }
                }

The problem that generating today for example, with every day 10, it is generating for the day 10/05, where the correct would be for the day 10/04. How can I do this date check ? Because currently I am 1 month

Using the logic of an added answer to this question, I made some changes and stayed this way:

 if (DateTime.Now.Day < Convert.ToInt32(par) && i == 1)
                        {
                            datavenc = DateTime.Parse(par + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("yyyy"));
                        }
                        else
                        {
                            datavenc = DateTime.Parse(par + "/" + DateTime.Now.AddMonths(i - 1).ToString("MM") + "/" + DateTime.Now.ToString("yyyy"));
                        }

Only that so generates for the day 10/04 and jumps to the 10/06 and not 10/05.

  • A comment that has nothing to do with the question, but has to do with the code: If we have in the month 12 and have 2 installments, it would not be "dd/12/2019" and "dd/01/2019", and the correct one would be "dd/01/2020"?

  • Regarding the question: If you change the int i = 1 for int i = 0, would probably generate on 10/04.

  • @Focuses in the case if it is every day, it is all next day 10, if it is another type of condition there should be chosen another.

  • @Focuses if I start at zero, instead of generating 2 times, would generate 3 times, for this reason starts at 1.

  • But then you put i <= parcelas - 1.

  • Or simply i < parcels

Show 1 more comment

1 answer

1


Changing your code a little bit the logic would look something like this. You have to improve the code but the logic is this.

          DateTime teste = new DateTme();
          for (int i = 1; i <= parcelas; i++)
            {
                if(DateTime.Now.Day < Convert.ToInt32(par))
                {
                    teste = (par + "/" + DateTime.Now.Month.ToString("MM") + "/" + DateTime.Now.ToString("yyyy"));
                }
                else
                {
                    teste = (par + "/" + DateTime.Now.AddMonth(1).ToString("MM") + "/" + DateTime.Now.ToString("yyyy"));
                }
                var venc = new FaturaContasReceber
                        {
                            FaturaContasReceberPId = idfatura,
                            DataFatura = DateTime.Now,
                            DiasAtraso = totalDia,
                            Data = teste,
                            DataPagamento = datapagamento,
                            Valor = valor1,
                            Total = saldo,
                            Saldo = saldo,
                            Quitado = quitado,
                            Caixa = caixa,
                        };
            }
  • It did not work, so generated the two for the same month. in the case of May. continue forward.

  • My mistake, the signal is reversed in the if

  • I took a little of the street answer, but I did so: if (DateTime.Now.Day < Convert.ToInt32(par) && i == 1){&#xA; datavenc = DateTime.Parse(par + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("yyyy"));&#xA; }&#xA; else&#xA; {&#xA; datavenc = DateTime.Parse(par + "/" + DateTime.Now.AddMonths(i - 1).ToString("MM") + "/" + DateTime.Now.ToString("yyyy"));&#xA; }

Browser other questions tagged

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