From e580e6d2da3ce6cdb945c033f3c92cf23ccc47a1 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 23 Aug 2012 22:30:27 +0530 Subject: [PATCH] Journal working fully along with Payment. --- .../brewman/static/scripts/angular_filter.js | 12 + brewman/brewman/static/scripts/journal.js | 11 +- brewman/brewman/static/scripts/payment.js | 222 ++++-------------- .../templates/transaction/journal.mako | 1 - .../templates/transaction/payment.mako | 152 ++++++------ brewman/brewman/views/services/ledger.py | 4 +- brewman/brewman/views/transactions/journal.py | 12 +- brewman/brewman/views/transactions/payment.py | 22 +- 8 files changed, 169 insertions(+), 267 deletions(-) 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 = '' + 'Total' + - '' + totalAmount + - ''; - this.$('#txtLedgerAmount').val(totalAmount); - var $table = this.$('#tfootMain') - $table.html(rowString); - } -}); + $scope.post = function () { + post_voucher($scope); + }; +} diff --git a/brewman/brewman/templates/transaction/journal.mako b/brewman/brewman/templates/transaction/journal.mako index 9119927d..31e8e792 100644 --- a/brewman/brewman/templates/transaction/journal.mako +++ b/brewman/brewman/templates/transaction/journal.mako @@ -9,7 +9,6 @@ ${h.ScriptLink(request, 'payment.js')} - ${h.ScriptLink(request, 'voucher.js')} + <%block name="content"> -
+ ${h.CsrfToken(request.session)} - ${h.HiddenInput('VoucherID')} - ${h.HiddenInput('Posted')}
- ${h.Label('Code', 'txtCode', 'control-label')} -
- ${h.TextInput('txtCode', css_class = 'non-search-box', placeholder='(Auto)', disabled='disabled')} -
-
-
- ${h.Label('Date', 'txtDate', 'control-label')} -
- ${h.TextInput('txtDate', css_class = 'non-search-box', autocomplete='off')} -
-
-
- ${h.Label('From', 'ddlLedger', 'control-label')} -
- - ${h.TextInput('txtLedgerAmount', autocomplete='off', placeholder='Amount')} - ${h.HiddenInput('txtJournalID')} -
-
-
- ${h.Label('Ledger', 'txtLedger', 'control-label')} -
- + -
- - ${h.TextInput('txtAmount', css_class = 'span2', autocomplete='off', placeholder='Amount')} -
- ${h.SubmitButton('btnAdd', 'Add', 'btn btn-success icon-plus-sign')} +
+
- ${h.Label('', 'gvGrid', 'control-label')} + + +
+ +
+
+
+ + +
+ +
+ + +
+
+
+
+ + +
+ +
+ + +
+ +
+
+ +
+
- - + + + + + @@ -101,16 +95,32 @@
- ${h.Label('Narration', 'txtNarration', 'control-label')} + +
- +
- ${h.SubmitButton('btnSave', 'Save', 'btn btn-primary')} - ${h.SubmitButton('btnUpdate', 'Update', 'btn-primary')} - ${h.SubmitButton('btnPost', 'Post', 'btn btn-inverse')} - ${h.SubmitButton('btnDelete', 'Delete', 'btn btn-danger')} + + + + +
+
+ Created on {{voucher.CreationDate}} and Last Edited on {{voucher.LastEditDate}} by {{voucher.User}}. Posted + by {{voucher.Poster}}
\ No newline at end of file diff --git a/brewman/brewman/views/services/ledger.py b/brewman/brewman/views/services/ledger.py index 2f8a3f1c..e8f518d0 100644 --- a/brewman/brewman/views/services/ledger.py +++ b/brewman/brewman/views/services/ledger.py @@ -37,10 +37,10 @@ def ledger_list_by_type(type): list = [] if type is None: for item in LedgerBase.list(): - list.append({'id': str(item.id), 'name': item.name}) + list.append({'LedgerID': str(item.id), 'Name': item.name}) else: for item in LedgerBase.list_by_type(type): - list.append({'id': str(item.id), 'name': item.name}) + list.append({'LedgerID': str(item.id), 'Name': item.name}) return list diff --git a/brewman/brewman/views/transactions/journal.py b/brewman/brewman/views/transactions/journal.py index 6e1aa290..cb692deb 100644 --- a/brewman/brewman/views/transactions/journal.py +++ b/brewman/brewman/views/transactions/journal.py @@ -34,8 +34,8 @@ def journal_get(request): json_voucher = Literal(json.dumps(voucher_info(voucher))) return {'title': 'Hops n Grains - Journal Entry', 'id': id, - 'json_voucher': json_voucher, - 'perms': perms} + 'perms': perms, + 'json_voucher': json_voucher} def voucher_info(voucher): @@ -73,13 +73,11 @@ def journal_post(request): def create_voucher(json, user): dt = datetime.datetime.strptime(json['Date'], '%d-%b-%Y') - voucher = Voucher(date=dt, narration=json['Narration'], user_id=user.id, - type=VoucherType.by_name('Journal'), posted=False) + voucher = Voucher(date=dt, narration=json['Narration'], user_id=user.id, type=VoucherType.by_name('Journal'), + posted=False) for item in json['Journals']: ledger = LedgerBase.by_id(uuid.UUID(item['Ledger']['LedgerID'])) - journal_id = uuid.UUID(item['JournalID']) - if uuid.UUID('00000000-0000-0000-0000-000000000000') == journal_id: - journal_id = None + journal_id = uuid.UUID(item['JournalID']) if 'JournalID' in item else None voucher.journals.append( Journal(id=journal_id, amount=Decimal(item['Amount']), debit=int(item['Debit']), ledger_id=ledger.id, cost_center_id=ledger.costcenter_id)) diff --git a/brewman/brewman/views/transactions/payment.py b/brewman/brewman/views/transactions/payment.py index 972370e9..2c755f52 100644 --- a/brewman/brewman/views/transactions/payment.py +++ b/brewman/brewman/views/transactions/payment.py @@ -26,31 +26,37 @@ from brewman.views.transactions import session_current_date permission='Payment Create') def journal_get(request): id = request.matchdict.get('id', None) + perms = Literal(json.dumps(request.session['perms'])) ledgers = Literal(json.dumps(ledger_list_by_type(1))) - default_journal = Literal( - json.dumps({'Ledger': {'LedgerID': 'ed2341bb-80b8-9649-90db-f9aaca183bb3'}, 'Amount': 0, 'Debit': -1})) if id is None: json_voucher = Literal(json.dumps({'Date': session_current_date(request), 'Posted': True, 'Narration': "", 'Journals': [{'JournalID': '00000000-0000-0000-0000-000000000000', - 'Ledger': {'LedgerID': 'ed2341bb-80b8-9649-90db-f9aaca183bb3'}, + 'Ledger': {'LedgerID': 'ed2341bb-80b8-9649-90db-f9aaca183bb3', + 'Name': 'Cash in Hand'}, 'Amount': 0, 'Debit': -1}]})) else: voucher = Voucher.by_id(uuid.UUID(id)) json_voucher = Literal(json.dumps(voucher_info(voucher))) return {'title': 'Hops n Grains - Payment Entry', 'id': id, + 'perms': perms, 'ledgers': ledgers, - 'default_journal': default_journal, 'json_voucher': json_voucher} + def voucher_info(voucher): json_voucher = {'VoucherID': str(voucher.id), 'Date': voucher.date.strftime('%d-%b-%Y'), 'Posted': voucher.posted, - 'Code': voucher.code, 'Narration': voucher.narration, 'Journals': []} + 'Code': voucher.code, 'Narration': voucher.narration, 'Journals': [], + 'CreationDate': voucher.creation_date.strftime('%d-%b-%Y %H:%M'), + 'LastEditDate': voucher.last_edit_date.strftime('%d-%b-%Y %H:%M'), 'User': voucher.user.name, + 'Poster': voucher.poster.name if voucher.posted else ''} + for item in voucher.journals: json_voucher['Journals'].append({'JournalID': str(item.id), 'Debit': item.debit, 'Amount': str(item.amount), 'Ledger': {'LedgerID': str(item.ledger.id), 'Name': item.ledger.name}}) return json_voucher + @view_config(request_method='POST', route_name='payment_id', renderer='json', xhr=True, permission='Payment Update') @view_config(request_method='POST', route_name='payment', renderer='json', xhr=True, permission='Payment Create') def journal_post(request): @@ -77,9 +83,7 @@ def create_voucher(json, user): type=VoucherType.by_name('Payment'), posted=False) for item in json['Journals']: ledger = LedgerBase.by_id(uuid.UUID(item['Ledger']['LedgerID'])) - journal_id = uuid.UUID(item['JournalID']) - if uuid.UUID('00000000-0000-0000-0000-000000000000') == journal_id: - journal_id = None + journal_id = uuid.UUID(item['JournalID']) if 'JournalID' in item else None voucher.journals.append( Journal(id=journal_id, amount=Decimal(item['Amount']), debit=int(item['Debit']), ledger_id=ledger.id, cost_center_id=ledger.costcenter_id)) @@ -101,7 +105,7 @@ def update_voucher(id, json, user): found = False for i in range(len(newJournals), 0, -1): j = newJournals[i - 1] - if item.id == uuid.UUID(j['JournalID']): + if 'JournalID' in j and item.id == uuid.UUID(j['JournalID']): ledger = LedgerBase.by_id(uuid.UUID(j['Ledger']['LedgerID'])) found = True item.debit = int(j['Debit'])
JournalIDLedgerID Name Amount Delete
{{journal.Ledger.Name}}{{journal.Amount}} + +