Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Commit

Permalink
reworked api v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Camel Aissani committed Apr 2, 2021
1 parent aa80c18 commit b1392ee
Show file tree
Hide file tree
Showing 8 changed files with 393 additions and 483 deletions.
26 changes: 17 additions & 9 deletions backend/managers/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ const update = (inputContract, modification) => {

const updatedContract = create(modifiedContract);

inputContract.rents
.filter(rent => _isPayment(rent) || rent.discounts.some(discount => discount.origin === 'settlement'))
.forEach(paidRent => {
payTerm(updatedContract, moment(String(paidRent.term), 'YYYYMMDDHH').format('DD/MM/YYYY HH:mm'), {
payments: paidRent.payments,
discounts: paidRent.discounts.filter(discount => discount.origin === 'settlement')
if (inputContract.rents) {
inputContract.rents
.filter(rent => _isPayment(rent) || rent.discounts.some(discount => discount.origin === 'settlement'))
.forEach(paidRent => {
payTerm(updatedContract, moment(String(paidRent.term), 'YYYYMMDDHH').format('DD/MM/YYYY HH:mm'), {
payments: paidRent.payments,
discounts: paidRent.discounts.filter(discount => discount.origin === 'settlement')
});
});
});
}

return updatedContract;
};
Expand All @@ -90,6 +92,9 @@ const terminate = (inputContract, termination) => {
};

const payTerm = (contract, term, settlements) => {
if (!contract.rents || !contract.rents.length) {
throw Error('cannot pay term, the rents were not generated');
}
const current = moment(term, 'DD/MM/YYYY HH:mm');
const momentBegin = moment(contract.begin, 'DD/MM/YYYY HH:mm');
const momentEnd = moment(contract.termination || contract.end, 'DD/MM/YYYY HH:mm');
Expand Down Expand Up @@ -122,8 +127,11 @@ const _isPayment = rent => {
};

const _checkLostPayments = (momentBegin, momentEnd, contract) => {
const lostPayments =
contract.rents
if (!contract.rents || !contract.rents.length) {
return;
}

const lostPayments = contract.rents
.filter(rent =>
!moment(rent.term, 'YYYYMMDDHH').isBetween(momentBegin, momentEnd, contract.frequency, '[]') &&
_isPayment(rent))
Expand Down
81 changes: 44 additions & 37 deletions backend/managers/frontdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,15 @@ function toOccupantData(inputOccupant) {
}
);

occupant.contactEmails = occupant.contacts.reduce((acc, {email}) => {
occupant.contactEmails = (occupant.contacts && occupant.contacts.length) ? occupant.contacts.reduce((acc, {email}) => {
if (email) {
return [
...acc,
email.toLowerCase()
];
}
return acc;
}, []);
}, []) : [];

occupant.hasContactEmails = occupant.contactEmails.length > 0;

Expand All @@ -332,44 +332,51 @@ function toOccupantData(inputOccupant) {
occupant.terminated = true;
occupant.status = 'stopped';
}
occupant.office = {
surface: 0,
m2Price: 0,
m2Expense: 0,
price: 0,
expense: 0
};
occupant.parking = {
price: 0,
expense: 0
};
occupant.properties.forEach((item) => {
var property = item.property;
if (property.type === 'parking') {
occupant.parking.price += property.price;
if (property.expense) {
occupant.parking.expense += property.expense;
}
} else {
occupant.office.surface += property.surface;
occupant.office.price += property.price;
if (property.expense) {
occupant.office.expense += property.expense;
if (occupant.properties) {
occupant.office = {
surface: 0,
m2Price: 0,
m2Expense: 0,
price: 0,
expense: 0
};
occupant.parking = {
price: 0,
expense: 0
};
occupant.properties.forEach((item) => {
var property = item.property;
if (property.type === 'parking') {
occupant.parking.price += property.price;
if (property.expense) {
occupant.parking.expense += property.expense;
}
} else {
occupant.office.surface += property.surface;
occupant.office.price += property.price;
if (property.expense) {
occupant.office.expense += property.expense;
}
}
occupant.rental += property.price || 0;
occupant.expenses += property.expense || 0;
});
occupant.preTaxTotal = occupant.rental + occupant.expenses - occupant.discount;
occupant.total = occupant.preTaxTotal;
if (occupant.vatRatio) {
occupant.vat = occupant.preTaxTotal * occupant.vatRatio;
occupant.total = occupant.preTaxTotal + occupant.vat;
}
if (occupant.office) {
occupant.office.m2Price = occupant.office.price / occupant.office.surface;
occupant.office.m2Expense = occupant.office.expense / occupant.office.surface;
}
occupant.rental += property.price || 0;
occupant.expenses += property.expense || 0;
});
occupant.preTaxTotal = occupant.rental + occupant.expenses - occupant.discount;
occupant.total = occupant.preTaxTotal;
if (occupant.vatRatio) {
occupant.vat = occupant.preTaxTotal * occupant.vatRatio;
occupant.total = occupant.preTaxTotal + occupant.vat;
}
if (occupant.office) {
occupant.office.m2Price = occupant.office.price / occupant.office.surface;
occupant.office.m2Expense = occupant.office.expense / occupant.office.surface;
}

occupant.hasPayments = occupant.rents ? occupant.rents.some(
rent => (rent.payments && rent.payments.some(payment => payment.amount > 0)) ||
rent.discounts.some(discount => discount.origin === 'settlement')
) : false;
delete occupant.rents;
return occupant;
}
Expand Down
Loading

0 comments on commit b1392ee

Please sign in to comment.