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

Commit

Permalink
fixed issue on rents when updating contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Camel Aissani committed May 22, 2021
1 parent a164ef6 commit cb1acbc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
27 changes: 18 additions & 9 deletions backend/managers/contract.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const Sugar = require('sugar');
const moment = require('moment');
const BL = require('../businesslogic');

Expand Down Expand Up @@ -60,7 +61,7 @@ const create = (contract) => {
};

const update = (inputContract, modification) => {
const originalContract = JSON.parse(JSON.stringify(inputContract));
const originalContract = Sugar.Object.clone(inputContract, true);
const modifiedContract = {
...originalContract,
...modification,
Expand All @@ -87,11 +88,7 @@ const update = (inputContract, modification) => {

if (inputContract.rents) {
inputContract.rents
.filter(
(rent) =>
_isPayment(rent) ||
rent.discounts.some((discount) => discount.origin === 'settlement')
)
.filter((rent) => _isPayment(rent))
.forEach((paidRent) => {
payTerm(
updatedContract,
Expand All @@ -100,9 +97,14 @@ const update = (inputContract, modification) => {
),
{
payments: paidRent.payments,
vats: paidRent.vats.filter((vat) => vat.origin === 'settlement'),
discounts: paidRent.discounts.filter(
(discount) => discount.origin === 'settlement'
),
debts: paidRent.debts.filter(
(debt) => debt.amount && debt.amount > 0
),
description: paidRent.description,
}
);
});
Expand Down Expand Up @@ -175,9 +177,16 @@ const payTerm = (contract, term, settlements) => {
};

const _isPayment = (rent) => {
return !!(
rent.payments.length > 0 &&
rent.payments.some((payment) => payment.amount && payment.amount > 0)
return (
rent.payments.some((payment) => payment.amount && payment.amount > 0) ||
rent.discounts.some(
(discount) =>
discount.origin === 'settlement' &&
discount.amount &&
discount.amount > 0
) ||
rent.debts.some((debt) => debt.amount && debt.amount > 0) ||
!!rent.description
);
};

Expand Down
5 changes: 1 addition & 4 deletions backend/managers/occupantmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,9 @@ function remove(req, res) {
);
});
if (occupantsWithPaidRents.length > 0) {
// TODO: to localize
return res.status(422).json({
errors: [
'Impossible de supprimer le locataire : ' +
occupantsWithPaidRents[0].name +
'. Des loyers ont été encaissés.',
`impossible to remove ${occupantsWithPaidRents[0].name}. Rents have been recorded.`,
],
});
}
Expand Down
2 changes: 1 addition & 1 deletion backend/models/occupant.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class OccupantModel extends Model {
frequency: String,
terminationDate: String,
guarantyPayback: Number,
properties: Array, // [{ propertyId, property: { ... }, entryDate, exitDate, rent, expenses: [title, amount] }]
properties: Array, // [{ propertyId, property: { ... }, entryDate, exitDate, rent, expenses: [{title, amount}] }]
guaranty: Number,
reference: String,
isVat: Boolean,
Expand Down
32 changes: 32 additions & 0 deletions test/managers/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,38 @@ describe('contract functionalities', () => {
);
});

it('update contract which has a payment', () => {
const contract = Contract.create({
begin: '01/01/2017 00:00',
end: '31/12/2025 23:59',
frequency: 'months',
properties: [{}, {}],
});
Contract.payTerm(contract, '01/01/2017 00:00', {
payments: [{ amount: 200 }],
debts: [{ description: 'extra', amount: 100 }],
discounts: [{ origin: 'settlement', description: '', amount: 100 }],
});
const newContract = Contract.update(contract, { end: '31/03/2026 23:59' });

assert.strictEqual(newContract.terms, 108 + 3, 'incorrect number of terms');
assert.strictEqual(
newContract.rents.length,
108 + 3,
'incorrect number of rents'
);

assert.strictEqual(
contract.rents.filter((rent) => rent.payments.length !== 0).length,
1
);

const firstRent = contract.rents.find((rent) => rent.term === 2017010100);
assert.strictEqual(firstRent.payments[0].amount, 200);
assert.strictEqual(firstRent.debts[0].amount, 100);
assert.strictEqual(firstRent.discounts[0].amount, 100);
});

it('terminate contract', () => {
const contract = Contract.create({
begin: '01/01/2017 00:00',
Expand Down

0 comments on commit cb1acbc

Please sign in to comment.