public List <MQCItem1> listMQCItemsOfDept(DateTime from, DateTime to, string site, string process)
        {
            List <MQCItem1> listMQCReturn = new List <MQCItem1>();

            try
            {
                List <MQCDataItems> listMQC = new List <MQCDataItems>();
                listMQC = listMQCDataItemsbySite(from, to, site, process);
                var ListItems = listMQC
                                .GroupBy(u => u.model)
                                .Select(grp => grp.ToList())
                                .ToList();

                foreach (var mQCDatas in ListItems)
                {
                    MQCItem1 mQCItem = new MQCItem1();

                    mQCItem.process    = process;
                    mQCItem.department = site;
                    mQCItem.product    = mQCDatas[0].model;
                    mQCItem.PO         = mQCDatas[0].lot;
                    mQCItem.TargetMQC  = new TargetMQC();
                    LoadTargetProduction loadTarget = new LoadTargetProduction();
                    mQCItem.TargetMQC = loadTarget.GetTargetMQC(mQCItem.product, DateTime.Now.Date.ToString("yyyyMMdd"));
                    var TotalOutputQty = mQCDatas
                                         .Where(d => d.remark == "OP")
                                         .Select((s => s.data))
                                         .ToList();
                    mQCItem.TotalOutput = TotalOutputQty.Sum();
                    var TotalNGQty = mQCDatas
                                     .Where(d => d.remark == "NG")
                                     .Select((s => s.data))
                                     .ToList();
                    mQCItem.TotalNG = TotalNGQty.Sum();
                    var TotalRework = mQCDatas
                                      .Where(d => d.remark == "RW")
                                      .Select((s => s.data))
                                      .ToList();
                    mQCItem.TotalRework   = TotalRework.Sum();
                    mQCItem.percentNG     = (mQCItem.TotalOutput + mQCItem.TotalNG + mQCItem.TotalRework) != 0 ? mQCItem.TotalNG / (mQCItem.TotalOutput + mQCItem.TotalNG + mQCItem.TotalRework) : 0;
                    mQCItem.percentRework = (mQCItem.TotalOutput + mQCItem.TotalNG + mQCItem.TotalRework) != 0 ? mQCItem.TotalRework / (mQCItem.TotalOutput + mQCItem.TotalNG + mQCItem.TotalRework) : 0;
                    List <MQCDataItems> listMQC_Error = new List <MQCDataItems>();
                    listMQC_Error = listMQCData_ErrorItems(from, to, mQCItem.product, mQCItem.PO, site, process);
                    mQCItem.InputMaterialNotYet = listMQC_Error.Where(w => w.remark == "OP" || w.remark == "NG").Select(d => d.data).ToArray().Sum();
                    double targetDefect = 0;
                    if (mQCItem.TargetMQC.TargetOutput != 0)
                    {
                        targetDefect = (mQCItem.TargetMQC.TargetDefect / (mQCItem.TargetMQC.TargetDefect + mQCItem.TargetMQC.TargetOutput));
                    }
                    if (mQCItem.InputMaterialNotYet > 0)
                    {
                        mQCItem.Status  = ProductionStatus.ShortageMaterial.ToString();
                        mQCItem.Measage = "Please supply material for production";
                    }
                    else if (mQCItem.percentNG > targetDefect && targetDefect > 0)
                    {
                        mQCItem.Status  = ProductionStatus.HighDefect.ToString();
                        mQCItem.Measage = "Defect is too much";
                    }
                    else
                    {
                        mQCItem.Status = ProductionStatus.Normal.ToString();
                    }
                    mQCItem.InputSFT = (mQCItem.TotalOutput + mQCItem.TotalNG) - mQCItem.InputMaterialNotYet;
                    listMQCReturn.Add(mQCItem);
                }
            }
            catch (Exception ex)
            {
                Log.Logfile.Output(Log.StatusLog.Error, "listMQCItemsOfDept()", ex.Message);
            }
            return(listMQCReturn);
        }
        public MQCItem1 GetQCCItemOK(DateTime from, DateTime to, string model, string lot, string site, string process)
        {
            MQCItem1 mQCItem = new MQCItem1();

            try
            {
                LoadDefectMapping     defectMapping   = new LoadDefectMapping();
                List <NGItemsMapping> nGItemsMappings = defectMapping.listNGMapping("B01", "MQC");
                mQCItem.TargetMQC = new TargetMQC();
                LoadTargetProduction loadTarget = new LoadTargetProduction();
                mQCItem.TargetMQC = loadTarget.GetTargetMQC(model, DateTime.Now.Date.ToString("yyyyMMdd"));
                List <MQCDataItems> listMQC = new List <MQCDataItems>();
                listMQC = listMQCDataItems(from, to, model, lot, site, process);
                //Load MQCItem to show
                mQCItem.process    = process;
                mQCItem.department = site;
                mQCItem.product    = model;

                mQCItem.PO = (listMQC != null && listMQC.Count > 0) ?   listMQC[0].lot: "";
                var TotalOutputQty = listMQC
                                     .Where(d => d.remark == "OP")
                                     .Select((s => s.data))
                                     .ToList();
                mQCItem.TotalOutput = TotalOutputQty.Sum();
                var TotalNGQty = listMQC
                                 .Where(d => d.remark == "NG")
                                 .Select((s => s.data))
                                 .ToList();
                mQCItem.TotalNG = TotalNGQty.Sum();
                var TotalRework = listMQC
                                  .Where(d => d.remark == "RW")
                                  .Select((s => s.data))
                                  .ToList();
                mQCItem.TotalRework   = TotalRework.Sum();
                mQCItem.percentNG     = (mQCItem.TotalOutput + mQCItem.TotalNG + mQCItem.TotalRework) != 0 ? mQCItem.TotalNG / (mQCItem.TotalOutput + mQCItem.TotalNG + mQCItem.TotalRework) :0;
                mQCItem.percentRework = (mQCItem.TotalOutput + mQCItem.TotalNG + mQCItem.TotalRework) != 0 ? mQCItem.TotalRework / (mQCItem.TotalOutput + mQCItem.TotalNG + mQCItem.TotalRework) : 0;
                var listNGItem = listMQC
                                 .Where(d => d.remark == "NG")
                                 .Select((s => new { s.item, s.data }))
                                 .ToList();
                mQCItem.listNGItems = new List <NGItems>();
                foreach (var item in listNGItem)
                {
                    NGItems nGItems = new NGItems();
                    var     _NG_SFT = nGItemsMappings.Where(d => d.NGCode_Process == item.item).Select(s => s.NGCode_SFT).ToArray();
                    string  NG_SFT  = (_NG_SFT != null && _NG_SFT.Count() > 0) ?_NG_SFT[0] : "";
                    nGItems.NGType = NG_SFT;
                    var    _NGName_SFT = nGItemsMappings.Where(d => d.NGCode_Process == item.item).Select(s => s.NGCodeName_SFT).ToArray();
                    string NGName_SFT  = (_NGName_SFT != null && _NGName_SFT.Count() > 0) ? _NGName_SFT[0] : "";
                    nGItems.NGName = NGName_SFT;

                    nGItems.NGKey      = item.item;
                    nGItems.NGQuantity = item.data;
                    mQCItem.listNGItems.Add(nGItems);
                }
                var listRWItem = listMQC
                                 .Where(d => d.item.Contains("RW"))
                                 .Select((s => new { s.item, s.data }))
                                 .ToList();
                mQCItem.listRWItems = new List <NGItems>();
                foreach (var item in listRWItem)
                {
                    NGItems nGItems    = new NGItems();
                    string  strReplace = item.item.Replace("RW", "NG");
                    var     _NG_SFT    = nGItemsMappings.Where(d => d.NGCode_Process == strReplace).Select(s => s.NGCode_SFT).ToArray();
                    string  NG_SFT     = (_NG_SFT != null && _NG_SFT.Count() > 0) ? _NG_SFT[0] : "";
                    nGItems.NGType = NG_SFT;
                    var    _NGName_SFT = nGItemsMappings.Where(d => d.NGCode_Process == strReplace).Select(s => s.NGCodeName_SFT).ToArray();
                    string NGName_SFT  = (_NGName_SFT != null && _NGName_SFT.Count() > 0) ? _NGName_SFT[0] : "";
                    nGItems.NGName     = NGName_SFT;
                    nGItems.NGKey      = item.item;
                    nGItems.NGQuantity = item.data;
                    mQCItem.listRWItems.Add(nGItems);
                }
                List <MQCDataItems> listMQC_Error = new List <MQCDataItems>();
                listMQC_Error = listMQCData_ErrorItems(from, to, model, lot, site, process);
                mQCItem.InputMaterialNotYet = listMQC_Error.Where(w => w.remark == "OP" || w.remark == "NG").Select(d => d.data).ToArray().Sum();
                double targetDefect = 0;
                if (mQCItem.TargetMQC.TargetOutput != 0)
                {
                    targetDefect = (mQCItem.TargetMQC.TargetDefect / (mQCItem.TargetMQC.TargetDefect + mQCItem.TargetMQC.TargetOutput));
                }

                if (mQCItem.InputMaterialNotYet > 0)
                {
                    mQCItem.Status  = ProductionStatus.ShortageMaterial.ToString();
                    mQCItem.Measage = "Please supply material for production";
                }
                else if (mQCItem.percentNG > targetDefect && targetDefect > 0)
                {
                    mQCItem.Status  = ProductionStatus.HighDefect.ToString();
                    mQCItem.Measage = "Defect is too much";
                }
                else
                {
                    mQCItem.Status = ProductionStatus.Normal.ToString();
                }
                mQCItem.InputSFT = (mQCItem.TotalOutput + mQCItem.TotalNG) - mQCItem.InputMaterialNotYet;
            }
            catch (Exception ex)
            {
                Log.Logfile.Output(Log.StatusLog.Error, "GetQCCItemOK", ex.Message);
            }
            // sql.Append()
            return(mQCItem);
        }
        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);
        }