protected void cvStartMileage_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int serviceId = Int32.Parse(hdfServiceId.Value);

            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGatewayForId = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
            int? unitId = serviceInformationBasicInformationGatewayForId.GetUnitID(serviceId);

            UnitsGateway unitsGateway = new UnitsGateway();
            unitsGateway.LoadByUnitId((int)unitId, companyId);
            string unitType = unitsGateway.GetType((int)unitId);

            if (unitType == "Vehicle")
            {
                if (tbxPnlStartWorkStartMileage.Text == "")
                {
                    args.IsValid = false;
                }
            }
        }
        private void LoadDetailedData(int serviceId)
        {
            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
            if (serviceInformationBasicInformationGateway.Table.Rows.Count > 0)
            {
                // Load for General Tab
                tbxGeneralCreatedBy.Text = serviceInformationBasicInformationGateway.GetCreatedBy(serviceId);
                tbxGeneralMileage.Text = serviceInformationBasicInformationGateway.GetMileage(serviceId);
                lblGeneralMileageUnitOfMeasurement.Text = serviceInformationBasicInformationGateway.GetMileageUnitOfMeasurement(serviceId);

                // Load for Assginment Tab
                tbxAssignmentDataAssignmentDateTime.Text = serviceInformationBasicInformationGateway.GetAssignmentDateTime(serviceId).ToString();

                tbxAssignmentDataAssignedDeadlineDate.Text = "";
                if (serviceInformationBasicInformationGateway.GetAssignedDeadlineDate(serviceId).HasValue)
                {
                    DateTime deadlineDate = (DateTime)serviceInformationBasicInformationGateway.GetAssignedDeadlineDate(serviceId);
                    tbxAssignmentDataAssignedDeadlineDate.Text = deadlineDate.Month.ToString() + "/" + deadlineDate.Day.ToString() + "/" + deadlineDate.Year.ToString();
                }

                if ((serviceInformationBasicInformationGateway.GetToTeamMember(serviceId)) && (serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId)) == "")
                {
                    // ... For team member
                    rbtnAssignmentDataToTeamMember.Checked = true;
                    if (serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId).HasValue)
                    {
                        int teamMemberId = (int)serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId);
                        EmployeeGateway employeeGateway = new EmployeeGateway();
                        employeeGateway.LoadByEmployeeId(teamMemberId);

                        tbxAssigmentDataToTeamMemberName.Text = employeeGateway.GetLastName(teamMemberId) + " " + employeeGateway.GetFirstName(teamMemberId);
                    }

                    // ... For third party vendor
                    rbtnAssignmentDataToThirdPartyVendor.Checked = false;
                }
                else
                {
                    if ((!serviceInformationBasicInformationGateway.GetToTeamMember(serviceId)) && (serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId)) != "")
                    {
                        // ... For team member
                        rbtnAssignmentDataToTeamMember.Checked = false;

                        // ... For third party vendor
                        rbtnAssignmentDataToThirdPartyVendor.Checked = true;
                        tbxAssignmentDataAssignToThirdPartyVendor.Text = serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId);
                    }
                    else
                    {
                        rbtnAssignmentDataToTeamMember.Checked = false;
                        rbtnAssignmentDataToThirdPartyVendor.Checked = false;
                    }
                }

                tbxAssignmentDataAcceptedDateTime.Text = serviceInformationBasicInformationGateway.GetAcceptedDateTime(serviceId).ToString();

                DateTime? acceptedDateTime = serviceInformationBasicInformationGateway.GetAcceptedDateTime(serviceId);
                if (acceptedDateTime.HasValue) tbxAssignmentDataAcceptedDateTime.Text = acceptedDateTime.ToString();

                DateTime? rejectedDateTime = serviceInformationBasicInformationGateway.GetRejectedDateTime(serviceId);
                if (rejectedDateTime.HasValue) tbxAssignmentDataRejectedDateTime.Text = rejectedDateTime.ToString();
                tbxAssignmentDataRejectedReason.Text = serviceInformationBasicInformationGateway.GetRejectedReason(serviceId);

                // Load for StartWork Tab
                tbxStartWorkDataWorkStartDateTime.Text = serviceInformationBasicInformationGateway.GetStartWorkDateTime(serviceId).ToString();

                tbxStartWorkDataUnitOutOfServiceDate.Text = "";
                if (serviceInformationBasicInformationGateway.GetUnitOutOfServiceDate(serviceId).HasValue)
                {
                    DateTime outOfServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitOutOfServiceDate(serviceId);
                    tbxStartWorkDataUnitOutOfServiceDate.Text = outOfServiceDate.Month.ToString() + "/" + outOfServiceDate.Day.ToString() + "/" + outOfServiceDate.Year.ToString();
                }

                tbxStartWorkDataStartMileage.Text = serviceInformationBasicInformationGateway.GetStartWorkMileage(serviceId);
                lblStartWorkDataMileageUnitOfMeasurement.Text = serviceInformationBasicInformationGateway.GetMileageUnitOfMeasurement(serviceId);

                // Load for Complete Work Tab
                tbxCompleteWorkDataCompleteWorkDateTime.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDateTime(serviceId).ToString();

                tbxCompleteWorkDataUnitBackInServiceDate.Text = "";
                if (serviceInformationBasicInformationGateway.GetUnitBackInServiceDate(serviceId).HasValue)
                {
                    DateTime backInServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitBackInServiceDate(serviceId);
                    tbxCompleteWorkDataUnitBackInServiceDate.Text = backInServiceDate.Month.ToString() + "/" + backInServiceDate.Day.ToString() + "/" + backInServiceDate.Year.ToString();
                }

                tbxCompleteWorkDataCompleteMileage.Text = serviceInformationBasicInformationGateway.GetCompleteWorkMileage(serviceId);
                lblCompleteWorkDataMileageUnitOfMeasurement.Text = serviceInformationBasicInformationGateway.GetMileageUnitOfMeasurement(serviceId);

                // ... For team member
                tbxCompleteWorkDataDescription.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailDescription(serviceId);
                ckbxCompleteWorkDataPreventable.Checked = serviceInformationBasicInformationGateway.GetCompleteWorkDetailPreventable(serviceId);
                tbxCompleteWorkDataLabourHours.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMLabourHours(serviceId).ToString();

                // ... For third party vendor
                tbxCompleteWorkDataDescriptionThirdPartyVendor.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailDescription(serviceId);
                ckbxCompleteWorkDataPreventableThirdPartyVendor.Checked = serviceInformationBasicInformationGateway.GetCompleteWorkDetailPreventable(serviceId);
                tbxCompleteWorkDataInvoiceNumberThirdPartyVendor.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceNumber(serviceId);
                tbxCompleteWorkDataInvoiceAmountThirdPartyVendor.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceAmout(serviceId).ToString();
            }
        }
        /// <summary>
        /// UpdateForDashboard
        /// </summary>
        /// <param name="companyId">companyId</param>
        public void UpdateForDashboard(int companyId)
        {
            foreach (DashboardTDS.DashboardInProgressServiceRequestsRow row in (DashboardTDS.DashboardInProgressServiceRequestsDataTable)Table)
            {
                // Get ruleId for the each service
                ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway();
                serviceInformationBasicInformationGateway.LoadByServiceId(row.ServiceID, companyId);
                int? ruleId = serviceInformationBasicInformationGateway.GetRuleId(row.ServiceID);

                if (ruleId.HasValue)
                {
                    RuleGateway ruleGateway = new RuleGateway();
                    ruleGateway.LoadAllByRuleId((int)ruleId, companyId);

                    if (ruleGateway.Table.Rows.Count > 0)
                    {
                        // Get ruleName for each service if exists
                        string ruleName = ruleGateway.GetName((int)ruleId);
                        row.InProgressServicesCompleteName = row.InProgressServicesCompleteName + " - " + ruleName;
                    }
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_SERVICES_VIEW"]) && Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_SERVICES_EDIT"])))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) || ((string)Request.QueryString["service_id"] == null) || ((string)Request.QueryString["active_tab"] == null) || ((string)Request.QueryString["dashboard"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in services_edit.aspx");
                }

                // Tag Page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfFmType.Value = "Services";
                hdfServiceId.Value = Request.QueryString["service_id"].ToString();
                hdfActiveTab.Value = Request.QueryString["active_tab"].ToString();
                hdfLoginId.Value = Convert.ToInt32(Session["loginID"]).ToString();
                hdfDashboard.Value = Request.QueryString["dashboard"].ToString();

                // Prepare initial data
                Session.Remove("serviceCostsDummy");
                Session.Remove("serviceNotesDummy");

                // If coming from
                int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                int serviceId = Int32.Parse(hdfServiceId.Value.Trim());
                string fmType = hdfFmType.Value;

                // ... services_navigator2.aspx, services_add_request.aspx, services_manager_tool.aspx
                if ((Request.QueryString["source_page"] == "services_navigator2.aspx") || (Request.QueryString["source_page"] == "services_add_request.aspx") || (Request.QueryString["source_page"] == "services_manager_tool.aspx"))
                {
                    StoreNavigatorState();
                    ViewState["update"] = "no";

                    // ... Set initial tab
                    if ((string)Session["dialogOpenedServices"] != "1")
                    {
                        hdfActiveTab.Value = Request.QueryString["active_tab"];

                        serviceInformationTDS = new ServiceInformationTDS();

                        ServiceInformationBasicInformation serviceInformationBasicInformation = new ServiceInformationBasicInformation(serviceInformationTDS);
                        serviceInformationBasicInformation.LoadByServiceId(serviceId, companyId);

                        ServiceInformationServiceCost serviceInformationServiceCostForEdit = new ServiceInformationServiceCost(serviceInformationTDS);
                        serviceInformationServiceCostForEdit.LoadByServiceId(serviceId, companyId);

                        ServiceInformationServiceNote serviceInformationServiceNoteForEdit = new ServiceInformationServiceNote(serviceInformationTDS);
                        serviceInformationServiceNoteForEdit.LoadByServiceId(serviceId, companyId);
                    }
                    else
                    {
                        hdfActiveTab.Value = (string)Session["activeTabServices"];

                        // Restore datasets
                        serviceInformationTDS = (ServiceInformationTDS)Session["serviceInformationTDS"];
                    }

                    // ... Store dataset
                    Session["serviceInformationTDS"] = serviceInformationTDS;
                }

                // ... services_summary.aspx or services_edit
                if ((Request.QueryString["source_page"] == "services_summary.aspx") || (Request.QueryString["source_page"] == "services_edit.aspx"))
                {
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];

                    // ... Restore dataset
                    serviceInformationTDS = (ServiceInformationTDS)Session["serviceInformationTDS"];

                    // ... Set initial tab
                    if ((string)Session["dialogOpenedServices"] != "1")
                    {
                        hdfActiveTab.Value = Request.QueryString["active_tab"];

                        if (ViewState["update"].ToString().Trim() == "yes")
                        {
                            ServiceInformationBasicInformation serviceInformationBasicInformation = new ServiceInformationBasicInformation(serviceInformationTDS);
                            serviceInformationBasicInformation.LoadByServiceId(serviceId, companyId);

                            ServiceInformationServiceCost serviceInformationServiceCostForEdit = new ServiceInformationServiceCost(serviceInformationTDS);
                            serviceInformationServiceCostForEdit.LoadByServiceId(serviceId, companyId);

                            ServiceInformationServiceNote serviceInformationServiceNoteForEdit = new ServiceInformationServiceNote(serviceInformationTDS);
                            serviceInformationServiceNoteForEdit.LoadByServiceId(serviceId, companyId);

                            // ... Store dataset
                            Session["serviceInformationTDS"] = serviceInformationTDS;
                        }
                    }
                    else
                    {
                        hdfActiveTab.Value = (string)Session["activeTabServices"];
                    }
                }

                // Prepare initial data
                lblMissingData.Visible = false;

                // ... Set initial tab
                int activeTab = Int32.Parse(hdfActiveTab.Value);
                tcDetailedInformation.ActiveTabIndex = activeTab;

                // ... For total cost
                ServiceInformationServiceCost serviceInformationServiceCostForTotalCost = new ServiceInformationServiceCost(serviceInformationTDS);
                tbxTotalCost.Text = Decimal.Round(serviceInformationServiceCostForTotalCost.GetTotalCost(serviceId, companyId), 2).ToString();

                // ... Data for current servicew
                LoadServiceData(companyId);

                // Databind
                Page.DataBind();

                ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
                if (serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId).HasValue)
                {
                    try
                    {
                        ddlAssignmentDataAssignToTeamMember.SelectedValue = ((int)serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId)).ToString();
                    }
                    catch
                    {
                        ddlAssignmentDataAssignToTeamMember.SelectedIndex = 0;
                    }

                    int teamMemberId = (int)serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId);
                    EmployeeGateway employeeGateway = new EmployeeGateway();
                    employeeGateway.LoadByEmployeeId(teamMemberId);
                    tbxAssignmentDataAssignToTeamMemberReadOnly.Text = employeeGateway.GetLastName(teamMemberId) + " " + employeeGateway.GetFirstName(teamMemberId);
                }

                // For thirds party vendor autocomplete
                aceThirdPartyVendor.ContextKey = serviceId.ToString() + "," + hdfCompanyId.Value;

                // ... For Library
                if (Session["libraryTDSForServices"] != null)
                {
                    libraryTDSForServices = (LibraryTDS)Session["libraryTDSForServices"];
                }
                else
                {
                    libraryTDSForServices = new LibraryTDS();
                }
            }
            else
            {
                // Restore datasets
                serviceInformationTDS = (ServiceInformationTDS)Session["serviceInformationTDS"];

                // Set initial tab
                int activeTab = Int32.Parse(hdfActiveTab.Value);
                tcDetailedInformation.ActiveTabIndex = activeTab;

                if (Session["libraryTDSForServices"] != null)
                {
                    libraryTDSForServices = (LibraryTDS)Session["libraryTDSForServices"];
                }
                else
                {
                    libraryTDSForServices = new LibraryTDS();
                }

                LoadNotes();
            }
        }
        protected void cvGeneralDataMileage_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;

            if (ckbxMtoDto.Checked)
            {
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int serviceId = Int32.Parse(hdfServiceId.Value);

                ServiceInformationBasicInformationGateway serviceInformationBasicInformationGatewayForId = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
                int? unitId = serviceInformationBasicInformationGatewayForId.GetUnitID(serviceId);

                UnitsGateway unitsGateway = new UnitsGateway();
                unitsGateway.LoadByUnitId((int)unitId, companyId);
                string unitType = unitsGateway.GetType((int)unitId);

                if (unitType == "Vehicle")
                {
                    if (tbxGeneralMileage.Text == "")
                    {
                        args.IsValid = false;
                        hdfErrorFieldList.Value = hdfErrorFieldList.Value + ", Mileage (General)";
                    }
                }
            }
        }
        private void Save()
        {
            // Validate data
            bool validData = true;

            validData = ValidatePage();

            // For valid data
            if (validData)
            {
                // Costs Gridview, if the gridview is edition mode
                if (grdCosts.EditIndex >= 0)
                {
                    grdCosts.UpdateRow(grdCosts.EditIndex, true);
                }

                // Notes Gridview, if the gridview is edition mode
                if (grdNotes.EditIndex >= 0)
                {
                    grdNotes.UpdateRow(grdNotes.EditIndex, true);
                }

                // Save cost and notes data
                GrdCostsAdd();
                GrdNotesAdd();

                // Save data
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int serviceId = Int32.Parse(hdfServiceId.Value);

                // Unmodified data
                ServiceInformationBasicInformationGateway serviceInformationBasicInformationGatewayForEdit = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
                string serviceState = serviceInformationBasicInformationGatewayForEdit.GetServiceState(serviceId);
                string associatedChecklistRuleState = serviceInformationBasicInformationGatewayForEdit.GetAssociatedChecklistRuleState(serviceId);

                // ... Get basic service data
                bool newMtoDto = ckbxMtoDto.Checked;
                string newServiceDescription = tbxServiceDescription.Text.Trim();

                // ... Get general service data
                string newMileage = tbxGeneralMileage.Text.Trim();

                // ... Get assigned data
                DateTime? newAssignmentDateTime = null;
                DateTime? newAssignmentDeadlineDate = null;
                bool newToTeamMember = false;
                int? newAssignTeamMemberID = null;
                string newThirdPartyVendor = "";
                DateTime? assignmentAcceptedDateTime = null;
                string newAssignmentRejectedReason = "";
                DateTime? assignmentRejectedDateTime = null;

                if (pnlAssignmentData.Visible)
                {
                    if (pnlAssignedToReadOnly.Visible)
                    {
                        if (serviceInformationBasicInformationGatewayForEdit.GetAssignmentDateTimeOriginal(serviceId).HasValue) newAssignmentDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetAssignmentDateTimeOriginal(serviceId);
                        if (serviceInformationBasicInformationGatewayForEdit.GetAssignedDeadlineDate(serviceId).HasValue) newAssignmentDeadlineDate = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetAssignedDeadlineDate(serviceId);
                        newToTeamMember = serviceInformationBasicInformationGatewayForEdit.GetToTeamMember(serviceId);
                        if (newToTeamMember) newAssignTeamMemberID = (int)serviceInformationBasicInformationGatewayForEdit.GetAssignTeamMemberId(serviceId);
                        newThirdPartyVendor = serviceInformationBasicInformationGatewayForEdit.GetAssignedThirdPartyVendor(serviceId);
                    }

                    if (pnlAssignedTo.Visible)
                    {
                        if (serviceInformationBasicInformationGatewayForEdit.GetAssignmentDateTimeOriginal(serviceId).HasValue) newAssignmentDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetAssignmentDateTimeOriginal(serviceId);
                        if (tkrdpAssignmentDataAssignedDeadlineDate.SelectedDate.HasValue) newAssignmentDeadlineDate = tkrdpAssignmentDataAssignedDeadlineDate.SelectedDate.Value;
                        newToTeamMember = rbtnAssignmentDataToTeamMember.Checked;
                        if (newToTeamMember) newAssignTeamMemberID = Int32.Parse(ddlAssignmentDataAssignToTeamMember.SelectedValue);
                        newThirdPartyVendor = tbxAssignmentDataAssignToThirdPartyVendor.Text.Trim();
                    }

                    if (serviceInformationBasicInformationGatewayForEdit.GetAcceptedDateTime(serviceId).HasValue) assignmentAcceptedDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetAcceptedDateTime(serviceId);
                    if (serviceInformationBasicInformationGatewayForEdit.GetRejectedDateTime(serviceId).HasValue) assignmentRejectedDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetRejectedDateTime(serviceId);
                    newAssignmentRejectedReason = tbxAssignmentDataRejectedReason.Text.Trim();
                }
                else
                {
                    if (pnlAssignmentDataReadOnly.Visible)
                    {
                        if (serviceInformationBasicInformationGatewayForEdit.GetAssignmentDateTimeOriginal(serviceId).HasValue) newAssignmentDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetAssignmentDateTimeOriginal(serviceId);
                        if (serviceInformationBasicInformationGatewayForEdit.GetAssignedDeadlineDate(serviceId).HasValue) newAssignmentDeadlineDate = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetAssignedDeadlineDate(serviceId);
                        newToTeamMember = serviceInformationBasicInformationGatewayForEdit.GetToTeamMember(serviceId);
                        if (newToTeamMember) newAssignTeamMemberID = (int)serviceInformationBasicInformationGatewayForEdit.GetAssignTeamMemberId(serviceId);
                        newThirdPartyVendor = serviceInformationBasicInformationGatewayForEdit.GetAssignedThirdPartyVendor(serviceId);
                        if (serviceInformationBasicInformationGatewayForEdit.GetAcceptedDateTime(serviceId).HasValue) assignmentAcceptedDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetAcceptedDateTime(serviceId);
                        if (serviceInformationBasicInformationGatewayForEdit.GetRejectedDateTime(serviceId).HasValue) assignmentRejectedDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetRejectedDateTime(serviceId);
                        newAssignmentRejectedReason = tbxAssignmentDataRejectedReasonReadOnly.Text.Trim();
                    }
                }

                // ... Get start work data
                DateTime? startWorkDateTime = null; if (serviceInformationBasicInformationGatewayForEdit.GetStartWorkDateTime(serviceId).HasValue) startWorkDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetStartWorkDateTime(serviceId);
                DateTime? newUnitOutOfServiceDate = null;
                string newUnitOutOfServiceTime = "";
                string newStartWorkMileage = "";

                if (pnlStartWorkData.Visible)
                {
                    if (tkrdpStartWorkDataUnitOutOfServiceDate.SelectedDate.HasValue) newUnitOutOfServiceDate = tkrdpStartWorkDataUnitOutOfServiceDate.SelectedDate.Value;
                    newUnitOutOfServiceTime = "8:00 AM";
                    newStartWorkMileage = tbxStartWorkDataStartMileage.Text.Trim();
                }
                else
                {
                    if (pnlStartWorkDataReadOnly.Visible)
                    {
                        if (tbxStartWorkDataUnitOutOfServiceDateReadOnly.Text.Trim() != "") newUnitOutOfServiceDate = DateTime.Parse(tbxStartWorkDataUnitOutOfServiceDateReadOnly.Text.Trim());
                        newUnitOutOfServiceTime = "8:00 AM";
                        newStartWorkMileage = tbxStartWorkDataStartMileageReadOnly.Text.Trim();
                    }
                }

                // ... Get complete work data
                DateTime? completeWorkDateTime = null; if (serviceInformationBasicInformationGatewayForEdit.GetCompleteWorkDateTime(serviceId).HasValue) completeWorkDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetCompleteWorkDateTime(serviceId);
                DateTime? newUnitBackInServiceDate = null;
                string newUnitBackInServiceTime = "";
                string newCompleteWorkMileage = "";
                string newCompleteWorkDetailDescription = "";
                bool newCompleteWorkDetailPreventable = false;
                decimal? newCompleteWorkDetailTMLabourHours = 0.0M;
                string newCompleteWorkDetailTPVInvoiceNumber = "";
                decimal? newCompleteWorkDetailTPVInvoiceAmout = 0.0M;

                if (pnlCompleteWorkData.Visible)
                {
                    if (tkrdpCompleteWorkDataUnitBackInServiceDate.SelectedDate.HasValue) newUnitBackInServiceDate = tkrdpCompleteWorkDataUnitBackInServiceDate.SelectedDate.Value;
                    newUnitBackInServiceTime = "8:00 AM";
                    newCompleteWorkMileage = tbxCompleteWorkDataCompleteMileage.Text.Trim();

                    // ... ... From team member
                    if (pnlTeamMemberAssigned.Visible)
                    {
                        if (tbxCompleteWorkDataDescription.Text.Trim() != "") newCompleteWorkDetailDescription = tbxCompleteWorkDataDescription.Text.Trim();
                        newCompleteWorkDetailPreventable = ckbxCompleteWorkDataPreventable.Checked;
                    }
                    newCompleteWorkDetailTMLabourHours = null; if (tbxCompleteWorkDataLabourHours.Text.Trim() != "") newCompleteWorkDetailTMLabourHours = decimal.Parse(tbxCompleteWorkDataLabourHours.Text.Trim());

                    // ... ... From Third party vendor
                    if (pnlThirdPartyVendorAssigned.Visible)
                    {
                        if (tbxCompleteWorkDataDescriptionThirdPartyVendor.Text.Trim() != "") newCompleteWorkDetailDescription = tbxCompleteWorkDataDescriptionThirdPartyVendor.Text.Trim();
                        newCompleteWorkDetailPreventable = ckbxCompleteWorkDataPreventableThirdPartyVendor.Checked;
                    }
                    newCompleteWorkDetailTPVInvoiceNumber = tbxCompleteWorkDataInvoiceNumberThirdPartyVendor.Text.Trim();
                    newCompleteWorkDetailTPVInvoiceAmout = null; if (tbxCompleteWorkDataInvoiceAmountThirdPartyVendor.Text.Trim() != "") newCompleteWorkDetailTPVInvoiceAmout = decimal.Parse(tbxCompleteWorkDataInvoiceAmountThirdPartyVendor.Text.Trim());
                }
                else
                {
                    if (pnlCompleteWorkDataReadOnly.Visible)
                    {
                        if (tbxCompleteWorkDataUnitBackInServiceDateReadOnly.Text.Trim() != "") newUnitBackInServiceDate = DateTime.Parse(tbxCompleteWorkDataUnitBackInServiceDateReadOnly.Text.Trim());
                        newUnitBackInServiceTime = "8:00 AM";
                        newCompleteWorkMileage = tbxCompleteWorkDataCompleteMileageReadOnly.Text.Trim();

                        // ... ... From team member
                        if (pnlTeamMemberAssignedReadOnly.Visible)
                        {
                            if (tbxCompleteWorkDataDescriptionReadOnly.Text.Trim() != "") newCompleteWorkDetailDescription = tbxCompleteWorkDataDescriptionReadOnly.Text.Trim();
                            newCompleteWorkDetailPreventable = ckbxCompleteWorkDataPreventableReadOnly.Checked;
                        }
                        newCompleteWorkDetailTMLabourHours = null; if (tbxCompleteWorkDataLabourHoursReadOnly.Text.Trim() != "") newCompleteWorkDetailTMLabourHours = decimal.Parse(tbxCompleteWorkDataLabourHoursReadOnly.Text.Trim());

                        // ... ... From Third party vendor
                        if (pnlThirdPartyVendorAssignedReadOnly.Visible)
                        {
                            if (tbxCompleteWorkDataDescriptionThirdPartyVendorReadOnly.Text.Trim() != "") newCompleteWorkDetailDescription = tbxCompleteWorkDataDescriptionThirdPartyVendorReadOnly.Text.Trim();
                            newCompleteWorkDetailPreventable = ckbxCompleteWorkDataPreventableThirdPartyVendorReadOnly.Checked;
                        }
                        newCompleteWorkDetailTPVInvoiceNumber = tbxCompleteWorkDataInvoiceNumberThirdPartyVendorReadOnly.Text.Trim();
                        newCompleteWorkDetailTPVInvoiceAmout = null; if (tbxCompleteWorkDataInvoiceAmountThirdPartyVendorReadOnly.Text.Trim() != "") newCompleteWorkDetailTPVInvoiceAmout = decimal.Parse(tbxCompleteWorkDataInvoiceAmountThirdPartyVendorReadOnly.Text.Trim());
                    }
                }

                // Update service data
                ServiceInformationBasicInformation serviceInformationBasicInformation = new ServiceInformationBasicInformation(serviceInformationTDS);
                int? libraryCategoriesId = null; if (serviceInformationBasicInformationGatewayForEdit.GetLibraryCategoriesId(int.Parse(hdfServiceId.Value)).HasValue) libraryCategoriesId = (int)serviceInformationBasicInformationGatewayForEdit.GetLibraryCategoriesId(int.Parse(hdfServiceId.Value));
                serviceInformationBasicInformation.Update(serviceId, serviceState, newMtoDto, newServiceDescription, newMileage, newAssignmentDateTime, newAssignmentDeadlineDate, newToTeamMember, newAssignTeamMemberID, newThirdPartyVendor, assignmentAcceptedDateTime, assignmentRejectedDateTime, newAssignmentRejectedReason, startWorkDateTime, newUnitOutOfServiceDate, newUnitOutOfServiceTime, newStartWorkMileage, completeWorkDateTime, newUnitBackInServiceDate, newUnitBackInServiceTime, newCompleteWorkMileage, newCompleteWorkDetailDescription, newCompleteWorkDetailPreventable, newCompleteWorkDetailTMLabourHours, newCompleteWorkDetailTPVInvoiceNumber, newCompleteWorkDetailTPVInvoiceAmout, associatedChecklistRuleState, libraryCategoriesId);

                // Store datasets
                Session["serviceInformationTDS"] = serviceInformationTDS;

                Session.Remove("libraryTDSForServices");

                // Update database
                UpdateDatabase();

                ViewState["update"] = "yes";

                // Redirect
                string url = "";
                if (Request.QueryString["source_page"] == "services_navigator2.aspx" || Request.QueryString["source_page"] == "services_add_request.aspx")
                {
                    url = "./services_navigator2.aspx?source_page=services_edit.aspx&service_id=" + hdfServiceId.Value + GetNavigatorState() + "&update=yes";
                }

                if (Request.QueryString["source_page"] == "services_summary.aspx")
                {
                    string activeTab = hdfActiveTab.Value;
                    url = "./services_summary.aspx?source_page=services_edit.aspx&dashboard=" + hdfDashboard.Value + "&service_id=" + hdfServiceId.Value + "&active_tab=" + activeTab + GetNavigatorState() + "&update=yes";
                }

                Response.Redirect(url);
            }
        }
        private void LoadDetailedData(int serviceId)
        {
            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
            if (serviceInformationBasicInformationGateway.Table.Rows.Count > 0)
            {
                // Load for General Tab
                tbxGeneralCreatedBy.Text = serviceInformationBasicInformationGateway.GetCreatedBy(serviceId);
                tbxGeneralMileage.Text = serviceInformationBasicInformationGateway.GetMileage(serviceId);
                lblGeneralMileageUnitOfMeasurement.Text = serviceInformationBasicInformationGateway.GetMileageUnitOfMeasurement(serviceId);

                // Load for Assginment Tab
                LoadDetailaDataAssignmentData(serviceId, serviceInformationBasicInformationGateway);

                // Load for Start Work Tab
                LoadDetailaDataStartWork(serviceId, serviceInformationBasicInformationGateway);

                // Load for Complete Work Tab
                LoadDetailaDataCompleteWork(serviceId, serviceInformationBasicInformationGateway);
            }
        }
        private void LoadDetailaDataCompleteWork(int serviceId, ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway)
        {
            // Load for CompleteWork Tab
            // ... pnlCompleteWork
            tbxCompleteWorkDataCompleteWorkDateTime.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDateTime(serviceId).ToString();

            if (serviceInformationBasicInformationGateway.GetUnitBackInServiceDate(serviceId).HasValue)
            {
                DateTime backInServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitBackInServiceDate(serviceId);
                tkrdpCompleteWorkDataUnitBackInServiceDate.SelectedDate = DateTime.Parse(backInServiceDate.Month.ToString() + "/" + backInServiceDate.Day.ToString() + "/" + backInServiceDate.Year.ToString());
            }

            tbxCompleteWorkDataCompleteMileage.Text = serviceInformationBasicInformationGateway.GetCompleteWorkMileage(serviceId);
            lblCompleteWorkDataMileageUnitOfMeasurement.Text = serviceInformationBasicInformationGateway.GetMileageUnitOfMeasurement(serviceId);
            lblCompleteWorkDataMileageUnitOfMeasurementReadOnly.Text = serviceInformationBasicInformationGateway.GetMileageUnitOfMeasurement(serviceId);

            // ... For team member
            tbxCompleteWorkDataDescription.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailDescription(serviceId);
            ckbxCompleteWorkDataPreventable.Checked = serviceInformationBasicInformationGateway.GetCompleteWorkDetailPreventable(serviceId);
            tbxCompleteWorkDataLabourHours.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMLabourHours(serviceId).ToString();

            // ... For third party vendor
            tbxCompleteWorkDataDescriptionThirdPartyVendor.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailDescription(serviceId);
            ckbxCompleteWorkDataPreventableThirdPartyVendor.Checked = serviceInformationBasicInformationGateway.GetCompleteWorkDetailPreventable(serviceId);
            tbxCompleteWorkDataInvoiceNumberThirdPartyVendor.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceNumber(serviceId);
            tbxCompleteWorkDataInvoiceAmountThirdPartyVendor.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceAmout(serviceId).ToString();

            // ... PnlCompleteWorkReadOnly
            tbxCompleteWorkDataCompleteWorkDateTimeReadOnly.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDateTime(serviceId).ToString();

            tbxCompleteWorkDataUnitBackInServiceDateReadOnly.Text = "";
            if (serviceInformationBasicInformationGateway.GetUnitBackInServiceDate(serviceId).HasValue)
            {
                DateTime backInServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitBackInServiceDate(serviceId);
                tbxCompleteWorkDataUnitBackInServiceDateReadOnly.Text = backInServiceDate.Month.ToString() + "/" + backInServiceDate.Day.ToString() + "/" + backInServiceDate.Year.ToString();
            }

            tbxCompleteWorkDataCompleteMileageReadOnly.Text = serviceInformationBasicInformationGateway.GetCompleteWorkMileage(serviceId);

            // ... For team member
            tbxCompleteWorkDataDescriptionReadOnly.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailDescription(serviceId);
            ckbxCompleteWorkDataPreventableReadOnly.Checked = serviceInformationBasicInformationGateway.GetCompleteWorkDetailPreventable(serviceId);
            tbxCompleteWorkDataLabourHoursReadOnly.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMLabourHours(serviceId).ToString();

            // ... For third party vendor
            tbxCompleteWorkDataDescriptionThirdPartyVendorReadOnly.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailDescription(serviceId);
            ckbxCompleteWorkDataPreventableThirdPartyVendorReadOnly.Checked = serviceInformationBasicInformationGateway.GetCompleteWorkDetailPreventable(serviceId);
            tbxCompleteWorkDataInvoiceNumberThirdPartyVendorReadOnly.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceNumber(serviceId);
            tbxCompleteWorkDataInvoiceAmountThirdPartyVendorReadOnly.Text = serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceAmout(serviceId).ToString();
        }
        private void SendMailFleetManagerAcceptRejectCompleted(string subject, string title)
        {
            // Get mail information
            string mailTo = "";
            string nameTo = "";
            string body = "";
            int serviceId = Int32.Parse(hdfServiceId.Value);
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int companyLevel = Int32.Parse(hdfCompanyLevel.Value);

            // MailtTo, nameTo
            int companyLevelId = Int32.Parse(hdfCompanyLevel.Value);

            Employee employees = new Employee();
            employees.LoadByFleetManager(companyLevelId);

            mailTo = employees.GetAllFleetManagersEMails();
            nameTo = employees.GetAllFleetManagersNames();

            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
            serviceInformationBasicInformationGateway.LoadByServiceId(serviceId, companyId);

            // Mails body
            body = body + "\nHi " + nameTo + ",\n\n" + title + " \n";
            body = body + "\n Service: " + serviceInformationBasicInformationGateway.GetServiceNumber(serviceId) + " - " + serviceInformationBasicInformationGateway.GetServiceDescription(serviceId);
            body = body + "\n Unit: " + serviceInformationBasicInformationGateway.GetUnitCode(serviceId) + " - " + serviceInformationBasicInformationGateway.GetUnitDescription(serviceId);
            body = body + "\n Fixed Date: ";
            if (serviceInformationBasicInformationGateway.GetMtoDto(serviceId)) body = body + "Yes "; else body = body + "No ";

            string unitType = hdfUnitType.Value;
            if (unitType == "Vehicle")
            {
                body = body + "\n Mileage: " + serviceInformationBasicInformationGateway.GetMileage(serviceId) + " " + hdfMileageUnitOfMeasurement.Value;
            }

            body = body + "\n Problem Description: " + serviceInformationBasicInformationGateway.GetServiceDescription(serviceId);

            if (serviceInformationBasicInformationGateway.GetAssignedDeadlineDate(serviceId).HasValue)
            {
                DateTime deadlineDate = (DateTime)serviceInformationBasicInformationGateway.GetAssignedDeadlineDate(serviceId);
                string deadlineDateText = deadlineDate.Month.ToString() + "/" + deadlineDate.Day.ToString() + "/" + deadlineDate.Year.ToString();
                body = body + "\n Deadline Date: " + deadlineDateText;
            }

            // Asignation
            if (serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId).HasValue)
            {
                int employeeId = (int)serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId);
                EmployeeGateway employeesGateway = new EmployeeGateway();
                employeesGateway.LoadByEmployeeId(employeeId);

                string assignedTo = "";
                if (employeesGateway.Table.Rows.Count > 0)
                {
                    // Assigned TeamMember
                    assignedTo = employeesGateway.GetFirstName(employeeId) + " " + employeesGateway.GetLastName(employeeId);
                }

                body = body + "\n Assigned Team Member: " + assignedTo;
            }

            // ... Start work information
            if (serviceInformationBasicInformationGateway.GetStartWorkDateTime(serviceId).HasValue)
            {
                DateTime startWorkDate = (DateTime)serviceInformationBasicInformationGateway.GetStartWorkDateTime(serviceId);
                body = body + "\n Start Work Date & Time: " + startWorkDate;
            }

            // ... Complete work information
            if (serviceInformationBasicInformationGateway.GetCompleteWorkDateTime(serviceId).HasValue)
            {
                DateTime completeWorkDate = (DateTime)serviceInformationBasicInformationGateway.GetCompleteWorkDateTime(serviceId);
                body = body + "\n Complete Work Date & Time: " + completeWorkDate;
            }

            //Send Mail
            SendMail(mailTo, subject, body);
        }
        private void SendMailFleetManager( string subject, string title)
        {
            // Get mail information
            string mailTo = "";
            string nameTo = "";
            string body = "";
            int serviceId = Int32.Parse(hdfServiceId.Value);
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int companyLevel = Int32.Parse(hdfCompanyLevel.Value);

            // MailtTo, nameTo
            int companyLevelId = Int32.Parse(hdfCompanyLevel.Value);

            Employee employees = new Employee();
            employees.LoadByFleetManager(companyLevelId);

            mailTo = employees.GetAllFleetManagersEMails();
            nameTo = employees.GetAllFleetManagersNames();

            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
            serviceInformationBasicInformationGateway.LoadByServiceId(serviceId, companyId);

            // Mails body
            body = body + "\nHi " + nameTo + ",\n\n"+ title + " \n";
            body = body + "\n Unit: " + serviceInformationBasicInformationGateway.GetUnitCode(serviceId) + " - " + serviceInformationBasicInformationGateway.GetUnitDescription(serviceId);
            body = body + "\n Fixed Date: ";
            if (serviceInformationBasicInformationGateway.GetMtoDto(serviceId)) body = body + "Yes "; else body = body + "No ";

            string unitType = hdfUnitType.Value;
            if (unitType == "Vehicle")
            {
                body = body + "\n Mileage: " + serviceInformationBasicInformationGateway.GetMileage(serviceId) + " " + hdfMileageUnitOfMeasurement.Value;
            }

            body = body + "\n Problem Description: " + serviceInformationBasicInformationGateway.GetServiceDescription(serviceId);

            if (tkrdpPnlAssignDeadlineDate.SelectedDate.HasValue)
            {
                DateTime deadlineDate = tkrdpPnlAssignDeadlineDate.SelectedDate.Value;
                string deadlineDateText = deadlineDate.Month.ToString() + "/" + deadlineDate.Day.ToString() + "/" + deadlineDate.Year.ToString();
                body = body + " \n Deadline date: " + deadlineDateText;
            }
            else
            {
                body = body + " \n Deadline date: ";
            }

            if(rbtnPnlAssignToTeamMember.Checked)
            {
                int employeeId = Int32.Parse(ddlPnlAssignAssignToTeamMember.SelectedValue);
                EmployeeGateway employeesGateway = new EmployeeGateway();
                employeesGateway.LoadForMailsByEmployeeId(employeeId);
                string assignedTo = "";
                if (employeesGateway.Table.Rows.Count > 0)
                {
                    // Assigned TeamMember
                    assignedTo = employeesGateway.GetFirstName(employeeId) + " " + employeesGateway.GetLastName(employeeId);
                }

                body = body + "\n Assigned Team Member: " + assignedTo;
            }

            if (rbtnPnlAssignToThirdPartyVendor.Checked)
            {
                body = body + "\n Assigned Third Party Vendor: " + tbxPnlAssignAssignToThirdPartyVendor.Text;
            }

            //Send Mail
            SendMail(mailTo, subject, body);
        }
        private void LoadMileage()
        {
            int serviceId = Int32.Parse(hdfServiceId.Value);

            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGatewayForId = new ServiceInformationBasicInformationGateway(serviceInformationTDS);

            // Operation check
            switch ((string)Request.QueryString["state"])
            {
                case "StartWork":
                    string generalMileage = serviceInformationBasicInformationGatewayForId.GetMileage(serviceId);

                    if (tbxPnlStartWorkStartMileage.Text == "" && generalMileage != "")
                    {
                        tbxPnlStartWorkStartMileage.Text = generalMileage;
                    }
                    break;

                case "CompleteWork":
                    string startWorkMileage = serviceInformationBasicInformationGatewayForId.GetStartWorkMileage(serviceId);

                    if (tbxPnlCompleteWorkCompleteMileage.Text == "" && startWorkMileage != "")
                    {
                        tbxPnlCompleteWorkCompleteMileage.Text = startWorkMileage;
                    }
                    break;
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_SERVICES_VIEW"]) && Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_SERVICES_EDIT"])))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                }

                // Validate query string
                if (((string)Request.QueryString["state"] == null) || ((string)Request.QueryString["source_page"] == null) || ((string)Request.QueryString["service_id"] == null) || ((string)Request.QueryString["active_tab"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in services_state.aspx");
                }

                // Tag page
                TagPage();

                // If coming from services_summary.aspx
                if ((string)Request.QueryString["source_page"] == "services_summary.aspx")
                {
                    // Store Navigator State and Update control
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];

                    // Get project record
                    int serviceId = Int32.Parse(hdfServiceId.Value);
                    int companyId = Int32.Parse(hdfCompanyId.Value);

                    serviceInformationTDS = (ServiceInformationTDS)Session["serviceInformationTDS"];
                    ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
                    serviceInformationBasicInformationGateway.LoadByServiceId(serviceId, companyId);

                    if (serviceInformationBasicInformationGateway.GetRuleId(serviceId).HasValue)
                    {
                        RuleGateway ruleGateway = new RuleGateway();
                        int? ruleId = serviceInformationBasicInformationGateway.GetRuleId(serviceId);
                        ruleGateway.LoadAllByRuleId(ruleId.Value, Int32.Parse(hdfCompanyId.Value));
                        int? serviceRequestDays = ruleGateway.GetServiceRequestDays(ruleId.Value);

                        if (serviceRequestDays.HasValue)
                        {
                            DateTime serviceRequestCreationDate = serviceInformationBasicInformationGateway.GetDateTime_(serviceId);
                            tkrdpPnlAssignDeadlineDate.SelectedDate = serviceRequestCreationDate.AddDays(Convert.ToDouble(serviceRequestDays.Value));
                        }
                    }

                    // Store datasets
                    Session["serviceInformationTDS"] = serviceInformationTDS;
                }

                // If coming from wucSRUnassigned.aspx
                if ((string)Request.QueryString["source_page"] == "wucSRUnassigned.ascx")
                {
                    // Store Navigator State and Update control
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];

                    // Get project record
                    int serviceId = Int32.Parse(hdfServiceId.Value);
                    int companyId = Int32.Parse(hdfCompanyId.Value);

                    serviceInformationTDS = new ServiceInformationTDS();
                    ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
                    serviceInformationBasicInformationGateway.LoadByServiceId(serviceId, companyId);

                    if (serviceInformationBasicInformationGateway.GetRuleId(serviceId).HasValue)
                    {
                        RuleGateway ruleGateway = new RuleGateway();
                        int? ruleId = serviceInformationBasicInformationGateway.GetRuleId(serviceId);
                        ruleGateway.LoadAllByRuleId(ruleId.Value, Int32.Parse(hdfCompanyId.Value));
                        int? serviceRequestDays = ruleGateway.GetServiceRequestDays(ruleId.Value);

                        if (serviceRequestDays.HasValue)
                        {
                            DateTime serviceRequestCreationDate = serviceInformationBasicInformationGateway.GetDateTime_(serviceId);
                            tkrdpPnlAssignDeadlineDate.SelectedDate = serviceRequestCreationDate.AddDays(Convert.ToDouble(serviceRequestDays.Value));
                        }
                    }

                    // Store datasets
                    Session["serviceInformationTDS"] = serviceInformationTDS;
                }

                // Restore dataset
                serviceInformationTDS = (ServiceInformationTDS)Session["serviceInformationTDS"];

                LoadMileage();
            }
            else
            {
                // Restore dataset
                serviceInformationTDS = (ServiceInformationTDS)Session["serviceInformationTDS"];
            }
        }
        private bool IsDeletedSR(int serviceId, int companyId)
        {
            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway();
            serviceInformationBasicInformationGateway.LoadByServiceId(serviceId, companyId);

            if (serviceInformationBasicInformationGateway.Table.Rows.Count > 0)
            {
                int loginId = Convert.ToInt32(Session["loginID"]);
                bool serviceAdmin = Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_SERVICES_ADMIN"]);
                EmployeeGateway employeeGateway = new EmployeeGateway(new DataSet());
                int employeeId = employeeGateway.GetEmployeIdByLoginId(loginId);
                int ownerId = serviceInformationBasicInformationGateway.GetOwnerID(serviceId);

                if (!(serviceInformationBasicInformationGateway.GetRuleId(serviceId).HasValue) && ((employeeId == ownerId) || (serviceAdmin)))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        private void UpdateDatabase()
        {
            // Get ids
            int serviceId = Int32.Parse(hdfServiceId.Value);
            string serviceType = hdfFmType.Value;
            int companyId = Int32.Parse(hdfCompanyId.Value);

            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGatewayForId = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
            int? unitId = serviceInformationBasicInformationGatewayForId.GetUnitID(serviceId);

            UnitsGateway unitsGateway = new UnitsGateway();
            unitsGateway.LoadByUnitId((int)unitId, companyId);
            string unitType = unitsGateway.GetType((int)unitId);

            // Delete
            DB.Open();
            DB.BeginTransaction();
            try
            {
                ServiceInformationBasicInformation serviceInformationBasicInformation = new ServiceInformationBasicInformation(serviceInformationTDS);
                serviceInformationBasicInformation.DeleteDirect(serviceId, unitType, (int)unitId, companyId);

                DB.CommitTransaction();
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        private void LoadNotes(int companyId)
        {
            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);

            int? libraryCategoriesId = null; if (serviceInformationBasicInformationGateway.GetLibraryCategoriesId(int.Parse(hdfServiceId.Value)).HasValue) libraryCategoriesId = (int)serviceInformationBasicInformationGateway.GetLibraryCategoriesId(int.Parse(hdfServiceId.Value));

            if (libraryCategoriesId.HasValue)
            {
                ViewState["libraryCategoriesId"] = (int)libraryCategoriesId;
                tbxCategoryAssocited.Text = GetFullCategoryName((int)libraryCategoriesId, companyId);
            }
            else
            {
                tbxCategoryAssocited.Text = "";
            }
        }
        private void LoadBasicData(int serviceId)
        {
            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
            if (serviceInformationBasicInformationGateway.Table.Rows.Count > 0)
            {
                // Load service basic data
                tbxServiceState.Text = serviceInformationBasicInformationGateway.GetServiceState(serviceId);
                tbxServiceNumber.Text = serviceInformationBasicInformationGateway.GetServiceNumber(serviceId);
                tbxDateTime.Text = serviceInformationBasicInformationGateway.GetDateTime_(serviceId).ToString();
                ckbxMtoDto.Checked = serviceInformationBasicInformationGateway.GetMtoDto(serviceId);
                tbxServiceDescription.Text = serviceInformationBasicInformationGateway.GetServiceDescription(serviceId);

                // Load unit basic data
                tbxUnitCode.Text = serviceInformationBasicInformationGateway.GetUnitCode(serviceId);
                tbxUnitDescription.Text = serviceInformationBasicInformationGateway.GetUnitDescription(serviceId);
                tbxVinSn.Text = serviceInformationBasicInformationGateway.GetVinSn(serviceId);
                tbxUnitState.Text = serviceInformationBasicInformationGateway.GetUnitState(serviceId);

                // Load checklist data
                tbxAssociatedChecklistRule.Text = serviceInformationBasicInformationGateway.GetAssociatedChecklistRule(serviceId);
                tbxChecklistState.Text = serviceInformationBasicInformationGateway.GetAssociatedChecklistRuleState(serviceId);

                if (serviceInformationBasicInformationGateway.GetRuleId(serviceId).HasValue)
                {
                    RuleGateway ruleGateway = new RuleGateway();
                    int? ruleId = serviceInformationBasicInformationGateway.GetRuleId(serviceId);
                    ruleGateway.LoadAllByRuleId(ruleId.Value, Int32.Parse(hdfCompanyId.Value));
                    int? serviceRequestDays = ruleGateway.GetServiceRequestDays(ruleId.Value);

                    if (ruleGateway.GetMto(ruleId.Value) && serviceInformationBasicInformationGateway.GetAssociatedChecklistLastService(serviceId).HasValue)
                    {
                        tbxChecklistNextDueDate.Text = serviceInformationBasicInformationGateway.GetAssociatedChecklistLastService(serviceId).Value.ToShortDateString();
                    }
                    else
                    {
                        if (serviceRequestDays.HasValue)
                        {
                            int negValue = -1;
                            serviceRequestDays = serviceRequestDays.Value * negValue;
                            DateTime serviceRequestCreationDate = serviceInformationBasicInformationGateway.GetDateTime_(serviceId);
                            tbxChecklistNextDueDate.Text = serviceInformationBasicInformationGateway.GetAssociatedChecklistNextDue(serviceId).Value.AddDays(Convert.ToDouble(serviceRequestDays.Value)).ToShortDateString();
                        }
                    }
                }
            }
        }
        private void LoadDetailaDataAssignmentData(int serviceId, ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway)
        {
            // Load for Assignment Tab
            // ... pnlAssignmentData
            tbxAssignmentDataAssignmentDateTime.Text = serviceInformationBasicInformationGateway.GetAssignmentDateTime(serviceId).ToString();

            if (serviceInformationBasicInformationGateway.GetAssignedDeadlineDate(serviceId).HasValue)
            {
                DateTime deadlineDate = (DateTime)serviceInformationBasicInformationGateway.GetAssignedDeadlineDate(serviceId);
                tkrdpAssignmentDataAssignedDeadlineDate.SelectedDate = DateTime.Parse(deadlineDate.Month.ToString() + "/" + deadlineDate.Day.ToString() + "/" + deadlineDate.Year.ToString());
            }

            if ((serviceInformationBasicInformationGateway.GetToTeamMember(serviceId)) && (serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId)) == "")
            {
                // ... For team member
                rbtnAssignmentDataToTeamMember.Checked = true;
                rbtnAssignmentDataToTeamMemberReadOnly.Checked = true;

                // ... For third party vendor
                rbtnAssignmentDataToThirdPartyVendor.Checked = false;
                rbtnAssignmentDataToThirdPartyVendorReadOnly.Checked = false;
            }
            else
            {
                if ((!serviceInformationBasicInformationGateway.GetToTeamMember(serviceId)) && (serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId)) != "")
                {
                    // ... For team member
                    rbtnAssignmentDataToTeamMember.Checked = false;
                    rbtnAssignmentDataToTeamMemberReadOnly.Checked = false;

                    // ... For third party vendor
                    rbtnAssignmentDataToThirdPartyVendor.Checked = true;
                    rbtnAssignmentDataToThirdPartyVendorReadOnly.Checked = true;

                    tbxAssignmentDataAssignToThirdPartyVendor.Text = serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId);
                    tbxAssignmentDataAssignToThirdPartyVendorReadOnly.Text = serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId);
                }
                else
                {
                    rbtnAssignmentDataToTeamMember.Checked = false;
                    rbtnAssignmentDataToTeamMemberReadOnly.Checked = false;
                    rbtnAssignmentDataToThirdPartyVendor.Checked = false;
                    rbtnAssignmentDataToThirdPartyVendorReadOnly.Checked = false;
                }
            }

            tbxAssignmentDataAcceptedDateTime.Text = serviceInformationBasicInformationGateway.GetAcceptedDateTime(serviceId).ToString();

            DateTime? acceptedDateTime = serviceInformationBasicInformationGateway.GetAcceptedDateTime(serviceId);
            if (acceptedDateTime.HasValue) tbxAssignmentDataAcceptedDateTime.Text = acceptedDateTime.ToString();

            DateTime? rejectedDateTime = serviceInformationBasicInformationGateway.GetRejectedDateTime(serviceId);
            if (rejectedDateTime.HasValue) tbxAssignmentDataRejectedDateTime.Text = rejectedDateTime.ToString();
            tbxAssignmentDataRejectedReason.Text = serviceInformationBasicInformationGateway.GetRejectedReason(serviceId);

            // ... pnlAssignmentDataReadOnly
            tbxAssignmentDataAssignmentDateTimeReadOnly.Text = serviceInformationBasicInformationGateway.GetAssignmentDateTime(serviceId).ToString();

            tbxAssignmentDataAssignedDeadlineDateReadOnly.Text = "";
            if (serviceInformationBasicInformationGateway.GetAssignedDeadlineDate(serviceId).HasValue)
            {
                DateTime deadlineDate = (DateTime)serviceInformationBasicInformationGateway.GetAssignedDeadlineDate(serviceId);
                tbxAssignmentDataAssignedDeadlineDateReadOnly.Text = deadlineDate.Month.ToString() + "/" + deadlineDate.Day.ToString() + "/" + deadlineDate.Year.ToString();
            }

            if ((serviceInformationBasicInformationGateway.GetToTeamMember(serviceId)) && (serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId)) == "")
            {
                // ... For team member
                rbtnAssignmentDataReadOnlyToTeamMemberReadOnly.Checked = true;

                if (serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId).HasValue)
                {
                    int teamMemberId = (int)serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId);
                    EmployeeGateway employeeGateway = new EmployeeGateway();
                    employeeGateway.LoadByEmployeeId(teamMemberId);
                    tbxAssignmentDataReadOnlyAssignToTeamMemberReadOnly.Text = employeeGateway.GetLastName(teamMemberId) + " " + employeeGateway.GetFirstName(teamMemberId);
                }

                // ... For third party vendor
                rbtnAssignmentDataReadOnlyToThirdPartyVendorReadOnly.Checked = false;
            }
            else
            {
                if ((!serviceInformationBasicInformationGateway.GetToTeamMember(serviceId)) && (serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId)) != "")
                {
                    // ... For team member
                    rbtnAssignmentDataReadOnlyToTeamMemberReadOnly.Checked = false;

                    // ... For third party vendor
                    rbtnAssignmentDataReadOnlyToThirdPartyVendorReadOnly.Checked = true;

                    tbxAssignmentDataReadOnlyAssignToThirdPartyVendorReadOnly.Text = serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId);
                }
                else
                {
                    rbtnAssignmentDataReadOnlyToTeamMemberReadOnly.Checked = false;
                    rbtnAssignmentDataReadOnlyToThirdPartyVendorReadOnly.Checked = false;
                }
            }

            tbxAssignmentDataAcceptedDateTimeReadOnly.Text = serviceInformationBasicInformationGateway.GetAcceptedDateTime(serviceId).ToString();

            DateTime? acceptedDateTimeReadOnly = serviceInformationBasicInformationGateway.GetAcceptedDateTime(serviceId);
            if (acceptedDateTimeReadOnly.HasValue) tbxAssignmentDataAcceptedDateTimeReadOnly.Text = acceptedDateTimeReadOnly.ToString();

            DateTime? rejectedDateTimeReadOnly = serviceInformationBasicInformationGateway.GetRejectedDateTime(serviceId);
            if (rejectedDateTimeReadOnly.HasValue) tbxAssignmentDataRejectedDateTimeReadOnly.Text = rejectedDateTimeReadOnly.ToString();
            tbxAssignmentDataRejectedReasonReadOnly.Text = serviceInformationBasicInformationGateway.GetRejectedReason(serviceId);
        }
        private void SendMailTeamMember()
        {
            // Get mail information
            string mailTo = "";
            string nameTo = "";
            string subject = "You have assigned service requests.";
            string body = "";

            int employeeId = Int32.Parse(ddlPnlAssignAssignToTeamMember.SelectedValue);
            EmployeeGateway employeesGateway = new EmployeeGateway();
            employeesGateway.LoadForMailsByEmployeeId(employeeId);

            if (employeesGateway.Table.Rows.Count > 0)
            {
                // Assigned TeamMember
                mailTo = employeesGateway.GetEMail(employeeId);
                nameTo = employeesGateway.GetFirstName(employeeId) + " " + employeesGateway.GetLastName(employeeId);
            }

            int serviceId = Int32.Parse(hdfServiceId.Value);
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int companyLevel = Int32.Parse(hdfCompanyLevel.Value);

            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
            serviceInformationBasicInformationGateway.LoadByServiceId(serviceId, companyId);

            // Mails body
            body = body + "\nHi " + nameTo + ",\n\nThe following service request has been assigned to you. \n";
            body = body + "\n Unit: " + serviceInformationBasicInformationGateway.GetUnitCode(serviceId) + " - " + serviceInformationBasicInformationGateway.GetUnitDescription(serviceId) + "\n";
            body = body + "\n Fixed Date: ";
            if (serviceInformationBasicInformationGateway.GetMtoDto(serviceId)) body = body + "Yes "; else body = body + "No ";

            string unitType = hdfUnitType.Value;
            if (unitType == "Vehicle")
            {
                body = body + "\n Mileage: " + serviceInformationBasicInformationGateway.GetMileage(serviceId) + " " + hdfMileageUnitOfMeasurement.Value;
            }

            body = body + "\n Problem Description: " + serviceInformationBasicInformationGateway.GetServiceDescription(serviceId);

            if (tkrdpPnlAssignDeadlineDate.SelectedDate.HasValue)
            {
                DateTime deadlineDate = tkrdpPnlAssignDeadlineDate.SelectedDate.Value;
                string deadlineDateText = deadlineDate.Month.ToString() + "/" + deadlineDate.Day.ToString() + "/" + deadlineDate.Year.ToString();
                body = body + " \n Deadline date: " + deadlineDateText;
            }
            else
            {
                body = body + " \n Deadline date: ";
            }

            int registeredByLoginId = Convert.ToInt32(Session["loginID"]);
            employeesGateway.LoadByLoginId(registeredByLoginId);
            int registeredByEmployeeId = employeesGateway.GetEmployeIdByLoginId(registeredByLoginId);
            if (employeesGateway.Table.Rows.Count > 0)
            {
                body = body + "\n Assigned By: " + employeesGateway.GetFirstName(registeredByEmployeeId) + " " + employeesGateway.GetLastName(registeredByEmployeeId);
            }

            //Send Mail
            SendMail(mailTo, subject, body);
        }
        private void LoadDetailaDataStartWork(int serviceId, ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway)
        {
            // Load for StartWork Tab
            // ... pnlStartWork
            tbxStartWorkDataWorkStartDateTime.Text = serviceInformationBasicInformationGateway.GetStartWorkDateTime(serviceId).ToString();

            if (serviceInformationBasicInformationGateway.GetUnitOutOfServiceDate(serviceId).HasValue)
            {
                DateTime outOfServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitOutOfServiceDate(serviceId);
                tkrdpStartWorkDataUnitOutOfServiceDate.SelectedDate = DateTime.Parse(outOfServiceDate.Month.ToString() + "/" + outOfServiceDate.Day.ToString() + "/" + outOfServiceDate.Year.ToString());
            }

            tbxStartWorkDataStartMileage.Text = serviceInformationBasicInformationGateway.GetStartWorkMileage(serviceId);
            lblStartWorkDataMileageUnitOfMeasurement.Text = serviceInformationBasicInformationGateway.GetMileageUnitOfMeasurement(serviceId);
            lblStartWorkDataMileageUnitOfMeasurementReadOnly.Text = serviceInformationBasicInformationGateway.GetMileageUnitOfMeasurement(serviceId);

            // ... pnlStartWorkReadOnly
            tbxStartWorkDataStartWorkDateTimeReadOnly.Text = serviceInformationBasicInformationGateway.GetStartWorkDateTime(serviceId).ToString();

            tbxStartWorkDataUnitOutOfServiceDateReadOnly.Text = "";
            if (serviceInformationBasicInformationGateway.GetUnitOutOfServiceDate(serviceId).HasValue)
            {
                DateTime outOfServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitOutOfServiceDate(serviceId);
                tbxStartWorkDataUnitOutOfServiceDateReadOnly.Text = outOfServiceDate.Month.ToString() + "/" + outOfServiceDate.Day.ToString() + "/" + outOfServiceDate.Year.ToString();
            }

            tbxStartWorkDataStartMileageReadOnly.Text = serviceInformationBasicInformationGateway.GetStartWorkMileage(serviceId);
        }
        private void TagPage()
        {
            hdfCompanyId.Value = Session["companyID"].ToString();
            hdfServiceId.Value = Request.QueryString["service_id"];
            hdfActiveTab.Value = Request.QueryString["active_tab"].ToString();

            int companyId = Int32.Parse(hdfCompanyId.Value);
            int serviceId = Int32.Parse(hdfServiceId.Value);

            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGatewayForId = new ServiceInformationBasicInformationGateway();
            serviceInformationBasicInformationGatewayForId.LoadByServiceId(serviceId, companyId);
            int? unitId = serviceInformationBasicInformationGatewayForId.GetUnitID(serviceId);

            UnitsGateway unitsGateway = new UnitsGateway();
            unitsGateway.LoadByUnitId((int)unitId, companyId);
            hdfUnitType.Value = unitsGateway.GetType((int)unitId);
            hdfCompanyLevel.Value = unitsGateway.GetCompanyLevelId((int)unitId).ToString();

            int companyLevel = Int32.Parse(hdfCompanyLevel.Value);
            CompanyLevelGateway companyLevelGateway = new CompanyLevelGateway();
            companyLevelGateway.LoadByCompanyLevelId(companyLevel, companyId);
            hdfMileageUnitOfMeasurement.Value = companyLevelGateway.GetUnitsUnitOfMeasurement(companyLevel);
            lblPnlStartWorkStartMileageUnitOfMeasurement.Text = hdfMileageUnitOfMeasurement.Value;
            lblPnlCompleteWorkCompleteMileageUnitOfMeasurement.Text = hdfMileageUnitOfMeasurement.Value;
        }
        private void LoadNotes()
        {
            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);

            int? libraryCategoriesId = null; if (serviceInformationBasicInformationGateway.GetLibraryCategoriesId(int.Parse(hdfServiceId.Value)).HasValue) libraryCategoriesId = (int)serviceInformationBasicInformationGateway.GetLibraryCategoriesId(int.Parse(hdfServiceId.Value));

            if (libraryCategoriesId.HasValue)
            {
                int companyId = Int32.Parse(hdfCompanyId.Value);
                ViewState["libraryCategoriesId"] = (int)libraryCategoriesId;
                tbxCategoryAssocited.Text = GetFullCategoryName((int)libraryCategoriesId, companyId);
                btnAssociate.Visible = false;
                btnUnassociate.Visible = true;

                tbxNoteNoteNew.ReadOnly = false;
                btnAddAtachment.Enabled = true;
            }
            else
            {
                tbxCategoryAssocited.Text = "";
                btnAssociate.Visible = true;
                btnUnassociate.Visible = false;

                tbxNoteNoteNew.ReadOnly = true;
                btnAddAtachment.Enabled = false;
            }
        }
        private void UpdateState()
        {
            int serviceId = Int32.Parse(hdfServiceId.Value);
            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);

            // General Data
            bool mtoDto = serviceInformationBasicInformationGateway.GetMtoDto(serviceId);
            string serviceDescription = serviceInformationBasicInformationGateway.GetServiceDescription(serviceId);
            string mileage = serviceInformationBasicInformationGateway.GetMileage(serviceId);
            int? libraryCategoriesId = null; if (serviceInformationBasicInformationGateway.GetLibraryCategoriesId(serviceId).HasValue) libraryCategoriesId = serviceInformationBasicInformationGateway.GetLibraryCategoriesId(serviceId).Value;

            // Initialize Data
            // ... Initialize Assigned Data with existent data
            DateTime? newAssignmentDateTime = null; if (serviceInformationBasicInformationGateway.GetAssignmentDateTime(serviceId).HasValue) newAssignmentDateTime = (DateTime)serviceInformationBasicInformationGateway.GetAssignmentDateTime(serviceId);
            DateTime? newDeadlineDate = null; if (serviceInformationBasicInformationGateway.GetAssignedDeadlineDate(serviceId).HasValue) newDeadlineDate = (DateTime)serviceInformationBasicInformationGateway.GetAssignedDeadlineDate(serviceId);
            bool newToTeamMember = serviceInformationBasicInformationGateway.GetToTeamMember(serviceId);
            int? newAssignTeamMemberID = null; if(serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId).HasValue) newAssignTeamMemberID = (int)serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId);
            string newThirdPartyVendor = serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId);

            // ... Initialize Assigned Accepted Data with existent data
            DateTime? newAssignmentAcceptedDateTime = null; if (serviceInformationBasicInformationGateway.GetAcceptedDateTime(serviceId).HasValue) newAssignmentAcceptedDateTime = (DateTime)serviceInformationBasicInformationGateway.GetAcceptedDateTime(serviceId);

            // ... Initialize Assigned Rejected Data with existent data
            DateTime? newAssignmentRejectedDateTime = null; if (serviceInformationBasicInformationGateway.GetRejectedDateTime(serviceId).HasValue) newAssignmentRejectedDateTime = (DateTime)serviceInformationBasicInformationGateway.GetRejectedDateTime(serviceId);
            string newAssignmnetRejectedReason = serviceInformationBasicInformationGateway.GetRejectedReason(serviceId);

            // ... Initialize Start Work Data with existent data
            DateTime? newStartWorkDateTime = null; if (serviceInformationBasicInformationGateway.GetStartWorkDateTime(serviceId).HasValue) newStartWorkDateTime = (DateTime)serviceInformationBasicInformationGateway.GetStartWorkDateTime(serviceId);
            DateTime? newUnitOutOfServiceDate = null; if (serviceInformationBasicInformationGateway.GetUnitOutOfServiceDate(serviceId).HasValue) newUnitOutOfServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitOutOfServiceDate(serviceId);
            string newUnitOutOfServiceTime = serviceInformationBasicInformationGateway.GetUnitOutOfServiceTime(serviceId);
            string newStartWorkMileage = serviceInformationBasicInformationGateway.GetStartWorkMileage(serviceId);

            // ... Initialize Complete Work Data with existent data
            DateTime? newCompleteWorkDateTime = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDateTime(serviceId).HasValue) newCompleteWorkDateTime = (DateTime)serviceInformationBasicInformationGateway.GetCompleteWorkDateTime(serviceId);
            DateTime? newUnitBackInServiceDate = null; if (serviceInformationBasicInformationGateway.GetUnitBackInServiceDate(serviceId).HasValue) newUnitBackInServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitBackInServiceDate(serviceId);
            string newUnitBackInServiceTime = serviceInformationBasicInformationGateway.GetUnitBackInServiceTime(serviceId);
            string newCompleteWorkMileage = serviceInformationBasicInformationGateway.GetCompleteWorkMileage(serviceId);
            string newAssociatedChecklistRuleState = serviceInformationBasicInformationGateway.GetAssociatedChecklistRuleState(serviceId);

            // ... Get new values
            string serviceState = null;
            switch ((string)Request.QueryString["state"])
            {
                case "Assigned":
                    serviceState = "Assigned";
                    newAssignmentDateTime = DateTime.Now;
                    if (tkrdpPnlAssignDeadlineDate.SelectedDate.HasValue) newDeadlineDate = tkrdpPnlAssignDeadlineDate.SelectedDate.Value;
                    newToTeamMember = rbtnPnlAssignToTeamMember.Checked;
                    newAssignTeamMemberID = null; if (newToTeamMember) newAssignTeamMemberID = Int32.Parse(ddlPnlAssignAssignToTeamMember.SelectedValue);
                    newThirdPartyVendor = tbxPnlAssignAssignToThirdPartyVendor.Text.Trim();
                    break;

                case "Accepted":
                    serviceState = "Accepted";
                    newAssignmentAcceptedDateTime = DateTime.Now;
                    break;

                case "Rejected":
                    serviceState = "Rejected";
                    newAssignmentRejectedDateTime = DateTime.Now;
                    newAssignmnetRejectedReason = tbxPnlAssignmentRejectDataRejectedReason.Text.Trim();
                    break;

                case "StartWork":
                    serviceState = "In Progress";
                    newStartWorkDateTime = DateTime.Now;
                    newUnitOutOfServiceDate = null; if (tkrdpPnlStartWorkUnitOutOfServiceDate.SelectedDate.HasValue) newUnitOutOfServiceDate = tkrdpPnlStartWorkUnitOutOfServiceDate.SelectedDate.Value;
                    newUnitOutOfServiceTime = DateTime.Now.ToShortTimeString();
                    newStartWorkMileage = tbxPnlStartWorkStartMileage.Text.Trim();
                    break;

                case "CompleteWork":
                    serviceState = "Completed";
                    newCompleteWorkDateTime = DateTime.Now;
                    newUnitBackInServiceDate = null; if (tkrdpPnlCompleteWorkUnitBackInServiceDate.SelectedDate.HasValue) newUnitBackInServiceDate = tkrdpPnlCompleteWorkUnitBackInServiceDate.SelectedDate.Value;
                    newUnitBackInServiceTime = DateTime.Now.ToShortTimeString();
                    newCompleteWorkMileage = tbxPnlCompleteWorkCompleteMileage.Text.Trim();
                    newAssociatedChecklistRuleState = "Healthy";
                    break;
            }

            // Update service data
            ServiceInformationBasicInformation serviceInformationBasicInformation = new ServiceInformationBasicInformation(serviceInformationTDS);
            serviceInformationBasicInformation.Update(serviceId, serviceState, mtoDto, serviceDescription, mileage, newAssignmentDateTime, newDeadlineDate, newToTeamMember, newAssignTeamMemberID, newThirdPartyVendor, newAssignmentAcceptedDateTime, newAssignmentRejectedDateTime, newAssignmnetRejectedReason, newStartWorkDateTime, newUnitOutOfServiceDate, newUnitOutOfServiceTime, newStartWorkMileage, newCompleteWorkDateTime, newUnitBackInServiceDate, newUnitBackInServiceTime, newCompleteWorkMileage, "", false, null, "", null, newAssociatedChecklistRuleState, libraryCategoriesId);
        }
        private void Save2()
        {
            // Costs Gridview, if the gridview is edition mode
            if (grdCosts.EditIndex >= 0)
            {
                grdCosts.UpdateRow(grdCosts.EditIndex, true);
            }

            // Notes Gridview, if the gridview is edition mode
            if (grdNotes.EditIndex >= 0)
            {
                grdNotes.UpdateRow(grdNotes.EditIndex, true);
            }

            // Save cost and notes data
            GrdCostsAdd();
            GrdNotesAdd();

            // Save data
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int serviceId = Int32.Parse(hdfServiceId.Value);

            // Unmodified data
            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGatewayForEdit = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
            string serviceState = serviceInformationBasicInformationGatewayForEdit.GetServiceState(serviceId);
            string associatedChecklistRuleState = serviceInformationBasicInformationGatewayForEdit.GetAssociatedChecklistRuleState(serviceId);

            // ... Get basic service data
            bool newMtoDto = ckbxMtoDto.Checked;
            string newServiceDescription = tbxServiceDescription.Text.Trim();

            // ... Get general service data
            string newMileage = tbxGeneralMileage.Text.Trim();

            // ... Get assigned data
            DateTime? newAssignmentDateTime = null;
            DateTime? newAssignmentDeadlineDate = null;
            bool newToTeamMember = false;
            int? newAssignTeamMemberID = null;
            string newThirdPartyVendor = "";
            DateTime? assignmentAcceptedDateTime = null;
            string newAssignmentRejectedReason = "";
            DateTime? assignmentRejectedDateTime = null;

            if (pnlAssignmentData.Visible)
            {
                if (pnlAssignedToReadOnly.Visible)
                {
                    if (serviceInformationBasicInformationGatewayForEdit.GetAssignmentDateTimeOriginal(serviceId).HasValue) newAssignmentDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetAssignmentDateTimeOriginal(serviceId);
                    if (serviceInformationBasicInformationGatewayForEdit.GetAssignedDeadlineDate(serviceId).HasValue) newAssignmentDeadlineDate = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetAssignedDeadlineDate(serviceId);
                    newToTeamMember = serviceInformationBasicInformationGatewayForEdit.GetToTeamMember(serviceId);
                    if (newToTeamMember) newAssignTeamMemberID = (int)serviceInformationBasicInformationGatewayForEdit.GetAssignTeamMemberId(serviceId);
                    newThirdPartyVendor = serviceInformationBasicInformationGatewayForEdit.GetAssignedThirdPartyVendor(serviceId);
                }

                if (pnlAssignedTo.Visible)
                {
                    if (serviceInformationBasicInformationGatewayForEdit.GetAssignmentDateTimeOriginal(serviceId).HasValue) newAssignmentDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetAssignmentDateTimeOriginal(serviceId);
                    if (tkrdpAssignmentDataAssignedDeadlineDate.SelectedDate.HasValue) newAssignmentDeadlineDate = tkrdpAssignmentDataAssignedDeadlineDate.SelectedDate.Value;
                    newToTeamMember = rbtnAssignmentDataToTeamMember.Checked;
                    if (newToTeamMember) newAssignTeamMemberID = Int32.Parse(ddlAssignmentDataAssignToTeamMember.SelectedValue);
                    newThirdPartyVendor = tbxAssignmentDataAssignToThirdPartyVendor.Text.Trim();
                }

                if (serviceInformationBasicInformationGatewayForEdit.GetAcceptedDateTime(serviceId).HasValue) assignmentAcceptedDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetAcceptedDateTime(serviceId);
                if (serviceInformationBasicInformationGatewayForEdit.GetRejectedDateTime(serviceId).HasValue) assignmentRejectedDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetRejectedDateTime(serviceId);
                newAssignmentRejectedReason = tbxAssignmentDataRejectedReason.Text.Trim();
            }
            else
            {
                if (pnlAssignmentDataReadOnly.Visible)
                {
                    if (serviceInformationBasicInformationGatewayForEdit.GetAssignmentDateTimeOriginal(serviceId).HasValue) newAssignmentDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetAssignmentDateTimeOriginal(serviceId);
                    if (serviceInformationBasicInformationGatewayForEdit.GetAssignedDeadlineDate(serviceId).HasValue) newAssignmentDeadlineDate = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetAssignedDeadlineDate(serviceId);
                    newToTeamMember = serviceInformationBasicInformationGatewayForEdit.GetToTeamMember(serviceId);
                    if (newToTeamMember) newAssignTeamMemberID = (int)serviceInformationBasicInformationGatewayForEdit.GetAssignTeamMemberId(serviceId);
                    newThirdPartyVendor = serviceInformationBasicInformationGatewayForEdit.GetAssignedThirdPartyVendor(serviceId);
                    if (serviceInformationBasicInformationGatewayForEdit.GetAcceptedDateTime(serviceId).HasValue) assignmentAcceptedDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetAcceptedDateTime(serviceId);
                    if (serviceInformationBasicInformationGatewayForEdit.GetRejectedDateTime(serviceId).HasValue) assignmentRejectedDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetRejectedDateTime(serviceId);
                    newAssignmentRejectedReason = tbxAssignmentDataRejectedReasonReadOnly.Text.Trim();
                }
            }

            // ... Get start work data
            DateTime? startWorkDateTime = null; if (serviceInformationBasicInformationGatewayForEdit.GetStartWorkDateTime(serviceId).HasValue) startWorkDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetStartWorkDateTime(serviceId);
            DateTime? newUnitOutOfServiceDate = null;
            string newUnitOutOfServiceTime = "";
            string newStartWorkMileage = "";

            if (pnlStartWorkData.Visible)
            {
                if (tkrdpStartWorkDataUnitOutOfServiceDate.SelectedDate.HasValue) newUnitOutOfServiceDate = tkrdpStartWorkDataUnitOutOfServiceDate.SelectedDate.Value;
                newUnitOutOfServiceTime = "8:00 AM";
                newStartWorkMileage = tbxStartWorkDataStartMileage.Text.Trim();
            }
            else
            {
                if (pnlStartWorkDataReadOnly.Visible)
                {
                    newUnitOutOfServiceDate = serviceInformationBasicInformationGatewayForEdit.GetUnitOutOfServiceDate(serviceId);
                    newUnitOutOfServiceTime = serviceInformationBasicInformationGatewayForEdit.GetUnitOutOfServiceTime(serviceId);
                    newStartWorkMileage = serviceInformationBasicInformationGatewayForEdit.GetMileage(serviceId);
                }
            }

            // ... Get complete work data
            DateTime? completeWorkDateTime = null; if (serviceInformationBasicInformationGatewayForEdit.GetCompleteWorkDateTime(serviceId).HasValue) completeWorkDateTime = (DateTime)serviceInformationBasicInformationGatewayForEdit.GetCompleteWorkDateTime(serviceId);
            DateTime? newUnitBackInServiceDate = null; if (tkrdpCompleteWorkDataUnitBackInServiceDate.SelectedDate.HasValue) newUnitBackInServiceDate = tkrdpCompleteWorkDataUnitBackInServiceDate.SelectedDate.Value;
            string newUnitBackInServiceTime = "8:00 AM";
            string newCompleteWorkMileage = tbxCompleteWorkDataCompleteMileage.Text.Trim();
            string newCompleteWorkDetailDescription = "";
            bool newCompleteWorkDetailPreventable = false;

            // ... ... From team member
            if (pnlTeamMemberAssigned.Visible)
            {
                if (tbxCompleteWorkDataDescription.Text.Trim() != "") newCompleteWorkDetailDescription = tbxCompleteWorkDataDescription.Text.Trim();
                newCompleteWorkDetailPreventable = ckbxCompleteWorkDataPreventable.Checked;
            }
            decimal? newCompleteWorkDetailTMLabourHours = null; if (tbxCompleteWorkDataLabourHours.Text.Trim() != "") newCompleteWorkDetailTMLabourHours = decimal.Parse(tbxCompleteWorkDataLabourHours.Text.Trim());

            // ... ... From Third party vendor
            if (pnlThirdPartyVendorAssigned.Visible)
            {
                if (tbxCompleteWorkDataDescriptionThirdPartyVendor.Text.Trim() != "") newCompleteWorkDetailDescription = tbxCompleteWorkDataDescriptionThirdPartyVendor.Text.Trim();
                newCompleteWorkDetailPreventable = ckbxCompleteWorkDataPreventableThirdPartyVendor.Checked;
            }
            string newCompleteWorkDetailTPVInvoiceNumber = tbxCompleteWorkDataInvoiceNumberThirdPartyVendor.Text.Trim();
            decimal? newCompleteWorkDetailTPVInvoiceAmout = null; if (tbxCompleteWorkDataInvoiceAmountThirdPartyVendor.Text.Trim() != "") newCompleteWorkDetailTPVInvoiceAmout = decimal.Parse(tbxCompleteWorkDataInvoiceAmountThirdPartyVendor.Text.Trim());

            // Update service data
            ServiceInformationBasicInformation serviceInformationBasicInformation = new ServiceInformationBasicInformation(serviceInformationTDS);
            int? libraryCategoriesId = null; if (serviceInformationBasicInformationGatewayForEdit.GetLibraryCategoriesId(int.Parse(hdfServiceId.Value)).HasValue) libraryCategoriesId = (int)serviceInformationBasicInformationGatewayForEdit.GetLibraryCategoriesId(int.Parse(hdfServiceId.Value));
            serviceInformationBasicInformation.Update(serviceId, serviceState, newMtoDto, newServiceDescription, newMileage, newAssignmentDateTime, newAssignmentDeadlineDate, newToTeamMember, newAssignTeamMemberID, newThirdPartyVendor, assignmentAcceptedDateTime, assignmentRejectedDateTime, newAssignmentRejectedReason, startWorkDateTime, newUnitOutOfServiceDate, newUnitOutOfServiceTime, newStartWorkMileage, completeWorkDateTime, newUnitBackInServiceDate, newUnitBackInServiceTime, newCompleteWorkMileage, newCompleteWorkDetailDescription, newCompleteWorkDetailPreventable, newCompleteWorkDetailTMLabourHours, newCompleteWorkDetailTPVInvoiceNumber, newCompleteWorkDetailTPVInvoiceAmout, associatedChecklistRuleState, libraryCategoriesId);

            // Store datasets
            Session["serviceInformationTDS"] = serviceInformationTDS;

            ViewState["update"] = "no";
        }
        /// <summary>
        /// Save
        /// </summary>
        /// <param name="companyId">companyId</param>
        public void Save(int companyId)
        {
            ServiceInformationTDS servicesInformationChanges = (ServiceInformationTDS)Data.GetChanges();

            if (servicesInformationChanges.BasicInformation.Rows.Count > 0)
            {
                ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(servicesInformationChanges);

                // Update services
                foreach (ServiceInformationTDS.BasicInformationRow basicInformationRow in (ServiceInformationTDS.BasicInformationDataTable)servicesInformationChanges.BasicInformation)
                {
                    // Unchanged values
                    int serviceId = basicInformationRow.ServiceID;
                    string number = serviceInformationBasicInformationGateway.GetServiceNumber(serviceId);
                    DateTime dateTime_ = serviceInformationBasicInformationGateway.GetDateTime_(serviceId);
                    int? unitId = null; if (serviceInformationBasicInformationGateway.GetUnitID(serviceId).HasValue) unitId = serviceInformationBasicInformationGateway.GetUnitID(serviceId);
                    string type = serviceInformationBasicInformationGateway.GetType(serviceId);
                    int ownerId = serviceInformationBasicInformationGateway.GetOwnerID(serviceId);

                    // Original values
                    bool originalMtoDto = serviceInformationBasicInformationGateway.GetMtoDtoOriginal(serviceId);
                    string originalDescription = serviceInformationBasicInformationGateway.GetServiceDescriptionOriginal(serviceId);
                    string originalState = serviceInformationBasicInformationGateway.GetServiceStateOriginal(serviceId);
                    DateTime? originalAssignDateTime = null; if (serviceInformationBasicInformationGateway.GetAssignmentDateTimeOriginal(serviceId).HasValue) originalAssignDateTime = (DateTime)serviceInformationBasicInformationGateway.GetAssignmentDateTimeOriginal(serviceId);
                    DateTime? originalAssignDeadlineDate = null; if (serviceInformationBasicInformationGateway.GetAssignedDeadlineDateOriginal(serviceId).HasValue) originalAssignDeadlineDate = (DateTime)serviceInformationBasicInformationGateway.GetAssignedDeadlineDateOriginal(serviceId);
                    bool originalAssignTeamMember = serviceInformationBasicInformationGateway.GetToTeamMemberOriginal(serviceId);
                    int? originalAssignTeamMemberID = null; if (serviceInformationBasicInformationGateway.GetAssignTeamMemberIdOriginal(serviceId).HasValue) originalAssignTeamMemberID = (int)serviceInformationBasicInformationGateway.GetAssignTeamMemberIdOriginal(serviceId);
                    string originalAssignThirdPartyVendor = serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendorOriginal(serviceId);
                    DateTime? originalAcceptDateTime = null; if (serviceInformationBasicInformationGateway.GetAcceptedDateTimeOriginal(serviceId).HasValue) originalAcceptDateTime = (DateTime)serviceInformationBasicInformationGateway.GetAcceptedDateTimeOriginal(serviceId);
                    DateTime? originalRejectDateTime = null; if (serviceInformationBasicInformationGateway.GetRejectedDateTimeOriginal(serviceId).HasValue) originalRejectDateTime = (DateTime)serviceInformationBasicInformationGateway.GetRejectedDateTimeOriginal(serviceId);
                    string originalRejectReason = serviceInformationBasicInformationGateway.GetRejectedReasonOriginal(serviceId);
                    DateTime? originalStartWorkDateTime = null; if (serviceInformationBasicInformationGateway.GetStartWorkDateTimeOriginal(serviceId).HasValue) originalStartWorkDateTime = (DateTime)serviceInformationBasicInformationGateway.GetStartWorkDateTimeOriginal(serviceId);
                    DateTime? originalStartWorkOutOfServiceDate = null; if (serviceInformationBasicInformationGateway.GetUnitOutOfServiceDateOriginal(serviceId).HasValue) originalStartWorkOutOfServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitOutOfServiceDateOriginal(serviceId);
                    string originalStartWorkOutOfServiceTime = serviceInformationBasicInformationGateway.GetUnitOutOfServiceTimeOriginal(serviceId);
                    DateTime? originalCompleteWorkDateTime = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDateTimeOriginal(serviceId).HasValue) originalCompleteWorkDateTime = (DateTime)serviceInformationBasicInformationGateway.GetCompleteWorkDateTimeOriginal(serviceId);
                    DateTime? originalCompleteWorkBackToServiceDate = null; if (serviceInformationBasicInformationGateway.GetUnitBackInServiceDateOriginal(serviceId).HasValue) originalCompleteWorkBackToServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitBackInServiceDateOriginal(serviceId);
                    string originalCompleteWorkBackToServiceTime = serviceInformationBasicInformationGateway.GetUnitBackInServiceTimeOriginal(serviceId);
                    string originalCompleteWorkDetailDescription = serviceInformationBasicInformationGateway.GetCompleteWorkDetailDescriptionOriginal(serviceId);
                    bool originalCompleteWorkDetailPreventable = serviceInformationBasicInformationGateway.GetCompleteWorkDetailPreventableOriginal(serviceId);
                    decimal? originalCompleteWorkDetailTMLabourHours = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMLabourHoursOriginal(serviceId).HasValue) originalCompleteWorkDetailTMLabourHours = (decimal)serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMLabourHoursOriginal(serviceId);
                    decimal? originalCompleteWorkDetailTMCost = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMCostOriginal(serviceId).HasValue) originalCompleteWorkDetailTMCost = (decimal)serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMCostOriginal(serviceId);
                    string originalCompleteWorkDetailTPVInvoiceNumber = serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceNumberOriginal(serviceId);
                    decimal? originalCompleteWorkDetailTPVInvoiceAmout = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceAmoutOriginal(serviceId).HasValue) originalCompleteWorkDetailTPVInvoiceAmout = (decimal)serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceAmoutOriginal(serviceId);
                    string originalNotes = serviceInformationBasicInformationGateway.GetNotesOriginal(serviceId);
                    int? originalRuleId = null; if (serviceInformationBasicInformationGateway.GetRuleIdOriginal(serviceId).HasValue) originalRuleId = (int)serviceInformationBasicInformationGateway.GetRuleIdOriginal(serviceId);
                    string originalMileage = serviceInformationBasicInformationGateway.GetMileageOriginal(serviceId);
                    string originalStartWorkMileage = serviceInformationBasicInformationGateway.GetStartWorkMileageOriginal(serviceId);
                    string originalCompleteWorkMileage = serviceInformationBasicInformationGateway.GetCompleteWorkMileageOriginal(serviceId);
                    bool originalDeleted = serviceInformationBasicInformationGateway.GetDeletedOriginal(serviceId);
                    int? originalLibraryCategoriesId = null; if (serviceInformationBasicInformationGateway.GetLibraryCategoriesIdOriginal(serviceId).HasValue) originalLibraryCategoriesId = serviceInformationBasicInformationGateway.GetLibraryCategoriesIdOriginal(serviceId).Value;

                    // New variables
                    bool newMtoDto = serviceInformationBasicInformationGateway.GetMtoDto(serviceId);
                    string newDescription = serviceInformationBasicInformationGateway.GetServiceDescription(serviceId);
                    string newState = serviceInformationBasicInformationGateway.GetServiceState(serviceId);
                    DateTime? newAssignDateTime = null; if(serviceInformationBasicInformationGateway.GetAssignmentDateTime(serviceId).HasValue) newAssignDateTime = (DateTime)serviceInformationBasicInformationGateway.GetAssignmentDateTime(serviceId);
                    DateTime? newAssignDeadlineDate = null; if (serviceInformationBasicInformationGateway.GetAssignedDeadlineDate(serviceId).HasValue) newAssignDeadlineDate = (DateTime)serviceInformationBasicInformationGateway.GetAssignedDeadlineDate(serviceId);
                    bool newAssignTeamMember = serviceInformationBasicInformationGateway.GetToTeamMember(serviceId);
                    int? newAssignTeamMemberId = null; if (serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId).HasValue) newAssignTeamMemberId = (int)serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId);
                    string newAssignThirdPartyVendor = serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId);
                    DateTime? newAcceptDateTime = null; if(serviceInformationBasicInformationGateway.GetAcceptedDateTime(serviceId).HasValue) newAcceptDateTime = (DateTime)serviceInformationBasicInformationGateway.GetAcceptedDateTime(serviceId);
                    DateTime? newRejectDateTime = null; if(serviceInformationBasicInformationGateway.GetRejectedDateTime(serviceId).HasValue) newRejectDateTime = (DateTime)serviceInformationBasicInformationGateway.GetRejectedDateTime(serviceId);
                    string newRejectReason = serviceInformationBasicInformationGateway.GetRejectedReason(serviceId);
                    DateTime? newStartWorkDateTime = null;  if( serviceInformationBasicInformationGateway.GetStartWorkDateTime(serviceId).HasValue) newStartWorkDateTime = (DateTime) serviceInformationBasicInformationGateway.GetStartWorkDateTime(serviceId);
                    DateTime? newStartWorkOutOfServiceDate = null; if (serviceInformationBasicInformationGateway.GetUnitOutOfServiceDate(serviceId).HasValue) newStartWorkOutOfServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitOutOfServiceDate(serviceId);
                    string newStartWorkOutOfServiceTime = serviceInformationBasicInformationGateway.GetUnitOutOfServiceTime(serviceId);
                    DateTime? newCompleteWorkDateTime = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDateTime(serviceId).HasValue) newCompleteWorkDateTime = (DateTime)serviceInformationBasicInformationGateway.GetCompleteWorkDateTime(serviceId);
                    DateTime? newCompleteWorkBackToServiceDate = null; if (serviceInformationBasicInformationGateway.GetUnitBackInServiceDate(serviceId).HasValue) newCompleteWorkBackToServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitBackInServiceDate(serviceId);
                    string newCompleteWorkBackToServiceTime = serviceInformationBasicInformationGateway.GetUnitBackInServiceTime(serviceId);
                    string newCompleteWorkDetailDescription = serviceInformationBasicInformationGateway.GetCompleteWorkDetailDescription(serviceId);
                    bool newCompleteWorkDetailPreventable = serviceInformationBasicInformationGateway.GetCompleteWorkDetailPreventable(serviceId);
                    decimal? newCompleteWorkDetailTMLabourHours = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMLabourHours(serviceId).HasValue) newCompleteWorkDetailTMLabourHours = (decimal)serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMLabourHours(serviceId);
                    decimal? newCompleteWorkDetailTMCost = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMCost(serviceId).HasValue) newCompleteWorkDetailTMCost = (decimal)serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMCost(serviceId);
                    string newCompleteWorkDetailTPVInvoiceNumber = serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceNumber(serviceId);
                    decimal? newCompleteWorkDetailTPVInvoiceAmout = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceAmout(serviceId).HasValue) newCompleteWorkDetailTPVInvoiceAmout = (decimal)serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceAmout(serviceId);
                    string newNotes = serviceInformationBasicInformationGateway.GetNotes(serviceId);
                    int? newRuleId = null; if (serviceInformationBasicInformationGateway.GetRuleId(serviceId).HasValue) newRuleId = (int)serviceInformationBasicInformationGateway.GetRuleId(serviceId);
                    string newMileage = serviceInformationBasicInformationGateway.GetMileage(serviceId);
                    string newStartWorkMileage = serviceInformationBasicInformationGateway.GetStartWorkMileage(serviceId);
                    string newCompleteWorkMileage = serviceInformationBasicInformationGateway.GetCompleteWorkMileage(serviceId);
                    int? newLibraryCategoriesId = null; if (serviceInformationBasicInformationGateway.GetLibraryCategoriesId(serviceId).HasValue) newLibraryCategoriesId = serviceInformationBasicInformationGateway.GetLibraryCategoriesId(serviceId).Value;
                    bool newDeleted = serviceInformationBasicInformationGateway.GetDeleted(serviceId);

                    // ... Update
                    UpdateService(serviceId, number, dateTime_, originalMtoDto, originalDescription, unitId, type, originalState, ownerId, originalAssignDateTime, originalAssignDeadlineDate, originalAssignTeamMember, originalAssignTeamMemberID, originalAssignThirdPartyVendor, originalAcceptDateTime, originalRejectDateTime, originalRejectReason, originalStartWorkDateTime, originalStartWorkOutOfServiceDate, originalStartWorkOutOfServiceTime, originalCompleteWorkDateTime, originalCompleteWorkBackToServiceDate, originalCompleteWorkBackToServiceTime, originalCompleteWorkDetailDescription, originalCompleteWorkDetailPreventable, originalCompleteWorkDetailTMLabourHours, originalCompleteWorkDetailTMCost, originalCompleteWorkDetailTPVInvoiceNumber, originalCompleteWorkDetailTPVInvoiceAmout, originalDeleted, companyId, originalNotes, originalRuleId, originalMileage, originalStartWorkMileage, originalCompleteWorkMileage, originalLibraryCategoriesId, number, dateTime_, newMtoDto, newDescription, unitId, type, newState, ownerId, newAssignDateTime, newAssignDeadlineDate, newAssignTeamMember, newAssignTeamMemberId, newAssignThirdPartyVendor, newAcceptDateTime, newRejectDateTime, newRejectReason, newStartWorkDateTime, newStartWorkOutOfServiceDate, newStartWorkOutOfServiceTime, newCompleteWorkDateTime, newCompleteWorkBackToServiceDate, newCompleteWorkBackToServiceTime, newCompleteWorkDetailDescription, newCompleteWorkDetailPreventable, newCompleteWorkDetailTMLabourHours, newCompleteWorkDetailTMCost, newCompleteWorkDetailTPVInvoiceNumber, newCompleteWorkDetailTPVInvoiceAmout, newDeleted, companyId, newNotes, newRuleId, newMileage, newStartWorkMileage, newCompleteWorkMileage, newLibraryCategoriesId);

                    // Update checklist
                    if (type == "Checklist")
                    {
                        // ... Original values
                        DateTime? originalAssociatedChecklistLastService = serviceInformationBasicInformationGateway.GetAssociatedChecklistLastServiceOriginal(serviceId);
                        DateTime? originalAssociatedChecklistNextDue = serviceInformationBasicInformationGateway.GetAssociatedChecklistNextDueOriginal(serviceId);
                        bool originalAssociatedChecklistDone = serviceInformationBasicInformationGateway.GetAssociatedChecklistDoneOriginal(serviceId);
                        bool originalAssociatedChecklistDeleted = serviceInformationBasicInformationGateway.GetAssociatedChecklistDeletedOriginal(serviceId);
                        int originalAssociatedChecklistCompanyId = serviceInformationBasicInformationGateway.GetAssociatedChecklistCompanyIdOriginal(serviceId);
                        string originalAssociatedChecklistRuleState = serviceInformationBasicInformationGateway.GetAssociatedChecklistRuleStateOriginal(serviceId);

                        // ... New values
                        DateTime? newAssociatedChecklistLastService = serviceInformationBasicInformationGateway.GetAssociatedChecklistLastService(serviceId);
                        DateTime? newAssociatedChecklistNextDue = serviceInformationBasicInformationGateway.GetAssociatedChecklistNextDue(serviceId);
                        bool newAssociatedChecklistDone = serviceInformationBasicInformationGateway.GetAssociatedChecklistDone(serviceId);
                        bool newAssociatedChecklistDeleted = serviceInformationBasicInformationGateway.GetAssociatedChecklistDeleted(serviceId);
                        int newAssociatedChecklistCompanyId = serviceInformationBasicInformationGateway.GetAssociatedChecklistCompanyId(serviceId);
                        string newAssociatedChecklistRuleState = serviceInformationBasicInformationGateway.GetAssociatedChecklistRuleState(serviceId);

                        // ... Update
                        Checklist checklist = new Checklist();
                        checklist.UpdateDirect((int)unitId, (int)originalRuleId, originalAssociatedChecklistLastService, originalAssociatedChecklistNextDue, originalAssociatedChecklistDone, originalAssociatedChecklistRuleState, originalAssociatedChecklistDeleted, originalAssociatedChecklistCompanyId, (int)unitId, (int)originalRuleId, newAssociatedChecklistLastService, newAssociatedChecklistNextDue, newAssociatedChecklistDone, newAssociatedChecklistRuleState, originalAssociatedChecklistDeleted, originalAssociatedChecklistCompanyId);
                    }
                }
            }
        }
        protected void grdNotes_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Edit items
            if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate))))
            {
                string originalFileNameEdit = ((TextBox)e.Row.FindControl("tbxNoteAttachmentEdit")).Text;
                string fileName = ((Label)e.Row.FindControl("lblFileNameEdit")).Text;
                int libraryFileId = 0; if (((Label)e.Row.FindControl("lblLibraryFileIdEdit")).Text != "") libraryFileId = Int32.Parse(((Label)e.Row.FindControl("lblLibraryFileIdEdit")).Text);

                // Button visibility
                if (originalFileNameEdit == "")
                {
                    ((Button)e.Row.FindControl("btnNoteDeleteEdit")).Visible = false;

                    ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
                    int? libraryCategoriesId = null; if (serviceInformationBasicInformationGateway.GetLibraryCategoriesId(int.Parse(hdfServiceId.Value)).HasValue) libraryCategoriesId = (int)serviceInformationBasicInformationGateway.GetLibraryCategoriesId(int.Parse(hdfServiceId.Value));

                    if (libraryCategoriesId.HasValue)
                    {
                        ((Button)e.Row.FindControl("btnNoteAddEdit")).Visible = true;
                    }
                    else
                    {
                        ((Button)e.Row.FindControl("btnNoteAddEdit")).Visible = false;
                    }
                }
                else
                {
                    ((Button)e.Row.FindControl("btnNoteDeleteEdit")).Visible = true;
                    ((Button)e.Row.FindControl("btnNoteAddEdit")).Visible = false;
                }
            }

            // Normal items
            if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState == DataControlRowState.Normal) || (e.Row.RowState == (DataControlRowState.Normal | DataControlRowState.Alternate))))
            {
                string originalFileName = ((TextBox)e.Row.FindControl("tbxNoteAttachment")).Text;
                string fileName = ((Label)e.Row.FindControl("lblFileName")).Text;
                int libraryFileId = 0; if (((Label)e.Row.FindControl("lblLibraryFileId")).Text != "") libraryFileId = Int32.Parse(((Label)e.Row.FindControl("lblLibraryFileId")).Text);

                // Button visibility
                if (originalFileName == "")
                {
                    ((Button)e.Row.FindControl("btnNoteDownload")).Visible = false;
                }
                else
                {
                    ((Button)e.Row.FindControl("btnNoteDownload")).Visible = true;
                }
            }

            // Footer item
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
                int? libraryCategoriesId = null; if (serviceInformationBasicInformationGateway.GetLibraryCategoriesId(int.Parse(hdfServiceId.Value)).HasValue) libraryCategoriesId = (int)serviceInformationBasicInformationGateway.GetLibraryCategoriesId(int.Parse(hdfServiceId.Value));

                if (libraryCategoriesId.HasValue)
                {
                    ((Button)e.Row.FindControl("btnAddFooter")).Visible = true;
                }
                else
                {
                    ((Button)e.Row.FindControl("btnAddFooter")).Visible = false;
                }
            }
        }
 // ////////////////////////////////////////////////////////////////////////
 // PUBLIC METHODS
 //
 /// <summary>
 /// LoadByServiceId
 /// </summary>
 /// <param name="serviceId">serviceId</param>              
 /// <param name="companyId">companyId</param>
 public void LoadByServiceId(int serviceId, int companyId)
 {
     ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(Data);
     serviceInformationBasicInformationGateway.LoadByServiceId(serviceId, companyId);
 }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            // Set active toolbar
            mForm6 master = (mForm6)this.Master;
            master.ActiveToolbar = "FleetManagement";

            // For error message
            hdfErrorFieldList.Value = "";

            // Validate left menu if the user has admin permission
            if (Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_SERVICES_ADMIN"]))
            {
                tkrpbLeftMenuAllServiceRequests.Visible = true;
                tkrpbLeftMenuMyServiceRequests.Visible = false;
                tkrpbLeftMenuTools.Visible = true;
            }
            else
            {
                tkrpbLeftMenuAllServiceRequests.Visible = false;
                tkrpbLeftMenuMyServiceRequests.Visible = true;
                tkrpbLeftMenuTools.Visible = false;
            }

            // Tabs validation
            // ... Validate vehicle info
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int serviceId = Int32.Parse(hdfServiceId.Value);

            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGatewayForId = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
            int? unitId = serviceInformationBasicInformationGatewayForId.GetUnitID(serviceId);

            UnitsGateway unitsGateway = new UnitsGateway();
            unitsGateway.LoadByUnitId((int)unitId, companyId);
            string unitType = unitsGateway.GetType((int)unitId);
            int companyLevel = unitsGateway.GetCompanyLevelId((int)unitId);

            if (unitType != "Vehicle")
            {
                lblGeneralMileage.Visible = false;
                tbxGeneralMileage.Visible = false;
                lblGeneralMileageUnitOfMeasurement.Visible = false;
                lblStartWorkDataStartMileage.Visible = false;
                tbxStartWorkDataStartMileage.Visible = false;
                lblStartWorkDataMileageUnitOfMeasurement.Visible = false;
                lblStartWorkDataMileageUnitOfMeasurementReadOnly.Visible = false;
                lblCompleteWorkDataCompleteMileage.Visible = false;
                tbxCompleteWorkDataCompleteMileage.Visible = false;
                lblCompleteWorkDataMileageUnitOfMeasurement.Visible = false;
                lblCompleteWorkDataMileageUnitOfMeasurementReadOnly.Visible = false;
            }
            else
            {
                // .. For Vehicles
                lblGeneralMileage.Visible = true;
                tbxGeneralMileage.Visible = true;
                lblStartWorkDataStartMileage.Visible = true;
                tbxStartWorkDataStartMileage.Visible = true;
                lblCompleteWorkDataCompleteMileage.Visible = true;
                tbxCompleteWorkDataCompleteMileage.Visible = true;
                lblGeneralMileageUnitOfMeasurement.Visible = true;
                lblStartWorkDataMileageUnitOfMeasurement.Visible = true;
                lblStartWorkDataMileageUnitOfMeasurementReadOnly.Visible = true;
                lblCompleteWorkDataMileageUnitOfMeasurementReadOnly.Visible = true;
            }

            // Assign Tab
            // ... Assigned To
            hdfServiceState.Value = serviceInformationBasicInformationGatewayForId.GetServiceState(serviceId);
            string state = hdfServiceState.Value;

            if (state == "Unassigned")
            {
                // ... Princial Panels
                pnlAssignmentData.Visible = false;
                pnlAssignmentDataReadOnly.Visible = true;

                // ... ...Subpanels
                pnlAssignedTo.Visible = false;
                pnlAssignedToReadOnly.Visible = false;
                pnlAssignmentAccept.Visible = false;
                pnlAssignmentReject.Visible = false;

                pnlAssignmentAcceptReadOnly.Visible = true;
                pnlAssignmentRejectReadOnly.Visible = true;
            }

            if ((state == "Assigned") || (state == "Rejected"))
            {
                // ... ... Principal Panels
                pnlAssignmentData.Visible = true;
                pnlAssignmentDataReadOnly.Visible = false;

                // ... ...Subpanels
                pnlAssignedTo.Visible = true;
                pnlAssignedToReadOnly.Visible = false;
                pnlAssignmentAccept.Visible = false;
                pnlAssignmentReject.Visible = false;

                pnlAssignmentAcceptReadOnly.Visible = false;
                pnlAssignmentRejectReadOnly.Visible = false;
            }
            else
            {
                if ((state == "Accepted") || (state == "In Progress") || (state == "Completed"))
                {
                    // ... ... Principal Panels
                    pnlAssignmentData.Visible = true;
                    pnlAssignmentDataReadOnly.Visible = false;

                    // ... ...Subpanels
                    pnlAssignedTo.Visible = false;
                    pnlAssignedToReadOnly.Visible = true;
                    pnlAssignmentAccept.Visible = false;
                    pnlAssignmentReject.Visible = false;
                    pnlAssignmentAcceptReadOnly.Visible = false;
                    pnlAssignmentRejectReadOnly.Visible = false;
                }
            }

            // ... Assignation result
            if ((state == "Accepted") || (state == "In Progress") || (state == "Completed"))
            {
                // ... ... Principal Panels
                pnlAssignmentData.Visible = true;
                pnlAssignmentDataReadOnly.Visible = false;

                // ... ...Subpanels
                pnlAssignedTo.Visible = false;
                pnlAssignedToReadOnly.Visible = true;
                pnlAssignmentAccept.Visible = true;
                pnlAssignmentReject.Visible = false;
                pnlAssignmentAcceptReadOnly.Visible = false;
                pnlAssignmentRejectReadOnly.Visible = false;
            }

            // StartWork tab
            if ((state == "Unassigned") || (state == "Assigned") || (state == "Rejected") || (state == "Accepted"))
            {
                pnlStartWorkDataReadOnly.Visible = true;
                pnlStartWorkData.Visible = false;
            }
            else
            {
                pnlStartWorkDataReadOnly.Visible = false;
                pnlStartWorkData.Visible = true;
            }

            // Complete Work tab
            if ((state == "Unassigned") || (state == "Assigned") || (state == "Rejected") || (state == "Accepted") || (state == "In Progress"))
            {
                pnlCompleteWorkDataReadOnly.Visible = true;
                pnlCompleteWorkData.Visible = false;
            }
            else
            {
                pnlCompleteWorkDataReadOnly.Visible = false;
                pnlCompleteWorkData.Visible = true;
            }

            // ... Validate for assignated person
            if (serviceInformationBasicInformationGatewayForId.GetToTeamMember(serviceId))
            {
                pnlTeamMemberAssigned.Visible = true;
                pnlThirdPartyVendorAssigned.Visible = false;
                pnlTeamMemberAssignedReadOnly.Visible = true;
                pnlThirdPartyVendorAssignedReadOnly.Visible = false;
            }
            else
            {
                pnlTeamMemberAssigned.Visible = false;
                pnlThirdPartyVendorAssigned.Visible = true;
                pnlTeamMemberAssignedReadOnly.Visible = false;
                pnlThirdPartyVendorAssignedReadOnly.Visible = true;

            }
        }
 /// <summary>
 /// LoadInProgressByServiceIdUnitIdRuleId
 /// </summary>
 /// <param name="serviceId">serviceId</param>              
 /// <param name="companyId">companyId</param>
 public void LoadInProgressByServiceIdUnitIdRuleId(int serviceId, int unitId, int ruleId, int companyId)
 {
     ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(Data);
     serviceInformationBasicInformationGateway.LoadInProgressByServiceIdUnitIdRuleId(serviceId, unitId, ruleId, companyId);
 }
예제 #29
0
        /// <summary>
        /// UpdateDataForNavigator
        /// </summary>
        private void UpdateDataForNavigator()
        {
            foreach (ServicesNavigatorTDS.ServicesNavigatorRow row in (ServicesNavigatorTDS.ServicesNavigatorDataTable)Table)
            {
                if (row.IsAssignedToNull())
                {
                    ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway();
                    serviceInformationBasicInformationGateway.LoadByServiceId(row.ServiceID, row.COMPANY_ID);
                    if (serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(row.ServiceID) != "")
                    {
                        row.AssignedTo = serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(row.ServiceID);
                    }
                }

                if (row.IsMTONull()) row.MTO = false;
                if (row.IsCompleteWorkDetailPreventableNull()) row.CompleteWorkDetailPreventable = false;
            }
        }
 private bool IsDeletedSR(int serviceId)
 {
     ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
     if (serviceInformationBasicInformationGateway.Table.Rows.Count > 0)
     {
         if (serviceInformationBasicInformationGateway.GetRuleId(serviceId).HasValue)
         {
             return false;
         }
         else
         {
             return true;
         }
     }
     else
     {
         return false;
     }
 }