angular.module('app.cases').controller('PostponeModal', PostponeModal);

PostponeModal.$inject = ['$uibModalInstance', '$scope', 'data', '$injector'];
function PostponeModal($uibModalInstance, $scope, data, $injector) {
    var vm = this;

    vm.type = data.type;
    vm.object = data.object;
    vm.dateField = data.dateField;
    vm.pickerIsOpen = false;
    vm.dateFormat = 'dd MMMM yyyy';
    vm.datepickerOptions = {
        startingDay: 1,
    };

    vm.disabledDates = disabledDates;
    vm.openDatePicker = openDatePicker;
    vm.postponeWithDays = postponeWithDays;
    vm.getFutureDate = getFutureDate;

    activate();

    ////

    function activate() {
        if (vm.object[vm.dateField]) {
            vm.date = moment(vm.object[vm.dateField]);
        } else {
            vm.date = moment();
        }

        vm.date = vm.date.format();

        _watchCloseDatePicker();
    }

    /**
     * When the datepicker popup is closed, update model and close modal
     *
     * @private
     */
    function _watchCloseDatePicker() {
        $scope.$watch('vm.pickerIsOpen', function(newValue, oldValue) {
            if (!newValue && oldValue) {
                _updateDayAndCloseModal();
            }
        });
    }

    function _updateDayAndCloseModal() {
        if (!moment(vm.date).isSame(moment(vm.object[vm.dateField]))) {
            // Update the due date for this case.
            var newDate = moment(vm.date).format('YYYY-MM-DD');

            var args = {
                id: vm.object.id,
            };

            args[vm.dateField] = newDate;
            // Update the model so changes are reflected instantly.
            vm.object[vm.dateField] = newDate;

            // Dynamically get the model that should be updated.
            $injector.get(vm.type).patch(args, function() {
                $uibModalInstance.close();
            });
        } else {
            $uibModalInstance.close();
        }
    }

    function disabledDates(date, mode) {
        if (!moment.isMoment(date)) {
            date = moment(date);
        }

        // Disable Saturday and Sunday.
        return ( mode === 'day' && ( date.day() === 6 || date.day() === 0 ) );
    }

    function openDatePicker($event) {
        $event.preventDefault();
        $event.stopPropagation();
        vm.pickerIsOpen = true;
    }

    function postponeWithDays(days) {
        vm.date = getFutureDate(days);

        _updateDayAndCloseModal();
    }

    function getFutureDate(days) {
        var futureDate = moment();

        if (futureDate.isBefore(vm.date)) {
            futureDate = moment(vm.date);
        }

        // Only skip weekends if we're postponing by 1 or 2 days.
        if (days !== 7 && futureDate.day() >= 5) {
            // Day is Saturday, so get the next monday and add the amount of days.
            futureDate = futureDate.add(1, 'week').day(days);
        } else if (days !== 7 && futureDate.day() === 0) {
            // Day is Sunday, which is a new week, so get the Monday and add the amount of days.
            futureDate = futureDate.day(1).day(days);
        } else {
            futureDate = futureDate.add(days, 'days');
        }

        return futureDate.format('YYYY-MM-DD');
    }
}

@@ -141,16 +141,27 @@
 njector'
+, 'HLUtils'
 %5D;%0Afunct
@@ -220,16 +220,25 @@
 injector
+, HLUtils
 ) %7B%0A    
@@ -993,16 +993,17 @@
 se modal
+.
 %0A     *%0A
@@ -1291,24 +1291,64 @@
 seModal() %7B%0A
+        var newDate;%0A        var args;%0A%0A
         if (
@@ -1465,28 +1465,24 @@
             
-var 
 newDate = mo
@@ -1526,28 +1526,24 @@
             
-var 
 args = %7B%0A   
@@ -2031,17 +2031,24 @@
 edDates(
-d
+currentD
 ate, mod
@@ -2056,29 +2056,35 @@
 ) %7B%0A        
-if (!
+var date = 
 moment.isMom
@@ -2091,43 +2091,44 @@
 ent(
-date)) %7B%0A            d
+currentDate) ? currentD
 ate 
-=
+:
  moment(
 date
@@ -2123,32 +2123,29 @@
  moment(
-date);%0A        %7D
+currentDate);
 %0A%0A      
@@ -2688,32 +2688,62 @@
 te);%0A        %7D%0A%0A
+        if (days !== 7) %7B%0A    
         // Only 
@@ -2804,403 +2804,65 @@
     
-if (days !== 7 && futureDate.day() %3E= 5) %7B%0A            // Day is Saturday, so get the next monday and add the amount of days.%0A            futureDate = futureDate.add(1, 'week').day(days);%0A        %7D else if (days !== 7 && futureDate.day() === 0) %7B%0A            // Day is Sunday, which is a new week, so get the Monday and add the amount of days.%0A            futureDate = futureDate.day(1).day(days
+    futureDate = HLUtils.addBusinessDays(days, futureDate
 );%0A 
@@ -2930,16 +2930,37 @@
  'days')
+.format('YYYY-MM-DD')
 ;%0A      
@@ -2989,37 +2989,16 @@
 tureDate
-.format('YYYY-MM-DD')
 ;%0A    %7D%0A

Fix postpone not always skipping weekends