private function get_movimientos($ref, $desde = '', $hasta = '', $codagente = '')
 {
     $mlist = array();
     $regularizacion = new regularizacion_stock();
     foreach ($regularizacion->all_from_articulo($ref) as $reg) {
         $anyadir = TRUE;
         if ($desde != '') {
             if (strtotime($desde) > strtotime($reg->fecha)) {
                 $anyadir = FALSE;
             }
         }
         if ($hasta != '') {
             if (strtotime($hasta) < strtotime($reg->fecha)) {
                 $anyadir = FALSE;
             }
         }
         if ($anyadir) {
             $mlist[] = array('referencia' => $ref, 'codalmacen' => $reg->codalmacendest, 'origen' => 'Regularización', 'url' => 'index.php?page=ventas_articulo&ref=' . $ref, 'clipro' => '-', 'movimiento' => '-', 'precio' => 0, 'dto' => 0, 'final' => $reg->cantidadfin, 'fecha' => $reg->fecha, 'hora' => $reg->hora);
         }
     }
     /// forzamos la comprobación de las tablas de albaranes
     $albc = new albaran_cliente();
     $lin1 = new linea_albaran_cliente();
     $albp = new albaran_proveedor();
     $lin2 = new linea_albaran_proveedor();
     $sql_extra = '';
     if ($desde != '') {
         $sql_extra .= " AND fecha >= " . $this->empresa->var2str($desde);
     }
     if ($hasta != '') {
         $sql_extra .= " AND fecha <= " . $this->empresa->var2str($hasta);
     }
     if ($codagente != '') {
         $sql_extra .= " AND codagente = " . $this->empresa->var2str($codagente);
     }
     /// buscamos el artículo en albaranes de compra
     $sql = "SELECT a.codigo,l.cantidad,l.pvpunitario,l.dtopor,a.fecha,a.hora" . ",a.codalmacen,a.idalbaran,a.codproveedor,a.nombre" . " FROM albaranesprov a, lineasalbaranesprov l" . " WHERE a.idalbaran = l.idalbaran AND l.referencia = " . $albc->var2str($ref) . $sql_extra;
     $data = $this->db->select_limit($sql, 1000, 0);
     if ($data) {
         foreach ($data as $d) {
             $mlist[] = array('referencia' => $ref, 'codalmacen' => $d['codalmacen'], 'origen' => 'Albaran compra ' . $d['codigo'], 'url' => 'index.php?page=compras_albaran&id=' . $d['idalbaran'], 'clipro' => $d['codproveedor'] . ' - ' . $d['nombre'], 'movimiento' => floatval($d['cantidad']), 'precio' => floatval($d['pvpunitario']), 'dto' => floatval($d['dtopor']), 'final' => 0, 'fecha' => date('d-m-Y', strtotime($d['fecha'])), 'hora' => $d['hora']);
         }
     }
     /// buscamos el artículo en facturas de compra
     $sql = "SELECT f.codigo,l.cantidad,l.pvpunitario,l.dtopor,f.fecha,f.hora" . ",f.codalmacen,f.idfactura,f.codproveedor,f.nombre" . " FROM facturasprov f, lineasfacturasprov l" . " WHERE f.idfactura = l.idfactura AND l.idalbaran IS NULL" . " AND l.referencia = " . $albc->var2str($ref) . $sql_extra;
     $data = $this->db->select_limit($sql, 1000, 0);
     if ($data) {
         foreach ($data as $d) {
             $mlist[] = array('referencia' => $ref, 'codalmacen' => $d['codalmacen'], 'origen' => 'Factura compra ' . $d['codigo'], 'url' => 'index.php?page=compras_factura&id=' . $d['idfactura'], 'clipro' => $d['codproveedor'] . ' - ' . $d['nombre'], 'movimiento' => floatval($d['cantidad']), 'precio' => floatval($d['pvpunitario']), 'dto' => floatval($d['dtopor']), 'final' => 0, 'fecha' => date('d-m-Y', strtotime($d['fecha'])), 'hora' => $d['hora']);
         }
     }
     /// buscamos el artículo en albaranes de venta
     $sql = "SELECT a.codigo,l.cantidad,l.pvpunitario,l.dtopor,a.fecha,a.hora" . ",a.codalmacen,a.idalbaran,a.codcliente,a.nombrecliente" . " FROM albaranescli a, lineasalbaranescli l" . " WHERE a.idalbaran = l.idalbaran AND l.referencia = " . $albc->var2str($ref) . $sql_extra;
     $data = $this->db->select_limit($sql, 1000, 0);
     if ($data) {
         foreach ($data as $d) {
             $mlist[] = array('referencia' => $ref, 'codalmacen' => $d['codalmacen'], 'origen' => 'Albaran venta ' . $d['codigo'], 'url' => 'index.php?page=ventas_albaran&id=' . $d['idalbaran'], 'clipro' => $d['codcliente'] . ' - ' . $d['nombrecliente'], 'movimiento' => 0 - floatval($d['cantidad']), 'precio' => floatval($d['pvpunitario']), 'dto' => floatval($d['dtopor']), 'final' => 0, 'fecha' => date('d-m-Y', strtotime($d['fecha'])), 'hora' => $d['hora']);
         }
     }
     /// buscamos el artículo en facturas de venta
     $sql = "SELECT f.codigo,l.cantidad,l.pvpunitario,l.dtopor,f.fecha,f.hora" . ",f.codalmacen,f.idfactura,f.codcliente,f.nombrecliente" . " FROM facturascli f, lineasfacturascli l" . " WHERE f.idfactura = l.idfactura AND l.idalbaran IS NULL" . " AND l.referencia = " . $albc->var2str($ref) . $sql_extra;
     $data = $this->db->select_limit($sql, 1000, 0);
     if ($data) {
         foreach ($data as $d) {
             $mlist[] = array('referencia' => $ref, 'codalmacen' => $d['codalmacen'], 'origen' => 'Factura venta ' . $d['codigo'], 'url' => 'index.php?page=ventas_factura&id=' . $d['idfactura'], 'clipro' => $d['codcliente'] . ' - ' . $d['nombrecliente'], 'movimiento' => 0 - floatval($d['cantidad']), 'precio' => floatval($d['pvpunitario']), 'dto' => floatval($d['dtopor']), 'final' => 0, 'fecha' => date('d-m-Y', strtotime($d['fecha'])), 'hora' => $d['hora']);
         }
     }
     /// ordenamos por fecha y hora
     usort($mlist, function ($a, $b) {
         if (strtotime($a['fecha'] . ' ' . $a['hora']) == strtotime($b['fecha'] . ' ' . $b['hora'])) {
             return 0;
         } else {
             if (strtotime($a['fecha'] . ' ' . $a['hora']) < strtotime($b['fecha'] . ' ' . $b['hora'])) {
                 return -1;
             } else {
                 return 1;
             }
         }
     });
     /// recalculamos
     $inicial = 0;
     foreach (array_reverse($mlist) as $i => $value) {
         if ($value['movimiento'] == '-') {
             $inicial = $value['final'];
         } else {
             $inicial -= $value['movimiento'];
         }
     }
     $total = max(array($inicial, 0));
     foreach ($mlist as $i => $value) {
         if ($value['movimiento'] == '-') {
             $total = $value['final'];
         } else {
             $total += $value['movimiento'];
         }
         $mlist[$i]['final'] = $total;
     }
     return $mlist;
 }
