@@ -659,7 +659,7 @@ protected function getDatosAgrupadosRef(array $data, array $keys): array
659659 return $ agrupados ;
660660 }
661661
662- protected function getInformeComprasDataWhere (): string
662+ protected function getInformeComprasDataWhere (bool $ withLineAlias = true ): string
663663 {
664664 $ sql = '' ;
665665
@@ -688,7 +688,14 @@ protected function getInformeComprasDataWhere(): string
688688 }
689689
690690 if (!empty ($ this ->variant ->id ())) {
691- $ sql .= " AND l.referencia = " . $ this ->dataBase ->var2str ($ this ->variant ->referencia );
691+ if ($ withLineAlias ) {
692+ $ sql .= " AND l.referencia = " . $ this ->dataBase ->var2str ($ this ->variant ->referencia );
693+ } else {
694+ $ lineTable = $ this ->type === 'invoices ' ? 'lineasfacturasprov ' : 'lineasalbaranesprov ' ;
695+ $ code = $ this ->type === 'invoices ' ? 'idfactura ' : 'idalbaran ' ;
696+ $ sql .= " AND d. " . $ code . " IN (SELECT " . $ code . " FROM " . $ lineTable
697+ . " WHERE referencia = " . $ this ->dataBase ->var2str ($ this ->variant ->referencia ) . ") " ;
698+ }
692699 }
693700
694701 return $ sql ;
@@ -704,7 +711,7 @@ protected function getInformeComprasDocumentData(): array
704711 . " WHERE d.fecha >= " . $ this ->dataBase ->var2str ($ this ->desde )
705712 . " AND d.fecha <= " . $ this ->dataBase ->var2str ($ this ->hasta )
706713 . " AND d.idempresa = " . $ this ->dataBase ->var2str ($ this ->idempresa )
707- . $ this ->getInformeComprasDataWhere ()
714+ . $ this ->getInformeComprasDataWhere (false )
708715 . " ORDER BY d.fecha ASC, d.hora ASC; " ;
709716
710717 return $ this ->dataBase ->select ($ sql );
@@ -713,17 +720,14 @@ protected function getInformeComprasDocumentData(): array
713720 protected function getInformeComprasNetoData (): array
714721 {
715722 $ table = $ this ->type === 'invoices ' ? 'facturasprov ' : 'albaranesprov ' ;
716- $ line = $ this ->type === 'invoices ' ? 'lineasfacturasprov ' : 'lineasalbaranesprov ' ;
717723 $ code = $ this ->type === 'invoices ' ? 'idfactura ' : 'idalbaran ' ;
718724
719- $ sql = "SELECT d.codproveedor, d.nombre, d.fecha, SUM( d.neto) as total "
725+ $ sql = "SELECT d.codproveedor, d.nombre, d.fecha, d.neto as total "
720726 . " FROM " . $ table . " d "
721- . " LEFT JOIN " . $ line . " l ON d. " . $ code . " = l. " . $ code
722727 . " WHERE d.fecha >= " . $ this ->dataBase ->var2str ($ this ->desde )
723728 . " AND d.fecha <= " . $ this ->dataBase ->var2str ($ this ->hasta )
724729 . " AND d.idempresa = " . $ this ->dataBase ->var2str ($ this ->idempresa )
725- . $ this ->getInformeComprasDataWhere ()
726- . " GROUP BY d.codproveedor, d.nombre, d.fecha "
730+ . $ this ->getInformeComprasDataWhere (false )
727731 . " ORDER BY d.codproveedor ASC, d.fecha DESC; " ;
728732
729733 $ data = $ this ->dataBase ->select ($ sql );
@@ -761,7 +765,7 @@ protected function getInformeComprasUnidadesData(): array
761765 return $ this ->getDatosAgrupadosRef ($ data , ['codproveedor ' , 'nombre ' ]);
762766 }
763767
764- protected function getInformeVentasDataWhere (): string
768+ protected function getInformeVentasDataWhere (bool $ withLineAlias = true ): string
765769 {
766770 $ sql = '' ;
767771 if (!empty ($ this ->cliente ->id ())) {
@@ -809,7 +813,14 @@ protected function getInformeVentasDataWhere(): string
809813 }
810814
811815 if (!empty ($ this ->variant ->id ())) {
812- $ sql .= " AND l.referencia = " . $ this ->dataBase ->var2str ($ this ->variant ->referencia );
816+ if ($ withLineAlias ) {
817+ $ sql .= " AND l.referencia = " . $ this ->dataBase ->var2str ($ this ->variant ->referencia );
818+ } else {
819+ $ lineTable = $ this ->type === 'invoices ' ? 'lineasfacturascli ' : 'lineasalbaranescli ' ;
820+ $ code = $ this ->type === 'invoices ' ? 'idfactura ' : 'idalbaran ' ;
821+ $ sql .= " AND d. " . $ code . " IN (SELECT " . $ code . " FROM " . $ lineTable
822+ . " WHERE referencia = " . $ this ->dataBase ->var2str ($ this ->variant ->referencia ) . ") " ;
823+ }
813824 }
814825
815826 return $ sql ;
@@ -818,17 +829,13 @@ protected function getInformeVentasDataWhere(): string
818829 protected function getInformeVentasNetoData (): array
819830 {
820831 $ table = $ this ->type === 'invoices ' ? 'facturascli ' : 'albaranescli ' ;
821- $ line = $ this ->type === 'invoices ' ? 'lineasfacturascli ' : 'lineasalbaranescli ' ;
822- $ code = $ this ->type === 'invoices ' ? 'idfactura ' : 'idalbaran ' ;
823832
824- $ sql = "SELECT d.codcliente, d.nombrecliente, d.fecha, SUM( d.neto) as total "
833+ $ sql = "SELECT d.codcliente, d.nombrecliente, d.fecha, d.neto as total "
825834 . " FROM " . $ table . " d "
826- . " LEFT JOIN " . $ line . " l ON d. " . $ code . " = l. " . $ code
827835 . " WHERE d.fecha >= " . $ this ->dataBase ->var2str ($ this ->desde )
828836 . " AND d.fecha <= " . $ this ->dataBase ->var2str ($ this ->hasta )
829837 . " AND d.idempresa = " . $ this ->dataBase ->var2str ($ this ->idempresa )
830- . $ this ->getInformeVentasDataWhere ()
831- . " GROUP BY d.codalmacen, d.codcliente, d.nombrecliente, d.fecha "
838+ . $ this ->getInformeVentasDataWhere (false )
832839 . " ORDER BY d.codcliente ASC, d.fecha DESC; " ;
833840
834841 $ data = $ this ->dataBase ->select ($ sql );
@@ -843,16 +850,14 @@ protected function getInformeVentasNetoData(): array
843850 protected function getInformeVentasDocumentData (): array
844851 {
845852 $ table = $ this ->type === 'invoices ' ? 'facturascli ' : 'albaranescli ' ;
846- $ line = $ this ->type === 'invoices ' ? 'lineasfacturascli ' : 'lineasalbaranescli ' ;
847853 $ code = $ this ->type === 'invoices ' ? 'idfactura ' : 'idalbaran ' ;
848854
849855 $ sql = "SELECT d. " . $ code . " as id, d.codigo, d.codcliente, d.nombrecliente, d.fecha, d.total "
850856 . " FROM " . $ table . " d "
851- . " LEFT JOIN " . $ line . " l ON d. " . $ code . " = l. " . $ code
852857 . " WHERE d.fecha >= " . $ this ->dataBase ->var2str ($ this ->desde )
853858 . " AND d.fecha <= " . $ this ->dataBase ->var2str ($ this ->hasta )
854859 . " AND d.idempresa = " . $ this ->dataBase ->var2str ($ this ->idempresa )
855- . $ this ->getInformeVentasDataWhere ()
860+ . $ this ->getInformeVentasDataWhere (false )
856861 . " ORDER BY d.fecha ASC, d.hora ASC; " ;
857862
858863 return $ this ->dataBase ->select ($ sql );
0 commit comments