diff --git a/brewman/brewman/static/scripts/angular_filter.js b/brewman/brewman/static/scripts/angular_filter.js index 9d534ec6..aba5cfa2 100644 --- a/brewman/brewman/static/scripts/angular_filter.js +++ b/brewman/brewman/static/scripts/angular_filter.js @@ -17,3 +17,15 @@ overlord_filter.filter('posted', function () { return input == true ? 'Posted' : 'Post'; }; }); + +overlord_filter.filter('journalDebit', function () { + return function (input, debit) { + var list = []; + for (var i = 0, l = input.length; i < l; i++) { + if (input[i].Debit === debit) { + list.push(input[i]); + } + } + return list; + }; +}); diff --git a/brewman/brewman/static/scripts/journal.js b/brewman/brewman/static/scripts/journal.js index 7651169b..4cee69ec 100644 --- a/brewman/brewman/static/scripts/journal.js +++ b/brewman/brewman/static/scripts/journal.js @@ -15,18 +15,27 @@ } } if (i >= l) { - this.voucher.Journals.push({Debit:$scope.debit, Amount:$scope.amount, Ledger:{LedgerID:$scope.id, Name:$scope.name}}); + this.voucher.Journals.push({Debit:$scope.debit, Amount:Number($scope.amount), Ledger:{LedgerID:$scope.id, Name:$scope.name}}); } delete $scope.amount; delete $scope.id; delete $scope.name; $('#txtLedger').focus(); }; + $scope.removeJournal = function (journal) { var index = this.voucher.Journals.indexOf(journal); this.voucher.Journals.splice(index, 1); }; + $scope.$watch('voucher.Journals', function (journals, oldValue) { + var amount = 0; + for (var i = 0, l = journals.length; i < l; i++) { + amount += (Number(journals[i].Amount) * parseInt(journals[i].Debit)); + } + $scope.amount = Math.abs(amount); + }, true); + $scope.save = function () { save_voucher($scope); }; diff --git a/brewman/brewman/static/scripts/payment.js b/brewman/brewman/static/scripts/payment.js index a3a47fca..d639c8da 100644 --- a/brewman/brewman/static/scripts/payment.js +++ b/brewman/brewman/static/scripts/payment.js @@ -1,184 +1,54 @@ -var Journal = Backbone.Model.extend({ - url:add_journal_url -}); - -var Journals = Backbone.Collection.extend({ - model:Journal -}); - -var DropDownInput = Backbone.View.extend({ - events:{ - 'change #ddlLedger':'ddlLedgerChanged' - }, - initialize:function () { - this.collection.on('add change remove', this.updateDdl, this); - }, - ddlLedgerChanged:function (e) { - var list = this.collection.where({Debit:-1}); - if (list.length != 1) { - showSimpleError("Too many or too little credit journals"); - } - var ddl = list[0]; - var ledger = ddl.get("Ledger"); - ledger.LedgerID = $('#ddlLedger').val(); - ddl.set("Ledger", ledger); - }, - updateDdl:function (status) { - var row = status.toJSON(); - if (row.Debit == -1) { - $('#ddlLedger').val(row.Ledger.LedgerID); - } else { - var totalAmount = 0; - $.each(this.collection.where({Debit:1}), function (index, item) { - totalAmount += item.get('Amount'); - }); - - var list = this.collection.where({Debit:-1}); - if (list.length != 1) { - showSimpleError("Too many or too little credit journals"); +function PaymentCtrl($scope, save_voucher, delete_voucher, post_voucher) { + $scope.addJournal = function () { + for (var i = 0, l = this.voucher.Journals.length; i < l; i++) { + if (this.voucher.Journals[i].Ledger.LedgerID === $scope.id) { + var amount = (this.voucher.Journals[i].Debit * this.voucher.Journals[i].Amount) + (parseInt($scope.debit) * Number($scope.amount)); + if (amount < 0) { + this.voucher.Journals[i].Debit = -1; + this.voucher.Journals[i].Amount = amount * -1; + } else { + this.voucher.Journals[i].Debit = 1; + this.voucher.Journals[i].Amount = amount; + } + break; } - var ddl = list[0]; - ddl.set("Amount", totalAmount); } - } -}); - -var GetFromInput = Backbone.View.extend({ - events:{ - 'click #btnAdd':'addJournal', - 'keypress #txtAmount':'addOnEnter' - }, - - initialize:function () { - this.collection.on('add', this.clearInput, this); - this.collection.on('change', this.clearInput, this); - }, - - addJournal:function (e) { - e.preventDefault(); - var journal = new Journal(); - journal.save({ - debit:1, - ledgerString:this.$('#txtLedger').val(), - amount:this.$('#txtAmount').val() }, - { - wait:true, - success:$.proxy(function (model, response) { - var newModel = model.toJSON(); - var found = null; - this.collection.each(function (item) { - var itemJson = item.toJSON(); - if (itemJson.Ledger.LedgerID == newModel.Ledger.LedgerID) { - found = item; - } - }); - if (found == null) { - this.collection.add(model); - } else { - var foundJson = found.toJSON(); - var amount = (foundJson.Debit * foundJson.Amount) + (newModel.Debit * newModel.Amount); - if (amount < 0) { - found.set({Debit:-1, Amount:amount * -1}); - } else { - found.set({Debit:1, Amount:amount}); - } - } - }, this)}); - }, - addOnEnter:function (e) { - if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) { - e.preventDefault(); - this.addJournal(e); + if (i >= l) { + this.voucher.Journals.push({Debit:1, Amount:Number($scope.amount), Ledger:{LedgerID:$scope.id, Name:$scope.name}}); } - }, - clearInput:function () { - this.$('#txtLedger').val(""); - this.$('#txtAmount').val(""); - this.$('#txtLedger').focus(); - } -}); + delete $scope.amount; + delete $scope.id; + delete $scope.name; + $('#txtLedger').focus(); + }; + $scope.removeJournal = function (journal) { + var index = this.voucher.Journals.indexOf(journal); + this.voucher.Journals.splice(index, 1); + }; -var ShowTable = Backbone.View.extend({ - events:{ - 'click .Journal button':'removeJournal' - }, - initialize:function () { - this.collection.on('add', this.addRow, this); - this.collection.on('remove', this.removeRow, this); - this.collection.on('change', this.changeRow, this); - this.collection.on('reset', this.resetRows, this); - }, - - removeJournal:function (ev) { - ev.preventDefault(); - var cid = $(ev.target).closest("tr").attr("id"); - this.collection.remove(this.collection.getByCid(cid)) - }, - - removeRow:function (options) { - $('.Journal#' + options.cid).remove(); - }, - addRow:function (status) { - var row = status.toJSON(); - row.cid = status.cid; - if (row.Debit == 1) { - var rowString = rowTemplate.render(row); - $(this.el).append(rowString); - } - }, - changeRow:function (status) { - var row = status.toJSON(); - row.cid = status.cid; - if (row.Debit == 1) { - var rowString = rowTemplate.render(row); - $('.Journal#' + status.cid).replaceWith(rowString); - } - }, - resetRows:function (status) { - $(this.el).empty(); - var rows; - this.collection.each(function (item) { - var row = item.toJSON(); - row.cid = status.cid; - if (row.Debit == 1) { - var rowString = rowTemplate.render(row); - rows += rowString; + $scope.$watch('voucher.Journals', function (journals, oldValue) { + var amount = 0; + for (var i = 0, l = journals.length; i < l; i++) { + if (journals[i].Debit === -1) { + var creditJournal = journals[i]; + } else { + amount += Number(journals[i].Amount); } - }); - $(this.el).append(rows); - } -}); + } + if (creditJournal.Amount !== amount) { + creditJournal.Amount = amount; + } + }, true); -var SyncJournals = Backbone.View.extend({ - initialize:function () { - this.collection.on('add remove change', this.updateVoucher, this); - this.model.on('destroy', this.updateJournals, this); - }, - updateVoucher:function (status) { - this.model.set('Journals', this.collection.toJSON()); - }, - updateJournals:function (status) { - this.collection.reset(); - this.collection.add(default_journal); - this.model.set('Journals', this.collection.toJSON()); - } -}); + $scope.save = function () { + save_voucher($scope); + }; -var ShowFooter = Backbone.View.extend({ - initialize:function () { - this.collection.on('add remove change', this.footerShow, this); - }, - footerShow:function (status) { - var totalAmount = 0; - $.each(this.collection.where({Debit:1}), function (index, item) { - totalAmount += parseInt(item.get('Amount')); - }); + $scope.delete = function () { + delete_voucher($scope); + }; - var rowString = '