2013-01-07 18:42:02 +01:00
|
|
|
function entry(data){
|
|
|
|
var id = ko.utils.unwrapObservable(data.id);
|
|
|
|
var value_date = ko.utils.unwrapObservable(data.value_date);
|
|
|
|
var operation_date = ko.utils.unwrapObservable(data.operation_date);
|
|
|
|
var label = ko.utils.unwrapObservable(data.label);
|
|
|
|
var value = ko.utils.unwrapObservable(data.value);
|
|
|
|
var account = ko.utils.unwrapObservable(data.account);
|
|
|
|
var sold = ko.utils.unwrapObservable(data.sold);
|
|
|
|
var pointedSold = ko.utils.unwrapObservable(data.pointedSold);
|
|
|
|
|
|
|
|
this.id=ko.observable(id ? id : null);
|
|
|
|
this.value_date=ko.observable(value_date ? value_date : null);
|
|
|
|
this.operation_date=ko.observable(operation_date ? operation_date : null);
|
|
|
|
this.label=ko.observable(label ? label : null);
|
|
|
|
this.value=ko.observable(value ? value : null);
|
|
|
|
this.account=ko.observable(account ? account : null);
|
|
|
|
this.sold=ko.observable(sold ? sold : null);
|
|
|
|
this.pointedSold=ko.observable(pointedSold ? pointedSold : null);
|
|
|
|
}
|
|
|
|
|
|
|
|
function message(alertType, title, message) {
|
|
|
|
$(".alert").alert('close');
|
|
|
|
$("#message-placeholder").append('<div class="alert alert-' + alertType + '"><button type="button" class="close" data-dismiss="alert">×</button><h4>' + title + '</h4><strong>' + message + '</strong></div>');
|
|
|
|
}
|
|
|
|
|
|
|
|
var ListViewModel = function() {
|
|
|
|
var self = this;
|
|
|
|
window.viewModel = self;
|
|
|
|
|
|
|
|
self.account = ko.observable();
|
|
|
|
self.accounts = ko.observableArray([]);
|
|
|
|
|
|
|
|
self.entries = ko.observableArray([]);
|
|
|
|
|
|
|
|
self.months = ko.observableArray();
|
|
|
|
self.month = ko.observable();
|
|
|
|
|
|
|
|
self.pointedSold = ko.observable();
|
|
|
|
self.futureSold = ko.observable();
|
|
|
|
self.currentSold = ko.observable();
|
|
|
|
|
|
|
|
self.selectedItem = ko.observable();
|
|
|
|
self.savedItem = ko.observable();
|
|
|
|
|
2013-01-10 13:10:41 +01:00
|
|
|
self.loadEntries = function(account, month) {
|
|
|
|
$.post("api/entry.php", {action: "get_entries", account: account.id, year: month.year, month: month.month}, function(data) {
|
2013-01-07 18:42:02 +01:00
|
|
|
self.entries.removeAll();
|
|
|
|
self.selectedItem(null);
|
|
|
|
|
2013-01-09 23:39:26 +01:00
|
|
|
$.each(data, function(index, element) {
|
2013-01-07 18:42:02 +01:00
|
|
|
self.entries.push(new entry({
|
|
|
|
id: element.id,
|
|
|
|
value_date: new Date(element.value_date),
|
|
|
|
operation_date: element.operation_date ? new Date(element.operation_date) : null,
|
|
|
|
label: element.label,
|
|
|
|
value: element.value,
|
|
|
|
account: element.account_id,
|
|
|
|
sold: element.sold,
|
|
|
|
pointedSold: element.operation_date ? element.pointedsold : ''
|
|
|
|
}));
|
|
|
|
});
|
2013-01-09 23:39:26 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
self.loadAccounts = function() {
|
|
|
|
$.post("api/entry.php", {action: "get_accounts"}).success(function (result) {
|
|
|
|
self.accounts(result);
|
|
|
|
|
|
|
|
if(!self.account()){
|
|
|
|
self.account(result[0]);
|
|
|
|
}
|
2013-01-10 13:10:41 +01:00
|
|
|
|
|
|
|
self.loadMonths(self.account());
|
2013-01-09 23:39:26 +01:00
|
|
|
});
|
|
|
|
};
|
2013-01-07 18:42:02 +01:00
|
|
|
|
2013-01-09 23:39:26 +01:00
|
|
|
self.loadMonths = function(account){
|
|
|
|
$.post("api/entry.php", {action: "get_months", account: account.id}).success(function (result) {
|
|
|
|
self.months(result);
|
|
|
|
|
2013-01-10 13:10:41 +01:00
|
|
|
if(!self.month()) {
|
|
|
|
self.month(result[result.length - 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.loadEntries(self.account(), self.month());
|
2013-01-07 18:42:02 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
self.templateToUse = function (item) {
|
|
|
|
return self.selectedItem() === item ? 'editTmpl' : 'itemsTmpl';
|
|
|
|
};
|
|
|
|
|
|
|
|
self.edit = function(item) {
|
|
|
|
if(self.savedItem() != null) {
|
|
|
|
self.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.savedItem(new entry(item));
|
|
|
|
self.selectedItem(item);
|
|
|
|
|
|
|
|
$("#value_date").datepicker({format: "yyyy-mm-dd"}).on('changeDate', function(ev){
|
|
|
|
self.selectedItem().value_date(new Date(ev.date.valueOf()));
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#operation_date").datepicker({format: "yyyy-mm-dd"}).on('changeDate', function(ev){
|
|
|
|
self.selectedItem().operation_date(new Date(ev.date.valueOf()));
|
|
|
|
});
|
2013-01-08 18:03:25 +01:00
|
|
|
|
|
|
|
$("#operation_date_clear").click(function(e){
|
|
|
|
self.selectedItem().operation_date(null);
|
|
|
|
});
|
2013-01-07 18:42:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
self.cancel = function() {
|
|
|
|
if(self.selectedItem() && self.savedItem()) {
|
|
|
|
self.selectedItem().id(self.savedItem().id());
|
|
|
|
self.selectedItem().operation_date(self.savedItem().operation_date());
|
|
|
|
self.selectedItem().value_date(self.savedItem().value_date());
|
|
|
|
self.selectedItem().label(self.savedItem().label());
|
|
|
|
self.selectedItem().value(self.savedItem().value());
|
|
|
|
self.selectedItem().account(self.savedItem().account());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This item was just added.
|
|
|
|
if(self.selectedItem() && !self.selectedItem().id()) {
|
|
|
|
self.entries.remove(self.selectedItem());
|
|
|
|
}
|
|
|
|
|
|
|
|
self.savedItem(null);
|
|
|
|
self.selectedItem(null);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.add = function() {
|
|
|
|
newEntry = new entry({
|
|
|
|
account: self.account().id
|
|
|
|
});
|
|
|
|
this.entries.unshift(newEntry);
|
|
|
|
self.selectedItem(newEntry);
|
|
|
|
|
|
|
|
$("#value_date").datepicker({format: "yyyy-mm-dd"}).on('changeDate', function(ev){
|
|
|
|
self.selectedItem().value_date(new Date(ev.date.valueOf()));
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#operation_date").datepicker({format: "yyyy-mm-dd"}).on('changeDate', function(ev){
|
|
|
|
self.selectedItem().operation_date(new Date(ev.date.valueOf()));
|
|
|
|
});
|
2013-01-08 18:03:25 +01:00
|
|
|
|
|
|
|
$("#operation_date_clear").click(function(e){
|
|
|
|
self.selectedItem().operation_date(null);
|
|
|
|
});
|
2013-01-07 18:42:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
self.save = function() {
|
|
|
|
var item = new entry(self.selectedItem());
|
|
|
|
|
|
|
|
if(item.value_date()) {
|
|
|
|
item.value_date(dateToString(item.value_date()));
|
|
|
|
}
|
|
|
|
if(item.operation_date()) {
|
|
|
|
item.operation_date(dateToString(item.operation_date()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$.post("api/entry.php", {action: "save_entry", entry:item}).success(function(data) {
|
|
|
|
message("success", "Save", data.message);
|
|
|
|
|
|
|
|
self.selectedItem(null);
|
2013-01-10 13:10:41 +01:00
|
|
|
self.loadAccounts();
|
2013-01-07 18:42:02 +01:00
|
|
|
}).error(function() {
|
|
|
|
message("error", "Error.", "Unexpected error.");
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
self.remove = function (item) {
|
|
|
|
if (item.id()) {
|
|
|
|
if (confirm('Are you sure you wish to delete this item?')) {
|
|
|
|
$.post("api/entry.php", {action: "remove_entry", entry:item}).success(function (result) {
|
|
|
|
self.selectedItem(null);
|
2013-01-09 23:39:26 +01:00
|
|
|
self.loadAccounts();
|
2013-01-07 18:42:02 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.entries.remove(item);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-09 23:39:26 +01:00
|
|
|
self.selectMonth = function(month) {
|
2013-01-10 13:10:41 +01:00
|
|
|
if(month) {
|
|
|
|
self.month(month);
|
|
|
|
self.loadEntries(self.account(), month);
|
|
|
|
}
|
2013-01-08 18:50:47 +01:00
|
|
|
};
|
|
|
|
|
2013-01-09 23:39:26 +01:00
|
|
|
self.selectAccount = function(account) {
|
2013-01-10 13:10:41 +01:00
|
|
|
if(account) {
|
|
|
|
self.account(account);
|
|
|
|
self.loadMonths(account);
|
|
|
|
}
|
2013-01-07 18:42:02 +01:00
|
|
|
};
|
|
|
|
|
2013-01-09 23:39:26 +01:00
|
|
|
$(document).ready(self.loadAccounts);
|
2013-01-07 18:42:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function dateToString(date) {
|
|
|
|
if(date) {
|
|
|
|
var year = date.getFullYear();
|
|
|
|
var month = date.getMonth() + 1;
|
|
|
|
var day = date.getDate();
|
|
|
|
|
|
|
|
dateStr = year + "-" + (month < 10 ? '0' : '') + month + "-" + (day < 10 ? '0' : '') + day;
|
|
|
|
|
|
|
|
return dateStr;;
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ko.bindingHandlers.date = {
|
|
|
|
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
|
|
|
|
var unwrap = ko.utils.unwrapObservable;
|
|
|
|
var dataSource = valueAccessor();
|
|
|
|
|
|
|
|
value = dataSource ? unwrap(dataSource) : null;
|
|
|
|
|
|
|
|
if(value) {
|
|
|
|
$(element).text(dateToString(value));
|
|
|
|
} else {
|
|
|
|
$(element).text('');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
|
|
|
|
var unwrap = ko.utils.unwrapObservable;
|
|
|
|
var dataSource = valueAccessor();
|
|
|
|
|
|
|
|
value = dataSource ? unwrap(dataSource) : null;
|
|
|
|
|
|
|
|
if(value) {
|
|
|
|
$(element).text(dateToString(value));
|
|
|
|
} else {
|
|
|
|
$(element).text('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ko.bindingHandlers.dateValue = {
|
|
|
|
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
|
|
|
|
var unwrap = ko.utils.unwrapObservable;
|
|
|
|
var dataSource = valueAccessor();
|
|
|
|
|
|
|
|
value = dataSource ? unwrap(dataSource) : null;
|
|
|
|
|
|
|
|
if(value) {
|
|
|
|
$(element).val(dateToString(value));
|
|
|
|
} else {
|
|
|
|
$(element).val('');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
|
|
|
|
var unwrap = ko.utils.unwrapObservable;
|
|
|
|
var dataSource = valueAccessor();
|
|
|
|
|
|
|
|
value = dataSource ? unwrap(dataSource) : null;
|
|
|
|
|
|
|
|
if(value) {
|
|
|
|
$(element).val(dateToString(value));
|
|
|
|
} else {
|
|
|
|
$(element).val('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ko.applyBindings(new ListViewModel());
|
|
|
|
|