Ejemplo n.º 1
0
    private string Procesar(string sForzar, string sAnno, string strDatos)
    {
        string    sResul = "";
        int       nSubnodo = 0, nPE = 0, nPSN = 0;//, nDesde = 0, nHasta = 0, nAux = 0
        Hashtable htSubnodos = new Hashtable();
        Hashtable htNodos    = new Hashtable();
        NODO      oNodoAux   = null;

        int[] nDatosNodo;
        int?  idFicepiValidador = null;

        try
        {
            #region abrir conexión y transacción
            try
            {
                oConn = Conexion.Abrir();
                tr    = Conexion.AbrirTransaccionSerializable(oConn);
            }
            catch (Exception ex)
            {
                if (oConn.State == ConnectionState.Open)
                {
                    Conexion.Cerrar(oConn);
                }
                sResul = "Error@#@" + Errores.mostrarError("Error al abrir la conexión", ex);
                return(sResul);
            }
            #endregion

            string[] aProyectos = Regex.Split(strDatos, "///");
            foreach (string oProyecto in aProyectos)
            {
                if (oProyecto == "")
                {
                    continue;
                }
                string[] aValores = Regex.Split(oProyecto, "##");
                /// aValores[0] = idNodo
                /// aValores[1] = idNaturaleza
                /// aValores[2] = FIV
                /// aValores[3] = FFV
                /// aValores[4] = idPlantilla
                /// aValores[5] = Denominación Naturaleza
                /// aValores[6] = Esreplicable
                ///
                /// aValores[7] = Hereda Nodo
                /// aValores[8] = Id Usuario responsable
                /// aValores[9] = Imputable GASVI
                /// aValores[10] = Id Ficepi Validador GASVI

                if (sForzar == "N" && PROYECTOSUBNODO.ExistePIG(tr, int.Parse(aValores[0]), int.Parse(aValores[1]), short.Parse(sAnno)))
                {
                    continue;
                }
                #region Datos de nodo y subnodo
                nDatosNodo = (int[])htSubnodos[int.Parse(aValores[0])];
                if (nDatosNodo == null)
                {
                    int nCountSubnodosManiobra2  = 0;
                    int idSubnodoManiobra2       = 0;
                    int nCountSubnodosNoManiobra = 0;
                    int idSubnodoNoManiobra      = 0;

                    DataSet dsSubNodos = SUBNODO.CatalogoActivos(tr, int.Parse(aValores[0]), true);
                    foreach (DataRow oSN in dsSubNodos.Tables[0].Rows)
                    {
                        if ((byte)oSN["t304_maniobra"] == 2)
                        {
                            nCountSubnodosManiobra2++;
                            idSubnodoManiobra2 = (int)oSN["t304_idsubnodo"];
                        }
                        else if ((byte)oSN["t304_maniobra"] == 0)
                        {
                            nCountSubnodosNoManiobra++;
                            idSubnodoNoManiobra = (int)oSN["t304_idsubnodo"];
                        }
                    }
                    dsSubNodos.Dispose();

                    //nSubnodo = SUBNODO.ObtenerSubnodoManiobra2(tr, int.Parse(aValores[0]));
                    NODO oNodo = NODO.Select(tr, int.Parse(aValores[0]));

                    if (nCountSubnodosNoManiobra == 1)
                    {
                        nSubnodo = idSubnodoNoManiobra;
                    }
                    else if (nCountSubnodosManiobra2 >= 1)
                    {
                        nSubnodo = idSubnodoManiobra2;
                    }
                    else
                    {
                        nSubnodo = SUBNODO.Insert(tr, "Improductivos genéricos", int.Parse(aValores[0]), 0, true, 2, oNodo.t314_idusuario_responsable, null);
                    }
                    htSubnodos.Add(int.Parse(aValores[0]), new int[3] {
                        int.Parse(aValores[0]),
                        nSubnodo,
                        oNodo.t314_idusuario_responsable
                    });
                    nDatosNodo = (int[])htSubnodos[int.Parse(aValores[0])];
                }


                oNodoAux = (NODO)htNodos[int.Parse(aValores[0])];
                if (oNodoAux == null)
                {
                    oNodoAux = NODO.Select(tr, int.Parse(aValores[0]));
                    htNodos.Add(int.Parse(aValores[0]), oNodoAux);
                }
                #endregion
                string sDenominacion = sAnno + " " + Utilidades.unescape(aValores[5]) + " (" + oNodoAux.t303_denabreviada + ")";
                int    nIdClientePIG = CLIENTE.ObtenerClientePIG(tr, int.Parse(aValores[0]));

                nPE = PROYECTO.Insert(tr, "A", sDenominacion.Substring(0, (sDenominacion.Length > 70) ? 70 : sDenominacion.Length), "",
                                      nIdClientePIG, null, null, int.Parse(aValores[1]), 4, DateTime.Parse(aValores[2]),
                                      DateTime.Parse(aValores[3]), "S", "J", "J", short.Parse(sAnno), false, false,
                                      (aValores[6] == "1")? true:false, null, false, null, null, Constantes.gIdNLO_Defecto);

                //nPSN = PROYECTOSUBNODO.Insert(tr, nPE, nDatosNodo[1], false, "C", true, nDatosNodo[2],
                //                            sAnno + " " + Utilidades.unescape(aValores[5]),
                //                            "X", "X", false, true, false, false, false, "", "", "", null, null, null, null,
                //                            null, null, false, 0);
                //Mikel 30/12/2011. Pongo como seudónimo del subnodo la misma denominación que para el proyecto
                if (aValores[10] != "" && aValores[10] != "null")
                {
                    idFicepiValidador = int.Parse(aValores[10]);
                }
                else
                {
                    idFicepiValidador = null;
                }
                //Mikel 02/02/2016 Los PIG deben llevar el admite recurso PST a cero
                nPSN = PROYECTOSUBNODO.Insert(tr, nPE, nDatosNodo[1], false, "C", (aValores[7] == "1") ? true : false,
                                              int.Parse(aValores[8]),                        //nDatosNodo[2],//Id Usuario Responsable
                                              sDenominacion.Substring(0, (sDenominacion.Length > 70) ? 70 : sDenominacion.Length),
                                              "X", "X", (aValores[9] == "1") ? true : false, //Imputable GASVI
                                              false,                                         //Admite recurso PST
                                              false, false, false, "", "", "", null, null, null, null, null,
                                              idFicepiValidador,                             //Id Ficepi Validador GASVI
                                              false, 0);

                //A falta de tener en cuenta si la naturaleza tiene plantilla.
                TAREAPSP.UpdateVigenciaByPSN(tr, nPSN, DateTime.Parse(aValores[2]), DateTime.Parse(aValores[3]));

                #region Grabación de plantilla
                if (aValores[4] != "0")
                {
                    //Hay que grabar la plantilla de PE.
                    int       iPos, iMargen, iPT = -1, iFase = -1, iActiv = -1, iTarea = -1, iHito = -1, iAux = -1, iOrden = 0, idItemHitoPl;
                    double    fDuracion;
                    decimal   fPresupuesto;
                    string    sTipo, sDesc, sFiniPL, sFfinPL, sFiniV, sFfinV, sAux, sAvisos, sIdTareaPL, sCad;
                    bool      bFacturable, bObligaEst, bAvanceAutomatico, bEstadoTarea;
                    ArrayList alTareas = new ArrayList();

                    PROYECTOSUBNODO.BorrarPTByPSN(tr, nPSN);

                    #region 1º Se insertan las filas de la estructura
                    SqlDataReader dr = PlantTarea.Catalogo(int.Parse(aValores[4]));
                    while (dr.Read())
                    {
                        sTipo = dr["Tipo"].ToString();
                        if (sTipo == "H")
                        {
                            sTipo = "HT";
                        }
                        sDesc   = Utilidades.escape(dr["Nombre"].ToString());
                        iMargen = int.Parse(dr["margen"].ToString());

                        //Si la linea es de hito compruebo si el hito es de tarea o no para actualizar la variable iTarea
                        if (sTipo == "HT" || sTipo == "HM" || sTipo == "HF")
                        {
                            switch (iMargen)
                            {
                            case 80:    //es un hito de tarea por lo que mantengo el código de tarea
                                break;

                            case 60:    //es un hito de fase y actividad o de tarea con actividad sin fase
                                if (iFase != -1)
                                {
                                    iTarea = -1;
                                }
                                break;

                            case 40:    //es un hito de fase o de tarea sin actividad ni fase o de actividad sin fase
                                if (iFase != -1)
                                {
                                    iTarea = -1;
                                    iActiv = -1;
                                }
                                else
                                {
                                    if (iActiv != -1)
                                    {
                                        iTarea = -1;
                                    }
                                }
                                break;

                            case 20:   //es un hito proyecto técnico
                            case 0:    //es un hito de proyecto económico
                                iTarea = -1;
                                iActiv = -1;
                                iFase  = -1;
                                break;
                            }
                        }

                        fDuracion    = 0;
                        sFiniPL      = ""; //¿alguno es obligatorio?
                        sFfinPL      = "";
                        sFiniV       = Fechas.primerDiaMes(DateTime.Today).ToShortDateString();
                        sFfinV       = "";
                        fPresupuesto = 0;
                        sIdTareaPL   = dr["t339_iditems"].ToString();

                        bFacturable = (bool)dr["t339_facturable"];

                        //if (sEstado != "D")
                        iOrden++;
                        //iOrden = int.Parse(aElem[8]);
                        //Si no ha cambiado la linea pero el orden actual es distinto del original hay que updatear la linea para actualizar el orden
                        switch (sTipo)
                        {
                        case "P":
                            iPT    = -1;
                            iFase  = -1;
                            iActiv = -1;
                            break;

                        case "F":
                            iFase  = -1;
                            iActiv = -1;
                            break;

                        case "A":
                            iActiv = -1;
                            if (iMargen != 40)
                            {
                                iFase = -1;
                            }
                            break;

                        case "T":
                            iTarea = -1;
                            if (iMargen == 40)
                            {
                                iFase = -1;
                            }
                            else
                            {
                                if (iMargen != 60)
                                {
                                    iFase  = -1;
                                    iActiv = -1;
                                }
                            }
                            break;

                        case "HT":
                        case "HF":
                        case "HM":
                            iHito = -1;    //int.Parse(aElem[7]);
                            break;
                        }

                        bObligaEst        = (bool)dr["obliga"];
                        bAvanceAutomatico = (bool)dr["avance"];
                        sAux = EstrProy.Insertar(tr, int.Parse(aValores[0]), nPE, nPSN, sTipo, sDesc, iPT, iFase, iActiv, iMargen, iOrden,
                                                 sFiniPL, sFfinPL, fDuracion, sFiniV, sFfinV, fPresupuesto,
                                                 bFacturable, bObligaEst, bAvanceAutomatico, "1", "", 0);

                        iPos    = sAux.IndexOf("##");
                        iAux    = int.Parse(sAux.Substring(0, iPos));
                        sAvisos = sAux.Substring(iPos + 2);

                        switch (sTipo)
                        {
                        case "P": iPT = iAux; break;

                        case "F": iFase = iAux; break;

                        case "A": iActiv = iAux; break;

                        case "T":
                            iTarea = iAux;
                            if (sIdTareaPL != "" && sIdTareaPL != "-1")
                            {
                                string[] aDatosAux = new string[] { sIdTareaPL, iAux.ToString() };
                                alTareas.Add(aDatosAux);
                                //Grabo los atributos estadísticos provenientes de la plantilla. iAux=código de tarea
                                TAREAPSP.InsertarAE(tr, int.Parse(sIdTareaPL), iAux);
                            }

                            //Hay que guardar las tareas que quedan pendientes, ya que luego hay que actualizar el estado en pantalla
                            bEstadoTarea = TAREAPSP.bFaltanValoresAE(tr, short.Parse(aValores[0]), iAux);
                            if (bEstadoTarea)
                            {
                                //actualizo el estado de la tarea
                                TAREAPSP.Modificar(tr, iTarea, sDesc, iPT, iActiv, iOrden, sFiniPL, sFfinPL, fDuracion, sFiniV,
                                                   sFfinV, (int)Session["UsuarioActual"], fPresupuesto, 2, bFacturable);
                                //sAvisos = "Se han insertado tareas que quedan en estado Pendiente ya que el C.R. tiene atributos estadísticos\nobligatorios para los que la tarea no tiene valores asignados";
                                //if (sTareasPendientes == "") sTareasPendientes = iAux.ToString();
                                //else sTareasPendientes += "//"+ iAux.ToString();
                            }
                            break;

                        case "HT":
                            iHito = iAux;
                            break;
                        }
                        if (sTipo.Substring(0, 1) == "H")
                        {
                            AsociarTareasHitos(tr, nPSN, iPT, iFase, iActiv, iTarea, iHito, iMargen);
                        }
                    }
                    dr.Close();
                    dr.Dispose();
                    #endregion

                    #region 2º Se insertan las filas de los hitos de cumplimiento discontinuo
                    dr = PlantTarea.CatalogoHitos(int.Parse(aValores[4]));
                    while (dr.Read())
                    {
                        sTipo        = "HM";
                        sDesc        = dr["t369_deshito"].ToString();
                        idItemHitoPl = (int)dr["t369_idhito"];
                        iOrden       = int.Parse(dr["t369_orden"].ToString());

                        sAux    = EstrProy.Insertar(tr, int.Parse(aValores[0]), nPE, nPSN, sTipo, sDesc, 0, 0, 0, 0, iOrden, "", "", 0, "", "", 0, false, false, false, "1", "", 0);
                        iPos    = sAux.IndexOf("##");
                        iAux    = int.Parse(sAux.Substring(0, iPos));
                        sAvisos = sAux.Substring(iPos + 2);
                        //Si es hito de cumplimiento discontinuo y se ha cargado desde plantilla hay que grabar sus tareas
                        if (sTipo == "HM")
                        {
                            if (idItemHitoPl > 0)
                            {
                                //Recojo las tareas de plantilla del código de hito en plantilla
                                sCad = HITOE_PLANT.fgListaTareasPlantilla(tr, idItemHitoPl);
                                string[] aElems2 = Regex.Split(sCad, @"##");
                                for (int j = 0; j < aElems2.Length; j++)
                                {
                                    sIdTareaPL = aElems2[j];
                                    if (sIdTareaPL != "" && sIdTareaPL != "-1")
                                    {
                                        //Identifico el código de tarea real asociado al codigo de tarea de plantilla
                                        for (int n = 0; n < alTareas.Count; n++)
                                        {
                                            if (((string[])alTareas[n])[0] == sIdTareaPL)
                                            {//Inserto la tarea del hito
                                                sCad   = ((string[])alTareas[n])[1];
                                                iTarea = int.Parse(sCad);
                                                EstrProy.InsertarTareaHito(tr, iAux, iTarea);
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }//while
                    dr.Close();
                    dr.Dispose();
                    #endregion
                }
                #endregion
            }

            Conexion.CommitTransaccion(tr);

            sResul = "OK";
        }
        catch (Exception ex)
        {
            Conexion.CerrarTransaccion(tr);
            sResul = "Error@#@" + Errores.mostrarError("Error al crear los proyectos improductivos genéricos.", ex, false);
        }
        finally
        {
            Conexion.Cerrar(oConn);
        }
        return(sResul);
    }