export class QuickStartWizardController extends BaseWizardController {
constructor( options = {} ) {
_.defaults( options, {
el: '.wizard-bg',
selected_province_index: -1,
selected_country_index: -1,
} );
super( options );
}
init( options ) {
//this._super('initialize', options );
this.title = $.i18n._( 'Quick Start Wizard' );
this.steps = 5;
this.current_step = 1;
this.render();
}
render() {
super.render();
this.initCurrentStep();
}
//Create each page UI
buildCurrentStepUI() {
var $this = this;
this.content_div.empty();
this.stepsWidgetDic[this.current_step] = {};
switch ( this.current_step ) {
case 1:
var label = this.getLabel();
label.text( $.i18n._( 'Welcome to' ) + ' ' + LocalCacheData.getApplicationName() + ', ' + $.i18n._( 'this Quick Start Wizard will walk you through the initial setup by asking you a few basic questions about your company.' ) );
var guide_label = $( '
' );
return date_picker.get( 0 ).outerHTML;
}
onCloseClick() {
var $this = this;
if ( !LocalCacheData.getCurrentCompany().is_setup_complete ) {
TAlertManager.showConfirmAlert( $.i18n._( 'Would you like to be reminded to complete the Quick Start Wizard next time you login?' ), '', function( flag ) {
if ( !flag ) {
var company = {};
company.id = LocalCacheData.getCurrentCompany().id;
company.is_setup_complete = true;
var company_api = TTAPI.APICompany;
LocalCacheData.getCurrentCompany().is_setup_complete = true;
company_api.setCompany( company, {
onResult: function() {
}
} );
}
$( $this.el ).remove();
LocalCacheData.current_open_wizard_controllers = LocalCacheData.current_open_wizard_controllers.filter( wizard => wizard.wizard_id !== $this.wizard_id );
} );
} else {
$( $this.el ).remove();
LocalCacheData.current_open_wizard_controllers = LocalCacheData.current_open_wizard_controllers.filter( wizard => wizard.wizard_id !== this.wizard_id );
}
}
onStep3DatePickerChange( target ) {
var $this = this;
var current_step_ui = this.stepsWidgetDic[this.current_step];
var grid = current_step_ui['example_dates'];
var target_id = target.attr( 'id' );
var row_id = target_id.split( '_' )[0];
var field = target_id.substring( target_id.indexOf( '_' ) + 1, target_id.length );
var data = grid.getData();
var target_val = target.getValue();
var first_cell_val = data[0].start_date;
var len = data.length;
for ( var i = 0; i < len; i++ ) {
var row_data = data[i];
if ( row_data.id == row_id ) {
row_data[field] = target_val;
if ( i === 0 && field === 'start_date' ) {
if ( first_cell_val && first_cell_val !== target_val ) {
$this.setDefaultDates( true );
} else if ( !first_cell_val ) {
$this.setDefaultDates();
}
}
break;
}
}
}
setDefaultDates( show_alert ) {
var $this = this;
if ( show_alert ) {
TAlertManager.showConfirmAlert( $.i18n._( 'Would you like to pre-populate all date fields based on the first start date' ), '', function( flag ) {
if ( flag ) {
doNext();
}
} );
} else {
doNext();
}
function doNext() {
var type_id = $this.stepsWidgetDic[3].type_id.getValue();
var grid = $this.stepsWidgetDic[3].example_dates;
var data = grid.getData();
var first_date = data[0].start_date;
var api = TTAPI.APIPayPeriodSchedule;
api.detectPayPeriodScheduleDates( type_id, first_date, {
onResult: function( result ) {
var res_data = result.getResult();
grid.setData( res_data );
$this.setStep3CellDatePickers( grid );
}
} );
}
}
getGridColumns( gridId, callBack ) {
var column_info_array = [];
var $this = this;
switch ( gridId ) {
case 'example_dates':
var column_info = {
name: 'start_date',
index: 'start_date',
label: $.i18n._( 'Start Date' ),
width: 100,
sortable: false,
title: false,
formatter: function( cell_value, related_data, row ) {
return $this.onTextInputRender( cell_value, related_data, row );
}
};
column_info_array.push( column_info );
column_info = {
name: 'end_date',
index: 'end_date',
label: $.i18n._( 'End Date' ),
width: 100,
sortable: false,
title: false,
formatter: function( cell_value, related_data, row ) {
return $this.onTextInputRender( cell_value, related_data, row );
}
};
column_info_array.push( column_info );
column_info = {
name: 'transaction_date',
index: 'transaction_date',
label: $.i18n._( 'Transaction Date' ),
width: 100,
sortable: false,
title: false,
formatter: function( cell_value, related_data, row ) {
return $this.onTextInputRender( cell_value, related_data, row );
}
};
column_info_array.push( column_info );
break;
}
callBack( column_info_array );
}
buildCurrentStepData() {
var $this = this;
var current_step_data = this.stepsDataDic[this.current_step];
var current_step_ui = this.stepsWidgetDic[this.current_step];
var api_return_count = 0;
switch ( this.current_step ) {
case 2:
Global.setWidgetEnabled( this.next_btn, false );
Global.setWidgetEnabled( this.back_btn, false );
var api_user_preference = TTAPI.APIUserPreference;
var api_current_user = TTAPI.APIAuthentication;
//Time ZOne
api_user_preference.getOptions( 'time_zone', {
onResult: function( result ) {
current_step_ui['time_zone'].setSourceData( Global.buildRecordArray( result.getResult() ) );
if ( current_step_data ) {
for ( var key in current_step_data ) {
if ( !current_step_data.hasOwnProperty( key ) ) {
continue;
}
current_step_ui[key].setValue( current_step_data[key] );
}
}
step2OptionsCallBack();
}
} );
//Time Format
api_user_preference.getOptions( 'time_format', {
onResult: function( result ) {
current_step_ui['time_format'].setSourceData( Global.buildRecordArray( result.getResult() ) );
if ( current_step_data ) {
for ( var key in current_step_data ) {
if ( !current_step_data.hasOwnProperty( key ) ) {
continue;
}
current_step_ui[key].setValue( current_step_data[key] );
}
}
step2OptionsCallBack();
}
} );
//Calendar Starts On
api_user_preference.getOptions( 'start_week_day', {
onResult: function( result ) {
current_step_ui['start_week_day'].setSourceData( Global.buildRecordArray( result.getResult() ) );
if ( current_step_data ) {
for ( var key in current_step_data ) {
if ( !current_step_data.hasOwnProperty( key ) ) {
continue;
}
current_step_ui[key].setValue( current_step_data[key] );
}
}
step2OptionsCallBack();
}
} );
api_current_user.getCurrentUserPreference( {
onResult: function( result ) {
var res_data = result.getResult();
setStep2Values( res_data );
}
} );
break;
case 3:
var api_pp_schedule = TTAPI.APIPayPeriodSchedule;
api_pp_schedule.getOptions( 'type', {
onResult: function( result ) {
var res_data = Global.buildRecordArray( result.getResult() );
res_data.splice( 0, 1 );
current_step_ui['type_id'].setSourceData( res_data );
if ( current_step_data ) {
var array = current_step_data['example_dates'];
var grid = current_step_ui['example_dates'];
grid.setData( array );
$this.setStep3CellDatePickers( grid );
current_step_ui['type_id'].setValue( current_step_data['type_id'] );
} else {
buildStep3EmptySource();
}
}
} );
break;
case 4:
$this.initInsideEditorData();
break;
}
function step2OptionsCallBack() {
api_return_count = api_return_count + 1;
if ( api_return_count === 4 ) {
Global.setWidgetEnabled( $this.next_btn, true );
Global.setWidgetEnabled( $this.back_btn, true );
}
}
function setStep2Values( user_preference ) {
var t = current_step_ui['time_zone'];
var d = current_step_ui['date_format'];
var tf = current_step_ui['time_format'];
var s = current_step_ui['start_week_day'];
if ( user_preference.language === 'en' ) {
api_user_preference.getOptions( 'date_format', {
onResult: function( result ) {
current_step_ui['date_format'].setSourceData( Global.buildRecordArray( result.getResult() ) );
if ( current_step_data ) {
for ( var key in current_step_data ) {
if ( !current_step_data.hasOwnProperty( key ) ) {
continue;
}
current_step_ui[key].setValue( current_step_data[key] );
}
} else {
t.setValue( user_preference.time_zone );
d.setValue( user_preference.date_format );
tf.setValue( user_preference.time_format );
s.setValue( user_preference.start_week_day );
}
}
} );
} else {
api_user_preference.getOptions( 'other_date_format', {
onResult: function( result ) {
current_step_ui['date_format'].setSourceData( Global.buildRecordArray( result.getResult() ) );
if ( current_step_data ) {
for ( var key in current_step_data ) {
if ( !current_step_data.hasOwnProperty( key ) ) {
continue;
}
current_step_ui[key].setValue( current_step_data[key] );
}
} else {
t.setValue( user_preference.time_zone );
d.setValue( user_preference.date_format );
tf.setValue( user_preference.time_format );
s.setValue( user_preference.start_week_day );
}
}
} );
}
step2OptionsCallBack();
}
function buildStep3EmptySource() {
var array = [
{ start_date: '', end_date: '', transaction_date: '' },
{ start_date: '', end_date: '', transaction_date: '' },
{ start_date: '', end_date: '', transaction_date: '' },
{ start_date: '', end_date: '', transaction_date: '' }
];
var grid = current_step_ui['example_dates'];
grid.setData( array );
$this.setStep3CellDatePickers( grid );
}
}
setStep3CellDatePickers( grid ) {
var inputs = grid.grid.find( 'div[custom_cell="true"]' );
var $this = this;
for ( var i = 0; i < inputs.length; i++ ) {
var input = $( inputs[i] ).TDatePicker( { width: 290 } );
input.bind( 'formItemChange', function( e, target ) {
$this.onStep3DatePickerChange( target );
} );
}
}
onDoneClick() {
var $this = this;
super.onDoneClick();
$( $this.el ).remove();
LocalCacheData.current_open_wizard_controllers = LocalCacheData.current_open_wizard_controllers.filter( wizard => wizard.wizard_id !== this.wizard_id );
IndexViewController.goToView( 'Employee' );
}
insideEditorGetValue() {
var len = this.rows_widgets_array.length;
var result = [];
for ( var i = 0; i < len; i++ ) {
var row = this.rows_widgets_array[i];
var data = {
country: row.country.getValue(),
province: row.province.getValue()
};
result.push( data );
}
return result;
}
onNextClick() {
var $this = this;
this.saveCurrentStep( 'forward', function( result ) {
Global.setWidgetEnabled( $this.next_btn, true );
Global.setWidgetEnabled( $this.back_btn, true );
if ( result ) {
$this.current_step = $this.current_step + 1;
$this.initCurrentStep();
}
} );
}
onBackClick() {
var $this = this;
this.saveCurrentStep( 'back', function( result ) {
Global.setWidgetEnabled( $this.next_btn, true );
Global.setWidgetEnabled( $this.back_btn, true );
if ( result ) {
$this.current_step = $this.current_step - 1;
$this.initCurrentStep();
}
} );
}
saveCurrentStep( direction, callBack ) {
this.stepsDataDic[this.current_step] = {};
var current_step_data = this.stepsDataDic[this.current_step];
var current_step_ui = this.stepsWidgetDic[this.current_step];
switch ( this.current_step ) {
case 1:
callBack( true );
break;
case 2:
Global.setWidgetEnabled( this.next_btn, false );
Global.setWidgetEnabled( this.back_btn, false );
current_step_data.time_zone = current_step_ui.time_zone.getValue();
current_step_data.time_format = current_step_ui.time_format.getValue();
current_step_data.date_format = current_step_ui.date_format.getValue();
current_step_data.start_week_day = current_step_ui.start_week_day.getValue();
if ( direction === 'forward' ) {
var api_current_user = TTAPI.APIAuthentication;
var api_user_preference = TTAPI.APIUserPreference;
api_current_user.getCurrentUserPreference( {
onResult: function( result ) {
var res_data = result.getResult();
res_data.date_format = current_step_data.date_format;
res_data.time_format = current_step_data.time_format;
res_data.time_zone = current_step_data.time_zone;
res_data.start_week_day = current_step_data.start_week_day;
api_user_preference.setUserPreference( res_data, {
onResult: function() {
Global.updateUserPreference( function() {
callBack( true );
} );
}
} );
}
} );
} else {
callBack( true );
}
break;
case 3:
Global.setWidgetEnabled( this.next_btn, false );
Global.setWidgetEnabled( this.back_btn, false );
current_step_data.type_id = current_step_ui.type_id.getValue();
current_step_data.example_dates = _.clone( current_step_ui.example_dates.getGridParam( 'data' ) );
if ( direction === 'forward' ) {
var api_pp_schedule = TTAPI.APIPayPeriodSchedule;
api_pp_schedule.detectPayPeriodScheduleSettings( current_step_data.type_id, current_step_data.example_dates, {
onResult: function( result ) {
result = result.getResult();
if ( result.hasOwnProperty( 'company_id' ) ) {
api_pp_schedule.setPayPeriodSchedule( result, {
onResult: function( result_1 ) {
if ( result_1.isValid() ) {
callBack( true );
} else {
TAlertManager.showErrorAlert( result_1 );
callBack( false );
}
}
} );
} else {
callBack( true );
}
}
} );
} else {
callBack( true );
}
break;
case 4:
Global.setWidgetEnabled( this.next_btn, false );
Global.setWidgetEnabled( this.back_btn, false );
current_step_data.country = current_step_ui.country.getValue();
current_step_data.legal_entity_id = null;
var selected_legal_entities = current_step_ui.legal_entity_id.getValue( true );
if ( selected_legal_entities && selected_legal_entities.length > 0 ) {
current_step_data.legal_entity_id = [];
for ( var n in selected_legal_entities ) {
current_step_data.legal_entity_id.push( selected_legal_entities[n].id );
}
}
if ( direction === 'forward' ) {
var company_api = TTAPI.APISetupPresets;
company_api.createPresets( current_step_data.country, current_step_data.legal_entity_id, {
onResult: function( result ) {
if ( result.isValid ) {
callBack( true );
} else {
TAlertManager.showErrorAlert( result );
callBack( false );
}
}
} );
} else {
callBack( true );
}
break;
case 5:
callBack( true );
break;
}
}
}