Validando fechamento de factura para tipo de compra e venda Javascript e submeter para uma api

Asked

Viewed 90 times

2

I’m a beginner and I’m stuck on the invoice closing validation. I built a list in this format:

[ 

  date: '2017-5-9', value: '59.99', type: 'credit', typePag: 1 },

 { date: '2017-12-17',value: 'R$ 151.08',type: 'debit', typePag: 2 }

];

What I need:

We have closing the credit card on the 5th of every right month, the installment always falls on the 10th, a purchase or payment made on the day 2017-01-05 onwards the first installment falls on the day 2017-02-10, in case it has on the day 2017-01-03 will fall in 2017-01-10.

Ai in case if the payment is debit, it will fall at the same instant described in the date column.

I tried so many ways and ended up not getting.

What I tried to do, I created a go through, in that I checked the dates if it is > 5/02 and if it was credit he added the value to the invoice of the month q comes or be 10/03, and if it was debit he would already add the value for the day, if it was <5 would fall in the invoice of the same month. did something like that

Note: I am using nodejs. And how do I submit to an api ?

  • Now I have doubts: In the case of credit, how do you know that it is a purchase and if it is the first payment ? ... If you made a purchase using credit on the date: 2018-04-02 the first instalment would be on the date 2018-05-10 correct ? Then the closing of this installment would be in the month 06?

  • Opa good afternoon, The typePag Field: 1 would be sale, 2 would be purchase. 2 Referent " the first installment would be on the date 2018-05-10 correct ? So the closing of this installment would be in the month 06? "Yes correct a purchase or a sale if it is > 5 the installment will fall in the following month.

  • Sorry for the delay, just one more question, if it is debit and the purchase is made between > 5/02 and < 10/02 would fall in the same month ? I have an example almost ready, and rereading your question arose this question.

  • if it is larger than day 5 the invoice will fall in the one of the following month, if it is less than 5 will fall in the invoice of the same month

2 answers

0

Do the following.

  1. Retrieve the current date in a variable:

    let DATE = new Date(); // Data atual
    
  2. Recover the current month:

    let MONTH = DATE.getMonth() + 1; // Mês atual
    

    It is worth remembering that the sum d DATE.getMonth() plus 1, it is due to the method return 0 to 11.

  3. Retrieve the Anoteriror month:

    let MONTH_PREVIOUS = ( MONTH === 1 ) ? 12 : (MONTH - 1); // Mês anterior
    

    The above condition checks whether the current month is January, if it is, the previous one will be December.

  4. Recover next month:

    let NEXT_MONTH = ( MONTH === 12 ) ? 1 : (MONTH + 1); // Próximo mês
    

    The above condition checks whether the current month is December, if it is, the next one will be January.

  5. Now recover the current, previous and next year:

    let YEAR = DATE.getFullYear(); // Ano atual
    let YEAR_PREVIOUS = YEAR - 1; // Ano anterior
    let NEXT_YEAR = YEAR + 1; // Próximo ano
    
  6. Declare the closing lists:

    let LIST_OF_CLOSING = []; // Lista de fechamento
    let LIST_THE_NEXT_CLOSINGIN = []; // Lista do próximo fechamento
    

Next step is to create the method, I won’t explain step-by-step, but the code is commented:

// Método para fechamento
const closing = () => {
  // Data de fechamento.
  let DATE_OF_CLOSING = new Date(`${YEAR}-${MONTH}-4`).getTime();
  // Percorre a lista
  LIST.map(item => {
    let DATE_ITEM = new Date(item.date).getTime();
    // Data de início
    let START_DATE = new Date(`${( MONTH === 1 ) ? YEAR_PREVIOUS : YEAR}-${MONTH_PREVIOUS}-5`).getTime();
    // Data do próximo fechamento.
    let NEXT_CLOSING_IN = new Date(`${( MONTH === 12 ) ? NEXT_YEAR : YEAR}-${NEXT_MONTH}-4`).getTime();

    /* Verifica se DATE_ITEM é menor ou igual a DATE_OF_CLOSING, depois
     * verifica se DATE_ITEM é maior ou igual a START_DATE, se for
     * adiciona o item na lista LIST_OF_CLOSING, caso contrário, verifica se
     * DATE_ITEM é maior ou igual a DATE_OF_CLOSING, depois verifica se
     * DATE_ITEM é menor ou igual a NEXT_CLOSING_IN, se for adiciona o item
     * na lista LIST_THE_NEXT_CLOSINGIN, caso contrário não faz nada.
     */
    (DATE_ITEM <= DATE_OF_CLOSING && DATE_ITEM >= START_DATE) ? LIST_OF_CLOSING.push(item) : (DATE_ITEM >= DATE_OF_CLOSING && DATE_ITEM <= NEXT_CLOSING_IN) && LIST_THE_NEXT_CLOSINGIN.push(item);

  });
}

Working example:

let LIST = [
  { date: '2018-2-4', value: '59.99', type: 'credit', typePag: 1 },
  { date: '2018-2-5', value: '59.99', type: 'debit', typePag: 2 },
  { date: '2018-2-5', value: '59.99', type: 'credit', typePag: 1 },
  { date: '2018-3-5', value: '59.99', type: 'credit', typePag: 1 },
  { date: '2018-3-5', value: '59.99', type: 'debit', typePag: 2 },
  { date: '2018-3-4', value: '59.99', type: 'credit', typePag: 1 },
  { date: '2018-4-5', value: '59.99', type: 'debit', typePag: 2 },
  { date: '2018-4-9', value: '234.99', type: 'credit', typePag: 1 },
  { date: '2018-4-3', value: '89.99', type: 'credit', typePag: 1 },
  { date: '2018-8-5', value: '89.99', type: 'credit', typePag: 1 },
  { date: '2018-4-17', value: '71.08', type: 'debit', typePag: 2 },
  { date: '2018-4-1', value: '151.08', type: 'debit', typePag: 2 },
];
let DATE = new Date();
let MONTH = DATE.getMonth() + 1;
let MONTH_PREVIOUS = ( MONTH === 1 ) ? 12 : (MONTH - 1);
let NEXT_MONTH = ( MONTH === 12 ) ? 1 : (MONTH + 1);
let YEAR = DATE.getFullYear();
let YEAR_PREVIOUS = YEAR - 1;
let NEXT_YEAR = YEAR + 1;
let LIST_OF_CLOSING = [];
let LIST_THE_NEXT_CLOSINGIN = [];
const closing = () => {
  let DATE_OF_CLOSING = new Date(`${YEAR}-${MONTH}-4`).getTime();
  LIST.map(item => {
    let DATE_ITEM = new Date(item.date).getTime();
    let START_DATE = new Date(`${( MONTH === 1 ) ? YEAR_PREVIOUS : YEAR}-${MONTH_PREVIOUS}-5`).getTime();
    let NEXT_CLOSING_IN = new Date(`${( MONTH === 12 ) ? NEXT_YEAR : YEAR}-${NEXT_MONTH}-4`).getTime();
    (DATE_ITEM <= DATE_OF_CLOSING && DATE_ITEM >= START_DATE) ? LIST_OF_CLOSING.push(item) : (DATE_ITEM >= DATE_OF_CLOSING && DATE_ITEM <= NEXT_CLOSING_IN) && LIST_THE_NEXT_CLOSINGIN.push(item);
    
  });
}
closing();
console.log('Lista de fechamento: ', LIST_OF_CLOSING);
console.log('Lista do próximo fechamento: ', LIST_THE_NEXT_CLOSINGIN);

0

Browser other questions tagged

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