private function exportar_xml()
 {
     /// desactivamos el motor de plantillas
     $this->template = FALSE;
     /// creamos el xml
     $cadena_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!--\n    Document   : ejercicio_" . $this->ejercicio->codejercicio . ".xml\n    Description:\n        Estructura de grupos de epígrafes, epígrafes, cuentas y subcuentas del ejercicio " . $this->ejercicio->codejercicio . ".\n-->\n\n<ejercicio>\n</ejercicio>\n";
     $archivo_xml = simplexml_load_string($cadena_xml);
     /// añadimos los balances
     $balance = new balance();
     foreach ($balance->all() as $ba) {
         $aux = $archivo_xml->addChild("balance");
         $aux->addChild("codbalance", $ba->codbalance);
         $aux->addChild("naturaleza", $ba->naturaleza);
         $aux->addChild("nivel1", $ba->nivel1);
         $aux->addChild("descripcion1", base64_encode($ba->descripcion1));
         $aux->addChild("nivel2", $ba->nivel2);
         $aux->addChild("descripcion2", base64_encode($ba->descripcion2));
         $aux->addChild("nivel3", $ba->nivel3);
         $aux->addChild("descripcion3", base64_encode($ba->descripcion3));
         $aux->addChild("orden3", $ba->orden3);
         $aux->addChild("nivel4", $ba->nivel4);
         $aux->addChild("descripcion4", base64_encode($ba->descripcion4));
         $aux->addChild("descripcion4ba", base64_encode($ba->descripcion4ba));
     }
     /// añadimos las cuentas de balances
     $balance_cuenta = new balance_cuenta();
     foreach ($balance_cuenta->all() as $ba) {
         $aux = $archivo_xml->addChild("balance_cuenta");
         $aux->addChild("codbalance", $ba->codbalance);
         $aux->addChild("codcuenta", $ba->codcuenta);
         $aux->addChild("descripcion", base64_encode($ba->desccuenta));
     }
     /// añadimos las cuentas de balance abreviadas
     $balance_cuenta_a = new balance_cuenta_a();
     foreach ($balance_cuenta_a->all() as $ba) {
         $aux = $archivo_xml->addChild("balance_cuenta_a");
         $aux->addChild("codbalance", $ba->codbalance);
         $aux->addChild("codcuenta", $ba->codcuenta);
         $aux->addChild("descripcion", base64_encode($ba->desccuenta));
     }
     /// añadimos las cuentas especiales
     $cuenta_esp = new cuenta_especial();
     foreach ($cuenta_esp->all() as $ce) {
         $aux = $archivo_xml->addChild("cuenta_especial");
         $aux->addChild("idcuentaesp", $ce->idcuentaesp);
         $aux->addChild("descripcion", base64_encode($ce->descripcion));
     }
     /// añadimos los Titulos  -- pgrupo
     $pgrupo_epigrafes = new pgrupo_epigrafes();
     $pgrupos_ep = $pgrupo_epigrafes->all_from_ejercicio($this->ejercicio->codejercicio);
     foreach ($pgrupos_ep as $ge) {
         $aux = $archivo_xml->addChild("pgrupo_epigrafes");
         $aux->addChild("codpgrupo", $ge->codpgrupo);
         $aux->addChild("descripcion", base64_encode($ge->descripcion));
     }
     /// añadimos los grupos de epigrafes
     $grupo_epigrafes = new grupo_epigrafes();
     $grupos_ep = $grupo_epigrafes->all_from_ejercicio($this->ejercicio->codejercicio);
     foreach ($grupos_ep as $ge) {
         $aux = $archivo_xml->addChild("grupo_epigrafes");
         $aux->addChild("codgrupo", $ge->codgrupo);
         $aux->addChild("descripcion", base64_encode($ge->descripcion));
         $aux->addChild("codpgrupo", $ge->codpgrupo);
         $aux->addChild("idpgrupo", $ge->idpgrupo);
     }
     /// añadimos los epigrafes
     $epigrafe = new epigrafe();
     foreach ($epigrafe->all_from_ejercicio($this->ejercicio->codejercicio) as $ep) {
         $aux = $archivo_xml->addChild("epigrafe");
         $aux->addChild("codgrupo", $ep->codgrupo);
         $aux->addChild("codpadre", $ep->codpadre());
         $aux->addChild("codepigrafe", $ep->codepigrafe);
         $aux->addChild("descripcion", base64_encode($ep->descripcion));
     }
     /// añadimos las cuentas
     $cuenta = new cuenta();
     $num = 0;
     $cuentas = $cuenta->full_from_ejercicio($this->ejercicio->codejercicio);
     foreach ($cuentas as $c) {
         $aux = $archivo_xml->addChild("cuenta");
         $aux->addChild("codepigrafe", $c->codepigrafe);
         $aux->addChild("codcuenta", $c->codcuenta);
         $aux->addChild("descripcion", base64_encode($c->descripcion));
         $aux->addChild("idcuentaesp", $c->idcuentaesp);
     }
     /// añadimos las subcuentas
     $subcuenta = new subcuenta();
     foreach ($subcuenta->all_from_ejercicio($this->ejercicio->codejercicio) as $sc) {
         $aux = $archivo_xml->addChild("subcuenta");
         $aux->addChild("codcuenta", $sc->codcuenta);
         $aux->addChild("codsubcuenta", $sc->codsubcuenta);
         $aux->addChild("descripcion", base64_encode($sc->descripcion));
         $aux->addChild("coddivisa", $sc->coddivisa);
     }
     /// volcamos el XML
     header("content-type: application/xml; charset=UTF-8");
     header('Content-Disposition: attachment; filename="ejercicio_' . $this->ejercicio->codejercicio . '.xml"');
     echo $archivo_xml->asXML();
 }
 protected function private_core()
 {
     $this->codejercicio = $this->empresa->codejercicio;
     $this->ejercicio = new ejercicio();
     $pgrupo0 = new pgrupo_epigrafes();
     $grupo0 = new grupo_epigrafes();
     $epi0 = new epigrafe();
     $this->super_epigrafes = array();
     /// ¿El usuario tiene permiso para eliminar en esta página?
     $this->allow_delete = $this->user->allow_delete_on(__CLASS__);
     /* PGrupo    Título  */
     if (isset($_POST['npgrupo'])) {
         $this->epigrafe = FALSE;
         $this->pgrupo = $pgrupo0->get_by_codigo($_GET['ngrupo'], $_POST['ejercicio']);
         if (!$this->pgrupo) {
             $this->pgrupo = new pgrupo_epigrafes();
             $this->pgrupo->codejercicio = $_POST['ejercicio'];
             $this->pgrupo->codpgrupo = $_POST['npgrupo'];
             $this->pgrupo->descripcion = $_POST['descripcion'];
             if ($this->pgrupo->save()) {
                 header('Location: ' . $this->pgrupo->url());
             } else {
                 $this->new_error_msg('Error al guardar el Título.');
                 $this->pgrupo = FALSE;
             }
         }
     } else {
         if (isset($_GET['pgrupo'])) {
             $this->epigrafe = FALSE;
             $this->pgrupo = $pgrupo0->get($_GET['pgrupo']);
             if ($this->pgrupo and isset($_POST['descripcion'])) {
                 $this->pgrupo->descripcion = $_POST['descripcion'];
                 if ($this->pgrupo->save()) {
                     $this->new_message('Título modificado correctamente.');
                 } else {
                     $this->new_error_msg('Error al modificar el Título.');
                 }
             }
         } else {
             if (isset($_GET['deletepg'])) {
                 $pgrupo1 = $pgrupo0->get($_GET['deletepg']);
                 if ($pgrupo1) {
                     if ($pgrupo1->delete()) {
                         $this->new_message('Título eliminado correctamente.');
                     } else {
                         $this->new_error_msg('Error al eliminar el Título.');
                     }
                 } else {
                     $this->new_error_msg('Título no encontrado.');
                 }
                 //    $this->pgrupo = FALSE;
                 //     $this->epigrafe = FALSE;
             }
         }
     }
     /*  Capítulo  */
     if (isset($_POST['ngrupo'])) {
         $this->epigrafe = FALSE;
         $this->grupo = $grupo0->get_by_codigo($_GET['ngrupo'], $_POST['ejercicio']);
         if (!$this->grupo) {
             $this->grupo = new grupo_epigrafes();
             $this->grupo->codejercicio = $_POST['ejercicio'];
             $this->grupo->codgrupo = $_POST['ngrupo'];
             $this->grupo->idpgrupo = $_POST['idpgrupo'];
             $this->grupo->codpgrupo = $_POST['codpgrupo'];
             $this->grupo->descripcion = $_POST['descripcion'];
             if ($this->grupo->save()) {
                 header('Location: ' . $this->grupo->url());
             } else {
                 $this->new_error_msg('Error al guardar el Capítulo.');
                 $this->grupo = FALSE;
             }
         }
     } else {
         if (isset($_GET['grupo'])) {
             $this->epigrafe = FALSE;
             $this->grupo = $grupo0->get($_GET['grupo']);
             if ($this->grupo and isset($_POST['descripcion'])) {
                 $this->grupo->descripcion = $_POST['descripcion'];
                 if ($this->grupo->save()) {
                     $this->new_message('Capítulo modificado correctamente.');
                 } else {
                     $this->new_error_msg('Error al modificar el Capítulo.');
                 }
             }
         } else {
             if (isset($_GET['deleteg'])) {
                 $grupo1 = $grupo0->get($_GET['deleteg']);
                 if ($grupo1) {
                     if ($grupo1->delete()) {
                         $this->new_message('Capítulo eliminado correctamente.');
                     } else {
                         $this->new_error_msg('Error al eliminar el Capítulo.');
                     }
                 } else {
                     $this->new_error_msg('Capítulo no encontrado.');
                 }
                 $this->grupo = FALSE;
                 $this->epigrafe = FALSE;
             } else {
                 if (isset($_POST['nepigrafe'])) {
                     $this->epigrafe = $epi0->get_by_codigo($_POST['nepigrafe'], $_POST['ejercicio']);
                     if (!$this->epigrafe) {
                         $this->epigrafe = new epigrafe();
                         $this->epigrafe->codejercicio = $_POST['ejercicio'];
                         $this->epigrafe->codepigrafe = $_POST['nepigrafe'];
                         if (isset($_POST['idpadre'])) {
                             $this->epigrafe->idpadre = $_POST['idpadre'];
                         } else {
                             $this->epigrafe->codgrupo = $_POST['codgrupo'];
                             $this->epigrafe->idgrupo = $_POST['idgrupo'];
                             $this->grupo = $grupo0->get($_POST['idgrupo']);
                         }
                         $this->epigrafe->descripcion = $_POST['descripcion'];
                         if ($this->epigrafe->save()) {
                             header('Location: ' . $this->epigrafe->url());
                         } else {
                             $this->new_error_msg('Error al guardar el Rubro.');
                         }
                     }
                 } else {
                     if (isset($_GET['epi'])) {
                         $this->grupo = FALSE;
                         $this->epigrafe = $epi0->get($_GET['epi']);
                         if ($this->ejercicio and isset($_POST['descripcion'])) {
                             $this->epigrafe->descripcion = $_POST['descripcion'];
                             if ($this->epigrafe->save()) {
                                 $this->new_message('Rubro modificado correctamente.');
                             } else {
                                 $this->new_error_msg('Error al modificar el Rubro.');
                             }
                         }
                     } else {
                         if (isset($_GET['deletee'])) {
                             $epi1 = $epi0->get($_GET['deletee']);
                             if ($epi1) {
                                 $this->grupo = $grupo0->get($epi1->idgrupo);
                                 if ($epi1->delete()) {
                                     $this->new_message('Rubro eliminado correctamente.');
                                 } else {
                                     $this->new_error_msg('Error al eliminar el Rubro.');
                                 }
                             } else {
                                 $this->new_error_msg('Rubro no encontrado.');
                                 $this->grupo = FALSE;
                             }
                         } else {
                             if (isset($_POST['ncuenta'])) {
                                 $this->grupo = FALSE;
                                 $this->epigrafe = FALSE;
                                 $cuenta0 = new cuenta();
                                 $cuenta1 = $cuenta0->get_by_codigo($_POST['ncuenta'], $_POST['ejercicio']);
                                 if ($cuenta1) {
                                     header('Location: ' . $cuenta1->url());
                                 } else {
                                     $cuenta1 = new cuenta();
                                     $cuenta1->codcuenta = $_POST['ncuenta'];
                                     $cuenta1->codejercicio = $_POST['ejercicio'];
                                     $cuenta1->codepigrafe = $_POST['codepigrafe'];
                                     $cuenta1->descripcion = $_POST['descripcion'];
                                     $cuenta1->idepigrafe = $_POST['idepigrafe'];
                                     if ($cuenta1->save()) {
                                         header('Location: ' . $cuenta1->url());
                                     } else {
                                         $this->new_error_msg('Error al guardar la cuenta.');
                                     }
                                     $this->epigrafe = $epi0->get($_POST['idepigrafe']);
                                 }
                             } else {
                                 if (isset($_GET['deletec'])) {
                                     $this->grupo = FALSE;
                                     $this->epigrafe = FALSE;
                                     $cuenta0 = new cuenta();
                                     $cuenta1 = $cuenta0->get($_GET['deletec']);
                                     if ($cuenta1) {
                                         $this->epigrafe = $epi0->get($cuenta1->idepigrafe);
                                         if ($cuenta1->delete()) {
                                             $this->new_message('Cuenta eliminada correctamente.');
                                         } else {
                                             $this->new_error_msg('Error al eliminar la cuenta.');
                                         }
                                     } else {
                                         $this->new_error_msg('Cuenta no encontrada.');
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     /* /////////////////////////////////////////////////////
     ////////////////////////////////////////////////////////
     ////////////////////////////////////////////////////////
       */
     if ($this->pgrupo) {
         $this->ppage = $this->page->get($this->page->name);
         $this->page->title = 'Título: ' . $this->pgrupo->idpgrupo;
         $this->resultados = $this->pgrupo->get_grupo();
     } else {
         if ($this->grupo) {
             /// configuramos la página previa
             $this->ppage = $this->page->get($this->page->name);
             //       $this->grupo->codpgrupo='11';
             if (!is_null($this->grupo->idgrupo)) {
                 $this->ppage->title = 'Título: ' . $this->grupo->codpgrupo;
                 $this->ppage->extra_url = '&pgrupo=' . $this->grupo->idpgrupo;
             } else {
                 $this->page->title = 'Capítulo: ' . $this->grupo->codgrupo;
             }
             $this->resultados = $this->grupo->get_epigrafes();
         } else {
             if ($this->epigrafe) {
                 /// configuramos la página previa
                 $this->ppage = $this->page->get($this->page->name);
                 if (!is_null($this->epigrafe->idgrupo)) {
                     $this->ppage->title = 'Capítulo: ' . $this->epigrafe->codgrupo;
                     $this->ppage->extra_url = '&grupo=' . $this->epigrafe->idgrupo;
                 } else {
                     if (!is_null($this->epigrafe->idpadre)) {
                         $this->ppage->title = 'Padre';
                         $this->ppage->extra_url = '&epi=' . $this->epigrafe->idpadre;
                     }
                 }
                 $this->page->title = 'Rubro: ' . $this->epigrafe->codepigrafe;
                 $this->resultados = $this->epigrafe->get_cuentas();
             } else {
                 if (isset($_POST['ejercicio'])) {
                     $this->codejercicio = $_POST['ejercicio'];
                     $this->grupo = FALSE;
                     $this->epigrafe = FALSE;
                     $this->resultados = $grupo0->all_from_ejercicio($this->codejercicio);
                     $this->super_epigrafes = $epi0->super_from_ejercicio($this->codejercicio);
                 } else {
                     $this->grupo = FALSE;
                     $this->epigrafe = FALSE;
                     $this->resultados = $pgrupo0->all_from_ejercicio($this->empresa->codejercicio);
                     $this->super_epigrafes = $epi0->super_from_ejercicio($this->empresa->codejercicio);
                 }
             }
         }
     }
 }