Files
safekat/ci4/app/Models/Presupuestos/PresupuestoModel.php

258 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 = [
"version",
"cliente_id",
"user_created_id",
"user_update_id",
"tipo_impresion_id",
"tipologia_id",
"pais_id",
"serie_id",
"catalogo_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",
"cosido",
"sobrecubiertas",
"sobrecubiertas_ancho",
"merma",
"merma_cubierta",
"comentarios_cliente",
"comentarios_safekat",
"comentarios_pdf",
"comentarios_tarifa",
"comentarios_produccion",
"paginas_color",
"lomo",
"total_presupuesto",
"envios_recoge_cliente",
"margen",
"margen_extra",
"margen_manual",
"descuento",
"base_imponible",
"forzar_total",
"tirada_alternativa_json_data",
"aprobado_user_id",
"aprobado_at",
"aprobado_json_data",
"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",
];
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, t8.estado AS estado, t9.id AS papel_formato, t10.nombre AS paginas_negro_papel, t11.nombre AS paginas_negro_papel_impresion, t12.nombre AS paginas_negro_maquina, t13.precio AS paginas_negro_tarifa_impresion, t14.nombre AS paginas_color_papel, t15.nombre AS paginas_color_papel_impresion, t16.nombre AS paginas_color_maquina, t17.precio AS paginas_color_tarifa_impresion, t18.nombre AS paginas_cubierta_papel, t19.nombre AS paginas_cubierta_papel_impresion, t20.nombre AS paginas_cubierta_maquina, t21.id AS paginas_cubierta_tarifa_impresion, t22.nombre AS paginas_sobrecubierta_papel, t23.nombre AS paginas_sobrecubierta_papel_impresion, t24.nombre AS paginas_sobrecubierta_maquina, t25.precio AS paginas_sobrecubierta_tarifa_impresion, t26.first_name AS total_confirmado_user, t27.first_name AS aprobado_user, t28.first_name AS pedido_espera_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 t8 ON t1.estado_id = t8.id LEFT JOIN lg_papel_formato t9 ON t1.papel_formato_id = t9.id LEFT JOIN lg_papel_generico t10 ON t1.paginas_negro_papel_id = t10.id LEFT JOIN lg_papel_impresion t11 ON t1.paginas_negro_papel_impresion_id = t11.id LEFT JOIN lg_maquinas t12 ON t1.paginas_negro_maquina_id = t12.id LEFT JOIN lg_maquinas_tarifas_impresion t13 ON t1.paginas_negro_tarifa_impresion_id = t13.id LEFT JOIN lg_papel_generico t14 ON t1.paginas_color_papel_id = t14.id LEFT JOIN lg_papel_impresion t15 ON t1.paginas_color_papel_impresion_id = t15.id LEFT JOIN lg_maquinas t16 ON t1.paginas_color_maquina_id = t16.id LEFT JOIN lg_maquinas_tarifas_impresion t17 ON t1.paginas_color_tarifa_impresion_id = t17.id LEFT JOIN lg_papel_generico t18 ON t1.paginas_cubierta_papel_id = t18.id LEFT JOIN lg_papel_impresion t19 ON t1.paginas_cubierta_papel_impresion_id = t19.id LEFT JOIN lg_maquinas t20 ON t1.paginas_cubierta_maquina_id = t20.id LEFT JOIN lg_maquinas_tarifas_impresion t21 ON t1.paginas_cubierta_tarifa_impresion_id = t21.id LEFT JOIN lg_papel_generico t22 ON t1.paginas_sobrecubierta_papel_id = t22.id LEFT JOIN lg_papel_impresion t23 ON t1.paginas_sobrecubierta_papel_impresion_id = t23.id LEFT JOIN lg_maquinas t24 ON t1.paginas_sobrecubierta_maquina_id = t24.id LEFT JOIN lg_maquinas_tarifas_impresion t25 ON t1.paginas_sobrecubierta_tarifa_impresion_id = t25.id LEFT JOIN auth_user t26 ON t1.total_confirmado_user_id = t26.id_user LEFT JOIN auth_user t27 ON t1.aprobado_user_id = t27.id_user LEFT JOIN auth_user t28 ON t1.pedido_espera_user_id = t28.id_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 = [])
{
$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);
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;
}
}
}