diff --git a/app/views/invoices/edit.blade.php b/app/views/invoices/edit.blade.php index 4eb1fe4bbb0b..675b1db67fee 100755 --- a/app/views/invoices/edit.blade.php +++ b/app/views/invoices/edit.blade.php @@ -32,12 +32,11 @@
@endif - {{ Former::select('client')->addOption('', '')->data_bind("dropdown: client") - ->addGroupClass('client_select closer-row') }} + {{ Former::select('client')->addOption('', '')->data_bind("dropdown: client")->addGroupClass('client_select closer-row') }}
- +
@@ -374,7 +373,7 @@ var $input = $('select#client'); $input.combobox().on('change', function(e) { - var clientId = parseInt($('input[name=client]').val(), 10); + var clientId = parseInt($('input[name=client]').val(), 10); if (clientId > 0) { model.loadClient(clientMap[clientId]); } else { @@ -389,7 +388,7 @@ $('.country_select input.form-control').on('change', function(e) { var countryId = parseInt($('input[name=country_id]').val(), 10); - model.client.country_id(countryId); + model.invoice().client().country_id(countryId); }); @@ -411,24 +410,7 @@ $('#taxModal').on('shown.bs.modal', function () { $('#taxModal input:first').focus(); }).on('hidden.bs.modal', function () { - console.log('TAX HIDDEN: %s %s', model.invoice_taxes(), model.invoice_item_taxes()) - /* - var blank = model.getBlankTaxRate(); - if (!model.invoice_taxes()) { - model.tax(blank); - } - if (!model.invoice_item_taxes()) { - for (var i=0; i button:first').click(function() { @@ -547,8 +529,6 @@ } function formEnterClick(event) { - console.log('form enter click'); - if (event.keyCode === 13){ if (event.target.type == 'textarea') { return; @@ -560,7 +540,6 @@ } function clientModalEnterClick(event) { - console.log('client form enter click'); if (event.keyCode === 13){ event.preventDefault(); model.clientFormComplete(); @@ -569,7 +548,6 @@ } function taxModalEnterClick(event) { - console.log('tax form enter click'); if (event.keyCode === 13){ event.preventDefault(); model.taxFormComplete(); @@ -577,20 +555,19 @@ } } - function ViewModel() { + function ViewModel(data) { var self = this; - self.invoice = new InvoiceModel(); + //self.invoice = data ? false : new InvoiceModel(); + self.invoice = ko.observable(data ? false : new InvoiceModel()); self.tax_rates = ko.observableArray(); self.loadClient = function(client) { - //console.log(client); - ko.mapping.fromJS(client, model.invoice.client.mapping, model.invoice.client); + ko.mapping.fromJS(client, model.invoice().client().mapping, model.invoice().client); } self.invoice_taxes = ko.observable({{ Auth::user()->account->invoice_taxes ? 'true' : 'false' }}); self.invoice_item_taxes = ko.observable({{ Auth::user()->account->invoice_item_taxes ? 'true' : 'false' }}); - - /* + self.mapping = { 'invoice': { create: function(options) { @@ -602,14 +579,17 @@ return new TaxRateModel(options.data); } }, + } + + if (data) { + ko.mapping.fromJS(data, self.mapping, self); } - */ self.invoice_taxes.show = ko.computed(function() { if (self.tax_rates().length > 2 && self.invoice_taxes()) { return true; } - if (self.invoice.tax_rate() > 0) { + if (self.invoice().tax_rate() > 0) { return true; } return false; @@ -619,8 +599,8 @@ if (self.tax_rates().length > 2 && self.invoice_item_taxes()) { return true; } - for (var i=0; i 0) { return true; } @@ -664,7 +644,7 @@ taxRate.name(name); taxRate.rate(parseFloat(rate)); taxRate.is_deleted(true); - model.tax_rates.push(taxRate); + self.tax_rates.push(taxRate); return taxRate; } @@ -680,7 +660,7 @@ } self.showClientForm = function() { - self.clientBackup = ko.mapping.toJS(self.invoice.client); + self.clientBackup = ko.mapping.toJS(self.invoice().client); $('#emailError').css( "display", "none" ); $('#clientModal').modal('show'); @@ -696,8 +676,8 @@ return; } - if (self.invoice.client.public_id() == 0) { - self.invoice.client.public_id(-1); + if (self.invoice().client().public_id() == 0) { + self.invoice().client().public_id(-1); } if (name) { @@ -724,7 +704,7 @@ function InvoiceModel(data) { var self = this; - this.client = new ClientModel(); + this.client = ko.observable(data ? false : new ClientModel()); this.id = ko.observable(''); self.discount = ko.observable(''); self.frequency_id = ko.observable(''); @@ -745,6 +725,11 @@ self.mapping = { + 'client': { + create: function(options) { + return new ClientModel(options.data); + } + }, 'invoice_items': { create: function(options) { return new ItemModel(options.data); @@ -757,6 +742,18 @@ }, } + self.addItem = function() { + var itemModel = new ItemModel(); + self.invoice_items.push(itemModel); + applyComboboxListeners(); + } + + if (data) { + ko.mapping.fromJS(data, self.mapping, self); + } else { + self.addItem(); + } + self._tax = ko.observable(); this.tax = ko.computed({ read: function () { @@ -764,8 +761,6 @@ }, write: function(value) { if (value) { - console.log("WRITE INVOICE TAX"); - console.log(value.name()); self._tax(value); self.tax_name(value.name()); self.tax_rate(value.rate()); @@ -805,15 +800,15 @@ }); self.client.linkText = ko.computed(function() { - return self.client.public_id() ? 'Edit client details' : 'Create new client'; + return self.client().public_id() ? 'Edit client details' : 'Create new client'; }); self.enable = {}; self.enable.save = ko.computed(function() { var isValid = false; - for (var i=0; i= 0; - } + } @endif - */ - if (!model.invoice.discount()) model.invoice.discount(''); - - @endif - @if (!$data) + model.invoice().addItem(); model.addTaxRate(); - model.invoice.addItem(); @endif - ko.applyBindings(model); + + model.invoice().tax(model.getTaxRate(model.invoice().tax_name(), model.invoice().tax_rate())); + for (var i=0; i diff --git a/public/js/script.js b/public/js/script.js index 4b450c6e7570..b579c919c57f 100755 --- a/public/js/script.js +++ b/public/js/script.js @@ -638,7 +638,7 @@ ko.bindingHandlers.dropdown = { var value = ko.utils.unwrapObservable(valueAccessor()); var id = (value && value.public_id) ? value.public_id() : (value && value.id) ? value.id() : value ? value : false; if (id) $(element).val(id); - console.log("combo-init: %s", id); + //console.log("combo-init: %s", id); $(element).combobox(options); /* @@ -653,7 +653,7 @@ ko.bindingHandlers.dropdown = { update: function (element, valueAccessor) { var value = ko.utils.unwrapObservable(valueAccessor()); var id = (value && value.public_id) ? value.public_id() : (value && value.id) ? value.id() : value ? value : false; - console.log("combo-update: %s", id); + //console.log("combo-update: %s", id); if (id) { $(element).val(id); $(element).combobox('refresh');