// ////////////////////////////////////////////////////////////////////////
        // PUBLIC METHODS
        //
        /// <summary>
        /// Insert costing sheet
        /// </summary>
        /// <param name="projectId">projectId</param>
        /// <param name="name">name</param>
        /// <param name="startDate">startDate</param>
        /// <param name="endDate">endDate</param>
        /// <param name="totalLabourHoursCad">totalLabourHoursCad</param>
        /// <param name="totalLabourHoursUsd">totalLabourHoursUsd</param>
        /// <param name="totalMaterialsCad">totalMaterialsCad</param>
        /// <param name="totalMaterialsUsd">totalMaterialsUsd</param>
        /// <param name="totalUnitsCad">totalUnitsCad</param>
        /// <param name="totalUnitsUsd">totalUnitsUsd</param>
        /// <param name="totalOtherCostsCad">totalOtherCostsCad</param>
        /// <param name="totalOtherCostsUsd">totalOtherCostsUsd</param>
        /// <param name="grandTotalCad">grandTotalCad</param>
        /// <param name="grandTotalUsd">grandTotalUsd</param>
        /// <param name="state">state</param>
        /// <param name="deleted">deleted</param>
        /// <param name="companyId">companyId</param>
        /// <param name="totalSubcontractorsCad">totalSubcontractorsCad</param>
        /// <param name="totalSubcontractorsUsd">totalSubcontractorsUsd</param>
        /// <returns>CostingSheetID</returns>
        public int InsertDirect(int projectId, string name, DateTime startDate, DateTime endDate, decimal totalLabourHoursCad, decimal totalLabourHoursUsd, decimal totalMaterialsCad, decimal totalMaterialsUsd, decimal totalUnitsCad, decimal totalUnitsUsd, decimal totalOtherCostsCad, decimal totalOtherCostsUsd, decimal grandTotalCad, decimal grandTotalUsd, string state, bool deleted, int companyId, decimal totalSubcontractorsCad, decimal totalSubcontractorsUsd, decimal grandRevenue, decimal grandProfit, decimal grandGrossMargin)
        {
            // Insert costing sheet and get costing sheet ID
            ProjectCostingSheetGateway projectCostingSheetGateway = new ProjectCostingSheetGateway(null);
            int costingSheetId = projectCostingSheetGateway.Insert(projectId, name, startDate, endDate, totalLabourHoursCad, totalLabourHoursUsd, totalMaterialsCad, totalMaterialsUsd, totalUnitsCad, totalUnitsUsd, totalOtherCostsCad, totalOtherCostsUsd, grandTotalCad, grandTotalUsd, state, deleted, companyId, totalSubcontractorsCad, totalSubcontractorsUsd, grandRevenue, grandProfit, grandGrossMargin);

            return costingSheetId;
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!Convert.ToBoolean(Session["sgLFS_PROJECTS_COSTINGSHEETS_ADMIN"]))
                {
                    if (!(Convert.ToBoolean(Session["sgLFS_PROJECTS_COSTINGSHEETS_VIEW"]) && Convert.ToBoolean(Session["sgLFS_PROJECTS_COSTINGSHEETS_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["costing_sheet_id"] == null)  || ((string)Request.QueryString["costing_sheet_state"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in project_costing_sheets_state.aspx");
                }

                // Tag Page
                hdfCostingSheetId.Value = Request.QueryString["costing_sheet_id"].ToString();
                hdfCostingSheetState.Value = Request.QueryString["costing_sheet_state"].ToString();
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfProjectId.Value = Request.QueryString["project_id"];
                hdfDataChanged.Value = Request.QueryString["data_changed"];
                hdfDataChangedMessage.Value = "Changes made to this project will not be saved.";
                ViewState["state"] = Request.QueryString["state"];
                ViewState["active_tab"] = Request.QueryString["active_tab"];
                ViewState["origin"] = Request.QueryString["origin"];
                ViewState["update"] = Request.QueryString["update"];
                int projectId = Int32.Parse(hdfProjectId.Value);

                projectTDS = (ProjectTDS)Session["lfsProjectTDS"];

                // If coming from project_costing_sheets_summary.aspx
                if ((string)Request.QueryString["source_page"] == "project_costing_sheets_summary.aspx")
                {
                    ViewState["update"] = Request.QueryString["update"];

                    int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                    int costingSheetId = Int32.Parse(hdfCostingSheetId.Value.Trim());

                    projectCostingSheetInformationTDS = (ProjectCostingSheetInformationTDS)Session["projectCostingSheetInformationTDS"];
                    ProjectCostingSheetInformationBasicInformationGateway projectCostingSheetInformationBasicInformationGateway = new ProjectCostingSheetInformationBasicInformationGateway(projectCostingSheetInformationTDS);
                    projectCostingSheetInformationBasicInformationGateway.LoadByCostingSheetId(costingSheetId, companyId);

                    // Store dataset
                    Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;
                }

                // Restore dataset
                projectCostingSheetInformationTDS = (ProjectCostingSheetInformationTDS)Session["projectCostingSheetInformationTDS"];

                // Costing Sheet state check
                ProjectCostingSheetInformationBasicInformationGateway projectCostingSheetInformationBasicInformationGatewayToValidate = new ProjectCostingSheetInformationBasicInformationGateway(projectCostingSheetInformationTDS);

                DateTime startDate = projectCostingSheetInformationBasicInformationGatewayToValidate.GetStartDate(Int32.Parse(hdfCostingSheetId.Value));
                DateTime endDate = projectCostingSheetInformationBasicInformationGatewayToValidate.GetEndDate(Int32.Parse(hdfCostingSheetId.Value));

                ProjectCostingSheetGateway projectCostingSheetGateway = new ProjectCostingSheetGateway();
                if (projectCostingSheetGateway.ExistCostingSheetApproved(projectId, startDate, endDate))
                {
                    string msg = "You cannot approve this costing sheet. There are other costing sheets already approved on the same date range. There is an overlaping issue between yours costing sheets.";
                    Response.Redirect("./../../error_page.aspx?error=" + msg);
                }
            }
            else
            {
                // Restore dataset
                projectTDS = (ProjectTDS)Session["lfsProjectTDS"];
                projectCostingSheetInformationTDS = (ProjectCostingSheetInformationTDS)Session["projectCostingSheetInformationTDS"];
            }
        }
 /// <summary>
 /// UpdateStateDirect
 /// </summary>
 /// <param name="originalcostingSheetId">originalcostingSheetId</param>
 /// <param name="originalCompanyId">originalCompanyId</param>
 /// <param name="newState">newState</param>
 public void UpdateStateDirect(int originalcostingSheetId, int originalCompanyId, string newState)
 {
     ProjectCostingSheetGateway projectCostingSheetGateway = new ProjectCostingSheetGateway(null);
     projectCostingSheetGateway.UpdateState(originalcostingSheetId, originalCompanyId, newState);
 }
 /// <summary>
 /// DeleteDirect
 /// </summary>
 /// <param name="costingSheetId">costingSheetId</param>
 /// <param name="companyId">companyId</param>
 public void DeleteDirect(int costingSheetId, int companyId)
 {
     ProjectCostingSheetGateway projectCostingSheetGateway = new ProjectCostingSheetGateway(null);
     projectCostingSheetGateway.Delete(costingSheetId, companyId);
 }
 /// <summary>
 /// Update costing sheet
 /// </summary>
 /// <param name="costingSheetId">costingSheetId</param>
 /// <param name="originalProjectId">originalProjectId</param>
 /// <param name="originalName">originalName</param>
 /// <param name="originalStartDate">originalStartDate</param>
 /// <param name="originalEndDate">originalEndDate</param>
 /// <param name="originalTotalLabourHoursCad">originalTotalLabourHoursCad</param>
 /// <param name="originalTotalLabourHoursUsd">originalTotalLabourHoursUsd</param>
 /// <param name="originalTotalMaterialsCad">originalTotalMaterialsCad</param>
 /// <param name="originalTotalMaterialsUsd">originalTotalMaterialsUsd</param>
 /// <param name="originalTotalUnitsCad">originalTotalUnitsCad</param>
 /// <param name="originalTotalUnitsUsd">originalTotalUnitsUsd</param>
 /// <param name="originalTotalOtherCostsCad">originalTotalOtherCostsCad</param>
 /// <param name="originalTotalOtherCostsUsd">originalTotalOtherCostsUsd</param>
 /// <param name="originalGrandTotalCad">originalGrandTotalCad</param>
 /// <param name="originalGrandTotalUsd">originalGrandTotalUsd</param>
 /// <param name="originalState">originalState</param>
 /// <param name="originalDeleted">originalDeleted</param>
 /// <param name="originalCompanyId">originalCompanyId</param>
 /// <param name="originalTotalSubcontractorsCad">originalTotalSubcontractorsCad</param>
 /// <param name="originalTotalSubcontractorsUsd">originalTotalSubcontractorsUsd</param>
 /// 
 /// <param name="newProjectId">newProjectId</param>
 /// <param name="newName">newName</param>
 /// <param name="newStartDate">newStartDate</param>
 /// <param name="newEndDate">newEndDate</param>
 /// <param name="newTotalLabourHoursCad">newTotalLabourHoursCad</param>
 /// <param name="newTotalLabourHoursUsd">newTotalLabourHoursUsd</param>
 /// <param name="newTotalMaterialsCad">newTotalMaterialsCad</param>
 /// <param name="newTotalMaterialsUsd">newTotalMaterialsUsd</param>
 /// <param name="newTotalUnitsCad">newTotalUnitsCad</param>
 /// <param name="newTotalUnitsUsd">newTotalUnitsUsd</param>
 /// <param name="newTotalOtherCostsCad">newTotalOtherCostsCad</param>
 /// <param name="newTotalOtherCostsUsd">newTotalOtherCostsUsd</param>
 /// <param name="newGrandTotalCad">newGrandTotalCad</param>
 /// <param name="newGrandTotalUsd">newGrandTotalUsd</param>
 /// <param name="newState">newState</param>
 /// <param name="newDeleted">newDeleted</param>
 /// <param name="newCompanyId">newCompanyId</param>
 /// <param name="newTotalSubcontractorsCad">newTotalSubcontractorsCad</param>
 /// <param name="newTotalSubcontractorsUsd">newTotalSubcontractorsUsd</param>
 public void UpdateDirect(int costingSheetId, int originalProjectId, string originalName, DateTime originalStartDate, DateTime originalEndDate, decimal originalTotalLabourHoursCad, decimal originalTotalLabourHoursUsd, decimal originalTotalMaterialsCad, decimal originalTotalMaterialsUsd, decimal originalTotalUnitsCad, decimal originalTotalUnitsUsd, decimal originalTotalOtherCostsCad, decimal originalTotalOtherCostsUsd, decimal originalGrandTotalCad, decimal originalGrandTotalUsd, string originalState, bool originalDeleted, int originalCompanyId, decimal originalTotalSubcontractorsCad, decimal originalTotalSubcontractorsUsd, decimal originalGrandRevenue, decimal originalGrandProfit, decimal originalGrandGrossMargin, int newProjectId, string newName, DateTime newStartDate, DateTime newEndDate, decimal newTotalLabourHoursCad, decimal newTotalLabourHoursUsd, decimal newTotalMaterialsCad, decimal newTotalMaterialsUsd, decimal newTotalUnitsCad, decimal newTotalUnitsUsd, decimal newTotalOtherCostsCad, decimal newTotalOtherCostsUsd, decimal newGrandTotalCad, decimal newGrandTotalUsd, string newState, bool newDeleted, int newCompanyId, decimal newTotalSubcontractorsCad, decimal newTotalSubcontractorsUsd, decimal newGrandRevenue, decimal newGrandProfit, decimal newGrandGrossMargin)
 {
     ProjectCostingSheetGateway projectCostingSheetGateway = new ProjectCostingSheetGateway(null);
     projectCostingSheetGateway.Update(costingSheetId, originalProjectId, originalName, originalStartDate, originalEndDate, originalTotalLabourHoursCad, originalTotalLabourHoursUsd, originalTotalMaterialsCad, originalTotalMaterialsUsd, originalTotalUnitsCad, originalTotalUnitsUsd, originalTotalOtherCostsCad, originalTotalOtherCostsUsd, originalGrandTotalCad, originalGrandTotalUsd, originalState, originalDeleted, originalCompanyId, originalTotalSubcontractorsCad, originalTotalSubcontractorsUsd, originalGrandRevenue, originalGrandProfit, originalGrandGrossMargin, newProjectId, newName, newStartDate, newEndDate, newTotalLabourHoursCad, newTotalLabourHoursUsd, newTotalMaterialsCad, newTotalMaterialsUsd, newTotalUnitsCad, newTotalUnitsUsd, newTotalOtherCostsCad, newTotalOtherCostsUsd, newGrandTotalCad, newGrandTotalUsd, newState, newDeleted, newCompanyId, newTotalSubcontractorsCad, newTotalSubcontractorsUsd, newGrandRevenue, newGrandProfit, newGrandGrossMargin);
 }