mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
920 lines
38 KiB
PHP
Executable File
920 lines
38 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models\Presupuestos;
|
|
|
|
use App\Models\Configuracion\TipoPresupuestoModel;
|
|
|
|
|
|
class PresupuestoModel extends \App\Models\BaseModel
|
|
{
|
|
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",
|
|
];
|
|
|
|
const SORTABLE_CLIENTE = [
|
|
0 => "t1.id",
|
|
1 => "t1.updated_at",
|
|
2 => "t7.codigo",
|
|
4 => "t1.titulo",
|
|
5 => "t1.paginas",
|
|
6 => "t1.tirada",
|
|
7 => "t1.total_presupuesto",
|
|
8 => "t6.id",
|
|
];
|
|
|
|
protected $allowedFields = [
|
|
"cliente_id",
|
|
"user_created_id",
|
|
"user_update_id",
|
|
"tipo_impresion_id",
|
|
"tipologia_id",
|
|
"catalogo_id",
|
|
"pais_id",
|
|
"estado_id",
|
|
"inc_rei",
|
|
"causa_cancelacion",
|
|
"retractilado",
|
|
"retractilado5",
|
|
"guardas",
|
|
"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",
|
|
"solapas_ancho_faja_color",
|
|
"faja_color",
|
|
"alto_faja_color",
|
|
"cosido",
|
|
"sobrecubiertas",
|
|
"sobrecubiertas_ancho",
|
|
"merma",
|
|
"merma_cubierta",
|
|
"comentarios_cliente",
|
|
"comentarios_safekat",
|
|
"comentarios_pdf",
|
|
"comentarios_tarifa",
|
|
"comentarios_presupuesto",
|
|
"comentarios_produccion",
|
|
"lomo_cubierta",
|
|
"lomo_sobrecubierta",
|
|
"total_presupuesto",
|
|
"envios_recoge_cliente",
|
|
"tirada_alternativa_json_data",
|
|
"aprobado_user_id",
|
|
"aprobado_at",
|
|
"comparador_json_data",
|
|
"is_deleted",
|
|
"comp_tipo_impresion",
|
|
"pos_paginas_color",
|
|
"paginas_color_consecutivas",
|
|
"papel_interior_diferente",
|
|
"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_aceptado_revisado",
|
|
"total_precio_unidad",
|
|
"total_factor",
|
|
"total_factor_ponderado",
|
|
'total_aceptado',
|
|
'iva_reducido',
|
|
'excluir_rotativa',
|
|
"acabado_cubierta_id",
|
|
"barniz_cubierta_id",
|
|
"estampado_cubierta_id",
|
|
"acabado_sobrecubierta_id",
|
|
"acabado_faja_id",
|
|
"is_duplicado",
|
|
'paginas_color_consecutivas',
|
|
'papel_interior_diferente',
|
|
'paginasCuadernillo',
|
|
'lomo_redondo',
|
|
'cabezada',
|
|
'envio_base',
|
|
'direcciones_fp_checks',
|
|
];
|
|
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 = [
|
|
"tirada" => [
|
|
"label" => "Presupuestos.tirada",
|
|
"rules" => "required|integer|greater_than[0]",
|
|
],
|
|
"paginas" => [
|
|
"label" => "Presupuestos.paginas",
|
|
"rules" => "required|integer|greater_than[0]",
|
|
],
|
|
"autor" => [
|
|
"label" => "Presupuestos.autor",
|
|
"rules" => "trim|max_length[150]",
|
|
],
|
|
"titulo" => [
|
|
"label" => "Presupuestos.titulo",
|
|
"rules" => "trim|required|max_length[300]",
|
|
],
|
|
"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" => "integer",
|
|
],
|
|
"cliente_id" => [
|
|
"label" => "Presupuestos.clienteId",
|
|
"rules" => "required|integer|greater_than[0]",
|
|
],
|
|
"referencia_cliente" => [
|
|
"label" => "Presupuestos.referenciaCliente",
|
|
"rules" => "trim|max_length[100]",
|
|
],
|
|
];
|
|
|
|
protected $validationMessagesAdd = [
|
|
"tirada" => [
|
|
"greater_than" => "Presupuestos.validation.greater_than_0",
|
|
],
|
|
"paginas" => [
|
|
"greater_than" => "Presupuestos.validation.greater_than_0",
|
|
],
|
|
"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 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 LEFT JOIN users
|
|
t10 ON t1.aprobado_user_id = t27.id LEFT JOIN users";
|
|
|
|
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("users t3", "t2.comercial_id = t3.id", "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;
|
|
}
|
|
}
|
|
|
|
public function removeIsDuplicado($presupuesto_id)
|
|
{
|
|
$this->db
|
|
->table($this->table . " t1")
|
|
->where('id', $presupuesto_id)
|
|
->set('is_duplicado', false)
|
|
->update();
|
|
}
|
|
|
|
/**
|
|
* 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.solapas_sobrecubierta AS solapas_sobrecubierta,
|
|
t1.lomo_cubierta AS lomo_cubierta, t1.lomo_sobrecubierta AS lomo_sobrecubierta,
|
|
t1.solapas_ancho AS solapas_ancho, t1.solapas_ancho_sobrecubierta AS solapas_sobrecubierta_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,
|
|
t1.solapas_ancho_faja_color AS ancho_solapas_faja,
|
|
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, t5.secret AS email_comercial,
|
|
CONCAT(t4.ancho, 'x', t4.alto) AS formato, t6.encuadernacion AS tipo_encuadernado,
|
|
t1.comp_tipo_impresion AS tipo_impresion"
|
|
);
|
|
$builder->join("clientes t2", "t1.cliente_id = t2.id", "left");
|
|
$builder->join("users t3", "t1.user_update_id = t3.id", "left");
|
|
$builder->join("lg_papel_formato t4", "t1.papel_formato_id = t4.id", "left");
|
|
$builder->join("auth_identities t5", "t1.user_update_id = t5.id", "left");
|
|
$builder->join("tipos_presupuestos t6", "t1.tipo_impresion_id = t6.id", "left");
|
|
|
|
$builder->where("t1.is_deleted", 0);
|
|
$builder->where("t1.id", $presupuesto_id);
|
|
|
|
return $builder;
|
|
}
|
|
|
|
function getListaPresupuestosCliente($search = [], $clienteId)
|
|
{
|
|
|
|
$builder = $this->db
|
|
->table($this->table . " t1")
|
|
->select(
|
|
"t1.id AS id, t1.created_at AS fecha, t7.codigo as codigo, 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("users t3", "t1.user_update_id = t3.id", "left");
|
|
$builder->join("lg_paises t5", "t1.pais_id = t5.id", "left");
|
|
$builder->join("presupuesto_estados t6", "t1.estado_id = t6.id", "left");
|
|
$builder->join("tipos_presupuestos t7", "t1.tipo_impresion_id = t7.id", "left");
|
|
|
|
if ($clienteId != 0)
|
|
$builder->where("t1.cliente_id", $clienteId);
|
|
|
|
$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_CLIENTE[$col_search[0]], $col_search[2]);
|
|
else {
|
|
$dates = explode(" ", $col_search[2]);
|
|
$builder->where(self::SORTABLE_CLIENTE[$col_search[0]] . ">=", $dates[0]);
|
|
$builder->where(self::SORTABLE_CLIENTE[$col_search[0]] . "<=", $dates[1]);
|
|
}
|
|
}
|
|
$builder->groupEnd();
|
|
return $builder;
|
|
}
|
|
}
|
|
|
|
function confirmarPresupuesto($presupuesto_id)
|
|
{
|
|
$this->db
|
|
->table($this->table . " t1")
|
|
->where('t1.id', $presupuesto_id)
|
|
->set('t1.estado_id', 2)
|
|
->update();
|
|
}
|
|
|
|
function insertarPresupuestoCliente($id, $tirada, $data, $data_cabecera, $extra_info, $resumen_totales, $iva_reducido, $excluir_rotativa, $tiradas_alternativas)
|
|
{
|
|
|
|
helper('date');
|
|
|
|
$model = model('App\Models\Configuracion\PapelFormatoModel');
|
|
$papel_formato_id = $model->where('ancho', $data['tamanio']['ancho'])->where('alto', $data['tamanio']['alto'])->first();
|
|
|
|
$is_cosido = (new TipoPresupuestoModel())->get_isCosido($data['tipo_impresion_id']);
|
|
|
|
$totalCostes = $resumen_totales['totalPapel'] + $resumen_totales['totalImpresion'] +
|
|
$resumen_totales['totalServicios']; //+ $resumen_totales['envio_base_coste'];
|
|
$totalMargenes = $resumen_totales['margenPapel'] + $resumen_totales['margenImpresion'] +
|
|
$resumen_totales['margenServicios']; //+ $resumen_totales['envio_base_margen'];
|
|
|
|
$fields = [
|
|
'cliente_id' => $data['clienteId'],
|
|
'tipo_impresion_id' => $data['tipo_impresion_id'],
|
|
'pais_id' => 1,
|
|
'retractilado' => $data['retractilado'] ? 1 : 0,
|
|
'retractilado5' => $data['retractilado5'] ? 1 : 0,
|
|
'ferro' => $data['ferro'] ? 1 : 0,
|
|
'ferro_digital' => $data['ferro_digital'] ? 1 : 0,
|
|
'marcapaginas' => $data['marcapaginas'] ? 1 : 0,
|
|
'prototipo' => $data['prototipo'] ? 1 : 0,
|
|
'envio_base' => $data['envio_base'],
|
|
'papel_formato_id' => is_null($papel_formato_id) ? 0 : $papel_formato_id->id,
|
|
'papel_formato_personalizado' => !$papel_formato_id ? 1 : 0,
|
|
'papel_formato_ancho' => !$papel_formato_id ? $data['tamanio']['ancho'] : null,
|
|
'papel_formato_alto' => !$papel_formato_id ? $data['tamanio']['alto'] : null,
|
|
'titulo' => $data_cabecera['titulo'],
|
|
'autor' => $data_cabecera['autor'],
|
|
'ISBN' => $data_cabecera['isbn'],
|
|
'coleccion' => $data_cabecera['coleccion'],
|
|
'referencia_cliente' => $data_cabecera['referenciaCliente'],
|
|
'paginas' => $data['interior']['paginas'],
|
|
'tirada' => $tirada,
|
|
'solapas' => $data['cubierta']['solapasCubierta'] == 0 ? 0 : 1,
|
|
'lomo_redondo' => $data['cubierta']['lomoRedondo'] == 0 ? 0 : 1,
|
|
'cabezada' => $data['cubierta']['cabezada'] == 0 ? 0 : 1,
|
|
'solapas_ancho' => $data['cubierta']['solapasCubierta'] > 0 ? $data['cubierta']['solapasCubierta'] : 0,
|
|
'solapas_sobrecubierta' => !$data['sobrecubierta'] ? 0 : 1,
|
|
'solapas_ancho_sobrecubierta' => !$data['sobrecubierta'] ? 0 : $data['sobrecubierta']['solapas'],
|
|
'cosido' => $is_cosido,
|
|
'merma' => $extra_info['merma'],
|
|
'merma_cubierta' => $extra_info['merma'],
|
|
'paginasCuadernillo' => $data['paginasCuadernillo'],
|
|
|
|
'recoger_en_taller' => $data['entrega_taller'],
|
|
|
|
'comp_pos_paginas_color' => $data['interior']['pos_paginas_color'],
|
|
'paginas_color_consecutivas' => $data['interior']['paginas_color_consecutivas'],
|
|
'papel_interior_diferente' => $data['interior']['papelInteriorDiferente'],
|
|
|
|
'lomo_cubierta' => $extra_info['lomo_cubierta'],
|
|
'lomo_sobrecubierta' => $extra_info['lomo_sobrecubierta'],
|
|
|
|
'comparador_json_data' => $this->generateJson($data),
|
|
|
|
'faja_color' => is_array($data['faja']) && count($data['faja']) > 0 ? 1 : 0,
|
|
'solapas_ancho_faja_color' => is_array($data['faja']) && $data['faja'] !== [] ? $data['faja']['solapas'] : 60,
|
|
'alto_faja_color' => is_array($data['faja']) && $data['faja'] !== [] ? $data['faja']['alto'] : 50,
|
|
|
|
'acabado_cubierta_id' => $data['cubierta']['acabado'],
|
|
'acabado_sobrecubierta_id' => !$data['sobrecubierta'] ? 0 : $data['sobrecubierta']['acabado'],
|
|
'acabado_faja_id' => is_array($data['faja']) && $data['faja'] !== [] ? $data['faja']['acabado'] : 0,
|
|
|
|
'comp_tipo_impresion' => $data['isHq'] ? ($data['isColor'] ? 'colorhq' : 'negrohq') : ($data['isColor'] ? 'color' : 'negro'),
|
|
|
|
'user_created_id' => $extra_info['user_id'],
|
|
'created_at' => date('Y-m-d H:i:s', now()),
|
|
'updated_at' => date('Y-m-d H:i:s', now()),
|
|
|
|
'tirada_alternativa_json_data' => json_encode($tiradas_alternativas),
|
|
|
|
'total_coste_papel' => round($resumen_totales['totalPapel'], 2),
|
|
'total_margen_papel' => round($resumen_totales['margenPapel'], 2),
|
|
'total_margenPercent_papel' => round($resumen_totales['porcentajeMargenPapel'], 0),
|
|
'total_coste_impresion' => round($resumen_totales['totalImpresion'], 2),
|
|
'total_margen_impresion' => round($resumen_totales['margenImpresion'], 2),
|
|
'total_margenPercent_impresion' => round($resumen_totales['porcentajeMargenImpresion'], 0),
|
|
'total_coste_servicios' => round($resumen_totales['totalServicios'], 2),
|
|
'total_margen_servicios' => round($resumen_totales['margenServicios'], 2),
|
|
'total_margenPercent_servicios' => round($resumen_totales['porcentajeMargenServicios'], 2),
|
|
'total_coste_envios' => round($resumen_totales['coste_envio'], 2),
|
|
'total_margen_envios' => round($resumen_totales['margen_envio'], 2),
|
|
'total_costes' => round($totalCostes, 2),
|
|
'total_margenes' => round($totalMargenes, 2),
|
|
|
|
'total_antes_descuento' => round(
|
|
$totalCostes + $totalMargenes +
|
|
$resumen_totales['coste_envio'] + $resumen_totales['margen_envio'] +
|
|
$data['envio_base'],
|
|
2
|
|
),
|
|
'total_descuento' => 0,
|
|
'total_descuentoPercent' => 0,
|
|
|
|
'total_precio_unidad' => $resumen_totales['precio_unidad'],
|
|
'total_presupuesto' => round(
|
|
$totalCostes + $totalMargenes +
|
|
$resumen_totales['coste_envio'] + $resumen_totales['margen_envio'] +
|
|
$data['envio_base'],
|
|
2
|
|
),
|
|
'total_aceptado' => round(
|
|
$totalCostes + $totalMargenes +
|
|
$resumen_totales['coste_envio'] + $resumen_totales['margen_envio'] +
|
|
$data['envio_base'],
|
|
2
|
|
),
|
|
|
|
'total_factor' => round(
|
|
($totalCostes + $totalMargenes) /
|
|
$resumen_totales['sumForFactor'],
|
|
2
|
|
),
|
|
'total_factor_ponderado' => round(
|
|
($totalCostes + $totalMargenes) /
|
|
$resumen_totales['sumForFactorPonderado'],
|
|
2
|
|
),
|
|
|
|
'iva_reducido' => $iva_reducido,
|
|
'excluir_rotativa' => $excluir_rotativa,
|
|
|
|
'direcciones_fp_checks' => $data['direcciones_fp_checks'] ? json_encode($data['direcciones_fp_checks']) : null,
|
|
|
|
];
|
|
/* Actualizacion */
|
|
if ($id != 0) {
|
|
$fields['id'] = $id;
|
|
$fields['updated_at'] = date('Y-m-d H:i:s', now());
|
|
$fields['user_update_id'] = auth()->id();
|
|
$this->db->table($this->table)->where('id', $id)->update($fields);
|
|
return $id;
|
|
}
|
|
/* Inserccion */ else {
|
|
$fields['user_created_id'] = auth()->id();
|
|
$fields['user_update_id'] = auth()->id();
|
|
$this->db->table($this->table)->insert($fields);
|
|
return $this->db->insertID();
|
|
}
|
|
}
|
|
|
|
private function generateJson($data)
|
|
{
|
|
$values = array();
|
|
if (is_array($data)) {
|
|
// -- INTERIOR --
|
|
// Si hay negro
|
|
if (intval($data['interior']['paginas']) > intval($data['interior']['paginas_color'])) {
|
|
|
|
if ($data['isHq'])
|
|
$key = 'bnhq';
|
|
else
|
|
$key = 'bn';
|
|
if (array_key_exists('id', $data['interior']['papel_generico'])) {
|
|
$papel_id = intval($data['interior']['papel_generico']['id']);
|
|
$gramaje = intval($data['interior']['gramaje']);
|
|
} else {
|
|
$papel_id = intval($data['interior']['papel_generico']['negro']['id']);
|
|
$gramaje = intval($data['interior']['gramaje']['negro']);
|
|
}
|
|
|
|
|
|
$values[$key] = array(
|
|
'paginas' => intval($data['interior']['paginas']) - intval(intval($data['interior']['paginas_color'])),
|
|
'papel_id' => $papel_id,
|
|
'gramaje' => $gramaje,
|
|
);
|
|
}
|
|
// Si hay color
|
|
if (intval($data['interior']['paginas_color']) > 0) {
|
|
|
|
if ($data['isHq'])
|
|
$key = 'colorhq';
|
|
else
|
|
$key = 'color';
|
|
if (array_key_exists('id', $data['interior']['papel_generico'])) {
|
|
$papel_id = intval($data['interior']['papel_generico']['id']);
|
|
$gramaje = intval($data['interior']['gramaje']);
|
|
} else {
|
|
$papel_id = intval($data['interior']['papel_generico']['color']['id']);
|
|
$gramaje = intval($data['interior']['gramaje']['color']);
|
|
}
|
|
$values[$key] = array(
|
|
'paginas' => intval(intval($data['interior']['paginas_color'])),
|
|
'papel_id' => $papel_id,
|
|
'gramaje' => $gramaje,
|
|
);
|
|
}
|
|
|
|
// -- CUBIERTA --
|
|
$values['cubierta'] = array(
|
|
'papel_id' => intval($data['cubierta']['papel_generico_cubierta']['id']),
|
|
'gramaje' => intval($data['cubierta']['gramajeCubierta']),
|
|
'paginas' => intval($data['cubierta']['carasCubierta']),
|
|
);
|
|
|
|
// -- SOBRECUBIERTA --
|
|
if ($data['sobrecubierta']) {
|
|
$values['sobrecubierta'] = array(
|
|
'papel_id' => intval($data['sobrecubierta']['papel']['id']),
|
|
'gramaje' => intval($data['sobrecubierta']['gramaje']),
|
|
'imprimir' => 1,
|
|
);
|
|
}
|
|
|
|
// -- GUARDAS --
|
|
if ($data['datos_guardas'] != 0) {
|
|
$values['guardas'] = array(
|
|
'papel_id' => intval($data['datos_guardas']['papel']['id']),
|
|
'gramaje' => intval($data['datos_guardas']['gramaje']),
|
|
'paginas' => intval($data['datos_guardas']['caras']),
|
|
);
|
|
}
|
|
|
|
// -- Faja --
|
|
if ($data['faja'] != 0 && $data['faja'] !== []) {
|
|
$values['faja'] = array(
|
|
'papel_id' => intval($data['faja']['papel']),
|
|
'gramaje' => intval($data['faja']['gramaje']),
|
|
);
|
|
}
|
|
|
|
}
|
|
$json = json_encode($values);
|
|
return $json;
|
|
}
|
|
|
|
public function generarLineaPedido($presupuesto_id, $forFactura = false, $pedido_id = 0)
|
|
{
|
|
$builder = $this->db
|
|
->table($this->table . " t1")
|
|
->select(
|
|
"t1.id AS numero, t1.tipo_impresion_id as tipo, t1.tirada AS unidades, t1.total_aceptado as total, t1.paginas AS paginas,
|
|
t1.titulo AS titulo, t1.autor AS autor, t1.isbn AS isbn,
|
|
t1.papel_formato_id AS papel_formato_id, t1.papel_formato_personalizado AS papel_formato_personalizado,
|
|
t1.papel_formato_ancho AS papel_formato_ancho, t1.papel_formato_alto AS papel_formato_alto,
|
|
CONCAT(CAST(t2.ancho AS INT), 'x', CAST(t2.alto AS INT)) AS tamanio,
|
|
t3.codigo AS codigo_encuadernacion,
|
|
t1.solapas AS solapas_cubierta, CAST(t1.solapas_ancho AS INT) AS solapas_ancho_cubierta,
|
|
t1.solapas_sobrecubierta AS solapas_sobrecubierta, CAST(t1.solapas_ancho_sobrecubierta AS INT) AS solapas_ancho_sobrecubierta,"
|
|
);
|
|
$builder->join("lg_papel_formato t2", "t1.papel_formato_id = t2.id", "left");
|
|
$builder->join("tipos_presupuestos t3", "t1.tipo_impresion_id = t3.id", "left");
|
|
$builder->where("t1.is_deleted", 0);
|
|
$builder->where("t1.id", $presupuesto_id);
|
|
$presupuesto = $builder->get()->getResultObject();
|
|
if (count($presupuesto) > 0) {
|
|
|
|
$modelLinea = model('App\Models\Presupuestos\PresupuestoLineaModel');
|
|
$lineas = $modelLinea->where('presupuesto_id', $presupuesto_id)->findAll();
|
|
|
|
$presupuesto = $presupuesto[0];
|
|
|
|
// Libro
|
|
if ($presupuesto->tipo < 10 || $presupuesto->tipo == 20 || $presupuesto->tipo == 21) {
|
|
if ($presupuesto->papel_formato_personalizado == 1) {
|
|
$presupuesto->tamanio = $presupuesto->papel_formato_ancho . "x" . $presupuesto->papel_formato_alto;
|
|
}
|
|
|
|
if ($forFactura) {
|
|
$presupuesto->concepto = sprintf(lang('Pedidos.lineasTemplates.pedido'), $pedido_id);
|
|
} else {
|
|
$presupuesto->concepto = sprintf(lang('Pedidos.lineasTemplates.presupuesto'), $presupuesto->numero);
|
|
}
|
|
$presupuesto->concepto .= sprintf(
|
|
lang('Pedidos.lineasTemplates.libro'),
|
|
$presupuesto->unidades,
|
|
$presupuesto->paginas,
|
|
$presupuesto->titulo,
|
|
$presupuesto->autor,
|
|
$presupuesto->isbn,
|
|
$presupuesto->tamanio
|
|
);
|
|
$presupuesto->concepto .= $this->generarConceptoLineasPresupuestoLibro($lineas, $presupuesto);
|
|
|
|
$presupuesto = (object) [
|
|
'numero' => $presupuesto->numero,
|
|
'unidades' => $presupuesto->unidades,
|
|
'total' => $presupuesto->total,
|
|
'concepto' => $presupuesto->concepto,
|
|
];
|
|
}
|
|
return [$presupuesto];
|
|
}
|
|
}
|
|
public function getServiciosPresupuesto($presupuesto_id)
|
|
{
|
|
$queryAcabado = $this->db->table($this->table)
|
|
->select(
|
|
[
|
|
'lg_tarifa_acabado.id',
|
|
'lg_tarifa_acabado.nombre',
|
|
|
|
|
|
]
|
|
)
|
|
->join('presupuesto_acabados', 'presupuesto_acabados.presupuesto_id = presupuestos.id', 'left')
|
|
->join('lg_tarifa_acabado', 'lg_tarifa_acabado.id = presupuesto_acabados.tarifa_acabado_id', 'left')
|
|
->where('presupuestos.id', $presupuesto_id);
|
|
|
|
$queryPreimpresion = $this->db->table($this->table)
|
|
->select(
|
|
[
|
|
'lg_tarifa_preimpresion.id',
|
|
'lg_tarifa_preimpresion.nombre',
|
|
'lg_tarifa_preimpresion.precio',
|
|
|
|
]
|
|
)
|
|
->join('presupuesto_preimpresiones', 'presupuesto_preimpresiones.presupuesto_id = presupuestos.id', 'left')
|
|
->join('lg_tarifa_preimpresion', 'lg_tarifa_preimpresion.id = presupuesto_preimpresiones.tarifa_preimpresion_id', 'left')
|
|
->where('presupuestos.id', $presupuesto_id);
|
|
|
|
$queryManipulado = $this->db->table($this->table)
|
|
->select(
|
|
[
|
|
'lg_tarifa_manipulado.id',
|
|
'lg_tarifa_manipulado.nombre',
|
|
|
|
]
|
|
)
|
|
->join('presupuesto_manipulados', 'presupuesto_manipulados.presupuesto_id = presupuestos.id', 'left')
|
|
->join('lg_tarifa_manipulado', 'lg_tarifa_manipulado.id = presupuesto_manipulados.tarifa_manipulado_id', 'left')
|
|
->where('presupuestos.id', $presupuesto_id);
|
|
|
|
$queryExtras = $this->db->table($this->table)
|
|
->select(
|
|
[
|
|
'lg_tarifa_preimpresion.id',
|
|
'lg_tarifa_preimpresion.nombre',
|
|
|
|
]
|
|
)
|
|
->join('presupuesto_serviciosExtra', 'presupuesto_serviciosExtra.presupuesto_id = presupuestos.id', 'left')
|
|
->join('lg_tarifa_preimpresion', 'lg_tarifa_preimpresion.id = presupuesto_serviciosExtra.tarifa_extra_id', 'left')
|
|
->where('presupuestos.id', $presupuesto_id);
|
|
|
|
$servicios['acabado'] = $queryAcabado->get()->getResultObject();
|
|
$servicios['manipulado'] = $queryManipulado->get()->getResultObject();
|
|
$servicios['preimpresion'] = $queryPreimpresion->get()->getResultObject();
|
|
$servicios['extra'] = $queryExtras->get()->getResultObject();
|
|
return $servicios;
|
|
}
|
|
|
|
public function getPresupuestosClienteForm($cliente_id = -1)
|
|
{
|
|
$builder = $this->db
|
|
->table($this->table . " pr")
|
|
->select('pr.id, pr.created_at as fecha, CONCAT(u.first_name, " ", u.last_name) AS comercial, pr.titulo,
|
|
pr.paginas as paginas, pr.tirada, pr.total_aceptado as total, pr.estado_id as estado')
|
|
->join("clientes c", "pr.cliente_id = c.id", "left")
|
|
->join("users u", "c.comercial_id= u.id", "left")
|
|
->where('pr.cliente_id', $cliente_id)
|
|
->groupBy('pr.id');
|
|
return $builder;
|
|
}
|
|
|
|
private function generarConceptoLineasPresupuestoLibro($lineas, $presupuesto)
|
|
{
|
|
|
|
$model_papel = model('App\Models\Configuracion\PapelImpresionModel');
|
|
$description_interior = "";
|
|
$description_cubierta = "";
|
|
$description_sobrecubierta = "";
|
|
$paginas_negro = 0;
|
|
$paginas_color = 0;
|
|
$papel_negro = "";
|
|
$papel_color = "";
|
|
$gramaje_negro = 0;
|
|
$gramaje_color = 0;
|
|
|
|
$lp_bn_lines = array_filter($lineas, function ($linea) {
|
|
return strpos($linea->tipo, 'lp_bn') === 0;
|
|
});
|
|
$lp_color_lines = array_filter($lineas, function ($linea) {
|
|
return strpos($linea->tipo, 'lp_color') === 0;
|
|
});
|
|
|
|
$lp_rot_bn = array_filter($lineas, function ($linea) {
|
|
return strpos($linea->tipo, 'lp_rot_bn') === 0;
|
|
});
|
|
|
|
$lp_rot_color = array_filter($lineas, function ($linea) {
|
|
return strpos($linea->tipo, 'lp_rot_color') === 0;
|
|
});
|
|
|
|
if (count($lp_bn_lines) > 0) {
|
|
$lp_bn_lines = array_values($lp_bn_lines)[0];
|
|
$paginas_negro = $lp_bn_lines->paginas;
|
|
$gramaje_negro = $lp_bn_lines->gramaje;
|
|
//$papel_negro = $model_papel->where('id', $lp_bn_lines->papel_impresion_id)->first()->nombre;
|
|
$papel_negro = $model_papel->getPapelGenericoNombre($lp_bn_lines->papel_impresion_id);
|
|
$description_interior .= sprintf(
|
|
lang('Pedidos.lineasTemplates.libro_linea_interior'),
|
|
strval($paginas_negro),
|
|
$papel_negro,
|
|
strval($gramaje_negro)
|
|
) . ". ";
|
|
}
|
|
if (count($lp_color_lines) > 0) {
|
|
$lp_color_lines = array_values($lp_color_lines)[0];
|
|
$paginas_color = $lp_color_lines->paginas;
|
|
$gramaje_color = $lp_color_lines->gramaje;
|
|
//$papel_color = $model_papel->where('id', $lp_color_lines->papel_impresion_id)->first()->nombre;
|
|
$papel_color = $model_papel->getPapelGenericoNombre($lp_color_lines->papel_impresion_id);
|
|
$description_interior .= sprintf(
|
|
lang('Pedidos.lineasTemplates.libro_linea_interior'),
|
|
strval($paginas_color),
|
|
$papel_color,
|
|
strval($gramaje_color)
|
|
) . ". ";
|
|
}
|
|
|
|
if (count($lp_rot_bn) > 0) {
|
|
$lp_rot_bn = array_values($lp_rot_bn)[0];
|
|
$paginas_negro = $lp_rot_bn->paginas;
|
|
$gramaje_negro = $lp_rot_bn->gramaje;
|
|
$papel_negro = $model_papel->getPapelGenericoNombre($lp_rot_bn->papel_impresion_id);
|
|
//$papel_negro = $model_papel->where('id', $lp_rot_bn->papel_impresion_id)->first()->nombre;
|
|
$description_interior .= sprintf(
|
|
lang('Pedidos.lineasTemplates.libro_linea_interior'),
|
|
strval($paginas_negro),
|
|
$papel_negro,
|
|
strval($gramaje_negro)
|
|
) . ". ";
|
|
}
|
|
|
|
if (count($lp_rot_color) > 0) {
|
|
$lp_rot_color = array_values($lp_rot_color)[0];
|
|
$paginas_negro = intval($lp_rot_color->paginas) - intval($lp_rot_color->rotativa_pag_color);
|
|
$gramaje = $lp_rot_color->gramaje;
|
|
//$papel = $model_papel->where('id', $lp_rot_color->papel_impresion_id)->first()->nombre;
|
|
$papel = $model_papel->getPapelGenericoNombre($lp_rot_color->papel_impresion_id);
|
|
if ($paginas_negro > 0) {
|
|
$description_interior .= sprintf(
|
|
lang('Pedidos.lineasTemplates.libro_linea_interior'),
|
|
strval($paginas_negro),
|
|
$papel,
|
|
strval($gramaje)
|
|
) . ". ";
|
|
}
|
|
|
|
$description_interior .= sprintf(
|
|
lang('Pedidos.lineasTemplates.libro_linea_interior'),
|
|
strval($lp_rot_color->rotativa_pag_color),
|
|
$papel,
|
|
strval($gramaje)
|
|
) . ". ";
|
|
}
|
|
|
|
$lp_cubierta = array_filter($lineas, function ($linea) {
|
|
return strpos($linea->tipo, 'lp_cubierta') === 0;
|
|
});
|
|
$lp_sobrecubierta = array_filter($lineas, function ($linea) {
|
|
return strpos($linea->tipo, 'lp_sobrecubierta') === 0;
|
|
});
|
|
|
|
if (count($lp_cubierta) > 0) {
|
|
$lp_cubierta = array_values($lp_cubierta)[0];
|
|
if ($lp_cubierta->paginas == 2) {
|
|
$lp_cubierta->caras = lang('Pedidos.unaCara');
|
|
} else {
|
|
$lp_cubierta->caras = lang('Pedidos.dosCaras');
|
|
}
|
|
$description_cubierta = sprintf(
|
|
lang('Pedidos.lineasTemplates.libro_linea_cubierta'),
|
|
$lp_cubierta->caras,
|
|
//$model_papel->where('id', $lp_cubierta->papel_impresion_id)->first()->nombre,
|
|
$model_papel->getPapelGenericoNombre($lp_cubierta->papel_impresion_id),
|
|
strval($lp_cubierta->gramaje)
|
|
);
|
|
$description_cubierta .= ($presupuesto->solapas_cubierta == 1 ? sprintf(lang('Pedidos.lineasTemplates.libro_solapas'), $presupuesto->solapas_ancho_cubierta) : ". ");
|
|
}
|
|
if (count($lp_sobrecubierta) > 0) {
|
|
$lp_sobrecubierta = array_values($lp_sobrecubierta)[0];
|
|
$description_sobrecubierta = sprintf(
|
|
lang('Pedidos.lineasTemplates.libro_linea_sobrecubierta'),
|
|
//$model_papel->where('id', $lp_sobrecubierta->papel_impresion_id)->first()->nombre,
|
|
$model_papel->getPapelGenericoNombre($lp_sobrecubierta->papel_impresion_id),
|
|
strval($lp_sobrecubierta->gramaje)
|
|
);
|
|
$description_sobrecubierta .= ($presupuesto->solapas_sobrecubierta == 1 ? sprintf(lang('Pedidos.lineasTemplates.libro_solapas'), $presupuesto->solapas_ancho_sobrecubierta) : ". ");
|
|
}
|
|
|
|
$acabado = sprintf(
|
|
lang('Pedidos.lineasTemplates.libro_encuadernacion'),
|
|
lang('Presupuestos.' . $presupuesto->codigo_encuadernacion)
|
|
);
|
|
|
|
return $description_interior . $description_cubierta . $description_sobrecubierta . $acabado;
|
|
}
|
|
|
|
public function vincularCatalogo(int $presupuesto_id, int $catalogo_id): bool
|
|
{
|
|
return $this->update($presupuesto_id, [
|
|
'catalogo_id' => $catalogo_id,
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
'user_update_id' => auth()->id(),
|
|
]);
|
|
}
|
|
|
|
|
|
}
|