Added authorized overdraft. Closes #6.

This commit is contained in:
Alexis Lahouze
2013-01-24 20:44:09 +01:00
parent a576a36d3d
commit 9e0a5a2ea1
6 changed files with 79 additions and 31 deletions

View File

@ -120,6 +120,10 @@
<div class="controls">
<input type="text" id="inputName" data-bind="value: name"></input>
</div>
<label class="control-label" for="inputAuthorizedOverdraft">D&eacute;couvert authoris&eacute;</label>
<div class="controls">
<input type="text" id="inputAuthorizedOverdraft" data-bind="value: authorized_overdraft"></input>
</div>
</div>
</form>
</div>
@ -130,13 +134,13 @@
</div>
<script id="itemsTmpl" type="text/html">
<tr data-bind="css: { 'error': sold() < 0 }">
<tr data-bind="css: { 'warning': sold() < 0 && sold() >= $root.account().authorized_overdraft(), 'error': sold() < $root.account().authorized_overdraft() }">
<td data-bind="text: value_date"></td>
<td data-bind="text: operation_date"></td>
<td data-bind="text: label"></td>
<td data-bind="text: value, css: {'text-error': value() < 0 }"></td>
<td data-bind="text: sold, css: {'text-error': sold() < 0 }"></td>
<td data-bind="text: operation_date() ? pointedsold : '', css: {'text-error': operation_date() && pointedsold() < 0 }"></td>
<td data-bind="text: sold, css: {'text-warning': sold() < 0 && sold() >= $root.account().authorized_overdraft(), 'text-error': sold() < $root.account().authorized_overdraft() }"></td>
<td data-bind="text: operation_date() ? pointedsold : '', css: {'text-warning': pointedsold() < 0 && pointedsold() >= $root.account().authorized_overdraft(), 'text-error': pointedsold() < $root.account().authorized_overdraft() }"></td>
<td data-bind="text: category"></td>
<td class="buttons">
<a class="btn btn-mini" data-bind="click: $root.edit" href="#" title="edit"><i class="icon-edit"></i></a>

View File

@ -15,6 +15,7 @@ function entry(){
function account() {
this.id=ko.observable();
this.name=ko.observable();
this.authorized_overdraft=ko.observable();
this.future=ko.observable();
this.current=ko.observable();
this.pointed=ko.observable();
@ -72,7 +73,8 @@ var ListViewModel = function() {
self.addAccount = function() {
self.editingAccount(ko.mapping.fromJS({
id: null,
name: null
name: null,
authorized_overdraft: null
}));
$("#edit-account").modal();
@ -88,6 +90,7 @@ var ListViewModel = function() {
self.cancelEditAccount = function() {
if(self.editingAccount() && self.savedAccount) {
self.editingAccount().name(self.savedAccount.name);
self.editingAccount().authorized_overdraft(self.savedAccount.authorized_overdraft);
}
self.editingAccount(null);
@ -195,7 +198,7 @@ var ListViewModel = function() {
}
});
return chartValues;
return {account: self.account(), entries: chartValues};
}, self);
// Function to load entries from server for a specific account and month.
@ -224,26 +227,26 @@ var ListViewModel = function() {
// Update accounts
self.accounts(ko.utils.arrayMap($.parseJSON(data), ko.mapping.fromJS));
var accountToSelect = null
// Reset selected account to the new instance corresponding to the old one.
if(self.account()) {
var oldId = self.account().id();
// Reset to null
self.account(null);
// Find the new instance of the previously selected account.
$.each(self.accounts(), function(index, account) {
if(account.id() == oldId) {
self.account(account);
if(account.id() == self.account().id()) {
accountToSelect = account;
}
});
}
// Set selected account to first one if not yet selected
if(!self.account() && self.accounts().length > 0){
self.account(self.accounts()[0]);
if(!accountToSelect && self.accounts().length > 0){
accountToSelect = self.accounts()[0];
}
// Reset to account to select
self.account(accountToSelect);
// Load months if there is any account, or remove months.
if(self.account()) {
self.loadMonths(self.account());
@ -259,27 +262,26 @@ var ListViewModel = function() {
// Update months
self.months(ko.utils.arrayMap($.parseJSON(data), ko.mapping.fromJS));
var monthToSelect = null;
// Reset selected month to the new instance corresponding to the old one
if(self.month()) {
var oldYear = self.month().year();
var oldMonth = self.month().month();
// Reset to null
self.month(null);
// Find the new instance of the previously selected month.
$.each(self.months(), function(index, month) {
if(month.year() == oldYear && month.month() == oldMonth) {
self.month(month);
if(month.year() === self.month().year() && month.month() === self.month().month()) {
monthToSelect = month;
}
});
}
// Set selected month to the last one if not yet selected.
if(!self.month() && self.months().length > 0) {
self.month(self.months()[self.months().length - 1]);
if(!monthToSelect && self.months().length > 0) {
monthToSelect = self.months()[self.months().length - 1];
}
// Reset to month to select
self.month(monthToSelect);
// Load entries if there is a month or remove entries.
if(self.month) {
self.loadEntries(self.account(), self.month());
@ -430,10 +432,12 @@ var ListViewModel = function() {
};
// Function to draw the sold evolution chart.
drawChart = function(entries, element) {
drawChart = function(data, element) {
// Clear previous chart
$(element).html("");
var entries = data.entries;
if(entries && entries.length > 0) {
// Prepare for today vertical line.
var today = new Date();
@ -494,11 +498,19 @@ drawChart = function(entries, element) {
canvasOverlay: {
show: true,
objects: [
// Red horizontal line for 0 limit
// Orange horizontal line for 0 limit
{dashedHorizontalLine: {
name: "zero",
y: 0,
lineWidth: 1,
color: "orange",
shadow: false
}},
// Red horizontal line for authorized overdraft limit
{dashedHorizontalLine: {
name: "overdraft",
y: data.account.authorized_overdraft(),
lineWidth: 1,
color: "red",
shadow: false
}},
@ -573,8 +585,8 @@ ko.bindingHandlers.chart = {
var unwrap = ko.utils.unwrapObservable;
var dataSource = valueAccessor();
var entries = dataSource ? unwrap(dataSource) : null;
drawChart(entries, element);
var data = dataSource ? unwrap(dataSource) : null;
drawChart(data, element);
}
};