public MQCItemSummary GetMQCItemSummaryByAmountOfTime(DateTime from, DateTime to, string site, string process)
        {
            MQCItemSummary itemSummary = new MQCItemSummary();

            try
            {
                LoadDataMQC         dataMQC      = new LoadDataMQC();
                List <MQCDataItems> mQCDataItems = dataMQC.listMQCDataItemsbyAmountOfTime(from, to, site, process);
                //Nhom theo san pham
                var ListItemsData = mQCDataItems
                                    .GroupBy(u => u.item)
                                    .Select(grp => grp.ToList())
                                    .ToList();

                var ListItemsTime = mQCDataItems
                                    .GroupBy(u => u.inspecttime)
                                    .ToList();



                itemSummary.product     = "";
                itemSummary.defectItems = new List <DefectItem>();

                //Khi thay doi ngay can phai chinh lai thoi gian
                itemSummary.Time_from = mQCDataItems.Min(d => d.inspecttime).ToString();
                itemSummary.Time_To   = mQCDataItems.Max(d => d.inspecttime).ToString();

                foreach (var itemData in ListItemsData)
                {
                    if (itemData[0].remark == "OP")
                    {
                        itemSummary.OutputQty = itemData.Select(d => d.data).Sum();
                    }
                    else if (itemData[0].remark == "NG")
                    {
                        DefectItem item = new DefectItem();
                        item.DefectCode = itemData[0].item;
                        item.Quantity   = itemData.Select(d => d.data).Sum();
                        LoadDefectMapping defectMapping  = new LoadDefectMapping();
                        NGItemsMapping    nGItemsMapping = defectMapping.GetNGMapping(site, process, item.DefectCode);
                        item.DefectSFT     = nGItemsMapping.NGCode_SFT;
                        item.DefectSFTName = nGItemsMapping.NGCodeName_SFT;
                        itemSummary.defectItems.Add(item);
                        itemSummary.NGQty += item.Quantity;
                    }
                }
                itemSummary.QuantityTotal = itemSummary.OutputQty + itemSummary.NGQty;
                itemSummary.DefectRate    = (itemSummary.QuantityTotal != 0) ? (itemSummary.NGQty / itemSummary.QuantityTotal) : 0;
            }
            catch (Exception ex)
            {
                Log.Logfile.Output(Log.StatusLog.Error, "GetMQCItemSummaries(DateTime from, DateTime to, string site, string process)", ex.Message);
            }
            return(itemSummary);
        }
        public DefectRateData GetDefectRateReportAmountOfTime(DateTime from, DateTime to, string Dept, string codeProcess)
        {
            DefectRateData defectRate = new DefectRateData();

            try
            {
                //code lay tren ERP va SFT
                //     StringBuilder sql = new StringBuilder();
                //     sql.Append("select sum(TA011) as outputQty, sum(TA012) as DefectQTy, sum(TA011)+ sum(TA012) as TotalQty ");
                //     sql.Append("from SFCTA ");
                //     sql.Append("where 1=1 ");
                //     sql.Append("and TA004 = '" + Dept + "'");
                //     sql.Append("and TA003 = '" + codeProcess + "'");
                //     sql.Append("and CREATE_DATE >= '" + from.ToString("yyyyMMdd") + "'");
                //     sql.Append("and CREATE_DATE <= '" + to.ToString("yyyyMMdd") + "'");
                //     sqlERPCON sqlERPCON = new sqlERPCON();
                //     DataTable dt = new DataTable();
                //     sqlERPCON.sqlDataAdapterFillDatatable(sql.ToString(), ref dt);

                //var     defectItems = (from DataRow dr in dt.Rows
                //                        select new DefectRateData()
                //                        {
                //                           TotalQuantity  =double.Parse( dr["TotalQty"].ToString()),
                //                            DefectQuantity = double.Parse( dr["DefectQTy"].ToString()),
                //                            OutputQuantity = double.Parse(dr["outputQty"].ToString())

                //                        }).ToList();

                //     defectRate = defectItems[0];
                //     defectRate.DateTime_from = from;
                //     defectRate.DateTime_to = to;
                //     defectRate.DefectRate = (defectRate.TotalQuantity != 0) ? (defectRate.DefectQuantity / defectRate.TotalQuantity) : 0;


                LoadDataSummary loadData = new LoadDataSummary();
                MQCItemSummary  mQCItems = loadData.GetMQCItemSummaryByAmountOfTime(from, to, Dept, "MQC");
                defectRate.TotalQuantity  = mQCItems.QuantityTotal;
                defectRate.DefectQuantity = mQCItems.NGQty;
                defectRate.OutputQuantity = mQCItems.OutputQty;
                defectRate.DefectRate     = (defectRate.TotalQuantity != 0) ? (defectRate.DefectQuantity / defectRate.TotalQuantity) : 0;
                LoadDefectMapping     loadDefectTop5 = new LoadDefectMapping();
                List <NGItemsMapping> listTop5       = loadDefectTop5.listNGMappingGetReport(Dept, "MQC");
                List <DefectItem>     listDefectTop5 = new List <DefectItem>();
                for (int i = 0; i < listTop5.Count; i++)
                {
                    var getlist = mQCItems.defectItems.Where(d => d.DefectSFT == listTop5[i].NGCode_SFT).ToList();

                    DefectItem defect = new DefectItem();
                    if (getlist != null && getlist.Count > 0)
                    {
                        defect          = getlist[0];
                        defect.Quantity = getlist.Select(s => s.Quantity).Sum();
                    }
                    defect.Note = listTop5[i].Note;
                    listDefectTop5.Add(defect);
                }
                var listDefectTop5Groupby = listDefectTop5.OrderBy(d => d.Note).ToList();
                defectRate.defectItems = listDefectTop5Groupby;
            }
            catch (Exception ex)
            {
                Log.Logfile.Output(Log.StatusLog.Error, "GetDefectRateReport(DateTime from, DateTime to, string Dept, string codeProcess)", ex.Message);
            }
            return(defectRate);
        }
        public List <MQCItemSummary> GetMQCItemSummaries(PeriodProduction period, string site, string process)
        {
            DateTime from = new DateTime(); DateTime to = new DateTime();

            DateTimeControl.ReturnDateTimePeriodProduction(period, ref from, ref to);
            string date = from.ToString("yyyy-MM-dd");
            string time = from.ToString("HH:mm:ss");
            List <MQCItemSummary> qCItemSummaries = new List <MQCItemSummary>();

            try
            {
                LoadDataMQC         dataMQC      = new LoadDataMQC();
                List <MQCDataItems> mQCDataItems = dataMQC.listMQCDataItemsbySite(from, to, site, process);
                //Nhom theo san pham
                var ListMQCbyProduct = mQCDataItems
                                       .GroupBy(u => u.lot)
                                       .Select(grp => grp.ToList())
                                       .ToList();
                foreach (var qCDataItems in ListMQCbyProduct)
                {
                    MQCItemSummary itemSummary = new MQCItemSummary();
                    itemSummary.product     = qCDataItems[0].model;
                    itemSummary.defectItems = new List <DefectItem>();
                    var ListItemsData = qCDataItems
                                        .GroupBy(u => u.item)
                                        .Select(grp => grp.ToList())
                                        .ToList();

                    var ListItemsDate = qCDataItems
                                        .GroupBy(u => u.inspectdate)
                                        .ToList();
                    foreach (var itemDate in ListItemsDate)
                    {
                        var ListItemsTime = qCDataItems
                                            .GroupBy(u => u.inspecttime)
                                            .ToList();
                        if (ListItemsDate.Count() == 1)
                        {
                            itemSummary.Time_from = "[" + ListItemsDate[0].Key.ToString("dd-MM-yy") + "] " + ListItemsTime.Min(d => d.Key).ToString();
                            itemSummary.Time_To   = "[" + ListItemsDate[0].Key.ToString("dd-MM-yy") + "] " + ListItemsTime.Max(d => d.Key).ToString();
                        }
                        else if (ListItemsDate.Count() == 2)
                        {
                            itemSummary.Time_from = "[" + ListItemsDate[0].Key.ToString("dd-MM-yy") + "] " + ListItemsTime.Min(d => d.Key).ToString();
                            itemSummary.Time_To   = "[" + ListItemsDate[1].Key.ToString("dd-MM-yy") + "] " + ListItemsTime.Max(d => d.Key).ToString();
                        }
                    }
                    //Khi thay doi ngay can phai chinh lai thoi gian


                    foreach (var itemData in ListItemsData)
                    {
                        DefectItem item = new DefectItem();
                        item.DefectCode = itemData[0].item;
                        itemSummary.Lot = itemData[0].lot;
                        item.Quantity   = itemData.Select(d => d.data).Sum();

                        if (itemData[0].remark == "OP")
                        {
                            itemSummary.OutputQty += item.Quantity;
                        }
                        else if (itemData[0].remark == "NG")
                        {
                            LoadDefectMapping defectMapping  = new LoadDefectMapping();
                            NGItemsMapping    nGItemsMapping = defectMapping.GetNGMapping(site, process, item.DefectCode);
                            item.DefectSFT     = nGItemsMapping.NGCode_SFT;
                            item.DefectSFTName = nGItemsMapping.NGCodeName_SFT;
                            itemSummary.defectItems.Add(item);
                            itemSummary.NGQty += item.Quantity;
                        }
                    }
                    itemSummary.QuantityTotal = itemSummary.OutputQty + itemSummary.NGQty;
                    itemSummary.DefectRate    = (itemSummary.QuantityTotal != 0) ? (itemSummary.NGQty / itemSummary.QuantityTotal) : 0;
                    qCItemSummaries.Add(itemSummary);
                }
            }
            catch (Exception ex)
            {
                Log.Logfile.Output(Log.StatusLog.Error, "GetMQCItemSummaries(DateTime from, DateTime to, string site, string process)", ex.Message);
            }
            return(qCItemSummaries);
        }
        public List <DefectRateData> GetListDefectRateReportAmountOfTimeDaily(string Dept, string codeProcess, PeriodProduction period)
        {
            List <DefectRateData> defectRates = new List <DefectRateData>();

            try
            {
                //code lay tren ERP va SFT
                //     StringBuilder sql = new StringBuilder();
                //     sql.Append("select sum(TA011) as outputQty, sum(TA012) as DefectQTy, sum(TA011)+ sum(TA012) as TotalQty ");
                //     sql.Append("from SFCTA ");
                //     sql.Append("where 1=1 ");
                //     sql.Append("and TA004 = '" + Dept + "'");
                //     sql.Append("and TA003 = '" + codeProcess + "'");
                //     sql.Append("and CREATE_DATE >= '" + from.ToString("yyyyMMdd") + "'");
                //     sql.Append("and CREATE_DATE <= '" + to.ToString("yyyyMMdd") + "'");
                //     sqlERPCON sqlERPCON = new sqlERPCON();
                //     DataTable dt = new DataTable();
                //     sqlERPCON.sqlDataAdapterFillDatatable(sql.ToString(), ref dt);

                //var     defectItems = (from DataRow dr in dt.Rows
                //                        select new DefectRateData()
                //                        {
                //                           TotalQuantity  =double.Parse( dr["TotalQty"].ToString()),
                //                            DefectQuantity = double.Parse( dr["DefectQTy"].ToString()),
                //                            OutputQuantity = double.Parse(dr["outputQty"].ToString())

                //                        }).ToList();

                //     defectRate = defectItems[0];
                //     defectRate.DateTime_from = from;
                //     defectRate.DateTime_to = to;
                //     defectRate.DefectRate = (defectRate.TotalQuantity != 0) ? (defectRate.DefectQuantity / defectRate.TotalQuantity) : 0;


                LoadDataSummary       loadData     = new LoadDataSummary();
                List <MQCItemSummary> ListmQCItems = loadData.GetMQCItemSummaries(period, Dept, "MQC");
                foreach (var mQCItems in ListmQCItems)
                {
                    DefectRateData defectRate = new DefectRateData();
                    defectRate.Lot            = mQCItems.Lot;
                    defectRate.Product        = mQCItems.product;
                    defectRate.DateTime_from  = mQCItems.Time_from;
                    defectRate.DateTime_to    = mQCItems.Time_To;
                    defectRate.TotalQuantity  = mQCItems.QuantityTotal;
                    defectRate.DefectQuantity = mQCItems.NGQty;
                    defectRate.OutputQuantity = mQCItems.OutputQty;
                    defectRate.DefectRate     = (defectRate.TotalQuantity != 0) ? (defectRate.DefectQuantity / defectRate.TotalQuantity) : 0;
                    LoadDefectMapping     loadDefectTop13 = new LoadDefectMapping();
                    List <NGItemsMapping> listTop13       = loadDefectTop13.listNGMappingGetReportTop13(Dept, "MQC");
                    List <DefectItem>     listDefectTop13 = new List <DefectItem>();
                    for (int i = 0; i < listTop13.Count; i++)
                    {
                        var getlist = mQCItems.defectItems.Where(d => d.DefectSFT == listTop13[i].NGCode_SFT).ToList();

                        DefectItem defect = new DefectItem();
                        if (getlist != null && getlist.Count > 0)
                        {
                            defect          = getlist[0];
                            defect.Quantity = getlist.Select(s => s.Quantity).Sum();
                        }
                        defect.Note = listTop13[i].Note;
                        listDefectTop13.Add(defect);
                    }
                    var listDefectTop13Groupby = listDefectTop13.OrderBy(d => d.Note).ToList();
                    defectRate.defectItems = listDefectTop13Groupby;
                    DateTime dateTarget = DateTime.Now.Date;
                    if (period == PeriodProduction.AllDay)
                    {
                        dateTarget = DateTime.Now.Date.AddDays(-1);
                    }
                    else if (period == PeriodProduction.dayshift)
                    {
                        dateTarget = DateTime.Now.Date;
                    }
                    else if (period == PeriodProduction.nightshift)
                    {
                        dateTarget = DateTime.Now.Date.AddDays(-1);
                    }
                    defectRate.TargetMQC = new TargetMQC();
                    LoadTargetProduction loadTarget = new LoadTargetProduction();
                    defectRate.TargetMQC = loadTarget.GetTargetMQC(defectRate.Product, dateTarget.ToString("yyyyMMdd"));
                    defectRates.Add(defectRate);
                }
            }
            catch (Exception ex)
            {
                Log.Logfile.Output(Log.StatusLog.Error, "GetDefectRateReport(DateTime from, DateTime to, string Dept, string codeProcess)", ex.Message);
            }
            return(defectRates);
        }