protected function process()
 {
     $this->codejercicio = $this->empresa->codejercicio;
     $this->ejercicio = new ejercicio();
     $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__);
     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->descripcion = $_POST['descripcion'];
             if ($this->grupo->save()) {
                 header('Location: ' . $this->grupo->url());
             } else {
                 $this->new_error_msg('Error al guardar el grupo.');
                 $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('Grupo modificado correctamente.');
                 } else {
                     $this->new_error_msg('Error al modificar el grupo.');
                 }
             }
         } else {
             if (isset($_GET['deleteg'])) {
                 $grupo1 = $grupo0->get($_GET['deleteg']);
                 if ($grupo1) {
                     if ($grupo1->delete()) {
                         $this->new_message('Grupo eliminado correctamente.');
                     } else {
                         $this->new_error_msg('Error al eliminar el grupo.');
                     }
                 } else {
                     $this->new_error_msg('Grupo 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 epígrafe.');
                         }
                     }
                 } 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('Epígrafe modificado correctamente.');
                             } else {
                                 $this->new_error_msg('Error al modificar el epígrafe.');
                             }
                         }
                     } else {
                         if (isset($_GET['deletee'])) {
                             $epi1 = $epi0->get($_GET['deletee']);
                             if ($epi1) {
                                 $this->grupo = $grupo0->get($epi1->idgrupo);
                                 if ($epi1->delete()) {
                                     $this->new_message('Epígrafe eliminado correctamente.');
                                 } else {
                                     $this->new_error_msg('Error al eliminar el epígrafe.');
                                 }
                             } else {
                                 $this->new_error_msg('Epígrafe 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->grupo) {
         $this->ppage = $this->page->get($this->page->name);
         $this->page->title = 'Grupo: ' . $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 = 'Grupo: ' . $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 = 'Epigrafe: ' . $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 = $grupo0->all_from_ejercicio($this->empresa->codejercicio);
                 $this->super_epigrafes = $epi0->super_from_ejercicio($this->empresa->codejercicio);
             }
         }
     }
 }
 private function importar_xml()
 {
     $import_step = 0;
     $this->importar_url = FALSE;
     if (isset($_POST['fuente'])) {
         if (file_exists('tmp/' . FS_TMP_NAME . 'ejercicio.xml')) {
             unlink('tmp/' . FS_TMP_NAME . 'ejercicio.xml');
         }
         if ($_POST['fuente'] == 'archivo' and isset($_POST['archivo'])) {
             if (copy($_FILES['farchivo']['tmp_name'], 'tmp/' . FS_TMP_NAME . 'ejercicio.xml')) {
                 $import_step = 1;
                 $this->importar_url = $this->url() . '&importar=' . (1 + $import_step);
             } else {
                 $this->new_error_msg('Error al copiar el archivo.');
             }
         } else {
             if ($_POST['fuente'] != '') {
                 if (copy($_POST['fuente'], 'tmp/' . FS_TMP_NAME . 'ejercicio.xml')) {
                     $import_step = 1;
                     $this->importar_url = $this->url() . '&importar=' . (1 + $import_step);
                 } else {
                     $this->new_error_msg('Error al copiar el archivo.');
                 }
             } else {
                 $this->new_error_msg('Has seleccionado importar desde un archivo externo,
            pero no has seleccionado ningún archivo.');
             }
         }
     } else {
         if (isset($_GET['importar'])) {
             $import_step = intval($_GET['importar']);
             if ($import_step < 7) {
                 $this->importar_url = $this->url() . '&importar=' . (1 + $import_step);
             } else {
                 $this->new_advice('Datos importados correctamente &nbsp; <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>');
                 $import_step = 0;
             }
         }
     }
     if (file_exists('tmp/' . FS_TMP_NAME . 'ejercicio.xml') and $import_step > 0) {
         $offset = 0;
         if (isset($_GET['offset'])) {
             $offset = intval($_GET['offset']);
         }
         if ($offset == 0) {
             $this->new_message('Importando ejercicio: paso ' . $import_step . ' de 6 ...');
         } else {
             $this->new_message('Importando ejercicio: paso ' . $import_step . '.' . $offset / 1000 . ' de 6 ...');
         }
         $xml = simplexml_load_file('tmp/' . FS_TMP_NAME . 'ejercicio.xml');
         if ($xml) {
             if ($xml->balance and $import_step == 1) {
                 foreach ($xml->balance as $b) {
                     $balance = new balance();
                     if (!$balance->get($b->codbalance)) {
                         $balance->codbalance = $b->codbalance;
                         $balance->naturaleza = $b->naturaleza;
                         $balance->nivel1 = $b->nivel1;
                         $balance->descripcion1 = base64_decode($b->descripcion1);
                         $balance->nivel2 = $balance->intval($b->nivel2);
                         $balance->descripcion2 = base64_decode($b->descripcion2);
                         $balance->nivel3 = $b->nivel3;
                         $balance->descripcion3 = base64_decode($b->descripcion3);
                         $balance->orden3 = $b->orden3;
                         $balance->nivel4 = $b->nivel4;
                         $balance->descripcion4 = base64_decode($b->descripcion4);
                         $balance->descripcion4ba = base64_decode($b->descripcion4ba);
                         if (!$balance->save()) {
                             $this->importar_url = FALSE;
                         }
                     }
                 }
                 if ($xml->balance_cuenta) {
                     $balance_cuenta = new balance_cuenta();
                     $all_bcs = $balance_cuenta->all();
                     foreach ($xml->balance_cuenta as $bc) {
                         $encontrado = FALSE;
                         foreach ($all_bcs as $bc2) {
                             if ($bc2->codbalance == $bc->codbalance and $bc2->codcuenta == $bc->codcuenta) {
                                 $encontrado = TRUE;
                                 break;
                             }
                         }
                         if (!$encontrado) {
                             $new_bc = new balance_cuenta();
                             $new_bc->codbalance = $bc->codbalance;
                             $new_bc->codcuenta = $bc->codcuenta;
                             $new_bc->desccuenta = base64_decode($bc->descripcion);
                             if (!$new_bc->save()) {
                                 $this->importar_url = FALSE;
                             }
                         }
                     }
                 }
                 if ($xml->balance_cuenta_a) {
                     $balance_cuenta_a = new balance_cuenta_a();
                     $all_bcas = $balance_cuenta_a->all();
                     foreach ($xml->balance_cuenta_a as $bc) {
                         $encontrado = FALSE;
                         foreach ($all_bcas as $bc2) {
                             if ($bc2->codbalance == $bc->codbalance and $bc2->codcuenta == $bc->codcuenta) {
                                 $encontrado = TRUE;
                                 break;
                             }
                         }
                         if (!$encontrado) {
                             $new_bc = new balance_cuenta_a();
                             $new_bc->codbalance = $bc->codbalance;
                             $new_bc->codcuenta = $bc->codcuenta;
                             $new_bc->desccuenta = base64_decode($bc->descripcion);
                             if (!$new_bc->save()) {
                                 $this->importar_url = FALSE;
                             }
                         }
                     }
                 }
             }
             if ($import_step == 2) {
                 if ($xml->cuenta_especial) {
                     foreach ($xml->cuenta_especial as $ce) {
                         $cuenta_especial = new cuenta_especial();
                         if (!$cuenta_especial->get($ce->idcuentaesp)) {
                             $cuenta_especial->idcuentaesp = $ce->idcuentaesp;
                             $cuenta_especial->descripcion = base64_decode($ce->descripcion);
                             if (!$cuenta_especial->save()) {
                                 $this->importar_url = FALSE;
                             }
                         }
                     }
                 }
                 if ($xml->grupo_epigrafes) {
                     foreach ($xml->grupo_epigrafes as $ge) {
                         $grupo_epigrafes = new grupo_epigrafes();
                         if (!$grupo_epigrafes->get_by_codigo($ge->codgrupo, $this->ejercicio->codejercicio)) {
                             $grupo_epigrafes->codejercicio = $this->ejercicio->codejercicio;
                             $grupo_epigrafes->codgrupo = $ge->codgrupo;
                             $grupo_epigrafes->descripcion = base64_decode($ge->descripcion);
                             if (!$grupo_epigrafes->save()) {
                                 $this->importar_url = FALSE;
                             }
                         }
                     }
                 }
                 if ($xml->epigrafe) {
                     $grupo_epigrafes = new grupo_epigrafes();
                     foreach ($xml->epigrafe as $ep) {
                         $epigrafe = new epigrafe();
                         if (!$epigrafe->get_by_codigo($ep->codepigrafe, $this->ejercicio->codejercicio)) {
                             $ge = $grupo_epigrafes->get_by_codigo($ep->codgrupo, $this->ejercicio->codejercicio);
                             if ($ge) {
                                 /// si encuentra el grupo, lo añade con el grupo
                                 $epigrafe->idgrupo = $ge->idgrupo;
                                 $epigrafe->codgrupo = $ge->codgrupo;
                                 $epigrafe->codejercicio = $this->ejercicio->codejercicio;
                                 $epigrafe->codepigrafe = $ep->codepigrafe;
                                 $epigrafe->descripcion = base64_decode($ep->descripcion);
                                 if (!$epigrafe->save()) {
                                     $this->importar_url = FALSE;
                                 }
                             } else {
                                 if ($ep->codpadre) {
                                     $padre = $epigrafe->get_by_codigo($ep->codpadre, $this->ejercicio->codejercicio);
                                     if ($padre) {
                                         /// si encuentra al padre, lo añade con el padre
                                         $epigrafe->idpadre = $padre->idepigrafe;
                                         $epigrafe->codejercicio = $this->ejercicio->codejercicio;
                                         $epigrafe->codepigrafe = $ep->codepigrafe;
                                         $epigrafe->descripcion = base64_decode($ep->descripcion);
                                         if (!$epigrafe->save()) {
                                             $this->importar_url = FALSE;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             if ($xml->cuenta and $import_step == 3) {
                 $epigrafe = new epigrafe();
                 foreach ($xml->cuenta as $c) {
                     $cuenta = new cuenta();
                     if (!$cuenta->get_by_codigo($c->codcuenta, $this->ejercicio->codejercicio)) {
                         $ep = $epigrafe->get_by_codigo($c->codepigrafe, $this->ejercicio->codejercicio);
                         if ($ep) {
                             $cuenta->idepigrafe = $ep->idepigrafe;
                             $cuenta->codepigrafe = $ep->codepigrafe;
                             $cuenta->codcuenta = $c->codcuenta;
                             $cuenta->codejercicio = $this->ejercicio->codejercicio;
                             $cuenta->descripcion = base64_decode($c->descripcion);
                             $cuenta->idcuentaesp = $c->idcuentaesp;
                             if (!$cuenta->save()) {
                                 $this->importar_url = FALSE;
                             }
                         }
                     }
                 }
             }
             if ($xml->subcuenta and $import_step == 4) {
                 $cuenta = new cuenta();
                 foreach ($xml->subcuenta as $sc) {
                     $subcuenta = new subcuenta();
                     if (!$subcuenta->get_by_codigo($sc->codsubcuenta, $this->ejercicio->codejercicio)) {
                         $cu = $cuenta->get_by_codigo($sc->codcuenta, $this->ejercicio->codejercicio);
                         if ($cu) {
                             $subcuenta->idcuenta = $cu->idcuenta;
                             $subcuenta->codcuenta = $cu->codcuenta;
                             $subcuenta->coddivisa = $this->empresa->coddivisa;
                             if (isset($sc->coddivisa)) {
                                 $subcuenta->coddivisa = $sc->coddivisa;
                             }
                             $subcuenta->codejercicio = $this->ejercicio->codejercicio;
                             $subcuenta->codsubcuenta = $sc->codsubcuenta;
                             $subcuenta->descripcion = base64_decode($sc->descripcion);
                             if (!$subcuenta->save()) {
                                 $this->importar_url = FALSE;
                             }
                         }
                     }
                 }
             }
             if ($import_step == 5) {
                 $cliente = new cliente();
                 $clientes = $cliente->all($offset);
                 while ($clientes) {
                     foreach ($clientes as $cli) {
                         /// forzamos la generación y asociación de una subcuenta para el cliente
                         $cli->get_subcuenta($this->ejercicio->codejercicio);
                         $offset++;
                     }
                     if ($offset % 1000 == 0) {
                         /// cada 1000 clientes volvemos a recargar la página para continuar
                         $this->importar_url = $this->url() . '&importar=' . $import_step . '&offset=' . $offset;
                         break;
                     } else {
                         $clientes = $cliente->all($offset);
                     }
                 }
             }
             if ($import_step == 6) {
                 $proveedor = new proveedor();
                 $proveedores = $proveedor->all($offset);
                 while ($proveedores) {
                     foreach ($proveedores as $pro) {
                         /// forzamos la generación y asociación de una subcuenta para cada proveedor
                         $pro->get_subcuenta($this->ejercicio->codejercicio);
                         $offset++;
                     }
                     if ($offset % 1000 == 0) {
                         /// cada 1000 proveedores volvemos a recargar la página para continuar
                         $this->importar_url = $this->url() . '&importar=' . $import_step . '&offset=' . $offset;
                         break;
                     } else {
                         $proveedores = $proveedor->all($offset);
                     }
                 }
             }
         } else {
             $this->new_error("Imposible leer el archivo.");
         }
     }
 }
 /**
  * Función auxiliar para generar el balance de sumas y saldos de un ejercicio y unas fechas concretas.
  * Este informe muestra los saldos (distintos de cero) de cada cuenta y subcuenta
  * por periodos, pero siempre excluyendo los asientos de cierre y pérdidas y ganancias.
  */
 public function sumas_y_saldos(&$pdf_doc, &$eje, $titulo, $fechaini, $fechafin, $excluir = FALSE, $np = TRUE)
 {
     $ge0 = new grupo_epigrafes();
     $epi0 = new epigrafe();
     $cuenta0 = new cuenta();
     $subcuenta0 = new subcuenta();
     $lineas = array();
     $sql = "SELECT p.codsubcuenta, SUM(p.debe) as debe, SUM(p.haber) as haber" . " FROM co_partidas p, co_asientos a WHERE p.idasiento = a.idasiento" . " AND a.codejercicio = " . $this->empresa->var2str($eje->codejercicio) . " AND a.fecha >= " . $this->empresa->var2str($fechaini) . " AND fecha <= " . $this->empresa->var2str($fechafin);
     if ($excluir) {
         foreach ($excluir as $exc) {
             $sql .= " AND p.idasiento != " . $this->empresa->var2str($exc);
         }
     }
     $sql .= " GROUP BY p.codsubcuenta ORDER BY codsubcuenta ASC;";
     $data = $this->db->select($sql);
     if ($data) {
         $grupos = $ge0->all_from_ejercicio($eje->codejercicio);
         $epigrafes = $epi0->all_from_ejercicio($eje->codejercicio);
         for ($i = 1; $i < 10; $i++) {
             $debe = 0;
             $haber = 0;
             foreach ($data as $d) {
                 if (substr($d['codsubcuenta'], 0, 1) == (string) $i) {
                     $debe += floatval($d['debe']);
                     $haber += floatval($d['haber']);
                 }
             }
             /// añadimos el grupo
             foreach ($grupos as $ge) {
                 if ($ge->codgrupo == $i) {
                     $lineas[] = array('cuenta' => $i, 'descripcion' => $ge->descripcion, 'debe' => $debe, 'haber' => $haber);
                     break;
                 }
             }
             for ($j = 0; $j < 10; $j++) {
                 $debe = 0;
                 $haber = 0;
                 foreach ($data as $d) {
                     if (substr($d['codsubcuenta'], 0, 2) == (string) $i . $j) {
                         $debe += floatval($d['debe']);
                         $haber += floatval($d['haber']);
                     }
                 }
                 /// añadimos el epígrafe
                 foreach ($epigrafes as $ep) {
                     if ($ep->codepigrafe == (string) $i . $j) {
                         $lineas[] = array('cuenta' => $i . $j, 'descripcion' => $ep->descripcion, 'debe' => $debe, 'haber' => $haber);
                         break;
                     }
                 }
                 for ($k = 0; $k < 10; $k++) {
                     $debe = 0;
                     $haber = 0;
                     foreach ($data as $d) {
                         if (substr($d['codsubcuenta'], 0, 3) == (string) $i . $j . $k) {
                             $debe += floatval($d['debe']);
                             $haber += floatval($d['haber']);
                         }
                     }
                     /// añadimos la cuenta
                     if ($debe != 0 or $haber != 0) {
                         $cuenta = $cuenta0->get_by_codigo($i . $j . $k, $eje->codejercicio);
                         if ($cuenta) {
                             $lineas[] = array('cuenta' => $i . $j . $k, 'descripcion' => $cuenta->descripcion, 'debe' => $debe, 'haber' => $haber);
                         } else {
                             $lineas[] = array('cuenta' => $i . $j . $k, 'descripcion' => '-', 'debe' => $debe, 'haber' => $haber);
                         }
                     }
                     /// añadimos las subcuentas
                     foreach ($data as $d) {
                         if (substr($d['codsubcuenta'], 0, 3) == (string) $i . $j . $k) {
                             $desc = '';
                             $subc = $subcuenta0->get_by_codigo($d['codsubcuenta'], $eje->codejercicio);
                             if ($subc) {
                                 $desc = $subc->descripcion;
                             }
                             $lineas[] = array('cuenta' => $d['codsubcuenta'], 'descripcion' => $desc, 'debe' => floatval($d['debe']), 'haber' => floatval($d['haber']));
                         }
                     }
                 }
             }
         }
     }
     /// a partir de la lista generamos el documento
     $linea = 0;
     $tdebe = 0;
     $thaber = 0;
     while ($linea < count($lineas)) {
         if ($linea > 0) {
             $pdf_doc->pdf->ezNewPage();
         }
         $pdf_doc->pdf->ezText($this->empresa->nombre . " - Balance de sumas y saldos " . $eje->year() . ' ' . $titulo . ".\n\n", 12);
         /// Creamos la tabla con las lineas
         $pdf_doc->new_table();
         $pdf_doc->add_table_header(array('cuenta' => '<b>Cuenta</b>', 'descripcion' => '<b>Descripción</b>', 'debe' => '<b>Debe</b>', 'haber' => '<b>Haber</b>', 'saldo' => '<b>Saldo</b>'));
         for ($i = $linea; $i < min(array($linea + 48, count($lineas))); $i++) {
             if (strlen($lineas[$i]['cuenta']) == 1) {
                 $a = '<b>';
                 $b = '</b>';
                 $tdebe += $lineas[$i]['debe'];
                 $thaber += $lineas[$i]['haber'];
             } else {
                 if (strlen($lineas[$i]['cuenta']) == 2) {
                     $a = $b = '';
                 } else {
                     $a = '<i>';
                     $b = '</i>';
                 }
             }
             $pdf_doc->add_table_row(array('cuenta' => $a . $lineas[$i]['cuenta'] . $b, 'descripcion' => $a . substr($lineas[$i]['descripcion'], 0, 50) . $b, 'debe' => $a . $this->show_numero($lineas[$i]['debe']) . $b, 'haber' => $a . $this->show_numero($lineas[$i]['haber']) . $b, 'saldo' => $a . $this->show_numero(floatval($lineas[$i]['debe']) - floatval($lineas[$i]['haber'])) . $b));
         }
         $linea += 48;
         /// añadimos las sumas de la línea actual
         $desc = 'Suma y sigue';
         if ($linea >= count($lineas)) {
             $desc = 'Totales';
         }
         $pdf_doc->add_table_row(array('cuenta' => '', 'descripcion' => '<b>' . $desc . '</b>', 'debe' => '<b>' . $this->show_numero($tdebe) . '</b>', 'haber' => '<b>' . $this->show_numero($thaber) . '</b>', 'saldo' => '<b>' . $this->show_numero($tdebe - $thaber) . '</b>'));
         $pdf_doc->save_table(array('fontSize' => 9, 'cols' => array('debe' => array('justification' => 'right'), 'haber' => array('justification' => 'right'), 'saldo' => array('justification' => 'right')), 'width' => 540, 'shaded' => 0));
     }
 }
 public function __construct($e = FALSE)
 {
     parent::__construct('co_epigrafes');
     if ($e) {
         $this->idepigrafe = $this->intval($e['idepigrafe']);
         $this->idpadre = $this->intval($e['idpadre']);
         $this->codepigrafe = $e['codepigrafe'];
         $this->idgrupo = $this->intval($e['idgrupo']);
         $this->descripcion = $e['descripcion'];
         $this->codejercicio = $e['codejercicio'];
         if (!isset(self::$grupos)) {
             $ge = new grupo_epigrafes();
             self::$grupos = $ge->all_from_ejercicio($this->codejercicio);
         }
         foreach (self::$grupos as $g) {
             if ($g->idgrupo == $this->idgrupo) {
                 $this->codgrupo = $g->codgrupo;
                 break;
             }
         }
     } else {
         $this->idepigrafe = NULL;
         $this->idpadre = NULL;
         $this->codepigrafe = NULL;
         $this->idgrupo = NULL;
         $this->codgrupo = NULL;
         $this->descripcion = NULL;
         $this->codejercicio = NULL;
     }
 }
 public function get_epigrafes()
 {
     $epigrafe = new \epigrafe();
     return $epigrafe->all_from_grupo($this->idgrupo);
 }
 private function importar_xml()
 {
     $import_step = 0;
     $this->importar_url = FALSE;
     if (isset($_POST['fuente'])) {
         if (file_exists('tmp/' . FS_TMP_NAME . 'ejercicio.xml')) {
             unlink('tmp/' . FS_TMP_NAME . 'ejercicio.xml');
         }
         if (in_array($_POST['fuente'], array('espanya', 'colombia', 'panama', 'peru', 'venezuela'))) {
             copy('extras/' . $_POST['fuente'] . '.xml', 'tmp/' . FS_TMP_NAME . 'ejercicio.xml');
         } else {
             if ($_POST['fuente'] == 'archivo' and isset($_POST['archivo'])) {
                 copy($_FILES['farchivo']['tmp_name'], 'tmp/' . FS_TMP_NAME . 'ejercicio.xml');
             } else {
                 $this->new_error_msg('Has seleccionado importar desde un archivo externo,
            pero no has seleccionado ningún archivo.');
             }
         }
         $import_step = 1;
         $this->importar_url = $this->url() . '&importar=' . (1 + $import_step);
     } else {
         if (isset($_GET['importar'])) {
             $import_step = intval($_GET['importar']);
             if ($import_step < 7) {
                 $this->importar_url = $this->url() . '&importar=' . (1 + $import_step);
             } else {
                 $this->new_message('Datos importados correctamente.');
                 $this->new_message("Ahora es el momento de <a href='index.php?page=ventas_clientes#nuevo'>" . "añadir algún cliente</a>, si todavía no lo has hecho.");
                 $import_step = 0;
             }
         }
     }
     if (file_exists('tmp/' . FS_TMP_NAME . 'ejercicio.xml') and $import_step > 0) {
         $this->new_message('Importando ejercicio: paso ' . $import_step . ' de 6 ...');
         $xml = simplexml_load_file('tmp/' . FS_TMP_NAME . 'ejercicio.xml');
         if ($xml) {
             if ($xml->balance and $import_step == 1) {
                 foreach ($xml->balance as $b) {
                     $balance = new balance();
                     if (!$balance->get($b->codbalance)) {
                         $balance->codbalance = $b->codbalance;
                         $balance->naturaleza = $b->naturaleza;
                         $balance->nivel1 = $b->nivel1;
                         $balance->descripcion1 = base64_decode($b->descripcion1);
                         $balance->nivel2 = $balance->intval($b->nivel2);
                         $balance->descripcion2 = base64_decode($b->descripcion2);
                         $balance->nivel3 = $b->nivel3;
                         $balance->descripcion3 = base64_decode($b->descripcion3);
                         $balance->orden3 = $b->orden3;
                         $balance->nivel4 = $b->nivel4;
                         $balance->descripcion4 = base64_decode($b->descripcion4);
                         $balance->descripcion4ba = base64_decode($b->descripcion4ba);
                         if (!$balance->save()) {
                             $this->importar_url = FALSE;
                         }
                     }
                 }
                 if ($xml->balance_cuenta) {
                     $balance_cuenta = new balance_cuenta();
                     $all_bcs = $balance_cuenta->all();
                     foreach ($xml->balance_cuenta as $bc) {
                         $encontrado = FALSE;
                         foreach ($all_bcs as $bc2) {
                             if ($bc2->codbalance == $bc->codbalance and $bc2->codcuenta == $bc->codcuenta) {
                                 $encontrado = TRUE;
                                 break;
                             }
                         }
                         if (!$encontrado) {
                             $new_bc = new balance_cuenta();
                             $new_bc->codbalance = $bc->codbalance;
                             $new_bc->codcuenta = $bc->codcuenta;
                             $new_bc->desccuenta = base64_decode($bc->descripcion);
                             if (!$new_bc->save()) {
                                 $this->importar_url = FALSE;
                             }
                         }
                     }
                 }
                 if ($xml->balance_cuenta_a) {
                     $balance_cuenta_a = new balance_cuenta_a();
                     $all_bcas = $balance_cuenta_a->all();
                     foreach ($xml->balance_cuenta_a as $bc) {
                         $encontrado = FALSE;
                         foreach ($all_bcas as $bc2) {
                             if ($bc2->codbalance == $bc->codbalance and $bc2->codcuenta == $bc->codcuenta) {
                                 $encontrado = TRUE;
                                 break;
                             }
                         }
                         if (!$encontrado) {
                             $new_bc = new balance_cuenta_a();
                             $new_bc->codbalance = $bc->codbalance;
                             $new_bc->codcuenta = $bc->codcuenta;
                             $new_bc->desccuenta = base64_decode($bc->descripcion);
                             if (!$new_bc->save()) {
                                 $this->importar_url = FALSE;
                             }
                         }
                     }
                 }
             }
             if ($import_step == 2) {
                 if ($xml->cuenta_especial) {
                     foreach ($xml->cuenta_especial as $ce) {
                         $cuenta_especial = new cuenta_especial();
                         if (!$cuenta_especial->get($ce->idcuentaesp)) {
                             $cuenta_especial->idcuentaesp = $ce->idcuentaesp;
                             $cuenta_especial->descripcion = base64_decode($ce->descripcion);
                             if (!$cuenta_especial->save()) {
                                 $this->importar_url = FALSE;
                             }
                         }
                     }
                 }
                 if ($xml->grupo_epigrafes) {
                     foreach ($xml->grupo_epigrafes as $ge) {
                         $grupo_epigrafes = new grupo_epigrafes();
                         if (!$grupo_epigrafes->get_by_codigo($ge->codgrupo, $this->ejercicio->codejercicio)) {
                             $grupo_epigrafes->codejercicio = $this->ejercicio->codejercicio;
                             $grupo_epigrafes->codgrupo = $ge->codgrupo;
                             $grupo_epigrafes->descripcion = base64_decode($ge->descripcion);
                             if (!$grupo_epigrafes->save()) {
                                 $this->importar_url = FALSE;
                             }
                         }
                     }
                 }
                 if ($xml->epigrafe) {
                     $grupo_epigrafes = new grupo_epigrafes();
                     foreach ($xml->epigrafe as $ep) {
                         $epigrafe = new epigrafe();
                         if (!$epigrafe->get_by_codigo($ep->codepigrafe, $this->ejercicio->codejercicio)) {
                             $ge = $grupo_epigrafes->get_by_codigo($ep->codgrupo, $this->ejercicio->codejercicio);
                             if ($ge) {
                                 $epigrafe->idgrupo = $ge->idgrupo;
                                 $epigrafe->codgrupo = $ge->codgrupo;
                                 $epigrafe->codejercicio = $this->ejercicio->codejercicio;
                                 $epigrafe->codepigrafe = $ep->codepigrafe;
                                 $epigrafe->descripcion = base64_decode($ep->descripcion);
                                 if (!$epigrafe->save()) {
                                     $this->importar_url = FALSE;
                                 }
                             }
                         }
                     }
                 }
             }
             if ($xml->cuenta and $import_step == 3) {
                 $epigrafe = new epigrafe();
                 foreach ($xml->cuenta as $c) {
                     $cuenta = new cuenta();
                     if (!$cuenta->get_by_codigo($c->codcuenta, $this->ejercicio->codejercicio)) {
                         $ep = $epigrafe->get_by_codigo($c->codepigrafe, $this->ejercicio->codejercicio);
                         if ($ep) {
                             $cuenta->idepigrafe = $ep->idepigrafe;
                             $cuenta->codepigrafe = $ep->codepigrafe;
                             $cuenta->codcuenta = $c->codcuenta;
                             $cuenta->codejercicio = $this->ejercicio->codejercicio;
                             $cuenta->descripcion = base64_decode($c->descripcion);
                             $cuenta->idcuentaesp = $c->idcuentaesp;
                             if (!$cuenta->save()) {
                                 $this->importar_url = FALSE;
                             }
                         }
                     }
                 }
             }
             if ($xml->subcuenta and $import_step == 4) {
                 $cuenta = new cuenta();
                 foreach ($xml->subcuenta as $sc) {
                     $subcuenta = new subcuenta();
                     if (!$subcuenta->get_by_codigo($sc->codsubcuenta, $this->ejercicio->codejercicio)) {
                         $cu = $cuenta->get_by_codigo($sc->codcuenta, $this->ejercicio->codejercicio);
                         if ($cu) {
                             $subcuenta->idcuenta = $cu->idcuenta;
                             $subcuenta->codcuenta = $cu->codcuenta;
                             $subcuenta->coddivisa = $sc->coddivisa;
                             $subcuenta->codejercicio = $this->ejercicio->codejercicio;
                             $subcuenta->codsubcuenta = $sc->codsubcuenta;
                             $subcuenta->descripcion = base64_decode($sc->descripcion);
                             if (!$subcuenta->save()) {
                                 $this->importar_url = FALSE;
                             }
                         }
                     }
                 }
             }
             if ($import_step == 5) {
                 $cliente = new cliente();
                 foreach ($cliente->all_full() as $cli) {
                     /// forzamos la generación y asociación de una subcuenta para el cliente
                     $cli->get_subcuenta($this->ejercicio->codejercicio);
                 }
             }
             if ($import_step == 6) {
                 $proveedor = new proveedor();
                 foreach ($proveedor->all_full() as $pro) {
                     /// forzamos la generación y asociación de una subcuenta para cada proveedor
                     $pro->get_subcuenta($this->ejercicio->codejercicio);
                 }
             }
         } else {
             $this->new_error("Imposible leer el archivo.");
         }
     }
 }