Files
dbois/public/js/lib/modules/modals/offerModal.js
T
2018-01-10 14:09:34 +01:00

153 lines
4.8 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( [] ),
startdate : ko.observable(),
acceptance_chance: ko.observable(),
accepteddate : ko.observable(),
rejectdate : ko.observable(),
customer_id : ko.observable(),
project_id : ko.observable(),
init: function () {
var vm = dbo.editOfferModal;
dbo.editOfferModal.refreshCustomers();
},
newEditOffer: function ( offer, callback ) {
var vm = dbo.editOfferModal;
vm.callback = typeof(callback) == 'function' ? callback : null;
vm.showOfferModal();
vm.setOffer( offer );
},
getCustomerProjects: function () {
var vm = dbo.editOfferModal;
},
saveOffer: function () {
var vm = dbo.editOfferModal;
if ( vm.formValid == false ) return;
var submitDATA = {
id : vm.id,
project_id : vm.project_id(),
customer_id : vm.customer_id(),
startdate : vm.startdate(),
acceptance_chance: vm.acceptance_chance(),
accepteddate : vm.accepteddate(),
rejectdate : vm.rejectdate(),
}
dbo.offerDataService.saveOffer( submitDATA, function ( data ) {
if ( data.success ) {
tellMe( {
title : 'offer aangemaakt',
message : 'de offer is succesvol opgeslagen',
modalClass: 'modal-success'
}, function () {
vm.hideOfferModal();
if ( typeof(vm.callback) == 'function' ) vm.callback();
} );
return;
}
var errorMessage = '<ul>';
$.each( data.messages, function ( i, p ) {
if ( p[0] ) {
errorMessage += '<li>' + i + ': ' + p[0] + '</li>';
}
} );
errorMessage += '</ul>';
tellMe( {
title : 'fout aanmaken offer',
message : errorMessage,
modalClass: 'modal-danger'
} );
} );
},
setOffer: function ( offer ) {
var vm = dbo.editOfferModal;
vm.id = offer.id || 0;
vm.customer_id( offer.customer_id || null );
vm.project_id( offer.project_id || null );
vm.startdate( offer.startdate || null );
vm.acceptance_chance( offer.acceptance_chance || 0 );
vm.accepteddate( offer.accepteddate || null );
vm.rejectdate( offer.rejectdate || null );
},
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();
} );
},
refreshCustomers: function () {
var vm = dbo.editOfferModal;
dbo.customerDataService.getAllCustomers( 'F', function ( data ) {
vm.customers( 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.customer_id() ) return false;
if ( !this.offerDateValid() ) return false;
return true;
}, dbo.editOfferModal )
ko.applyBindings( dbo.editOfferModal, document.getElementById( 'offer-modal' ) );
} );