From c781acfa35f8c6fbbf284034163a47d5d5e1064e Mon Sep 17 00:00:00 2001 From: "Kyle E. Aungst" Date: Tue, 28 Jun 2016 22:40:15 -0700 Subject: [PATCH] Fixes Issue 467 A `payment` model is created when hitting the `Add Payment` button on invoices. This model is invalid, but it's creation sets the `paidTotal` to 0. This set's `status` to 'Paid' for invoices requiring 0 payment. - request adds additional logic to where the `status` field is set - keeps track of last unpaid status if it needs to return to it (e.g. return to either Bill or Draft) - fixes missing checks for invalid payments when calculating remaining balance - implements cleanup function to destroy payment model placeholder upon modal exit with invalid model state. --- app/invoices/payment/controller.js | 12 ++++++++++++ app/invoices/payment/template.hbs | 1 + app/models/invoice.js | 18 ++++++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/invoices/payment/controller.js b/app/invoices/payment/controller.js index 2504fcc3f8..cfcfbb04ff 100644 --- a/app/invoices/payment/controller.js +++ b/app/invoices/payment/controller.js @@ -3,11 +3,23 @@ import Ember from 'ember'; import PatientSubmodule from 'hospitalrun/mixins/patient-submodule'; export default AbstractEditController.extend(PatientSubmodule, { + closeModalAction: 'cleanup', cancelAction: 'closeModal', findPatientVisits: false, invoiceController: Ember.inject.controller('invoices'), newPayment: false, + actions: { + cleanup: function() { + var model = this.model; + this.model.validate().then(function() { + model.save(); + }).catch(function() { + model.destroyRecord(); + }); + } + }, + expenseAccountList: Ember.computed.alias('invoiceController.expenseAccountList'), patientList: Ember.computed.alias('invoiceController.patientList'), diff --git a/app/invoices/payment/template.hbs b/app/invoices/payment/template.hbs index 082544d707..d2699aec0f 100644 --- a/app/invoices/payment/template.hbs +++ b/app/invoices/payment/template.hbs @@ -1,4 +1,5 @@ {{#modal-dialog + closeModalAction=closeModalAction isUpdateDisabled=isUpdateDisabled title=title updateButtonAction=updateButtonAction diff --git a/app/models/invoice.js b/app/models/invoice.js index 324cf79e51..a9583923f5 100644 --- a/app/models/invoice.js +++ b/app/models/invoice.js @@ -14,6 +14,7 @@ export default AbstractModel.extend(DateFormat, NumberFormat, { visit: DS.belongsTo('visit', { async: false }), + lastUnpaidStatus: DS.attr('string'), status: DS.attr('string'), remarks: DS.attr('string'), billDate: DS.attr('date'), @@ -108,14 +109,23 @@ export default AbstractModel.extend(DateFormat, NumberFormat, { patientResponsibility: Ember.computed.sum('patientResponsibilityTotals'), paymentAmountChanged: function() { - var payments = this.get('payments'), - paidTotal = payments.reduce(function(previousValue, payment) { + var payments = this.get('payments'); + var hasValidPayments = false; + var paidTotal = payments.reduce(function(previousValue, payment) { + if (payment.get('isValid')) { + hasValidPayments = true; return previousValue += this._getValidNumber(payment.get('amount')); - }.bind(this), 0); + } + }.bind(this), 0); this.set('paidTotal', this._numberFormat(paidTotal, true)); var remainingBalance = this.get('remainingBalance'); - if (remainingBalance <= 0) { + if (remainingBalance <= 0 && hasValidPayments) { + if (this.get('status') !== 'Paid') { + this.set('lastUnpaidStatus', this.get('status')); + } this.set('status', 'Paid'); + } else if (this.get('status') == 'Paid') { + this.set('status', this.get('lastUnpaidStatus')); } }.observes('payments.[]', 'payments.@each.amount'),