protected function private_core()
 {
     $this->ppage = $this->page->get('ventas_clientes');
     $this->agente = new agente();
     $this->cuenta_banco = new cuenta_banco_cliente();
     $this->divisa = new divisa();
     $this->forma_pago = new forma_pago();
     $this->grupo = new grupo_clientes();
     $this->pais = new pais();
     $this->serie = new serie();
     /// ¿El usuario tiene permiso para eliminar en esta página?
     $this->allow_delete = $this->user->allow_delete_on(__CLASS__);
     /// cargamos el cliente
     $cliente = new cliente();
     $this->cliente = FALSE;
     if (isset($_POST['codcliente'])) {
         $this->cliente = $cliente->get($_POST['codcliente']);
     } else {
         if (isset($_GET['cod'])) {
             $this->cliente = $cliente->get($_GET['cod']);
         }
     }
     /// ¿Hay que hacer algo más?
     if (isset($_GET['delete_cuenta'])) {
         $cuenta = $this->cuenta_banco->get($_GET['delete_cuenta']);
         if ($cuenta) {
             if ($cuenta->delete()) {
                 $this->new_message('Cuenta bancaria eliminada correctamente.');
             } else {
                 $this->new_error_msg('Imposible eliminar la cuenta bancaria.');
             }
         } else {
             $this->new_error_msg('Cuenta bancaria no encontrada.');
         }
     } else {
         if (isset($_GET['delete_dir'])) {
             $dir = new direccion_cliente();
             $dir0 = $dir->get($_GET['delete_dir']);
             if ($dir0) {
                 if ($dir0->delete()) {
                     $this->new_message('Dirección eliminada correctamente.');
                 } else {
                     $this->new_error_msg('Imposible eliminar la dirección.');
                 }
             } else {
                 $this->new_error_msg('Dirección no encontrada.');
             }
         } else {
             if (isset($_POST['coddir'])) {
                 $dir = new direccion_cliente();
                 if ($_POST['coddir'] != '') {
                     $dir = $dir->get($_POST['coddir']);
                 }
                 $dir->apartado = $_POST['apartado'];
                 $dir->ciudad = $_POST['ciudad'];
                 $dir->codcliente = $this->cliente->codcliente;
                 $dir->codpais = $_POST['pais'];
                 $dir->codpostal = $_POST['codpostal'];
                 $dir->descripcion = $_POST['descripcion'];
                 $dir->direccion = $_POST['direccion'];
                 $dir->domenvio = isset($_POST['direnvio']);
                 $dir->domfacturacion = isset($_POST['dirfact']);
                 $dir->provincia = $_POST['provincia'];
                 if ($dir->save()) {
                     $this->new_message("Dirección guardada correctamente.");
                 } else {
                     $this->new_message("¡Imposible guardar la dirección!");
                 }
             } else {
                 if (isset($_POST['iban'])) {
                     if (isset($_POST['codcuenta'])) {
                         $cuentab = $this->cuenta_banco->get($_POST['codcuenta']);
                     } else {
                         $cuentab = new cuenta_banco_cliente();
                         $cuentab->codcliente = $_POST['codcliente'];
                     }
                     $cuentab->descripcion = $_POST['descripcion'];
                     if ($_POST['ciban'] != '') {
                         $cuentab->iban = $this->calcular_iban($_POST['ciban']);
                     } else {
                         $cuentab->iban = $_POST['iban'];
                     }
                     $cuentab->swift = $_POST['swift'];
                     if ($cuentab->save()) {
                         $this->new_message('Cuenta bancaria guardada correctamente.');
                     } else {
                         $this->new_error_msg('Imposible guardar la cuenta bancaria.');
                     }
                 } else {
                     if (isset($_POST['codcliente'])) {
                         $this->cliente->nombre = $_POST['nombre'];
                         $this->cliente->razonsocial = $_POST['razonsocial'];
                         $this->cliente->cifnif = $_POST['cifnif'];
                         $this->cliente->telefono1 = $_POST['telefono1'];
                         $this->cliente->telefono2 = $_POST['telefono2'];
                         $this->cliente->fax = $_POST['fax'];
                         $this->cliente->web = $_POST['web'];
                         $this->cliente->email = $_POST['email'];
                         $this->cliente->observaciones = $_POST['observaciones'];
                         $this->cliente->codserie = $_POST['codserie'];
                         $this->cliente->codpago = $_POST['codpago'];
                         $this->cliente->coddivisa = $_POST['coddivisa'];
                         $this->cliente->regimeniva = $_POST['regimeniva'];
                         $this->cliente->recargo = isset($_POST['recargo']);
                         $this->cliente->debaja = isset($_POST['debaja']);
                         $this->cliente->codagente = NULL;
                         if ($_POST['codagente'] != '---') {
                             $this->cliente->codagente = $_POST['codagente'];
                         }
                         $this->cliente->codgrupo = NULL;
                         if ($_POST['codgrupo'] != '---') {
                             $this->cliente->codgrupo = $_POST['codgrupo'];
                         }
                         if ($this->cliente->save()) {
                             $this->new_message("Datos del cliente modificados correctamente.");
                         } else {
                             $this->new_error_msg("¡Imposible modificar los datos del cliente!");
                         }
                     }
                 }
             }
         }
     }
     if ($this->cliente) {
         $this->page->title = $this->cliente->codcliente;
     } else {
         $this->new_error_msg("¡Cliente no encontrado!");
     }
 }
 protected function private_core()
 {
     $this->ppage = $this->page->get('ventas_clientes');
     $this->agente = new agente();
     $this->cuenta_banco = new cuenta_banco_cliente();
     $this->divisa = new divisa();
     $this->forma_pago = new forma_pago();
     $this->grupo = new grupo_clientes();
     $this->pais = new pais();
     $this->serie = new serie();
     $this->ncf_tipo = new ncf_tipo();
     $this->ncf_entidad_tipo = new ncf_entidad_tipo();
     /// ¿El usuario tiene permiso para eliminar en esta página?
     $this->allow_delete = $this->user->allow_delete_on(__CLASS__);
     /// cargamos el cliente
     $cliente = new cliente();
     $this->cliente = FALSE;
     if (isset($_POST['codcliente'])) {
         $this->cliente = $cliente->get($_POST['codcliente']);
     } else {
         if (isset($_GET['cod'])) {
             $this->cliente = $cliente->get($_GET['cod']);
         }
     }
     $this->ncf_cliente_tipo = $this->ncf_entidad_tipo->get($this->empresa->id, $this->cliente->codcliente, 'CLI');
     if ($this->cliente) {
         $this->page->title = $this->cliente->codcliente;
         /// ¿Hay que hacer algo más?
         if (isset($_GET['delete_cuenta'])) {
             $cuenta = $this->cuenta_banco->get($_GET['delete_cuenta']);
             if ($cuenta) {
                 if ($cuenta->delete()) {
                     $this->new_message('Cuenta bancaria eliminada correctamente.');
                 } else {
                     $this->new_error_msg('Imposible eliminar la cuenta bancaria.');
                 }
             } else {
                 $this->new_error_msg('Cuenta bancaria no encontrada.');
             }
         } else {
             if (isset($_GET['delete_dir'])) {
                 $dir = new direccion_cliente();
                 $dir0 = $dir->get($_GET['delete_dir']);
                 if ($dir0) {
                     if ($dir0->delete()) {
                         $this->new_message('Dirección eliminada correctamente.');
                     } else {
                         $this->new_error_msg('Imposible eliminar la dirección.');
                     }
                 } else {
                     $this->new_error_msg('Dirección no encontrada.');
                 }
             } else {
                 if (isset($_POST['coddir'])) {
                     $dir = new direccion_cliente();
                     if ($_POST['coddir'] != '') {
                         $dir = $dir->get($_POST['coddir']);
                     }
                     $dir->apartado = $_POST['apartado'];
                     $dir->ciudad = $_POST['ciudad'];
                     $dir->codcliente = $this->cliente->codcliente;
                     $dir->codpais = $_POST['pais'];
                     $dir->codpostal = $_POST['codpostal'];
                     $dir->descripcion = $_POST['descripcion'];
                     $dir->direccion = $_POST['direccion'];
                     $dir->domenvio = isset($_POST['direnvio']);
                     $dir->domfacturacion = isset($_POST['dirfact']);
                     $dir->provincia = $_POST['provincia'];
                     if ($dir->save()) {
                         $this->new_message("Dirección guardada correctamente.");
                     } else {
                         $this->new_message("¡Imposible guardar la dirección!");
                     }
                 } else {
                     if (isset($_POST['iban'])) {
                         if (isset($_POST['codcuenta'])) {
                             $cuentab = $this->cuenta_banco->get($_POST['codcuenta']);
                         } else {
                             $cuentab = new cuenta_banco_cliente();
                             $cuentab->codcliente = $_POST['codcliente'];
                         }
                         $cuentab->descripcion = $_POST['descripcion'];
                         $cuentab->iban = $_POST['iban'];
                         $cuentab->swift = $_POST['swift'];
                         $cuentab->principal = isset($_POST['principal']);
                         $cuentab->fmandato = NULL;
                         if (isset($_POST['fmandato'])) {
                             if ($_POST['fmandato'] != '') {
                                 $cuentab->fmandato = $_POST['fmandato'];
                             }
                         }
                         if ($cuentab->save()) {
                             $this->new_message('Cuenta bancaria guardada correctamente.');
                         } else {
                             $this->new_error_msg('Imposible guardar la cuenta bancaria.');
                         }
                     } else {
                         if (isset($_POST['codcliente'])) {
                             $this->cliente->nombre = $_POST['nombre'];
                             $this->cliente->razonsocial = $_POST['razonsocial'];
                             $this->cliente->tipoidfiscal = $_POST['tipoidfiscal'];
                             $this->cliente->cifnif = $_POST['cifnif'];
                             $this->cliente->telefono1 = $_POST['telefono1'];
                             $this->cliente->telefono2 = $_POST['telefono2'];
                             $this->cliente->fax = $_POST['fax'];
                             $this->cliente->web = $_POST['web'];
                             $this->cliente->email = $_POST['email'];
                             $this->cliente->observaciones = $_POST['observaciones'];
                             $this->cliente->codpago = $_POST['codpago'];
                             $this->cliente->coddivisa = $_POST['coddivisa'];
                             $this->cliente->regimeniva = $_POST['regimeniva'];
                             $this->cliente->recargo = isset($_POST['recargo']);
                             $this->cliente->debaja = isset($_POST['debaja']);
                             $this->cliente->personafisica = isset($_POST['personafisica']);
                             $this->cliente->codserie = NULL;
                             if ($_POST['codserie'] != '') {
                                 $this->cliente->codserie = $_POST['codserie'];
                             }
                             $this->cliente->codagente = NULL;
                             if ($_POST['codagente'] != '') {
                                 $this->cliente->codagente = $_POST['codagente'];
                             }
                             $this->cliente->codgrupo = NULL;
                             if ($_POST['codgrupo'] != '') {
                                 $this->cliente->codgrupo = $_POST['codgrupo'];
                             }
                             if (isset($_POST['tipo_comprobante'])) {
                                 $continue = TRUE;
                                 $tipo_comprobante = \filter_input(INPUT_POST, 'tipo_comprobante');
                                 if ($tipo_comprobante == '01' and strlen($this->cliente->cifnif) < 9) {
                                     $this->new_error_msg("¡Imposible actualizar información de NCF para el cliente, por favor corrija primero la Cédula o RNC asignados!");
                                 } else {
                                     $ncf_entidad_tipo = new ncf_entidad_tipo();
                                     $ncf_entidad_tipo->idempresa = $this->empresa->id;
                                     $ncf_entidad_tipo->entidad = \filter_input(INPUT_POST, 'codcliente');
                                     $ncf_entidad_tipo->tipo_entidad = 'CLI';
                                     $ncf_entidad_tipo->tipo_comprobante = $tipo_comprobante;
                                     $ncf_entidad_tipo->usuario_creacion = $this->user->nick;
                                     $ncf_entidad_tipo->fecha_creacion = \Date('d-m-Y H:i:s');
                                     $ncf_entidad_tipo->usuario_modificacion = $this->user->nick;
                                     $ncf_entidad_tipo->fecha_modificacion = \Date('d-m-Y H:i:s');
                                     $ncf_entidad_tipo->estado = TRUE;
                                     if (!$ncf_entidad_tipo->save()) {
                                         $this->new_error_msg("¡Imposible actualizar información de NCF para  Cliente " . $ncf_entidad_tipo->entidad . "!");
                                     } else {
                                         $this->ncf_cliente_tipo = $this->ncf_entidad_tipo->get($this->empresa->id, \filter_input(INPUT_POST, 'codcliente'), 'CLI');
                                     }
                                 }
                             }
                             if ($this->cliente->save()) {
                                 $this->new_message("Datos del cliente modificados correctamente.");
                                 $this->propagar_cifnif();
                             } else {
                                 $this->new_error_msg("¡Imposible modificar los datos del cliente!");
                             }
                         }
                     }
                 }
             }
         }
     } else {
         $this->new_error_msg("¡Cliente no encontrado!", 'error', FALSE, FALSE);
     }
 }