mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
116 lines
3.9 KiB
PHP
116 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Pedidos;
|
|
|
|
class PedidoLineaModel extends \App\Models\BaseModel
|
|
{
|
|
protected $table = "pedidos_linea";
|
|
|
|
/**
|
|
* Whether primary key uses auto increment.
|
|
*
|
|
* @var bool
|
|
*/
|
|
protected $useAutoIncrement = true;
|
|
|
|
const SORTABLE = [
|
|
0 => "t1.id",
|
|
1 => "t1.estado",
|
|
2 => "t1.total_precio",
|
|
3 => "t1.total_tirada",
|
|
];
|
|
|
|
protected $allowedFields = [
|
|
"pedido_id",
|
|
"presupuesto_id",
|
|
"ubicacion_id",
|
|
"user_created_id",
|
|
"user_updated_id",
|
|
"created_at",
|
|
"updated_at",
|
|
];
|
|
protected $returnType = "App\Entities\Pedidos\PedidoLineaEntity";
|
|
|
|
protected $useTimestamps = true;
|
|
protected $useSoftDeletes = false;
|
|
|
|
protected $createdField = "created_at";
|
|
protected $updatedField = "updated_at";
|
|
|
|
public static $labelField = "id";
|
|
|
|
|
|
public function getResource(string $search = "", $estado="")
|
|
{
|
|
$builder = $this->db
|
|
->table($this->table . " t1")
|
|
->select(
|
|
"t2.id AS id, t2.updated_at AS fecha, t2.fecha_entrega_real AS fecha_entrega,
|
|
t4.nombre AS cliente, CONCAT(t5.first_name, ' ', t5.last_name) AS comercial,
|
|
t3.titulo AS titulo, t6.nombre AS ubicacion, t3.inc_rei AS inc_rei,
|
|
t3.paginas AS paginas, t3.tirada AS tirada, t3.total_aceptado AS total_presupuesto,
|
|
t2.estado AS estado"
|
|
);
|
|
|
|
$builder->join("pedidos t2", "t2.id = t1.pedido_id", "left");
|
|
$builder->join("presupuestos t3", "t1.presupuesto_id = t3.id", "left");
|
|
$builder->join("clientes t4", "t4.id = t3.cliente_id", "left");
|
|
$builder->join("users t5", "t5.id = t4.comercial_id", "left");
|
|
$builder->join("ubicaciones t6", "t6.id = t1.ubicacion_id", "left");
|
|
|
|
if($estado != "") {
|
|
if($estado == "activo") {
|
|
$sql = "t2.estado = 'validacion' OR t2.estado = 'produccion'";
|
|
$builder->where($sql);
|
|
} else {
|
|
$builder->where("t2.estado", $estado);
|
|
}
|
|
}
|
|
|
|
// Falta implementar la busqueda por grupos
|
|
return empty($search)
|
|
? $builder
|
|
: $builder
|
|
->groupStart()
|
|
->like("t1.id", $search)
|
|
->orLike("t1.id", $search)
|
|
->groupEnd();
|
|
}
|
|
|
|
|
|
public function obtenerLineasPedidoSinFacturar($cliente_id) {
|
|
$resultaArray = [];
|
|
|
|
$subquery = $this->db
|
|
->table('facturas_pedidos_lineas')
|
|
->select('pedido_linea_id, SUM(cantidad) AS total_cantidad')
|
|
->groupBy('pedido_linea_id')
|
|
->getCompiledSelect();
|
|
|
|
$builder = $this->db
|
|
->table($this->table . " t1")
|
|
->select("t1.id AS id, t1.pedido_id AS pedido_id, t3.titulo AS titulo, t4.codigo AS tipo_impresion")
|
|
->join("pedidos t2", "t2.id = t1.pedido_id", "left")
|
|
->join("presupuestos t3", "t3.id = t1.presupuesto_id", "left")
|
|
->join("tipos_presupuestos t4", "t4.id = t3.tipo_impresion_id", "left")
|
|
->join("($subquery) fpl", "fpl.pedido_linea_id = t1.id", "left")
|
|
->where("t3.cliente_id", $cliente_id)
|
|
->where("t2.estado", "finalizado")
|
|
->where("(t3.tirada > IFNULL(fpl.total_cantidad, 0))");
|
|
|
|
// Ejecutar la consulta y devolver resultados
|
|
$query = $builder->get();
|
|
$data = $query->getResult();
|
|
|
|
foreach($data as $register) {
|
|
$item = (object)[
|
|
'id' => $register->id,
|
|
'text' => '['. lang('Pedidos.pedido') . ' ' . $register->pedido_id . '] ' . $register->titulo . ' - ' . lang('Presupuestos.' . $register->tipo_impresion),
|
|
];
|
|
array_push($resultaArray, $item);
|
|
}
|
|
|
|
return $resultaArray;
|
|
}
|
|
|
|
} |