<commit_before>
/*****************************************************************************/
/* TravelRequisitionCreate: Event Handlers */
/*****************************************************************************/
import _ from 'underscore';

Template.TravelRequisitionCreate.events({
    'click #createProcurementRequisition': function(e, tmpl) {
        e.preventDefault()

        Modal.show('TravelRequisitionCreate')
    },
    "change .fieldInputField": _.throttle(function(e, tmpl) {
        const fieldName = $(e.target).attr('name');
        let inputVal = $(e.target).val().trim();
        const customConfig = tmpl.businessUnitCustomConfig.get();

        if(customConfig) {
            let travelRequestConfig = customConfig.travelRequestConfig;    
            if(travelRequestConfig) {
                let fields = travelRequestConfig.fields || [];
                fields.forEach(field => {
                    if(field.dbFieldName === fieldName) {
                        if(!tmpl[fieldName]) {
                            tmpl[fieldName] = new ReactiveVar();
                        }

                        if(field.type === 'String' || field.type === 'TextArea') {
                            tmpl[fieldName].set(inputVal)
                        } else if(field.type === 'Date' || field.type === 'Time') {
                            if(inputVal && inputVal.length > 0) {
                                const date = new Date(inputVal)
                                const momentObj = moment(date)

                                if(momentObj.isBefore(moment())) {
                                    if(!field.allowDatesInPast) {
                                        tmpl[fieldName].set(null)
                                        $(e.target).val(null)
                                    } else {
                                        if(inputVal && inputVal.length > 0)
                                            tmpl[fieldName].set(new Date(inputVal))
                                        else
                                            tmpl[fieldName].set(null)
                                    }
                                } else {
                                    if(inputVal && inputVal.length > 0)
                                        tmpl[fieldName].set(new Date(inputVal))
                                    else
                                        tmpl[fieldName].set(null)
                                }
                            } else {
                                tmpl[fieldName].set(null)
                                $(e.target).val(null)
                            }
                        }
                    }
                })
            }
        }   
    }, 200),
    "change .currencyField": _.throttle(function(e, tmpl) {
        var currency = $(e.target).val().trim();
        
        tmpl.selectedCurrency.set(currency)
        Meteor.defer(function() {
            $('.costInputField').selectpicker('refresh');
        });
    }, 200),
    "change .numberOfDaysField": _.throttle(function(e, tmpl) {
        const fieldName = $(e.target).attr('name');        
        var text = $(e.target).val().trim();
        
        if (!text || text.trim().length === 0) {
            text = "0"
        }
        let daysAsNumber = parseFloat(text)
        if(isNaN(daysAsNumber)) {
            daysAsNumber = 0
        }
        tmpl.selectedNumDays.set(daysAsNumber)
    }, 200),
    "change .costCenterField": _.throttle(function(e, tmpl) {
        var val = $(e.target).val().trim();
        
        if (!val || val.trim().length === 0) {
            tmpl.selectdCostCenter.set(val)
        } else {
            tmpl.selectdCostCenter.set(null)
        }
    }, 200),
    "keyup .costInputField": _.throttle(function(e, tmpl) {
        const fieldName = $(e.target).attr('name');        
        var text = $(e.target).val().trim();
        
        if (!text || text.trim().length === 0) {
            text = "0"
        }
        let costAsNumber = parseFloat(text)
        if(isNaN(costAsNumber)) {
            costAsNumber = 0
        }
        tmpl[fieldName].set(costAsNumber)
        tmpl.updateTotalTripCost()
    }, 200),

    'click #new-requisition-save-draft': function(e, tmpl) {
        e.preventDefault()
        let requisitionDoc = {}

        let currentUserUnitId = Template.instance().unitId.get()
        if(currentUserUnitId) {
            requisitionDoc.unitId = currentUserUnitId
        }
        //--
        const customConfig = tmpl.businessUnitCustomConfig.get();
        if(customConfig) {
            let travelRequestConfig = customConfig.travelRequestConfig;    
            if(travelRequestConfig) {
                let fields = travelRequestConfig.fields || [];<commit_msg>fix for travel cost on travel request modal<commit_after>
/*****************************************************************************/
/* TravelRequisitionCreate: Event Handlers */
/*****************************************************************************/
import _ from 'underscore';

Template.TravelRequisitionCreate.events({
    'click #createProcurementRequisition': function(e, tmpl) {
        e.preventDefault()

        Modal.show('TravelRequisitionCreate')
    },
    "change .fieldInputField": _.throttle(function(e, tmpl) {
        const fieldName = $(e.target).attr('name');
        let inputVal = $(e.target).val().trim();
        const customConfig = tmpl.businessUnitCustomConfig.get();

        if(customConfig) {
            let travelRequestConfig = customConfig.travelRequestConfig;    
            if(travelRequestConfig) {
                let fields = travelRequestConfig.fields || [];
                fields.forEach(field => {
                    if(field.dbFieldName === fieldName) {
                        if(!tmpl[fieldName]) {
                            tmpl[fieldName] = new ReactiveVar();
                        }

                        if(field.type === 'String' || field.type === 'TextArea') {
                            tmpl[fieldName].set(inputVal)
                        } else if(field.type === 'Date' || field.type === 'Time') {
                            if(inputVal && inputVal.length > 0) {
                                const date = new Date(inputVal)
                                const momentObj = moment(date)

                                if(momentObj.isBefore(moment())) {
                                    if(!field.allowDatesInPast) {
                                        tmpl[fieldName].set(null)
                                        $(e.target).val(null)
                                    } else {
                                        if(inputVal && inputVal.length > 0)
                                            tmpl[fieldName].set(new Date(inputVal))
                                        else
                                            tmpl[fieldName].set(null)
                                    }
                                } else {
                                    if(inputVal && inputVal.length > 0)
                                        tmpl[fieldName].set(new Date(inputVal))
                                    else
                                        tmpl[fieldName].set(null)
                                }
                            } else {
                                tmpl[fieldName].set(null)
                                $(e.target).val(null)
                            }
                        }
                    }
                })
            }
        }   
    }, 200),
    "change .currencyField": _.throttle(function(e, tmpl) {
        var currency = $(e.target).val().trim();
        
        tmpl.selectedCurrency.set(currency)
        Meteor.defer(function() {
            $('.costInputField').selectpicker('refresh');
        });
    }, 200),
    "change .numberOfDaysField": _.throttle(function(e, tmpl) {
        const fieldName = $(e.target).attr('name');        
        var text = $(e.target).val().trim();
        
        if (!text || text.trim().length === 0) {
            text = "0"
        }
        let daysAsNumber = parseFloat(text)
        if(isNaN(daysAsNumber)) {
            daysAsNumber = 0
        }
        tmpl.selectedNumDays.set(daysAsNumber)
    }, 200),
    "change .costCenterField": _.throttle(function(e, tmpl) {
        var val = $(e.target).val().trim();
        console.log(`val: `, val)
        
        if (!val || val.trim().length === 0) {
            tmpl.selectdCostCenter.set(val)
        } else {
            tmpl.selectdCostCenter.set(null)
        }
    }, 200),
    "change .costInputField": _.throttle(function(e, tmpl) {
        const fieldName = $(e.target).attr('name');        
        var text = $(e.target).val().trim();
        
        if (!text || text.trim().length === 0) {
            text = "0"
        }
        let costAsNumber = parseFloat(text)
        if(isNaN(costAsNumber)) {
            costAsNumber = 0
        }

        tmpl[fieldName].set(costAsNumber)
        tmpl.updateTotalTripCost()
    }, 200),
    "keyup .costInputField": _.throttle(function(e, tmpl) {
        const fieldName = $(e.target).attr('name');        
        var text = $(e.target).val().trim();
        
        if (!text || text.trim().length === 0) {
            text = "0"
        }
        let costAsNumber = parseFloat(text)
        if(isNaN(costAsNumber)) {
            costAsNumber = 0
        }

        tmpl[fieldName].set(costAsNumber)
        tmpl.updateTotalTripCost()
    }, 200),

    'click #new-requisition-save-draft': function(e, tmpl) {
        e.preventDefault()
        let requisitionDoc = {}

        let currentUserUnitId = Template.instance().unitId.get()
        if(currentUserUnitId) {
            requisitionDoc.unitId = currentUserUnitId
        }
        //--
        const customConfig = tmpl.businessUnitCustomConfig.get();
        if(customConfig) {
            let travelRequestConfig = customConfig.travelRequestConfig;    
            if(travelRequestConfig) {
                let fields = travelRequestConfig.fields || [];
{'fraction_non_alphanumeric': 0.1481299212598425, 'fraction_numerical': 0.005511811023622047, 'mean_word_length': 1.3198630136986302, 'pattern_counts': {'":': 11, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2898582', 'n_tokens_mistral': 2632, 'n_tokens_neox': 2472, 'n_words': 404}