Untitled

mail@pastecode.io avatarunknown
plain_text
2 months ago
4.4 kB
1
Indexable
Never
import { sum } from "rootDir/angular/shared/util/functions";

const reportTimeTypeKeys = {
    ON_CALL: 'onCall',
    OVERTIME: 'overtime',
    REGULAR: 'regular'
};

export default class ............................. {
    constructor($scope, ContractorWeeklyResource, reportTimeEvents, ReportTimeContentService) {
        this.$scope = $scope;
        this.resource = ContractorWeeklyResource;
        this.reportTimeContentService = ReportTimeContentService;
        this.events = reportTimeEvents;
        this.getValue = ReportTimeContentService.getValue;
        this._reload = this._reload.bind(this);
        this._sumHours = this._sumHours.bind(this);
    }


    $onInit() {
        this.$scope.$on(this.events.RELOAD_CONTENT, this._reload);
        this.rows = [];
    }

    _reload(_e, filters) {
        if (filters) {
            this.filters = filters;
        }
        this.week = this.filters.week[0];
        this.weekName = this.reportTimeContentService.getWeekName(this.week);

        this.resource.getWeeklyData(this.filters.week[0].dateFrom, this.userId).then(resp => {
            if (resp.data.activities.length === 0 || filters) {
                this.rows = resp.data.activities;
            } else {
                this._mergeRows(this.rows, resp.data.activities);
            }
            this._calculateSummary();
            this.summary.fullPeriodExpectedHours = resp.data.expectedWorkHours;
            this.reportTimeContentService.checkActivitiesWeeklyExtraTime(this.rows);
        });
    }

    _mergeRows(rows, updatedRows) {
        const customizer = (objValue, srcValue) => {
            if (_.isUndefined(objValue) || _.isString(objValue) || _.isNumber(objValue) || objValue.status || objValue.reportedHours) {
                return srcValue;
            }
        };
        updatedRows.forEach((updatedRow) => {
            let existingRow = _.find(rows, ['activityInfo.subactivityId', updatedRow.activityInfo.subactivityId]);
            existingRow ? _.mergeWith(existingRow, updatedRow, customizer) : rows.push(updatedRow);
        });
        if (rows.length > updatedRows.length) {
            _.remove(rows, (r) => _.includes(_.differenceBy(rows, updatedRows, 'activityInfo.subactivityId'), r));
        }
    }

    toggleDetails() {
        this.detailsVisible = !this.detailsVisible;
    }

    _calculateSummary() {
        this.summary = {
            reportedByType: {
                regular: sum(_.flatMap(_.flatMap(this._prepareRowsToCalculateSummary(), 'regular'), REPORTED_HOURS)),
                overtimeDay: this._sumHours('overtime', 'day', REPORTED_HOURS),
                overtimeHolidays: this._sumHours('overtime', 'holidays', REPORTED_HOURS),
                overtimeNight: this._sumHours('overtime', 'night', REPORTED_HOURS),
                onCallHolidays: this._sumHours('onCall', 'holidays', REPORTED_HOURS),
                onCallNonHolidays: this._sumHours('onCall', 'nonHolidays', REPORTED_HOURS),
                onCallWork: this._sumHours('onCall', 'work', REPORTED_HOURS)
            },
            expectedByType: {
                overtimeDay: this._sumHours('overtime', 'day', EXPECTED_HOURS),
                overtimeHolidays: this._sumHours('overtime', 'holidays', EXPECTED_HOURS),
                overtimeNight: this._sumHours('overtime', 'night', EXPECTED_HOURS),
                onCallHolidays: this._sumHours('onCall', 'holidays', EXPECTED_HOURS),
                onCallNonHolidays: this._sumHours('onCall', 'nonHolidays', EXPECTED_HOURS)
            }
        };
        this.summary.totalReported = sum([
            this.summary.reportedByType.regular,
            this.summary.reportedByType.overtimeDay,
            this.summary.reportedByType.overtimeHolidays,
            this.summary.reportedByType.overtimeNight,
            this.summary.reportedByType.onCallHolidays,
            this.summary.reportedByType.onCallNonHolidays,
            this.summary.reportedByType.onCallWork]);
    }

    _sumHours(serviceType, hoursType, fieldToSum) {
        return sum(_.flatMap(_.flatMap(_.flatMap(this._prepareRowsToCalculateSummary(), serviceType), hoursType), fieldToSum));
    }

    _prepareRowsToCalculateSummary() {
        return this.rows
            .filter(r => !this.reportTimeContentService.notApprovedStatus(r.activityInfo.activityStatus.status));
    }
}