/// <summary>
        /// DeleteDirect
        /// </summary>
        /// <param name="workId">workId</param>
        /// <param name="companyId">companyId</param>
        public void DeleteDirect(int workId, int companyId)
        {
            WorkJunctionLiningSectionGateway workJunctionLiningSectionGateway = new WorkJunctionLiningSectionGateway();
            workJunctionLiningSectionGateway.LoadByWorkId(workId, companyId);

            if (workJunctionLiningSectionGateway.Table.Rows.Count > 0)
            {
                WorkJunctionLiningLateralGateway workJunctionLiningLateralGateway = new WorkJunctionLiningLateralGateway();
                workJunctionLiningLateralGateway.LoadBySectionWorkId(workId, companyId);

                // ... Delete Laterals
                WorkJunctionLiningLateral workJunctionLiningLateral = new WorkJunctionLiningLateral();

                foreach (WorkTDS.LFS_WORK_JUNCTIONLINING_LATERALRow row in (WorkTDS.LFS_WORK_JUNCTIONLINING_LATERALDataTable)workJunctionLiningLateralGateway.Table)
                {
                    workJunctionLiningLateral.DeleteDirect(row.WorkID, row.SectionWorkID, companyId);
                }

                // ... Delete WorkJunctionLiningSection
                workJunctionLiningSectionGateway.Delete(workId, companyId);

                // ... Delete work
                Work work = new Work(null);
                work.DeleteDirect(workId, companyId);
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // PUBLIC METHODS
        //
        /// <summary>
        /// UpdateSection        
        /// <param name="companyId">companyId</param>
        public void UpdateSection(int companyId)
        {
            foreach (Fix1WorkJuntionLiningSectionTDS.LFS_WORK_JUNCTIONLINING_SECTIONRow row in (Fix1WorkJuntionLiningSectionTDS.LFS_WORK_JUNCTIONLINING_SECTIONDataTable)Table)
            {
                WorkJunctionLiningSectionGateway workJunctionLiningSectionGateway = new WorkJunctionLiningSectionGateway();
                workJunctionLiningSectionGateway.LoadByWorkId(row.WorkID, companyId);

                // get old values of section
                int numLats = workJunctionLiningSectionGateway.GetNumLats(row.WorkID);
                int notLinedYet = workJunctionLiningSectionGateway.GetNotLinedYet(row.WorkID);
                bool allMeasured = workJunctionLiningSectionGateway.GetAllMeasured(row.WorkID);
                bool deleted = workJunctionLiningSectionGateway.GetDeleted(row.WorkID);
                string issueWithLaterals = workJunctionLiningSectionGateway.GetIssueWithLaterals(row.WorkID);
                int notMeasuredYet = workJunctionLiningSectionGateway.GetNotMeasuredYet(row.WorkID);
                int notDeliveredYet = workJunctionLiningSectionGateway.GetNotDeliveredYet(row.WorkID);
                string trafficControl = workJunctionLiningSectionGateway.GetTrafficControl(row.WorkID);
                string trafficControlDetails = workJunctionLiningSectionGateway.GetTrafficControlDetails(row.WorkID);
                bool standardBypass = workJunctionLiningSectionGateway.GetStandardBypass(row.WorkID);
                string standardBypassComments = workJunctionLiningSectionGateway.GetStandardBypassComments(row.WorkID);
                int availableToLine = workJunctionLiningSectionGateway.GetAvailableToLine(row.WorkID);

                //  get new values of section
                int newAvailableToLine = 0;

                // load laterals
                WorkJunctionLiningLateralGateway workJunctionLiningLateralGateway = new WorkJunctionLiningLateralGateway();
                workJunctionLiningLateralGateway.LoadBySectionWorkId(row.WorkID, companyId);

                int delivered = 0;
                int installed = 0;
                foreach (WorkTDS.LFS_WORK_JUNCTIONLINING_LATERALRow row1 in (WorkTDS.LFS_WORK_JUNCTIONLINING_LATERALDataTable)workJunctionLiningLateralGateway.Table)
                {
                    if ((!row1.Deleted) && (!row1.OutOfScope))
                    {
                        if (!row1.IsDeliveredNull())  delivered++;
                        if (!row1.IsLinerInstalledNull()) installed++;
                    }
                }

                if (numLats > 0)
                {
                    newAvailableToLine = delivered - installed;
                }

                // Update Work Juntion Lining Section
                WorkJunctionLiningSection workJunctionLiningSection = new WorkJunctionLiningSection(null);
                workJunctionLiningSection.UpdateDirect(row.WorkID, numLats, notLinedYet, allMeasured, deleted, issueWithLaterals, notMeasuredYet, notDeliveredYet, companyId, trafficControl, trafficControlDetails, standardBypass, standardBypassComments, availableToLine, numLats, notLinedYet, allMeasured, issueWithLaterals, notMeasuredYet, notDeliveredYet, trafficControl, trafficControlDetails, standardBypass, standardBypassComments, newAvailableToLine);
            }
        }
        /// <summary>
        /// UpdateSection
        /// <param name="sectionWorkId">sectionWorkId</param>
        /// <param name="lateralWorkId">lateralWorkId</param>
        /// <param name="companyId">companyId</param>
        public void UpdateSection(int sectionWorkId, int lateralWorkId, int companyId)
        {
            // load section
            WorkJunctionLiningSectionGateway workJunctionLiningSectionGateway = new WorkJunctionLiningSectionGateway();
            workJunctionLiningSectionGateway.LoadByWorkId(sectionWorkId, companyId);

            // get old values of section
            int numLats = workJunctionLiningSectionGateway.GetNumLats(sectionWorkId);
            int notLinedYet = workJunctionLiningSectionGateway.GetNotLinedYet(sectionWorkId);
            bool allMeasured = workJunctionLiningSectionGateway.GetAllMeasured(sectionWorkId);
            bool deleted = workJunctionLiningSectionGateway.GetDeleted(sectionWorkId);
            string issueWithLaterals = workJunctionLiningSectionGateway.GetIssueWithLaterals(sectionWorkId);
            int notMeasuredYet = workJunctionLiningSectionGateway.GetNotMeasuredYet(sectionWorkId);
            int notDeliveredYet = workJunctionLiningSectionGateway.GetNotDeliveredYet(sectionWorkId);
            string trafficControl = workJunctionLiningSectionGateway.GetTrafficControl(sectionWorkId);
            string trafficControlDetails = workJunctionLiningSectionGateway.GetTrafficControlDetails(sectionWorkId);
            bool standardBypass = workJunctionLiningSectionGateway.GetStandardBypass(sectionWorkId);
            string standardBypassComments = workJunctionLiningSectionGateway.GetStandardBypassComments(sectionWorkId);
            int availableToLine = workJunctionLiningSectionGateway.GetAvailableToLine(sectionWorkId);

            //  get new values of section
            int newNumLats = 0;
            int newNotLinedYet = 0;
            bool newAllMeasured = true;
            int newNotMeasuredYet = 0;
            int newNotDeliveredYet = 0;
            string newIssueWithLaterals = "No";
            int newAvailableToLine = 0;
            int totDelivered = 0;
            int totInstalled = 0;

            // load laterals
            WorkJunctionLiningLateralGateway workJunctionLiningLateralGateway = new WorkJunctionLiningLateralGateway();
            workJunctionLiningLateralGateway.LoadBySectionWorkId(sectionWorkId, companyId);

            foreach (WorkTDS.LFS_WORK_JUNCTIONLINING_LATERALRow row in (WorkTDS.LFS_WORK_JUNCTIONLINING_LATERALDataTable)workJunctionLiningLateralGateway.Table)
            {
                if (!row.Deleted)
                {
                    // ... With no outofscope issue
                    if (!row.OutOfScope)
                    {
                        newNumLats++;
                        if (row.IsLinerInstalledNull()) newNotLinedYet++;
                        if (row.IsMeasuredNull()) newAllMeasured = false;
                        if (row.IsMeasuredNull()) newNotMeasuredYet++;
                        if (row.IsDeliveredNull()) newNotDeliveredYet++;
                        if (!row.IsDeliveredNull()) totDelivered++;
                        if (!row.IsLinerInstalledNull()) totInstalled++;
                    }
                }
            }

            if (newNumLats == 0) newAllMeasured = false;

            if (newNumLats > 0)
            {
                newAvailableToLine = totDelivered - totInstalled;

                // lining plan
                JlLiningPlanGateway jlLiningPlanGateway = new JlLiningPlanGateway();

                if (jlLiningPlanGateway.IsLateralsIssueNo(sectionWorkId))
                {
                    newIssueWithLaterals = "No";
                }
                else
                {
                    if (jlLiningPlanGateway.IsLateralsIssueOutOfScope(sectionWorkId))
                    {
                        newIssueWithLaterals = "Out Of Scope";
                    }
                    else
                    {
                        if (jlLiningPlanGateway.IsLateralsIssueYesOutOfScope(sectionWorkId))
                        {
                            newIssueWithLaterals = "Yes, Out Of Scope";
                        }
                        else
                        {
                            if (jlLiningPlanGateway.IsLateralsIssueYes(sectionWorkId))
                            {
                                newIssueWithLaterals = "Yes";
                            }
                        }
                    }
                }
            }

            // Update Work Juntion Lining Section
            WorkJunctionLiningSection workJunctionLiningSection = new WorkJunctionLiningSection(null);
            workJunctionLiningSection.UpdateDirect(sectionWorkId, numLats, notLinedYet, allMeasured, deleted, issueWithLaterals, notMeasuredYet, notDeliveredYet, companyId, trafficControl, trafficControlDetails, standardBypass, standardBypassComments, availableToLine, newNumLats, newNotLinedYet, newAllMeasured, newIssueWithLaterals, newNotMeasuredYet, newNotDeliveredYet, trafficControl, trafficControlDetails, standardBypass, standardBypassComments, newAvailableToLine);
        }
        /// <summary>
        /// Save a Previous JL work
        /// </summary>
        /// <param name="projectId">projectId</param>
        /// <param name="section_assetId">section_assetId</param>
        /// <param name="companyId">companyId</param>
        private void SavePreviousJLWork(int projectId, int section_assetId, int companyId)
        {
            // Load Previous work  -  Junction Lining section data (last sections work)
            string workType = "Junction Lining Section";
            WorkJunctionLiningSectionGateway workJunctionLiningSectionGateway = new WorkJunctionLiningSectionGateway();
            workJunctionLiningSectionGateway.LoadTop1ByProjectIdAssetIdWorkType(projectId, section_assetId, workType, companyId);

            int workId = workJunctionLiningSectionGateway.GetWorkIdTop1();
            int numLats = workJunctionLiningSectionGateway.GetNumLats(workId);
            int notLinedYet = workJunctionLiningSectionGateway.GetNotLinedYet(workId);
            Boolean allMeasured = workJunctionLiningSectionGateway.GetAllMeasured(workId);
            string issueWithLaterals = workJunctionLiningSectionGateway.GetIssueWithLaterals(workId);
            int notMeasuredYet = workJunctionLiningSectionGateway.GetNotMeasuredYet(workId);
            int notDeliveredYet = workJunctionLiningSectionGateway.GetNotDeliveredYet(workId);
            string trafficControl = ""; if (workJunctionLiningSectionGateway.GetTrafficControl(workId) != "") trafficControl = workJunctionLiningSectionGateway.GetTrafficControl(workId);
            string trafficControlDetails = ""; if (workJunctionLiningSectionGateway.GetTrafficControlDetails(workId) != "") trafficControlDetails = workJunctionLiningSectionGateway.GetTrafficControlDetails(workId);
            Boolean standardBypass = workJunctionLiningSectionGateway.GetStandardBypass(workId);
            string standardBypassComments = ""; if (workJunctionLiningSectionGateway.GetStandardBypassComments(workId) != "") standardBypassComments = workJunctionLiningSectionGateway.GetStandardBypassComments(workId);
            int availableToLine = workJunctionLiningSectionGateway.GetAvailableToLine(workId);

            // Load Previous work  -  General work data
            WorkGateway workGateway = new WorkGateway();
            workGateway.LoadByWorkId(workId, companyId);

            int? libraryCategoriesId = null; if (workGateway.GetLibraryCategoriesId(workId).HasValue) libraryCategoriesId = workGateway.GetLibraryCategoriesId(workId);
            string comments = workGateway.GetComments(workId);
            string history = workGateway.GetHistory(workId);

            // Save new work
            WorkJunctionLiningSection workJunctionLiningSection = new WorkJunctionLiningSection(null);
            int newSectionWorkId = workJunctionLiningSection.InsertDirect(projectId, section_assetId, libraryCategoriesId, numLats, notLinedYet, allMeasured, issueWithLaterals, notMeasuredYet, notDeliveredYet, false, companyId, comments, history, trafficControl, trafficControlDetails, standardBypass, standardBypassComments, availableToLine);

            // Load Previous work  - Junction Lining Lateral data
            WorkJunctionLiningLateralGateway workJunctionLiningLateralGateway = new WorkJunctionLiningLateralGateway();
            workJunctionLiningLateralGateway.LoadBySectionWorkId(workId, companyId);

            foreach (WorkTDS.LFS_WORK_JUNCTIONLINING_LATERALRow lateralRow in (WorkTDS.LFS_WORK_JUNCTIONLINING_LATERALDataTable)workJunctionLiningLateralGateway.Table)
            {
                WorkGateway workGatewayForLateral = new WorkGateway();
                workGatewayForLateral.LoadByWorkId(lateralRow.WorkID, companyId);
                int lateral_assetId = workGatewayForLateral.GetAssetId(lateralRow.WorkID);

                DateTime? pipeLocated = null; if (!lateralRow.IsPipeLocatedNull()) pipeLocated = lateralRow.PipeLocated;
                DateTime? sevicesLocated = null; if (!lateralRow.IsServicesLocatedNull()) sevicesLocated = lateralRow.ServicesLocated;
                DateTime? coInstalled = null; if (!lateralRow.IsCoInstalledNull()) coInstalled = lateralRow.CoInstalled;
                DateTime? backfilledConcrete = null; if (!lateralRow.IsBackfilledConcreteNull()) backfilledConcrete = lateralRow.BackfilledConcrete;
                DateTime? backfilledSoil = null; if (!lateralRow.IsBackfilledSoilNull()) backfilledSoil = lateralRow.BackfilledSoil;
                DateTime? grouted = null; if (!lateralRow.IsGroutedNull()) grouted = lateralRow.Grouted;
                DateTime? cored = null; if (!lateralRow.IsCoredNull()) cored = lateralRow.Cored;
                DateTime? prepped = null; if (!lateralRow.IsPreppedNull()) prepped = lateralRow.Prepped;
                DateTime? measured = null; if (!lateralRow.IsMeasuredNull()) measured = lateralRow.Measured;
                string linerSize = ""; if (!lateralRow.IsLinerSizeNull()) linerSize = lateralRow.LinerSize;
                DateTime? inProcess = null; if (!lateralRow.IsInProcessNull()) inProcess = lateralRow.InProcess;
                DateTime? inStock = null; if (!lateralRow.IsInStockNull()) inStock = lateralRow.InStock;
                DateTime? delivered = null; if (!lateralRow.IsDeliveredNull()) delivered = lateralRow.Delivered;
                int? buildRebuild = null; if (!lateralRow.IsBuildRebuildNull()) buildRebuild = lateralRow.BuildRebuild;
                DateTime? preVideo = null; if (!lateralRow.IsPreVideoNull()) preVideo = lateralRow.PreVideo;
                DateTime? linerInstalled = null; if (!lateralRow.IsLinerInstalledNull()) linerInstalled = lateralRow.LinerInstalled;
                DateTime? finalVideo = null; if (!lateralRow.IsFinalVideoNull()) finalVideo = lateralRow.FinalVideo;
                decimal? cost = null; if (!lateralRow.IsCostNull()) cost = lateralRow.Cost;
                DateTime? videoInspection = null; if (!lateralRow.IsVideoInspectionNull()) videoInspection = lateralRow.VideoInspection;
                bool coRequired = lateralRow.CoRequired;
                bool pitRequired = lateralRow.PitRequired;
                string coPitLocation = ""; if (!lateralRow.IsCoPitLocationNull()) coPitLocation = lateralRow.CoPitLocation;
                bool postContractDigRequired = lateralRow.PostContractDigRequired;
                DateTime? coCutDown = null; if (!lateralRow.IsCoCutDownNull()) coCutDown = lateralRow.CoCutDown;
                DateTime? finalRestoration = null; if (!lateralRow.IsFinalRestorationNull()) finalRestoration = lateralRow.FinalRestoration;
                string videoLengthToPropertyLine = ""; if (!lateralRow.IsVideoLengthToPropertyLineNull()) videoLengthToPropertyLine = lateralRow.VideoLengthToPropertyLine;
                bool liningThruCo = lateralRow.LiningThruCo;
                DateTime? noticeDelivered = null; if (!lateralRow.IsNoticeDeliveredNull()) noticeDelivered = lateralRow.NoticeDelivered;
                string hamiltonInspectionNumber = ""; if (!lateralRow.IsHamiltonInspectionNumberNull()) hamiltonInspectionNumber = lateralRow.HamiltonInspectionNumber;
                string flange = ""; if (!lateralRow.IsFlangeNull()) flange = lateralRow.Flange;
                string gasket = ""; if (!lateralRow.IsGasketNull()) gasket = lateralRow.Gasket;
                string depthOfLocated = ""; if (!lateralRow.IsDepthOfLocatedNull()) depthOfLocated = lateralRow.DepthOfLocated;
                bool digRequiredPriorToLining = lateralRow.DigRequiredPriorToLining;
                DateTime? digRequiredPriorToLiningCompleted = null; if (!lateralRow.IsDigRequiredPriorToLiningCompletedNull()) digRequiredPriorToLiningCompleted = lateralRow.DigRequiredPriorToLiningCompleted;
                bool digRequiredAfterLining = lateralRow.DigRequiredAfterLining;
                DateTime? digRequiredAfterLiningCompleted = null; if (!lateralRow.IsDigRequiredAfterLiningCompletedNull()) digRequiredAfterLiningCompleted = lateralRow.DigRequiredAfterLiningCompleted;
                bool outOfScope = lateralRow.OutOfScope;
                bool holdClientIssue = lateralRow.HoldClientIssue;
                DateTime? holdClientIssueResolved = null; if (!lateralRow.IsHoldClientIssueResolvedNull()) holdClientIssueResolved = lateralRow.HoldClientIssueResolved;
                bool holdLFSIssue = lateralRow.HoldLFSIssue;
                DateTime? holdLFSIssueResolved = null; if (!lateralRow.IsHoldLFSIssueResolvedNull()) holdLFSIssueResolved = lateralRow.HoldLFSIssueResolved;
                bool requiresRoboticPrep = lateralRow.LateralRequiresRoboticPrep;
                DateTime? requiresRoboticPrepCompleted = null; if (!lateralRow.IsLateralRequiresRoboticPrepCompletedNull()) requiresRoboticPrepCompleted = lateralRow.LateralRequiresRoboticPrepCompleted;
                string linerType = ""; if (!lateralRow.IsLinerTypeNull()) linerType = lateralRow.LinerType;
                string prepType = ""; if (!lateralRow.IsPrepTypeNull()) prepType = lateralRow.PrepType;
                bool dyeTestReq = lateralRow.DyeTestReq;
                DateTime? dyeTestComplete = null; if (!lateralRow.IsDyeTestCompleteNull()) dyeTestComplete = lateralRow.DyeTestComplete;
                string contractYear = ""; if (!lateralRow.IsContractYearNull()) contractYear = lateralRow.ContractYear;

                WorkJunctionLiningLateral workJunctionLiningLateral = new WorkJunctionLiningLateral(null);
                workJunctionLiningLateral.InsertDirect(projectId, lateral_assetId, newSectionWorkId, pipeLocated, sevicesLocated, coInstalled, backfilledConcrete, backfilledSoil, grouted, cored, prepped, measured, linerSize, inProcess, inStock, delivered, buildRebuild, preVideo, linerInstalled, finalVideo, cost, videoInspection, coRequired, pitRequired, coPitLocation, postContractDigRequired, coCutDown, finalRestoration, false, companyId, comments, history, videoLengthToPropertyLine, liningThruCo, noticeDelivered, hamiltonInspectionNumber, flange, gasket, depthOfLocated, digRequiredPriorToLining, digRequiredPriorToLiningCompleted, digRequiredAfterLining, digRequiredAfterLiningCompleted, outOfScope, holdClientIssue, holdClientIssueResolved, holdLFSIssue, holdLFSIssueResolved, requiresRoboticPrep, requiresRoboticPrepCompleted, linerType, prepType, dyeTestReq, dyeTestComplete, contractYear);
            }

            // Load Previous work  - Comments and History
            SavePreviousComments(workId, workType, companyId, newSectionWorkId);
            SavePreviousHistory(workId, workType, companyId, newSectionWorkId);
        }
        /// <summary>
        /// Save a Previous RA work
        /// </summary>
        /// <param name="projectId">projectId</param>
        /// <param name="section_assetId">section_assetId</param>
        /// <param name="companyId">companyId</param>
        private void SavePreviousWork(int parentProjectId, int childProjectId, int assetId, string workType, int companyId)
        {
            int parentWorkId = 0;
            int childWorkId = 0;
            int childJLLWorkId = 0;
            int parentJLLWorkID = 0;

            // Load Previous work  - Rehab assessment data (last sections work)
            WorkGateway parentWorkGateway = new WorkGateway();
            WorkGateway childWorkGateway = new WorkGateway();
            WorkGateway childJLLWorkGateway = new WorkGateway();

            parentWorkGateway.LoadByProjectIdAssetIdWorkType(parentProjectId, assetId, workType, companyId);

            if (parentWorkGateway.Table.Rows.Count > 0)
            {
                parentWorkId = parentWorkGateway.GetWorkId(assetId, workType, parentProjectId);

                childWorkGateway.LoadByProjectIdAssetIdWorkType(childProjectId, assetId, workType, companyId);
                childWorkId = childWorkGateway.GetWorkId(assetId, workType, childProjectId);

                // Load Previous work  - Comments and History
                SavePreviousComments(parentWorkId, workType, companyId, childWorkId);
                SavePreviousHistory(parentWorkId, workType, companyId, childWorkId);

                if (workType == "Junction Lining Section")
                {
                    // Load Previous work  - Junction Lining Lateral data
                    WorkJunctionLiningLateralGateway workJunctionLiningLateralGateway = new WorkJunctionLiningLateralGateway();
                    workJunctionLiningLateralGateway.LoadBySectionWorkId(parentWorkId, companyId);

                    foreach (WorkTDS.LFS_WORK_JUNCTIONLINING_LATERALRow lateralRow in (WorkTDS.LFS_WORK_JUNCTIONLINING_LATERALDataTable)workJunctionLiningLateralGateway.Table)
                    {
                        try
                        {
                            WorkGateway workGatewayForLateral = new WorkGateway();
                            workGatewayForLateral.LoadByWorkId(lateralRow.WorkID, companyId);

                            int lateral_assetId = workGatewayForLateral.GetAssetId(lateralRow.WorkID);
                            parentJLLWorkID = lateralRow.WorkID;

                            childJLLWorkGateway.LoadByProjectIdAssetIdWorkType(childProjectId, lateral_assetId, "Junction Lining Lateral", companyId);
                            childJLLWorkId = childJLLWorkGateway.GetWorkId(lateral_assetId, "Junction Lining Lateral", childProjectId);

                            // Load Previous work  - Comments and History
                            SavePreviousComments(parentJLLWorkID, "Junction Lining Lateral", companyId, childJLLWorkId);
                            SavePreviousHistory(parentJLLWorkID, "Junction Lining Lateral", companyId, childJLLWorkId);

                            workUpdate(childJLLWorkId, childProjectId, lateral_assetId, companyId, "Junction Lining Lateral");
                        }
                        catch
                        {
                        }
                    }
                }
                else
                {
                    workUpdate(childWorkId, childProjectId, assetId, companyId, workType);
                }
            }
        }