import TTVueUtils from '@/services/TTVueUtils';
import ScheduleControlBar from '@/components/schedule/ScheduleControlBar';
export class ScheduleViewController extends BaseViewController {
constructor( options = {} ) {
_.defaults( options, {
el: '#schedule_view_container', //Must set el here and can only set string, so events can work
user_group_api: null,
status_array: null,
user_group_array: null,
toggle_button: null,
start_date_picker: null,
start_date: null,
end_date: null,
full_schedule_data: null,
schedule_columns: null,
full_format: 'ddd-MMM-DD-YYYY',
weekly_format: 'ddd, MMM DD',
final_schedule_data_array: [],
has_date_array: [],
no_date_array: [],
shift_key_name_array: [],
select_cells_Array: [], //Timesheet grid
select_all_shifts_array: [], //Timesheet grid.
select_shifts_array: [], //Timesheet grid.
select_recurring_shifts_array: [], //Timesheet grid.
all_employee_btn: null,
daily_totals_btn: null,
weekly_totals_btn: null,
strict_range_btn: null,
month_date_row_array: null,
month_date_row_tr_ids: null, // month date tr id in grid table
month_date_row_position: null, //month date tr position in table
month_current_header_number: 0, //0 is default column header
day_header_width: 0,
day_hour_width: 40,
select_drag_menu_id: '', //Do drag move or copy
is_override: false,
notify_user_schedule_change: false,
scroll_position: 0,
selected_user_ids: [],
prev_total_time: 0,
prev_status_id: null,
is_mass_adding: false,
calculate_cell_number: 0,
scroll_interval: null,
scroll_unit: 0,
holiday_data_dic: {},
absence_policy_api: null,
job_api: null,
job_item_api: null,
user_api: null,
department_api: null,
punch_tag_api: null,
default_punch_tag: [],
previous_punch_tag_selection: [],
year_mode_original_date: null, //set this when search for yer mode with use_date_picker true, so Keep select date in ritict mode
events: {},
vue_control_bar_id: ''
} );
super( options );
}
init( options ) {
//this._super('initialize', options );
this.permission_id = 'schedule';
this.script_name = 'ScheduleView';
this.viewId = 'Schedule';
this.table_name_key = 'schedule';
this.context_menu_name = $.i18n._( 'Schedules' );
this.navigation_label = $.i18n._( 'Schedule' );
this.api = TTAPI.APISchedule;
this.user_group_api = TTAPI.APIUserGroup;
this.absence_policy_api = TTAPI.APIAbsencePolicy;
this.company_api = TTAPI.APICompany;
this.user_api = TTAPI.APIUser;
this.currency_api = TTAPI.APICurrency;
this.event_bus = new TTEventBus({ view_id: this.viewId });
if ( Global.getProductEdition() >= 20 ) {
this.job_api = TTAPI.APIJob;
this.job_item_api = TTAPI.APIJobItem;
this.punch_tag_api = TTAPI.APIPunchTag;
this.department_api = TTAPI.APIDepartment;
}
this.api_absence_policy = TTAPI.APIAbsencePolicy;
this.scroll_position = 0;
this.initPermission();
this.render();
this.buildContextMenu();
this.initData();
}
jobUIValidate( p_id ) {
if ( !p_id ) {
p_id = this.permission_id;
}
if ( PermissionManager.validate( 'job', 'enabled' ) &&
PermissionManager.validate( p_id, 'edit_job' ) &&
( Global.getProductEdition() >= 20 ) ) {
return true;
}
return false;
}
jobItemUIValidate( p_id ) {
if ( !p_id ) {
p_id = this.permission_id;
}
if ( PermissionManager.validate( 'job_item', 'enabled' ) &&
PermissionManager.validate( p_id, 'edit_job_item' ) ) {
return true;
}
return false;
}
punchTagUIValidate( p_id ) {
if ( !p_id ) {
p_id = this.permission_id;
}
if ( PermissionManager.validate( 'punch_tag', 'enabled' ) &&
PermissionManager.validate( p_id, 'edit_job_item' ) ) {
return true;
}
return false;
}
branchUIValidate( p_id ) {
if ( !p_id ) {
p_id = this.permission_id;
}
if ( PermissionManager.validate( p_id, 'edit_branch' ) ) {
return true;
}
return false;
}
departmentUIValidate( p_id ) {
if ( !p_id ) {
p_id = this.permission_id;
}
if ( PermissionManager.validate( p_id, 'edit_department' ) ) {
return true;
}
return false;
}
unscheduledEmployeeUIValidate() {
if ( PermissionManager.validate( 'schedule', 'view' ) || PermissionManager.validate( 'schedule', 'view_child' ) ) {
return true;
}
return false;
}
//Speical permission check for views, need override
initPermission() {
super.initPermission();
this.show_search_tab = true;
//See buildSearchFields() for additional permission checks.
// if ( PermissionManager.validate( this.permission_id, 'view' ) || PermissionManager.validate( this.permission_id, 'view_child' ) ) {
// this.show_search_tab = true;
// } else {
// this.show_search_tab = false;
// }
if ( this.jobUIValidate() ) {
this.show_job_ui = true;
} else {
this.show_job_ui = false;
}
if ( this.jobItemUIValidate() ) {
this.show_job_item_ui = true;
} else {
this.show_job_item_ui = false;
}
if ( this.punchTagUIValidate() ) {
this.show_punch_tag_ui = true;
} else {
this.show_punch_tag_ui = false;
}
if ( this.branchUIValidate() ) {
this.show_branch_ui = true;
} else {
this.show_branch_ui = false;
}
if ( this.departmentUIValidate() ) {
this.show_department_ui = true;
} else {
this.show_department_ui = false;
}
}
//only be call once when open this view
initData() {
var $this = this;
//Remove tab if any
Global.removeViewTab( this.viewId );
ProgressBar.showOverlay();
this.initOptions();
//For regular employees who currently can't see the "Saved Search and Layout" tab, try to be smarter about what columns they do see by default.
this.default_display_columns = [];
if ( PermissionManager.validate( this.permission_id, 'edit_branch' ) ) {
this.default_display_columns.push( 'branch' );
}
if ( PermissionManager.validate( this.permission_id, 'edit_department' ) ) {
this.default_display_columns.push( 'department' );
}
if ( Global.getProductEdition() >= 20 ) {
if ( PermissionManager.validate( this.permission_id, 'edit_job' ) ) {
this.default_display_columns.push( 'job' );
}
if ( PermissionManager.validate( this.permission_id, 'edit_job_item' ) ) {
this.default_display_columns.push( 'job_item' );
}
}
var date = new Date();
if ( Global.UNIT_TEST_MODE == true ) {
LocalCacheData.last_schedule_selected_date = '15-Feb-18';
}
var format = Global.getLoginUserDateFormat();
var dateStr = date.format( format );
if ( !LocalCacheData.last_schedule_selected_date ) {
if ( LocalCacheData.current_select_date && Global.strToDate( LocalCacheData.current_select_date, 'YYYY-MM-DD' ) ) { //Select date get from URL.
this.setDatePickerValue( Global.strToDate( LocalCacheData.current_select_date, 'YYYY-MM-DD' ).format() );
LocalCacheData.current_select_date = '';
} else {
this.setDatePickerValue( dateStr );
}
} else {
this.setDatePickerValue( LocalCacheData.last_schedule_selected_date );
}
this.setMoveOrDropMode( this.select_drag_menu_id ? this.select_drag_menu_id : 'move' );
//Issue #3280 - Race condition where previous search layout will not exist in UI when expected as API for custom field data has not returned yet.
//ScheduleViewController overrides initData() from BaseViewController that waits on the custom field promise to resolve before continuing.
TTPromise.wait( 'BaseViewController', 'getCustomFields', function() {
$this.getAllColumns( function() {
$this.initLayout();
} );
} );
}
setToggleButtonValue( val ) {
if ( this.toggle_button ) {
this.toggle_button.setValue( val );
this.setToggleButtonUrl();
}
}
setToggleButtonUrl() {
var mode = this.getMode();
var default_date = this.start_date_picker.getDefaultFormatValue();
if ( !this.edit_view ) {
Global.setURLToBrowser( Global.getBaseURL() + '#!m=' + this.viewId + '&mode=' + mode );
}
}
setDatePickerValue( val ) {
this.start_date_picker.setValue( val );
//this.setDateUrl();
LocalCacheData.last_schedule_selected_date = val;
}
getAllColumns( callBack ) {
var $this = this;
this.api.getOptions( 'columns', {
onResult: function( columns_result ) {
var columns_result_data = columns_result.getResult();
$this.all_columns = Global.buildColumnArray( columns_result_data );
$this.api.getOptions( 'group_columns', {
onResult: function( group_columns_result ) {
var all_columns = Global.buildColumnArray( columns_result_data );
var group_columns_result_data = group_columns_result.getResult();
var final_all_columns = [];
var all_len = all_columns.length;
var group_len = group_columns_result_data.length;
for ( var i = 0; i < group_len; i++ ) {
var group_column_id = group_columns_result_data[i];
for ( var j = 0; j < all_len; j++ ) {
var column = all_columns[j];
if ( column.value === group_column_id ) {
final_all_columns.push( column );
break;
}
}
}
$this.all_columns = final_all_columns;
$this.column_selector.setUnselectedGridData( $this.all_columns );
if ( callBack ) {
callBack();
}
}
} );
}
} );
}
initLayout() {
var $this = this;
$this.getAllLayouts( function() {
$this.setSelectLayout();
$this.search( true, false ); //Make sure we setDefaultMenu is TRUE so autoOpenEditViewIfNecessary() is called.
} );
}
initOptions() {
var $this = this;
this.initDropDownOption( 'status', '', this.api );
this.user_group_api.getUserGroup( '', false, false, {
onResult: function( res ) {
res = res.getResult();
res = Global.buildTreeRecord( res );
$this.user_group_array = res;
if ( $this.basic_search_field_ui_dic['group_ids'] ) {
$this.basic_search_field_ui_dic['group_ids'].setSourceData( res );
}
if ( $this.adv_search_field_ui_dic['group_ids'] ) {
$this.adv_search_field_ui_dic['group_ids'].setSourceData( res );
}
}
} );
}
getSelectDate() {
var retval = this.start_date_picker.getValue();
if ( retval == 'Invalid date' ) {
retval = new Date();
}
return retval;
}
getGridSelectIdArray() {
var result = [];
var len = this.select_all_shifts_array.length;
for ( var i = 0; i < len; i++ ) {
var item = this.select_all_shifts_array[i];
if ( item.id && item.id != TTUUID.zero_id && item.id != TTUUID.not_exist_id ) {
result.push( item.id );
}
}
return result;
}
getSelectedItem() {
var selected_item = null;
if ( this.edit_view ) {
selected_item = this.current_edit_record;
} else {
if ( this.select_all_shifts_array.length > 0 ) {
selected_item = this.select_all_shifts_array[0];
}
}
if ( selected_item ) {
return Global.clone( selected_item );
} else {
return null;
}
}
_continueDoCopyAsNew() {
var $this = this;
this.setCurrentEditViewState( 'new' );
LocalCacheData.current_doing_context_action = 'copy_as_new';
if ( Global.isSet( this.edit_view ) ) {
this.current_edit_record.id = '';
var navigation_div = this.edit_view.find( '.navigation-div' );
navigation_div.css( 'display', 'none' );
this.setCurrentEditRecordData(); // Reset data to widgets to reset all widgets stat
this.setEditMenu();
this.setTabStatus();
this.is_changed = false;
ProgressBar.closeOverlay();
} else {
var filter = {};
var grid_selected_id_array = this.getGridSelectIdArray();
var grid_selected_length = grid_selected_id_array.length;
if ( grid_selected_length > 0 ) {
var selectedId = grid_selected_id_array[0];
} else {
var select_shift = Global.clone( $this.select_all_shifts_array[0] );
select_shift = $this.resetSomeFields( select_shift );
$this.current_edit_record = select_shift;
$this.openEditView();
$this.initEditView();
return;
}
filter.filter_data = {};
filter.filter_data.id = [selectedId];
this.api['get' + this.api.key_name]( filter, {
onResult: function( result ) {
var result_data = result.getResult();
//#2571 - result_data is undefined (when result_data === true there is no result[0])
if ( !result_data || result_data === true ) {
TAlertManager.showAlert( $.i18n._( 'Record does not exist' ) );
$this.onCancelClick();
return;
}
$this.openEditView(); // Put it here is to avoid if the selected one is not existed in data or have deleted by other pragram. in this case, the edit view should not be opend.
result_data = result_data[0];
result_data.id = '';
if ( $this.sub_view_mode && $this.parent_key ) {
result_data[$this.parent_key] = $this.parent_value;
}
$this.current_edit_record = result_data;
$this.initEditView();
}
} );
}
}
onViewClick( record, noRefreshUI ) {
this.setCurrentEditViewState( 'view' );
var record_id = this.getViewSelectedRecordId( record );
if ( Global.isFalseOrNull( record_id ) ) {
TTPromise.add( 'Schedule', 'init' );
this.openEditView();
var select_shift = Global.clone( this.select_all_shifts_array[0] );
select_shift = this.resetSomeFields( select_shift );
this.current_edit_record = select_shift;
var $this = this;
TTPromise.wait( 'Schedule', 'init', function() {
$this.initEditView();
} );
return;
}
this.setCurrentSelectedRecord( record_id );
this.openEditView();
var filter = this.getAPIFilters();
return this.doViewAPICall( filter );
}
getCommonFields() {
var baseRecord;
$.each( this.select_all_shifts_array, function( index, value ) {
if ( !baseRecord ) {
baseRecord = Global.clone( value );
return true;
}
for ( var key in value ) {
baseRecord[key] !== value[key] && delete baseRecord[key];
}
} );
return baseRecord;
}
onMassEditClick() {
var $this = this;
var filter = {};
var grid_selected_id_array = [];
this.setCurrentEditViewState( 'mass_edit' );
this.mass_edit_record_ids = [];
grid_selected_id_array = this.getGridSelectIdArray();
$this.openEditView();
$.each( grid_selected_id_array, function( index, value ) {
$this.mass_edit_record_ids.push( value );
} );
$this.selected_user_ids = [];
$.each( this.select_all_shifts_array, function( index, value ) {
var shift = value;
if ( shift.hasOwnProperty( 'user_id' ) ) {
$this.selected_user_ids.push( shift.user_id );
}
} );
filter.filter_data = {};
filter.filter_data.id = this.mass_edit_record_ids;
if ( this.mass_edit_record_ids.length !== this.select_all_shifts_array.length ) {
onMassEditResult( this.getCommonFields() );
return;
}
this.api['getCommon' + this.api.key_name + 'Data']( filter, {
onResult: function( result ) {
var result_data = result.getResult();
if ( !result_data ) {
result_data = [];
}
onMassEditResult( result_data );
}
} );
function onMassEditResult( result_data ) {
$this.api['getOptions']( 'unique_columns', {
onResult: function( result ) {
$this.unique_columns = result.getResult();
$this.api['getOptions']( 'linked_columns', {
onResult: function( result1 ) {
$this.linked_columns = result1.getResult();
if ( $this.sub_view_mode && $this.parent_key ) {
result_data[$this.parent_key] = $this.parent_value;
}
$this.current_edit_record = result_data;
$this.is_mass_editing = true;
$this.initEditView();
}
} );
}
} );
}
}
onEditClick( record_id, noRefreshUI ) {
// #2644: Note: This code fixes a bug where the edit screen is not opened on first try, but is on second. Likely due to the TTPromise and potential race conditions. See commit history for details.
TTPromise.add( 'Schedule', 'init' );
this.setCurrentEditViewState( 'edit' );
this.openEditView();
record_id = this.getEditSelectedRecordId( record_id );
if ( Global.isFalseOrNull( record_id ) ) {
this.openEditView();
var select_shift = Global.clone( this.select_all_shifts_array[0] );
select_shift = this.resetSomeFields( select_shift );
this.current_edit_record = select_shift;
this.current_edit_record.user_ids = [this.current_edit_record.user_id]; //#2610 - ensure that edit record is properly formed in respect to user_ids
this.is_viewing = false;
var $this = this;
TTPromise.wait( 'Schedule', 'init', function() {
$this.initEditView();
} );
return;
}
this.setCurrentSelectedRecord( record_id );
var filter = this.getAPIFilters();
return this.doEditAPICall( filter );
}
doDeleteAPICall( remove_ids, callback ) {
if ( !callback ) {
callback = {
onResult: function( result ) {
ProgressBar.closeOverlay();
doNext.call( this, result );
if ( result.isValid() ) {
this.onDeleteDone( result );
if ( this.edit_view ) {
this.removeEditView();
} else {
this.setCurrentEditViewState( '' );
}
} else {
TAlertManager.showErrorAlert( result );
}
}.bind( this )
};
}
if ( remove_ids && remove_ids.length > 0 ) {
return this.api['delete' + this.api.key_name]( remove_ids, callback );
} else {
doNext.call( this, {
isValid: function() {
return false;
}
} );
}
function doNext( result ) {
//Issue #3034 - TypeError: Cannot read properties of undefined (reading 'id').
//Because deleting recurring schedules does not go through the normal pathway, we need to reset state
//so that Vue does not decide we on an edit view and retrieve the wrong context menu array.
//Otherwise JavaScript exceptions may trigger when reading an empty context menu array during other actions.
if ( this.edit_view ) {
this.removeEditView();
} else {
this.setCurrentEditViewState( '' );
}
//Since we can't delete recurring schedules, we need to override them as absent without a absence policy instead.
var recurring_delete_shifts_array = [];
for ( var i = 0; i < this.select_cells_Array.length; i++ ) {
if ( this.select_cells_Array[i].shift ) {
this.select_cells_Array[i].shift.status_id = '20'; //Set shift to absent.
recurring_delete_shifts_array.push( this.select_cells_Array[i].shift );
}
}
if ( recurring_delete_shifts_array.length > 0 ) {
this.api.setSchedule( recurring_delete_shifts_array, {
onResult: function() {
this.search();
}.bind( this )
} );
} else {
if ( result.isValid() ) {
this.search();
}
}
}
}
getCustomContextMenuModel() {
var context_menu_model = {
groups: {
drag_and_drop: {
label: $.i18n._( 'Drag & Drop' ),
id: this.viewId + 'drag_and_drop'
}
},
exclude: [
'export_excel',
'save_and_next',
'delete_and_next',
'copy'
],
include: [
// Note Drag&Drop icons will be conditionally prepended here later in code for list view modes.
{
label: $.i18n._( 'Jump To' ),
id: 'jump_to_header',
menu_align: 'right',
action_group: 'jump_to',
action_group_header: true,
permission_result: false, // to hide it in legacy context menu and avoid errors in legacy parsers.
sort_order: 9030
},
{
label: $.i18n._( 'TimeSheet' ),
id: 'timesheet',
menu_align: 'right',
action_group: 'jump_to',
group: 'navigation',
permission_result: true,
sort_order: 9030
},
{
label: $.i18n._( 'Edit Employee' ),
id: 'edit_employee',
menu_align: 'right',
action_group: 'jump_to',
group: 'navigation',
permission_result: true,
sort_order: 9030
},
{
label: $.i18n._( 'Import' ),
id: 'import_icon',
menu_align: 'right',
action_group: 'import_export',
group: 'other',
vue_icon: 'tticon tticon-file_download_black_24dp',
permission_result: PermissionManager.checkTopLevelPermission( 'ImportCSVSchedule' ),
sort_order: 9025
}
]
};
// Edit Views
context_menu_model.include.push( {
label: $.i18n._( 'Print' ),
id: 'print',
action_group_header: true,
action_group: 'print',
menu_align: 'right',
sort_order: 9020,
} );
context_menu_model.include.push( {
label: $.i18n._( 'Individual Schedules' ),
id: 'pdf_schedule',
action_group: 'print',
menu_align: 'right',
sort_order: 9020
} );
context_menu_model.include.push(
{
label: $.i18n._( 'Find Available' ),
id: 'find_available',
vue_icon: 'tticon tticon-search_black_24dp',
menu_align: 'right',
permission_result: true,
show_on_right_click: true,
sort_order: 7000
}
);
if ( PermissionManager.validate( 'request', 'add' ) ) {
context_menu_model.include.push( {
label: $.i18n._( 'Add Request' ),
id: 'AddRequest',
vue_icon: 'tticon tticon-post_add_black_24dp',
menu_align: 'right',
permission_result: true,
permission: true,
show_on_right_click: true,
sort_order: 7010
} );
}
if ( !this.is_edit && !this.is_mass_editing && !this.is_viewing ) {
context_menu_model.include.unshift( // Add to begginning of array to preserve item order.
{
label: $.i18n._( 'Drag & Drop: Move' ),
id: 'move',
action_group: 'drag_and_drop',
multi_select_group: 1,
menu_align: 'right',
group: 'drag_and_drop',
permission_result: true,
sort_order: 8050
},
{
label: $.i18n._( 'Drag & Drop: Copy' ),
id: 'drag_copy',
action_group: 'drag_and_drop',
menu_align: 'right',
multi_select_group: 1,
group: 'drag_and_drop',
permission_result: true,
sort_order: 8060
},
{
label: $.i18n._( 'Drag & Drop: Swap' ),
id: 'swap',
action_group: 'drag_and_drop',
menu_align: 'right',
multi_select_group: 1,
group: 'drag_and_drop',
permission_result: true,
sort_order: 8070
},
{
id: 'separator_drag_and_drop_1',
action_group: 'drag_and_drop',
menu_align: 'right',
separator: true,
sort_order: 8080
},
{
label: $.i18n._( 'Drag & Drop: Overwrite' ),
id: 'override',
action_group: 'drag_and_drop',
multi_select_group: 2,
menu_align: 'right',
group: 'drag_and_drop',
permission_result: true,
sort_order: 8090
},
);
if ( Global.getProductEdition() >= 15 ) {
context_menu_model.include.unshift(
{
label: $.i18n._( 'Drag & Drop: Notify Employee' ),
id: 'notify_user_schedule_change',
action_group: 'drag_and_drop',
multi_select_group: 3,
menu_align: 'right',
group: 'drag_and_drop',
permission_result: true,
sort_order: 8100
} );
}
}
if ( PermissionManager.validate( 'schedule', 'view' ) || PermissionManager.validate( 'schedule', 'view_child' ) ) {
context_menu_model.include.push(
{
label: $.i18n._( 'Group - Combined' ),
id: 'pdf_schedule_group_combined',
action_group: 'print',
menu_align: 'right',
sort_order: 9020,
},
{
label: $.i18n._( 'Group - Separated' ),
id: 'pdf_schedule_group',
action_group: 'print',
menu_align: 'right',
sort_order: 9020,
},
{
label: $.i18n._( 'Group - Separated (Page Breaks)' ),
id: 'pdf_schedule_group_pagebreak',
action_group: 'print',
menu_align: 'right',
sort_order: 9020,
}
);
}
context_menu_model.include.push(
);
if ( ( PermissionManager.validate( 'punch', 'add' ) && ( PermissionManager.validate( 'punch', 'edit' ) || PermissionManager.validate( 'punch', 'edit_child' ) ) ) ) {
context_menu_model.include.push(
{
label: '', //Empty label. vue_icon is displayed instead of text.
id: 'other_header',
action_group: 'other',
menu_align: 'right',
action_group_header: true,
vue_icon: 'tticon tticon-more_vert_black_24dp',
sort_order: 9050
},
{
label: $.i18n._( 'Auto Punch' ),
id: 'AutoPunch',
action_group: 'other',
menu_align: 'right',
group: 'other',
permission_result: true,
permission: true,
show_on_right_click: true,
sort_order: 9050
} );
}
return context_menu_model;
}
parseCustomContextModelForEditViews( context_menu_model ) {
context_menu_model = super.parseCustomContextModelForEditViews( context_menu_model );
if ( this.determineContextMenuMountAttributes().menu_type === 'editview_contextmenu' ) {
context_menu_model.exclude.push(
'move',
'drag_copy',
'swap',
'override',
'notify_user_schedule_change',
'print',
'pdf_schedule',
'pdf_schedule_group_combined',
'pdf_schedule_group',
'pdf_schedule_group_pagebreak'
);
}
return context_menu_model;
}
onReportMenuClick( id ) {
this.onNavigationClick( id );
}
onCustomContextClick( id ) {
switch ( id ) {
case 'move':
case 'drag_copy':
case 'swap':
this.setMoveOrDropMode( id );
break;
case 'override':
this.onOverrideClick();
break;
case 'notify_user_schedule_change':
this.onNotifyUserScheduleChangeClick();
break;
case 'edit_employee':
case 'timesheet':
this.onNavigationClick( id );
break;
case 'find_available':
this.onFindAvailableClick( id );
break;
case 'AutoPunch':
this.addPunchesFromScheduledShifts( id );
break;
case 'AddRequest':
this.addRequestFromScheduledShifts( id );
break;
case 'import_icon':
this.onImportClick();
break;
case 'pdf_schedule':
case 'pdf_schedule_group':
case 'pdf_schedule_group_combined':
case 'pdf_schedule_group_pagebreak':
this.onReportMenuClick( id );
break;
}
}
addRequestFromScheduledShifts( id ) {
if ( Global.getProductEdition() <= 10 ) {
TAlertManager.showAlert( Global.getUpgradeMessage() );
return false;
}
if ( shift_array && shift_array.length <= 0 ) {
return false;
}
var shift_array = this.select_cells_Array;
var first_shift = ( shift_array[0] ) ? shift_array[0] : null;
var last_shift = ( shift_array[shift_array.length - 1] ) ? shift_array[shift_array.length - 1] : null;
if ( !first_shift || !last_shift ) {
return false;
}
var request = this.api.getScheduleDefaultData( this.select_cells_Array, { async: false } ).getResult();
var shift_status = 10;
var type_id = 40;
var mon = false, tue = false, wed = false, thu = false, fri = false, sat = false, sun = false;
for ( var w in shift_array ) {
if ( first_shift.shift == undefined && shift_array[w].shift ) {
//Set the archetype to the first day with a shift.
first_shift = shift_array[w];
}
//Set selected days of the week.
var d = new Date( shift_array[w].time_stamp_num );
switch ( d.getDay() ) {
case 0:
sun = true;
break;
case 1:
mon = true;
break;
case 2:
tue = true;
break;
case 3:
wed = true;
break;
case 4:
thu = true;
break;
case 5:
fri = true;
break;
case 6:
sat = true;
break;
}
// delete ( d ); #2792 In the new ES6 conversion, delete is causing errors, but technically should never have worked anyway. Potentially never did anything. Could maybe delete. Try null first.
d = null;
}
request.mon = mon;
request.tue = tue;
request.wed = wed;
request.thu = thu;
request.fri = fri;
request.sat = sat;
request.sun = sun;
if ( first_shift && first_shift.date ) {
var start_date = first_shift.date;
}
if ( last_shift && last_shift.date ) {
var end_date = last_shift.date;
}
if ( first_shift ) {
if ( first_shift.shift && first_shift.shift.status_id == 10 && first_shift.shift.user_id && first_shift.shift.user_id != TTUUID.zero_id ) {
shift_status = 20;
type_id = 30;
}
}
request.status_id = shift_status;
request.type_id = type_id;
request.user_id = LocalCacheData.getLoginUser().id;
request.full_name = LocalCacheData.getLoginUser().full_name;
if ( start_date ) {
request.date_stamp = start_date;
request.start_date = start_date;
}
if ( end_date ) {
request.end_date = end_date;
}
if ( first_shift.start_time ) {
request.start_time = first_shift.start_time;
}
if ( first_shift.end_time ) {
request.end_time = first_shift.end_time;
}
if ( first_shift.branch_id ) {
request.branch_id = first_shift.branch_id;
}
if ( first_shift.department_id ) {
request.department_id = first_shift.department_id;
}
if ( first_shift.job_id ) {
request.job_id = first_shift.job_id;
}
if ( first_shift.job_item_id ) {
request.job_item_id = first_shift.job_item_id;
}
if ( first_shift.punch_tag_id ) {
request.punch_tag_id = first_shift.punch_tag_id;
}
IndexViewController.openEditView( this, 'Request', request, 'openAddView' );
}
addPunchesFromScheduledShifts( id ) {
if ( Global.getProductEdition() <= 10 ) {
TAlertManager.showAlert( Global.getUpgradeMessage() );
return false;
}
if ( this.select_cells_Array == undefined || this.select_cells_Array.length < 1 ) {
TAlertManager.showAlert( 'No schedules selected. You can\'t autopunch no schedules.' );
return false;
}
var shift_array = this.select_cells_Array;
var schedules = {};
var users = [];
schedules.schedule = [];
schedules.recurring = [];
for ( var i = 0; i < shift_array.length; i++ ) {
if ( shift_array[i].shift != undefined ) { //avoid when no user scheduled.
if ( shift_array[i].shift.id
&& shift_array[i].shift.id != TTUUID.zero_id
&& shift_array[i].shift.id != TTUUID.not_exist_id
) {
schedules.schedule.push( shift_array[i].shift.id );
} else if ( shift_array[i].shift.recurring_schedule_id
&& shift_array[i].shift.recurring_schedule_id != TTUUID.not_exist_id
) {
schedules.recurring.push( shift_array[i].shift.recurring_schedule_id );
}
users.push( shift_array[i].shift.user_id );
}
}
this.api.addPunchesFromScheduledShifts( schedules, {
onResult: function( result ) {
if ( result.isValid() ) {
UserGenericStatusWindowController.open( result.getAttributeInAPIDetails( 'user_generic_status_batch_id' ), [LocalCacheData.getLoginUser().id] );
} else {
TAlertManager.showErrorAlert( result );
}
}
} );
}
getSelectEmployee() {
var shift = this.select_cells_Array[0];
//Error: Uncaught TypeError: Cannot read property 'user_id' of undefined in /interface/html5/#!m=Schedule&date=20141117&mode=week&a=new&tab=Schedule line 1116
if ( !shift || shift.user_id == TTUUID.zero_id ) {
shift = { user_id: LocalCacheData.getLoginUser().id };
} else if ( shift.user_id && shift.user_id != TTUUID.zero_id ) {
shift = { user_id: shift.user_id };
}
if ( this.edit_view && this.current_edit_record ) {
shift.user_id = this.current_edit_record.user_id;
}
return shift.user_id;
}
onFindAvailableClick() {
if ( Global.getProductEdition() <= 10 ) {
TAlertManager.showAlert( Global.getUpgradeMessage() );
return;
}
var $this = this;
var args = {};
args.selected = [];
var len = this.select_all_shifts_array.length;
for ( var i = 0; i < len; i++ ) {
var item = this.select_all_shifts_array[i];
args.selected.push( item );
}
LocalCacheData.extra_filter_for_next_open_view = {};
LocalCacheData.extra_filter_for_next_open_view.filter_data = args;
IndexViewController.openWizard( 'FindAvailableWizard', null, function( employee_id, notify_users ) {
$this.onFindAvailableClose( employee_id, args.selected, notify_users );
} );
}
onFindAvailableClose( employee_id, shift_array, notify_users ) {
var $this = this;
var len = shift_array.length;
for ( var i = 0; i < len; i++ ) {
var item = shift_array[i];
item.user_id = employee_id;
item.replaced_id = item.id;
item.notify_user_schedule_change = notify_users;
delete item.id;
}
this.api.setSchedule( shift_array, {
onResult: function( result ) {
if ( !result.isValid() ) {
TAlertManager.showErrorAlert( result );
}
$this.search();
}
} );
}
onImportClick() {
var $this = this;
IndexViewController.openWizard( 'ImportCSVWizard', 'Schedule', function() {
$this.search();
} );
}
onNavigationClick( iconName ) {
if ( !this.checkScheduleData() ) {
return;
}
var post_data;
switch ( iconName ) {
case 'edit_employee':
IndexViewController.openEditView( this, 'Employee', this.getSelectEmployee() );
break;
case 'timesheet':
var filter = { filter_data: {} };
filter.user_id = this.getSelectEmployee();
if ( this.edit_view ) {
filter.base_date = this.current_edit_record.date_stamp;
} else {
filter.base_date = this.start_date_picker.getValue();
}
Global.addViewTab( this.viewId, $.i18n._( 'Schedules' ), window.location.href );
IndexViewController.goToView( 'TimeSheet', filter );
break;
case 'pdf_schedule':
case 'pdf_schedule_group_combined':
case 'pdf_schedule_group':
case 'pdf_schedule_group_pagebreak':
filter = Global.convertLayoutFilterToAPIFilter( this.select_layout );
if ( !filter ) {
filter = {};
}
filter.time_period = {};
filter.time_period.time_period = 'custom_date';
filter.time_period.start_date = this.full_schedule_data.schedule_dates.start_display_date;
filter.time_period.end_date = this.full_schedule_data.schedule_dates.end_display_date;
if ( filter.time_period.start_date == filter.time_period.end_date ) {
var new_end_date = new Date( new Date( this.start_date.getTime() ).setDate( this.start_date.getDate() + 6 ) );
filter.time_period.end_date = new_end_date.format();
}
post_data = { 0: filter, 1: iconName };
this.doFormIFrameCall( post_data );
break;
}
}
doFormIFrameCall( postData ) {
Global.APIFileDownload( 'APIScheduleSummaryReport', 'getScheduleSummaryReport', postData );
}
setScheduleGridDragAble() {
var mode = this.getMode();
switch ( mode ) {
case ScheduleViewControllerMode.DAY:
this.setWeekModeDragAble();
break;
case ScheduleViewControllerMode.WEEK:
this.setWeekModeDragAble();
break;
case ScheduleViewControllerMode.MONTH:
this.setWeekModeDragAble();
break;
case ScheduleViewControllerMode.YEAR:
this.setWeekModeDragAble();
break;
}
var $this = this;
//set bottom drag to scroll area
$( '.schedule-grid-div' ).off( 'dragover' ).on( 'dragover', function( e ) {
var grid_div = $( '.schedule-grid-div' );
var grid_pos = grid_div.offset().top;
var mouse_y = e.originalEvent.clientY;
var grid_height = grid_div.height();
if ( mouse_y > ( grid_pos + grid_height ) ) {
$this.scroll_unit = mouse_y - ( grid_pos + grid_height );
if ( !$this.scroll_interval ) {
$this.scroll_interval = setInterval( function() {
var div = $this.grid.grid.parent().parent();
div.scrollTop( div.scrollTop() + $this.scroll_unit );
}, 50 );
}
} else if ( mouse_y < ( grid_pos + 15 ) && mouse_y > ( grid_pos - 50 ) ) {
$this.scroll_unit = ( grid_pos + 15 ) - mouse_y;
if ( !$this.scroll_interval ) {
$this.scroll_interval = setInterval( function() {
var div = $this.grid.grid.parent().parent();
div.scrollTop( div.scrollTop() - $this.scroll_unit );
}, 50 );
}
} else {
clearInterval( $this.scroll_interval );
$this.scroll_interval = null;
}
} );
$( '.schedule-grid-div' ).off( 'dragend' ).on( 'dragend', function( e ) {
if ( $this.scroll_interval ) {
clearInterval( $this.scroll_interval );
$this.scroll_interval = null;
}
} );
$( '.schedule-grid-div td' ).unbind( 'dragenter' ).bind( 'dragenter', function( event ) {
event.preventDefault();
$( '.schedule-drag-over' ).removeClass( 'schedule-drag-over' );
if ( $( this ).attr( 'draggable' ) || $( this ).parents( 'td' ).attr( 'draggable' ) ) {
$( this ).addClass( 'schedule-drag-over' );
}
} );
}
setWeekModeDragAble() {
var $this = this;
var position = 0;
var cells = this.grid.grid.find( '.date-column' ).parents( 'td' );
cells.attr( 'draggable', true );
cells.unbind( 'dragstart' ).bind( 'dragstart', function( event ) {
var td = event.target;
if ( $this.select_all_shifts_array.length < 1 || !$( td ).hasClass( 'ui-state-highlight' ) || !$this.select_drag_menu_id ) {
return false;
}
var container = $( '
' );
var len = $this.select_all_shifts_array.length;
for ( var i = 0; i < len; i++ ) {
var shift = $this.select_all_shifts_array[i];
var span = $( '' );
if ( shift.status_id == 20 ) {
span.text( $this.getAbsenceCellValue( shift ) );
} else {
span.text( shift.start_time + ' - ' + shift.end_time );
}
container.append( span );
}
$( 'body' ).find( '.drag-holder-div' ).remove();
$( 'body' ).append( container );
event.originalEvent.dataTransfer.setData( 'Text', 'schedule' );//JUST ELEMENT references is ok here NO ID
if ( event.originalEvent.dataTransfer.setDragImage ) {
event.originalEvent.dataTransfer.setDragImage( container[0], 0, 0 );
}
return true;
} );
cells.unbind( 'drop' ).bind( 'drop', function( event ) {
event.preventDefault();
if ( event.stopPropagation ) {
event.stopPropagation(); // stops the browser from redirecting.
}
$( '.drag-holder-div' ).remove();
var target_empty_row = false;
var delete_old_items = false;
var new_shifts_array = [];
var delete_shifts_array = [];
var recurring_delete_shifts_array = [];
var target_cell = event.currentTarget;
var selected_shifts = $this.select_cellls_and_shifts_array;
//Error: Uncaught TypeError: Cannot read property 'length' of undefined in interface/html5/#!m=Schedule&date=20151213&mode=week line 1420
if ( !selected_shifts ) {
return;
}
var first_target_row_index;
var first_target_cell_index;
first_target_row_index = target_cell.parentNode.rowIndex - 1;
first_target_cell_index = target_cell.cellIndex;
var row_index_offset = 0;
var cell_index_offset = 0;
var first_selected_row_index;
var first_selected_cell_index;
var colModel = $this.grid.grid.getGridParam( 'colModel' );
if ( $this.select_drag_menu_id === 'move' ) {
delete_old_items = true;
} else {
delete_old_items = false;
}
var len = selected_shifts.length;
for ( var i = 0; i < len; i++ ) {
var cell = selected_shifts[i];
var shift;
if ( i === 0 ) {
first_selected_row_index = cell.row_id;
first_selected_cell_index = cell.cell_index;
} else {
if ( !target_empty_row ) {
row_index_offset = cell.row_id - first_selected_row_index;
}
cell_index_offset = cell.cell_index - first_selected_cell_index;
}
if ( cell.shift ) {
shift = cell.shift;
} else {
var target_row_index = first_target_row_index + row_index_offset;
var target_cell_index = first_target_cell_index + cell_index_offset;
if ( target_cell_index > colModel.length - 1 ) {
continue;
}
var target_data = $this.getDataByCellIndex( target_row_index, target_cell_index );
var target_row = $this.schedule_source[target_row_index];
if ( !target_row || !target_row.user_id ) {
target_empty_row = true;
}
continue;
}
shift.branch_id = shift.branch ? shift.branch_id : '';
shift.department_id = shift.department ? shift.department_id : '';
shift.job_id = shift.job_id ? shift.job_id : '';
shift.job_item_id = shift.job_item_id ? shift.job_item_id : '';
shift.punch_tag_id = shift.punch_tag_id ? shift.punch_tag_id : '';
target_row_index = first_target_row_index + row_index_offset;
target_cell_index = first_target_cell_index + cell_index_offset;
if ( target_cell_index > colModel.length - 1 ) {
continue;
}
target_data = $this.getDataByCellIndex( target_row_index, target_cell_index );
target_row = $this.schedule_source[target_row_index];
if ( !target_row || !target_row.user_id ) {
target_empty_row = true;
}
if ( target_row ) {
if ( target_row.type === ScheduleViewControllerRowType.DATE ) {
break;
}
if ( !target_data || target_empty_row ) {
var date_stamp;
//Error: TypeError: colModel[target_cell_index] is undefined in /interface/html5/framework/jquery.min.js?v=8.0.0-20141230-153210 line 2 > eval line 1443
if ( colModel ) {
if ( $this.getMode() === ScheduleViewControllerMode.MONTH ) {
//Error: "TypeError: Cannot read property 'format' of null"
// when user drags a scedule to a non-date grid element we need to quietly fail
var related_date = $this.getCellRelatedDate( target_row_index, colModel, target_cell_index, colModel[target_cell_index].name );
if ( related_date ) {
date_stamp = related_date.format();
}
} else {
date_stamp = Global.strToDate( colModel[target_cell_index].name, $this.full_format ).format();
}
}
if ( !date_stamp || date_stamp == 'Invalid date' ) {
continue;
}
target_data = {};
if ( !target_row.user_id ) { //Only happens in month mode;
target_data = shift;
target_data.date_stamp = date_stamp;
target_data.start_date_stamp = date_stamp;
} else {
target_data.user_id = target_row.user_id;
target_data.branch = target_row.branch;
target_data.branch_id = target_row.branch ? target_row.branch_id : '';
target_data.schedule_policy_id = target_row.schedule_policy_id;
target_data.department_id = target_row.department ? target_row.department_id : '';
target_data.department = target_row.department;
target_data.job_id = target_row.job_id ? target_row.job_id : '';
target_data.job = target_row.job;
target_data.job_item_id = target_row.job_item_id ? target_row.job_item_id : '';
target_data.punch_tag_id = target_row.punch_tag_id ? target_row.punch_tag_id : '';
target_data.job_item = target_row.job_item;
target_data.date_stamp = date_stamp;
target_data.start_date_stamp = date_stamp;
}
}
} else {
continue;
}
var new_shift = Global.clone( shift );
if ( $this.select_drag_menu_id !== 'swap' ) {
new_shift.id = '';
new_shift.date_stamp = target_data.date_stamp;
new_shift.start_date_stamp = target_data.start_date_stamp;
new_shift.user_id = target_data.user_id;
// When dragging an open shift to an empty cell in a user row with no branch column visible, the branch id value now defaults to user default branch id
new_shift.branch_id = target_data.branch ? target_data.branch_id : TTUUID.not_exist_id;
new_shift.department_id = target_data.department ? target_data.department_id : TTUUID.not_exist_id;
new_shift.job_id = target_data.job_id ? target_data.job_id : TTUUID.not_exist_id;
new_shift.job_item_id = target_data.job_item_id ? target_data.job_item_id : TTUUID.not_exist_id;
new_shift.punch_tag_id = target_data.punch_tag_id ? target_data.punch_tag_id : TTUUID.not_exist_id;
if ( $this.is_override ) {
new_shift.overwrite = true;
}
if ( $this.notify_user_schedule_change ) {
new_shift.notify_user_schedule_change = true;
}
new_shifts_array.push( new_shift );
if ( shift.id && shift.id != TTUUID.zero_id ) {
delete_shifts_array.push( shift.id );
} else if ( shift.user_id != TTUUID.zero_id && shift.user_id != TTUUID.not_exist_id ) {
//If dragging (move) a recurring shift assigned to a user and dropping on another user, switch the source shift to Absent in the process, otherwise both shifts will exist as being worked.
// However when dragging from a OPEN shift as the source, that isn't required, as the OPEN shift will automatically be filled.
shift.status_id = '20';
recurring_delete_shifts_array.push( shift );
} else if ( shift.user_id == TTUUID.zero_id ) {
delete_old_items = false; //Never delete old items when the source is a OPEN shift.
}
} else {
var temp_selected_data = Global.clone( new_shift );
var temp_target_data = Global.clone( target_data );
if ( !temp_target_data.start_date ) {
continue;
}
for ( var key in target_data ) {
if ( key !== 'id' &&
key !== 'user_id' &&
key !== 'date_stamp' &&
key !== 'start_date_stamp' &&
key !== 'branch_id' &&
key !== 'department_id' &&
key !== 'job_id' &&
key !== 'job_item_id' &&
key !== 'punch_tag_id' &&
key !== 'branch' &&
key !== 'department' &&
key !== 'job' &&
key !== 'job_item' &&
key !== 'schedule_policy_id' ) {
target_data[key] = temp_selected_data[key];
new_shift[key] = temp_target_data[key];
}
}
// When dragging an open shift to an empty cell in a user row with no branch column visible, the branch id value now defaults to user default branch id
target_data.branch_id = target_data.branch ? target_data.branch_id : TTUUID.not_exist_id;
target_data.department_id = target_data.department ? target_data.department_id : TTUUID.not_exist_id;
target_data.job_id = target_data.job_id ? target_data.job_id : TTUUID.not_exist_id;
target_data.job_item_id = target_data.job_item_id ? target_data.job_item_id : TTUUID.not_exist_id;
target_data.punch_tag_id = target_data.punch_tag_id ? target_data.punch_tag_id : TTUUID.not_exist_id;
new_shifts_array.push( target_data );
new_shifts_array.push( new_shift );
}
}
if ( new_shifts_array.length > 0 ) {
$this.api.setSchedule( new_shifts_array, {
onResult: function( res ) {
if ( res.isValid() ) {
if ( delete_old_items ) {
if ( delete_shifts_array.length > 0 ) {
$this.api.deleteSchedule( delete_shifts_array, {
onResult: function() {
if ( recurring_delete_shifts_array.length > 0 ) {
$this.api.setSchedule( recurring_delete_shifts_array, {
onResult: function() {
$this.search();
}
} );
} else {
$this.search();
}
}
} );
} else if ( recurring_delete_shifts_array.length > 0 ) {
$this.api.setSchedule( recurring_delete_shifts_array, {
onResult: function() {
$this.search();
}
} );
} else {
$this.search();
}
} else {
$this.search();
}
} else {
TAlertManager.showErrorAlert( res );
}
}
} );
}
} );
cells.unbind( 'dragenter' ).bind( 'dragenter', function( event ) {
event.preventDefault();
} );
cells.unbind( 'dragover' ).bind( 'dragover', function( event ) {
event.preventDefault(); //Must prevent tihs
} );
cells.unbind( 'dragend' ).bind( 'dragend', function( event ) {
$( '.drag-holder-div' ).remove();
$( '.schedule-drag-over' ).removeClass( 'schedule-drag-over' );
} );
}
resetSomeFields( item ) {
item.branch_id = item.branch ? item.branch_id : '';
item.department_id = item.department ? item.department_id : '';
item.job_id = item.job ? item.job_id : '';
item.job_item_id = item.job_item ? item.job_item_id : '';
item.punch_tag_id = item.punch_tag_id ? item.punch_tag_id : [];
return item;
}
_createParametersForAdd() {
var result = [], user;
if ( this.select_cells_Array.length > 0 ) {
for ( var i = 0, n = this.select_cells_Array.length; i < n; i++ ) {
var item = this.select_cells_Array[i];
user = {};
user.user_id = item.user_id;
user.branch_id = item.branch_id;
user.department_id = item.department_id;
user.job_id = item.job_id;
user.job_item_id = item.job_item_id;
user.punch_tag_id = item.punch_tag_id;
user.date = item.date;
result.push( user );
}
}
if ( result.length < 1 ) {
var login_user = LocalCacheData.getLoginUser();
user = {};
user.user_id = login_user.id;
user.branch_id = login_user.branch_id;
user.department_id = login_user.department_id;
user.job_id = login_user.job_id;
user.job_item_id = login_user.job_item_id;
user.punch_tag_id = login_user.punch_tag_id;
user.date = this.getSelectDate();
result.push( user );
}
return result;
}
onAddClick( doing_save_and_new ) {
var $this = this;
this.setCurrentEditViewState( 'new' );
if ( this.select_cells_Array.length > 1 ) {
this.is_mass_adding = true;
}
$this.openEditView();
var args;
if ( !doing_save_and_new ) {
args = this._createParametersForAdd();
} else {
args = [
{
user_id: this.current_edit_record.user_id,
branch_id: this.current_edit_record.branch_id,
department_id: this.current_edit_record.department_id,
job_id: this.current_edit_record.job_id,
job_item_id: this.current_edit_record.job_item_id,
punch_tag_id: this.current_edit_record.punch_tag_id,
date: this.current_edit_record.date_stamp
}
];
}
this.api['get' + this.api.key_name + 'DefaultData']( args, {
onResult: function( result ) {
var select_shift;
var result_data = result.getResult();
select_shift = result_data;
if ( $this.select_cells_Array.length >= 1 ) {
for ( var i = 0, n = args.length; i < n; i++ ) {
var item = args[i];
if ( i == 0 ) {
select_shift.branch_id = item.branch_id;
select_shift.department_id = item.department_id;
select_shift.job_id = item.job_id;
select_shift.job_item_id = item.job_item_id;
select_shift.punch_tag_id = item.punch_tag_id;
} else {
( select_shift.branch_id !== item.branch_id && select_shift.branch_id !== '-2' ) ? select_shift.branch_id = '-2' : item.branch_id;
( select_shift.department_id !== item.department_id && select_shift.department_id !== '-2' ) ? select_shift.department_id = '-2' : item.department_id;
( select_shift.job_id !== item.job_id && select_shift.job_id !== '-2' ) ? select_shift.job_id = '-2' : item.job_id;
( select_shift.job_item_id !== item.job_item_id && select_shift.job_item_id !== '-2' ) ? select_shift.job_item_id = '-2' : item.job_item_id;
( select_shift.punch_tag_id !== item.punch_tag_id && select_shift.punch_tag_id !== '-2' ) ? select_shift.punch_tag_id = '-2' : item.punch_tag_id;
}
}
}
if ( !doing_save_and_new ) {
select_shift.date_stamp = $this.getSelectDate();
} else {
var temp_date = Global.strToDate( $this.current_edit_record.date_stamp );
select_shift.date_stamp = new Date( new Date( temp_date.getTime() ).setDate( temp_date.getDate() + 1 ) ).format();
}
if ( !select_shift.start_date_stamp ) {
select_shift.start_date_stamp = select_shift.date_stamp;
}
select_shift.id = '';
if ( $this.sub_view_mode && $this.parent_key ) {
result_data[$this.parent_key] = $this.parent_value;
}
$this.current_edit_record = select_shift;
$this.initEditView();
}
} );
}
openEditView() {
if ( !this.edit_view ) {
this.initEditViewUI( 'Schedule', 'ScheduleEditView.html' );
}
this.previous_absence_policy_id = false;
}
//set widget disablebility if view mode or edit mode
setEditViewWidgetsMode() {
var did_clean_dic = {};
for ( var key in this.edit_view_ui_dic ) {
if ( !this.edit_view_ui_dic.hasOwnProperty( key ) ) {
continue;
}
var widget = this.edit_view_ui_dic[key];
widget.css( 'opacity', 1 );
var column = widget.parent().parent().parent();
var tab_id = column.parent().attr( 'id' );
if ( !column.hasClass( 'v-box' ) ) {
if ( !did_clean_dic[tab_id] ) {
did_clean_dic[tab_id] = true;
}
}
if ( this.is_viewing ) {
if ( Global.isSet( widget.setEnabled ) ) {
widget.setEnabled( false );
}
} else {
if ( Global.isSet( widget.setEnabled ) ) {
widget.setEnabled( true );
}
}
}
}
setJobValueWhenUserChanged( job, job_id_col_name, filter_data ) {
var $this = this;
//Error: Uncaught TypeError: Cannot set property 'job_item_id' of null in /interface/html5/#!m=TimeSheet&date=20150126&user_id=54286 line 6785
if ( !$this.current_edit_record ) {
return;
}
if ( this.edit_view_ui_dic['user_ids'] && this.edit_view_ui_dic['user_ids'].is( ':visible' ) ) {
filter_data['user_id'] = this.edit_view_ui_dic['user_ids'].getValue();
//If more than one user is selected, don't filter by user_id at all, show all jobs and let the validation system handle it.
if ( filter_data['user_id'].length == 1 ) {
filter_data['user_id'] = filter_data['user_id'][0];
} else {
filter_data['user_id'] = false;
}
} else {
filter_data['user_id'] = this.current_edit_record['user_id'];
}
var job_widget = $this.edit_view_ui_dic[job_id_col_name];
var current_job_id = job_widget.getValue();
job_widget.setSourceData( null ); //Clear out source data so its reloaded when the Job dropdown is expanded again.
job_widget.setCheckBox( true );
this.edit_view_ui_dic['job_item_quick_search'].setCheckBox( true );
var args = {};
args.filter_data = filter_data;
$this.edit_view_ui_dic[job_id_col_name].setDefaultArgs( args );
return;
}
onFormItemChange( target, doNotValidate ) {
var $this = this;
this.setIsChanged( target );
this.setMassEditingFieldsWhenFormChange( target );
var key = target.getField();
var c_value = target.getValue();
this.current_edit_record[key] = c_value;
switch ( key ) {
case 'job_id':
if ( Global.getProductEdition() >= 20 ) {
this.edit_view_ui_dic['job_quick_search'].setValue( target.getValue( true ) ? ( target.getValue( true ).manual_id ? target.getValue( true ).manual_id : '' ) : '' );
this.setJobItemValueWhenJobChanged( target.getValue( true ), 'job_item_id', {
status_id: 10,
job_id: this.current_edit_record.job_id
} );
this.edit_view_ui_dic['job_quick_search'].setCheckBox( true );
this.setPunchTagValuesWhenCriteriaChanged( this.getPunchTagFilterData(), 'punch_tag_id' );
}
break;
case 'job_item_id':
if ( Global.getProductEdition() >= 20 ) {
this.edit_view_ui_dic['job_item_quick_search'].setValue( target.getValue( true ) ? ( target.getValue( true ).manual_id ? target.getValue( true ).manual_id : '' ) : '' );
this.edit_view_ui_dic['job_item_quick_search'].setCheckBox( true );
this.setPunchTagValuesWhenCriteriaChanged( this.getPunchTagFilterData(), 'punch_tag_id' );
}
break;
case 'punch_tag_id':
if ( Global.getProductEdition() >= 20 ) {
if ( c_value !== TTUUID.zero_id && c_value !== false && c_value.length > 0 ) {
this.setPunchTagQuickSearchManualIds( target.getSelectItems() );
} else {
this.edit_view_ui_dic['punch_tag_quick_search'].setValue( '' );
}
$this.previous_punch_tag_selection = c_value;
//Reset source data to make sure correct punch tags are always shown.
this.edit_view_ui_dic['punch_tag_id'].setSourceData( null );
}
break;
case 'branch_id':
if ( Global.getProductEdition() >= 20 ) {
this.setPunchTagValuesWhenCriteriaChanged( this.getPunchTagFilterData(), 'punch_tag_id' );
this.setJobValueWhenCriteriaChanged( 'job_id', {
status_id: 10,
user_id: this.current_edit_record.user_id,
punch_branch_id: this.current_edit_record.branch_id,
punch_department_id: this.current_edit_record.department_id
} );
this.setDepartmentValueWhenBranchChanged( target.getValue( true ), 'department_id', {
branch_id: this.current_edit_record.branch_id,
user_id: this.current_edit_record.user_id
} );
}
break;
case 'department_id':
if ( Global.getProductEdition() >= 20 ) {
this.setPunchTagValuesWhenCriteriaChanged( this.getPunchTagFilterData(), 'punch_tag_id' );
this.setJobValueWhenCriteriaChanged( 'job_id', {
status_id: 10,
user_id: this.current_edit_record.user_id,
punch_branch_id: this.current_edit_record.branch_id,
punch_department_id: this.current_edit_record.department_id
} );
}
break;
case 'job_quick_search':
case 'job_item_quick_search':
if ( Global.getProductEdition() >= 20 ) {
this.onJobQuickSearch( key, c_value );
this.setPunchTagValuesWhenCriteriaChanged( this.getPunchTagFilterData(), 'punch_tag_id' );
}
break;
case 'punch_tag_quick_search':
if ( Global.getProductEdition() >= 20 ) {
this.onPunchTagQuickSearch( c_value, this.getPunchTagFilterData(), 'punch_tag_id' );
//Don't validate immediately as onPunchTagQuickSearch is doing async API calls, and it would cause a guaranteed validation failure.
doNotValidate = true;
}
break;
case 'status_id':
this.onTypeChange( true );
break;
case 'user_id':
case 'user_ids':
this.setEditMenu();
this.setAbsencePolicyWhenUserChanged();
if ( Global.getProductEdition() >= 20 ) {
if ( this.edit_view_ui_dic['job_id'] ) {
this.setJobValueWhenUserChanged( this.edit_view_ui_dic['job_id'].getValue( true ), 'job_id', {
status_id: 10,
user_id: this.edit_view_ui_dic[key].getValue(),
} );
}
this.setPunchTagValuesWhenCriteriaChanged( this.getPunchTagFilterData(), 'punch_tag_id' );
}
return;
case 'start_date_stamps':
this.setEditMenu();
this.current_edit_record['start_date_stamp'] = c_value;
break;
}
if ( key == 'absence_policy_id' ) {
this.previous_absence_policy_id = this.current_edit_record.absence_policy_id;
}
if ( key === 'date_stamp' ||
key === 'start_date_stamps' ||
key === 'start_date_stamp' ||
key === 'start_time' ||
key === 'end_time' ||
key === 'schedule_policy_id' ||
key === 'absence_policy_id' ) {
if ( this.current_edit_record['date_stamp'] !== '' &&
this.current_edit_record['start_time'] !== '' &&
this.current_edit_record['end_time'] !== '' ) {
var startTime = this.current_edit_record['date_stamp'] + ' ' + this.current_edit_record['start_time'];
var endTime = this.current_edit_record['date_stamp'] + ' ' + this.current_edit_record['end_time'];
var schedulePolicyId = this.current_edit_record['schedule_policy_id'];
var user_id = this.current_edit_record.user_id;
this.api.getScheduleTotalTime( startTime, endTime, schedulePolicyId, user_id, {
onResult: function( total_time ) {
//Uncaught TypeError: Cannot set property 'total_time' of null
//Error: Uncaught TypeError: Cannot read property 'setValue' of undefined in interface/html5/#!m=Schedule&date=20160202&mode=week&a=new&tab=Schedule line 1799
if ( !$this.edit_view || !$this.current_edit_record || !$this.edit_view_ui_dic['total_time'] ) {
return;
}
//Fixed exception that total_time is null
if ( total_time ) {
total_time = total_time.getResult();
} else {
total_time = $this.current_edit_record.total_time ? $this.current_edit_record.total_time : 0;
}
$this.current_edit_record.total_time = total_time;
total_time = Global.getTimeUnit( total_time );
$this.edit_view_ui_dic['total_time'].setValue( total_time );
$this.onAvailableBalanceChange();
}
} );
//
} else {
this.onAvailableBalanceChange();
}
}
if ( !doNotValidate ) {
this.validate();
}
}
setAbsencePolicyWhenUserChanged() {
var $this = this;
var absence_widget = $this.edit_view_ui_dic['absence_policy_id'];
absence_widget.setSourceData( null );
var old_value = absence_widget.getValue();
var args = {};
args.filter_data = { id: old_value };
args = this.setAbsencePolicyFilter( args );
if ( old_value ) {
$this.absence_policy_api.getAbsencePolicy( args, {
onResult: function( task_result ) {
// Returning early to help mitigate #2889 - "Error: Uncaught TypeError: Cannot set property 'absence_policy_id' of null"
// This can happen when the user saves or leaves the page before the API call is completed.
if ( $this.current_edit_record === null || $this.current_edit_record === undefined ) {
return;
}
var data = task_result.getResult();
if ( data.length > 0 ) {
absence_widget.setValue( old_value );
$this.current_edit_record.absence_policy_id = old_value;
} else {
absence_widget.setValue( false );
$this.current_edit_record.absence_policy_id = false;
}
$this.onAvailableBalanceChange();
$this.validate();
}
} );
} else {
this.onAvailableBalanceChange();
this.validate();
}
}
//Make sure this.current_edit_record is updated before validate
validate() {
var $this = this;
var record = {};
if ( this.is_mass_adding ) {
record = [];
$.each( this.select_cells_Array, function( index, value ) {
if ( value.hasOwnProperty( 'user_id' ) && value.hasOwnProperty( 'date' ) && value.date ) {
var commonRecord = Global.clone( $this.current_edit_record );
delete commonRecord.user_ids;
delete commonRecord.start_dates;
commonRecord.id = '';
commonRecord.user_id = value.user_id;
commonRecord.start_date_stamp = value.date;
commonRecord = $this.buildMassAddRecord( commonRecord );
record.push( commonRecord );
}
} );
} else if ( this.is_mass_editing ) {
for ( var key in this.edit_view_ui_dic ) {
if ( !this.edit_view_ui_dic.hasOwnProperty( key ) ) {
continue;
}
var widget = this.edit_view_ui_dic[key];
if ( Global.isSet( widget.isChecked ) ) {
if ( widget.isChecked() && widget.getEnabled() ) {
record[key] = widget.getValue();
}
}
}
if ( this.mass_edit_record_ids.length > 0 ) {
var checkFields = record;
record = [];
$.each( this.mass_edit_record_ids, function( index, value ) {
var commonRecord = Global.clone( checkFields );
commonRecord.id = value;
commonRecord = $this.processAddRecord( commonRecord );
record.push( commonRecord );
} );
$.each( this.select_all_shifts_array, function( index, value ) {
if ( !value.id || value.id == TTUUID.zero_id ) {
var commonRecord = Global.clone( value );
for ( var key in checkFields ) {
commonRecord[key] = checkFields[key];
}
commonRecord = $this.processAddRecord( commonRecord );
record.push( commonRecord );
}
} );
record = this.getRecordsFromUserIDs( record );
} else {
var record_array = [];
$.each( this.select_all_shifts_array, function( index, value ) {
if ( !value.id || value.id == TTUUID.zero_id ) {
var commonRecord = Global.clone( value );
for ( var key in record ) {
commonRecord[key] = record[key];
}
commonRecord = $this.processAddRecord( commonRecord );
record_array.push( commonRecord );
}
} );
if ( record_array.length < 1 ) {
if ( this.select_cells_Array.length > 0 ) {
$this.processAddRecord( record );
record = this.getRecordsFromUserIDs( [record] );
}
} else {
record = record_array;
record = this.getRecordsFromUserIDs( record );
}
}
} else {
//Error: Uncaught TypeError: Cannot read property 'indexOf' of undefined in interface/html5/#!m=Schedule&date=20151204&mode=day line 1954
if ( this.current_edit_record && this.current_edit_record.start_date_stamp &&
( this.current_edit_record.start_date_stamp.indexOf( ' - ' ) > 0 ||
$.type( this.current_edit_record.start_date_stamp ) === 'array' ) ) {
if ( this.current_edit_record.start_date_stamp.indexOf( ' - ' ) > 0 ) {
this.current_edit_record.start_date_stamp = this.parserDatesRange( this.current_edit_record.start_date_stamp );
}
record = [];
for ( var i = 0; i < this.current_edit_record.start_date_stamp.length; i++ ) {
var commonRecord = Global.clone( $this.current_edit_record );
commonRecord.start_date_stamp = this.current_edit_record.start_date_stamp[i];
if ( this.select_cells_Array.length > 0 ) {
$this.processAddRecord( commonRecord );
}
record.push( commonRecord );
}
record = this.getRecordsFromUserIDs( record );
} else {
record = Global.clone( this.current_edit_record );
if ( this.select_cells_Array.length > 0 ) {
$this.processAddRecord( record );
}
record = this.getRecordsFromUserIDs( [record] );
}
}
this.api['validate' + this.api.key_name]( record, {
onResult: function( result ) {
$this.validateResult( result );
}
} );
}
getRecordsFromUserIDs( record ) {
var result = [];
for ( var j = 0; j < record.length; j++ ) {
var common_record = record[j];
if ( common_record.user_ids && common_record.user_ids.length > 0 ) {
for ( var y = 0; y < common_record.user_ids.length; y++ ) {
var user_id = common_record.user_ids[y];
if ( Global.isObject( user_id ) && user_id.id ) {
user_id = user_id.id;
}
var new_common_record = Global.clone( common_record );
new_common_record.user_id = user_id;
result.push( new_common_record );
}
} else {
if ( ( !this.current_edit_record || !this.current_edit_record.id || this.current_edit_record.id == TTUUID.zero_id ) && !this.is_mass_editing ) {
common_record.user_id = TTUUID.zero_id;
}
result.push( common_record );
}
}
return result;
}
onSaveAndCopy( ignoreWarning ) {
var $this = this;
if ( !Global.isSet( ignoreWarning ) ) {
ignoreWarning = false;
}
this.is_add = true;
this.is_changed = false;
LocalCacheData.current_doing_context_action = 'save_and_copy';
var record = this.current_edit_record;
record = this.processAddRecord( record );
record = this.getRecordsFromUserIDs( [record] );
if ( this.current_edit_record.start_date_stamp.indexOf( ' - ' ) > 0 ||
$.type( this.current_edit_record.start_date_stamp ) === 'array' ) {
if ( this.current_edit_record.start_date_stamp.indexOf( ' - ' ) > 0 ) {
this.current_edit_record.start_date_stamp = this.parserDatesRange( this.current_edit_record.start_date_stamp );
}
record = [];
for ( var i = 0; i < this.current_edit_record.start_date_stamp.length; i++ ) {
var commonRecord = Global.clone( $this.current_edit_record );
commonRecord.start_date_stamp = this.current_edit_record.start_date_stamp[i];
commonRecord = this.processAddRecord( commonRecord );
record.push( commonRecord );
}
record = this.getRecordsFromUserIDs( record );
}
this.clearNavigationData();
this.api['set' + this.api.key_name]( record, false, false, ignoreWarning, {
onResult: function( result ) {
if ( $this.current_edit_record ) {
var current_date_str = $this.current_edit_record.start_date_stamp;
if ( $.type( current_date_str ) === 'array' ) {
current_date_str = current_date_str[current_date_str.length - 1];
}
var current_date = Global.strToDate( current_date_str );
var next_date = new Date( new Date( current_date.getTime() ).setDate( current_date.getDate() + 1 ) );
$this.current_edit_record.start_date_stamp = next_date.format();
$this.onSaveAndCopyResult( result );
}
}
} );
}
onSaveAndNewClick( ignoreWarning ) {
var $this = this;
if ( !Global.isSet( ignoreWarning ) ) {
ignoreWarning = false;
}
this.setCurrentEditViewState( 'new' );
var record = this.current_edit_record;
record = this.processAddRecord( record );
record = this.getRecordsFromUserIDs( [record] );
if ( this.current_edit_record.start_date_stamp.indexOf( ' - ' ) > 0 ||
$.type( this.current_edit_record.start_date_stamp ) === 'array' ) {
if ( this.current_edit_record.start_date_stamp.indexOf( ' - ' ) > 0 ) {
this.current_edit_record.start_date_stamp = this.parserDatesRange( this.current_edit_record.start_date_stamp );
}
record = [];
for ( var i = 0; i < this.current_edit_record.start_date_stamp.length; i++ ) {
var commonRecord = Global.clone( $this.current_edit_record );
commonRecord.start_date_stamp = this.current_edit_record.start_date_stamp[i];
commonRecord = this.processAddRecord( commonRecord );
record.push( commonRecord );
}
record = this.getRecordsFromUserIDs( record );
}
this.api['set' + this.api.key_name]( record, false, ignoreWarning, {
onResult: function( result ) {
$this.onSaveAndNewResult( result );
}
} );
}
buildSelectedCellsRecord() {
var $this = this;
var retval = [];
$.each( this.select_cells_Array, function( index, value ) {
if ( value.hasOwnProperty( 'user_id' ) && value.hasOwnProperty( 'date' ) && value.date ) {
var commonRecord = Global.clone( $this.current_edit_record );
delete commonRecord.user_ids;
delete commonRecord.start_dates;
commonRecord.id = '';
commonRecord.user_id = value.user_id;
commonRecord.start_date_stamp = value.date;
commonRecord = $this.buildMassAddRecord( commonRecord );
retval.push( commonRecord );
}
} );
return retval;
}
buildMassAddRecord( record ) {
var massAddArgs = this._createParametersForAdd();
for ( var i = 0, n = massAddArgs.length; i < n; i++ ) {
var item = massAddArgs[i];
if ( record.user_id === item.user_id ) {
record.branch_id == '-2' ? ( record.branch_id = item.branch_id ) : record.branch_id;
record.department_id == '-2' ? ( record.department_id = item.department_id ) : record.department_id;
record.job_id == '-2' ? ( record.job_id = item.job_id ) : record.job_id;
record.job_item_id == '-2' ? ( record.job_item_id = item.job_item_id ) : record.job_item_id;
record.punch_tag_id == '-2' ? ( record.punch_tag_id = item.punch_tag_id ) : record.punch_tag_id;
}
}
return record;
}
processAddRecord( record ) {
var massAddArgs = this._createParametersForAdd();
for ( var i = 0, n = massAddArgs.length; i < n; i++ ) {
var item = massAddArgs[i];
record.branch_id == '-2' ? ( record.branch_id = item.branch_id ) : record.branch_id;
record.department_id == '-2' ? ( record.department_id = item.department_id ) : record.department_id;
record.job_id == '-2' ? ( record.job_id = item.job_id ) : record.job_id;
record.job_item_id == '-2' ? ( record.job_item_id = item.job_item_id ) : record.job_item_id;
record.punch_tag_id == '-2' ? ( record.punch_tag_id = item.punch_tag_id ) : record.punch_tag_id;
break;
}
return record;
}
getSelectedId( record, field, massAddArgs ) {
for ( var i = 0, n = massAddArgs.length; i < n; i++ ) {
var item = massAddArgs[i];
if ( record.user_id === item.user_id ) {
record[field] = item[field];
}
}
}
onSaveAndContinue( ignoreWarning ) {
var $this = this;
if ( !Global.isSet( ignoreWarning ) ) {
ignoreWarning = false;
}
this.is_changed = false;
this.is_add = false;
LocalCacheData.current_doing_context_action = 'save_and_continue';
var record = this.current_edit_record;
record = this.processAddRecord( record );
record = this.uniformVariable( record );
if ( this.current_edit_record.start_date_stamp && ( this.current_edit_record.start_date_stamp.indexOf( ' - ' ) > 0 || $.type( this.current_edit_record.start_date_stamp ) === 'array' ) ) {
if ( this.current_edit_record.start_date_stamp.indexOf( ' - ' ) > 0 ) {
this.current_edit_record.start_date_stamp = this.parserDatesRange( this.current_edit_record.start_date_stamp );
}
record = [];
for ( var i = 0; i < this.current_edit_record.start_date_stamp.length; i++ ) {
var commonRecord = Global.clone( $this.current_edit_record );
commonRecord.start_date_stamp = this.current_edit_record.start_date_stamp[i];
commonRecord = this.processAddRecord( commonRecord );
record.push( commonRecord );
}
record = this.getRecordsFromUserIDs( record );
} else {
record = this.getRecordsFromUserIDs( [record] );
}
this.api['set' + this.api.key_name]( record, false, ignoreWarning, {
onResult: function( result ) {
$this.previous_absence_policy_id = false;
$this.onSaveAndContinueResult( result );
}
} );
}
onSaveClick( ignoreWarning ) {
var $this = this;
var record;
if ( !Global.isSet( ignoreWarning ) ) {
ignoreWarning = false;
}
LocalCacheData.current_doing_context_action = 'save';
if ( this.is_mass_adding ) {
record = this.buildSelectedCellsRecord();
} else if ( this.is_mass_editing ) {
var checkFields = this.getChangedFields();
record = [];
$.each( this.mass_edit_record_ids, function( index, value ) {
var commonRecord = Global.clone( checkFields );
commonRecord.id = value;
commonRecord = $this.processAddRecord( commonRecord );
record.push( commonRecord );
} );
$.each( this.select_all_shifts_array, function( index, value ) {
if ( !value.id || value.id == TTUUID.zero_id ) {
var commonRecord = Global.clone( value );
for ( var key in checkFields ) {
commonRecord[key] = checkFields[key];
}
commonRecord = $this.processAddRecord( commonRecord );
record.push( commonRecord );
}
} );
} else if ( this.current_edit_record.start_date_stamp.indexOf( ' - ' ) > 0 ||
$.type( this.current_edit_record.start_date_stamp ) === 'array' ) {
if ( this.current_edit_record.start_date_stamp.indexOf( ' - ' ) > 0 ) {
this.current_edit_record.start_date_stamp = this.parserDatesRange( this.current_edit_record.start_date_stamp );
}
record = [];
for ( var i = 0; i < this.current_edit_record.start_date_stamp.length; i++ ) {
var commonRecord = Global.clone( $this.current_edit_record );
commonRecord.start_date_stamp = this.current_edit_record.start_date_stamp[i];
commonRecord = $this.processAddRecord( commonRecord );
record.push( commonRecord );
}
record = this.getRecordsFromUserIDs( record );
} else {
record = this.current_edit_record;
record = $this.processAddRecord( record );
record = this.getRecordsFromUserIDs( [record] );
}
this.api['set' + this.api.key_name]( record, false, ignoreWarning, {
onResult: function( result ) {
if ( result.isValid() ) {
var result_data = result.getResult();
//#2571 - Cannot read property 'id' of null
if ( result_data === true && $this.current_edit_record ) {
$this.refresh_id = $this.current_edit_record.id;
} else if ( TTUUID.isUUID( result_data ) && result_data != TTUUID.zero_id && result_data != TTUUID.not_exist_id ) {
$this.refresh_id = result_data;
}
$this.search( false, false );
$this.previous_absence_policy_id = false;
$this.removeEditView();
} else {
//BUG#2073 - Pulled out the error message box that was showing the result array as its "toString" representation. ([object][object]);
$this.setErrorTips( result );
$this.setErrorMenu();
}
}
} );
}
removeEditView() {
super.removeEditView();
this.setMoveOrDropMode( this.select_drag_menu_id ? this.select_drag_menu_id : 'move' );
this.selected_user_ids = [];
this.is_mass_adding = false;
}
setEditMenuSaveAndContinueIcon( context_btn ) {
this.saveAndContinueValidate( context_btn );
if ( this.is_mass_editing || this.is_viewing || this.is_mass_adding || this.isMassEmployeeOrDate() ) {
ContextMenuManager.disableMenuItem( this.determineContextMenuMountAttributes().id, context_btn.id, false );
}
}
isMassEmployeeOrDate() {
if ( this.current_edit_record && this.current_edit_record.start_date_stamps && ( this.current_edit_record.start_date_stamps.indexOf( ' - ' ) > 0 ||
$.type( this.current_edit_record.start_date_stamps ) === 'array' && this.current_edit_record.start_date_stamps.length > 1
) ) {
return true;
}
if ( this.current_edit_record && this.current_edit_record.user_ids && this.current_edit_record.user_ids.length > 1 ) {
return true;
}
return false;
}
setEditMenuSaveAndAddIcon( context_btn ) {
this.saveAndNewValidate( context_btn );
if ( this.is_viewing || this.is_mass_editing || this.is_mass_adding ) {
ContextMenuManager.disableMenuItem( this.determineContextMenuMountAttributes().id, context_btn.id, false );
}
}
setEditMenuSaveAndCopyIcon( context_btn ) {
this.saveAndCopyValidate( context_btn );
if ( this.is_viewing || this.is_mass_editing || this.is_mass_adding ) {
ContextMenuManager.disableMenuItem( this.determineContextMenuMountAttributes().id, context_btn.id, false );
}
}
onTypeChange( getRate ) {
if ( this.current_edit_record.status_id == 20 ) {
this.attachElement( 'absence_policy_id' );
} else {
this.detachElement( 'absence_policy_id' );
}
}
setEditViewData() {
var $this = this;
super.setEditViewData(); //Set Navigation
$this.onTypeChange( false );
}
checkOpenPermission() {
if ( Global.getProductEdition() >= 15 && PermissionManager.validate( 'schedule', 'view_open' ) ) {
return true;
}
return false;
}
getCustomFieldReferenceField() {
return 'note';
}
buildEditViewUI() {
var $this = this;
var form_item_input;
var widgetContainer;
// #VueContextMenu# After we add the edit_view to the page in initEditViewUI(), add the context menu (Vue needs a valid id in dom)
if( ContextMenuManager.getMenu( this.determineContextMenuMountAttributes().id ) === undefined ) {
this.buildContextMenu();
} else {
Debug.Warn( 'Context Menu ('+ this.determineContextMenuMountAttributes().id +') already exists for: '+ this.viewId, 'ScheduleViewController.js', 'ScheduleViewController', 'buildEditViewUI', 10 );
}
this.edit_view_close_icon = this.edit_view.find( '.close-icon' );
this.edit_view_close_icon.hide();
this.edit_view_close_icon.click( function() {
$this.onCloseIconClick();
} );
var tab_model = {
'tab_schedule': { 'label': $.i18n._( 'Schedule' ) },
'tab_audit': true,
};
this.setTabModel( tab_model );
//Tab 0 start
var tab_schedule = this.edit_view_tab.find( '#tab_schedule' );
var tab_schedule_column1 = tab_schedule.find( '.first-column' );
//Employee
var production_edition_id = Global.getProductEdition();
form_item_input = Global.loadWidgetByName( FormItemType.AWESOME_BOX );
form_item_input.AComboBox( {
api_class: TTAPI.APIUser,
allow_multiple_selection: false,
layout_name: 'global_user',
show_search_inputs: true,
set_empty: !this.checkOpenPermission(),
set_open: this.checkOpenPermission(),
field: 'user_id'
} );
var default_args = {};
default_args.permission_section = 'schedule';
form_item_input.setDefaultArgs( default_args );
this.addEditFieldToColumn( $.i18n._( 'Employee' ), form_item_input, tab_schedule_column1, '', null, true );
//Mass Add Employees
form_item_input = Global.loadWidgetByName( FormItemType.AWESOME_BOX );
form_item_input.AComboBox( {
api_class: TTAPI.APIUser,
allow_multiple_selection: true,
layout_name: 'global_user',
show_search_inputs: true,
set_empty: !this.checkOpenPermission(),
set_open: this.checkOpenPermission(),
addition_source_function: ( function( target, source_data ) {
return $this.onEmployeeSourceCreate( target, source_data );
} ),
field: 'user_ids'
} );
default_args = {};
default_args.permission_section = 'schedule';
form_item_input.setDefaultArgs( default_args );
this.addEditFieldToColumn( $.i18n._( 'Employee' ), form_item_input, tab_schedule_column1, '', null, true );
//Status
form_item_input = Global.loadWidgetByName( FormItemType.COMBO_BOX );
form_item_input.TComboBox( { field: 'status_id' } );
form_item_input.setSourceData( $this.status_array );
this.addEditFieldToColumn( $.i18n._( 'Status' ), form_item_input, tab_schedule_column1 );
//Absence Policy
form_item_input = Global.loadWidgetByName( FormItemType.AWESOME_BOX );
form_item_input.AComboBox( {
api_class: TTAPI.APIAbsencePolicy,
allow_multiple_selection: false,
layout_name: 'global_absences',
show_search_inputs: true,
set_empty: true,
field: 'absence_policy_id'
} );
form_item_input.customSearchFilter = function( filter ) {
return $this.setAbsencePolicyFilter( filter );
};
this.addEditFieldToColumn( $.i18n._( 'Absence Policy' ), form_item_input, tab_schedule_column1, '', null, true );
//Available Balance
form_item_input = Global.loadWidgetByName( FormItemType.TEXT );
form_item_input.TText( { field: 'available_balance' } );
widgetContainer = $( '' );
this.available_balance_info = $( '' );
widgetContainer.append( form_item_input );
widgetContainer.append( this.available_balance_info );
this.addEditFieldToColumn( $.i18n._( 'Available Balance' ), form_item_input, tab_schedule_column1, '', widgetContainer, true );
if ( !this.current_edit_record || ( this.current_edit_record.user_ids && this.current_edit_record.user_ids.length > 1 ) ) {
this.detachElement( 'available_balance' );
}
//Date
form_item_input = Global.loadWidgetByName( FormItemType.DATE_PICKER );
form_item_input.TDatePicker( { field: 'start_date_stamp', validation_field: 'date_stamp' } );
this.addEditFieldToColumn( $.i18n._( 'Date' ), form_item_input, tab_schedule_column1, '', null, true );
//Dates
form_item_input = Global.loadWidgetByName( FormItemType.DATE_PICKER );
form_item_input.TRangePicker( { field: 'start_date_stamps' } );
this.addEditFieldToColumn( $.i18n._( 'Date' ), form_item_input, tab_schedule_column1, '', null, true );
//Mass Add Date
form_item_input = Global.loadWidgetByName( FormItemType.AWESOME_BOX );
form_item_input.AComboBox( {
allow_multiple_selection: true,
layout_name: 'global_option_column',
show_search_inputs: false,
set_empty: true,
field: 'start_dates'
} );
this.addEditFieldToColumn( $.i18n._( 'Date' ), form_item_input, tab_schedule_column1, '', null, true );
//Start Time
form_item_input = Global.loadWidgetByName( FormItemType.TIME_PICKER );
form_item_input.TTimePicker( { field: 'start_time' } );
this.addEditFieldToColumn( $.i18n._( 'In' ), form_item_input, tab_schedule_column1, '', null, true );
//End Time
form_item_input = Global.loadWidgetByName( FormItemType.TIME_PICKER );
form_item_input.TTimePicker( { field: 'end_time' } );
this.addEditFieldToColumn( $.i18n._( 'Out' ), form_item_input, tab_schedule_column1, '', null, true );
//Total
form_item_input = Global.loadWidgetByName( FormItemType.TEXT );
form_item_input.TText( { field: 'total_time' } );
form_item_input.css( 'cursor', 'pointer' );
this.addEditFieldToColumn( $.i18n._( 'Total' ), form_item_input, tab_schedule_column1 );
//Schedule Policy
form_item_input = Global.loadWidgetByName( FormItemType.AWESOME_BOX );
form_item_input.AComboBox( {
api_class: TTAPI.APISchedulePolicy,
allow_multiple_selection: false,
layout_name: 'global_schedule',
show_search_inputs: true,
set_empty: true,
field: 'schedule_policy_id'
} );
this.addEditFieldToColumn( $.i18n._( 'Schedule Policy' ), form_item_input, tab_schedule_column1 );
//Default Branch
form_item_input = Global.loadWidgetByName( FormItemType.AWESOME_BOX );
form_item_input.AComboBox( {
api_class: TTAPI.APIBranch,
allow_multiple_selection: false,
layout_name: 'global_branch',
show_search_inputs: true,
set_empty: true,
field: 'branch_id',
addition_source_function: ( function( target, source_data ) {
return $this.onSourceDataCreate( target, source_data );
} ),
//FIXME: Follow -2 to the API do not switch to UUID unless absolutely necessary?
added_items: [
{ value: TTUUID.not_exist_id, label: Global.default_item },
{ value: '-2', label: Global.selected_item }
]
} );
this.addEditFieldToColumn( $.i18n._( 'Branch' ), form_item_input, tab_schedule_column1, '', null, true );
if ( !this.show_branch_ui ) {
this.detachElement( 'branch_id' );
}
//Department
form_item_input = Global.loadWidgetByName( FormItemType.AWESOME_BOX );
form_item_input.AComboBox( {
api_class: TTAPI.APIDepartment,
allow_multiple_selection: false,
layout_name: 'global_department',
show_search_inputs: true,
set_empty: true,
field: 'department_id',
addition_source_function: ( function( target, source_data ) {
return $this.onSourceDataCreate( target, source_data );
} ),
added_items: [
{ value: TTUUID.not_exist_id, label: Global.default_item },
{ value: '-2', label: Global.selected_item }
]
} );
this.addEditFieldToColumn( $.i18n._( 'Department' ), form_item_input, tab_schedule_column1, '', null, true );
if ( !this.show_department_ui ) {
this.detachElement( 'department_id' );
}
if ( Global.getProductEdition() >= 20 ) {
//Job
form_item_input = Global.loadWidgetByName( FormItemType.AWESOME_BOX );
form_item_input.AComboBox( {
api_class: TTAPI.APIJob,
allow_multiple_selection: false,
layout_name: 'global_job',
show_search_inputs: true,
set_empty: true,
setRealValueCallBack: ( function( val ) {
if ( val ) {
job_coder.setValue( val.manual_id );
}
} ),
field: 'job_id',
addition_source_function: ( function( target, source_data ) {
return $this.onSourceDataCreate( target, source_data );
} ),
added_items: [
{ value: TTUUID.not_exist_id, label: Global.default_item },
{ value: '-2', label: Global.selected_item }
]
} );
widgetContainer = $( '' );
var job_coder = Global.loadWidgetByName( FormItemType.TEXT_INPUT );
job_coder.TTextInput( { field: 'job_quick_search', disable_keyup_event: true } );
job_coder.addClass( 'job-coder' );
widgetContainer.append( job_coder );
widgetContainer.append( form_item_input );
this.addEditFieldToColumn( $.i18n._( 'Job' ), [form_item_input, job_coder], tab_schedule_column1, '', widgetContainer, true );
if ( !this.show_job_ui ) {
this.detachElement( 'job_id' );
}
//Job Item
form_item_input = Global.loadWidgetByName( FormItemType.AWESOME_BOX );
form_item_input.AComboBox( {
api_class: TTAPI.APIJobItem,
allow_multiple_selection: false,
layout_name: 'global_job_item',
show_search_inputs: true,
set_empty: true,
setRealValueCallBack: ( function( val ) {
if ( val ) {
job_item_coder.setValue( val.manual_id );
}
} ),
field: 'job_item_id',
addition_source_function: ( function( target, source_data ) {
return $this.onSourceDataCreate( target, source_data );
} ),
added_items: [
{ value: TTUUID.not_exist_id, label: Global.default_item },
{ value: '-2', label: Global.selected_item }
]
} );
widgetContainer = $( '' );
var job_item_coder = Global.loadWidgetByName( FormItemType.TEXT_INPUT );
job_item_coder.TTextInput( { field: 'job_item_quick_search', disable_keyup_event: true } );
job_item_coder.addClass( 'job-coder' );
widgetContainer.append( job_item_coder );
widgetContainer.append( form_item_input );
this.addEditFieldToColumn( $.i18n._( 'Task' ), [form_item_input, job_item_coder], tab_schedule_column1, '', widgetContainer, true );
if ( !this.show_job_item_ui ) {
this.detachElement( 'job_item_id' );
}
//Punch Tag
form_item_input = Global.loadWidgetByName( FormItemType.AWESOME_BOX );
form_item_input.AComboBox( {
api_class: TTAPI.APIPunchTag,
allow_multiple_selection: true,
layout_name: 'global_punch_tag',
show_search_inputs: true,
set_empty: true,
get_real_data_on_multi: true,
setRealValueCallBack: ( ( punch_tags, get_real_data ) => {
if ( punch_tags ) {
this.setPunchTagQuickSearchManualIds( punch_tags, get_real_data );
}
} ),
field: 'punch_tag_id',
addition_source_function: ( function( target, source_data ) {
return $this.onSourceDataCreatePunchTag( target, source_data );
} ),
added_items: [
{ value: TTUUID.not_exist_id, label: Global.default_item }
]
} );
widgetContainer = $( '' );
var punch_tag_coder = Global.loadWidgetByName( FormItemType.TEXT_INPUT );
punch_tag_coder.TTextInput( { field: 'punch_tag_quick_search', disable_keyup_event: true } );
punch_tag_coder.addClass( 'job-coder' );
widgetContainer.append( punch_tag_coder );
widgetContainer.append( form_item_input );
this.addEditFieldToColumn( $.i18n._( 'Tags' ), [form_item_input, punch_tag_coder], tab_schedule_column1, '', widgetContainer, true );
if ( !this.show_punch_tag_ui ) {
this.detachElement( 'punch_tag_id' );
}
}
//Note
form_item_input = Global.loadWidgetByName( FormItemType.TEXT_AREA );
form_item_input.TTextArea( { field: 'note', width: '100%' } );
this.addEditFieldToColumn( $.i18n._( 'Note' ), form_item_input, tab_schedule_column1, '', null, null, true );
form_item_input.parent().width( '45%' );
if ( Global.getProductEdition() >= 15 && this.is_viewing == false ) {
//Notify Employee
form_item_input = Global.loadWidgetByName( FormItemType.CHECKBOX );
form_item_input.TCheckbox( { field: 'notify_user_schedule_change' } );
this.addEditFieldToColumn( $.i18n._( 'Notify Employee' ), form_item_input, tab_schedule_column1, '', null, true );
}
TTPromise.resolve( 'Schedule', 'init' );
}
setDefaultMenuDeleteIcon( context_btn, grid_selected_length ) {
if ( !this.deletePermissionValidate( 'schedule' ) || this.edit_only_mode ) {
ContextMenuManager.hideMenuItem( this.determineContextMenuMountAttributes().id, context_btn.id, false )
}
if ( grid_selected_length > 0 && this.deleteOwnerOrChildPermissionValidate( 'schedule' ) ) {
ContextMenuManager.disableMenuItem( this.determineContextMenuMountAttributes().id, context_btn.id, true );
} else {
ContextMenuManager.disableMenuItem( this.determineContextMenuMountAttributes().id, context_btn.id, false );
}
}
onAvailableBalanceChange() {
if ( this.current_edit_record.status_id == 20
&& this.current_edit_record.hasOwnProperty( 'absence_policy_id' )
&& this.current_edit_record.absence_policy_id && !this.is_mass_editing ) {
this.getAvailableBalance();
} else {
this.detachElement( 'available_balance' );
}
}
parserDatesRange( date ) {
var dates = date.split( ' - ' );
var resultArray = [];
var beginDate = Global.strToDate( dates[0] );
var endDate = Global.strToDate( dates[1] );
var nextDate = beginDate;
while ( nextDate.getTime() < endDate.getTime() ) {
resultArray.push( nextDate.format() );
nextDate = new Date( new Date( nextDate.getTime() ).setDate( nextDate.getDate() + 1 ) );
}
resultArray.push( dates[1] );
return resultArray;
}
getSelectUsersArray() {
var result = [];
var cells_array = this.select_cells_Array;
var len = cells_array.length;
var date_dic = {};
for ( var i = 0; i < len; i++ ) {
var item = cells_array[i];
// If select empty cell with no user inside, set user_id to 0 as OPEN
if ( !item.user_id ) {
item.user_id = TTUUID.zero_id;
}
date_dic[item.user_id] = true;
}
for ( var key in date_dic ) {
result.push( key );
}
if ( result.length === 0 ) {
result.push( this.getDefaultUser() );
}
return result;
}
getSelectDateArray() {
var result = [];
var cells_array = this.select_cells_Array;
var len = cells_array.length;
var date_dic = {};
for ( var i = 0; i < len; i++ ) {
var item = cells_array[i];
if ( item.date ) {
date_dic[item.date] = true;
}
}
for ( var key in date_dic ) {
result.push( key );
}
return result;
}
getAvailableBalance() {
var $this = this;
var user_id = this.current_edit_record.user_id;
var total_time = this.current_edit_record.total_time;
var last_date_stamp = this.current_edit_record.start_date_stamp;
//On first run, set previous_absence_policy_id.
if ( this.previous_absence_policy_id == false ) {
this.previous_absence_policy_id = this.current_edit_record.absence_policy_id;
}
//For mass adding case, select multiple cells and click new
if ( this.is_mass_adding ) {
if ( this.current_edit_record.user_ids.length > 1 ) {
this.detachElement( 'available_balance' );
return;
} else {
user_id = this.current_edit_record.user_ids[0];
if ( !user_id ) {
this.detachElement( 'available_balance' );
return;
}
}
total_time = total_time * this.current_edit_record.start_dates.length;
last_date_stamp = this.current_edit_record.start_dates[this.current_edit_record.start_dates.length - 1];
} else {
//get dates from date ranger
if ( last_date_stamp.indexOf( ' - ' ) > 0 || $.type( last_date_stamp ) === 'array' ) {
if ( last_date_stamp.indexOf( ' - ' ) > 0 ) {
last_date_stamp = this.parserDatesRange( last_date_stamp );
}
if ( last_date_stamp.length > 0 ) {
total_time = total_time * last_date_stamp.length;
last_date_stamp = last_date_stamp[last_date_stamp.length - 1];
}
}
if ( ( !this.current_edit_record || !this.current_edit_record.id || this.current_edit_record.id == TTUUID.zero_id ) && !this.is_mass_editing ) {
if ( this.current_edit_record.user_ids.length < 1 || this.current_edit_record.user_ids.length > 1 ) {
this.detachElement( 'available_balance' );
return;
} else {
user_id = this.current_edit_record.user_ids[0];
if ( !user_id ) {
this.detachElement( 'available_balance' );
return;
}
}
}
}
//Check if the user is editing a existing record that was originally Status=Working, and changing it to Status=Absent, in that case we can't adjust for the existing total time and must clear it out.
if ( this.current_edit_record && this.current_edit_record.id && this.prev_status_id == 10 && this.edit_view_ui_dic['status_id'].getValue() == 20 ) {
this.prev_total_time = 0;
}
if ( this.current_edit_record.absence_policy_id != TTUUID.zero_id ) {
this.api_absence_policy.getProjectedAbsencePolicyBalance(
this.current_edit_record.absence_policy_id,
user_id,
last_date_stamp,
total_time,
this.prev_total_time,
this.previous_absence_policy_id, {
onResult: function( result ) {
$this.getBalanceHandler( result, last_date_stamp );
}
}
);
} else {
this.getBalanceHandler( false, last_date_stamp );
}
}
buildSearchAndLayoutUI() {
var layout_div = this.search_panel.find( 'div #saved_layout_content_div' );
//Display Columns
var form_item = $( $.fn.SearchPanel.html.form_item );
var form_item_label = form_item.find( '.form-item-label' );
var form_item_input_div = form_item.find( '.form-item-input-div' );
this.column_selector = Global.loadWidgetByName( FormItemType.AWESOME_DROPDOWN );
this.column_selector = this.column_selector.ADropDown( {
display_show_all: false,
id: 'column_selector',
key: 'value',
allow_drag_to_order: true,
display_close_btn: false,
display_column_settings: false,
static_height: 150
} );
form_item_label.text( $.i18n._( 'Display Columns' ) );
form_item_input_div.append( this.column_selector );
layout_div.append( form_item );
layout_div.append( '' );
this.column_selector.setColumns( [
{ name: 'label', index: 'label', label: $.i18n._( 'Column Name' ), width: 100, sortable: false }
] );
//Save and update layout
form_item = $( $.fn.SearchPanel.html.form_item );
form_item_label = form_item.find( '.form-item-label' );
form_item_input_div = form_item.find( '.form-item-input-div' );
form_item_label.text( $.i18n._( 'Save Search As' ) );
this.save_search_as_input = Global.loadWidgetByName( FormItemType.TEXT_INPUT );
this.save_search_as_input.TTextInput();
var save_btn = $( '' );
form_item_input_div.append( this.save_search_as_input );
form_item_input_div.append( save_btn );
var $this = this;
save_btn.click( function() {
$this.onSaveNewLayout();
} );
//Previous Saved Layout
this.previous_saved_layout_div = $( '' );
form_item_input_div.append( this.previous_saved_layout_div );
form_item_label = $( '' + $.i18n._( 'Previous Saved Searches' ) + ':' );
this.previous_saved_layout_div.append( form_item_label );
this.previous_saved_layout_selector = $( '