Untitled
unknown
plain_text
8 months ago
6.4 kB
7
Indexable
const getDaysInYear = (year) => {
const startDate = new Date(year, 0, 1);
const endDate = new Date(year, 11, 31);
const oneDay = 1000 * 60 * 60 * 24;
//console.log(startDate, endDate, (endDate - startDate));
const days = Math.floor((endDate - startDate) / oneDay) + 1;
return days;
};
// 2024 şubat için örnek olarak 2024, 2 verilmesi lazım
const getDaysInMonth = (year, month) => new Date(year, month, 0).getDate();
const daysUntilEndOfMonth = (date) => {
const year = date.getFullYear();
const month = date.getMonth();
const endOfMonth = new Date(year, month, getDaysInMonth(year, month));
const oneDay = 1000 * 60 * 60 * 24;
const diffInTime = endOfMonth - date;
const remainingDays = Math.ceil(diffInTime / oneDay);
// console.log(year, month, endOfMonth, diffInTime, remainingDays);
return remainingDays;
};
const daysUntilEndOfYear = (date) => {
const year = date.getFullYear();
const endOfYear = new Date(year, 11, 31);
const oneDay = 1000 * 60 * 60 * 24;
const diffInTime = endOfYear - date;
const remainingDays = Math.ceil(diffInTime / oneDay);
return remainingDays;
};
function convertToDate(dateString) {
const [day, month, year] = dateString.split('/').map(Number);
return new Date(year, month - 1, day);
};
class DepreciationIFRSHelper {
calculateIFRSDepreciation(year, month, depreciationStartDate, price, lifetime) {
let remainingMonth = lifetime * 12;
let calculations = [];
let cumulativeDepreciation = 0;
let netBookValue = 0;
//console.log('calculateIFRSDepreciation', year, month, depreciationStartDate, price, lifetime);
function calculateDepreciation(_year, _month) {
const daysInYear = getDaysInYear(_year);
const daysInMonth = getDaysInMonth(_year, _month);
const depreciationRatio = (1 / lifetime);
const yearlyDepreciation = (price / lifetime);
const dailyDepreciation = (yearlyDepreciation / daysInYear);
const monthlyDepreciation = (daysInMonth * dailyDepreciation);
//console.log('calculateDepreciation', _year, _month, remainingMonth, daysInYear, daysInMonth, depreciationRatio, yearlyDepreciation, dailyDepreciation, monthlyDepreciation);
let remainingDays = daysInMonth;
if (_year === depreciationStartDate.getFullYear() && _month === (depreciationStartDate.getMonth() + 1)) {
remainingDays = daysUntilEndOfMonth(depreciationStartDate);
}
let depreciation = Math.min(monthlyDepreciation, (remainingDays * dailyDepreciation));
if (remainingMonth === 0) {
depreciation = calculations[calculations.length - 1].netBookValue;
cumulativeDepreciation += depreciation;
netBookValue = price - cumulativeDepreciation;
} else {
cumulativeDepreciation += depreciation;
netBookValue = price - cumulativeDepreciation;
}
calculations.push({
year: _year,
month: _month,
price,
depreciationRatio,
percentage: `%${(depreciationRatio * 100).toFixed(2)}`,
yearlyDepreciation,
monthlyDepreciation,
dailyDepreciation,
remainingDays,
depreciation,
cumulativeDepreciation,
netBookValue,
});
if (remainingMonth > 0) {
remainingMonth -= 1;
let nextYear = _month === 12 ? _year + 1 : _year;
let nextMonth = _month === 12 ? 1 : _month + 1;
calculateDepreciation(nextYear, nextMonth);
}
}
if (lifetime > 100) {
return undefined;
}
const startYear = depreciationStartDate.getFullYear();
const startMonth = depreciationStartDate.getMonth() + 1;
calculateDepreciation(startYear, startMonth);
const mappedDepreciation = calculations.map(calc => ({
year: calc.year,
month: calc.month,
price: Number(calc.price).toFixed(2),
percentage: `%${(calc.depreciationRatio * 100).toFixed(2)}`,
yearlyDepreciation: calc.yearlyDepreciation.toFixed(2),
monthlyDepreciation: calc.monthlyDepreciation.toFixed(2),
dailyDepreciation: calc.dailyDepreciation.toFixed(2),
remainingDays: calc.remainingDays,
depreciation: calc.depreciation.toFixed(2),
cumulativeDepreciation: calc.cumulativeDepreciation.toFixed(2),
netBookValue: calc.netBookValue.toFixed(2),
}));
const message = 'Seçilen dönem amortisman hesabı yoktur.';
function findAndCalculateActiveYearDepreciation(year, month) {
const cumulativeActiveYearDepreciation = calculations
.filter(calc => calc.year === year && calc.month <= month)
.reduce((acc, curr) => acc + curr.depreciation, 0);
const result = mappedDepreciation.find(calc => calc.year === year && calc.month === month);
if (result) {
result.activeYearDepreciation = cumulativeActiveYearDepreciation.toFixed(2);
return result;
}
return undefined;
}
//console.log(mappedDepreciation);
if (mappedDepreciation.length > 1) {
const first = mappedDepreciation[0];
const last = mappedDepreciation[mappedDepreciation.length - 1];
if (year === first.year && month >= first.month) {
return findAndCalculateActiveYearDepreciation(year, month);
//return mappedDepreciation.find(calc => calc.year === year && calc.month === month);
} else if (year === last.year && month <= last.month) {
return findAndCalculateActiveYearDepreciation(year, month);
//return mappedDepreciation.find(calc => calc.year === year && calc.month === month);
} else if (year > first.year && year < last.year) {
return findAndCalculateActiveYearDepreciation(year, month);
//return mappedDepreciation.find(calc => calc.year === year && calc.month === month);
}
}
return {
year: message,
month: message,
price: message,
percentage: message,
yearlyDepreciation: message,
monthlyDepreciation: message,
dailyDepreciation: message,
remainingDays: message,
depreciation: message,
cumulativeDepreciation: message,
netBookValue: message,
activeYearDepreciation: message,
};
}
}
module.exports = DepreciationIFRSHelper;
//const result = new DepreciationIFRSHelper().calculateIFRSDepreciation(2024, 9, new Date(2024, 6, 10), 33609.00, 4);
//console.log(result);
Editor is loading...
Leave a Comment