Manage contributor by their email

This commit is contained in:
Benjamin Renard 2014-01-19 14:48:41 +01:00
parent bf9233401f
commit 0b8d66fbf5
3 changed files with 84 additions and 93 deletions

View file

@ -93,13 +93,13 @@ view_home=function() {
balances=groups.balances(); balances=groups.balances();
var sum=0; var sum=0;
for (g in balances) { for (g in balances) {
if (jQuery.type(balances[g]['balance'][user.name])!='undefined') { if (jQuery.type(balances[g]['balance'][user.email])!='undefined') {
if (balances[g]['balance'][user.name]['diff']<0) { if (balances[g]['balance'][user.email]['diff']<0) {
sum+=balances[g]['balance'][user.name]['diff']; sum+=balances[g]['balance'][user.email]['diff'];
diff='<td class="negative">'+balances[g]['balance'][user.name]['diff'].toFixed(2)+'</td>'; diff='<td class="negative">'+balances[g]['balance'][user.email]['diff'].toFixed(2)+'</td>';
} }
else { else {
value=balances[g]['balance'][user.name]['total']-balances[g]['min']; value=balances[g]['balance'][user.email]['total']-balances[g]['min'];
sum+=value; sum+=value;
diff='<td class="positive">+'+value.toFixed(2)+' €</td>'; diff='<td class="positive">+'+value.toFixed(2)+' €</td>';
} }
@ -143,9 +143,9 @@ set_contributors=function(group) {
$('#view-group #contributor').append('<option>Aucun participant</option>'); $('#view-group #contributor').append('<option>Aucun participant</option>');
} }
else { else {
$(group.contributors).each(function(idx,c) { for (email in group.contributors) {
$('#view-group #contributor').append('<option value="'+c.name+'">'+c.name+'</option>'); $('#view-group #contributor').append('<option value="'+email+'">'+group.contributors[email].name+'</option>');
}); }
} }
} }
@ -153,11 +153,11 @@ on_contributor_change=function(e) {
show_contributions(groups[$('#view-group').data('uuid')],$('#view-group #contributor')[0].value); show_contributions(groups[$('#view-group').data('uuid')],$('#view-group #contributor')[0].value);
} }
show_contributions=function(group,contributor_name) { show_contributions=function(group,contributor_email) {
var tbody=$($('#view-group #contributions tbody')[0]); var tbody=$($('#view-group #contributions tbody')[0]);
tbody.html(''); tbody.html('');
total=$($('#view-group #total-value')[0]); total=$($('#view-group #total-value')[0]);
contributions=group.contributionsByContributorName(contributor_name); contributions=group.contributionsByContributorEmail(contributor_email);
contributions.reverse(); contributions.reverse();
if (contributions.length==0) { if (contributions.length==0) {
tbody.append('<tr><td colspan=3>Aucune contributions</td></tr>'); tbody.append('<tr><td colspan=3>Aucune contributions</td></tr>');
@ -185,6 +185,7 @@ on_click_add_contributor_btn=function() {
$($('#add_contributor_modal h4')[0]).html('Ajouter un participant'); $($('#add_contributor_modal h4')[0]).html('Ajouter un participant');
$($('#add_contributor_modal #add_contributor_submit')[0]).html('Ajouter'); $($('#add_contributor_modal #add_contributor_submit')[0]).html('Ajouter');
$('#view-group #edit_id')[0].value='-1'; $('#view-group #edit_id')[0].value='-1';
$('#add_contributor_modal #add_contributor_email').attr('disabled',false);
$('#add_contributor_modal').modal('show'); $('#add_contributor_modal').modal('show');
} }
@ -201,18 +202,13 @@ on_valid_add_contributor_modal=function(e) {
alert('Vous devez saisir un nom !'); alert('Vous devez saisir un nom !');
return; return;
} }
duplicate=group.contributorByName(name);
if (duplicate!=undefined && duplicate.id!=edit_id) {
alert('Un contributor porte déjà ce nom !');
return;
}
var email=$('#add_contributor_email')[0].value; var email=$('#add_contributor_email')[0].value;
if (email=='') { if (email=='') {
alert('Vous devez saisir un email !'); alert('Vous devez saisir un email !');
return; return;
} }
duplicate=group.contributorByEmail(email) duplicate=group.contributorByEmail(email)
if (duplicate!=undefined && duplicate.id!=edit_id) { if (duplicate!=undefined && edit=='-1') {
alert('Un contributor utilise déjà cet email !'); alert('Un contributor utilise déjà cet email !');
return; return;
} }
@ -220,12 +216,10 @@ on_valid_add_contributor_modal=function(e) {
group.addContributor(new Contributor(name,email)); group.addContributor(new Contributor(name,email));
} }
else { else {
group.replaceContributor(edit_id,new Contributor(name,email)); group.replaceContributor(email,new Contributor(name,email));
} }
set_contributors(group); set_contributors(group);
$('#add_contributor_modal').modal('hide'); $('#add_contributor_modal').modal('hide');
$('#add_contributor_name')[0].value='';
$('#add_contributor_email')[0].value='';
groups.save(); groups.save();
} }
@ -237,11 +231,12 @@ on_close_add_contributor_modal=function () {
on_edit_contributor_btn_click=function(e) { on_edit_contributor_btn_click=function(e) {
var group=groups[$('#view-group').data('uuid')]; var group=groups[$('#view-group').data('uuid')];
$('#add_contributor_modal').data('group-uuid',group.uuid); $('#add_contributor_modal').data('group-uuid',group.uuid);
contributor_name=$('#view-group #contributor')[0].value; contributor_email=$('#view-group #contributor')[0].value;
contributor=group.contributorByName(contributor_name); contributor=group.contributorByEmail(contributor_email);
$('#view-group #edit_id')[0].value=contributor.id; $('#view-group #edit_id')[0].value=contributor_email;
$('#add_contributor_modal #add_contributor_name')[0].value=contributor.name; $('#add_contributor_modal #add_contributor_name')[0].value=contributor.name;
$('#add_contributor_modal #add_contributor_email')[0].value=contributor.email; $('#add_contributor_modal #add_contributor_email')[0].value=contributor.email;
$('#add_contributor_modal #add_contributor_email').attr('disabled',true);
$($('#add_contributor_modal h4')[0]).html('Editer un participant'); $($('#add_contributor_modal h4')[0]).html('Editer un participant');
$($('#add_contributor_modal #add_contributor_submit')[0]).html('Valider'); $($('#add_contributor_modal #add_contributor_submit')[0]).html('Valider');
$('#add_contributor_modal').modal('show'); $('#add_contributor_modal').modal('show');
@ -264,12 +259,8 @@ on_confirm_del_contributor=function(contributor) {
* Add/edit/delete contribution * Add/edit/delete contribution
********************************/ ********************************/
on_show_add_contribution_modal=function(e) { on_show_add_contribution_modal=function(e) {
var cs=$($('#add_contribution_modal #add_contribution_contributor_name')[0]); $('#add_contribution_modal #add_contribution_contributor_email').html($('#view-group #contributor').html());
cs.html(''); $('#add_contribution_modal #add_contribution_contributor_email')[0].value=$('#view-group #contributor')[0].value;
$('#view-group #contributor option').each(function(idx,option) {
cs.append($(option).clone());
});
cs[0].value = $('#view-group #contributor')[0].value;
$('#add_contribution_modal #add_contribution_title').focus(); $('#add_contribution_modal #add_contribution_title').focus();
} }
@ -284,8 +275,8 @@ on_click_add_contribution_btn=function() {
on_valid_add_contribution_modal=function(e) { on_valid_add_contribution_modal=function(e) {
e.preventDefault(); e.preventDefault();
var group=groups[$('#add_contribution_modal').data('group-uuid')]; var group=groups[$('#add_contribution_modal').data('group-uuid')];
var contributor_name=$('#add_contribution_contributor_name')[0].value; var contributor_email=$('#add_contribution_contributor_email')[0].value;
var contributor=group.contributorByName(contributor_name); var contributor=group.contributorByEmail(contributor_email);
if (contributor==undefined) { if (contributor==undefined) {
alert('Participant inconu !'); alert('Participant inconu !');
return; return;
@ -333,7 +324,7 @@ on_valid_add_contribution_modal=function(e) {
contribution_uuid=$('#add_contribution_modal #edit_uuid')[0].value; contribution_uuid=$('#add_contribution_modal #edit_uuid')[0].value;
group.updateContribution(contribution_uuid,new Contribution(contributor,cost,title,date)); group.updateContribution(contribution_uuid,new Contribution(contributor,cost,title,date));
} }
show_contributions(group,contributor_name); show_contributions(group,contributor_email);
$('#add_contribution_modal').modal('hide'); $('#add_contribution_modal').modal('hide');
$('#add_contribution_modal form')[0].reset(); $('#add_contribution_modal form')[0].reset();
groups.save(); groups.save();
@ -363,7 +354,7 @@ on_contribution_edit_btn_click=function(e) {
$($('#add_contribution_modal #add_contribution_submit')[0]).html('Valider'); $($('#add_contribution_modal #add_contribution_submit')[0]).html('Valider');
contribution_uuid=$($(e.target).parents('div')[0]).data('uuid'); contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
contribution=group.contributions[contribution_uuid]; contribution=group.contributions[contribution_uuid];
$('#add_contribution_modal #add_contribution_contributor_name')[0].value=contribution.contributor.name; $('#add_contribution_modal #add_contribution_contributor_email')[0].value=contribution.contributor.email;
$('#add_contribution_modal #add_contribution_title')[0].value=contribution.getTitle(); $('#add_contribution_modal #add_contribution_title')[0].value=contribution.getTitle();
$('#add_contribution_modal #add_contribution_cost')[0].value=contribution.cost; $('#add_contribution_modal #add_contribution_cost')[0].value=contribution.cost;
$('#add_contribution_modal #add_contribution_date')[0].value=moment(contribution.date).format('DD/MM/YYYY'); $('#add_contribution_modal #add_contribution_date')[0].value=moment(contribution.date).format('DD/MM/YYYY');
@ -406,7 +397,7 @@ display_balance=function(group) {
else { else {
diff='<td><span class="glyphicon glyphicon-thumbs-up"></span></td>'; diff='<td><span class="glyphicon glyphicon-thumbs-up"></span></td>';
} }
tbody.append('<tr><td>'+c+'</td><td>'+bal['balance'][c]['total'].toFixed(2)+' €</td>'+diff+'</tr>'); tbody.append('<tr><td>'+bal['balance'][c]['name']+'</td><td>'+bal['balance'][c]['total'].toFixed(2)+' €</td>'+diff+'</tr>');
} }
$('#display_balance_modal #total-value').html(bal.sum.toFixed(2)+' €'); $('#display_balance_modal #total-value').html(bal.sum.toFixed(2)+' €');
$('#display_balance_modal').modal('show'); $('#display_balance_modal').modal('show');
@ -523,8 +514,7 @@ on_click_sync_btn=function(e) {
navbar_collapse_hide(); navbar_collapse_hide();
if (user) { if (user) {
pleaseWaitShow(); pleaseWaitShow();
sync_server.sync(user.server, user.email, user.password, groups.export(), onsuccess=function(data) {
function(data) {
console.log(data); console.log(data);
if (data.groups) { if (data.groups) {
groups.import(data.groups); groups.import(data.groups);
@ -534,17 +524,23 @@ on_click_sync_btn=function(e) {
alert('Groupes synchronisés'); alert('Groupes synchronisés');
} }
else { else {
onerror(data);
}
}
onerror=function(data) {
pleaseWaitHide(); pleaseWaitHide();
console.log(data); console.log(data);
if (jQuery.type(data['loginerror'])!='undefined') {
alert(data['loginerror']);
}
else if (jQuery.type(data['syncerror'])!='undefined') {
alert(data['syncerror']);
}
else {
alert('Erreur durant la synchronisation :('); alert('Erreur durant la synchronisation :(');
} }
},
function(data) {
pleaseWaitHide();
console.log(data);
alert('Impossible de contacter le serveur :(');
} }
); sync_server.sync(user.server, user.email, user.password, groups.export(),onsuccess,onerror);
} }
else { else {
alert("Vous devez vous connecter pour commencer"); alert("Vous devez vous connecter pour commencer");

View file

@ -116,7 +116,7 @@ function GroupList() {
function Group(uuid,name,data) { function Group(uuid,name,data) {
this.uuid=uuid || generate_uuid(); this.uuid=uuid || generate_uuid();
this.name=name || false; this.name=name || false;
this.contributors=[]; this.contributors={};
this.contributions={}; this.contributions={};
this.deletedContributions={}; this.deletedContributions={};
@ -126,9 +126,9 @@ function Group(uuid,name,data) {
} }
this.export=function() { this.export=function() {
var contributors=[]; var contributors={};
for (idx in this.contributors) { for (email in this.contributors) {
contributors.push(this.contributors[idx].export()); contributors[email]=this.contributors[email].export();
} }
var contributions={} var contributions={}
for (uuid in this.contributions) { for (uuid in this.contributions) {
@ -147,42 +147,39 @@ function Group(uuid,name,data) {
* Contributors * Contributors
*/ */
this.removeContributor=function(c) { this.removeContributor=function(c) {
this.contributors=this.contributors.filter(function(v){ delete this.contributors[c.email];
return (v.name!=c);
});
} }
this.contributorByName=function(name) { this.contributorByName=function(name) {
for (c in this.contributors) { for (email in this.contributors) {
if (this.contributors[c].name == name) return this.contributors[c]; if (this.contributors[email].name == name) return this.contributors[email];
} }
return undefined; return undefined;
} }
this.contributorByEmail=function(email) { this.contributorByEmail=function(email) {
for (c in this.contributors) { if (jQuery.type(this.contributors[email])!='undefined') {
if (this.contributors[c].email == email) return this.contributors[c]; return this.contributors[email];
} }
return undefined; return undefined;
} }
this.addContributor=function(c) { this.addContributor=function(c) {
c.id=this.contributors.length; this.contributors[c.email]=c;
this.contributors.push(c);
} }
this.replaceContributor=function(idx,c) { this.replaceContributor=function(email,c) {
c.id=idx; delete this.contributors[email];
this.contributors[idx]=c; this.contributors[c.email]=c;
} }
/* /*
* Contributions * Contributions
*/ */
this.contributionsByContributorName=function(name) { this.contributionsByContributorEmail=function(email) {
var ret=[]; var ret=[];
for (uuid in this.contributions) { for (uuid in this.contributions) {
if (this.contributions[uuid].contributor.name==name) { if (this.contributions[uuid].contributor.email==email) {
ret.push(this.contributions[uuid]); ret.push(this.contributions[uuid]);
} }
} }
@ -223,10 +220,9 @@ function Group(uuid,name,data) {
total={} total={}
min=-1; min=-1;
max=0; max=0;
for (idx in this.contributors) { for (email in this.contributors) {
var sum=0; var sum=0;
c=this.contributors[idx].name; cl=this.contributionsByContributorEmail(email);
cl=this.contributionsByContributorName(c);
for (idx in cl) { for (idx in cl) {
sum+=cl[idx].cost; sum+=cl[idx].cost;
} }
@ -236,16 +232,17 @@ function Group(uuid,name,data) {
if(max<sum) { if(max<sum) {
max=sum; max=sum;
} }
total[c]=sum; total[email]=sum;
} }
balance={} balance={}
var sum=0; var sum=0;
for (c in total) { for (email in total) {
balance[c]={ balance[email]={
'total': total[c], 'name': this.contributors[email].name,
'diff': total[c]-max, 'total': total[email],
'diff': total[email]-max,
} }
sum=sum+total[c]; sum=sum+total[email];
} }
return { return {
'balance': balance, 'balance': balance,
@ -262,19 +259,18 @@ function Group(uuid,name,data) {
try { try {
this.uuid=data.uuid; this.uuid=data.uuid;
this.name=data.name; this.name=data.name;
if (jQuery.type(data.contributors) == 'array') { if (jQuery.type(data.contributors) == 'object') {
for (idx in data.contributors) { for (email in data.contributors) {
this.contributors.push(new Contributor( this.contributors[email]=new Contributor(
decodeURIComponent(data.contributors[idx].name), decodeURIComponent(data.contributors[email].name),
decodeURIComponent(data.contributors[idx].email), data.contributors[email].email
idx );
));
} }
} }
if (jQuery.type(data.contributions) == 'object') { if (jQuery.type(data.contributions) == 'object') {
for (uuid in data.contributions) { for (uuid in data.contributions) {
this.contributions[uuid]=new Contribution( this.contributions[uuid]=new Contribution(
this.contributorByName(data.contributions[uuid].contributor), this.contributorByEmail(data.contributions[uuid].contributor),
data.contributions[uuid].cost, data.contributions[uuid].cost,
decodeURIComponent(data.contributions[uuid].title), decodeURIComponent(data.contributions[uuid].title),
data.contributions[uuid].date, data.contributions[uuid].date,
@ -295,14 +291,13 @@ function Group(uuid,name,data) {
} }
} }
function Contributor(name,email,id) { function Contributor(name,email) {
this.name=name; this.name=name;
this.email=email; this.email=email;
this.id=id;
this.export=function() { this.export=function() {
return { return {
'name': encodeURIComponent(this.name), 'name': encodeURIComponent(this.name),
'email': encodeURIComponent(this.email) 'email': this.email
}; };
} }
} }
@ -319,7 +314,7 @@ function Contribution(contributor,cost,title,date,uuid,lastChange) {
this.lastChange=lastChange || new Date().getTime(); this.lastChange=lastChange || new Date().getTime();
this.export=function() { this.export=function() {
return { return {
'contributor': encodeURIComponent(this.contributor.name), 'contributor': this.contributor.email,
'uuid': this.uuid, 'uuid': this.uuid,
'cost': this.cost, 'cost': this.cost,
'title': encodeURIComponent(this.title), 'title': encodeURIComponent(this.title),

View file

@ -316,7 +316,7 @@ body{
<div class="form-group"> <div class="form-group">
<div class="input-group"> <div class="input-group">
<span class="input-group-addon">Participant *</span> <span class="input-group-addon">Participant *</span>
<select id='add_contribution_contributor_name' class="form-control"></select> <select id='add_contribution_contributor_email' class="form-control"></select>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">