public DataView PrintCPRpt(ControlPlanRptModel cpRptModel, string seqNos)
        {
            DataView dtv = new DataView();
            string   sqlLoca, sqlCustQry, sqlOrgDateQry, sqlRevDateQry = "";
            string   supplierLoc = "";

            try
            {
                StringBuilder sbsql = new StringBuilder();
                sqlLoca = " select bif_proj from prd_mast where part_no= '" + cpRptModel.PartNo + "'";
                // sqlCustQry = "select CUST_DWG_NO from  ddci_info where  ci_reference in (select ci_ref from prd_ciref where part_no='" + cpRptModel.PartNo + "' AND CURRENT_CIREF = 1 )";
                sqlOrgDateQry = "select issue_date from pccs_issue  where (ISSUE_NO='1' or ISSUE_NO='01' or ISSUE_NO='001') AND part_no =  '" + cpRptModel.PartNo + "' and route_no in (select route_no from process_main where part_no = '" + cpRptModel.PartNo + "' and current_proc = 1)";
                //  "select min(ISSUE_DATE) as ISSUE_DATE from PCCS_ISSUE where PART_NO='" + cpRptModel.PartNo + "'";

                ///Location
                try
                {
                    supplierLoc = DB.ExecuteQuery <string>(sqlLoca.ToString()).FirstOrDefault <string>().ToValueAsString();
                    if (supplierLoc.ToUpper().Trim() == "MM" || supplierLoc.ToUpper().Trim() == "MN" || supplierLoc.ToUpper().Trim() == "MS")
                    {
                        supplierLoc = "Padi";
                    }
                    else if (supplierLoc.ToUpper().Trim() == "KK" || supplierLoc.ToUpper().Trim() == "KS")
                    {
                        supplierLoc = "Krishnapuram";
                    }
                    else
                    {
                        supplierLoc = "Pondy";
                    }
                    ///End
                    //IEnumerable<string> sqlCusts = DB.ExecuteQuery<string>(sqlCustQry).ToList();
                    //if (sqlCusts != null)
                    //{
                    //    sqlCustQry = sqlCusts.FirstOrDefault();
                    //}
                    //else
                    //{
                    //    sqlCustQry = "";
                    //}

                    //List<PCCS_ISSUE> lstOrgDate = (from row in DB.PCCS_ISSUE
                    //                               where row.PART_NO == cpRptModel.PartNo
                    //                               orderby row.ISSUE_DATE
                    //                               select row).ToList<PCCS_ISSUE>();

                    /// OrgDate -Revised Date
                    DateTime?sqlOrgDate = null;
                    DateTime?sqlRevDate = null;
                    //if (lstOrgDate.IsNotNullOrEmpty() && lstOrgDate.Count > 0)
                    //{
                    //    sqlOrgDate = lstOrgDate[0].ISSUE_DATE;
                    //    sqlRevDate = lstOrgDate[lstOrgDate.Count - 1].ISSUE_DATE;
                    //}
                    sqlOrgDate = DB.ExecuteQuery <DateTime>(sqlOrgDateQry.ToString()).FirstOrDefault <DateTime>();
                    if (sqlOrgDate != null && Convert.ToDateTime(sqlOrgDate).ToString("dd/MM/yyyy") != "01/01/0001")
                    {
                        if (sqlOrgDate.IsNotNullOrEmpty())
                        {
                            sqlOrgDateQry = Convert.ToDateTime(sqlOrgDate).ToString("dd/MM/yyyy");
                        }
                        else
                        {
                            sqlOrgDateQry = "--";
                        }
                    }
                    else
                    {
                        sqlOrgDateQry = "--";
                    }
                    //IEnumerable<Nullable<DateTime>> sqlRevDate = DB.ExecuteQuery<Nullable<DateTime>>(sqlRevDateQry);

                    sqlRevDateQry = "select max(ISSUE_DATE) as ISSUE_DATE from PCCS_ISSUE where PART_NO='" + cpRptModel.PartNo + "' and route_no='" + cpRptModel.RouteNo + "'";
                    sqlRevDate    = DB.ExecuteQuery <DateTime>(sqlRevDateQry.ToString()).FirstOrDefault <DateTime>();
                    sqlRevDateQry = "select * from pccs_issue where  part_no='" + cpRptModel.PartNo + "' and route_no='" + cpRptModel.RouteNo + "'";
                    DataTable dtrev = ToDataTableWithType(DB.ExecuteQuery <PCCS_ISSUE>(sqlRevDateQry).ToList());
                    if (sqlRevDate != null && Convert.ToDateTime(sqlRevDate).ToString("dd/MM/yyyy") != "01/01/0001")
                    {
                        if (sqlRevDate.IsNotNullOrEmpty())
                        {
                            dtrev.DefaultView.RowFilter = "issue_date='" + sqlRevDate.ToDateAsString() + "'";
                        }
                        if (dtrev.Rows.Count > 0 && dtrev.DefaultView[0]["issue_date"].IsNotNullOrEmpty())
                        {
                            if (dtrev.DefaultView[0]["issue_no"].ToValueAsString() == "1")
                            {
                                sqlRevDateQry = "--";
                            }
                            else
                            {
                                sqlRevDateQry = Convert.ToDateTime(dtrev.DefaultView[0]["issue_date"]).ToString("dd/MM/yyyy");
                            }
                        }
                        else
                        {
                            sqlRevDateQry = "--";
                        }
                        dtrev.DefaultView.RowFilter = string.Empty;
                    }
                    else
                    {
                        sqlRevDateQry = "--";
                    }
                }
                catch (Exception ex)
                {
                    ex.LogException();
                }
                /// End OrgDate -Revised Date
                ///
                RetreieveCustomerName(cpRptModel);
                string suppApprDate  = "____";
                string suppOtherDate = "____";
                if (cpRptModel.SupplierApprDate != null)
                {
                    if (cpRptModel.SupplierApprDate.IsNotNullOrEmpty())
                    {
                        suppApprDate = Convert.ToDateTime(cpRptModel.SupplierApprDate).ToString("dd/MM/yyyy");
                    }
                    else
                    {
                        suppApprDate = "____";
                    }
                }
                if (cpRptModel.OtherApprDate != null)
                {
                    if (cpRptModel.OtherApprDate.IsNotNullOrEmpty())
                    {
                        suppOtherDate = Convert.ToDateTime(cpRptModel.OtherApprDate).ToString("dd/MM/yyyy");
                    }
                    else
                    {
                        suppOtherDate = "____";
                    }
                }
                string partNum = cpRptModel.PartNo;
                try
                {
                    DataTable dt            = new DataTable();
                    decimal?  dwgIssueMaxNo = 0;
                    try
                    {
                        dwgIssueMaxNo = (from o in DB.PRD_DWG_ISSUE
                                         where o.PART_NO == cpRptModel.PartNo && o.DWG_TYPE == 0
                                         select o.ISSUE_NO).Max();
                    }
                    catch (Exception ex)
                    {
                    }

                    if (dwgIssueMaxNo.IsNotNullOrEmpty() && dwgIssueMaxNo != 0)
                    {
                        // Check DWG_TYPE as 0 to get date from product drawing (if it is 1 then it is sequence drawing)
                        PRD_DWG_ISSUE dwgIssue = (from o in DB.PRD_DWG_ISSUE
                                                  where o.PART_NO == cpRptModel.PartNo && o.ISSUE_NO == dwgIssueMaxNo && o.DWG_TYPE == 0
                                                  select o).FirstOrDefault <PRD_DWG_ISSUE>();
                        if (dwgIssue.IsNotNullOrEmpty())
                        {
                            if (dwgIssue.ISSUE_NO.IsNotNullOrEmpty())
                            {
                                partNum = partNum + "/ " + dwgIssue.ISSUE_NO;
                            }
                            if (dwgIssue.ISSUE_DATE.IsNotNullOrEmpty())
                            {
                                partNum = partNum + "/ " + Convert.ToDateTime(dwgIssue.ISSUE_DATE).ToString("dd/MM/yyyy");
                            }
                        }
                        else
                        {
                            partNum = cpRptModel.PartNo + "/ --";
                        }
                    }
                }
                catch (Exception ex)
                {
                    partNum = cpRptModel.PartNo + "/ --";
                    ex.LogException();
                }

                //new by me

                DateTime?pccsIssueMaxDate = (from o in DB.PCCS_ISSUE
                                             where o.PART_NO == cpRptModel.PartNo && o.ROUTE_NO == cpRptModel.RouteNo.ToDecimalValue()
                                             select o.ISSUE_DATE).Max();
                if (pccsIssueMaxDate.IsNotNullOrEmpty())
                {
                    PCCS_ISSUE pccsIssue = (from o in DB.PCCS_ISSUE
                                            where o.PART_NO == cpRptModel.PartNo && o.ROUTE_NO == cpRptModel.RouteNo.ToDecimalValue() && o.ISSUE_DATE == pccsIssueMaxDate.Value.Date
                                            select o).FirstOrDefault <PCCS_ISSUE>();
                    if (pccsIssue.IsNotNullOrEmpty())
                    {
                        if (cpRptModel.ControlPlanType == "PRE LAUNCH")
                        {
                            cpRptModel.ControlPlanNo = "CP-PL/" + cpRptModel.PartNo + "/ " + String.Format("{0,2:00}", pccsIssue.ISSUE_NO.ToIntValue());
                        }
                        else if (cpRptModel.ControlPlanType == "PRODUCTION")
                        {
                            cpRptModel.ControlPlanNo = "CP-PN/" + cpRptModel.PartNo + "/ " + String.Format("{0,2:00}", pccsIssue.ISSUE_NO.ToIntValue());
                        }
                        else if (cpRptModel.ControlPlanType == "PROTOTYPE")
                        {
                            cpRptModel.ControlPlanNo = "CP-PT/" + cpRptModel.PartNo + "/ " + String.Format("{0,2:00}", pccsIssue.ISSUE_NO.ToIntValue());
                        }
                    }
                }

                //end new

                sbsql.Append("select '" + cpRptModel.PartNo + "' as PartNo,  '" + cpRptModel.RouteNo + "' as ProcessNo,  '" + cpRptModel.ControlPlanType + "' as ControlPlanType, '" + cpRptModel.ControlPlanNo + "' as ControlPlanNo, '" + cpRptModel.Fax + "' as Fax, '" + cpRptModel.Phone + "' as Phone,  '" + cpRptModel.Ctm1 + "' as Ctm1,  '" + cpRptModel.Ctm2 + "' as Ctm2,");
                sbsql.Append(" '" + cpRptModel.Ctm3 + "' as Ctm3, '" + cpRptModel.Ctm4 + "' as Ctm4, '" + cpRptModel.Ctm5 + "' as Ctm5, '" + cpRptModel.Ctm6 + "' as Ctm6,  '" + cpRptModel.Ctm7 + "' as Ctm7, '" + partNum + "' as SupplierPartNo, '" + custdwgNo + "' as CustomerPartNo, d.part_desc as PartNameDesc,d.bif_proj as locCode,'" + supplierLoc + "' as SupplierLocation,'" + supplierCode + "' as SupplierCode, '' as PreparedBy, '' as ApprovedBy, '" + sqlOrgDateQry + "' as DateOrg, '" + sqlRevDateQry + "' as DateRev,'" + suppApprDate + "' as SupplierApprDate,'" + suppOtherDate + "' as OtherApprDate, '" + cpRptModel.KeyContactPerson + "' as ContactPerson, ' ___ ' as CustomerEnggApprov,");
                sbsql.Append(" ' ____ ' as CustomerQualityApprov, ' ____ ' as OtherApprovalcustomer,cast(b.seq_no as varchar) as PartProcessNo,b.opn_desc as ProcessNameOperationDesc, (SELECT Stuff((SELECT N'/ ' + cc_code FROM process_cc where part_no=a.part_no and route_no=C.route_no and seq_no= CAST(b.seq_no AS VARCHAR) FOR XML PATH(''),TYPE) .value('text()[1]','nvarchar(max)'),1,2,N'')  ) AS MachineDevice,cast(a.ISR_NO as varchar)  as IsrNo,a.feature as Product,cast(a.PROCESS_FEATURE as varchar)  as Process,ISNULL(a.SPEC_CHAR,'') as SplChar,(ISNULL(a.CTRL_SPEC_MIN,'')+ (CASE WHEN SUBSTRING(a.CTRL_SPEC_MIN, 1, LEN(a.CTRL_SPEC_MIN)) Like '%[0-9]%' AND SUBSTRING(a.CTRL_SPEC_MAX, 1, LEN(a.CTRL_SPEC_MAX)) Like '%[0-9]%' THEN  '/' ELSE  ' ' END) +ISNULL(a.CTRL_SPEC_MAX,'')) as ProcessSpec,");
                sbsql.Append("a.gauges_used as Gauges_Used,cast(a.sample_size as varchar) as SampleSize,a.freq_of_insp as SampleFreq,a.control_method as ControlMethod,a.reaction_plan as ReactionPlan,(select max(issue_date) from pccs_issue p where p.part_no =b.part_no and p.route_no=b.route_no) as issue_date ");
                if (seqNos == "")
                {
                    sbsql.Append("from pccs a,process_sheet b,process_cc c ,prd_mast  d where A.part_no = d.part_no and A.part_no = B.part_no and a.route_no = b.route_no and a.seq_no = b.seq_no and B.part_no = C.part_no and  b.route_no = c.route_no and b.seq_no = c.seq_no and C.part_no = '" + cpRptModel.PartNo + "'  and C.route_no ='" + cpRptModel.RouteNo + "' and cc_sno='1' ORDER BY A.seq_no,A.SNO");
                }
                else
                {
                    sbsql.Append("from pccs a,process_sheet b,process_cc c ,prd_mast  d where A.part_no = d.part_no and A.part_no = B.part_no and a.route_no = b.route_no and a.seq_no = b.seq_no and B.part_no = C.part_no and  b.route_no = c.route_no and b.seq_no = c.seq_no and C.part_no = '" + cpRptModel.PartNo + "'  and C.route_no ='" + cpRptModel.RouteNo + "' and c.seq_no in('" + seqNos + "') and cc_sno='1' ORDER BY A.seq_no,A.SNO");
                }

                dtv = ToDataTable(DB.ExecuteQuery <RptControlPlanModel>(sbsql.ToString()).ToList()).DefaultView;
                return(dtv);
            }
            catch (Exception ex)
            {
                ex.LogException();
                return(dtv);
            }
        }
