// ////////////////////////////////////////////////////////////////////////
        // PUBLIC METHODS
        //
        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="workId">workId</param>
        /// <param name="companyId">companyId</param>
        public void Delete(int workId, int companyId)
        {
            WorkFullLengthLiningM1LateralGateway workFullLengthLiningM1LateralGateway = new WorkFullLengthLiningM1LateralGateway();
            workFullLengthLiningM1LateralGateway.LoadByWorkId(workId, companyId);

            foreach (WorkTDS.LFS_WORK_FULLLENGTHLINING_M1_LATERALRow row in (WorkTDS.LFS_WORK_FULLLENGTHLINING_M1_LATERALDataTable)workFullLengthLiningM1LateralGateway.Table)
            {
                // delete lfs lateral client
                WorkGateway workGateway = new WorkGateway();
                workGateway.LoadByWorkId(workId, companyId);
                int currentProjectId = workGateway.GetProjectId(workId);

                ProjectGateway projectGateway = new ProjectGateway();
                projectGateway.LoadByProjectId(currentProjectId);
                int clientId = projectGateway.GetClientID(currentProjectId);

                LfsAssetSewerLateralClientGateway lfsAssetSewerLateralClientGateway = new LfsAssetSewerLateralClientGateway();
                lfsAssetSewerLateralClientGateway.LoadByAssetIdClientId(row.Lateral, clientId, companyId);

                if (lfsAssetSewerLateralClientGateway.Table.Rows.Count > 0)
                {
                    LfsAssetSewerLateralClient lfsAssetSewerLateralClient = new LfsAssetSewerLateralClient(null);
                    lfsAssetSewerLateralClient.DeleteDirect(row.Lateral, clientId, companyId);
                }

                // Delete work lateral
                workFullLengthLiningM1LateralGateway.Delete(workId, row.Lateral, companyId);

                // Delete section
                LfsAssetSewerLateral lfsAssetSewerLateral = new LfsAssetSewerLateral(null);
                lfsAssetSewerLateral.DeleteDirect(row.Lateral, companyId);
            }
        }
        /// <summary>
        /// Save a Previous FL work
        /// </summary>
        /// <param name="projectId">projectId</param>
        /// <param name="section_assetId">section_assetId</param>
        /// <param name="companyId">companyId</param>
        private void SavePreviousFLWork(int projectId, int section_assetId, int companyId)
        {
            // Load Previous work  -  Full Length Lining data (last sections work)
            string workType = "Full Length Lining";
            WorkFullLengthLiningGateway workFullLengthLiningGateway = new WorkFullLengthLiningGateway();
            workFullLengthLiningGateway.LoadTop1ByProjectIdAssetIdWorkType(projectId, section_assetId, workType, companyId);

            int workId = workFullLengthLiningGateway.GetWorkIdTop1();
            string clientId = ""; if (workFullLengthLiningGateway.GetClientId(workId) != "") clientId = workFullLengthLiningGateway.GetClientId(workId);
            DateTime? proposedLiningDate = null; if (workFullLengthLiningGateway.GetProposedLiningDate(workId).HasValue) proposedLiningDate = workFullLengthLiningGateway.GetProposedLiningDate(workId);
            DateTime? deadlineLiningDate = null; if (workFullLengthLiningGateway.GetDeadlineLiningDate(workId).HasValue) deadlineLiningDate = workFullLengthLiningGateway.GetDeadlineLiningDate(workId);
            DateTime? p1Date = null; if (workFullLengthLiningGateway.GetP1Date(workId).HasValue) p1Date = workFullLengthLiningGateway.GetP1Date(workId);
            DateTime? m1Date = null; if (workFullLengthLiningGateway.GetM1Date(workId).HasValue) m1Date = workFullLengthLiningGateway.GetM1Date(workId);
            DateTime? m2Date = null; if (workFullLengthLiningGateway.GetM2Date(workId).HasValue) m2Date = workFullLengthLiningGateway.GetM2Date(workId);
            DateTime? installDate = null; if (workFullLengthLiningGateway.GetInstallDate(workId).HasValue) installDate = workFullLengthLiningGateway.GetInstallDate(workId);
            DateTime? finalVideoDate = null; if (workFullLengthLiningGateway.GetFinalVideoDate(workId).HasValue) finalVideoDate = workFullLengthLiningGateway.GetFinalVideoDate(workId);
            bool issueIdentified = workFullLengthLiningGateway.GetIssueIdentified(workId);
            bool issueLfs = workFullLengthLiningGateway.GetIssueLFS(workId);
            bool issueClient = workFullLengthLiningGateway.GetIssueClient(workId);
            bool issueSales = workFullLengthLiningGateway.GetIssueSales(workId);
            bool issueGivenToClient = workFullLengthLiningGateway.GetIssueGivenToClient(workId);
            bool issueResolved = workFullLengthLiningGateway.GetIssueResolved(workId);
            bool issueInvestigation = workFullLengthLiningGateway.GetIssueInvestigation(workId);

            // Load Previous work  -  Full Length Lining M1 data (last sections work)
            FullLengthLiningWorkDetailsGateway fullLengthLiningWorkDetailsGateway = new FullLengthLiningWorkDetailsGateway();
            fullLengthLiningWorkDetailsGateway.LoadByWorkIdAssetId(workId, section_assetId, companyId);

            string measurementTakenBy = ""; if (fullLengthLiningWorkDetailsGateway.GetMeasurementTakenBy(workId) != "") measurementTakenBy = fullLengthLiningWorkDetailsGateway.GetMeasurementTakenBy(workId);
            string trafficControl = ""; if (fullLengthLiningWorkDetailsGateway.GetTrafficControl(workId) != "") trafficControl = fullLengthLiningWorkDetailsGateway.GetTrafficControl(workId);
            string siteDetails = ""; if (fullLengthLiningWorkDetailsGateway.GetSiteDetails(workId) != "") siteDetails = fullLengthLiningWorkDetailsGateway.GetSiteDetails(workId);
            string accessType = ""; if (fullLengthLiningWorkDetailsGateway.GetAccessType(workId) != "") accessType = fullLengthLiningWorkDetailsGateway.GetAccessType(workId);
            bool pipeSizeChange = fullLengthLiningWorkDetailsGateway.GetPipeSizeChange(workId);
            bool standardByPass = fullLengthLiningWorkDetailsGateway.GetStandardBypass(workId);
            string standardByPassComments = ""; if (fullLengthLiningWorkDetailsGateway.GetStandardBypassComments(workId) != "") standardByPassComments = fullLengthLiningWorkDetailsGateway.GetStandardBypassComments(workId);
            string trafficControlDetails = ""; if (fullLengthLiningWorkDetailsGateway.GetTrafficControlDetails(workId) != "") trafficControlDetails = fullLengthLiningWorkDetailsGateway.GetTrafficControlDetails(workId);
            string measurementType = ""; if (fullLengthLiningWorkDetailsGateway.GetMeasurementType(workId) != "") measurementType = fullLengthLiningWorkDetailsGateway.GetMeasurementType(workId);
            string measurementFromMH = ""; if (fullLengthLiningWorkDetailsGateway.GetMeasurementFromMh(workId) != "") measurementFromMH = fullLengthLiningWorkDetailsGateway.GetMeasurementFromMh(workId);
            string videoDoneFromMH = ""; if (fullLengthLiningWorkDetailsGateway.GetVideoDoneFromMh(workId) != "") videoDoneFromMH = fullLengthLiningWorkDetailsGateway.GetVideoDoneFromMh(workId);
            string videoDoneToMH = ""; if (fullLengthLiningWorkDetailsGateway.GetVideoDoneToMh(workId) != "") videoDoneToMH = fullLengthLiningWorkDetailsGateway.GetVideoDoneToMh(workId);

            // Load Previous work  -  Full Length Lining M2 data (last sections work)
            string videoLength = ""; if (fullLengthLiningWorkDetailsGateway.GetVideoLength(workId) != "") videoLength = fullLengthLiningWorkDetailsGateway.GetVideoLength(workId);
            string measurementTakenBy2 = ""; if (fullLengthLiningWorkDetailsGateway.GetMeasurementTakenByM2(workId) != "") measurementTakenBy2 = fullLengthLiningWorkDetailsGateway.GetMeasurementTakenByM2(workId);
            bool dropPipe = fullLengthLiningWorkDetailsGateway.GetDropPipe(workId);
            string dropPipeInvertDepth = ""; if (fullLengthLiningWorkDetailsGateway.GetDropPipeInvertDepth(workId) != "") dropPipeInvertDepth = fullLengthLiningWorkDetailsGateway.GetDropPipeInvertDepth(workId);
            int? cappedLaterals = null; if (fullLengthLiningWorkDetailsGateway.GetCappedLaterals(workId).HasValue) cappedLaterals = fullLengthLiningWorkDetailsGateway.GetCappedLaterals(workId);
            string lineWithID = ""; if (fullLengthLiningWorkDetailsGateway.GetLineWithId(workId) != "") lineWithID = fullLengthLiningWorkDetailsGateway.GetLineWithId(workId);
            string hydrantAddress = ""; if (fullLengthLiningWorkDetailsGateway.GetHydrantAddress(workId) != "") hydrantAddress = fullLengthLiningWorkDetailsGateway.GetHydrantAddress(workId);
            string hydroWireWithin10FtOfInversionMH = ""; if (fullLengthLiningWorkDetailsGateway.GetHydroWiredWithin10FtOfInversionMH(workId) != "") hydroWireWithin10FtOfInversionMH = fullLengthLiningWorkDetailsGateway.GetHydroWiredWithin10FtOfInversionMH(workId);
            string distanceToInversionMH = ""; if (fullLengthLiningWorkDetailsGateway.GetDistanceToInversionMh(workId) != "") distanceToInversionMH = fullLengthLiningWorkDetailsGateway.GetDistanceToInversionMh(workId);
            string surfaceGrade = ""; if (fullLengthLiningWorkDetailsGateway.GetSurfaceGrade(workId) != "") surfaceGrade = fullLengthLiningWorkDetailsGateway.GetSurfaceGrade(workId);
            bool hydroPulley = fullLengthLiningWorkDetailsGateway.GetHydroPulley(workId);
            bool fridgeCart = fullLengthLiningWorkDetailsGateway.GetFridgeCart(workId);
            bool twoPump = fullLengthLiningWorkDetailsGateway.GetTwoPump(workId);
            bool sixBypass = fullLengthLiningWorkDetailsGateway.GetSixBypass(workId);
            bool scaffolding = fullLengthLiningWorkDetailsGateway.GetScaffolding(workId);
            bool winchExtention = fullLengthLiningWorkDetailsGateway.GetWinchExtension(workId);
            bool extraGenerator = fullLengthLiningWorkDetailsGateway.GetExtraGenerator(workId);
            bool greyCableExtension = fullLengthLiningWorkDetailsGateway.GetGreyCableExtension(workId);
            bool easementMats = fullLengthLiningWorkDetailsGateway.GetEasementMats(workId);
            bool rampRequired = fullLengthLiningWorkDetailsGateway.GetRampRequired(workId);
            bool cameraSkid = fullLengthLiningWorkDetailsGateway.GetCameraSkid(workId);

            // Load Previous work  -  Full Length Lining P1 data (last sections work)
            int? cxisRemoved = null; if (fullLengthLiningWorkDetailsGateway.GetCxisRemoved(workId).HasValue) fullLengthLiningWorkDetailsGateway.GetCxisRemoved(workId);
            bool roboticPrepCompleted = fullLengthLiningWorkDetailsGateway.GetRoboticPrepCompleted(workId);
            DateTime? roboticPrepCompletedDate = fullLengthLiningWorkDetailsGateway.GetRoboticPrepCompletedDate(workId);
            bool p1Completed = fullLengthLiningWorkDetailsGateway.GetP1Completed(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
            WorkFullLengthLining workFullLengthLining = new WorkFullLengthLining(null);
            int newSectionWorkId = workFullLengthLining.InsertDirectFullWork(projectId, section_assetId, libraryCategoriesId, clientId, proposedLiningDate, deadlineLiningDate, p1Date, m1Date, m2Date, installDate, finalVideoDate, issueIdentified, issueLfs, issueClient, issueSales, issueGivenToClient, issueResolved, false, companyId, issueInvestigation, comments, history, cxisRemoved, roboticPrepCompleted, roboticPrepCompletedDate, measurementTakenBy, trafficControl, siteDetails, pipeSizeChange, standardByPass, standardByPassComments, trafficControlDetails, measurementType, measurementFromMH, videoDoneFromMH, videoDoneToMH, videoLength, measurementTakenBy2, dropPipe, dropPipeInvertDepth, cappedLaterals, lineWithID, hydrantAddress, hydroWireWithin10FtOfInversionMH, distanceToInversionMH, surfaceGrade, hydroPulley, fridgeCart, twoPump, sixBypass, scaffolding, winchExtention, extraGenerator, greyCableExtension, easementMats, rampRequired, cameraSkid, accessType, p1Completed);

            // Load Previous work  -  Save fl laterals
            WorkFullLengthLiningM1LateralGateway workFullLengthLiningM1LateralGateway = new WorkFullLengthLiningM1LateralGateway();
            workFullLengthLiningM1LateralGateway.LoadByWorkId(workId, companyId);

            foreach (WorkTDS.LFS_WORK_FULLLENGTHLINING_M1_LATERALRow lateralRow in (WorkTDS.LFS_WORK_FULLLENGTHLINING_M1_LATERALDataTable)workFullLengthLiningM1LateralGateway.Table)
            {
                int lateral = lateralRow.Lateral;
                string videoDistance = ""; if (!lateralRow.IsVideoDistanceNull()) videoDistance = lateralRow.VideoDistance;
                string clockPosition = ""; if (!lateralRow.IsClockPositionNull()) clockPosition = lateralRow.ClockPosition;
                string distanceToCentre = ""; if (!lateralRow.IsDistanceToCentreNull()) distanceToCentre = lateralRow.DistanceToCentre;
                string timeOpened = ""; if (!lateralRow.IsTimeOpenedNull()) timeOpened = lateralRow.TimeOpened;
                string reverseSetup = ""; if (!lateralRow.IsReverseSetupNull()) reverseSetup = lateralRow.ReverseSetup;
                DateTime? reinstate = null; if (!lateralRow.IsReinstateNull()) reinstate = lateralRow.Reinstate;
                string lateralComments = ""; if (!lateralRow.IsCommentsNull()) lateralComments = lateralRow.Comments;
                string clientInspectionNo = ""; if (!lateralRow.IsClientInspectionNoNull()) clientInspectionNo = lateralRow.ClientInspectionNo;
                DateTime? v1Inspection = null; if (!lateralRow.IsV1InspectionNull()) v1Inspection = lateralRow.V1Inspection;
                bool requiresRoboticPrep = lateralRow.RequiresRoboticPrep;
                DateTime? requiresRoboticPrepDate = null; if (!lateralRow.IsRequiresRoboticPrepDateNull()) requiresRoboticPrepDate = lateralRow.RequiresRoboticPrepDate;
                bool holdClientIssue = lateralRow.HoldClientIssue;
                bool holdLFSIssue = lateralRow.HoldLFSIssue;
                bool lineLateral = lateralRow.LineLateral;
                bool dyeTestReq = lateralRow.DyeTestReq;
                DateTime? dyeTestComplete = null; if (!lateralRow.IsDyeTestCompleteNull()) dyeTestComplete = lateralRow.DyeTestComplete;
                string contractYear = lateralRow.ContractYear;

                WorkFullLengthLiningM1Lateral workFullLengthLiningM1Lateral = new WorkFullLengthLiningM1Lateral();
                workFullLengthLiningM1Lateral.InsertDirect(newSectionWorkId, lateral, videoDistance, clockPosition, distanceToCentre, timeOpened, reverseSetup, reinstate, lateralComments, false, companyId, clientInspectionNo, v1Inspection, requiresRoboticPrep, requiresRoboticPrepDate, holdClientIssue, holdLFSIssue, lineLateral, dyeTestReq, dyeTestComplete, contractYear);
            }

            // Load Previous work  - Comments and History
            SavePreviousComments(workId, workType, companyId, newSectionWorkId);
            SavePreviousHistory(workId, workType, companyId, newSectionWorkId);
        }
 /// <summary>
 /// DeleteDirect
 /// </summary>
 /// <param name="workId">workId</param>
 /// <param name="lateral">lateral</param>
 /// <param name="companyId">companyId</param>
 public void DeleteDirect(int workId, int lateral, int companyId)
 {
     WorkFullLengthLiningM1LateralGateway workFullLengthLiningM1LateralGateway = new WorkFullLengthLiningM1LateralGateway(null);
     workFullLengthLiningM1LateralGateway.Delete(workId, lateral, companyId);
 }
 /// <summary>
 /// InsertDirect
 /// </summary>
 /// <param name="workId">workId</param>
 /// <param name="lateral">lateral</param>
 /// <param name="videoDistance">videoDistance</param>
 /// <param name="clockPosition">clockPosition</param>
 /// <param name="distanceToCentre">distanceToCentre</param>
 /// <param name="timeOpened">timeOpened</param>
 /// <param name="reverseSetup">reverseSetup</param>
 /// <param name="reinstate">reinstate</param>
 /// <param name="comments">comments</param>
 /// <param name="deleted">deleted</param>
 /// <param name="companyId">companyId</param>
 /// <param name="clientInspectionNo">clientInspectionNo</param>
 /// <param name="v1Inspection">v1Inspection</param>
 /// <param name="requiresRoboticPrep">requiresRoboticPrep</param>
 /// <param name="requiresRoboticPrepDate">requiresRoboticPrepDate</param> 
 /// <param name="holdClientIssue">holdClientIssue</param>
 /// <param name="holdLFSIssue">holdLFSIssue</param>
 /// <param name="lineLateral">lineLateral</param>
 /// <param name="dyeTestReq">dyeTestReq</param>
 /// <param name="dyeTestComplete">dyeTestComplete</param>
 /// <param name="contractYear">contractYear</param>
 public void InsertDirect(int workId, int lateral, string videoDistance, string clockPosition, string distanceToCentre, string timeOpened, string reverseSetup, DateTime? reinstate, string comments, bool deleted, int companyId, string clientInspectionNo, DateTime? v1Inspection, bool requiresRoboticPrep, DateTime? requiresRoboticPrepDate, bool holdClientIssue, bool holdLFSIssue, bool lineLateral, bool dyeTestReq, DateTime? dyeTestComplete, string contractYear)
 {
     WorkFullLengthLiningM1LateralGateway workFullLengthLiningM1LateralGateway = new WorkFullLengthLiningM1LateralGateway(Data);
     workFullLengthLiningM1LateralGateway.Insert(workId, lateral, videoDistance, clockPosition, distanceToCentre, timeOpened, reverseSetup, reinstate, comments, deleted, companyId, clientInspectionNo, v1Inspection, requiresRoboticPrep, requiresRoboticPrepDate, holdClientIssue, holdLFSIssue, lineLateral, dyeTestReq, dyeTestComplete, contractYear);
 }
 /// <summary>
 /// UpdateDirect
 /// </summary>
 /// <param name="originalWorkId">originalWorkId</param>
 /// <param name="originalLateral">originalLateral</param>
 /// <param name="originalVideoDistance">originalVideoDistance</param>
 /// <param name="originalClockPosition">originalClockPosition</param>
 /// <param name="originalDistanceToCentre">originalDistanceToCentre</param>
 /// <param name="originalTimeOpened">originalTimeOpened</param>
 /// <param name="originalReverseSetup">originalReverseSetup</param>
 /// <param name="originalReinstate">originalReinstate</param>
 /// <param name="originalComments">originalComments</param>
 /// <param name="originalDeleted">originalDeleted</param>
 /// <param name="originalCompanyId">originalCompanyId</param>
 /// <param name="originalClientInspectionNo">originalClientInspectionNo</param>
 /// <param name="originalV1Inspection">originalV1Inspection</param>        
 /// <param name="originalRequiresRoboticPrep">originalRequiresRoboticPrep</param>
 /// <param name="originalRequiresRoboticPrepDate">originalRequiresRoboticPrepDate</param>
 /// <param name="originalHoldClientIssue">originalHoldClientIssue</param>
 /// <param name="originalHoldLFSIssue">originalHoldLFSIssue</param>
 /// <param name="originalLineLateral">originalLineLateral</param>
 /// <param name="originalDyeTestReq">originalDyeTestReq</param>
 /// <param name="originalDyeTestComplete">originalDyeTestComplete</param>
 /// <param name="originalContractYear">originalContractYear</param>
 /// 
 /// <param name="newWorkId">newWorkId</param>
 /// <param name="newLateral">newLateral</param>
 /// <param name="newVideoDistance">newVideoDistance</param>
 /// <param name="newClockPosition">newClockPosition</param>
 /// <param name="newDistanceToCentre">newDistanceToCentre</param>
 /// <param name="newTimeOpened">newTimeOpened</param>
 /// <param name="newReverseSetup">newReverseSetup</param>
 /// <param name="newReinstate">newReinstate</param>
 /// <param name="newComments">newComments</param>
 /// <param name="newDeleted">newDeleted</param>
 /// <param name="newCompanyId">newCompanyId</param>
 /// <param name="newClientInspectionNo">newClientInspectionNo</param>
 /// <param name="newV1Inspection">newV1Inspection</param>
 /// <param name="newRequiresRoboticPrep">newRequiresRoboticPrep</param>
 /// <param name="newRequiresRoboticPrepDate">newRequiresRoboticPrepDate</param>
 /// <param name="newHoldClientIssue">newHoldClientIssue</param>
 /// <param name="newHoldLFSIssue">newHoldLFSIssue</param>
 /// <param name="newLineLateral">newLineLateral</param>
 /// <param name="newDyeTestReq">newDyeTestReq</param>
 /// <param name="newDyeTestComplete">newDyeTestComplete</param>
 /// <param name="newContractYear">newContractYear</param>
 public void UpdateDirect(int originalWorkId, int originalLateral, string originalVideoDistance, string originalClockPosition, string originalDistanceToCentre, string originalTimeOpened, string originalReverseSetup, DateTime? originalReinstate, string originalComments, bool originalDeleted, int originalCompanyId, string originalClientInspectionNo, DateTime? originalV1Inspection, bool originalRequiresRoboticPrep, DateTime? originalRequiresRoboticPrepDate, bool originalHoldClientIssue, bool originalHoldLFSIssue, bool originalLineLateral, bool originalDyeTestReq, DateTime? originalDyeTestComplete, string originalContractYear, int newWorkId, int newLateral, string newVideoDistance, string newClockPosition, string newDistanceToCentre, string newTimeOpened, string newReverseSetup, DateTime? newReinstate, string newComments, bool newDeleted, int newCompanyId, string newClientInspectionNo, DateTime? newV1Inspection, bool newRequiresRoboticPrep, DateTime? newRequiresRoboticPrepDate, bool newHoldClientIssue, bool newHoldLFSIssue, bool newLineLateral, bool newDyeTestReq, DateTime? newDyeTestComplete, string newContractYear)
 {
     WorkFullLengthLiningM1LateralGateway workFullLengthLiningM1LateralGateway = new WorkFullLengthLiningM1LateralGateway(null);
     workFullLengthLiningM1LateralGateway.Update(originalWorkId, originalLateral, originalVideoDistance, originalClockPosition, originalDistanceToCentre, originalTimeOpened, originalReverseSetup, originalReinstate, originalComments, originalDeleted, originalCompanyId, originalClientInspectionNo, originalV1Inspection, originalRequiresRoboticPrep, originalRequiresRoboticPrepDate, originalHoldClientIssue, originalHoldLFSIssue, originalLineLateral, originalDyeTestReq, originalDyeTestComplete, originalContractYear, newWorkId, newLateral, newVideoDistance, newClockPosition, newDistanceToCentre, newTimeOpened, newReverseSetup, newReinstate, newComments, newDeleted, newCompanyId, newClientInspectionNo, newV1Inspection, newRequiresRoboticPrep, newRequiresRoboticPrepDate, newHoldClientIssue, newHoldLFSIssue, newLineLateral, newDyeTestReq, newDyeTestComplete, newContractYear);
 }
        /// <summary>
        /// Save all sections & works to database (direct)
        /// </summary>
        /// <param name="workId">workId</param>
        /// <param name="projectId">projectId</param>
        /// <param name="sectionAssetId">sectionAssetId</param>
        /// <param name="countryId">countryId</param>
        /// <param name="provinceId">provinceId</param>
        /// <param name="countyId">countyId</param>
        /// <param name="cityId">cityId</param>
        /// <param name="videoLength">videoLength</param>
        /// <param name="companyId">companyId</param>
        /// <param name="isNewMeasuredFromDsmh">isNewMeasuredFromDsmh</param>
        public void Save(int workId, int projectId, int sectionAssetId, Int64 countryId, Int64? provinceId, Int64? countyId, Int64? cityId, string videoLength, int companyId, bool isNewMeasuredFromDsmh, bool prepDataRoboticPrep, DateTime? prepDataRoboticPrepCompleted)
        {
            FullLengthLiningTDS fullLengthLiningChanges = (FullLengthLiningTDS)Data.GetChanges();

            if (fullLengthLiningChanges.LateralDetails.Rows.Count > 0)
            {
                FullLengthLiningLateralDetailsGateway fullLengthLiningLateralDetailsGateway = new FullLengthLiningLateralDetailsGateway(fullLengthLiningChanges);

                foreach (FullLengthLiningTDS.LateralDetailsRow row in (FullLengthLiningTDS.LateralDetailsDataTable)fullLengthLiningChanges.LateralDetails)
                {
                    // Process modified rows
                    if (row.ToProcess)
                    {
                        // Insert new laterals
                        if ((!row.Deleted) && (row.InProject) && (!row.InProjectDatabase))
                        {
                            // Insert asset
                            int lateral_assetId = SaveLateral(row, projectId, sectionAssetId, countryId, provinceId, countyId, cityId, companyId, isNewMeasuredFromDsmh);
                            int lateral = row.Lateral;

                            // Insert work
                            string videoDistance = fullLengthLiningLateralDetailsGateway.GetVideoDistance(lateral);
                            string clockPosition = fullLengthLiningLateralDetailsGateway.GetClockPosition(lateral);
                            string distanceToCentre = fullLengthLiningLateralDetailsGateway.GetDistanceToCentre(lateral);
                            string timeOpened = fullLengthLiningLateralDetailsGateway.GetTimeOpened(lateral);
                            string reverseSetup = fullLengthLiningLateralDetailsGateway.GetReverseSetup(lateral);
                            DateTime? reinstate = fullLengthLiningLateralDetailsGateway.GetReinstate(lateral);
                            string comments = fullLengthLiningLateralDetailsGateway.GetComments(lateral);
                            string clientInspectionNo = fullLengthLiningLateralDetailsGateway.GetClientInspectionNo(lateral);
                            DateTime? v1Inspection = null;
                            bool requiresRoboticPrep = fullLengthLiningLateralDetailsGateway.GetRequiresRoboticPrep(lateral);
                            DateTime? requiresRoboticPrepDate = null;
                            bool holdClientIssue = fullLengthLiningLateralDetailsGateway.GetHoldClientIssue(lateral);
                            bool holdLFSIssue = fullLengthLiningLateralDetailsGateway.GetHoldLFSIssue(lateral);
                            bool lineLateral = fullLengthLiningLateralDetailsGateway.GetLineLateral(lateral);
                            string flange = fullLengthLiningLateralDetailsGateway.GetFlange(lateral);
                            bool dyeTestReq = fullLengthLiningLateralDetailsGateway.GetDyeTestReq(lateral);
                            DateTime? dyeTestComplete = null; if (fullLengthLiningLateralDetailsGateway.GetDyeTestComplete(lateral).HasValue) dyeTestComplete = fullLengthLiningLateralDetailsGateway.GetDyeTestComplete(lateral);
                            string contractYear = fullLengthLiningLateralDetailsGateway.GetContractYear(lateral);

                            WorkFullLengthLiningM1LateralGateway workFullLengthLiningM1LateralGateay = new WorkFullLengthLiningM1LateralGateway();
                            workFullLengthLiningM1LateralGateay.LoadAllByWorkIdLateral(workId, lateral_assetId, companyId);

                            if (workFullLengthLiningM1LateralGateay.Table.Rows.Count == 0)
                            {
                                InsertFLLLateral(workId, lateral_assetId, videoDistance, clockPosition, distanceToCentre, timeOpened, reverseSetup, reinstate, comments, row.Deleted, companyId, clientInspectionNo, v1Inspection, requiresRoboticPrep, requiresRoboticPrepDate, holdClientIssue, holdLFSIssue, lineLateral, dyeTestReq, dyeTestComplete, contractYear);
                            }

                            // ... ... If lateral will be in Junction Lining
                            if ((row.LineLateral) && (!row.InJlDatabase))
                            {
                                if (((!prepDataRoboticPrep) && (!prepDataRoboticPrepCompleted.HasValue)) || ((prepDataRoboticPrep) && (prepDataRoboticPrepCompleted.HasValue)))
                                {
                                    // ... ... Load work id
                                    int sectionWorkId = 0;
                                    WorkJunctionLiningSection workJunctionLiningSection = new WorkJunctionLiningSection(null);
                                    sectionWorkId = workJunctionLiningSection.InsertDirect(projectId, sectionAssetId, null, 0, 0, false, "No", 0, 0, false, companyId, "", "", "", "", false, "", 0);

                                    WorkJunctionLiningLateralGateway workJunctionLiningLateralGateway = new WorkJunctionLiningLateralGateway();
                                    WorkJunctionLiningLateral workJunctionLiningLateral = new WorkJunctionLiningLateral(workJunctionLiningLateralGateway.Data);
                                    workJunctionLiningLateral.InsertDirect(projectId, lateral_assetId, sectionWorkId, null, null, null, null, null, null, null, null, null, "", null, null, null, 0, null, null, null, 0, null, true, false, "", false, null, null, false, companyId, "", "", "", false, null, "", flange, "", "", false, null, false, null, false, holdClientIssue, null, holdLFSIssue, null, requiresRoboticPrep, requiresRoboticPrepDate, "", "", dyeTestReq, dyeTestComplete, contractYear);
                                }
                            }

                            //... Insert material for m1 lateral
                            string material = fullLengthLiningLateralDetailsGateway.GetMaterialType(lateral);
                            if (material != "")
                            {
                                InsertMaterial(lateral_assetId, material, companyId);
                            }

                            //... Insert client lateral id
                            string clientLateralId = fullLengthLiningLateralDetailsGateway.GetClientLateralId(lateral);

                            if (clientLateralId.Trim() != "")
                            {
                                ProjectGateway projectGateway = new ProjectGateway();
                                projectGateway.LoadByProjectId(projectId);
                                int clientId = projectGateway.GetClientID(projectId);

                                LfsAssetSewerLateralClient lfsAssetSewerLateralClient = new LfsAssetSewerLateralClient(null);
                                lfsAssetSewerLateralClient.InsertDirect(lateral_assetId, clientId, clientLateralId, false, companyId);
                            }

                            // Change row process state
                            NotProcess(lateral);
                        }

                        // Update laterals
                        if ((!row.Deleted) && (row.InProject) && (row.InProjectDatabase))
                        {
                            int lateral = row.Lateral;

                            // original values
                            string originalVideoDistance = fullLengthLiningLateralDetailsGateway.GetVideoDistanceOriginal(lateral);
                            string originalClockPosition = fullLengthLiningLateralDetailsGateway.GetClockPositionOriginal(lateral);
                            string originalDistanceToCentre = fullLengthLiningLateralDetailsGateway.GetDistanceToCentreOriginal(lateral);
                            string originalTimeOpened = fullLengthLiningLateralDetailsGateway.GetTimeOpenedOriginal(lateral);
                            string originalReverseSetup = fullLengthLiningLateralDetailsGateway.GetReverseSetupOriginal(lateral);
                            DateTime? originalReinstate = fullLengthLiningLateralDetailsGateway.GetReinstateOriginal(lateral);
                            string originalComments = fullLengthLiningLateralDetailsGateway.GetCommentsOriginal(lateral);
                            string originalClientFullLateralId = fullLengthLiningLateralDetailsGateway.GetClientLateralIdOriginal(lateral);
                            string originalClientLateralId = ""; if (originalClientFullLateralId != "") originalClientLateralId = originalClientFullLateralId;
                            string originalClientInspectionNo = fullLengthLiningLateralDetailsGateway.GetClientInspectionNoOriginal(lateral);
                            DateTime? originalV1Inspection = fullLengthLiningLateralDetailsGateway.GetV1InspectionOriginal(lateral);
                            bool originalRequiresRoboticPrep = fullLengthLiningLateralDetailsGateway.GetRequiresRoboticPrepOriginal(lateral);
                            DateTime? originalRequiresRoboticPrepDate = fullLengthLiningLateralDetailsGateway.GetRequiresRoboticPrepDateOriginal(lateral);
                            bool originalHoldClientIssue = fullLengthLiningLateralDetailsGateway.GetHoldClientIssueOriginal(lateral);
                            bool originalHoldLFSIssue = fullLengthLiningLateralDetailsGateway.GetHoldLFSIssueOriginal(lateral);
                            bool originalLineLateral = fullLengthLiningLateralDetailsGateway.GetLineLateralOriginal(lateral);
                            bool originalDyeTestReq = fullLengthLiningLateralDetailsGateway.GetDyeTestReqOriginal(lateral);
                            DateTime? originalDyeTestComplete = null; if (fullLengthLiningLateralDetailsGateway.GetDyeTestCompleteOriginal(lateral).HasValue) originalDyeTestComplete = fullLengthLiningLateralDetailsGateway.GetDyeTestCompleteOriginal(lateral);
                            string originalContractYear = fullLengthLiningLateralDetailsGateway.GetContractYearOriginal(lateral);

                            // new values
                            string newVideoDistance = fullLengthLiningLateralDetailsGateway.GetVideoDistance(lateral);
                            string newClockPosition = fullLengthLiningLateralDetailsGateway.GetClockPosition(lateral);
                            string newDistanceToCentre = fullLengthLiningLateralDetailsGateway.GetDistanceToCentre(lateral);
                            string newTimeOpened = fullLengthLiningLateralDetailsGateway.GetTimeOpened(lateral);
                            string newReverseSetup = fullLengthLiningLateralDetailsGateway.GetReverseSetup(lateral);
                            DateTime? newReinstate = fullLengthLiningLateralDetailsGateway.GetReinstate(lateral);
                            string newComments = fullLengthLiningLateralDetailsGateway.GetComments(lateral);
                            string newClientFullLateralId = fullLengthLiningLateralDetailsGateway.GetClientLateralId(lateral);
                            string newClientLateralId = ""; if (newClientFullLateralId != "") newClientLateralId = newClientFullLateralId;
                            string newClientInspectionNo = fullLengthLiningLateralDetailsGateway.GetClientInspectionNo(lateral);
                            bool newRequiresRoboticPrep = fullLengthLiningLateralDetailsGateway.GetRequiresRoboticPrep(lateral);
                            DateTime? newRequiresRoboticPrepDate = fullLengthLiningLateralDetailsGateway.GetRequiresRoboticPrepDate(lateral);
                            bool newHoldClientIssue = fullLengthLiningLateralDetailsGateway.GetHoldClientIssue(lateral);
                            bool newHoldLFSIssue = fullLengthLiningLateralDetailsGateway.GetHoldLFSIssue(lateral);
                            bool newLineLateral = fullLengthLiningLateralDetailsGateway.GetLineLateral(lateral);
                            string newFlange = fullLengthLiningLateralDetailsGateway.GetFlange(lateral);
                            bool newDyeTestReq = fullLengthLiningLateralDetailsGateway.GetDyeTestReq(lateral);
                            DateTime? newDyeTestComplete = null; if (fullLengthLiningLateralDetailsGateway.GetDyeTestComplete(lateral).HasValue) newDyeTestComplete = fullLengthLiningLateralDetailsGateway.GetDyeTestComplete(lateral);
                            string newContractYear = fullLengthLiningLateralDetailsGateway.GetContractYear(lateral);

                            // ... Update fll laterals
                            int lateral_assetId = SaveLateral(row, projectId, sectionAssetId, countryId, provinceId, countyId, cityId, companyId, isNewMeasuredFromDsmh);
                            UpdateLateral(row, projectId, sectionAssetId, countryId, provinceId, countyId, cityId, videoLength, companyId);
                            UpdateFLLLateral(workId, lateral_assetId, originalVideoDistance, originalClockPosition, originalDistanceToCentre, originalTimeOpened, originalReverseSetup, originalReinstate, originalComments, false, companyId, originalClientInspectionNo, originalV1Inspection, originalRequiresRoboticPrep, originalRequiresRoboticPrepDate, originalHoldClientIssue, originalHoldLFSIssue, originalLineLateral, originalDyeTestReq, originalDyeTestComplete, originalContractYear, workId, lateral_assetId, newVideoDistance, newClockPosition, newDistanceToCentre, newTimeOpened, newReverseSetup, newReinstate, newComments, false, companyId, newClientInspectionNo, originalV1Inspection, newRequiresRoboticPrep, newRequiresRoboticPrepDate, newHoldClientIssue, newHoldLFSIssue, newLineLateral, newDyeTestReq, newDyeTestComplete, newContractYear);

                            // ... Update if lateral is in Junction Lining
                            if (row.InJlDatabase)
                            {
                                if (originalLineLateral == newLineLateral)
                                {
                                    // ... ... Update jl lateral (clientInspectionNo, v1Inspection, requiredRoboticPrep, requiredRoboticPrepDate, holdClientIssue, holdLFSIssue, flange)
                                    UpdateJLLaterals(projectId, lateral_assetId, companyId, newClientInspectionNo, newRequiresRoboticPrep, newRequiresRoboticPrepDate, newHoldClientIssue, newHoldLFSIssue, newFlange, newDyeTestReq, newDyeTestComplete, newContractYear);
                                }
                                else
                                {
                                    int sectionWorkId = 0;
                                    WorkJunctionLiningSection workJunctionLiningSection = new WorkJunctionLiningSection(null);
                                    sectionWorkId = workJunctionLiningSection.InsertDirect(projectId, sectionAssetId, null, 0, 0, false, "No", 0, 0, false, companyId, "", "", "", "", false, "", 0);

                                    // Delete empty lateral form jl.
                                    DeleteJLLaterals(projectId, lateral_assetId, companyId, sectionWorkId);
                                }
                            }
                            else
                            {
                                // ... ... Insert if should be in junction Lining
                                if (row.LineLateral)
                                {
                                    if (((!prepDataRoboticPrep) && (!prepDataRoboticPrepCompleted.HasValue)) || ((prepDataRoboticPrep) && (prepDataRoboticPrepCompleted.HasValue)))
                                    {
                                        int sectionWorkId = 0;

                                        // Insert to jl laterals
                                        WorkJunctionLiningSection workJunctionLiningSection = new WorkJunctionLiningSection(null);
                                        sectionWorkId = workJunctionLiningSection.InsertDirect(projectId, sectionAssetId, null, 0, 0, false, "No", 0, 0, false, companyId, "", "", "", "", false, "", 0);

                                        WorkJunctionLiningLateralGateway workJunctionLiningLateralGateway = new WorkJunctionLiningLateralGateway();
                                        WorkJunctionLiningLateral workJunctionLiningLateral = new WorkJunctionLiningLateral(workJunctionLiningLateralGateway.Data);
                                        workJunctionLiningLateral.InsertDirect(projectId, lateral_assetId, sectionWorkId, null, null, null, null, null, null, null, null, null, "", null, null, null, 0, null, null, null, 0, null, false, false, "", false, null, null, false, companyId, "", "", "", false, null, newClientInspectionNo, newFlange, "", "", false, null, false, null, false, newHoldClientIssue, null, newHoldLFSIssue, null, newRequiresRoboticPrep, newRequiresRoboticPrepDate, "", "", newDyeTestReq, newDyeTestComplete, newContractYear);
                                    }
                                }
                            }

                            //... Insert material for m1 lateral
                            string material = fullLengthLiningLateralDetailsGateway.GetMaterialType(lateral);
                            if (material != "")
                            {
                                InsertMaterial(lateral_assetId, material, companyId);
                            }

                            ProjectGateway projectGateway = new ProjectGateway();
                            projectGateway.LoadByProjectId(projectId);
                            int clientId = projectGateway.GetClientID(projectId);

                            LfsAssetSewerLateralClientGateway lfsAssetSewerLateralClientGateway = new LfsAssetSewerLateralClientGateway();
                            lfsAssetSewerLateralClientGateway.LoadAllByAssetIdClientId(lateral_assetId, clientId, companyId);

                            if (lfsAssetSewerLateralClientGateway.Table.Rows.Count == 0)
                            {
                                LfsAssetSewerLateralClient lfsAssetSewerLateralClient = new LfsAssetSewerLateralClient(null);
                                lfsAssetSewerLateralClient.InsertDirect(lateral_assetId, clientId, originalClientLateralId, false, companyId);
                            }
                            else
                            {
                                LfsAssetSewerLateralClient lfsAssetSewerLateralClient = new LfsAssetSewerLateralClient(null);
                                string originalFullClientLateralId = lfsAssetSewerLateralClientGateway.GetClientLateralId(lateral_assetId, clientId);

                                lfsAssetSewerLateralClient.UpdateDirect(lateral_assetId, clientId, originalClientLateralId, false, companyId, lateral_assetId, clientId, newClientLateralId, false, companyId);
                            }

                            // Change row process state
                            NotProcess(lateral);
                        }

                        // Deleted laterals or exclude them from fulllength lining work
                        if (((row.Deleted) && (row.InProject) && (row.InProjectDatabase)) || ((!row.Deleted) && (!row.InProject) && (row.InProjectDatabase)))
                        {
                            DeleteFLLLateral(workId, row.Lateral, companyId, projectId);

                            // Change row process state
                            NotProcess(row.Lateral);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// SaveFll
        /// </summary>
        /// <param name="workId">workId</param>
        /// <param name="assetIdLateral">assetIdLateral</param>
        /// <param name="companyId">companyId</param>
        /// <param name="opened">opened</param>
        /// <param name="brushed">brushed</param>
        public void SaveFll(int workId, int assetIdLateral, int companyId, DateTime? opened, DateTime? brushed)
        {
            // ... ... Modifications at M1
            WorkFullLengthLiningM1LateralGateway workFullLengthLiningM1LateralGateway = new WorkFullLengthLiningM1LateralGateway();
            workFullLengthLiningM1LateralGateway.LoadByWorkIdLateral(workId, assetIdLateral, companyId);

            if (workFullLengthLiningM1LateralGateway.Table.Rows.Count > 0)
            {
                // ... .... ... Load original data
                string originalVideoDistance = workFullLengthLiningM1LateralGateway.GetVideoDistance(workId, assetIdLateral);
                string originalClockPosition = workFullLengthLiningM1LateralGateway.GetClockPosition(workId, assetIdLateral);
                string originalDistanceToCentre = workFullLengthLiningM1LateralGateway.GetDistanceToCentre(workId, assetIdLateral);
                string originalTimeOpened = workFullLengthLiningM1LateralGateway.GetTimeOpened(workId, assetIdLateral);
                string originalReverseSetup = workFullLengthLiningM1LateralGateway.GetReverseSetup(workId, assetIdLateral);
                DateTime? originalReinstate = workFullLengthLiningM1LateralGateway.GetReinstate(workId, assetIdLateral);
                string originalComments = workFullLengthLiningM1LateralGateway.GetComments(workId, assetIdLateral);
                bool originalDeleted = workFullLengthLiningM1LateralGateway.GetDeleted(workId, assetIdLateral);
                int originalCompanyId = workFullLengthLiningM1LateralGateway.GetCompanyId(workId, assetIdLateral);
                string originalClientInspectionNo = workFullLengthLiningM1LateralGateway.GetClientInspectionNo(workId, assetIdLateral);
                DateTime? originalV1Inspection = workFullLengthLiningM1LateralGateway.GetV1Inspection(workId, assetIdLateral);
                bool originalRequiresRoboticPrep = workFullLengthLiningM1LateralGateway.GetRequiresRoboticPrep(workId, assetIdLateral);
                DateTime? originalRequiresRoboticPrepDate = workFullLengthLiningM1LateralGateway.GetRequiresRoboticPrepDate(workId, assetIdLateral);
                bool originalHoldClientIssue = workFullLengthLiningM1LateralGateway.GetHoldClientIssue(workId, assetIdLateral);
                bool originalHoldLFSIssue = workFullLengthLiningM1LateralGateway.GetHoldLFSIssue(workId, assetIdLateral);
                bool originalLinelateral = workFullLengthLiningM1LateralGateway.GetLineLateral(workId, assetIdLateral);
                bool originalDyeTestReq = workFullLengthLiningM1LateralGateway.GetDyeTestReq(workId, assetIdLateral);
                DateTime? originalDyeTestComplete = null; if (workFullLengthLiningM1LateralGateway.GetDyeTestComplete(workId, assetIdLateral).HasValue) originalDyeTestComplete = workFullLengthLiningM1LateralGateway.GetDyeTestComplete(workId, assetIdLateral);
                string originalContractYear = workFullLengthLiningM1LateralGateway.GetContractYear(workId, assetIdLateral);

                // New data
                string newTimeOpened = ""; if (opened.HasValue) newTimeOpened = opened.Value.ToString();
                DateTime? newReinstate = null; if (brushed.HasValue) newReinstate = brushed;

                // Update work
                WorkFullLengthLiningM1Lateral workFullLengthLiningM1Lateral = new WorkFullLengthLiningM1Lateral(null);
                workFullLengthLiningM1Lateral.UpdateDirect(workId, assetIdLateral, originalVideoDistance, originalClockPosition, originalDistanceToCentre, originalTimeOpened, originalReverseSetup, originalReinstate, originalComments, originalDeleted, originalCompanyId, originalClientInspectionNo, originalV1Inspection, originalRequiresRoboticPrep, originalRequiresRoboticPrepDate, originalHoldClientIssue, originalHoldLFSIssue, originalLinelateral, originalDyeTestReq, originalDyeTestComplete, originalContractYear, workId, assetIdLateral, originalVideoDistance, originalClockPosition, originalDistanceToCentre, newTimeOpened, originalReverseSetup, newReinstate, originalComments, originalDeleted, originalCompanyId, originalClientInspectionNo, originalV1Inspection, originalRequiresRoboticPrep, originalRequiresRoboticPrepDate, originalHoldClientIssue, originalHoldLFSIssue, originalLinelateral, originalDyeTestReq, originalDyeTestComplete, originalContractYear);
            }
        }
        /// <summary>
        /// UpdateFieldsForSections
        /// </summary>
        /// <param name="projectId">projectId</param>
        /// <param name="companyId">companyId</param>
        private void UpdateFieldsForSections(int projectId, int companyId)
        {
            foreach (FlatSectionJlTDS.FlatSectionJlRow row in (FlatSectionJlTDS.FlatSectionJlDataTable)Table)
            {
                int jlWorkId = row.WorkID;

                // ... Get raWorkId
                int raWorkId = 0;
                WorkGateway raWorkGateway = new WorkGateway();
                raWorkGateway.LoadByProjectIdAssetIdWorkType(projectId, row.Section_, "Rehab Assessment", companyId);
                if (raWorkGateway.Table.Rows.Count > 0)
                {
                    raWorkId = raWorkGateway.GetWorkId(row.Section_, "Rehab Assessment", projectId);
                }

                // ... Get flWorkId
                int flWorkId = 0;
                WorkGateway flWorkGateway = new WorkGateway();
                flWorkGateway.LoadByProjectIdAssetIdWorkType(projectId, row.Section_, "Full Length Lining", companyId);
                if (flWorkGateway.Table.Rows.Count > 0)
                {
                    flWorkId = flWorkGateway.GetWorkId(row.Section_, "Full Length Lining", projectId);
                }

                // ... Load All Comments
                FlatSectionJLAllComments flatSectionJLAllComments = new FlatSectionJLAllComments();
                flatSectionJLAllComments.LoadAllByJlWorkIdFlWorkIdRaWorkId(jlWorkId, flWorkId, raWorkId, companyId);

                row.Comments = flatSectionJLAllComments.GetJLOrFLOrRAComments(companyId, flatSectionJLAllComments.Table.Rows.Count, "\n");

                // Add M1 comments
                WorkFullLengthLiningM1LateralGateway workFullLengthLiningM1LateralGateway = new WorkFullLengthLiningM1LateralGateway();
                workFullLengthLiningM1LateralGateway.LoadByWorkIdLateral(flWorkId, row.AssetID, companyId);
                if (workFullLengthLiningM1LateralGateway.Table.Rows.Count > 0)
                {
                    string m1LateralComments = workFullLengthLiningM1LateralGateway.GetComments(flWorkId, row.AssetID);
                    if (m1LateralComments != "")
                    {
                        row.Comments = row.Comments + "\n\nType: M1 Lateral Comments\nComment: " + m1LateralComments ;
                    }
                }

                WorkFullLengthLiningM1Gateway workFullLengthLiningM1Gateway = new WorkFullLengthLiningM1Gateway();
                workFullLengthLiningM1Gateway.LoadByWorkId(flWorkId, companyId);
                if (workFullLengthLiningM1Gateway.Table.Rows.Count > 0)
                {
                    string trafficControlDetails = workFullLengthLiningM1Gateway.GetTrafficControlDetails(flWorkId);
                    if (trafficControlDetails != "")
                    {
                        row.Comments = row.Comments + "\n\nType: Traffic Control Details\nComment: " + trafficControlDetails;
                    }

                    string standardByPassComments = workFullLengthLiningM1Gateway.GetStandardBypassComments(flWorkId);
                    if (standardByPassComments != "")
                    {
                        row.Comments = row.Comments + "\n\nType: Standard Bypass Comments\nComment: " + standardByPassComments ;
                    }

                    string measurementFromMH = "USMH"; if (workFullLengthLiningM1Gateway.GetMeasurementFromMh(flWorkId) != "") measurementFromMH = workFullLengthLiningM1Gateway.GetMeasurementFromMh(flWorkId);

                    if (measurementFromMH == "DSMH")
                    {
                        string auxDistanceFromUSMH = row.DistanceFromUSMH;
                        row.DistanceFromUSMH = row.DistanceFromDSMH;
                        row.DistanceFromDSMH = auxDistanceFromUSMH;
                    }
                }

                if (!row.IsCommentsNull())
                {
                    row.Comments = row.Comments.Replace("<br>", "\n");
                }

                // Update History
                FlatSectionJLAllHistory flatSectionJLAllHistory = new FlatSectionJLAllHistory();
                flatSectionJLAllHistory.LoadAllByJlWorkIdFlWorkIdRaWorkId(jlWorkId, flWorkId, raWorkId, companyId);

                row.History = flatSectionJLAllHistory.GetJLOrFLOrRAHistory(companyId, flatSectionJLAllHistory.Table.Rows.Count, "\n");

                if (!row.IsHistoryNull())
                {
                    row.History = row.History.Replace("<br>", "\n");
                }

                // Update FlowOrderID
                if (!row.IsFlowOrderIDNull())
                {
                    row.LateralID = row.FlowOrderID + "-JL-" + row.LateralID;
                }
                else
                {
                    row.LateralID = "JL-" + row.LateralID;
                }

                // Update MainSize
                if (!row.IsMainSizeNull())
                {
                    try
                    {
                        if (Distance.IsValidDistance(row.MainSize))
                        {
                            Distance distance = new Distance(row.MainSize);

                            switch (distance.DistanceType)
                            {
                                case 2:
                                    row.MainSize = distance.ToStringInEng1();
                                    break;
                                case 3:
                                    if (Convert.ToDouble(row.MainSize) > 99)
                                    {
                                        double newMainSize = 0;
                                        newMainSize = Convert.ToDouble(row.MainSize) * 0.03937;
                                        row.MainSize = Convert.ToString(Math.Ceiling(newMainSize)) + "\"";
                                    }
                                    else
                                    {
                                        row.MainSize = row.MainSize + "\"";
                                    }
                                    break;
                                case 4:
                                    row.MainSize = distance.ToStringInEng1();
                                    break;
                                case 5:
                                    row.MainSize = distance.ToStringInEng1();
                                    break;
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }
        }
        /// <summary>
        /// UpdateCommentsHistoryForSummaryEdit
        /// </summary>
        /// <param name="jlWorkId">jlWorkId</param>
        /// <param name="workType">workType</param>
        /// <param name="companyId">companyId</param>
        /// <returns>comments</returns>
        public void UpdateCommentsHistoryForSummaryEdit(int jlWorkId, string workType, int companyId)
        {
            foreach (FlatSectionJlTDS.FlatSectionJlRow row in (FlatSectionJlTDS.FlatSectionJlDataTable)Table)
            {
                WorkGateway workGateway = new WorkGateway();
                workGateway.LoadByWorkId(jlWorkId, companyId);
                int assetId = workGateway.GetAssetId(jlWorkId);
                int projectId = workGateway.GetProjectId(jlWorkId);

                if (row.AssetID == assetId)
                {
                    // Update Comments
                    // ... Get raWorkId
                    int raWorkId = 0;
                    WorkGateway raWorkGateway = new WorkGateway();
                    raWorkGateway.LoadByProjectIdAssetIdWorkType(projectId, row.Section_, "Rehab Assessment", companyId);
                    if (raWorkGateway.Table.Rows.Count > 0)
                    {
                        raWorkId = raWorkGateway.GetWorkId(row.Section_, "Rehab Assessment", projectId);
                    }

                    // ... Get flWorkId
                    int flWorkId = 0;
                    WorkGateway flWorkGateway = new WorkGateway();
                    flWorkGateway.LoadByProjectIdAssetIdWorkType(projectId, row.Section_, "Full Length Lining", companyId);
                    if (flWorkGateway.Table.Rows.Count > 0)
                    {
                        flWorkId = flWorkGateway.GetWorkId(row.Section_, "Full Length Lining", projectId);
                    }

                    // ... Load All Comments
                    FlatSectionJLAllComments flatSectionJLAllComments = new FlatSectionJLAllComments();
                    flatSectionJLAllComments.LoadAllByJlWorkIdFlWorkIdRaWorkId(jlWorkId, flWorkId, raWorkId, companyId);

                    row.Comments = flatSectionJLAllComments.GetJLOrFLOrRAComments(companyId, flatSectionJLAllComments.Table.Rows.Count, "\n");

                    // Add M1 comments
                    WorkFullLengthLiningM1LateralGateway workFullLengthLiningM1LateralGateway = new WorkFullLengthLiningM1LateralGateway();
                    workFullLengthLiningM1LateralGateway.LoadByWorkIdLateral(flWorkId, row.AssetID, companyId);
                    if (workFullLengthLiningM1LateralGateway.Table.Rows.Count > 0)
                    {
                        string m1LateralComments = workFullLengthLiningM1LateralGateway.GetComments(flWorkId, row.AssetID);
                        if (m1LateralComments != "")
                        {
                            row.Comments = row.Comments + "\n\nType: M1 Lateral Comments\nComment: " + m1LateralComments;
                        }
                    }

                    WorkFullLengthLiningM1Gateway workFullLengthLiningM1Gateway = new WorkFullLengthLiningM1Gateway();
                    workFullLengthLiningM1Gateway.LoadByWorkId(flWorkId, companyId);
                    if (workFullLengthLiningM1Gateway.Table.Rows.Count > 0)
                    {
                        string trafficControlDetails = workFullLengthLiningM1Gateway.GetTrafficControlDetails(flWorkId);
                        if (trafficControlDetails != "")
                        {
                            row.Comments = row.Comments + "\n\nType: Traffic Control Details\nComment: " + trafficControlDetails ;
                        }

                        string standardByPassComments = workFullLengthLiningM1Gateway.GetStandardBypassComments(flWorkId);
                        if (standardByPassComments != "")
                        {
                            row.Comments = row.Comments + "\n\nType: Standard Bypass Comments\nComment: " + standardByPassComments ;
                        }
                    }

                    if (!row.IsCommentsNull())
                    {
                        row.Comments = row.Comments.Replace("<br>", "\n");
                    }

                    // Update History
                    FlatSectionJLAllHistory flatSectionJLAllHistory = new FlatSectionJLAllHistory();
                    flatSectionJLAllHistory.LoadAllByJlWorkIdFlWorkIdRaWorkId(jlWorkId, flWorkId, raWorkId, companyId);

                    row.History = flatSectionJLAllHistory.GetJLOrFLOrRAHistory(companyId, flatSectionJLAllHistory.Table.Rows.Count, "\n");

                    if (!row.IsHistoryNull())
                    {
                        row.History = row.History.Replace("<br>", "\n");
                    }
                }
            }
        }
        /// <summary>
        /// UpdateFLLaterals
        /// </summary>
        /// <param name="currentProjectId">currentProjectId</param>
        /// <param name="lateral_assetId">lateral_assetId</param>
        /// <param name="companyId">companyId</param>
        /// <param name="v1Inspection">v1Inspection</param>
        /// <param name="clientInspectionNo">clientInspectionNo</param>
        /// <param name="section_">section_</param>
        /// <param name="requiresRoboticPrep">requiresRoboticPrep</param>
        /// <param name="requiresRoboticPrepCompleted">requiresRoboticPrepCompleted</param>
        /// <param name="holdClientIssue">holdClientIssue</param>
        /// <param name="holdLFSIssue">holdLFSIssue</param>
        /// <param name="dyeTestReq">dyeTestReq</param>
        /// <param name="dyeTestComplete">dyeTestComplete</param>
        private void UpdateFLLaterals(int currentProjectId, int lateral_assetId, int companyId, DateTime? v1Inspection, string clientInspectionNo, int section_, bool requiresRoboticPrep, DateTime? requiresRoboticPrepCompleted, bool holdClientIssue, bool holdLFSIssue, bool dyeTestReq, DateTime? dyeTestComplete, string contractYear)
        {
            // Load work id
            WorkGateway workGateway = new WorkGateway();
            workGateway.LoadByProjectIdAssetIdWorkType(currentProjectId, section_, "Full Length Lining", companyId);
            if (workGateway.Table.Rows.Count > 0)
            {
                // ... ... Get WorkId for Full Length Lining
                int workIdFll = workGateway.GetWorkId(section_, "Full Length Lining", currentProjectId);

                // ... ... Modifications at M1
                WorkFullLengthLiningM1LateralGateway workFullLengthLiningM1LateralGateway = new WorkFullLengthLiningM1LateralGateway();
                workFullLengthLiningM1LateralGateway.LoadByWorkIdLateral(workIdFll, lateral_assetId, companyId);
                if (workFullLengthLiningM1LateralGateway.Table.Rows.Count > 0)
                {
                    // ... .... ... Load original data
                    string originalVideoDistance = workFullLengthLiningM1LateralGateway.GetVideoDistance(workIdFll, lateral_assetId);
                    string originalClockPosition = workFullLengthLiningM1LateralGateway.GetClockPosition(workIdFll, lateral_assetId);
                    string originalDistanceToCentre = workFullLengthLiningM1LateralGateway.GetDistanceToCentre(workIdFll, lateral_assetId);
                    string originalTimeOpened = workFullLengthLiningM1LateralGateway.GetTimeOpened(workIdFll, lateral_assetId);
                    string originalReverseSetup = workFullLengthLiningM1LateralGateway.GetReverseSetup(workIdFll, lateral_assetId);
                    DateTime? originalReinstate = workFullLengthLiningM1LateralGateway.GetReinstate(workIdFll, lateral_assetId);
                    string originalComments = workFullLengthLiningM1LateralGateway.GetComments(workIdFll, lateral_assetId);
                    bool originalDeleted = workFullLengthLiningM1LateralGateway.GetDeleted(workIdFll, lateral_assetId);
                    int originalCompanyId = workFullLengthLiningM1LateralGateway.GetCompanyId(workIdFll, lateral_assetId);
                    string originalClientInspectionNo = workFullLengthLiningM1LateralGateway.GetClientInspectionNo(workIdFll, lateral_assetId);
                    DateTime? originalV1Inspection = workFullLengthLiningM1LateralGateway.GetV1Inspection(workIdFll, lateral_assetId);
                    bool originalRequiresRoboticPrep = workFullLengthLiningM1LateralGateway.GetRequiresRoboticPrep(workIdFll, lateral_assetId);
                    DateTime? originalRequiresRoboticPrepDate = workFullLengthLiningM1LateralGateway.GetRequiresRoboticPrepDate(workIdFll, lateral_assetId);
                    bool originalHoldClientIssue = workFullLengthLiningM1LateralGateway.GetHoldClientIssue(workIdFll, lateral_assetId);
                    bool originalHoldLFSIssue = workFullLengthLiningM1LateralGateway.GetHoldLFSIssue(workIdFll, lateral_assetId);
                    bool originalLinelateral = workFullLengthLiningM1LateralGateway.GetLineLateral(workIdFll, lateral_assetId);
                    bool originalDyeTestReq = workFullLengthLiningM1LateralGateway.GetDyeTestReq(workIdFll, lateral_assetId);
                    DateTime? originalDyeTestComplete = null; if (workFullLengthLiningM1LateralGateway.GetDyeTestComplete(workIdFll, lateral_assetId).HasValue) originalDyeTestComplete = workFullLengthLiningM1LateralGateway.GetDyeTestComplete(workIdFll, lateral_assetId);
                    string originalContractYear = workFullLengthLiningM1LateralGateway.GetContractYear(workIdFll, lateral_assetId);

                    // New data
                    DateTime? newV1Inspection = v1Inspection;
                    string newClientInspectionNo = clientInspectionNo;
                    bool newRequiresRoboticPrep = requiresRoboticPrep;
                    bool newHoldClientIssue = holdClientIssue;
                    bool newHoldLFSIssue = holdLFSIssue;
                    DateTime? newRequiresRoboticPrepCompleted = null; if (requiresRoboticPrepCompleted.HasValue) newRequiresRoboticPrepCompleted = requiresRoboticPrepCompleted;
                    bool newDyeTestReq = dyeTestReq;
                    DateTime? newDyeTestComplete = null; if (dyeTestComplete.HasValue) newDyeTestComplete = dyeTestComplete;
                    string newContractYear = contractYear;

                    // Update work
                    WorkFullLengthLiningM1Lateral workFullLengthLiningM1Lateral = new WorkFullLengthLiningM1Lateral(null);
                    workFullLengthLiningM1Lateral.UpdateDirect(workIdFll, lateral_assetId, originalVideoDistance, originalClockPosition, originalDistanceToCentre, originalTimeOpened, originalReverseSetup, originalReinstate, originalComments, originalDeleted, originalCompanyId, originalClientInspectionNo, originalV1Inspection, originalRequiresRoboticPrep, originalRequiresRoboticPrepDate, originalHoldClientIssue, originalHoldLFSIssue, originalLinelateral, originalDyeTestReq, originalDyeTestComplete, originalContractYear, workIdFll, lateral_assetId, originalVideoDistance, originalClockPosition, originalDistanceToCentre, originalTimeOpened, originalReverseSetup, originalReinstate, originalComments, originalDeleted, originalCompanyId, newClientInspectionNo, newV1Inspection, newRequiresRoboticPrep, newRequiresRoboticPrepCompleted, newHoldClientIssue, newHoldLFSIssue, originalLinelateral, newDyeTestReq, newDyeTestComplete, newContractYear);
                }
            }
        }
        /// <summary>
        /// Save all sections & works to database (direct)
        /// </summary>
        /// <param name="workId">workId</param>
        /// <param name="projectId">projectId</param>
        /// <param name="sectionAssetId">sectionAssetId</param>
        /// <param name="countryId">countryId</param>
        /// <param name="provinceId">provinceId</param>
        /// <param name="countyId">countyId</param>
        /// <param name="cityId">cityId</param>
        /// <param name="length">length</param>
        /// <param name="companyId">companyId</param>
        /// <param name="isNewMeasuredFromDsmh">isNewMeasuredFromDsmh</param>
        /// <param name="prepDataRoboticPrep">prepDataRoboticPrep</param>
        /// <param name="prepDataRoboticPrepCompleted">prepDataRoboticPrepCompleted</param>
        public void Save(int workId, int projectId, int sectionAssetId, Int64 countryId, Int64? provinceId, Int64? countyId, Int64? cityId, string length, int companyId, bool isNewMeasuredFromDsmh, bool prepDataRoboticPrep, DateTime? prepDataRoboticPrepCompleted)
        {
            // Update Laterals
            RehabAssessmentLateralDetailsGateway rehabAssessmentLateralDetailsGateway = new RehabAssessmentLateralDetailsGateway(Data);

            if (rehabAssessmentLateralDetailsGateway.Table.Rows.Count > 0)
            {
                // Get WorkId
                if (workId == 0)
                {
                    WorkFullLengthLining workFullLengthLining = new WorkFullLengthLining(null);
                    workId = workFullLengthLining.InsertDirectEmptyWorks(projectId, sectionAssetId, null, "", null, null, null, null, null, null, null, false, false, false, false, false, false, false, companyId, false, "", "");
                }

                foreach (RehabAssessmentTDS.LateralDetailsRow row in (RehabAssessmentTDS.LateralDetailsDataTable)Table)
                {
                    int lateral = row.Lateral;

                    ProjectGateway projectGateway = new ProjectGateway();
                    projectGateway.LoadByProjectId(projectId);
                    int clientId = projectGateway.GetClientID(projectId);

                    // ... Insert lateral
                    if ((!row.InDatabase) && (!row.Deleted))
                    {
                        string videoDistance = rehabAssessmentLateralDetailsGateway.GetVideoDistance(lateral);
                        string clockPosition = rehabAssessmentLateralDetailsGateway.GetClockPosition(lateral);
                        string distanceToCentre = rehabAssessmentLateralDetailsGateway.GetDistanceToCentre(lateral);
                        string timeOpened = rehabAssessmentLateralDetailsGateway.GetTimeOpened(lateral);
                        string reverseSetup = rehabAssessmentLateralDetailsGateway.GetReverseSetup(lateral);
                        DateTime? reinstate = rehabAssessmentLateralDetailsGateway.GetReinstate(lateral);
                        string comments = rehabAssessmentLateralDetailsGateway.GetComments(lateral);
                        string clientLateralId = rehabAssessmentLateralDetailsGateway.GetClientLateralId(lateral);
                        string clientInspectionNo = rehabAssessmentLateralDetailsGateway.GetClientInspectionNo(lateral);
                        DateTime? v1Inspection = rehabAssessmentLateralDetailsGateway.GetV1Inspection(lateral);
                        bool requiredRoboticPrep = rehabAssessmentLateralDetailsGateway.GetRequiresRoboticPrep(lateral);
                        DateTime? requiredRoboticPrepDate = rehabAssessmentLateralDetailsGateway.GetRequiresRoboticPrepDate(lateral);
                        bool holdClientIssue = rehabAssessmentLateralDetailsGateway.GetHoldClientIssue(lateral);
                        bool holdLFSIssue = rehabAssessmentLateralDetailsGateway.GetHoldLFSIssue(lateral);
                        bool lineLateral = rehabAssessmentLateralDetailsGateway.GetLineLateral(lateral);
                        string flange = rehabAssessmentLateralDetailsGateway.GetFlange(lateral);
                        bool dyeTestReq = rehabAssessmentLateralDetailsGateway.GetDyeTestReq(lateral);
                        DateTime? dyeTestComplete = null; if (rehabAssessmentLateralDetailsGateway.GetDyeTestComplete(lateral).HasValue) dyeTestComplete = rehabAssessmentLateralDetailsGateway.GetDyeTestComplete(lateral);
                        string contractYear = rehabAssessmentLateralDetailsGateway.GetContractYear(lateral);

                        // ... ... Insert asset
                        int lateral_assetId = SaveLateral(row, projectId, sectionAssetId, countryId, provinceId, countyId, cityId, companyId, isNewMeasuredFromDsmh);

                        // ... ... Insert lateral to FulllengthLining
                        InsertFLLLateral(workId, lateral_assetId, videoDistance, clockPosition, distanceToCentre, timeOpened, reverseSetup, reinstate, comments, false, companyId, clientInspectionNo, v1Inspection, requiredRoboticPrep, requiredRoboticPrepDate, holdClientIssue, holdLFSIssue, lineLateral, dyeTestReq, dyeTestComplete, contractYear);

                        // ... ... If lateral will be in Junction Lining
                        if ((row.LineLateral) && (!row.InJlDatabase))
                        {
                            if (((!prepDataRoboticPrep) && (!prepDataRoboticPrepCompleted.HasValue)) || ((prepDataRoboticPrep) && (prepDataRoboticPrepCompleted.HasValue)))
                            {
                                // ... ... Load work id
                                int sectionWorkId = 0;
                                WorkJunctionLiningSection workJunctionLiningSection = new WorkJunctionLiningSection(null);
                                sectionWorkId = workJunctionLiningSection.InsertDirect(projectId, sectionAssetId, null, 0, 0, false, "No", 0, 0, false, companyId, "", "", "", "", false, "", 0);

                                WorkJunctionLiningLateralGateway workJunctionLiningLateralGateway = new WorkJunctionLiningLateralGateway();
                                WorkJunctionLiningLateral workJunctionLiningLateral = new WorkJunctionLiningLateral(workJunctionLiningLateralGateway.Data);
                                workJunctionLiningLateral.InsertDirect(projectId, lateral_assetId, sectionWorkId, null, null, null, null, null, null, null, null, null, "", null, null, null, 0, null, null, null, 0, v1Inspection, true, false, "", false, null, null, false, companyId, "", "", "", false, null, "", flange, "", "", false, null, false, null, false, holdClientIssue, null, holdLFSIssue, null, requiredRoboticPrep, requiredRoboticPrepDate, "", "", dyeTestReq, dyeTestComplete, contractYear);
                            }
                        }

                        // ... Insert material for M1 lateral
                        try
                        {
                            string material = rehabAssessmentLateralDetailsGateway.GetMaterialType(lateral);
                            if (material != "")
                            {
                                InsertMaterial(lateral_assetId, material, companyId);
                            }
                        }
                        catch { }

                        //... Insert client lateral id
                        try
                        {
                            if (clientLateralId.Trim() != "")
                            {
                                LfsAssetSewerLateralClient lfsAssetSewerLateralClient = new LfsAssetSewerLateralClient(null);
                                lfsAssetSewerLateralClient.InsertDirect(lateral_assetId, clientId, clientLateralId, false, companyId);
                            }
                        }
                        catch { }
                    }

                    // ... Edit laterals
                    if ((row.InDatabase) && (!row.Deleted))
                    {
                        // original values
                        string originalVideoDistance = rehabAssessmentLateralDetailsGateway.GetVideoDistanceOriginal(lateral);
                        string originalClockPosition = rehabAssessmentLateralDetailsGateway.GetClockPositionOriginal(lateral);
                        string originalDistanceToCentre = rehabAssessmentLateralDetailsGateway.GetDistanceToCentreOriginal(lateral);
                        string originalTimeOpened = rehabAssessmentLateralDetailsGateway.GetTimeOpenedOriginal(lateral);
                        string originalReverseSetup = rehabAssessmentLateralDetailsGateway.GetReverseSetupOriginal(lateral);
                        DateTime? originalReinstate = rehabAssessmentLateralDetailsGateway.GetReinstateOriginal(lateral);
                        string originalComments = rehabAssessmentLateralDetailsGateway.GetCommentsOriginal(lateral);
                        string originalClientLateralId = rehabAssessmentLateralDetailsGateway.GetClientLateralIdOriginal(lateral);
                        string originalClientInspectionNo = rehabAssessmentLateralDetailsGateway.GetClientInspectionNoOriginal(lateral);
                        DateTime? originalV1Inspection = rehabAssessmentLateralDetailsGateway.GetV1InspectionOriginal(lateral);
                        bool originalRequiresRoboticPrep = rehabAssessmentLateralDetailsGateway.GetRequiresRoboticPrepOriginal(lateral);
                        DateTime? originalRequiresRoboticPrepDate = rehabAssessmentLateralDetailsGateway.GetRequiresRoboticPrepDateOriginal(lateral);
                        bool originalHoldClientIssue = rehabAssessmentLateralDetailsGateway.GetHoldClientIssueOriginal(lateral);
                        bool originalHoldLFSIssue = rehabAssessmentLateralDetailsGateway.GetHoldLFSIssueOriginal(lateral);
                        bool originalLineLateral = rehabAssessmentLateralDetailsGateway.GetLineLateralOriginal(lateral);
                        bool originalDyeTestReq = rehabAssessmentLateralDetailsGateway.GetDyeTestReqOriginal(lateral);
                        DateTime? originalDyeTestComplete = null; if (rehabAssessmentLateralDetailsGateway.GetDyeTestCompleteOriginal(lateral).HasValue) originalDyeTestComplete = rehabAssessmentLateralDetailsGateway.GetDyeTestCompleteOriginal(lateral);
                        string originalContractYear = rehabAssessmentLateralDetailsGateway.GetContractYearOriginal(lateral);

                        // new values
                        string newVideoDistance = rehabAssessmentLateralDetailsGateway.GetVideoDistance(lateral);
                        string newClockPosition = rehabAssessmentLateralDetailsGateway.GetClockPosition(lateral);
                        string newDistanceToCentre = rehabAssessmentLateralDetailsGateway.GetDistanceToCentre(lateral);
                        string newTimeOpened = rehabAssessmentLateralDetailsGateway.GetTimeOpened(lateral);
                        string newReverseSetup = rehabAssessmentLateralDetailsGateway.GetReverseSetup(lateral);
                        DateTime? newReinstate = rehabAssessmentLateralDetailsGateway.GetReinstate(lateral);
                        string newComments = rehabAssessmentLateralDetailsGateway.GetComments(lateral);
                        string newClientLateralId = rehabAssessmentLateralDetailsGateway.GetClientLateralId(lateral);
                        string newClientInspectionNo = rehabAssessmentLateralDetailsGateway.GetClientInspectionNo(lateral);
                        DateTime? newV1Inspection = rehabAssessmentLateralDetailsGateway.GetV1Inspection(lateral);
                        bool newRequiresRoboticPrep = rehabAssessmentLateralDetailsGateway.GetRequiresRoboticPrep(lateral);
                        DateTime? newRequiresRoboticPrepDate = rehabAssessmentLateralDetailsGateway.GetRequiresRoboticPrepDate(lateral);
                        bool newHoldClientIssue = rehabAssessmentLateralDetailsGateway.GetHoldClientIssue(lateral);
                        bool newHoldLFSIssue = rehabAssessmentLateralDetailsGateway.GetHoldLFSIssue(lateral);
                        bool newLineLateral = rehabAssessmentLateralDetailsGateway.GetLineLateral(lateral);
                        string newFlange = rehabAssessmentLateralDetailsGateway.GetFlange(lateral);
                        bool newDyeTestReq = rehabAssessmentLateralDetailsGateway.GetDyeTestReq(lateral);
                        DateTime? newDyeTestComplete = null; if (rehabAssessmentLateralDetailsGateway.GetDyeTestComplete(lateral).HasValue) newDyeTestComplete = rehabAssessmentLateralDetailsGateway.GetDyeTestComplete(lateral);
                        string newContractYear = rehabAssessmentLateralDetailsGateway.GetContractYear(lateral);

                        // ... Update laterals
                        int lateral_assetId = SaveLateral(row, projectId, sectionAssetId, countryId, provinceId, countyId, cityId, companyId, isNewMeasuredFromDsmh);
                        UpdateLateral(row, projectId, sectionAssetId, countryId, provinceId, countyId, cityId, length, companyId);

                        // ... Update if lateral is in FulllenghtLining
                        if (!row.InFllDatabase)
                        {
                            WorkFullLengthLiningM1LateralGateway workFullLengthLiningM1LateralGateay = new WorkFullLengthLiningM1LateralGateway();
                            workFullLengthLiningM1LateralGateay.LoadAllByWorkIdLateral(workId, lateral_assetId, companyId);

                            if (workFullLengthLiningM1LateralGateay.Table.Rows.Count == 0)
                            {
                                InsertFLLLateral(workId, lateral_assetId, newVideoDistance, newClockPosition, newDistanceToCentre, newTimeOpened, newReverseSetup, newReinstate, newComments, false, companyId, newClientInspectionNo, newV1Inspection, newRequiresRoboticPrep, newRequiresRoboticPrepDate, newHoldClientIssue, newHoldLFSIssue, newLineLateral, newDyeTestReq, newDyeTestComplete, newContractYear);
                            }
                            else
                            {
                                UpdateFLLLateral(workId, lateral_assetId, originalVideoDistance, originalClockPosition, originalDistanceToCentre, originalTimeOpened, originalReverseSetup, originalReinstate, originalComments, true, companyId, originalClientInspectionNo, originalV1Inspection, originalRequiresRoboticPrep, originalRequiresRoboticPrepDate, originalHoldClientIssue, originalHoldLFSIssue, originalLineLateral, originalDyeTestReq, originalDyeTestComplete, originalContractYear, workId, lateral_assetId, newVideoDistance, newClockPosition, newDistanceToCentre, newTimeOpened, newReverseSetup, newReinstate, newComments, false, companyId, newClientInspectionNo, newV1Inspection, newRequiresRoboticPrep, newRequiresRoboticPrepDate, newHoldClientIssue, newHoldLFSIssue, newLineLateral, newDyeTestReq, newDyeTestComplete, newContractYear);
                            }
                        }
                        else
                        {
                            UpdateFLLLateral(workId, lateral_assetId, originalVideoDistance, originalClockPosition, originalDistanceToCentre, originalTimeOpened, originalReverseSetup, originalReinstate, originalComments, false, companyId, originalClientInspectionNo, originalV1Inspection, originalRequiresRoboticPrep, originalRequiresRoboticPrepDate, originalHoldClientIssue, originalHoldLFSIssue, originalLineLateral, originalDyeTestReq, originalDyeTestComplete, originalContractYear,workId, lateral_assetId, newVideoDistance, newClockPosition, newDistanceToCentre, newTimeOpened, newReverseSetup, newReinstate, newComments, false, companyId, newClientInspectionNo, newV1Inspection, newRequiresRoboticPrep, newRequiresRoboticPrepDate, newHoldClientIssue, newHoldLFSIssue, newLineLateral, newDyeTestReq, newDyeTestComplete, newContractYear);
                        }

                        // ... Update if lateral is in Junction Lining
                        if (row.InJlDatabase)
                        {
                            if (originalLineLateral == newLineLateral)
                            {
                                // ... ... Update jl lateral (clientInspectionNo, v1Inspection, requiredRoboticPrep, requiredRoboticPrepDate, holdClientIssue, holdLFSIssue, flange)
                                UpdateJLLaterals(projectId, lateral_assetId, companyId, newV1Inspection, newClientInspectionNo, newRequiresRoboticPrep, newRequiresRoboticPrepDate, newHoldClientIssue, newHoldLFSIssue, newFlange, newDyeTestReq, newDyeTestComplete, newContractYear);
                            }
                            else
                            {
                                int sectionWorkId = 0;
                                WorkJunctionLiningSection workJunctionLiningSection = new WorkJunctionLiningSection(null);
                                sectionWorkId = workJunctionLiningSection.InsertDirect(projectId, sectionAssetId, null, 0, 0, false, "No", 0, 0, false, companyId, "", "", "", "", false, "", 0);

                                // Delete empty lateral form jl.
                                DeleteJLLaterals(projectId, lateral_assetId, companyId, sectionWorkId);
                            }
                        }
                        else
                        {
                            // ... ... Insert if should be in junction Lining
                            if (row.LineLateral)
                            {
                                if (((!prepDataRoboticPrep) && (!prepDataRoboticPrepCompleted.HasValue)) || ((prepDataRoboticPrep) && (prepDataRoboticPrepCompleted.HasValue)))
                                {
                                    int sectionWorkId = 0;

                                    // Insert to jl laterals
                                    WorkJunctionLiningSection workJunctionLiningSection = new WorkJunctionLiningSection(null);
                                    sectionWorkId = workJunctionLiningSection.InsertDirect(projectId, sectionAssetId, null, 0, 0, false, "No", 0, 0, false, companyId, "", "", "", "", false, "", 0);

                                    WorkJunctionLiningLateralGateway workJunctionLiningLateralGateway = new WorkJunctionLiningLateralGateway();
                                    WorkJunctionLiningLateral workJunctionLiningLateral = new WorkJunctionLiningLateral(workJunctionLiningLateralGateway.Data);
                                    workJunctionLiningLateral.InsertDirect(projectId, lateral_assetId, sectionWorkId, null, null, null, null, null, null, null, null, null, "", null, null, null, 0, null, null, null, 0, newV1Inspection, false, false, "", false, null, null, false, companyId, "", "", "", false, null, newClientInspectionNo, newFlange, "", "", false, null, false, null, false, newHoldClientIssue, null, newHoldLFSIssue, null, newRequiresRoboticPrep, newRequiresRoboticPrepDate, "", "", newDyeTestReq, newDyeTestComplete, newContractYear);
                                }
                            }
                        }

                        string material = rehabAssessmentLateralDetailsGateway.GetMaterialType(lateral);
                        if (material != "")
                        {
                            InsertMaterial(lateral_assetId, material, companyId);
                        }

                        try
                        {
                            LfsAssetSewerLateralClientGateway lfsAssetSewerLateralClientGateway = new LfsAssetSewerLateralClientGateway();
                            lfsAssetSewerLateralClientGateway.LoadAllByAssetIdClientId(lateral_assetId, clientId, companyId);

                            if (lfsAssetSewerLateralClientGateway.Table.Rows.Count == 0)
                            {
                                LfsAssetSewerLateralClient lfsAssetSewerLateralClient = new LfsAssetSewerLateralClient(null);
                                lfsAssetSewerLateralClient.InsertDirect(lateral_assetId, clientId, originalClientLateralId, false, companyId);
                            }
                            else
                            {
                                LfsAssetSewerLateralClient lfsAssetSewerLateralClient = new LfsAssetSewerLateralClient(null);
                                string originalClientLateralId2 = lfsAssetSewerLateralClientGateway.GetClientLateralId(lateral_assetId, clientId);

                                lfsAssetSewerLateralClient.UpdateDirect(lateral_assetId, clientId, originalClientLateralId, false, companyId, lateral_assetId, clientId, newClientLateralId, false, companyId);
                            }
                        }
                        catch
                        { }
                    }

                    // ... Delete laterals
                    if ((row.InDatabase) && (row.Deleted))
                    {
                        DeleteFLLLateral(workId, row.Lateral, companyId, clientId);
                    }
                }
            }
        }
        /// <summary>
        /// UpdateForReport
        /// </summary>        
        /// <param name="companyId">companyId</param>
        public void UpdateForReport(int companyId)
        {
            foreach (JlLiningPlanTDS.JlLiningPlanJlinerRow row in (JlLiningPlanTDS.JlLiningPlanJlinerDataTable)Table)
            {
                // Update comments
                int jlWorkId = row.SectionWorkID;
                WorkGateway workGateway = new WorkGateway();
                workGateway.LoadByWorkId(jlWorkId, companyId);
                int assetId = workGateway.GetAssetId(jlWorkId);
                int projectId = workGateway.GetProjectId(jlWorkId);

                // ... Get raWorkId
                int raWorkId = 0;
                WorkGateway raWorkGateway = new WorkGateway();
                raWorkGateway.LoadByProjectIdAssetIdWorkType(projectId, row.Section_, "Rehab Assessment", companyId);
                if (raWorkGateway.Table.Rows.Count > 0)
                {
                    raWorkId = raWorkGateway.GetWorkId(row.Section_, "Rehab Assessment", projectId);
                }

                // ... Get raWorkId
                int flWorkId = 0;
                WorkGateway flWorkGateway = new WorkGateway();
                flWorkGateway.LoadByProjectIdAssetIdWorkType(projectId, row.Section_, "Full Length Lining", companyId);
                if (flWorkGateway.Table.Rows.Count > 0)
                {
                    flWorkId = flWorkGateway.GetWorkId(row.Section_, "Full Length Lining", projectId);
                }

                // ... Load All Comments
                FlatSectionJLAllComments flatSectionJLAllComments = new FlatSectionJLAllComments();
                flatSectionJLAllComments.LoadAllByJlWorkIdFlWorkIdRaWorkId(jlWorkId, flWorkId, raWorkId, companyId);

                row.Comments = flatSectionJLAllComments.GetJLOrFLOrRAComments(companyId, flatSectionJLAllComments.Table.Rows.Count, "\n");

                // Add M1 comments
                WorkFullLengthLiningM1LateralGateway workFullLengthLiningM1LateralGateway = new WorkFullLengthLiningM1LateralGateway();
                workFullLengthLiningM1LateralGateway.LoadByWorkIdLateral(flWorkId, row.AssetID, companyId);
                if (workFullLengthLiningM1LateralGateway.Table.Rows.Count > 0)
                {
                    string m1LateralComment = workFullLengthLiningM1LateralGateway.GetComments(flWorkId, row.AssetID);
                    if (m1LateralComment != "")
                    {
                        row.Comments = row.Comments + "\n\nType: M1 Lateral Comments\nComment: " + m1LateralComment;
                    }
                }

                if (!row.IsCommentsNull())
                {
                    row.Comments = row.Comments.Replace("<br>", "\n");
                }

                // Round PullInDistance
                if (Validator.IsValidDecimal(row.PullInDistance))
                {
                    row.PullInDistance = decimal.Round(decimal.Parse(row.PullInDistance), 1).ToString();
                }
                else
                {
                    row.PullInDistance = "0";
                }

                // Update Main size
                if (!row.IsMainSizeNull())
                {
                    if (Distance.IsValidDistance(row.MainSize))
                    {
                        Distance distance = new Distance(row.MainSize);

                        switch (distance.DistanceType)
                        {
                            case 2:
                                row.MainSize = distance.ToStringInEng1();
                                break;
                            case 3:
                                if (Convert.ToDouble(row.MainSize) > 99)
                                {
                                    double newMainSize = 0;
                                    newMainSize = Convert.ToDouble(row.MainSize) * 0.03937;
                                    row.MainSize = Convert.ToString(Math.Ceiling(newMainSize)) + "\"";
                                }
                                else
                                {
                                    row.MainSize = row.MainSize + "\"";
                                }
                                break;
                            case 4:
                                row.MainSize = distance.ToStringInEng1();
                                break;
                            case 5:
                                row.MainSize = distance.ToStringInEng1();
                                break;
                        }
                    }
                }
            }
        }