Files
safekat/ci4/app/Models/Presupuestos/PresupuestoModel.php
2024-03-08 15:30:20 +01:00

301 lines
10 KiB
PHP
Executable File

<?php
namespace App\Models\Presupuestos;
class PresupuestoModel extends \App\Models\GoBaseModel
{
protected $table = "presupuestos";
/**
* Whether primary key uses auto increment.
*
* @var bool
*/
protected $useAutoIncrement = true;
const SORTABLE = [
0 => "t1.id",
1 => "t1.created_at",
2 => "t2.nombre",
3 => "t3.first_name",
4 => "t1.titulo",
5 => "t5.nombre",
6 => "t1.inc_rei",
7 => "t1.paginas",
8 => "t1.tirada",
9 => "t1.total_presupuesto",
10 => "t6.estado",
];
protected $allowedFields = [
"cliente_id",
"user_created_id",
"user_update_id",
"tipo_impresion_id",
"tipologia_id",
"pais_id",
"estado_id",
"inc_rei",
"causa_cancelacion",
"retractilado",
"retractilado5",
"guardas",
"faja_color",
"recoger_en_taller",
"ferro",
"ferro_digital",
"marcapaginas",
"prototipo",
"papel_formato_id",
"papel_formato_personalizado",
"papel_formato_ancho",
"papel_formato_alto",
"titulo",
"autor",
"coleccion",
"numero_edicion",
"isbn",
"referencia_cliente",
"paginas",
"tirada",
"solapas",
"solapas_ancho",
"solapas_sobrecubierta",
"solapas_ancho_sobrecubierta",
"cosido",
"sobrecubiertas",
"sobrecubiertas_ancho",
"merma",
"merma_cubierta",
"comentarios_cliente",
"comentarios_safekat",
"comentarios_pdf",
"comentarios_tarifa",
"comentarios_produccion",
"lomo",
"total_presupuesto",
"envios_recoge_cliente",
"tirada_alternativa_json_data",
"aprobado_user_id",
"aprobado_at",
"comparador_json_data",
"is_deleted",
"comp_tipo_impresion",
"comp_pos_paginas_color",
"total_coste_papel",
"total_margen_papel",
"total_margenPercent_papel",
"total_coste_impresion",
"total_margen_impresion",
"total_margenPercent_impresion",
"total_coste_servicios",
"total_margen_servicios",
"total_margenPercent_servicios",
"total_coste_envios",
"total_margen_envios",
"total_costes",
"total_margenes",
"total_antes_descuento",
"total_descuento",
"total_descuentoPercent",
"total_presupuesto",
"total_precio_unidad",
"acabado_cubierta_id",
"acabado_sobrecubierta_id",
];
protected $returnType = "App\Entities\Presupuestos\PresupuestoEntity";
protected $useTimestamps = true;
protected $useSoftDeletes = false;
protected $createdField = "created_at";
protected $updatedField = "updated_at";
public static $labelField = "titulo";
protected $validationRulesAdd = [
"autor" => [
"label" => "Presupuestos.autor",
"rules" => "trim|max_length[150]",
],
"titulo" => [
"label" => "Presupuestos.titulo",
"rules" => "trim|required|max_length[30]",
],
"inc_rei" => [
"label" => "Presupuestos.incRei",
"rules" => "integer|permit_empty",
],
"coleccion" => [
"label" => "Presupuestos.incRei",
"rules" => "trim|max_length[255]",
],
"numero_edicion" => [
"label" => "Presupuestos.numeroEdicion",
"rules" => "trim|max_length[50]",
],
"isbn" => [
"label" => "Presupuestos.isbn",
"rules" => "trim|max_length[50]",
],
"pais_id" => [
"label" => "Presupuestos.paisId",
"rules" => "required|integer|greater_than[0]",
],
"cliente_id" => [
"label" => "Presupuestos.clienteId",
"rules" => "required|integer|greater_than[0]",
],
"referencia_cliente" => [
"label" => "Presupuestos.referenciaCliente",
"rules" => "trim|max_length[100]",
],
];
protected $validationMessagesAdd = [
"autor" => [
"max_length" => "Presupuestos.validation.max_length",
"required" => "Presupuestos.validation.requerido",
],
"titulo" => [
"max_length" => "Presupuestos.validation.max_length",
"required" => "Presupuestos.validation.requerido",
],
"inc_rei" => [
"integer" => "Presupuestos.validation.integer",
],
"coleccion" => [
"max_length" => "Presupuestos.validation.max_length",
],
"numero_edicion" => [
"max_length" => "Presupuestos.validation.max_length",
],
"isbn" => [
"max_length" => "Presupuestos.validation.max_length",
],
"pais_id" => [
"required" => "Presupuestos.validation.requerido",
],
"cliente_id" => [
"required" => "Presupuestos.validation.requerido",
],
"referencia_cliente" => [
"max_length" => "Presupuestos.validation.max_length",
],
];
public function findAllWithAllRelations(string $selcols = "*", int $limit = null, int $offset = 0)
{
$sql =
"SELECT t1." .
$selcols .
", t2.nombre AS cliente, t3.nombre AS forma_pago, t4.nombre AS tipo_impresion, t5.nombre AS tipologia, t6.nombre AS pais, t7.estado AS estado, t8.id AS papel_formato, t9.first_name AS total_confirmado_user, t10.first_name AS aprobado_user FROM " .
$this->table .
" t1 LEFT JOIN clientes
t2 ON t1.cliente_id = t2.id LEFT JOIN lg_formas_pago
t3 ON t1.forma_pago_id = t3.id LEFT JOIN lg_tipos_impresion
t4 ON t1.tipo_impresion_id = t4.id LEFT JOIN lg_tipologias_libros
t5 ON t1.tipologia_id = t5.id LEFT JOIN lg_paises
t6 ON t1.pais_id = t6.id = t7.id LEFT JOIN presupuesto_estados
t7 ON t1.estado_id = t8.id LEFT JOIN lg_papel_formato
t8 ON t1.papel_formato_id = t9.id LEFT JOIN lg_papel_generico
t9 ON t1.total_confirmado_user_id = t26.id_user LEFT JOIN auth_user
t10 ON t1.aprobado_user_id = t27.id_user LEFT JOIN auth_user";
if (!is_null($limit) && intval($limit) > 0) {
$sql .= " LIMIT " . intval($limit);
}
if (!is_null($offset) && intval($offset) > 0) {
$sql .= " OFFSET " . intval($offset);
}
$query = $this->db->query($sql);
$result = $query->getResultObject();
return $result;
}
/**
* Get resource data.
*
* @param string $search
*
* @return \CodeIgniter\Database\BaseBuilder
*/
public function getResource($search = [], $tipo_impresion_id = 4)
{
$builder = $this->db
->table($this->table . " t1")
->select(
"t1.id AS id, t1.created_at AS fecha, t2.nombre AS cliente,
CONCAT(t3.first_name, ' ', t3.last_name) AS comercial, t1.titulo AS titulo,
t5.nombre AS pais, t1.inc_rei AS inc_rei, t1.paginas AS paginas, t1.tirada AS tirada,
t1.total_presupuesto AS total_presupuesto, t1.total_presupuesto AS total_presupuesto, t6.estado AS estado"
);
$builder->join("clientes t2", "t1.cliente_id = t2.id", "left");
$builder->join("auth_user t3", "t1.user_update_id = t3.id_user", "left");
$builder->join("lg_paises t5", "t1.pais_id = t5.id", "left");
$builder->join("presupuesto_estados t6", "t1.estado_id = t6.id", "left");
$builder->where("t1.is_deleted", 0);
$builder->where("t1.tipo_impresion_id", $tipo_impresion_id);
if (empty($search))
return $builder;
else {
$builder->groupStart();
foreach ($search as $col_search) {
if ($col_search[0] != 1)
$builder->like(self::SORTABLE[$col_search[0]], $col_search[2]);
else {
$dates = explode(" ", $col_search[2]);
$builder->where(self::SORTABLE[$col_search[0]] . ">=", $dates[0]);
$builder->where(self::SORTABLE[$col_search[0]] . "<=", $dates[1]);
}
}
$builder->groupEnd();
return $builder;
}
}
/**
* Get resource data for creating PDFs.
*
* @param string $search
*
* @return \CodeIgniter\Database\BaseBuilder
*/
public function getResourceForPdf($presupuesto_id = -1)
{
$builder = $this->db
->table($this->table . " t1")
->select(
"t1.id AS id, t1.created_at AS fecha, t1.titulo AS titulo, t1.autor AS autor, t1.ferro AS ferro,
t1.ferro_digital AS ferro_digital, t1.prototipo AS prototipo, t1.solapas AS solapas, t1.lomo AS lomo,
t1.solapas_ancho AS solapas_ancho, t1.paginas AS paginas, t1.tirada AS tirada, t1.coleccion AS coleccion,
t1.retractilado AS retractilado, t1.guardas AS guardas, t1.marcapaginas AS marcapaginas,
t1.comentarios_pdf AS comentarios_pdf, t1.tirada_alternativa_json_data AS tiradas_alternativas,
t1.total_presupuesto AS total_presupuesto, t1.total_precio_unidad AS total_precio_unidad,
t1.papel_formato_personalizado AS isPersonalizado, t1.envios_recoge_cliente AS recoge_cliente,
CONCAT(t1.papel_formato_ancho, 'x', t1.papel_formato_alto) AS formatoPersonalizado,
t2.nombre AS cliente,
CONCAT(t3.first_name, ' ', t3.last_name) AS comercial, t3.email AS email_comercial,
CONCAT(t4.ancho, 'x', t4.alto) AS formato"
);
$builder->join("clientes t2", "t1.cliente_id = t2.id", "left");
$builder->join("auth_user t3", "t1.user_update_id = t3.id_user", "left");
$builder->join("lg_papel_formato t4", "t1.papel_formato_id = t4.id", "left");
$builder->where("t1.is_deleted", 0);
$builder->where("t1.id", $presupuesto_id);
return $builder;
}
}