Esempio n. 2
0
 protected function private_core()
 {
     /// ¿El usuario tiene permiso para eliminar en esta página?
     $this->allow_delete = $this->user->allow_delete_on(__CLASS__);
     $this->coddivisa = $this->empresa->coddivisa;
     $this->pagado = FALSE;
     $this->pago = new pago();
     $this->pagos = array();
     $this->pendiente = 0;
     $this->pago->cron_job();
     if (isset($_GET['delete'])) {
         $pago = $this->pago->get($_GET['delete']);
         if ($pago) {
             if ($pago->delete()) {
                 $this->new_message('Pago eliminado correctamente.');
             } else {
                 $this->new_error_msg('Error al eliminar el pago.');
             }
         } else {
             $this->new_error_msg('Pago no encontrado.');
         }
     } else {
         if (isset($_POST['idpago'])) {
             $pago = $this->pago->get($_POST['idpago']);
             if ($pago) {
                 $pago->fecha = $_POST['fecha'];
                 $pago->importe = floatval($_POST['importe']);
                 $pago->nota = $_POST['nota'];
                 if ($pago->save()) {
                     $this->new_message('Pago modificado correctamente.');
                 } else {
                     $this->new_error_msg('Error al modificar el pago.');
                 }
             } else {
                 $this->new_error_msg('Pago no encontrado.');
             }
         } else {
             if (isset($_POST['importe'])) {
                 if (isset($_REQUEST['albaran'])) {
                     $this->pago->fase = ucfirst(FS_ALBARAN);
                     $this->pago->idalbaran = $_REQUEST['id'];
                 } else {
                     if (isset($_REQUEST['pedido'])) {
                         $this->pago->fase = ucfirst(FS_PEDIDO);
                         $this->pago->idpedido = $_REQUEST['id'];
                     }
                 }
                 $this->pago->fecha = $_POST['fecha'];
                 $this->pago->importe = floatval($_POST['importe']);
                 $this->pago->nota = $_POST['nota'];
                 if ($this->pago->save()) {
                     $this->new_message('Pago guardado correctamente.');
                 } else {
                     $this->new_error_msg('Error al guardar el pago.');
                 }
             }
         }
     }
     $this->bloquear = TRUE;
     if (isset($_REQUEST['albaran'])) {
         $alb0 = new albaran_cliente();
         $albaran = $alb0->get($_REQUEST['id']);
         if ($albaran) {
             /// buscamos pagos de la fase pedido
             /// un albarán puede ser una agrupación de muchos pedidos
             $idpedido = NULL;
             foreach ($albaran->get_lineas() as $linea) {
                 /// el idpedido lo tienes en las lineas del albarán
                 if ($linea->idpedido != $idpedido) {
                     $idpedido = $linea->idpedido;
                     $this->db->exec("UPDATE pagos SET idalbaran = " . $alb0->var2str($_REQUEST['id']) . " WHERE idpedido = " . $alb0->var2str($idpedido) . ";");
                 }
             }
             /// fase de albarán
             $this->pagos = $this->pago->all_from_albaran($_REQUEST['id']);
             $this->pendiente = $albaran->total;
             foreach ($this->pagos as $i => $value) {
                 $this->pendiente -= $value->importe;
                 $this->pagos[$i]->pendiente = $this->pendiente;
             }
             if (abs($this->pendiente) < 0.1) {
                 $this->pagado = TRUE;
             }
             if ($albaran->ptefactura) {
                 $this->bloquear = FALSE;
             }
         }
     } else {
         if (isset($_REQUEST['pedido'])) {
             /// fose de pedido
             $this->pagos = $this->pago->all_from_pedido($_REQUEST['id']);
             $ped0 = new pedido_cliente();
             $pedido = $ped0->get($_REQUEST['id']);
             if ($pedido) {
                 $this->pendiente = $pedido->total;
                 foreach ($this->pagos as $i => $value) {
                     $this->pendiente -= $value->importe;
                     $this->pagos[$i]->pendiente = $this->pendiente;
                 }
                 if (abs($this->pendiente) < 0.1) {
                     $this->pagado = TRUE;
                 }
                 if ($pedido->status == 0) {
                     $this->bloquear = FALSE;
                 }
             }
         }
     }
     $this->share_extensions();
 }