222 lines
8.1 KiB
JavaScript
222 lines
8.1 KiB
JavaScript
/**
|
|
* Created by Pensare Software.
|
|
* Offer: Theo Weijmer
|
|
* Date: 25/12/2017
|
|
* Time: 17:32
|
|
*/
|
|
|
|
$( function () {
|
|
dbo.editOfferModal = {
|
|
id : null,
|
|
projects : ko.observableArray( [] ),
|
|
customers : ko.observableArray( [] ),
|
|
users : ko.observableArray( [] ),
|
|
offername : ko.observable(),
|
|
startdate : ko.observable(),
|
|
acceptance_chance: ko.observable(),
|
|
accepteddate : ko.observable(),
|
|
rejectdate : ko.observable(),
|
|
customer_id : ko.observable(),
|
|
project_id : ko.observable(),
|
|
offernumber : ko.observable(),
|
|
offerleader_id : ko.observable(),
|
|
own_revenue : ko.observable(),
|
|
other_revenue : ko.observable(),
|
|
costs : ko.observable(),
|
|
hasProject : ko.observable(),
|
|
|
|
init: function ( callback = false ) {
|
|
dbo.editOfferModal.refreshCustomers();
|
|
|
|
dbo.editOfferModal.refreshUsers();
|
|
|
|
if ( typeof(callback) == 'function' ) callback();
|
|
},
|
|
|
|
newEditOffer: function ( offer, projectID = false, customerID = false, hasProject = false, callback ) {
|
|
var vm = dbo.editOfferModal;
|
|
vm.callback = typeof(callback) == 'function' ? callback : null;
|
|
|
|
vm.init( function () {
|
|
vm.showOfferModal();
|
|
|
|
vm.setOffer( offer, projectID, customerID, hasProject );
|
|
} );
|
|
},
|
|
|
|
getCustomerProjects: function () {
|
|
var vm = dbo.editOfferModal;
|
|
},
|
|
|
|
saveOffer: function () {
|
|
var vm = dbo.editOfferModal;
|
|
|
|
if ( vm.formValid == false ) return;
|
|
|
|
var submitDATA = {
|
|
id : vm.id,
|
|
offername : vm.offername(),
|
|
customer_id : vm.customer_id(),
|
|
offerleader_id : vm.offerleader_id(),
|
|
offernumber : vm.offernumber(),
|
|
own_revenue : vm.own_revenue(),
|
|
other_revenue : vm.other_revenue(),
|
|
costs : vm.costs(),
|
|
startdate : vm.startdate(),
|
|
acceptance_chance: vm.acceptance_chance(),
|
|
accepteddate : vm.accepteddate(),
|
|
rejectdate : vm.rejectdate(),
|
|
}
|
|
|
|
dbo.offerDataService.saveOffer( submitDATA, function ( data ) {
|
|
if ( data.createProject ) {
|
|
tellMe( {
|
|
title : 'project aanmaken',
|
|
message : 'De offerte is goedgekeurd maar nog niet gekoppeld aan een project, wilt u een project aanmaken?',
|
|
notify : true,
|
|
noText : 'Nee, maak geen project aan',
|
|
yesText : 'Ja, maak een project aan',
|
|
modalClass: 'modal-warning'
|
|
}, function ( response ) {
|
|
if ( response ) {
|
|
dbo.projectDataService.createProject( data.offerID, function ( data ) {
|
|
if ( data.success ) {
|
|
tellMe( {
|
|
title : 'project aangemaakt',
|
|
message : 'er is een project aangemaakt met nummer: ' + data.projectnumber,
|
|
modalClass: 'modal-success'
|
|
}, function () {
|
|
vm.hideOfferModal();
|
|
vm.callback();
|
|
} );
|
|
} else {
|
|
tellMe( {
|
|
title : 'fout project aanmaken',
|
|
message : 'helaas kon het project niet aangemaakt worden',
|
|
modalClass: 'modal-danger'
|
|
} );
|
|
}
|
|
} );
|
|
}
|
|
} )
|
|
} else if ( !data.success ) {
|
|
var errorMessage = '<ul>';
|
|
$.each( data.messages, function ( i, p ) {
|
|
if ( p[0] ) {
|
|
errorMessage += '<li>' + i + ': ' + p[0] + '</li>';
|
|
}
|
|
} );
|
|
errorMessage += '</ul>';
|
|
tellMe( {
|
|
title : 'fout opslaan offerte',
|
|
message : errorMessage,
|
|
modalClass: 'modal-danger'
|
|
} );
|
|
} else {
|
|
vm.hideOfferModal();
|
|
vm.callback();
|
|
}
|
|
} );
|
|
},
|
|
|
|
setOffer: function ( offer, projectID, customerID, hasProject ) {
|
|
var vm = dbo.editOfferModal;
|
|
|
|
vm.hasProject( hasProject );
|
|
|
|
vm.id = offer.id || 0;
|
|
|
|
if ( projectID ) {
|
|
vm.project_id( projectID );
|
|
vm.customer_id( customerID );
|
|
} else {
|
|
vm.project_id( offer.project_id || null );
|
|
vm.customer_id( offer.customer_id || null );
|
|
}
|
|
vm.offername(offer.offername || '');
|
|
vm.offerleader_id( offer.offerleader_id || null );
|
|
vm.offernumber( offer.offernumber || null );
|
|
vm.startdate( offer.startdate || null );
|
|
vm.acceptance_chance( offer.acceptance_chance || 0 );
|
|
vm.accepteddate( offer.accepteddate || null );
|
|
vm.rejectdate( offer.rejectdate || null );
|
|
vm.own_revenue( offer.own_revenue || 0 );
|
|
vm.other_revenue( offer.other_revenue || 0 );
|
|
vm.costs( offer.costs || 0 );
|
|
},
|
|
|
|
showOfferModal: function () {
|
|
$( "#offer-modal" ).modal( 'show' );
|
|
},
|
|
|
|
hideOfferModal: function () {
|
|
$( "#offer-modal" ).modal( 'hide' );
|
|
},
|
|
|
|
validateNumber: function () {
|
|
var vm = dbo.editOfferModal;
|
|
vm.acceptance_chance( dbo.helpers.validateNumber( vm.acceptance_chance(), false ) );
|
|
},
|
|
|
|
addCustomer: function () {
|
|
var vm = dbo.editOfferModal;
|
|
dbo.editCustomerModal.newEditCustomer( false, function () {
|
|
vm.refreshCustomers();
|
|
} );
|
|
},
|
|
|
|
addUser: function () {
|
|
var vm = dbo.editOfferModal;
|
|
dbo.editUserModal.newEditUser( false, function () {
|
|
vm.refreshUsers();
|
|
} );
|
|
|
|
},
|
|
|
|
refreshCustomers: function () {
|
|
var vm = dbo.editOfferModal;
|
|
dbo.customerDataService.getAllCustomers( 'F', function ( data ) {
|
|
vm.customers( data );
|
|
} );
|
|
},
|
|
|
|
refreshUsers: function () {
|
|
var vm = dbo.editOfferModal;
|
|
dbo.userDataService.getAllUsers( 'F', function ( data ) {
|
|
vm.users( data );
|
|
} );
|
|
},
|
|
|
|
resetDate: function ( field ) {
|
|
var vm = dbo.editOfferModal;
|
|
|
|
vm[field]( null );
|
|
}
|
|
};
|
|
|
|
dbo.editOfferModal.init();
|
|
|
|
dbo.editOfferModal.offerDateValid = ko.pureComputed( function () {
|
|
if ( !this.startdate() ) return false;
|
|
if ( this.startdate().length != 10 ) return false;
|
|
|
|
return true;
|
|
}, dbo.editOfferModal );
|
|
|
|
dbo.editOfferModal.chance = ko.pureComputed( function () {
|
|
if ( this.acceptance_chance() < 0 ) return this.acceptance_chance( 0 );
|
|
if ( this.acceptance_chance() > 100 ) return this.acceptance_chance( 100 );
|
|
}, dbo.editOfferModal );
|
|
|
|
dbo.editOfferModal.formValid = ko.pureComputed( function () {
|
|
if ( !this.offernumber() ) return false;
|
|
if ( !this.customer_id() ) return false;
|
|
if ( !this.offerleader_id() ) return false;
|
|
if ( !this.offerDateValid() ) return false;
|
|
|
|
return true;
|
|
}, dbo.editOfferModal )
|
|
|
|
ko.applyBindings( dbo.editOfferModal, document.getElementById( 'offer-modal' ) );
|
|
|
|
} ); |