Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Controller/ReportProducto.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getPageData(): array
/**
* Filtros comunes a todos los documentos
*/
private function addCommonFilters(string $viewName, string $dateField, string $warehouseField): void
private function addCommonFilters(string $viewName, string $dateField, string $warehouseField, bool $showAgent = true): void
{
// periodo
$this->addFilterPeriod($viewName, 'fecha', 'date', $dateField);
Expand All @@ -60,10 +60,12 @@ private function addCommonFilters(string $viewName, string $dateField, string $w
$this->addFilterSelect($viewName, 'nick', 'user', 'nick', $users);
}

// agente
$agents = Agentes::codeModel();
if (count($agents) > 1) {
$this->addFilterSelect($viewName, 'codagente', 'agent', 'codagente', $agents);
// agente (solo aplica a documentos de venta, que son los que tienen codagente)
if ($showAgent) {
$agents = Agentes::codeModel();
if (count($agents) > 1) {
$this->addFilterSelect($viewName, 'codagente', 'agent', 'codagente', $agents);
}
}

// empresa
Expand Down Expand Up @@ -187,7 +189,7 @@ protected function createViewsSupplierDeliveryNotes(string $viewName = 'FacturaP
->addOrderBy(['stockfis'], 'stock');

// filtros
$this->addCommonFilters($viewName, 'albaranesprov.fecha', 'albaranesprov.codalmacen');
$this->addCommonFilters($viewName, 'albaranesprov.fecha', 'albaranesprov.codalmacen', false);

// proveedor
$this->addFilterAutocomplete($viewName, 'codproveedor', 'supplier', 'codproveedor', 'Proveedor', 'codproveedor', 'nombre');
Expand All @@ -203,7 +205,7 @@ protected function createViewsSupplierInvoices(string $viewName = 'FacturaProvee
->addOrderBy(['cantidad'], 'quantity', 2);

// filtros
$this->addCommonFilters($viewName, 'facturasprov.fecha', 'facturasprov.codalmacen');
$this->addCommonFilters($viewName, 'facturasprov.fecha', 'facturasprov.codalmacen', false);

// proveedor
$this->addFilterAutocomplete($viewName, 'codproveedor', 'supplier', 'codproveedor', 'Proveedor', 'codproveedor', 'nombre');
Expand Down
35 changes: 33 additions & 2 deletions Model/Join/FacturaClienteProducto.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace FacturaScripts\Plugins\Informes\Model\Join;

use FacturaScripts\Core\Template\JoinModel;
use FacturaScripts\Core\Where;

/**
* Description of FacturaClienteProducto
Expand All @@ -36,6 +37,37 @@ public function url(string $type = 'auto', string $list = 'List'): string
return 'ReportProducto';
}

/**
* Carga la descripción aparte para que no entre en el GROUP BY (columna TEXT).
*
* @param Where[] $where
*/
public static function all(array $where = [], array $order = [], int $offset = 0, int $limit = 0): array
{
$result = parent::all($where, $order, $offset, $limit);

$productIds = array_unique(array_filter(
array_map(fn($item) => $item->idproducto, $result),
fn($idproducto) => $idproducto !== null
));
if (empty($productIds)) {
return $result;
}

$descriptions = [];
$sql = 'SELECT idproducto, descripcion FROM productos WHERE idproducto IN ('
. implode(',', array_map('intval', $productIds)) . ')';
foreach (self::db()->select($sql) as $row) {
$descriptions[$row['idproducto']] = $row['descripcion'];
}

foreach ($result as $item) {
$item->descripcion = $descriptions[$item->idproducto] ?? null;
}

return $result;
}

protected function getFields(): array
{
return [
Expand All @@ -46,7 +78,6 @@ protected function getFields(): array
'codfabricante' => 'productos.codfabricante',
'codfamilia' => 'productos.codfamilia',
'coste' => 'variantes.coste',
'descripcion' => 'productos.descripcion',
'idproducto' => static::MAIN_TABLE . '.idproducto',
'precio' => 'variantes.precio',
'referencia' => static::MAIN_TABLE . '.referencia',
Expand All @@ -58,7 +89,7 @@ protected function getGroupFields(): string
{
return static::DOC_TABLE . '.codalmacen, ' . static::MAIN_TABLE . '.idproducto, '
. static::MAIN_TABLE . '.referencia, productos.codfabricante, productos.codfamilia, variantes.coste, '
. 'productos.descripcion, variantes.precio, stocks.cantidad';
. 'variantes.precio, stocks.cantidad';
}

protected function getSQLFrom(): string
Expand Down
35 changes: 33 additions & 2 deletions Model/Join/FacturaProveedorProducto.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace FacturaScripts\Plugins\Informes\Model\Join;

use FacturaScripts\Core\Template\JoinModel;
use FacturaScripts\Core\Where;

/**
* Description of FacturaProveedorProducto
Expand All @@ -36,6 +37,37 @@ public function url(string $type = 'auto', string $list = 'List'): string
return 'ReportProducto';
}

/**
* Carga la descripción aparte para que no entre en el GROUP BY (columna TEXT).
*
* @param Where[] $where
*/
public static function all(array $where = [], array $order = [], int $offset = 0, int $limit = 0): array
{
$result = parent::all($where, $order, $offset, $limit);

$productIds = array_unique(array_filter(
array_map(fn($item) => $item->idproducto, $result),
fn($idproducto) => $idproducto !== null
));
if (empty($productIds)) {
return $result;
}

$descriptions = [];
$sql = 'SELECT idproducto, descripcion FROM productos WHERE idproducto IN ('
. implode(',', array_map('intval', $productIds)) . ')';
foreach (self::db()->select($sql) as $row) {
$descriptions[$row['idproducto']] = $row['descripcion'];
}

foreach ($result as $item) {
$item->descripcion = $descriptions[$item->idproducto] ?? null;
}

return $result;
}

protected function getFields(): array
{
return [
Expand All @@ -45,7 +77,6 @@ protected function getFields(): array
'codfabricante' => 'productos.codfabricante',
'codfamilia' => 'productos.codfamilia',
'coste' => 'variantes.coste',
'descripcion' => 'productos.descripcion',
'idproducto' => static::MAIN_TABLE . '.idproducto',
'precio' => 'variantes.precio',
'referencia' => static::MAIN_TABLE . '.referencia',
Expand All @@ -57,7 +88,7 @@ protected function getGroupFields(): string
{
return static::DOC_TABLE . '.codalmacen, ' . static::MAIN_TABLE . '.idproducto, '
. static::MAIN_TABLE . '.referencia, productos.codfabricante, productos.codfamilia, variantes.coste, '
. 'productos.descripcion, variantes.precio, stocks.cantidad';
. 'variantes.precio, stocks.cantidad';
}

protected function getSQLFrom(): string
Expand Down