Files
SweetHome/app/utils/calculations.js

67 lines
1.9 KiB
JavaScript
Raw Normal View History

export const calculateRentWithCommission = (
dailyPrice,
numberOfDays,
commissionRate,
commissionType
) => {
const baseRent = dailyPrice * numberOfDays;
const commission = (baseRent * commissionRate) / 100;
switch(commissionType) {
case 'from_tenant':
return {
totalForTenant: baseRent + commission,
totalForOwner: baseRent,
commission: commission
};
case 'from_owner':
return {
totalForTenant: baseRent,
totalForOwner: baseRent - commission,
commission: commission
};
case 'from_both':
return {
totalForTenant: baseRent + (commission / 2),
totalForOwner: baseRent - (commission / 2),
commission: commission
};
default:
return {
totalForTenant: baseRent,
totalForOwner: baseRent,
commission: 0
};
}
};
export const calculateDaysBetween = (startDate, endDate) => {
const start = new Date(startDate);
const end = new Date(endDate);
const diffTime = Math.abs(end - start);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
return diffDays;
};
export const formatCurrency = (amount) => {
return new Intl.NumberFormat('ar-SY', {
style: 'currency',
currency: 'SYP',
minimumFractionDigits: 0,
maximumFractionDigits: 0
}).format(amount).replace('SYP', '') + ' ل.س';
};
export const calculateTenantBalance = (bookings, securityDeposits) => {
return bookings.reduce((acc, booking) => {
const deposit = securityDeposits.find(d => d.bookingId === booking.id) || 0;
return {
totalRent: acc.totalRent + booking.totalAmount,
paidAmount: acc.paidAmount + booking.paidAmount,
securityDeposit: acc.securityDeposit + deposit.amount,
pendingAmount: (acc.pendingAmount + (booking.totalAmount - booking.paidAmount))
};
}, { totalRent: 0, paidAmount: 0, securityDeposit: 0, pendingAmount: 0 });
};