Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Dental {
- public function __isset($name) {
- return isset($this->{$name});
- }
- public function __set($name, $value) {
- $this->{$name} = $value;
- }
- public function __get($name) {
- if ($name == 'db') {
- return MySQL::getInstance();
- }
- elseif (isset($this->{$name})) {
- return $this->{$name};
- }
- return null;
- }
- private function __construct($user, $pass){
- if (!$user || !$pass) {
- return false;
- }
- $user = $this->db->escape($user);
- $p = $this->db->oneFieldQuery("SELECT clave_seguridad FROM usuarios WHERE correo_electronico = '{$user}'");
- if ($p == md5($pass)) {
- $q = "SELECT id_usuario,correo_electronico,nombre,apellido FROM usuarios WHERE correo_electronico = '{$user}'";
- $r = $this->db->oneRowQuery($q);
- $this->id = $r['id_usuario'];
- $this->fullname = $r['apellido'] . ',' . $r['nombre'];
- $this->apellido = $r['apellido'];
- $this->nombre = $r['nombre'];
- return true;
- }
- else{
- return false;
- }
- }
- public static function getInstance($user = null, $pass = null) {
- if ( !isset($instance)) {
- $instance = new self($user, $pass);
- }
- return $instance;
- }
- public function paciente($id){
- if (!$id) {
- return false;
- }
- // ME FIJO SI EL PACIENTE YA ESTA SETEADO Y LO RETORNO
- if (is_array($this->pacientes) && in_array($id, $this->pacientes)) {
- return $this->pacientes[$id];
- }
- else{
- // CREO LA LISTA DE PACIENTES SI NO ESTA CREADA
- if (!is_array($this->pacientes)){
- $this->pacientes = array();
- }
- if (is_array($id)) {
- $paciente = new Paciente($this->id, $id);
- }
- else{
- $paciente = new Paciente($id);
- }
- if ($paciente) {
- $this->pacientes[$id] = $paciente;
- return $paciente;
- }
- }
- return false;
- }
- public function pacientes(){
- $q = "SELECT id_paciente AS id FROM pacientes WHERE id_usuario = {$this->id} AND eliminado <> 1";
- $this->db->query($q);
- if ($this->db->numRows()) {
- while ($_ = $this->db->fetchAssoc()) {
- $this->paciente($_['id']);
- }
- }
- $this->db->free();
- return $this->pacientes;
- }
- /**
- * Trae los usuarios disponebles para compartir dado tal paciente
- * @param Paciente
- * @return array usuarios
- **/
- public function usuarios_disponibles($paciente){
- if (!$paciente || !($paciente instanceof Paciente) || !is_numeric($paciente->id)) {
- return false;
- }
- $q = "SELECT id_usuario AS id, apellido, nombre, correo_electronico FROM usuarios WHERE id_usuario NOT IN (SELECT id_usuario_compartir FROM compartidos WHERE id_paciente = {$paciente->id}) AND id_usuario != {$this->id}";
- $this->db->query($q);
- $usuarios = array();
- if ($this->db->numRows()) {
- while ($_ = $this->db->fetchAssoc()) {
- $_['url'] = crypt_params(array(PACIENTE => $paciente->id, USUARIO => $_['id']));
- $_['fullname'] = $_['apellido'] . ', ' . $_['nombre'];
- $usuarios[$_['id']] = $_;
- }
- }
- $this->db->free();
- return $usuarios;
- }
- /**
- * comparte el paciente dado en el primer parametro con el usuario del segundo parametro
- *
- * @param Paciente $paciente paciente a ser compartido
- * @param numeric $usuario usuario con quien compartir paciente
- * @return bool puede retornar falso si es que la fila ya existe
- **/
- public function compartir($paciente, $usuario){
- if (!$paciente || !$usuario || !($paciente instanceof Paciente) || !is_numeric($usuario) || !is_numeric($paciente->id)) {
- return false;
- }
- $q = "INSERT INTO compartidos (id_usuario, id_usuario_compartir, id_paciente) SELECT * FROM (SELECT '{$this->id}', {$usuario}, '{$paciente->id}') AS tmp WHERE NOT EXISTS (SELECT id_usuario,id_usuario_compartir,id_paciente FROM compartidos WHERE id_usuario = '{$this->id}' AND id_usuario_compartir = '{$usuario}' AND id_paciente = '{$paciente->id}') LIMIT 1";
- return $this->db->query($q);
- }
- /**
- * deja de compartir el paciente dado en relacion con el id compartir
- *
- * @param Paciente $paciente paciente que se va a dejar de compartir
- * @param numeric $id relacion usuario paciente
- * @return bool retorna 0 si se elimino la relacion
- **/
- public function nocompartir($paciente, $id){
- if (!$paciente || !$id || !($paciente instanceof Paciente) || !is_numeric($id) || !is_numeric($paciente->id)) {
- return false;
- }
- $q = "DELETE FROM compartidos WHERE id_compartir = {$id} AND id_paciente = {$paciente->id} AND id_usuario = {$this->id}";
- $this->db->query($q);
- $q = "SELECT COUNT(id_compartir) FROM compartidos WHERE id_compartir = {$id} AND id_paciente = {$paciente->id} AND id_usuario = {$this->id}";
- return $this->db->oneFieldQuery($q);
- }
- /**
- * trae a todas las filas paciente/usuario que comparto
- *
- * @return void
- * @author
- **/
- public function comparto(){
- $comparto = array();
- $q = "SELECT P.id_paciente AS id, concat(P.apellido, ', ', P.nombre) AS PACIENTE, concat(U.apellido, ', ', U.nombre) AS USUARIO, id_compartir AS COMPARTIR FROM compartidos AS C INNER JOIN pacientes AS P ON P.id_paciente = C.id_paciente INNER JOIN usuarios AS U ON U.id_usuario = C.id_usuario_compartir WHERE C.id_usuario = {$this->id}";
- $this->db->query($q);
- if ($this->db->numRows()) {
- while ($_ = $this->db->fetchAssoc()) {
- $_['COMPARTIR'] = crypt_params(array(PACIENTE => $_['id'], COMPARTIR => $_['COMPARTIR']));
- $_['URL'] = URL_ROOT . '/paciente/' . crypt_params(array(PACIENTE => $_['id']));
- $comparto[] = $_;
- }
- }
- $this->db->free();
- return $comparto;
- }
- public function mecomparten(){
- $mecomarten = array();
- $q = "SELECT P.id_paciente AS id, concat(P.apellido, ', ', P.nombre) AS PACIENTE, concat(U.apellido, ', ', U.nombre) AS USUARIO FROM compartidos AS C INNER JOIN pacientes AS P ON P.id_paciente = C.id_paciente INNER JOIN usuarios AS U ON U.id_usuario = {$this->id} WHERE C.id_usuario_compartir = {$this->id}";
- $this->db->query($q);
- if ($this->db->numRows()) {
- while ($_ = $this->db->fetchAssoc()) {
- $_['URL'] = URL_ROOT . '/paciente/' . crypt_params(array(PACIENTE => $_['id']));
- $mecomarten[] = $_;
- }
- }
- $this->db->free();
- return $mecomarten;
- }
- }
- class Paciente{
- private static $regex = '/^(?:id_)?usuario|fecha_(?:nacimient|ingres)|(?:apellid|telefon|eliminad|estad|sex|fot|correo_electronic)o|[mp]adre_(?:apellido|nombre)|dni|celular|direccion|ciudad|provincia|nombre|derivado_por|codigo_postal$/';
- public function __isset($name) {
- return isset($this->{$name});
- }
- public function __set($name, $value) {
- $this->{$name} = $value;
- }
- public function __get($name) {
- if ($name == 'db') {
- return MySQL::getInstance();
- }
- elseif (preg_match(self::$regex, $name)) {
- if (!isset($this->{$name})) {
- return $this->select($name)->{$name};
- }
- }
- elseif (isset($this->{$name})) {
- return $this->{$name};
- }
- return null;
- }
- public function __construct($id = null, $data = null){
- if (!$id && !$data) {
- return false;
- }
- if (is_array($data) && is_numeric($id)){
- $keys = array('id_usuario');
- $values = array($id);
- foreach ($data as $key => $value) {
- if (preg_match(self::$regex, $key)) {
- if(preg_match('/^fecha_(?:nacimient|ingres)o$/', $key)){
- $v = date('Y-m-d H:i:s', strtotime($value));
- }
- $keys[] = $key;
- $values[] = $this->db->escape($value);
- }
- }
- $q = "INSERT INTO pacientes (" . implode(",", $keys) . ") VALUES ('" . implode("','", $values) . "')";
- $this->db->query($q);
- $this->id = $this->db->lastID();
- }
- else{
- $this->id = $id;
- }
- $this->URL = crypt_params(array(PACIENTE => $this->id));
- return $this;
- }
- public function edad() {
- return (int) floor(diff_date($this->fecha_nacimiento, date('Y-m-d'))/365);
- }
- public function toarray($data) {
- pre_dump(func_get_args());
- $this->select($data);
- foreach ($data as $k => $v) {
- $data[$v] = $this->{$v};
- }
- pre_dump($data);
- }
- public function tratamiento($id = null){
- if (!$id) {
- return $this->ultimoTratamiento();
- }
- if (is_array($this->tratamientos) && in_array($id, $this->tratamientos)) {
- return $this->tratamientos[$id];
- }
- else{
- if (!is_array($this->tratamientos)){
- $this->tratamientos = array();
- }
- if (is_array($id)) {
- $tratamiento = new Tratamiento($this->id, $id);
- }
- elseif (is_numeric($id)) {
- $tratamiento = new Tratamiento($id);
- }
- if ($tratamiento) {
- $this->tratamientos[$tratamiento->id] = $tratamiento;
- return $tratamiento;
- }
- }
- return false;
- }
- public function tratamientos(){
- $q = "SELECT T.id_tratamiento AS id FROM tratamientos T INNER JOIN pacientes P ON P.id_paciente = T.id_paciente AND P.id_paciente = {$this->id} AND T.eliminado != 1 ORDER BY T.id_tratamiento DESC";
- $this->db->query($q);
- if ($this->db->numRows()) {
- while ($_ = $this->db->fetchAssoc()) {
- $this->tratamiento($_['id']);
- }
- }
- $this->db->free();
- return $this->tratamientos;
- }
- public function ultimoTratamiento(){
- $q = "SELECT id_tratamiento as id FROM tratamientos WHERE id_paciente = {$this->id} AND eliminado <> 1 ORDER BY id_tratamiento DESC LIMIT 0, 1";
- $this->db = MySQL::getInstance();
- $id = $this->db->oneFieldQuery($q);
- $this->db->free();
- if ($id) {
- return $this->tratamiento($id);
- }
- return false;
- }
- public function fullname(){
- return $this->apellido . ', ' . $this->nombre;
- }
- /**
- * concatena los datos para y forma la url para redirigir a la seccion solicitada
- *
- * @param string $section seccion a la cual se quiere redirigir
- * @return sting url formada
- **/
- public function url($section = 'paciente'){
- if (!preg_match('/^(?:editar|(?:(?:foto|radio)graf|cefalometr)ias|economia|diagnostico\/(?:uno|dos|examen|historia))$/', $section)) {
- $section = 'paciente';
- }
- return URL_ROOT . '/' . $section . '/' . $this->URL;
- }
- public function setInfo($data){
- $q = "UPDATE pacientes SET ";
- foreach ($data as $k => $v) {
- if (preg_match(self::$regex, $k)){
- if(preg_match('/^fecha_(?:nacimient|ingres)o$/', $k)){
- $v = date('Y-m-d H:i:s', strtotime($v));
- }
- $q .= $k . "='" . $v ."',";
- }
- }
- $q = rtrim($q ,",") . " WHERE id_paciente = '{$this->id}'";
- $this->db->query($q);
- return $this->select();
- }
- public function select($data = '*'){
- if (is_string($data)) {
- $data = array($data);
- }
- if (is_array($data)) {
- $keys = array();
- foreach ($data as $field) {
- if (preg_match('/^(?:id_)?usuario|fecha_(?:nacimient|ingres)o|(?:apellid|telefon|eliminad|estad|sex|fot|correo_electronic)o|[mp]adre_(?:apellido|nombre)|dni|celular|direccion|ciudad|provincia|nombre|derivado_por|codigo_postal|\*$/', $field)) {
- if ($field == 'usuario') {
- $field = 'id_' . $field;
- }
- $keys[] = $field;
- }
- }
- if (count($keys)) {
- $q = "SELECT " . implode(', ', array_unique($keys)) . " FROM pacientes WHERE id_paciente = {$this->id}";
- $db = MySQL::getInstance();
- $paciente = $db->oneRowQuery($q);
- if ($paciente) {
- foreach ($paciente as $k => $v) {
- if (preg_match('/^fecha_(?:nacimient|ingres)o$/', $v)) {
- $v = date('d-m-Y', strtotime($v));
- }
- $this->{str_replace('id_', '', $k)} = utf8_encode($v);
- }
- }
- return $this;
- }
- }
- return false;
- }
- }
- class Tratamiento{
- public function __isset($name) {
- return isset($this->{$name});
- }
- public function __set($name, $value) {
- $this->{$name} = $value;
- }
- public function __get($name) {
- if ($name == 'db') {
- return MySQL::getInstance();
- }
- elseif (preg_match('/^(?:fecha_hora_inici|estad|presupuest|eliminad)o|tecnica|(?:descrip|dura)cion|avance|fecha$/', $name)) {
- if (!isset($this->{$name})) {
- return $this->select($name)->{$name};
- }
- }
- elseif ($name == 'odontograma') {
- $this->odontograma = $this->odontograma();
- return $this->odontograma;
- }
- elseif ($name == 'paciente') {
- $this->paciente = new Paciente($this->select('id_paciente')->paciente);
- return $this->paciente;
- }
- elseif (isset($this->{$name})) {
- return $this->{$name};
- }
- return null;
- }
- public function __construct($id = null, $data = null){
- if (!$id && !$data) {
- return false;
- }
- if (is_array($data) && is_numeric($id)){
- $keys = array('id_paciente','estado');
- $values = array($id,1);
- foreach ($data as $k => $v) {
- if (preg_match('/^fecha_hora_inicio|duracion|tecnica|presupuesto|descripcion$/', $k)) {
- if ($k == 'fecha_hora_inicio') {
- $v = $v !== '' ? date('Y-m-d H:i:s', strtotime($v)) : date("Y-m-d H:i:s");
- }
- elseif ($k == 'estado') {
- $v = 1;
- }
- $keys[] = $k;
- $values[] = $this->db->escape($v);
- }
- }
- $odontograma = new Odontograma();
- if ($odontograma->id) {
- $keys[] = 'id_odontograma';
- $values[] = $odontograma->id;
- }
- $q = "INSERT INTO tratamientos (" . implode(",", $keys) . ") VALUES ('" . implode("','", $values) . "')";
- $this->db->query($q);
- $this->id = $this->db->lastID();
- }
- else{
- $this->id = $id;
- }
- return $this;
- }
- public function balancear(){
- $acumulado = $this->acumulado();
- foreach($this->pagos() as $pago){
- $pago->acumulado = $acumulado;
- $pago->balance = $this->presupuesto - $pago->acumulado;
- $acumulado -= $pago->monto;
- $pago->update();
- }
- }
- public function url() {
- $this->url = crypt_params(array(TRATAMIENTO => $this->id, PACIENTE => $this->paciente->id));
- return $this->url;
- }
- public function pago($id){
- // SI id ES UN ARRAY ES PORQUE SE CREA UN NUEVO PAGO Y LE ADJUNTO EL id DEL TRATAMIENTO
- if (is_array($id)) {
- $id['id_tratamiento'] = $this->id;
- }
- // CREO EL ARREGLO DE PAGOS SI NO EXISTE
- if (!is_array($this->pagos)){
- $this->pagos = array();
- }
- // SI ES UNA FOTOGRAFIA NUEVA id ES UN ARRAY SINO ES UN NUMERO Y RETORNO LA FOTOGRAFIA POR ID
- $pago = new Pago($id);
- if ($pago->id) {
- $this->pagos[$pago->id] = $pago;
- return $pago;
- }
- return false;
- }
- public function toarray($data) {
- pre_dump(func_get_args());
- $this->select($data);
- foreach ($data as $k => $v) {
- $data[$v] = $this->{$v};
- }
- pre_dump($data);
- }
- public function pagos() {
- $q = "SELECT id_pago AS id FROM pagos WHERE id_tratamiento = {$this->id} ORDER BY fecha_hora DESC";
- $this->db->query($q);
- if ($this->db->numRows()) {
- $this->pagos = array();
- while ($_ = $this->db->fetchAssoc()) {
- $this->pago($_['id']);
- }
- return $this->pagos;
- }
- return false;
- }
- public function acumulado(){
- $q = "SELECT SUM(monto) AS acumulado FROM pagos WHERE id_tratamiento = {$this->id}";
- return (int) $this->db->oneFieldQuery($q);
- }
- public function odontograma($data = null){
- return new Odontograma($this->select('id_odontograma')->odontograma);
- }
- /**
- * Busca todas las cefalometrias para este tratamiento
- *
- * @return array array con todas las filas de cefalometrias
- **/
- public function cefalometrias(){
- $q = "SELECT id_cefalometria AS id FROM cefalometrias WHERE id_tratamiento = {$this->id} AND eliminado <> 1 ORDER BY fecha_hora DESC";
- if (is_array($this->cefalometrias)) {
- return $this->cefalometrias;
- }
- $this->db->query($q);
- if ($this->db->numRows()) {
- while ($_ = $this->db->fetchAssoc()) {
- $this->cefalometria($_['id']);
- }
- }
- return $this->cefalometrias;
- }
- public function cefalometria($id = null, $data = null){
- // SI VIENE UN NUMERO Y ESTA FOTOGRAFIA YA EXISTE LA MANDO
- if (is_numeric($id) && is_array($this->cefalometrias) && in_array($id, $this->cefalometrias)) {
- return $this->cefalometrias[$id];
- }
- else{
- // SI id ES UN ARRAY ES PORQUE SE INSERTA UNA NUEVA FOTOGRAFIA Y LE ADJUNTO EL id DEL TRATAMIENTO
- if (is_array($id)) {
- $id['id_tratamiento'] = $this->id;
- }
- // CREO EL ARREGLO DE FOTOGRAFIAS SI NO EXISTE
- if (!is_array($this->cefalometrias)){
- $this->cefalometrias = array();
- }
- // SI ES UNA FOTOGRAFIA NUEVA id ES UN ARRAY SINO ES UN NUMERO Y RETORNO LA FOTOGRAFIA POR ID
- $cefalometria = new Cefalometria($id);
- if ($cefalometria) {
- $this->cefalometrias[$cefalometria->id] = $cefalometria;
- return $cefalometria;
- }
- }
- return false;
- }
- /**
- * Busca todas las fotografias para este tratamiento
- *
- * @return array array con todas las filas de fotografias
- **/
- public function fotografias(){
- $q = "SELECT id_fotografia AS id FROM fotografias WHERE id_tratamiento = {$this->id} AND eliminado <> 1 ORDER BY fecha_hora DESC";
- if (is_array($this->fotografias)) {
- return $this->fotografias;
- }
- $this->db->query($q);
- if ($this->db->numRows()) {
- while ($_ = $this->db->fetchAssoc()) {
- $this->fotografia($_['id']);
- }
- }
- return $this->fotografias;
- }
- public function fotografia($id = null, $data = null){
- // SI VIENE UN NUMERO Y ESTA FOTOGRAFIA YA EXISTE LA MANDO
- if (is_numeric($id) && is_array($this->fotografias) && in_array($id, $this->fotografias)) {
- return $this->fotografias[$id];
- }
- else{
- // SI id ES UN ARRAY ES PORQUE SE INSERTA UNA NUEVA FOTOGRAFIA Y LE ADJUNTO EL id DEL TRATAMIENTO
- if (is_array($id)) {
- $id['id_tratamiento'] = $this->id;
- }
- // CREO EL ARREGLO DE FOTOGRAFIAS SI NO EXISTE
- if (!is_array($this->fotografias)){
- $this->fotografias = array();
- }
- // SI ES UNA FOTOGRAFIA NUEVA id ES UN ARRAY SINO ES UN NUMERO Y RETORNO LA FOTOGRAFIA POR ID
- $fotografia = new Fotografia($id);
- if ($fotografia) {
- $this->fotografias[$fotografia->id] = $fotografia;
- return $fotografia;
- }
- }
- return false;
- }
- /**
- * Busca todas las radiografias para este tratamiento
- *
- * @return array array con todas las filas de radiografias
- **/
- public function radiografias(){
- $q = "SELECT id_radiografia AS id FROM radiografias WHERE id_tratamiento = {$this->id} AND eliminado <> 1 ORDER BY fecha_hora DESC";
- if (is_array($this->radiografias)) {
- return $this->radiografias;
- }
- $this->db->query($q);
- if ($this->db->numRows()) {
- while ($_ = $this->db->fetchAssoc()) {
- $this->radiografia($_['id']);
- }
- }
- return $this->radiografias;
- }
- public function radiografia($id = null, $data = null){
- // SI VIENE UN NUMERO Y ESTA RADIOGRAFIA YA EXISTE LA MANDO
- if (is_numeric($id) && is_array($this->radiografias) && in_array($id, $this->radiografias)) {
- return $this->radiografias[$id];
- }
- else{
- // SI id ES UN ARRAY ES PORQUE SE INSERTA UNA NUEVA RADIOGRAFIA Y LE ADJUNTO EL id DEL TRATAMIENTO
- if (is_array($id)) {
- $id['id_tratamiento'] = $this->id;
- }
- // CREO EL ARREGLO DE RADIOGRAFIAS SI NO EXISTE
- if (!is_array($this->radiografias)){
- $this->radiografias = array();
- }
- // SI ES UNA RADIOGRAFIA NUEVA id ES UN ARRAY SINO ES UN NUMERO Y RETORNO LA RADIOGRAFIA POR ID
- $radiografia = new Radiografia($id);
- if ($radiografia) {
- $this->radiografias[$radiografia->id] = $radiografia;
- return $radiografia;
- }
- }
- return false;
- }
- public function setInfo($data){
- $q = "UPDATE tratamientos SET ";
- foreach ($data as $k => $v) {
- if (preg_match('/^fecha_hora_inicio|estado|duracion|tecnica|presupuesto|descripcion$/', $k)) {
- if ($k == 'fecha_hora_inicio') {
- $v = date('Y-m-d H:i:s', strtotime($v));
- }
- $q .= $k . "='" . $v ."',";
- }
- }
- $q = rtrim($q ,",") . " WHERE id_tratamiento = '{$this->id}'";
- $this->db->query($q);
- return $this->select();
- }
- public function select($data = '*'){
- if (is_string($data)) {
- $data = array($data);
- }
- if (is_array($data)) {
- $keys = array();
- if (in_array('fecha', $data)) {
- $keys[] = 'fecha_hora_inicio';
- }
- // VALIDO LOS CAMPOS SOLICITADOS, LOS ID PUEDEN VENIR SIN EL PREFIJO
- foreach ($data as $field) {
- if (preg_match('/^(?:fecha_hora_inici|estad|presupuest|eliminad)o|tecnica|(?:descrip|dura)cion|\*$/', $field)) {
- $keys[] = $field;
- }
- else{
- preg_match('/^(?:id_)?(examen|paciente|historia|diagnostico|resumen|odontograma)$/', $field, $m);
- if (count($m)) {
- $keys[] = 'id_' . $m[1];
- }
- }
- }
- // SI PIDE EL AVANCE SE NECESITAN DOS CAMPOS MAS
- if (in_array('avance', $data)) {
- $keys[] = 'fecha_hora_inicio';
- $keys[] = 'duracion';
- }
- // SI HAY CAMPOS VALIDADOS
- if (count($keys)) {
- $q = "SELECT " . implode(', ', array_unique($keys)) . " FROM tratamientos WHERE id_tratamiento = {$this->id}";
- $tratamiento = $this->db->oneRowQuery($q);
- if ($tratamiento) {
- foreach ($tratamiento as $k => $v) {
- if ($k == 'fecha_hora_inicio') {
- $v = date('d-m-Y', strtotime($v));
- }
- elseif($k == 'estado'){
- $v = is_numeric($v) ? constant('TRATAMIENTO_ESTADO_' . $v) : $v;
- }
- elseif($k == 'tecnica'){
- $v = is_numeric($v) ? constant('TECNICA_' . $v) : $v;
- }
- $this->{str_replace('id_', '', $k)} = utf8_encode($v);
- }
- if (array_key_exists('fecha_hora_inicio', $tratamiento)) {
- $this->fecha = date('d-m-Y', strtotime($tratamiento['fecha_hora_inicio']));
- }
- }
- // SI PIDIO EL AVANCE LO CALCULO
- if (in_array('avance', $data)) {
- $this->avance = (int) progress($this->fecha_hora_inicio, $this->duracion);
- }
- return $this;
- }
- }
- $this->url = crypt_params(array(TRATAMIENTO => $this->id, PACIENTE => $this->paciente));
- return false;
- }
- }
- class File {
- public function __isset($name) {
- return isset($this->{$name});
- }
- public function __set($name, $value) {
- $this->{$name} = $value;
- }
- /**
- * concatena los datos para obtener la url necesaria para ver
- *
- * @return sting url para ver la radiografia
- **/
- public function url_ver(){
- return URL_ROOT . '/' . strtolower(get_class($this)) . 's/ver/' . $this->url;
- }
- /**
- * concatena los datos para obtener la url para editar
- *
- * @return sting url para editar la radiografia
- **/
- public function url_editar(){
- return URL_ROOT . '/' . strtolower(get_class($this)) . 's/editar/' . $this->url;
- }
- public function eliminar(){
- $this->eliminado = 1;
- $this->update();
- }
- public function select(){
- if (!$this->id) {
- return false;
- }
- $name = strtolower(get_class($this));
- $extra_field = $name == 'cefalometria' ? ', tipo' : null;
- $q = "SELECT X.eliminado, X.etapa, X.id_tratamiento AS tratamiento, X.fecha_hora, X.datos_json, T.id_paciente AS paciente{$extra_field} FROM {$name}s AS X INNER JOIN tratamientos AS T ON T.id_tratamiento = X.id_tratamiento WHERE X.id_{$name} = {$this->id}";
- $_ = $this->db->oneRowQuery($q);
- $this->fecha_hora = date('d-m-Y', strtotime($_['fecha_hora']));
- $this->datos_json = (array) json_decode($_['datos_json']);
- $this->cantidad = count((array) end($this->datos_json));
- $this->key = key($this->datos_json);
- $this->etapa = constant('ETAPA_' . $_['etapa']);
- $this->_etapa = $_['etapa'];
- $this->tratamiento = new Tratamiento($_['tratamiento']);
- $this->url = crypt_params(array(constant(strtoupper($name)) => $this->id, TRATAMIENTO => $this->tratamiento->id, PACIENTE => $this->tratamiento->paciente->id));
- if ($extra_field) {
- $this->tipo =constant('C_TIPO_' . $_['tipo']);
- $this->_tipo = $_['tipo'];
- }
- return $this;
- }
- public function update($data = null){
- // ES NECESARIO EL id Y LA INFO
- if (!$this->id) {
- return false;
- }
- $name = strtolower(get_class($this));
- $q = "UPDATE {$name}s SET ";
- if (!is_array($data)) {
- $data = array('datos_json' => json_encode($this->datos_json), 'etapa' => $this->_etapa, 'eliminado' => $this->eliminado);
- }
- if ($name == 'cefalometria'){
- $data['tipo'] = $this->_tipo;
- }
- foreach ($data as $k => $v) {
- if (preg_match('/^datos_json|etapa|eliminado|tipo$/', $k)) {
- $q .= $k . "='" . $v ."',";
- }
- }
- $q = rtrim($q ,",") . " WHERE id_{$name} = '{$this->id}'";
- $this->db->query($q);
- $this->db->free();
- return $this->select();
- }
- }
- class Radiografia extends File{
- public function __get($name) {
- if ($name == 'db') {
- return MySQL::getInstance();
- }
- // elseif (preg_match('/^$/', $name)) {
- // if (!isset($this->{$name})) {
- // return $this->select($name)->{$name};
- // }
- // }
- elseif (isset($this->{$name})) {
- return $this->{$name};
- }
- return null;
- }
- public function __construct($id = null, $data = null){
- // SI id ES UN ARRAY ES PARA INSERTAR UNO NUEVO
- if (is_array($id)) {
- if (isset($id['datos_json'], $id['id_tratamiento'])) {
- // VALIDO LOS VALORES ENVIADOS
- if (!json_decode($id['datos_json']) || !is_numeric($id['id_tratamiento'])) {
- return false;
- }
- else{
- if( isset($id['etapa']) || !is_numeric($id['etapa'])){
- $id['etapa'] = 1;
- }
- // SI LOS DATOS SON VALIDOS LOS INSERTO EN LA BASE
- $date = date('Y-m-d H:i:s');
- $json = stripslashes($id['datos_json']);
- $q = "INSERT INTO radiografias (id_tratamiento, fecha_hora, datos_json, etapa) VALUES ({$id['id_tratamiento']}, '{$date}', '{$json}', {$id['etapa']})";
- $this->db->query($q);
- $this->id = $this->db->lastID();
- }
- }
- }
- // SI id ES UN NUEMERO LEVANTO LA INFO DE LA BBDD
- if (is_numeric($id) && $data == null) {
- $this->id = $id;
- }
- // TENGO EL id SETEADO ASIQUE TRAIGO LOS DATOS
- return $this->select();
- }
- public function update($data = null){
- // ES NECESARIO EL id Y LA INFO
- if (!$this->id) {
- return false;
- }
- $q = "UPDATE radiografias SET ";
- if (!is_array($data)) {
- $data = array('datos_json' => json_encode($this->datos_json), 'etapa' => $this->_etapa, 'eliminado' => $this->eliminado);
- }
- foreach ($data as $k => $v) {
- if (preg_match('/^datos_json|etapa|eliminado$/', $k)) {
- $q .= $k . "='" . $v ."',";
- }
- }
- $q = rtrim($q ,",") . " WHERE id_radiografia = '{$this->id}'";
- $this->db->query($q);
- $this->db->free();
- return $this->select();
- }
- public function select(){
- if (!$this->id) {
- return false;
- }
- $q = "SELECT R.eliminado, R.etapa, R.id_tratamiento AS tratamiento, R.fecha_hora, R.datos_json, T.id_paciente AS paciente FROM radiografias AS R INNER JOIN tratamientos AS T ON T.id_tratamiento = R.id_tratamiento WHERE R.id_radiografia = {$this->id}";
- $_ = $this->db->oneRowQuery($q);
- $this->fecha_hora = date('d-m-Y', strtotime($_['fecha_hora']));
- $this->datos_json = (array) json_decode($_['datos_json']);
- $this->cantidad = count((array) end($this->datos_json));
- $this->key = key($this->datos_json);
- $this->etapa = constant('ETAPA_' . $_['etapa']);
- $this->_etapa = $_['etapa'];
- $this->tratamiento = new Tratamiento($_['tratamiento']);
- $this->url = crypt_params(array(RADIOGRAFIA => $this->id, TRATAMIENTO => $this->tratamiento->id, PACIENTE => $this->tratamiento->paciente->id));
- return $this;
- }
- }
- class Fotografia extends File{
- public function __get($name) {
- if ($name == 'db') {
- return MySQL::getInstance();
- }
- elseif (isset($this->{$name})) {
- return $this->{$name};
- }
- return null;
- }
- public function __construct($id = null, $data = null){
- // SI id ES UN ARRAY ES PARA INSERTAR UNO NUEVO
- if (is_array($id)) {
- if (isset($id['datos_json'], $id['id_tratamiento'])) {
- // VALIDO LOS VALORES ENVIADOS
- if (!json_decode($id['datos_json']) || !is_numeric($id['id_tratamiento'])) {
- return false;
- }
- else{
- if(!isset($id['etapa']) || !is_numeric($id['etapa'])){
- $id['etapa'] = 1;
- }
- // SI LOS DATOS SON VALIDOS LOS INSERTO EN LA BASE
- $date = date('Y-m-d H:i:s');
- $json = stripslashes($id['datos_json']);
- $q = "INSERT INTO fotografias (id_tratamiento, fecha_hora, datos_json, etapa) VALUES ({$id['id_tratamiento']}, '{$date}', '{$json}', {$id['etapa']})";
- $this->db->query($q);
- $this->id = $this->db->lastID();
- }
- }
- }
- // SI id ES UN NUEMERO LEVANTO LA INFO DE LA BBDD
- if (is_numeric($id) && $data == null) {
- $this->id = $id;
- }
- // CON id SETEADO TRAIGO LOS DATOS
- return $this->select();
- }
- }
- class Cefalometria extends File{
- public function __get($name) {
- if ($name == 'db') {
- return MySQL::getInstance();
- }
- elseif (isset($this->{$name})) {
- return $this->{$name};
- }
- return null;
- }
- public function __construct($id = null, $data = null){
- // SI id ES UN ARRAY ES PARA INSERTAR UNO NUEVO
- if (is_array($id)) {
- if (isset($id['datos_json'], $id['id_tratamiento'])) {
- // VALIDO LOS VALORES ENVIADOS
- if (!json_decode($id['datos_json']) || !is_numeric($id['id_tratamiento'])) {
- return false;
- }
- else{
- if(!isset($id['etapa']) || !is_numeric($id['etapa'])){
- $id['etapa'] = 1;
- }
- if(!isset($id['tipo']) || !is_numeric($id['tipo'])){
- $id['tipo'] = 5;
- }
- // SI LOS DATOS SON VALIDOS LOS INSERTO EN LA BASE
- $date = date('Y-m-d H:i:s');
- $json = stripslashes($id['datos_json']);
- $q = "INSERT INTO cefalometrias (id_tratamiento, fecha_hora, datos_json, etapa, tipo) VALUES ({$id['id_tratamiento']}, '{$date}', '{$json}', {$id['etapa']}, {$id['tipo']})";
- $this->db->query($q);
- $this->id = $this->db->lastID();
- }
- }
- }
- // SI id ES UN NUEMERO LEVANTO LA INFO DE LA BBDD
- if (is_numeric($id) && $data == null) {
- $this->id = $id;
- }
- // CON id SETEADO TRAIGO LOS DATOS
- return $this->select();
- }
- }
- class Odontograma {
- public function __get($name) {
- if ($name == 'db') {
- return MySQL::getInstance();
- }
- elseif ($name == 'url') {
- $this->url = crypt_params(array(ODONTOGRAMA => $this->id, TRATAMIENTO => $this->tratamiento->id, PACIENTE => $this->tratamiento->paciente->id));
- return $this->url;
- }
- elseif ($name == 'tratamiento') {
- $q = "SELECT id_tratamiento AS id FROM tratamientos WHERE id_odontograma = {$this->id}";
- $this->tratamiento = new Tratamiento($this->db->oneFieldQuery($q));
- return $this->tratamiento;
- }
- elseif (isset($this->{$name})) {
- return $this->{$name};
- }
- return null;
- }
- function __construct($id = null){
- if (!$id) {
- $q = "INSERT INTO odontogramas (datos_json) VALUES ('{}')";
- $this->db->query($q);
- $id = $this->db->lastID();
- }
- $this->id = $id;
- return $this->select();
- }
- public function select(){
- if (!$this->id || !is_numeric($this->id)) {
- return false;
- }
- $q = "SELECT datos_json FROM odontogramas WHERE id_odontograma = {$this->id}";
- $_ = $this->db->oneFieldQuery($q);
- $this->datos_json = (array) json_decode($_);
- return $this;
- }
- public function update($data = null){
- // ES NECESARIO EL id Y LA INFO
- if (!$this->id) {
- return false;
- }
- $name = strtolower(get_class($this));
- if (!is_string($this->datos_json)) {
- $this->datos_json = json_encode($this->datos_json);
- }
- $q = "UPDATE odontogramas SET datos_json = '{$this->datos_json}' WHERE id_odontograma = '{$this->id}'";
- $this->db->query($q);
- $this->db->free();
- return $this->select();
- }
- }
- class Pago {
- public function __get($name) {
- if ($name == 'db') {
- return MySQL::getInstance();
- }
- elseif ($name == 'tratamiento') {
- $q = "SELECT id_tratamiento AS id FROM tratamientos WHERE id_odontograma = {$this->id}";
- $this->tratamiento = new Tratamiento($this->db->oneFieldQuery($q));
- return $this->tratamiento;
- }
- elseif (isset($this->{$name})) {
- return $this->{$name};
- }
- return null;
- }
- public function __construct($id){
- if (!$id) {
- return false;
- }
- if (is_numeric($id)) {
- $this->id = $id;
- }
- elseif(is_array($id)) {
- // CAMPOS NECESARIOS
- if (!isset($id['id_tratamiento'], $id['monto']) && !is_numeric($id['id_tratamiento']) && !is_numeric($id['monto'])) {
- return false;
- }
- $this->tratamiento = new Tratamiento($id['id_tratamiento']);
- foreach ($id as $k => $v) {
- if (preg_match('/^id_tratamiento|monto|anotaciones|motivo$/', $k)) {
- $keys[] = $k;
- $values[] = $this->db->escape($v);
- }
- }
- $keys[] = 'fecha_hora';
- if (!isset($id['fecha_hora']) || !$id['fecha_hora']) {
- $values[] = date('Y-m-d H:i:s');
- }
- else{
- $values[] = date('Y-m-d H:i:s', strtotime($id['fecha_hora']));
- }
- $keys[] = 'acumulado';
- $acumulado = $this->tratamiento->acumulado() + ($id['monto'] + 0);
- $values[] = $acumulado;
- $keys[] = 'balance';
- $values[] = ($this->tratamiento->presupuesto + 0) - $acumulado;
- $q = "INSERT INTO pagos (" . implode(",", $keys) . ") VALUES ('" . implode("','", $values) . "')";
- $this->db->query($q);
- $this->id = $this->db->lastID();
- }
- return $this->select();
- }
- /**
- * concatena los datos para obtener la url necesaria
- * @param string $action 'editar' | 'ver' devuelven la url para segun sea ver o editar los detalles del pago, sino retorna url
- * @return sting url para ver la radiografia
- **/
- public function url($action = null){
- if (preg_match('/^ver|editar$/', $action)) {
- return URL_ROOT . '/economia/' . $action . '/' . $this->url;
- }
- return $this->url;
- }
- public function select($data = '*'){
- if (!$this->id || !is_numeric($this->id)) {
- return false;
- }
- $keys = ' * ';
- if ($data != '*') {
- if (!is_array($data)) {
- $data = array($data);
- }
- foreach ($data as $k) {
- if (is_string($k)) {
- if (preg_match('/^monto|anotaciones|motivo|acumulado|balance$/', $k)) {
- $keys[] = $k;
- }
- elseif (preg_match('/^(:?id_)tratamiento$/', $k)) {
- $keys[] = 'id_tratamiento AS tratamiento';
- }
- }
- }
- $keys = implode(', ', $keys);
- }
- $q = "SELECT {$keys} FROM pagos WHERE id_pago = {$this->id}";
- $_ = $this->db->oneRowQuery($q);
- if ($_) {
- foreach ($_ as $k => $v) {
- if ($k == 'fecha_hora') {
- $this->{$k} = date('d-m-Y', strtotime($v));
- }
- elseif (preg_match('/^monto|anotaciones|motivo|acumulado|balance$/', $k)) {
- $this->{$k} = utf8_encode($v);
- }
- elseif (preg_match('/^(:?id_)tratamiento$/', $k)) {
- $this->tratamiento = new Tratamiento($v);
- }
- }
- $this->url = crypt_params(array(PAGO => $this->id, TRATAMIENTO => $this->tratamiento->id, PACIENTE => $this->tratamiento->paciente->id));
- return $this;
- }
- unset($this->id);
- }
- public function eliminar() {
- $tratamiento = $this->tratamiento;
- $q = "DELETE FROM pagos WHERE id_pago = '{$this->id}';";
- $this->db->query($q);
- }
- public function update($data = null){
- $q = "UPDATE pagos SET acumulado ='{$this->acumulado}', balance ='{$this->balance}' WHERE id_pago = '{$this->id}';";
- $this->db->query($q);
- return $this;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement