コード例 #1
0
        protected void btnGuardar_Click(object sender, EventArgs e)
        {
            if (Session["Usuario"] == null)
            {
                ScriptManager.RegisterStartupScript(Page, this.GetType(), "mykey", "CancelEdit();", true);
            }

            PlanificacionWCFClient objCierreByPlanWCF = new PlanificacionWCFClient();
            int idPlanificacion = 0;

            try
            {
                //if (Request.QueryString["objCierreContable"] != "")
                //    idPlanificacion = (int)ViewState["idPlanificacion"];

                if (VerificaInsertOrUpdate())
                {
                    List <GS_GetPlanDetalleToInsertResult> lstCierreByPlan =
                        (List <GS_GetPlanDetalleToInsertResult>)HttpContext.Current.Session["lstCierreByPlan"];
                    var periodo      = dpPeriodo.SelectedDate.Value.Month.ToString("D2") + "/" + dpPeriodo.SelectedDate.Value.Year;
                    var fechaInicial = lstCierreByPlan.Min(x => x.FechaCierre);
                    var fechaFinal   = lstCierreByPlan.Max(x => x.FechaCierre);

                    var idEmpresa     = ((Usuario_LoginResult)Session["Usuario"]).idEmpresa;
                    var codigoUsuario = ((Usuario_LoginResult)Session["Usuario"]).codigoUsuario;
                    var nroDocumento  = ((Usuario_LoginResult)Session["Usuario"]).nroDocumento;

                    idPlanificacion = objCierreByPlanWCF.PlanificacionCabecera_Insertar(idEmpresa, codigoUsuario, periodo, fechaInicial.Value, fechaFinal.Value, nroDocumento);

                    var lstToDb = lstCierreByPlan.Where(x => x.idPlanificacion == 0 && x.FechaCierre.HasValue);

                    if (lstCierreByPlan.Exists(x => !x.FechaCierre.HasValue))
                    {
                        rwmCierre.RadAlert("No se ha ingresado una fecha", 500, 100, "Validación de fechas", "");
                    }

                    foreach (var item in lstToDb)
                    {
                        objCierreByPlanWCF.PlanificacionDetalle_Insertar(idEmpresa, codigoUsuario, item.id_Modulo,
                                                                         idPlanificacion, item.FechaCierre.Value, item.Detalle, item.Observacion, item.Estado,
                                                                         codigoUsuario.ToString());
                    }
                }

                else

                {
                    List <GS_GetPlanDetalleToEditResult> lstCierreByPlan =
                        (List <GS_GetPlanDetalleToEditResult>)HttpContext.Current.Session["lstCierreByPlan"];

                    var periodo      = dpPeriodo.SelectedDate.Value.Month.ToString("D2") + "/" + dpPeriodo.SelectedDate.Value.Year;
                    var fechaInicial = lstCierreByPlan.Min(x => x.FechaCierre);
                    var fechaFinal   = lstCierreByPlan.Max(x => x.FechaCierre);

                    var idEmpresa     = ((Usuario_LoginResult)Session["Usuario"]).idEmpresa;
                    var codigoUsuario = ((Usuario_LoginResult)Session["Usuario"]).codigoUsuario;
                    var nroDocumento  = ((Usuario_LoginResult)Session["Usuario"]).nroDocumento;

                    idPlanificacion = Convert.ToInt32(ViewState["idPlanificacion"].ToString());

                    objCierreByPlanWCF.PlanificacionCabecera_Update(idEmpresa, codigoUsuario, idPlanificacion, fechaInicial.Value, fechaFinal.Value, nroDocumento);

                    var lstToDb = lstCierreByPlan.Where(x => x.idPlanificacion == 0 && x.FechaCierre.HasValue);
                    if (lstCierreByPlan.Exists(x => !x.FechaCierre.HasValue))
                    {
                        rwmCierre.RadAlert("No se ha ingresado una fecha", 500, 100, "Validación de fechas", "");
                    }

                    foreach (var item in lstToDb)
                    {
                        objCierreByPlanWCF.PlanificacionDetalle_Insertar(idEmpresa, codigoUsuario, item.id_Modulo,
                                                                         idPlanificacion, item.FechaCierre.Value, item.Detalle, item.Observacion, item.Estado.Value,
                                                                         codigoUsuario.ToString());
                    }
                }
                ScriptManager.RegisterStartupScript(Page, this.GetType(), "mykey", "CloseAndRebind();", true);
            }
            catch (Exception ex)
            {
                lblMensaje.Text     = "ERROR: " + ex.Message;
                lblMensaje.CssClass = "mensajeError";
            }
        }