diff --git a/client/src/i18n/en/form.json b/client/src/i18n/en/form.json index dc19a8a03d..f91521005e 100644 --- a/client/src/i18n/en/form.json +++ b/client/src/i18n/en/form.json @@ -403,7 +403,7 @@ "ERROR_404": "Error 404", "EXCHANGE_RATE": "Exchange Rate", "EXPENSE": "Expenses", - "EXTRANCT_EMPLOYEE_STANDINGS": "Extract from Employee Standing of period", + "EXTRACT_EMPLOYEE_STANDING": "Extract of the Employee Standing by Period", "FAMILY_ALLOWANCES": "Family allowances", "FATHER_NAME": "Father's Name", "FAX": "Fax", diff --git a/client/src/i18n/en/table.json b/client/src/i18n/en/table.json index 8c31dec192..4606ea84e6 100644 --- a/client/src/i18n/en/table.json +++ b/client/src/i18n/en/table.json @@ -18,6 +18,7 @@ "AUXILIARY_CENTER": "Auxiliary Center", "AUXILIARY_CENTERS" : "Auxiliary Centers", "BALANCE": "Balance", + "BALANCE_AT": "Balance at", "BALANCE_SECTION": "Balance Sheet Section", "BASIC_SALARY": "Basic Salary", "BEFORE": "Before", @@ -103,7 +104,7 @@ "EXCHANGE_RATE": "Exchange Rate", "EXPIRATION_DATE": "Expiration Date", "EXPIRE_IN":"Expire In", - "EXTRACT_EMPLOYEE_STANDINGS": "View extract of the employee's standings by date range", + "EXTRACT_EMPLOYEE_STANDING": "View extract of the employee's standing by date range", "FIXED_CHARGES": "Fixed costs", "NUM_PIECE": "Num Doc", "ENTRY": "Entry", diff --git a/client/src/i18n/fr/form.json b/client/src/i18n/fr/form.json index 452f29069f..330435f67c 100644 --- a/client/src/i18n/fr/form.json +++ b/client/src/i18n/fr/form.json @@ -406,7 +406,7 @@ "ERROR_404": "Erreur 404", "EXCHANGE_RATE": "Taux d'echange", "EXPENSE": "Charges", - "EXTRANCT_EMPLOYEE_STANDINGS": "Extrait de la situation financière de l'employée pour la période", + "EXTRANCT_EMPLOYEE_STANDING": "Extrait de la situation financière de l'employée pour la période", "FATHER_NAME": "Nom du Père", "FAX": "Fax", "FEE_CENTER": "Centre de frais", diff --git a/client/src/i18n/fr/table.json b/client/src/i18n/fr/table.json index 2c607f462a..b5e2cfa80a 100644 --- a/client/src/i18n/fr/table.json +++ b/client/src/i18n/fr/table.json @@ -18,6 +18,7 @@ "AUXILIARY_CENTER": "Centre auxiliaire", "AUXILIARY_CENTERS": "Centres Auxiliaires", "BALANCE": "Balance", + "BALANCE_AT": "Solde au", "BASIC_SALARY": "Salaire de base", "BALANCE_SECTION": "Section du Bilan", "BEFORE": "Avant", @@ -103,7 +104,7 @@ "EXCHANGE_RATE": "Taux de change", "EXPIRATION_DATE": "Date d'expiration", "EXPIRE_IN" : "Expire Dans", - "EXTRACT_EMPLOYEE_STANDINGS": "Voir l'extrait de la situation financière de l'employé par rapport à une plage de dates", + "EXTRACT_EMPLOYEE_STANDING": "Voir l'extrait de la situation financière de l'employé par rapport à une plage de dates", "NUM_PIECE": "N° Piece", "ENTRY": "Entrée", "EXIT": "Sortie", diff --git a/client/src/modules/reports/generate/employeeStanding/employeeStanding.html b/client/src/modules/reports/generate/employeeStanding/employeeStanding.html index 4fdc5c327f..9f9a36a7bc 100644 --- a/client/src/modules/reports/generate/employeeStanding/employeeStanding.html +++ b/client/src/modules/reports/generate/employeeStanding/employeeStanding.html @@ -34,7 +34,7 @@

