Validating invoice closing for purchase type and selling JavaScript and submitting for an api

2

I am a beginner and I locked in 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 credit card closing on the 5th of each month right, the installment always falls on the 10th, a purchase or payment made on the 2017-01-05 day onwards the first installment falls on the 2017-02-10 day , in case if you have 2017-01-03 will fall in 2017-01-10.

Then in case the payment is debit, it will fall at the same time described in the date column.

I tried in several ways and did not succeed.

What I tried to do, I created a for to 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 next month ie 10/03, and if it was debit he already added the value for the day, if it was

asked by anonymous 31.03.2018 / 05:12

2 answers

0

For more information my repository: link I made it right on server.js

    
18.04.2018 / 17:00
0

Do the following.

  • Retrieve the current date in a variable:

    let DATE = new Date(); // Data atual
    
  • Retrieve the current month:

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

    Remember that the sum of d DATE.getMonth() plus 1, is due to return method 0 to 11.

  • Recover the previous 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 December .

  • 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 January .

  • Now retrieve 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
    
  • Declare 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 will not 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);
    
      });
    }
    

    Example running:

    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);
        
    19.04.2018 / 05:08