Exemple #2
0
        public bool SavePccs(PCCSModel pccsModel, ref string typ)
        {
            try
            {
                DataTable dtPccsDetails        = new DataTable();
                DataTable dtPccsRevisonDetails = new DataTable();
                bool      _status = false;

                if (pccsModel.PartNo.IsNotNullOrEmpty() && pccsModel.SeqNo.IsNotNullOrEmpty() && pccsModel.RouteNo.IsNotNullOrEmpty())
                {
                    if (pccsModel.PCCSDetails.IsNotNullOrEmpty() && pccsModel.PCCSDetails.Count > 0)
                    {
                        //Save PCCS Details
                        DataTable   dt = new DataTable();
                        List <PCCS> lstexistingDatas = new List <PCCS>();
                        dtPccsDetails    = pccsModel.PCCSDetails.Table;
                        lstexistingDatas = ((from c in DB.PCCS
                                             where c.PART_NO == pccsModel.PartNo && c.ROUTE_NO == pccsModel.RouteNo && c.SEQ_NO == Convert.ToDecimal(pccsModel.SeqNo)
                                             select c).ToList());
                        if (lstexistingDatas.Count > 0)
                        {
                            DB.PCCS.DeleteAllOnSubmit(lstexistingDatas);
                            DB.SubmitChanges();
                        }

                        for (int i = 0; i < dtPccsDetails.Rows.Count; i++)
                        {
                            PCCS pccs = null;
                            try
                            {
                                if (dtPccsDetails.Rows[i]["REACTION_PLAN"].ToString() != "" || dtPccsDetails.Rows[i]["CONTROL_METHOD"].ToString() != "" || dtPccsDetails.Rows[i]["FREQ_OF_INSP"].ToString() != "" || dtPccsDetails.Rows[i]["GAUGES_USED"].ToString() != "" || dtPccsDetails.Rows[i]["DEPT_RESP"].ToString() != "" || dtPccsDetails.Rows[i]["SPEC_CHAR"].ToString() != "" || dtPccsDetails.Rows[i]["CTRL_SPEC_MAX"].ToString() != "" || dtPccsDetails.Rows[i]["CTRL_SPEC_MIN"].ToString() != "" || dtPccsDetails.Rows[i]["SPEC_MAX"].ToString() != "" || dtPccsDetails.Rows[i]["SPEC_MIN"].ToString() != "" || dtPccsDetails.Rows[i]["PROCESS_FEATURE"].ToString() != "" || dtPccsDetails.Rows[i]["ISR_NO"].ToValueAsString().ToDecimalValue() != 0 || dtPccsDetails.Rows[i]["FEATURE"].ToString() != "")
                                {
                                    pccs          = new PCCS();
                                    pccs.PART_NO  = pccsModel.PartNo.ToString();
                                    pccs.ROUTE_NO = pccsModel.RouteNo.ToValueAsString().ToDecimalValue();
                                    pccs.SEQ_NO   = pccsModel.SeqNo.IsNumeric() ? Convert.ToDecimal(pccsModel.SeqNo) : 0;
                                    //pccs.SNO = dtPccsDetails.Rows[i]["SNO"].ToString().IsNumeric() ? Convert.ToDouble(dtPccsDetails.Rows[i]["SNO"].ToString()) : 0;
                                    pccs.SNO             = dtPccsDetails.Rows[i]["SNO"].ToValueAsString().ToDoubleValue();
                                    pccs.ISR_NO          = dtPccsDetails.Rows[i]["ISR_NO"].ToValueAsString().ToDecimalValue();
                                    pccs.FEATURE         = dtPccsDetails.Rows[i]["FEATURE"].ToString();
                                    pccs.PROCESS_FEATURE = dtPccsDetails.Rows[i]["PROCESS_FEATURE"].ToString();
                                    pccs.SPEC_MIN        = dtPccsDetails.Rows[i]["SPEC_MIN"].ToString();
                                    pccs.SPEC_MAX        = dtPccsDetails.Rows[i]["SPEC_MAX"].ToString();
                                    pccs.CTRL_SPEC_MIN   = dtPccsDetails.Rows[i]["CTRL_SPEC_MIN"].ToString();
                                    pccs.CTRL_SPEC_MAX   = dtPccsDetails.Rows[i]["CTRL_SPEC_MAX"].ToString();
                                    pccs.SPEC_CHAR       = dtPccsDetails.Rows[i]["SPEC_CHAR"].ToString();
                                    pccs.DEPT_RESP       = dtPccsDetails.Rows[i]["DEPT_RESP"].ToString();
                                    pccs.GAUGES_USED     = dtPccsDetails.Rows[i]["GAUGES_USED"].ToString();
                                    //pccs.SAMPLE_SIZE = dtPccsDetails.Rows[i]["SAMPLE_SIZE"].ToString().IsNumeric() ? Convert.ToDecimal(dtPccsDetails.Rows[i]["SAMPLE_SIZE"].ToString()) : 0;
                                    pccs.SAMPLE_SIZE    = dtPccsDetails.Rows[i]["SAMPLE_SIZE"].ToValueAsString();
                                    pccs.FREQ_OF_INSP   = dtPccsDetails.Rows[i]["FREQ_OF_INSP"].ToString();
                                    pccs.CONTROL_METHOD = dtPccsDetails.Rows[i]["CONTROL_METHOD"].ToString();
                                    pccs.REACTION_PLAN  = dtPccsDetails.Rows[i]["REACTION_PLAN"].ToString();
                                    pccs.DELETE_FLAG    = false;
                                    pccs.ENTERED_DATE   = DateTime.Now;
                                    pccs.ENTERED_BY     = userInformation.UserName;
                                    pccs.ROWID          = Guid.NewGuid();
                                    DB.PCCS.InsertOnSubmit(pccs);
                                    DB.SubmitChanges();
                                    typ = "INS";
                                }
                            }
                            catch (Exception ex)
                            {
                                ex.LogException();
                                DB.PCCS.DeleteOnSubmit(pccs);
                            }
                        }
                        _status = true;

                        //Save PCCS Revison Details
                        // GetPccsRevisonDetails(pccsModel, pccsModel.PartNo, pccsModel.RouteNo);
                        dtPccsRevisonDetails = pccsModel.PccsRevisionDetails.Table;
                        //PART_NO,ROUTE_NO,ISSUE_NO,ISSUE_DATE,ISSUE_ALTER,COMPILED_BY
                        decimal routeNo = pccsModel.RouteNo.ToValueAsString().ToDecimalValue();

                        List <PCCS_ISSUE> lstexistingDatasPccsIssue = new List <PCCS_ISSUE>();
                        dtPccsDetails             = pccsModel.PCCSDetails.Table;
                        lstexistingDatasPccsIssue = ((from c in DB.PCCS_ISSUE
                                                      where c.PART_NO == pccsModel.PartNo &&
                                                      c.ROUTE_NO == pccsModel.RouteNo
                                                      select c).ToList());
                        if (lstexistingDatasPccsIssue.Count > 0)
                        {
                            DB.PCCS_ISSUE.DeleteAllOnSubmit(lstexistingDatasPccsIssue);
                            DB.SubmitChanges();
                        }

                        for (int i = 0; i < dtPccsRevisonDetails.Rows.Count; i++)
                        {
                            if (dtPccsRevisonDetails.Rows[i]["ROUTE_NO"].IsNotNullOrEmpty())
                            {
                                routeNo = dtPccsRevisonDetails.Rows[i]["ROUTE_NO"].ToString().ToDecimalValue();
                            }
                            else
                            {
                                routeNo = pccsModel.RouteNo.ToValueAsString().ToDecimalValue();
                            }

                            //PCCS_ISSUE pccsRevison = (from c in DB.PCCS_ISSUE
                            //                          where c.PART_NO == pccsModel.PartNo && c.ROUTE_NO == routeNo && c.ISSUE_NO == (i + 1).ToString()
                            //                          select c).FirstOrDefault<PCCS_ISSUE>();
                            //if (!pccsRevison.IsNotNullOrEmpty())
                            //{
                            PCCS_ISSUE pccsRevison = null;
                            if (routeNo == pccsModel.RouteNo)
                            {
                                try
                                {
                                    if (dtPccsRevisonDetails.Rows[i]["ISSUE_DATE"].ToString().IsNotNullOrEmpty() || dtPccsRevisonDetails.Rows[i]["ISSUE_ALTER"].ToString().IsNotNullOrEmpty() || dtPccsRevisonDetails.Rows[i]["COMPILED_BY"].ToString().IsNotNullOrEmpty())
                                    {
                                        pccsRevison = new PCCS_ISSUE()
                                        {
                                            PART_NO  = pccsModel.PartNo.ToString(),
                                            ROUTE_NO = pccsModel.RouteNo.ToValueAsString().ToDecimalValue(),
                                            ISSUE_NO = dtPccsRevisonDetails.Rows[i]["ISSUE_NO"].ToString(),
                                            // ISSUE_DATE = dtPccsRevisonDetails.Rows[i]["ISSUE_DATE"].ToString().ToDateTimeValue(),
                                            ISSUE_ALTER  = dtPccsRevisonDetails.Rows[i]["ISSUE_ALTER"].ToString(),
                                            COMPILED_BY  = dtPccsRevisonDetails.Rows[i]["COMPILED_BY"].ToString(),
                                            DELETE_FLAG  = false,
                                            ENTERED_DATE = DateTime.Now,
                                            ENTERED_BY   = userInformation.UserName,
                                            ROWID        = Guid.NewGuid()
                                        };
                                        if (dtPccsRevisonDetails.Rows[i]["ISSUE_DATE"].ToString() != "")
                                        {
                                            pccsRevison.ISSUE_DATE = Convert.ToDateTime(dtPccsRevisonDetails.Rows[i]["ISSUE_DATE"]);
                                        }
                                        else
                                        {
                                            pccsRevison.ISSUE_DATE = null;
                                        }

                                        DB.PCCS_ISSUE.InsertOnSubmit(pccsRevison);
                                        DB.SubmitChanges();
                                        typ = "INS";
                                    }
                                }
                                catch (Exception ex)
                                {
                                    ex.LogException();
                                    DB.PCCS_ISSUE.DeleteOnSubmit(pccsRevison);
                                }
                            }
                        }
                        _status = true;
                    }
                }

                return(_status);
            }
            catch (Exception ex)
            {
                ex.LogException();
                return(false);
            }
        }
        public bool GetCPRptIssueNoCurrentPartNo(ControlPlanRptModel cpRptModel)
        {
            try
            {
                //select issue_no from pccs_issue where  part_no='M40050' and route_no='1'
                //and issue_date = (select max(issue_date) from pccs_issue where part_no ='M40050' and route_no='1' )
                //
                DataTable dt = new DataTable();
                DateTime? pccsIssueMaxDate = (from o in DB.PCCS_ISSUE
                                              where o.PART_NO == cpRptModel.PartNo && o.ROUTE_NO == cpRptModel.RouteNo.ToDecimalValue()
                                              select o.ISSUE_DATE).Max();
                if (pccsIssueMaxDate.IsNotNullOrEmpty())
                {
                    PCCS_ISSUE pccsIssue = (from o in DB.PCCS_ISSUE
                                            where o.PART_NO == cpRptModel.PartNo && o.ROUTE_NO == cpRptModel.RouteNo.ToDecimalValue() && o.ISSUE_DATE == pccsIssueMaxDate.Value.Date
                                            select o).FirstOrDefault <PCCS_ISSUE>();
                    if (pccsIssue.IsNotNullOrEmpty())
                    {
                        if (cpRptModel.ControlPlanType == "PRE LAUNCH")
                        {
                            cpRptModel.ControlPlanNo = "CP-PL/" + cpRptModel.PartNo + "/ " + String.Format("{0,2:00}", pccsIssue.ISSUE_NO.ToIntValue());
                        }
                        else if (cpRptModel.ControlPlanType == "PRODUCTION")
                        {
                            cpRptModel.ControlPlanNo = "CP-PN/" + cpRptModel.PartNo + "/ " + String.Format("{0,2:00}", pccsIssue.ISSUE_NO.ToIntValue());
                        }
                        else if (cpRptModel.ControlPlanType == "PROTOTYPE")
                        {
                        }
                    }
                    return(true);
                }
                else
                {
                    try
                    {
                        PCCS_ISSUE pccsIssueMaxNo = (from o in DB.PCCS_ISSUE
                                                     where o.PART_NO == cpRptModel.PartNo && o.ROUTE_NO == cpRptModel.RouteNo.ToDecimalValue()
                                                     select o).LastOrDefault <PCCS_ISSUE>();
                        if (pccsIssueMaxNo.IsNotNullOrEmpty())
                        {
                            if (cpRptModel.ControlPlanType == "PRE LAUNCH")
                            {
                                cpRptModel.ControlPlanNo = "CP-PL/" + cpRptModel.PartNo + "/ " + String.Format("{0,2:00}", pccsIssueMaxNo.ISSUE_NO.ToIntValue());
                            }
                            else if (cpRptModel.ControlPlanType == "PRODUCTION")
                            {
                                cpRptModel.ControlPlanNo = "CP-PN/" + cpRptModel.PartNo + "/ " + String.Format("{0,2:00}", pccsIssueMaxNo.ISSUE_NO.ToIntValue());
                            }
                            else if (cpRptModel.ControlPlanType == "PROTOTYPE")
                            {
                            }
                        }
                        else
                        {
                            if (cpRptModel.ControlPlanType == "PRE LAUNCH")
                            {
                                cpRptModel.ControlPlanNo = "CP-PL/" + cpRptModel.PartNo + "/ " + String.Format("{0,2:00}", pccsIssueMaxNo.ISSUE_NO.ToIntValue());
                            }
                            else if (cpRptModel.ControlPlanType == "PRODUCTION")
                            {
                                cpRptModel.ControlPlanNo = "CP-PN/" + cpRptModel.PartNo + "/ " + String.Format("{0,2:00}", pccsIssueMaxNo.ISSUE_NO.ToIntValue());
                            }
                            else if (cpRptModel.ControlPlanType == "PROTOTYPE")
                            {
                            }
                        }
                    }
                    catch (Exception)
                    {
                        if (cpRptModel.ControlPlanType == "PRE LAUNCH")
                        {
                            cpRptModel.ControlPlanNo = "CP-PL/" + cpRptModel.PartNo + "/ " + String.Format("{0,2:00}", 0);
                        }
                        else if (cpRptModel.ControlPlanType == "PRODUCTION")
                        {
                            cpRptModel.ControlPlanNo = "CP-PN/" + cpRptModel.PartNo + "/ " + String.Format("{0,2:00}", 0);
                        }
                        else if (cpRptModel.ControlPlanType == "PROTOTYPE")
                        {
                        }
                        return(false);
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                ex.LogException();
                return(false);
            }
        }