Ejemplo n.º 1
0
        public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context)
        {
            var functionArguments = arguments as FunctionArgument[] ?? arguments.ToArray();

            ValidateArguments(functionArguments, 2);
            var startDate  = System.DateTime.FromOADate(ArgToInt(functionArguments, 0));
            var nWorkDays  = ArgToInt(functionArguments, 1);
            var resultDate = System.DateTime.MinValue;

            var calculator = new WorkdayCalculator();
            var result     = calculator.CalculateWorkday(startDate, nWorkDays);

            if (functionArguments.Length > 2)
            {
                result = calculator.AdjustResultWithHolidays(result, functionArguments[2]);
            }
            return(CreateResult(result.EndDate.ToOADate(), DataType.Date));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Execute returns the date based on the user's input.
        /// </summary>
        /// <param name="arguments">The user specified date, number of workdays, and optional dates of holidays</param>
        /// <param name="context">Not used, but needed for overriding the method.</param>
        /// <returns>The date based on the date given and the number of workdays and optional holidays.</returns>
        public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context)
        {
            System.DateTime output;
            var             functionArguments = arguments as FunctionArgument[] ?? arguments.ToArray();

            if (this.ArgumentsAreValid(arguments, 2, out eErrorType argumentError) == false)
            {
                return(new CompileResult(argumentError));
            }

            var serialNumberCandidate = arguments.ElementAt(0).Value;
            var workDaysCandidate     = arguments.ElementAt(1).Value;

            if (workDaysCandidate == null || serialNumberCandidate == null)
            {
                return(new CompileResult(eErrorType.NA));
            }

            var serialNumberIsZero = (ConvertUtil.TryParseObjectToDecimal(serialNumberCandidate, out double parsedSerialNumber) &&
                                      parsedSerialNumber < 1 && parsedSerialNumber >= 0);

            if (serialNumberIsZero || ConvertUtil.TryParseDateObject(serialNumberCandidate, out output, out eErrorType? error))
            {
                if (serialNumberCandidate is int && this.ArgToInt(functionArguments, 1) < 0)
                {
                    return(new CompileResult(eErrorType.Num));
                }

                if (workDaysCandidate is string)
                {
                    if (!ConvertUtil.TryParseDateString(workDaysCandidate.ToString(), out output))
                    {
                        return(new CompileResult(eErrorType.Value));
                    }
                }

                var dateSerial = (int)this.ArgToDecimal(arguments, 0);
                if (dateSerial < 0)
                {
                    return(new CompileResult(eErrorType.Num));
                }

                if (serialNumberIsZero && this.ArgToInt(arguments, 1) < 0)
                {
                    return(new CompileResult(eErrorType.Num));
                }

                var startDate      = System.DateTime.FromOADate(dateSerial);
                var workDateSerial = this.ArgToDecimal(arguments, 1);
                var resultDate     = System.DateTime.MinValue;
                var calculator     = new WorkdayCalculator();
                var dateResult     = calculator.CalculateWorkday(startDate, (int)workDateSerial);

                if (functionArguments.Length > 2)
                {
                    var weekend = arguments.ElementAt(weekendIndex).Value;

                    if (this.WeekendSpecified(functionArguments))
                    {
                        if (weekend is int && ArgToInt(functionArguments, 2) <= 0)
                        {
                            return(new CompileResult(eErrorType.Num));
                        }

                        calculator = this.GetCalculator(weekend);

                        if (this.IsNumeric(weekend) && calculator == null)
                        {
                            return(new CompileResult(eErrorType.Num));
                        }
                        else if (calculator == null)
                        {
                            return(new CompileResult(eErrorType.Value));
                        }

                        dateResult = calculator.CalculateWorkday(startDate, (int)workDateSerial);
                    }
                }

                if (this.HolidaysSpecified(functionArguments))
                {
                    var  holidayCandidate = arguments.ElementAt(this.HolidayIndex).Value;
                    bool isHolidayZero    = (serialNumberCandidate is int holAsint && holAsint == 0);

                    if (holidayCandidate is int holAsInt && holAsInt < 0)
                    {
                        return(new CompileResult(eErrorType.Num));
                    }

                    if (holidayCandidate is string && !ConvertUtil.TryParseDateString(holidayCandidate, out output))
                    {
                        return(new CompileResult(eErrorType.Value));
                    }

                    dateResult = calculator.AdjustResultWithHolidays(dateResult, functionArguments[this.HolidayIndex]);
                }

                if (serialNumberIsZero)
                {
                    return(CreateResult(dateResult.EndDate.ToOADate() - 1, DataType.Date));
                }
                return(CreateResult(dateResult.EndDate.ToOADate(), DataType.Date));
            }
            else
            {
                return(new CompileResult(error.Value));
            }
        }