Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Modified invoice model #914

Merged
merged 2 commits into from
Jan 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 45 additions & 55 deletions app/models/invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,69 @@ import Ember from 'ember';
import NumberFormat from 'hospitalrun/mixins/number-format';
import PatientValidation from 'hospitalrun/utils/patient-validation';

const { computed, get, set } = Ember;

export default AbstractModel.extend(DateFormat, NumberFormat, {
// Attributes
billDate: DS.attr('date'),
externalInvoiceNumber: DS.attr('string'),
patient: DS.belongsTo('patient', {
async: false
}),
paidTotal: DS.attr('number'),
patientInfo: DS.attr('string'), // Needed for searching
visit: DS.belongsTo('visit', {
async: false
}),
status: DS.attr('string'),
remarks: DS.attr('string'),
billDate: DS.attr('date'),
paidTotal: DS.attr('number'),
paymentProfile: DS.belongsTo('price-profile', {
async: false
}),
// payments track the number of payment events attached to an invoice.
payments: DS.hasMany('payment', {
async: false
}),
// the individual line items of the invoice
lineItems: DS.hasMany('billing-line-item', {
async: false
}),
status: DS.attr('string'),

// Associations
/* the individual line items of the invoice */
lineItems: DS.hasMany('billing-line-item', { async: false }),
patient: DS.belongsTo('patient', { async: false }),
paymentProfile: DS.belongsTo('price-profile', { async: false }),
/* payments track the number of payment events attached to an invoice. */
payments: DS.hasMany('payment', { async: false }),
visit: DS.belongsTo('visit', { async: false }),

addPayment(payment) {
let payments = this.get('payments');
let payments = get(this, 'payments');
payments.addObject(payment);
this.paymentAmountChanged();
},

billDateAsTime: function() {
return this.dateToTime(this.get('billDate'));
}.property('billDate'),
billDateAsTime: computed('billDate', function() {
return this.dateToTime(get(this, 'billDate'));
}),

discountTotals: Ember.computed.mapBy('lineItemsByCategory', 'discount'),
discount: Ember.computed.sum('discountTotals'),

nationalInsuranceTotals: Ember.computed.mapBy('lineItemsByCategory', 'nationalInsurance'),
nationalInsurance: Ember.computed.sum('nationalInsuranceTotals'),

paidFlag: function() {
return (this.get('status') === 'Paid');
}.property('status'),
paidFlag: computed('status', function() {
return get(this, 'status') === 'Paid';
}),

remainingBalance: function() {
let patientResponsibility = this.get('patientResponsibility');
let paidTotal = this.get('paidTotal');
remainingBalance: computed('patientResponsibility', 'paidTotal', function() {
let patientResponsibility = get(this, 'patientResponsibility');
let paidTotal = get(this, 'paidTotal');
return this._numberFormat((patientResponsibility - paidTotal), true);
}.property('patientResponsibility', 'paidTotal'),
}),

privateInsuranceTotals: Ember.computed.mapBy('lineItemsByCategory', 'privateInsurance'),
privateInsurance: Ember.computed.sum('privateInsuranceTotals'),

lineTotals: Ember.computed.mapBy('lineItems', 'total'),
total: Ember.computed.sum('lineTotals'),

displayInvoiceNumber: function() {
let externalInvoiceNumber = this.get('externalInvoiceNumber');
let id = this.get('id');
if (Ember.isEmpty(externalInvoiceNumber)) {
return id;
} else {
return externalInvoiceNumber;
}
}.property('externalInvoiceNumber', 'id'),
displayInvoiceNumber: computed('externalInvoiceNumber', 'id', function() {
let externalInvoiceNumber = get(this, 'externalInvoiceNumber');
let id = get(this, 'id');
return Ember.isEmpty(externalInvoiceNumber) ? id : externalInvoiceNumber;
}),

lineItemsByCategory: function() {
let lineItems = this.get('lineItems');
lineItemsByCategory: computed('[email protected]', function() {
let lineItems = get(this, 'lineItems');
let byCategory = [];
lineItems.forEach(function(lineItem) {
let category = lineItem.get('category');
let category = get(lineItem, 'category');
let categoryList = byCategory.findBy('category', category);
if (Ember.isEmpty(categoryList)) {
categoryList = {
Expand All @@ -95,42 +86,41 @@ export default AbstractModel.extend(DateFormat, NumberFormat, {
categoryList.total = this._calculateTotal(categoryList.items, 'total');
}.bind(this));
return byCategory;
}.property('[email protected]'),
}),

patientIdChanged: function() {
if (!Ember.isEmpty(this.get('patient'))) {
let patientDisplayName = this.get('patient.displayName');
let patientDisplayId = this.get('patient.displayPatientId');
this.set('patientInfo', `${patientDisplayName} - ${patientDisplayId}`);
if (!Ember.isEmpty(get(this, 'patient'))) {
let patientDisplayName = get(this, 'patient.displayName');
let patientDisplayId = get(this, 'patient.displayPatientId');
set(this, 'patientInfo', `${patientDisplayName} - ${patientDisplayId}`);
}
}.observes('patient.displayName', 'patient.id', 'patient.displayPatientId'),

patientResponsibilityTotals: Ember.computed.mapBy('lineItems', 'amountOwed'),
patientResponsibility: Ember.computed.sum('patientResponsibilityTotals'),

paymentAmountChanged: function() {
let payments = this.get('payments').filter(function(payment) {
return !payment.get('isNew');
let payments = get(this, 'payments').filter(function(payment) {
return !get(payment, 'isNew');
});
if (payments.length === 0) {
return;
}
let paidTotal = payments.reduce(function(previousValue, payment) {
return previousValue += this._getValidNumber(payment.get('amount'));
}.bind(this), 0);
this.set('paidTotal', this._numberFormat(paidTotal, true));
let remainingBalance = this.get('remainingBalance');
set(this, 'paidTotal', this._numberFormat(paidTotal, true));
let remainingBalance = get(this, 'remainingBalance');
if (remainingBalance <= 0) {
this.set('status', 'Paid');
set(this, 'status', 'Paid');
}
}.observes('payments.[]', '[email protected]'),

validations: {
patientTypeAhead: PatientValidation.patientTypeAhead,

patient: {
presence: true
},

visit: {
presence: true
}
Expand Down