protected function private_core()
 {
     $this->tipo = FALSE;
     $this->subcuenta = FALSE;
     $this->cuenta = new cuenta();
     /// seleccionamos el ejercicio
     $ejercicio = new ejercicio();
     $eje0 = $ejercicio->get_by_fecha(date('d-m-Y'));
     if (isset($_POST['codejercicio'])) {
         $this->codejercicio = $_POST['codejercicio'];
     } else {
         if ($eje0) {
             $this->codejercicio = $eje0->codejercicio;
         } else {
             $this->codejercicio = $this->default_items->codejercicio();
         }
     }
     if (isset($_POST['ejercicio']) and isset($_POST['query'])) {
         $this->new_search();
     } else {
         if (isset($_REQUEST['cli'])) {
             $this->tipo = 'cli';
             $cliente = new cliente();
             $this->cliente = $cliente->get($_REQUEST['cli']);
             if ($this->cliente) {
                 $subcuenta_cliente = new subcuenta_cliente();
                 if (isset($_GET['delete_sca'])) {
                     $aux_sca = $subcuenta_cliente->get2($_GET['delete_sca']);
                     if ($aux_sca) {
                         if ($aux_sca->delete()) {
                             $this->new_message('El cliente ya no está asocuado a esa subcuenta.');
                         } else {
                             $this->new_error_msg('Imposible quitar la subcuenta.');
                         }
                     } else {
                         $this->new_error_msg('Relación con la subcuenta no encontrada.');
                     }
                 } else {
                     if (isset($_GET['idsc'])) {
                         $this->subcuenta_a = $subcuenta_cliente->get($_GET['cli'], $_GET['idsc']);
                         if ($this->subcuenta_a) {
                             $this->subcuenta = $this->subcuenta_a->get_subcuenta();
                             $this->codejercicio = $this->subcuenta_a->codejercicio;
                         }
                     } else {
                         if (isset($_POST['idsc'])) {
                             $this->subcuenta_a = $subcuenta_cliente->get($_POST['cli'], $_POST['idsc']);
                             if ($this->subcuenta_a) {
                                 $subc = new subcuenta();
                                 $subc0 = $subc->get($_POST['idsc2']);
                                 if ($subc0) {
                                     $this->subcuenta_a->idsubcuenta = $subc0->idsubcuenta;
                                     $this->subcuenta_a->codsubcuenta = $subc0->codsubcuenta;
                                     $this->subcuenta_a->codejercicio = $subc0->codejercicio;
                                     if ($this->subcuenta_a->save()) {
                                         $this->new_message('Datos guardados correctamente.');
                                     } else {
                                         $this->new_error_msg('Imposible asignar la subcuenta al cliente.');
                                     }
                                     $this->subcuenta = $subc0;
                                 } else {
                                     $this->new_error_msg('Subcuenta no encontrada.');
                                     $this->subcuenta = $this->subcuenta_a->get_subcuenta();
                                 }
                             }
                         } else {
                             if (isset($_POST['idsc2'])) {
                                 $subc = new subcuenta();
                                 $subc0 = $subc->get($_POST['idsc2']);
                                 if ($subc0) {
                                     $subcuenta_cliente->codcliente = $this->cliente->codcliente;
                                     $subcuenta_cliente->idsubcuenta = $subc0->idsubcuenta;
                                     $subcuenta_cliente->codsubcuenta = $subc0->codsubcuenta;
                                     $subcuenta_cliente->codejercicio = $subc0->codejercicio;
                                     if ($subcuenta_cliente->save()) {
                                         $this->new_message('Datos guardados correctamente.');
                                     } else {
                                         $this->new_error_msg('Imposible asignar la subcuenta al cliente.');
                                     }
                                     $this->subcuenta = $subc0;
                                 } else {
                                     $this->new_error_msg('Subcuenta no encontrada.');
                                 }
                             } else {
                                 if (isset($_POST['cuenta'])) {
                                     $cuenta0 = $this->cuenta->get($_POST['cuenta']);
                                     if ($cuenta0) {
                                         $subc0 = new subcuenta();
                                         $subc0->codcuenta = $cuenta0->codcuenta;
                                         $subc0->coddivisa = $this->default_items->coddivisa();
                                         $subc0->codejercicio = $cuenta0->codejercicio;
                                         $subc0->codsubcuenta = $_POST['codsubcuenta'];
                                         $subc0->descripcion = $this->cliente->nombre;
                                         $subc0->idcuenta = $cuenta0->idcuenta;
                                         if ($subc0->save()) {
                                             $subcuenta_cliente->codcliente = $this->cliente->codcliente;
                                             $subcuenta_cliente->idsubcuenta = $subc0->idsubcuenta;
                                             $subcuenta_cliente->codsubcuenta = $subc0->codsubcuenta;
                                             $subcuenta_cliente->codejercicio = $subc0->codejercicio;
                                             if ($subcuenta_cliente->save()) {
                                                 $this->new_message('Datos guardados correctamente.');
                                             } else {
                                                 $this->new_error_msg('Imposible asignar la subcuenta al cliente.');
                                             }
                                             $this->subcuenta = $subc0;
                                         } else {
                                             $this->new_error_msg('Imposible crear la sucuenta.');
                                         }
                                     } else {
                                         $this->new_error_msg('Cuenta no encontrada.');
                                     }
                                 } else {
                                     foreach ($subcuenta_cliente->all_from_cliente($_REQUEST['cli']) as $sca) {
                                         if ($sca->codejercicio == $this->codejercicio) {
                                             $this->subcuenta_a = $sca;
                                             $this->subcuenta = $sca->get_subcuenta();
                                             break;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         } else {
             if (isset($_REQUEST['pro'])) {
                 $this->tipo = 'pro';
                 $proveedor = new proveedor();
                 $this->proveedor = $proveedor->get($_REQUEST['pro']);
                 if ($this->proveedor) {
                     $subcuenta_proveedor = new subcuenta_proveedor();
                     if (isset($_GET['delete_sca'])) {
                         $aux_sca = $subcuenta_proveedor->get2($_GET['delete_sca']);
                         if ($aux_sca) {
                             if ($aux_sca->delete()) {
                                 $this->new_message('El proveedor ya no está asocuado a esa subcuenta.');
                             } else {
                                 $this->new_error_msg('Imposible quitar la subcuenta.');
                             }
                         } else {
                             $this->new_error_msg('Relación con la subcuenta no encontrada.');
                         }
                     } else {
                         if (isset($_GET['idsc'])) {
                             $this->subcuenta_a = $subcuenta_proveedor->get($_GET['pro'], $_GET['idsc']);
                             if ($this->subcuenta_a) {
                                 $this->subcuenta = $this->subcuenta_a->get_subcuenta();
                                 $this->codejercicio = $this->subcuenta_a->codejercicio;
                             }
                         } else {
                             if (isset($_POST['idsc'])) {
                                 $this->subcuenta_a = $subcuenta_proveedor->get($_POST['pro'], $_POST['idsc']);
                                 if ($this->subcuenta_a) {
                                     $subc = new subcuenta();
                                     $subc0 = $subc->get($_POST['idsc2']);
                                     if ($subc0) {
                                         $this->subcuenta_a->idsubcuenta = $subc0->idsubcuenta;
                                         $this->subcuenta_a->codsubcuenta = $subc0->codsubcuenta;
                                         $this->subcuenta_a->codejercicio = $subc0->codejercicio;
                                         if ($this->subcuenta_a->save()) {
                                             $this->new_message('Datos guardados correctamente.');
                                         } else {
                                             $this->new_error_msg('Imposible asignar la subcuenta al proveedor.');
                                         }
                                         $this->subcuenta = $subc0;
                                     } else {
                                         $this->new_error_msg('Subcuenta no encontrada.');
                                         $this->subcuenta = $this->subcuenta_a->get_subcuenta();
                                     }
                                 }
                             } else {
                                 if (isset($_POST['idsc2'])) {
                                     $subc = new subcuenta();
                                     $subc0 = $subc->get($_POST['idsc2']);
                                     if ($subc0) {
                                         $subcuenta_proveedor->codproveedor = $this->proveedor->codproveedor;
                                         $subcuenta_proveedor->idsubcuenta = $subc0->idsubcuenta;
                                         $subcuenta_proveedor->codsubcuenta = $subc0->codsubcuenta;
                                         $subcuenta_proveedor->codejercicio = $subc0->codejercicio;
                                         if ($subcuenta_proveedor->save()) {
                                             $this->new_message('Datos guardados correctamente.');
                                         } else {
                                             $this->new_error_msg('Imposible asignar la subcuenta al cliente.');
                                         }
                                         $this->subcuenta = $subc0;
                                     } else {
                                         $this->new_error_msg('Subcuenta no encontrada.');
                                     }
                                 } else {
                                     if (isset($_POST['cuenta'])) {
                                         $cuenta0 = $this->cuenta->get($_POST['cuenta']);
                                         if ($cuenta0) {
                                             $subc0 = new subcuenta();
                                             $subc0->codcuenta = $cuenta0->codcuenta;
                                             $subc0->coddivisa = $this->default_items->coddivisa();
                                             $subc0->codejercicio = $cuenta0->codejercicio;
                                             $subc0->codsubcuenta = $_POST['codsubcuenta'];
                                             $subc0->descripcion = $this->proveedor->nombre;
                                             $subc0->idcuenta = $cuenta0->idcuenta;
                                             if ($subc0->save()) {
                                                 $subcuenta_proveedor->codproveedor = $this->proveedor->codproveedor;
                                                 $subcuenta_proveedor->idsubcuenta = $subc0->idsubcuenta;
                                                 $subcuenta_proveedor->codsubcuenta = $subc0->codsubcuenta;
                                                 $subcuenta_proveedor->codejercicio = $subc0->codejercicio;
                                                 if ($subcuenta_proveedor->save()) {
                                                     $this->new_message('Datos guardados correctamente.');
                                                 } else {
                                                     $this->new_error_msg('Imposible asignar la subcuenta al proveedor.');
                                                 }
                                                 $this->subcuenta = $subc0;
                                             } else {
                                                 $this->new_error_msg('Imposible crear la sucuenta.');
                                             }
                                         } else {
                                             $this->new_error_msg('Cuenta no encontrada.');
                                         }
                                     } else {
                                         foreach ($subcuenta_proveedor->all_from_proveedor($_REQUEST['pro']) as $sca) {
                                             if ($sca->codejercicio == $this->codejercicio) {
                                                 $this->subcuenta_a = $sca;
                                                 $this->subcuenta = $sca->get_subcuenta();
                                                 break;
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
 protected function private_core()
 {
     /// ¿El usuario tiene permiso para eliminar en esta página?
     $this->allow_delete = $this->user->allow_delete_on(__CLASS__);
     $this->cuenta = FALSE;
     if (isset($_POST['nsubcuenta'])) {
         $subc0 = new subcuenta();
         $subc0->codcuenta = $_POST['codcuenta'];
         $subc0->codejercicio = $_POST['ejercicio'];
         $subc0->codsubcuenta = $_POST['nsubcuenta'];
         $subc0->descripcion = $_POST['descripcion'];
         $subc0->idcuenta = $_POST['idcuenta'];
         $subc0->alias = $_POST['alias'];
         if ($subc0->save()) {
             header('Location: ' . $subc0->url());
         } else {
             $this->new_error_msg('Error al crear la subcuenta.');
         }
         $this->cuenta = $subc0->get_cuenta();
     } else {
         if (isset($_GET['deletes'])) {
             $subc0 = new subcuenta();
             $subc1 = $subc0->get($_GET['deletes']);
             if ($subc1) {
                 $this->cuenta = $subc1->get_cuenta();
                 if ($subc1->delete()) {
                     $this->new_message('Subcuenta eliminada correctamente.');
                 } else {
                     $this->new_error_msg('Error al eliminar la subcuenta.');
                 }
             } else {
                 $this->new_error_msg('Subcuenta no encontrada.');
             }
         } else {
             if (isset($_GET['id'])) {
                 $cuenta = new cuenta();
                 $this->cuenta = $cuenta->get($_GET['id']);
                 if ($this->cuenta and isset($_POST['descripcion'])) {
                     $this->cuenta->descripcion = $_POST['descripcion'];
                     if ($_POST['idcuentaesp'] == '---') {
                         $this->cuenta->idcuentaesp = NULL;
                     } else {
                         $this->cuenta->idcuentaesp = $_POST['idcuentaesp'];
                     }
                     if ($this->cuenta->save()) {
                         $this->new_message('Cuenta modificada correctamente.');
                     } else {
                         $this->new_error_msg('Error al modificar la cuenta.');
                     }
                 }
             }
         }
     }
     if ($this->cuenta) {
         /// configuramos la página previa
         $this->ppage = $this->page->get('contabilidad_epigrafes');
         if ($this->ppage) {
             $this->ppage->title = 'Rubro: ' . $this->cuenta->codepigrafe;
             $this->ppage->extra_url = '&epi=' . $this->cuenta->idepigrafe;
         }
         $this->page->title = 'Cuenta: ' . $this->cuenta->codcuenta;
         $this->ejercicio = $this->cuenta->get_ejercicio();
         $this->nuevo_codsubcuenta = sprintf('%-0' . $this->ejercicio->longsubcuenta . 's', $this->cuenta->codcuenta);
     } else {
         $this->new_error_msg("Cuenta no encontrada.");
         $this->ppage = $this->page->get('contabilidad_cuentas');
     }
 }
 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.");
         }
     }
 }
Exemplo n.º 4
0
 public function get_by_codigo($cod, $codejercicio, $crear = FALSE)
 {
     $sql = "SELECT * FROM " . $this->table_name . " WHERE codsubcuenta = " . $this->var2str($cod) . " AND codejercicio = " . $this->var2str($codejercicio) . ";";
     $subc = $this->db->select($sql);
     if ($subc) {
         return new \subcuenta($subc[0]);
     } else {
         if ($crear) {
             /// buscamos la subcuenta equivalente en otro ejercicio
             $subc = $this->db->select("SELECT * FROM " . $this->table_name . " WHERE codsubcuenta = " . $this->var2str($cod) . ";");
             if ($subc) {
                 $old_sc = new \subcuenta($subc[0]);
                 /// buscamos la cuenta equivalente es ESTE ejercicio
                 $cuenta = new \cuenta();
                 $new_c = $cuenta->get_by_codigo($old_sc->codcuenta, $codejercicio);
                 if ($new_c) {
                     $new_sc = new \subcuenta();
                     $new_sc->codcuenta = $new_c->codcuenta;
                     $new_sc->coddivisa = $old_sc->coddivisa;
                     $new_sc->codejercicio = $codejercicio;
                     $new_sc->codimpuesto = $old_sc->codimpuesto;
                     $new_sc->codsubcuenta = $old_sc->codsubcuenta;
                     $new_sc->descripcion = $old_sc->descripcion;
                     $new_sc->idcuenta = $new_c->idcuenta;
                     $new_sc->iva = $old_sc->iva;
                     $new_sc->recargo = $old_sc->recargo;
                     if ($new_sc->save()) {
                         return $new_sc;
                     } else {
                         return FALSE;
                     }
                 } else {
                     $this->new_error_msg('No se ha encontrado la cuenta equivalente a ' . $old_sc->codcuenta . ' en el ejercicio ' . $codejercicio . ' <a href="index.php?page=contabilidad_ejercicio&cod=' . $codejercicio . '">¿Has importado el plan contable?</a>');
                     return FALSE;
                 }
             } else {
                 $this->new_error_msg('No se ha encontrado ninguna subcuenta equivalente a ' . $cod . ' para copiar.');
                 return FALSE;
             }
         } else {
             return FALSE;
         }
     }
 }
Exemplo n.º 5
0
 public function get_by_codigo($cod, $ejercicio, $crear = FALSE)
 {
     $subc = $this->db->select("SELECT * FROM " . $this->table_name . " WHERE codsubcuenta = " . $this->var2str($cod) . " AND codejercicio = " . $this->var2str($ejercicio) . ";");
     if ($subc) {
         return new subcuenta($subc[0]);
     } else {
         if ($crear) {
             /// buscamos la subcuenta equivalente en otro ejercicio
             $subc = $this->db->select("SELECT * FROM " . $this->table_name . " WHERE codsubcuenta = " . $this->var2str($cod) . ";");
             if ($subc) {
                 $old_sc = new subcuenta($subc[0]);
                 /// buscamos la cuenta equivalente es ESTE ejercicio
                 $cuenta = new cuenta();
                 $new_c = $cuenta->get_by_codigo($old_sc->codcuenta, $ejercicio);
                 if ($new_c) {
                     $new_sc = new subcuenta();
                     $new_sc->codcuenta = $new_c->codcuenta;
                     $new_sc->coddivisa = $old_sc->coddivisa;
                     $new_sc->codejercicio = $ejercicio;
                     $new_sc->codimpuesto = $old_sc->codimpuesto;
                     $new_sc->codsubcuenta = $old_sc->codsubcuenta;
                     $new_sc->descripcion = $old_sc->descripcion;
                     $new_sc->idcuenta = $new_c->idcuenta;
                     $new_sc->iva = $old_sc->iva;
                     $new_sc->alias = $old_alias->alias;
                     $new_sc->recargo = $old_sc->recargo;
                     if ($new_sc->save()) {
                         return $new_sc;
                     } else {
                         return FALSE;
                     }
                 } else {
                     $this->new_error_msg('No se ha encontrado la cuenta equivalente a ' . $old_sc->codcuenta . ' en el ejercicio ' . $ejercicio . '.');
                     return FALSE;
                 }
             } else {
                 $this->new_error_msg('No se ha encontrado ninguna subcuenta equivalente a ' . $cod . ' para copiar.');
                 return FALSE;
             }
         } else {
             return FALSE;
         }
     }
 }
 protected function process()
 {
     $this->cuenta = FALSE;
     if (isset($_POST['nsubcuenta'])) {
         $subc0 = new subcuenta();
         $subc0->codcuenta = $_POST['codcuenta'];
         $subc0->codejercicio = $_POST['ejercicio'];
         $subc0->codsubcuenta = $_POST['nsubcuenta'];
         $subc0->descripcion = $_POST['descripcion'];
         $subc0->idcuenta = $_POST['idcuenta'];
         if ($subc0->save()) {
             header('Location: ' . $subc0->url());
         } else {
             $this->new_error_msg('Error al crear la subcuenta.');
         }
         $this->cuenta = $subc0->get_cuenta();
     } else {
         if (isset($_GET['deletes'])) {
             $subc0 = new subcuenta();
             $subc1 = $subc0->get($_GET['deletes']);
             if ($subc1) {
                 $this->cuenta = $subc1->get_cuenta();
                 if ($subc1->delete()) {
                     $this->new_message('Subcuenta eliminada correctamente.');
                 } else {
                     $this->new_error_msg('Error al eliminar la subcuenta.');
                 }
             } else {
                 $this->new_error_msg('Subcuenta no encontrada.');
             }
         } else {
             if (isset($_GET['id'])) {
                 $cuenta = new cuenta();
                 $this->cuenta = $cuenta->get($_GET['id']);
                 if ($this->cuenta and isset($_POST['descripcion'])) {
                     $this->cuenta->descripcion = $_POST['descripcion'];
                     if ($_POST['idcuentaesp'] == '---') {
                         $this->cuenta->idcuentaesp = NULL;
                     } else {
                         $this->cuenta->idcuentaesp = $_POST['idcuentaesp'];
                     }
                     if ($this->cuenta->save()) {
                         $this->new_message('Cuenta modificada correctamente.');
                     } else {
                         $this->new_error_msg('Error al modificar la cuenta.');
                     }
                 }
             }
         }
     }
     if ($this->cuenta) {
         /// configuramos la página previa
         $this->ppage = $this->page->get('contabilidad_epigrafes');
         if ($this->ppage) {
             $this->ppage->title = 'Epígrafe: ' . $this->cuenta->codepigrafe;
             $this->ppage->extra_url = '&epi=' . $this->cuenta->idepigrafe;
         }
         $this->page->title = 'Cuenta: ' . $this->cuenta->codcuenta;
         $this->ejercicio = $this->cuenta->get_ejercicio();
         $this->buttons[] = new fs_button_img('b_eliminar', 'Eliminar', 'trash.png', '#', TRUE);
     } else {
         $this->new_error_msg("Cuenta no encontrada.");
         $this->ppage = $this->page->get('contabilidad_cuentas');
     }
 }
 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.");
         }
     }
 }