public async Task <IActionResult> TableActualMonthly([FromBody] OptionChartViewModel Option)
        {
            var Message = "";

            try
            {
                if (Option != null && Option.SDate.HasValue && Option.EDate.HasValue)
                {
                    var HasData = await this.repository.GetToListAsync(
                        x => x,
                        x => x.Daily.Date >= Option.SDate.Value.Date &&
                        x.Daily.Date <= Option.EDate.Value.Date);

                    if (HasData != null)
                    {
                        #region TableData

                        var ActualFabTables = new List <ActualFabTable>();

                        foreach (var item in HasData.GroupBy(x => new { x.JobNo, x.GroupCode, x.GroupName }))
                        {
                            ActualFabTables.Add(new ActualFabTable
                            {
                                JobNo        = item.Key.JobNo,
                                WorkShopName = item?.FirstOrDefault()?.WorkShop ?? "-",
                                WorkGroup    = item.Key.GroupName,
                                NormalTime   = item.Sum(z => z.TotalManHour ?? 0),
                                OverTime     = item.Sum(z => z.TotalManHourOT ?? 0),
                                OverTimeX    = item.Sum(z => z.TotalManHourNTOT ?? 0)
                            });
                        }

                        #endregion TableData

                        return(new JsonResult(new
                        {
                            ActualFabTables = ActualFabTables.OrderBy(x => x.JobNo).ThenBy(x => x.WorkGroup),
                        }, this.DefaultJsonSettings));
                    }
                }
            }
            catch (Exception ex)
            {
                Message = $"Has error {ex.ToString()}";
            }

            return(BadRequest(new { Error = Message }));
        }
        public async Task <IActionResult> GetManHourDaily(OptionChartViewModel option)
        {
            var message = "Data not been found.";

            try
            {
                if (option != null)
                {
                    if (option.SDate.HasValue && option.EDate.HasValue)
                    {
                        var MapDatas      = new List <ActualDaily>();
                        var planWorkGroup = await this.repositoryNickName.GetToListAsync(x => x);

                        var conditionGroup = planWorkGroup.Select(x => x.GroupCode).ToList();
                        // Get workshop from MachineVipcoDatabase
                        var workShop = await this.repositoryWorkShop.GetToListAsync(x => x,
                                                                                    x => conditionGroup.Contains(x.GroupMis),
                                                                                    null, x => x.Include(z => z.WorkShop).Include(z => z.GroupMisNavigation));

                        // Get Employee total time by date from vipco
                        var HasVipcoTime = await this.repositoryEmpTimeVipco.GetToListAsync(x => x,
                                                                                            x => conditionGroup.Contains(x.GroupCode) &&
                                                                                            x.WorkDate >= option.SDate.Value.Date &&
                                                                                            x.WorkDate <= option.EDate.Value.Date &&
                                                                                            x.WorkDate != null
                                                                                            , x => x.OrderBy(z => z.WorkDate));

                        if (HasVipcoTime != null)
                        {
                            foreach (var itemV in HasVipcoTime)
                            {
                                var MapData  = this.mapper.Map <EmpTimeVipcoView, ActualDaily>(itemV);
                                var WorkNick = planWorkGroup.FirstOrDefault(x => x.GroupCode == itemV.GroupCode);

                                if (WorkNick != null)
                                {
                                    var CurWorkShop = workShop?.FirstOrDefault(x => x.GroupMis == MapData.GroupCode);
                                    if (CurWorkShop != null)
                                    {
                                        MapData.WorkShop  = CurWorkShop?.WorkShop?.WorkShopName ?? "-";
                                        MapData.GroupName = CurWorkShop?.GroupMisNavigation?.GroupDesc ?? "-";
                                    }
                                    else
                                    {
                                        var groupMis = await this.repositoryWorkGroup.
                                                       GetFirstOrDefaultAsync(x => x, x => x.GroupMis == MapData.GroupCode);

                                        MapData.GroupName = groupMis?.GroupDesc ?? "-";
                                    }


                                    MapData.NickName         = WorkNick?.NickName ?? MapData.GroupName;
                                    MapData.ActualDetailType = ActualDetailType.VIPCO;
                                    MapData.ActualType       = WorkNick?.ActualType ?? ActualType.NONE;
                                }

                                if (MapDatas.Any(x => x.GroupCode == MapData.GroupCode &&
                                                 x.JobNo == MapData.JobNo &&
                                                 x.Daily.Date == MapData.Daily))      // check if have data try to plus it
                                {
                                    var updateManHour = MapDatas.FirstOrDefault(x =>
                                                                                x.GroupCode == MapData.GroupCode &&
                                                                                x.JobNo == MapData.JobNo &&
                                                                                x.Daily.Date == MapData.Daily);

                                    updateManHour.TotalManHour     += MapData.TotalManHour;
                                    updateManHour.TotalManHourNTOT += MapData.TotalManHourNTOT;
                                    updateManHour.TotalManHourOT   += MapData.TotalManHourOT;
                                }
                                else // else add it to list
                                {
                                    MapDatas.Add(MapData);
                                }
                            }
                        }
                        // Get Employee total time by date from sub
                        var HasSubTime = await this.repositoryEmpTimeSub.GetToListAsync(x => x,
                                                                                        x => conditionGroup.Contains(x.GroupMIS) &&
                                                                                        x.WorkDate >= option.SDate.Value.Date &&
                                                                                        x.WorkDate <= option.EDate.Value.Date &&
                                                                                        x.WorkDate != null
                                                                                        , x => x.OrderBy(z => z.WorkDate));

                        if (HasSubTime != null)
                        {
                            foreach (var itemS in HasSubTime)
                            {
                                var MapDataSub  = this.mapper.Map <EmpTimeSubView, ActualDaily>(itemS);
                                var WorkNickSub = planWorkGroup.FirstOrDefault(x => x.GroupCode == MapDataSub.GroupCode);

                                if (WorkNickSub.ReferenceGroupCode != null && !string.IsNullOrEmpty(WorkNickSub.ReferenceGroupCode)) // If have change this group to other group
                                {
                                    MapDataSub.GroupCode        = WorkNickSub?.ReferenceGroupCode ?? "-";
                                    MapDataSub.ActualDetailType = ActualDetailType.VIPCO;
                                }
                                else
                                {
                                    MapDataSub.ActualDetailType = ActualDetailType.SUBCONTRACTOR;
                                }

                                if (MapDatas.Any(x => x.GroupCode == MapDataSub.GroupCode &&
                                                 x.JobNo == MapDataSub.JobNo &&
                                                 x.Daily.Date == MapDataSub.Daily))      // check if have data try to plus it
                                {
                                    var updateManHourSub = MapDatas.FirstOrDefault(x =>
                                                                                   x.GroupCode == MapDataSub.GroupCode &&
                                                                                   x.JobNo == MapDataSub.JobNo &&
                                                                                   x.Daily.Date == MapDataSub.Daily);

                                    updateManHourSub.TotalManHour     += MapDataSub.TotalManHour;
                                    updateManHourSub.TotalManHourNTOT += MapDataSub.TotalManHourNTOT;
                                    updateManHourSub.TotalManHourOT   += MapDataSub.TotalManHourOT;
                                }
                                else // else add it to list
                                {
                                    if (WorkNickSub != null)
                                    {
                                        var CurWorkShopSub = workShop?.FirstOrDefault(x => x.GroupMis == MapDataSub.GroupCode);

                                        if (CurWorkShopSub != null)
                                        {
                                            MapDataSub.WorkShop  = CurWorkShopSub?.WorkShop?.WorkShopName ?? "-";
                                            MapDataSub.GroupName = CurWorkShopSub?.GroupMisNavigation?.GroupDesc ?? "-";
                                        }
                                        else
                                        {
                                            var groupMis = await this.repositoryWorkGroup.
                                                           GetFirstOrDefaultAsync(x => x, x => x.GroupMis == MapDataSub.GroupCode);

                                            MapDataSub.GroupName = groupMis?.GroupDesc ?? "-";
                                        }


                                        MapDataSub.NickName         = WorkNickSub?.NickName ?? MapDataSub.GroupName;
                                        MapDataSub.ActualDetailType = ActualDetailType.VIPCO;
                                        MapDataSub.ActualType       = WorkNickSub?.ActualType ?? ActualType.NONE;
                                    }

                                    MapDatas.Add(MapDataSub);
                                }
                            }
                        }

                        if (MapDatas.Any())
                        {
                            return(new JsonResult(MapDatas.OrderBy(z => z.Daily).ThenBy(z => z.JobNo), this.DefaultJsonSettings));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                message = $"Has error {ex.ToString()}";
            }

            return(BadRequest(new { Error = message }));
        }