631 lines
22 KiB
JavaScript
631 lines
22 KiB
JavaScript
/**
|
|
* Created by Pensare Software.
|
|
* Offer: Theo Weijmer
|
|
* Date: 25/12/2017
|
|
* Time: 17:32
|
|
*/
|
|
|
|
$( function () {
|
|
var timeout;
|
|
dbo.editProjectModal = {
|
|
|
|
offers : ko.observableArray( [] ),
|
|
projectEmployees : ko.observableArray( [] ),
|
|
customers : ko.observableArray( [] ),
|
|
users : ko.observableArray( [] ),
|
|
invoices : ko.observableArray( [] ),
|
|
projecthours : ko.observableArray( [] ),
|
|
offerFiles : ko.observableArray( [] ),
|
|
offerComments : ko.observableArray( [] ),
|
|
projectFiles : ko.observableArray( [] ),
|
|
projectComments : ko.observableArray( [] ),
|
|
externals : ko.observableArray( [] ),
|
|
projectcosts : ko.observableArray( [] ),
|
|
vats : ko.observableArray( [] ),
|
|
availableEmployees: ko.observableArray( [] ),
|
|
showProject : ko.observable( false ),
|
|
projectID : null,
|
|
callback : null,
|
|
customerID : ko.observable( null ),
|
|
projectleaderID : ko.observable(),
|
|
archive : ko.observable( false ),
|
|
startdate : ko.observable( null ),
|
|
enddate : ko.observable( null ),
|
|
busy : false,
|
|
|
|
init: function () {
|
|
var vm = dbo.editProjectModal;
|
|
|
|
dbo.customerDataService.getAllCustomers( 'F', function ( data ) {
|
|
vm.customers( data );
|
|
dbo.userDataService.getAllUsers( 'F', function ( data ) {
|
|
vm.users( data );
|
|
dbo.externalDataService.getAllExternals( 'F', function ( data ) {
|
|
vm.externals( data );
|
|
dbo.vatDataService.getVats( 'F', function ( data ) {
|
|
vm.vats( data );
|
|
|
|
vm.refreshAvailableEmployees( function () {
|
|
vm.refreshProject();
|
|
} );
|
|
} );
|
|
} );
|
|
} );
|
|
} );
|
|
},
|
|
|
|
togglePayments: function () {
|
|
this.showPayments( !this.showPayments() );
|
|
|
|
var showPayments = sessionStorage.showPayments || [];
|
|
|
|
if ( this.showPayments() ) {
|
|
showPayments.push( this.id );
|
|
} else {
|
|
var index = showPayments.indexOf( this.id );
|
|
showPayments.splice( index, 1 );
|
|
}
|
|
|
|
sessionStorage.setItem( "showPayment", showPayments );
|
|
},
|
|
|
|
editProjectemployee: function () {
|
|
var self = this;
|
|
var vm = dbo.editProjectModal;
|
|
$.each( vm.projectEmployees(), function ( i, p ) {
|
|
if ( p.id == self.id ) {
|
|
p.inEdit( !p.inEdit() );
|
|
} else {
|
|
p.inEdit( false );
|
|
}
|
|
} );
|
|
},
|
|
|
|
editInvoice: function () {
|
|
var self = this;
|
|
var vm = dbo.editProjectModal;
|
|
$.each( vm.invoices(), function ( i, p ) {
|
|
if ( p.id == self.id ) {
|
|
p.inEdit( !p.inEdit() );
|
|
} else {
|
|
p.inEdit( false );
|
|
}
|
|
} );
|
|
},
|
|
|
|
editPayment: function ( parent ) {
|
|
var self = this;
|
|
$.each( parent.payments(), function ( i, p ) {
|
|
console.log( p.invoice_id(), ' ', parent.id );
|
|
if ( p.id == self.id ) {
|
|
p.inEdit( !p.inEdit() );
|
|
} else {
|
|
p.inEdit( false );
|
|
}
|
|
} );
|
|
},
|
|
|
|
editProjecthour: function () {
|
|
var self = this;
|
|
var vm = dbo.editProjectModal;
|
|
$.each( vm.projecthours(), function ( i, p ) {
|
|
if ( p.id == self.id ) {
|
|
p.inEdit( !p.inEdit() );
|
|
} else {
|
|
p.inEdit( false );
|
|
}
|
|
} );
|
|
},
|
|
|
|
editProjectcosts: function () {
|
|
var self = this;
|
|
var vm = dbo.editProjectModal;
|
|
$.each( vm.projectcosts(), function ( i, p ) {
|
|
if ( p.id == self.id ) {
|
|
p.inEdit( !p.inEdit() );
|
|
} else {
|
|
p.inEdit( false );
|
|
}
|
|
} );
|
|
},
|
|
|
|
newOffer: function () {
|
|
var vm = dbo.editProjectModal;
|
|
var service = dbo.offersService;
|
|
service.vm = dbo.editProjectModal;
|
|
|
|
service.newOffer( vm.projectID, vm.customerID() );
|
|
},
|
|
|
|
newInvoice: function () {
|
|
var vm = dbo.editProjectModal;
|
|
|
|
var invoice = {
|
|
id : 0,
|
|
project_id : vm.projectID,
|
|
invoicenumber : '',
|
|
invoicedate : null,
|
|
invoiceamount : 0,
|
|
forecastamount: 0,
|
|
forecastdate : null,
|
|
inEdit : true,
|
|
showPayments : false,
|
|
payments : []
|
|
}
|
|
|
|
vm.invoices.push( new vm.Invoice( invoice ) );
|
|
},
|
|
|
|
newPayment: function () {
|
|
var vm = dbo.editProjectModal;
|
|
var self = this;
|
|
console.log( self );
|
|
var payment = {
|
|
id : 0,
|
|
invoice_id : self.id,
|
|
paymentdate : null,
|
|
paymentamount: 0,
|
|
inEdit : true
|
|
};
|
|
|
|
self.payments.push( new vm.Payment( payment ) );
|
|
},
|
|
|
|
newProjecthour: function () {
|
|
var vm = dbo.editProjectModal;
|
|
|
|
var projecthour = {
|
|
id : 0,
|
|
projectemployee_id: null,
|
|
hourdate : null,
|
|
hours : 0,
|
|
inEdit : true
|
|
};
|
|
|
|
vm.projecthours.push( new vm.Projecthour( projecthour ) );
|
|
},
|
|
|
|
newProjectcost: function () {
|
|
var vm = dbo.editProjectModal;
|
|
|
|
var projectcost = {
|
|
id : 0,
|
|
external_id: null,
|
|
description: '',
|
|
date : null,
|
|
costs : 0,
|
|
inEdit : true
|
|
};
|
|
|
|
vm.projectcosts.push( new vm.Projectcost( projectcost ) );
|
|
},
|
|
|
|
editOffer: function () {
|
|
var service = dbo.offersService;
|
|
service.vm = dbo.editProjectModal;
|
|
|
|
service.editOffer( this, false, false, true );
|
|
},
|
|
|
|
addProject: function ( callback ) {
|
|
var vm = dbo.editProjectModal;
|
|
|
|
vm.callback = callback;
|
|
|
|
dbo.projectDataService.addProject( function ( data ) {
|
|
vm.projectID = data;
|
|
|
|
vm.showProject( true );
|
|
|
|
vm.init();
|
|
} );
|
|
},
|
|
|
|
addProjectemployee: function () {
|
|
var vm = dbo.editProjectModal;
|
|
dbo.projectDataService.addProjectemployee( vm.projectID, function () {
|
|
vm.refreshProject();
|
|
} );
|
|
},
|
|
|
|
editProject: function ( projectID, callback ) {
|
|
var vm = dbo.editProjectModal;
|
|
|
|
vm.callback = callback;
|
|
vm.projectID = projectID;
|
|
|
|
vm.showProject( true );
|
|
|
|
vm.init();
|
|
},
|
|
|
|
saveProject: function () {
|
|
var vm = dbo.editProjectModal;
|
|
|
|
if ( vm.busy ) return;
|
|
|
|
var submitDATA = {
|
|
id : vm.projectID,
|
|
customer_id : vm.customerID(),
|
|
projectleader_id: vm.projectleaderID(),
|
|
startdate : vm.startdate(),
|
|
enddate : vm.enddate()
|
|
}
|
|
|
|
dbo.projectDataService.saveProject( vm.projectID, submitDATA, function ( data ) {
|
|
vm.projectID = data;
|
|
} );
|
|
},
|
|
|
|
saveProjectemployee: function () {
|
|
var vm = dbo.editProjectModal;
|
|
var self = this;
|
|
|
|
$.each( vm.users(), function ( i, p ) {
|
|
if ( self.employeeID == p.id ) self.fullname( p.fullname );
|
|
} );
|
|
|
|
if ( timeout ) {
|
|
clearTimeout( timeout );
|
|
timeout = null;
|
|
}
|
|
timeout = setTimeout( function () {
|
|
var submitDATA = {
|
|
id : self.id,
|
|
user_id: self.employeeID(),
|
|
tarif : self.tarif() ? self.tarif().toString().replace( ',', '.' ) : 0,
|
|
budget : self.budget() ? self.budget().toString().replace( ',', '.' ) : 0
|
|
}
|
|
|
|
dbo.projectDataService.saveProjectemployee( submitDATA, function () {
|
|
vm.refreshAvailableEmployees();
|
|
} );
|
|
}, 400 );
|
|
},
|
|
|
|
saveInvoice: function () {
|
|
var self = this;
|
|
|
|
var submitDATA = {
|
|
id : self.id,
|
|
project_id : self.project_id(),
|
|
invoicenumber : self.invoicenumber(),
|
|
invoicedate : self.invoicedate(),
|
|
invoiceamount : self.invoiceamount(),
|
|
forecastamount: self.forecastamount(),
|
|
forecastdate : self.forecastdate(),
|
|
vat_id : self.vat_id()
|
|
};
|
|
|
|
dbo.projectDataService.saveInvoice( submitDATA, function ( data ) {
|
|
if ( data.success ) self.id = data.invoiceID;
|
|
} );
|
|
|
|
},
|
|
|
|
savePayment: function () {
|
|
var self = this;
|
|
|
|
var submitDATA = {
|
|
id : self.id,
|
|
invoice_id : self.invoice_id(),
|
|
paymentdate : self.paymentdate(),
|
|
paymentamount: self.paymentamount()
|
|
};
|
|
|
|
dbo.projectDataService.savePayment( submitDATA, function ( data ) {
|
|
if ( data.success ) self.id = data.paymentID;
|
|
} );
|
|
},
|
|
|
|
saveProjecthour: function () {
|
|
var self = this;
|
|
|
|
if ( !self.projectemployee_id() ) return;
|
|
|
|
var submitDATA = {
|
|
id : self.id,
|
|
projectemployee_id: self.projectemployee_id(),
|
|
hourdate : self.hourdate(),
|
|
hours : self.hours()
|
|
};
|
|
|
|
dbo.projectDataService.saveProjecthour( submitDATA, function ( data ) {
|
|
console.log( data );
|
|
if ( data.success ) self.id = data.projecthourID;
|
|
} );
|
|
},
|
|
|
|
saveProjectcosts: function () {
|
|
var vm = dbo.editProjectModal;
|
|
var self = this;
|
|
|
|
var submitDATA = {
|
|
id : self.id,
|
|
project_id : vm.projectID,
|
|
external_id: self.external_id(),
|
|
description: self.description(),
|
|
date : self.date(),
|
|
costs : self.costs()
|
|
};
|
|
|
|
dbo.projectDataService.saveProjectcost( submitDATA, function ( data ) {
|
|
if ( data.success ) self.id = data.projectcostID;
|
|
} );
|
|
},
|
|
|
|
removeProjectemployee: function () {
|
|
var vm = dbo.editProjectModal;
|
|
var self = this;
|
|
|
|
tellMe( {
|
|
title : 'verwijderen projectmedewerker',
|
|
message : 'weet u zeker dat u de projectmedewerker wilt verwijderen, dit kan niet ongedaan worden gemaakt',
|
|
notify : true,
|
|
noText : 'nee verwijder de projectmedewerker niet',
|
|
yesText : 'ja verwijder de projectmedewerker',
|
|
modalClass: 'modal-warning'
|
|
}, function ( response ) {
|
|
console.log( response );
|
|
if ( response ) {
|
|
dbo.projectDataService.removeProjectemployee( self.id, function ( data ) {
|
|
if ( data.result ) {
|
|
vm.refreshProject();
|
|
} else {
|
|
tellMe( {
|
|
title : 'fout bij verwijderen projectmedewerker',
|
|
message : 'de projectmedewerker kan niet worden verwijderd',
|
|
modalClass: 'modal-danger'
|
|
} );
|
|
}
|
|
} );
|
|
}
|
|
} );
|
|
},
|
|
|
|
removeOffer: function () {
|
|
var vm = dbo.editProjectModal;
|
|
var self = this;
|
|
|
|
tellMe( {
|
|
title : 'verwijderen offerte',
|
|
message : 'weet u zeker dat u de offerte wilt verwijderen, dit kan niet ongedaan worden gemaakt',
|
|
notify : true,
|
|
noText : 'nee verwijder de offerte niet',
|
|
yesText : 'ja verwijder de offerte',
|
|
modalClass: 'modal-warning'
|
|
}, function ( response ) {
|
|
console.log( response );
|
|
if ( response ) {
|
|
dbo.offerDataService.removeOffer( self.id, function ( data ) {
|
|
if ( data.result ) {
|
|
vm.refreshProject();
|
|
} else {
|
|
tellMe( {
|
|
title : 'fout bij verwijderen offerte',
|
|
message : 'de offerte kan niet worden verwijderd',
|
|
modalClass: 'modal-danger'
|
|
} );
|
|
}
|
|
} );
|
|
}
|
|
} );
|
|
},
|
|
|
|
removePayment: function () {
|
|
var vm = dbo.editProjectModal;
|
|
var self = this;
|
|
|
|
tellMe( {
|
|
title : 'verwijderen betaling',
|
|
message : 'weet u zeker dat u de betaling wilt verwijderen, dit kan niet ongedaan worden gemaakt',
|
|
notify : true,
|
|
noText : 'nee verwijder de betaling niet',
|
|
yesText : 'ja verwijder de betaling',
|
|
modalClass: 'modal-warning'
|
|
}, function ( response ) {
|
|
console.log( response );
|
|
if ( response ) {
|
|
dbo.projectDataService.removePayment( self.id, function ( data ) {
|
|
if ( data.result ) {
|
|
vm.refreshProject();
|
|
} else {
|
|
tellMe( {
|
|
title : 'fout bij verwijderen betaling',
|
|
message : 'de betaling kan niet worden verwijderd',
|
|
modalClass: 'modal-danger'
|
|
} );
|
|
}
|
|
} );
|
|
}
|
|
} );
|
|
},
|
|
|
|
removeProjecthour: function () {
|
|
var vm = dbo.editProjectModal;
|
|
var self = this;
|
|
|
|
tellMe( {
|
|
title : 'verwijderen projectuur',
|
|
message : 'weet u zeker dat u de projectuur wilt verwijderen, dit kan niet ongedaan worden gemaakt',
|
|
notify : true,
|
|
noText : 'nee verwijder de projectuur niet',
|
|
yesText : 'ja verwijder de projectuur',
|
|
modalClass: 'modal-warning'
|
|
}, function ( response ) {
|
|
console.log( response );
|
|
if ( response ) {
|
|
dbo.projectDataService.removeProjecthour( self.id, function ( data ) {
|
|
if ( data.result ) {
|
|
vm.refreshProject();
|
|
} else {
|
|
tellMe( {
|
|
title : 'fout bij verwijderen projectuur',
|
|
message : 'de projectuur kan niet worden verwijderd',
|
|
modalClass: 'modal-danger'
|
|
} );
|
|
}
|
|
} );
|
|
}
|
|
} );
|
|
},
|
|
|
|
closeProject: function () {
|
|
var vm = dbo.editProjectModal;
|
|
|
|
vm.showProject( false );
|
|
|
|
vm.callback();
|
|
},
|
|
|
|
validateNumber: function ( field ) {
|
|
this[field]( dbo.helpers.validateNumber( this[field](), true ) );
|
|
},
|
|
|
|
refreshAvailableEmployees: function ( callback ) {
|
|
var vm = dbo.editProjectModal;
|
|
|
|
dbo.projectDataService.getAvailableEmployees( vm.projectID, function ( data ) {
|
|
vm.availableEmployees( data );
|
|
|
|
if ( typeof(callback) == 'function' ) callback();
|
|
} );
|
|
},
|
|
|
|
refreshProject: function () {
|
|
var vm = dbo.editProjectModal;
|
|
vm.busy = true;
|
|
|
|
dbo.projectDataService.getProject( vm.projectID, function ( data ) {
|
|
vm.id = data.id
|
|
vm.projectleaderID( data.projectleader_id );
|
|
vm.customerID( data.customer_id );
|
|
vm.startdate( dbo.helpers.mysql2date( data.startdate ) );
|
|
vm.enddate( dbo.helpers.mysql2date( data.enddate ) );
|
|
|
|
vm.projectEmployees.removeAll();
|
|
vm.projecthours.removeAll();
|
|
$.each( data.projectemployees, function ( i, p ) {
|
|
vm.projectEmployees.push( new vm.Projectemployee( p ) );
|
|
} );
|
|
|
|
$.each( data.projectemployees, function ( i, p ) {
|
|
var fullname = p.user ? p.user.fullname : '#NB';
|
|
$.each( p.projecthours, function ( b, q ) {
|
|
vm.projecthours.push( new vm.Projecthour( q, fullname ) );
|
|
} );
|
|
} );
|
|
|
|
vm.projectcosts.removeAll();
|
|
$.each( data.projectcosts, function ( i, p ) {
|
|
vm.projectcosts.push( new vm.Projectcost( p ) );
|
|
} );
|
|
|
|
vm.offers.removeAll();
|
|
$.each( data.offers, function ( i, p ) {
|
|
vm.offers.push( new dbo.offersService.Offer( p ) );
|
|
} );
|
|
|
|
vm.invoices.removeAll();
|
|
$.each( data.invoices, function ( i, p ) {
|
|
vm.invoices.push( new vm.Invoice( p ) );
|
|
} );
|
|
|
|
vm.busy = false;
|
|
|
|
dbo.charts.drawHoursVsBudget(340, 230, 'hours-vs-budget');
|
|
dbo.charts.drawInvoicesVsPayed(36000, 24000, 11000, 'invoice-vs-payed');
|
|
} );
|
|
},
|
|
|
|
Projectemployee: function ( data ) {
|
|
var self = this;
|
|
|
|
self.id = data.id;
|
|
self.employeeID = ko.observable( data.user_id );
|
|
self.fullname = ko.observable( data.user ? data.user.fullname : '#NB' );
|
|
self.tarif = ko.observable( data.tarif ? data.tarif.toString().replace( '.', ',' ) : 0 );
|
|
self.budget = ko.observable( data.budget ? data.budget.toString().replace( '.', ',' ) : 0 );
|
|
self.inEdit = ko.observable( sessionStorage.editEmployee == data.id );
|
|
},
|
|
|
|
Projecthour: function ( data, fullname ) {
|
|
var inEdit = sessionStorage.Projecthour == data.id;
|
|
if ( !inEdit ) inEdit = data.inEdit || false;
|
|
|
|
this.id = data.id;
|
|
this.fullname = ko.observable( fullname );
|
|
this.projectemployee_id = ko.observable( data.projectemployee_id );
|
|
this.hourdate = ko.observable( dbo.helpers.mysql2date( data.hourdate ) );
|
|
this.hours = ko.observable( data.hours );
|
|
this.inEdit = ko.observable( inEdit );
|
|
},
|
|
|
|
Projectcost: function ( data ) {
|
|
this.id = data.id;
|
|
this.external_id = ko.observable( data.external_id );
|
|
this.contactperson = ko.observable( data.external ? data.external.contactperson : '' );
|
|
this.description = ko.observable( data.description );
|
|
this.date = ko.observable( dbo.helpers.mysql2date( data.date ) );
|
|
this.costs = ko.observable( data.costs );
|
|
this.inEdit = ko.observable( data.inEdit || false );
|
|
},
|
|
|
|
Invoice: function ( data ) {
|
|
var vm = dbo.editProjectModal;
|
|
var self = this;
|
|
var inEdit = sessionStorage.editInvoice == data.id;
|
|
if ( !inEdit ) inEdit = data.inEdit || false;
|
|
|
|
self.id = data.id;
|
|
self.project_id = ko.observable( data.project_id );
|
|
self.invoicenumber = ko.observable( data.invoicenumber );
|
|
self.invoicedate = ko.observable( dbo.helpers.mysql2date( data.invoicedate ) );
|
|
self.invoiceamount = ko.observable( data.invoiceamount.toFixed( 2 ) );
|
|
self.forecastamount = ko.observable( data.forecastamount.toFixed( 2 ) );
|
|
self.forecastdate = ko.observable( dbo.helpers.mysql2date( data.forecastdate ) );
|
|
self.vat_id = ko.observable( data.vat_id );
|
|
|
|
self.inEdit = ko.observable( inEdit );
|
|
|
|
self.inclBtw = ko.pureComputed( function () {
|
|
if ( !self.vat_id() ) return self.invoiceamount();
|
|
|
|
return (Number( self.invoiceamount() ) + (self.invoiceamount() *
|
|
(dbo.helpers.findArrayElement( 'id', 'vatrate', self.vat_id(), this.vats() ) / 100))).toFixed( 2 );
|
|
}, dbo.editProjectModal );
|
|
|
|
self.showPayments = ko.observable( data.showPayments || false );
|
|
if ( sessionStorage.showPayment == data.id ) self.showPayments( true );
|
|
|
|
self.payments = ko.observableArray( [] );
|
|
$.each( data.payments, function ( i, p ) {
|
|
self.payments.push( new vm.Payment( p, self.invoicenumber ) );
|
|
} );
|
|
|
|
self.outstanding = ko.pureComputed( function () {
|
|
var payed = 0
|
|
$.each( self.payments(), function ( i, p ) {
|
|
payed += Number( p.paymentamount() );
|
|
} );
|
|
return (self.inclBtw() - payed).toFixed( 2 );
|
|
} );
|
|
},
|
|
|
|
Payment: function ( data ) {
|
|
var inEdit = sessionStorage.editPayment == data.id;
|
|
if ( !inEdit ) inEdit = data.inEdit || false;
|
|
|
|
this.id = data.id;
|
|
this.invoice_id = ko.observable( data.invoice_id );
|
|
this.paymentdate = ko.observable( dbo.helpers.mysql2date( data.paymentdate ) );
|
|
this.paymentamount = ko.observable( data.paymentamount );
|
|
this.inEdit = ko.observable( inEdit );
|
|
}
|
|
};
|
|
|
|
ko.applyBindings( dbo.editProjectModal, document.getElementById( 'project-modal' ) );
|
|
} );
|
|
|
|
|