Advertisement
Pushm3

Untitled

Jun 26th, 2022
1,064
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.35 KB | None
  1. const { Client } = require('pg');
  2.  
  3. const client = new Client({
  4.   host: 'localhost',
  5.   user: 'postgres',
  6.   port: 5432,
  7.   password: '123',
  8.   database: 'AltOne',
  9. });
  10.  
  11. function calculateData(result) {
  12.   let monthCounter = 0;
  13.   let interestDebt = 0;
  14.   let principalDebt = 0;
  15.   let balanceDebt = result.credit_amount;
  16.  
  17.   const monthlyRate = +(result.bet_amount / (100 * 12));
  18.   const monthlyPaymentDenominator = (1 - ((1 + monthlyRate) ** (-result.credit_term)));
  19.   const monthlyPayment = +(result.credit_amount * (monthlyRate / monthlyPaymentDenominator));
  20.  
  21.   return () => {
  22.     monthCounter += 1;
  23.     interestDebt = (balanceDebt * monthlyRate);
  24.     principalDebt = (monthlyPayment - interestDebt);
  25.     balanceDebt -= principalDebt;
  26.  
  27.     return {
  28.       a: monthCounter,
  29.       b: monthlyPayment.toFixed(2),
  30.       d: principalDebt.toFixed(2),
  31.       c: interestDebt.toFixed(2),
  32.       e: balanceDebt.toFixed(2),
  33.     };
  34.   };
  35. }
  36.  
  37. async function getData(applicationId) {
  38.   const rqst1 = 'select credit_amount, bet_amount, credit_term from applications where id = $1';
  39.   const rqst2 = 'select sum(service_cost)'
  40.     + ' from application_additional_services'
  41.     + ' join additional_services asm on asm.id = application_additional_services.additional_service_id'
  42.     + ' where application_id = $1';
  43.  
  44.   try {
  45.     client.connect();
  46.     const objectsArray = [];
  47.     const appData = await client.query(rqst1, [applicationId]);
  48.     const serviceCostData = await client.query(rqst2, [applicationId]);
  49.     appData.rows[0].credit_amount = +appData.rows[0].credit_amount + +serviceCostData.rows[0].sum;
  50.     const calculator = calculateData(appData.rows[0]);
  51.  
  52.     console.log(`Сумма кредита: ${appData.rows[0].credit_amount}`);
  53.     console.log(`Ставка: ${appData.rows[0].bet_amount}`);
  54.     console.log(`Срок: ${appData.rows[0].credit_term}`);
  55.     console.log('Месяц | Ежемесячный платеж | Основной долг | Долг по процентам | Остаток');
  56.     for (let i = 0; i < appData.rows[0].credit_term; i += 1) {
  57.       objectsArray.push(calculator());
  58.       console.log(`${objectsArray[i].a} | ${objectsArray[i].b} | ${objectsArray[i].c} | ${objectsArray[i].d} | ${objectsArray[i].e}`);
  59.     }
  60.  
  61.     return objectsArray;
  62.   } catch (err) { return err.stack; } finally { client.end(); }
  63. }
  64.  
  65. getData(5);
  66.  
Advertisement
RAW Paste Data Copied
Advertisement