Validating invoice closure to buy and sell Type JavaScript and submit to an api

I'm a beginner and stuck on 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 2017-01-05 onwards the first installment falls on 2017-02-10, in case if it has on 2017-01-03 it will fall on 2017-01-10.

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

I tried several ways and ended up failing.

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 month q comes IE 10/03, and if it was debit he already added the value for the day, if it was

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

Author: Samael Pereira Simões, 2018-03-31

2 answers

For more information my repository: https://github.com/samaelsimoes/activity-nodejs-with-json-files I did it right on the server.js

 0
Author: Samael Pereira Simões, 2018-04-18 15:00:28

Do the following.

  1. Retrieve the current date in a variable:

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

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

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

  3. Recover previous month:

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

    The above condition checks if 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 condition above checks if the current month is December , if it is, the next one will be January.

  5. 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
    
  6. 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);

  });
}

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
Author: NoobSaibot, 2018-04-19 03:08:01