REPORT.EMPLOYEE_STANDING.TITLE

diff --git a/server/controllers/finance/creditors.js b/server/controllers/finance/creditors.js index 5d94d9aa7b..2e852a01a3 100644 --- a/server/controllers/finance/creditors.js +++ b/server/controllers/finance/creditors.js @@ -99,6 +99,33 @@ function balance(creditorUuid) { return db.exec(sql, [creditorUid, creditorUid]); } + +/** + * This function returns the Opening balance of a creditor account with the hospital + * until a date from + * + * @method openingBalanceCreditor + */ +function openingBalanceCreditor(creditorUuid, dateFrom) { + const creditorUid = db.bid(creditorUuid); + + const sql = ` + SELECT IFNULL(SUM(ledger.debit_equiv), 0) AS debit, IFNULL(SUM(ledger.credit_equiv), 0) AS credit, + IFNULL(SUM(ledger.credit_equiv - ledger.debit_equiv), 0) AS balance, MIN(trans_date) AS since, + MAX(trans_date) AS until + FROM ( + SELECT debit_equiv, credit_equiv, entity_uuid, trans_date FROM posting_journal WHERE entity_uuid = ? + AND DATE(trans_date) < DATE(?) + UNION ALL + SELECT debit_equiv, credit_equiv, entity_uuid, trans_date FROM general_ledger WHERE entity_uuid = ? + AND DATE(trans_date) < DATE(?) + ) AS ledger + GROUP BY ledger.entity_uuid; + `; + + return db.exec(sql, [creditorUid, dateFrom, creditorUid, dateFrom]); +} + /** * @function getFinancialActivity * @@ -116,7 +143,7 @@ function getFinancialActivity(creditorUuid, dateFrom, dateTo) { filterBydatePosting = ` AND (DATE(p.trans_date) >= DATE('${transDateFrom}') AND DATE(p.trans_date) <= DATE('${transDateTo}'))`; - filterBydateLegder = ` AND (DATE(g.trans_date) >= DATE('${transDateFrom}') + filterBydateLegder = ` AND (DATE(g.trans_date) >= DATE('${transDateFrom}') AND DATE(g.trans_date) <= DATE('${transDateTo}'))`; } @@ -146,16 +173,17 @@ function getFinancialActivity(creditorUuid, dateFrom, dateTo) { ORDER BY trans_date ASC, trans_id; `; - return q.all([ - db.exec(sql, [uid, uid]), - balance(creditorUuid), - ]) - .spread((transactions, aggs) => { + const tabSql = (dateFrom && dateTo) ? + [db.exec(sql, [uid, uid]), balance(creditorUuid), openingBalanceCreditor(creditorUuid, dateFrom)] : + [db.exec(sql, [uid, uid]), balance(creditorUuid)]; + + return q.all(tabSql) + .spread((transactions, aggs, openingBalance) => { if (!aggs.length) { - aggs.push({ debit : 0, credit : 0, balance : 0 }); + aggs.push({ debit: 0, credit: 0, balance: 0 }); } const [aggregates] = aggs; - return { transactions, aggregates }; + return { transactions, aggregates, openingBalance }; }); } diff --git a/server/controllers/finance/reports/financial.employee.handlebars b/server/controllers/finance/reports/financial.employee.handlebars index 9115bb4736..0bb258fdf0 100644 --- a/server/controllers/finance/reports/financial.employee.handlebars +++ b/server/controllers/finance/reports/financial.employee.handlebars @@ -9,18 +9,18 @@ {{translate "REPORT.EMPLOYEE_STANDING.REPORT"}} -

{{translate "FORM.LABELS.EMPLOYEE_NAME"}} : {{ employee.display_name }}

-

{{translate "FORM.LABELS.REFERENCE"}} : {{ employee.reference }}

-
{{translate "FORM.LABELS.REGISTRATION_NUMBER"}} : {{ employee.code }}
+

{{translate "FORM.LABELS.EMPLOYEE_NAME"}} : {{ employee.display_name }}

+

{{translate "FORM.LABELS.REFERENCE"}} : {{ employee.reference }}

+
{{translate "FORM.LABELS.REGISTRATION_NUMBER"}} : {{ employee.code }}

- {{translate balanceCreditorText}} == {{debcred ./creditorAggregates.balance metadata.enterprise.currency_id}} + {{translate balanceCreditorText}} = {{debcred ./creditorAggregates.balance metadata.enterprise.currency_id}}

{{#if includeMedicalCare }}
- {{translate "TABLE.COLUMNS.TOTAL_MEDICAL_CARE_EMPLOYEE"}} == {{debcred ./debtorAggregates.balance metadata.enterprise.currency_id}} + {{translate "TABLE.COLUMNS.TOTAL_MEDICAL_CARE_EMPLOYEE"}} = {{debcred ./debtorAggregates.balance metadata.enterprise.currency_id}}
{{/if}} @@ -43,6 +43,21 @@ + {{#if extractEmployee}} + + {{translate "TABLE.COLUMNS.BALANCE_AT" }} {{date dates.dateFrom }} + + {{debcred creditoropeningBalance.debit metadata.enterprise.currency_id}} + + + {{debcred creditoropeningBalance.credit metadata.enterprise.currency_id}} + + + {{debcred creditoropeningBalance.balance metadata.enterprise.currency_id}} + + + {{/if}} + {{#each creditorTransactions}} {{date this.trans_date}} @@ -80,14 +95,12 @@ {{/if}} {{#if extractEmployee}} - {{translate "FORM.LABELS.EXTRANCT_EMPLOYEE_STANDINGS" }} ({{date dates.dateFrom }} - {{date dates.dateTo }}) + {{translate "FORM.LABELS.EXTRACT_EMPLOYEE_STANDING" }} ({{date dates.dateFrom }} - {{date dates.dateTo }}) - {{translate extratCreditorText }} : {{debcred lastCum.cumsum metadata.enterprise.currency_id}} + {{translate extratCreditorText }} : {{debcred lastTransaction.cumsum metadata.enterprise.currency_id}} {{/if}} - - {{#if includeMedicalCare }} diff --git a/server/controllers/finance/reports/financial.employee.js b/server/controllers/finance/reports/financial.employee.js index 527d0fe173..bf029f2cc5 100644 --- a/server/controllers/finance/reports/financial.employee.js +++ b/server/controllers/finance/reports/financial.employee.js @@ -18,7 +18,7 @@ const db = require('../../../lib/db'); const TEMPLATE = './server/controllers/finance/reports/financial.employee.handlebars'; const PDF_OPTIONS = { - filename : 'FORM.LABELS.FINANCIAL_STATUS', + filename: 'FORM.LABELS.FINANCIAL_STATUS', }; /** @@ -70,29 +70,31 @@ async function build(req, res, next) { _.extend(data, { employee, - creditorTransactions : creditorOperations.transactions, - creditorAggregates : creditorOperations.aggregates, - debtorTransactions : debtorOperations.transactions, - debtorAggregates : debtorOperations.aggregates, + creditorTransactions: creditorOperations.transactions, + creditorAggregates: creditorOperations.aggregates, + debtorTransactions: debtorOperations.transactions, + debtorAggregates: debtorOperations.aggregates, }); + if (creditorOperations.openingBalance) { + _.extend(data, { + creditoropeningBalance: creditorOperations.openingBalance[0], + }); + } + // provides the latest element of the table, // as the request is ordered by date, the last line item will // also be the employee's balance for the search period if (options.extractEmployee) { - if (creditorOperations.transactions.length) { - data.lastCum = creditorOperations.transactions[creditorOperations.transactions.length - 1]; - data.extratCreditorText = data.lastCum.cumsum >= 0 - ? 'FORM.LABELS.CREDIT_BALANCE' : 'FORM.LABELS.DEBIT_BALANCE'; - } else { - data.lastCum = { - cumsum : 0, - }; - } + + const lastTxn = _.last(creditorOperations.transactions); + data.lastTransaction = lastTxn || { cumsum: 0 }; + data.extratCreditorText = data.lastTransaction.cumsum >= 0 + ? 'FORM.LABELS.CREDIT_BALANCE' : 'FORM.LABELS.DEBIT_BALANCE'; data.dates = { - dateFrom : options.dateFrom, - dateTo : options.dateTo, + dateFrom: options.dateFrom, + dateTo: options.dateTo, }; }