<commit_before>import AbstractEditController from 'hospitalrun/controllers/abstract-edit-controller';
import Ember from 'ember';
import NumberFormat from 'hospitalrun/mixins/number-format';
import PatientSubmodule from 'hospitalrun/mixins/patient-submodule';
import PublishStatuses from 'hospitalrun/mixins/publish-statuses';
import SelectValues from 'hospitalrun/utils/select-values';

export default AbstractEditController.extend(NumberFormat, PatientSubmodule, PublishStatuses, {
  invoiceController: Ember.inject.controller('invoices'),
  expenseAccountList: Ember.computed.alias('invoiceController.expenseAccountList.value'),
  patientList: Ember.computed.alias('invoiceController.patientList'),
  pharmacyCharges: [],
  pricingProfiles: Ember.computed.map('invoiceController.pricingProfiles', SelectValues.selectObjectMap),
  supplyCharges: [],
  updateCapability: 'add_invoice',
  wardCharges: [],

  additionalButtons: function() {
    var buttons = [],
      isValid = this.get('model.isValid'),
      status = this.get('model.status');
    if (isValid && status === 'Draft') {
      buttons.push({
        class: 'btn btn-default default',
        buttonAction: 'finalizeInvoice',
        buttonIcon: 'glyphicon glyphicon-ok',
        buttonText: 'Invoice Ready'
      });
    }
    buttons.push({
      class: 'btn btn-default neutral',
      buttonAction: 'printInvoice',
      buttonIcon: 'glyphicon glyphicon-print',
      buttonText: 'Print'
    });
    return buttons;

  }.property('model.isValid', 'model.status'),

  canAddCharge: function() {
    return this.currentUserCan('add_charge');
  }.property(),

  canAddPayment: function() {
    return this.currentUserCan('add_payment');
  }.property(),

  pharmacyExpenseAccount: function() {
    var expenseAccountList = this.get('expenseAccountList');
    if (!Ember.isEmpty(expenseAccountList)) {
      var account = expenseAccountList.find(function(value) {
        if (value.toLowerCase().indexOf('pharmacy') > -1) {
          return true;
        }
      });
      return account;
    }
  }.property('expenseAccountList.value'),

  actions: {
    addItemCharge: function(lineItem) {
      var details = lineItem.get('details');
      var detail = this.store.createRecord('line-item-detail', {
        id: PouchDB.utils.uuid()
      });
      details.addObject(detail);
    },

    addLineItem: function(lineItem) {
      var lineItems = this.get('model.lineItems');
      lineItems.addObject(lineItem);
      this.send('update', true);
      this.send('closeModal');
    },

    deleteCharge: function(deleteInfo) {
      this._deleteObject(deleteInfo.itemToDelete, deleteInfo.deleteFrom);
    },

    deleteLineItem: function(deleteInfo) {
      this._deleteObject(deleteInfo.itemToDelete, this.get('model.lineItems'));
    },

    finalizeInvoice: function() {
      var currentInvoice = this.get('model'),
        invoicePayments = currentInvoice.get('payments'),
        paymentsToSave = [];
      currentInvoice.get('patient.payments').then(function(patientPayments) {
        patientPayments.forEach(function(payment) {
          var invoice = payment.get('invoice');
          if (Ember.isEmpty(invoice)) {
            payment.set('invoice', currentInvoice);
            paymentsToSave.push(payment.save());
            invoicePayments.addObject(payment);
          }
        }.bind(this));
        Ember.RSVP.all(paymentsToSave).then(function() {
          this.set('model.status', 'Billed');
          this.send('update');
        }.bind(this));
      }.bind(this));
    },

    printInvoice: function() {
      this.transitionToRoute('print.invoice', this.get('model'));
    },

    removePayment: function(removeInfo) {
      var payments = this.get('model.payments'),
        payment = removeInfo.itemToRemove;
      payment.set('invoice');
      payments.removeObject(removeInfo.itemToRemove);
      this.send('update', true);
      this.send('closeModal');
    },

    showAddLineItem: function() {
      var newLineItem = this.store.createRecord('billing-line-item', {
        id: PouchDB.utils.uuid()
      });
      this.send('openModal', 'invoices.add-line-item', newLineItem);
    },

    showDeleteItem: function(itemToDelete, deleteFrom) {
      this.send('openModal', 'dialog', Ember.Object.create({
        confirmAction: 'deleteCharge',
        deleteFrom: deleteFrom,
        title: 'Delete Charge',
        message: `Are you sure you want to delete ${itemToDelete.get('name')}?`,
        itemToDelete: itemToDelete,
        updateButtonAction: 'confirm',
        updateButtonText: 'Ok'
      }));
    },

    showDeleteLineItem: function(item) {
      this.send('openModal', 'dialog', Ember.Object.create({
        confirmAction: 'deleteLineItem',
        title: 'Delete Line Item',
        message: `Are you sure you want to delete ${item.get('name')}?`,
        itemToDelete: item,
        updateButtonAction: 'confirm',
        updateButtonText: 'Ok'
      }));
    },

    showRemovePayment: function(payment) {
      var message = 'Are you sure you want to remove this payment from this invoice?',
        model = Ember.Object.create({
          itemToRemove: payment
        }),
        title = 'Remove Payment';
      this.displayConfirm(title, message, 'removePayment', model);
    },

    toggleDetails: function(item) {
      item.toggleProperty('showDetails');
    }
  },

  changePaymentProfile: function() {<commit_msg>Small refactor (#541)<commit_after>import AbstractEditController from 'hospitalrun/controllers/abstract-edit-controller';
import Ember from 'ember';
import NumberFormat from 'hospitalrun/mixins/number-format';
import PatientSubmodule from 'hospitalrun/mixins/patient-submodule';
import PublishStatuses from 'hospitalrun/mixins/publish-statuses';
import SelectValues from 'hospitalrun/utils/select-values';

export default AbstractEditController.extend(NumberFormat, PatientSubmodule, PublishStatuses, {
  invoiceController: Ember.inject.controller('invoices'),
  expenseAccountList: Ember.computed.alias('invoiceController.expenseAccountList.value'),
  patientList: Ember.computed.alias('invoiceController.patientList'),
  pharmacyCharges: [],
  pricingProfiles: Ember.computed.map('invoiceController.pricingProfiles', SelectValues.selectObjectMap),
  supplyCharges: [],
  updateCapability: 'add_invoice',
  wardCharges: [],

  additionalButtons: function() {
    var buttons = [],
      isValid = this.get('model.isValid'),
      status = this.get('model.status');
    if (isValid && status === 'Draft') {
      buttons.push({
        class: 'btn btn-default default',
        buttonAction: 'finalizeInvoice',
        buttonIcon: 'glyphicon glyphicon-ok',
        buttonText: 'Invoice Ready'
      });
    }
    buttons.push({
      class: 'btn btn-default neutral',
      buttonAction: 'printInvoice',
      buttonIcon: 'glyphicon glyphicon-print',
      buttonText: 'Print'
    });
    return buttons;

  }.property('model.isValid', 'model.status'),

  canAddCharge: function() {
    return this.currentUserCan('add_charge');
  }.property(),

  canAddPayment: function() {
    return this.currentUserCan('add_payment');
  }.property(),

  pharmacyExpenseAccount: function() {
    var expenseAccountList = this.get('expenseAccountList');
    if (!Ember.isEmpty(expenseAccountList)) {
      var account = expenseAccountList.find(function(value) {
        if (value.toLowerCase().indexOf('pharmacy') > -1) {
          return true;
        }
      });
      return account;
    }
  }.property('expenseAccountList.value'),

  actions: {
    addItemCharge: function(lineItem) {
      var details = lineItem.get('details');
      var detail = this.store.createRecord('line-item-detail', {
        id: PouchDB.utils.uuid()
      });
      details.addObject(detail);
    },

    addLineItem: function(lineItem) {
      var lineItems = this.get('model.lineItems');
      lineItems.addObject(lineItem);
      this.send('update', true);
      this.send('closeModal');
    },

    deleteCharge: function(deleteInfo) {
      this._deleteObject(deleteInfo.itemToDelete, deleteInfo.deleteFrom);
    },

    deleteLineItem: function(deleteInfo) {
      this._deleteObject(deleteInfo.itemToDelete, this.get('model.lineItems'));
    },

    finalizeInvoice: function() {
      var currentInvoice = this.get('model'),
        invoicePayments = currentInvoice.get('payments'),
        paymentsToSave = [];
      currentInvoice.get('patient.payments').then(function(patientPayments) {
        patientPayments.forEach(function(payment) {
          var invoice = payment.get('invoice');
          if (Ember.isEmpty(invoice)) {
            payment.set('invoice', currentInvoice);
            paymentsToSave.push(payment.save());
            invoicePayments.addObject(payment);
          }
        }.bind(this));
        Ember.RSVP.all(paymentsToSave).then(function() {
          this.set('model.status', 'Billed');
          this.send('update');
        }.bind(this));
      }.bind(this));
    },

    printInvoice: function() {
      this.transitionToRoute('print.invoice', this.get('model'));
    },

    removePayment: function(removeInfo) {
      var payments = this.get('model.payments'),
        payment = removeInfo.itemToRemove;
      payment.set('invoice');
      payments.removeObject(removeInfo.itemToRemove);
      this.send('update', true);
      this.send('closeModal');
    },

    showAddLineItem: function() {
      var newLineItem = this.store.createRecord('billing-line-item', {
        id: PouchDB.utils.uuid()
      });
      this.send('openModal', 'invoices.add-line-item', newLineItem);
    },

    showDeleteItem: function(itemToDelete, deleteFrom) {
      this.showDeleteModal(itemToDelete, Ember.Object.create({
        confirmAction: 'deleteCharge',
        deleteFrom: deleteFrom,
        title: 'Delete Charge'
      }));
    },

    showDeleteLineItem: function(item) {
      this.showDeleteModal(item, Ember.Object.create({
        confirmAction: 'deleteLineItem',
        title: 'Delete Line Item'
      }));
    },

    showDeleteModal(item, options) {
      options = Ember.merge(options, Ember.Object.create({
        message: `Are you sure you want to delete ${item.get('name')}?`,
        itemToDelete: item,
        updateButtonAction: 'confirm',
        updateButtonText: this.get('i18n').t('buttons.ok')
      }));
      this.send('openModal', 'dialog', options);
    },

    showRemovePayment: function(payment) {
      var message = 'Are you sure you want to remove this payment from this invoice?',
        model = Ember.Object.create({
          itemToRemove: payment
        }),
        title = 'Remove Payment';
      this.displayConfirm(title, message, 'removePayment', model);
    },

    toggleDetails: function(item) {
      item.toggleProperty('showDetails');
    }
  },

  changePaymentProfile: function() {
{'fraction_non_alphanumeric': 0.13692831508235945, 'fraction_numerical': 0.0006441520198766909, 'mean_word_length': 3.2536203522504894, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7296403', 'n_tokens_mistral': 3175, 'n_tokens_neox': 3120, 'n_words': 551}