Merge branch 'main' into 'dev/pdf_albaranes'

Main

See merge request jjimenez/safekat!274
This commit is contained in:
Ignacio Martinez Navajas
2024-06-16 06:30:06 +00:00
28 changed files with 2921 additions and 76 deletions

View File

@ -308,4 +308,11 @@ class ClienteModel extends \App\Models\BaseModel
->orLike("t7.nombre", $search)
->groupEnd();
}
/*
TO-DO: Implementar la lógica de negocio para el crédito disponible
*/
public function creditoDisponible($cliente_id){
return true;
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace App\Models\Pedidos;
class AlbaranLineaModel extends \App\Models\BaseModel
{
protected $table = "albaranes_lineas";
/**
* Whether primary key uses auto increment.
*
* @var bool
*/
protected $useAutoIncrement = true;
protected $primaryKey = 'id';
protected $returnType = 'App\Entities\Pedidos\AlbaranLineaEntity';
protected $allowedFields = [
'albaran_id',
'titulo',
'isbn',
'ref_cliente',
'cantidad',
'cajas',
'ejemplares_por_caja',
'precio_unidad',
'total',
'user_created_id',
'user_updated_id',
'created_at',
'updated_at',
'deleted_at',
];
protected $useSoftDeletes = true;
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
}

View File

@ -0,0 +1,113 @@
<?php
namespace App\Models\Pedidos;
class AlbaranModel extends \App\Models\BaseModel
{
protected $table = "albaranes";
/**
* Whether primary key uses auto increment.
*
* @var bool
*/
protected $useAutoIncrement = true;
protected $primaryKey = 'id';
protected $returnType = 'App\Entities\Pedidos\AlbaranEntity';
protected $allowedFields = [
'pedido_id',
'presupuesto_id',
'presupuesto_direccion_id',
'cliente_id',
'serie_id',
'numero_albaran',
'mostrar_precios',
'total',
'direccion_albaran',
'att_albaran',
'user_created_id',
'user_updated_id',
'created_at',
'updated_at',
'deleted_at',
];
protected $useSoftDeletes = true;
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
public function generarAlbaranes($pedido_id, $presupuestos_id, $user_id){
$model_presupuesto = model('App\Models\Presupuestos\PresupuestoModel');
$model_presupuesto_direcciones = model('App\Models\Presupuestos\PresupuestoDireccionesModel');
$model_series = model('App\Models\Configuracion\SeriesFacturasModel');
$presupuestos = $model_presupuesto->find($presupuestos_id);
$return_data = [];
foreach ($presupuestos as $presupuesto) {
$envios = $model_presupuesto_direcciones->where('presupuesto_id', $presupuesto->id)->findAll();
foreach($envios as $envio){
// calculo precio_unidad
$precio_unidad = $presupuesto->total_aceptado/$presupuesto->tirada;
$albaran_linea = [];
$albaran_linea = [
'titulo' => $presupuesto->titulo,
'isbn' => $presupuesto->isbn,
'ref_cliente' => $presupuesto->ref_cliente,
'cantidad' => $envio->cantidad,
'cajas' => 1,
'ejemplares_por_caja' => $envio->cantidad,
'precio_unidad' => $precio_unidad,
'total' => $precio_unidad * $envio->cantidad,
'user_created_id' => $user_id,
'user_updated_id' => $user_id,
];
$serie = $model_series->find(11);
$numero_albaran = str_replace('{number}', $serie->next, $serie->formato);
$numero_albaran = str_replace( '{year}', date("Y"), $numero_albaran);
$serie->next = $serie->next + 1;
$model_series->save($serie);
$albaran = [
'pedido_id' => $pedido_id,
'presupuesto_id' => $presupuesto->id,
'presupuesto_direccion_id' => $envio->id,
'cliente_id' => $presupuesto->cliente_id,
'serie_id' => 11, // Serie de albaranes
'numero_albaran' => $numero_albaran,
'mostrar_precios' => 0,
'total' => $albaran_linea['total'],
'direccion_albaran' => $envio->direccion,
'att_albaran' => $envio->att,
'user_created_id' => $user_id,
'user_updated_id' => $user_id,
'fecha_albaran' => date('d/m/Y'),
];
$id_albaran = $this->insert($albaran);
$model_albaran_linea = model('App\Models\Pedidos\AlbaranLineaModel');
$albaran['id'] = $id_albaran;
$albaran_linea['albaran_id'] = $id_albaran;
$id_albaran_linea =$model_albaran_linea->insert($albaran_linea);
$albaran_linea['id'] = $id_albaran_linea;
array_push($return_data, ["albaran"=>$albaran, "lineas" =>[$albaran_linea]]);
}
}
return $return_data;
}
}

View File

@ -0,0 +1,79 @@
<?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();
}
}

View File

@ -0,0 +1,88 @@
<?php
namespace App\Models\Pedidos;
class PedidoModel extends \App\Models\BaseModel
{
protected $table = "pedidos";
/**
* Whether primary key uses auto increment.
*
* @var bool
*/
protected $useAutoIncrement = true;
const SORTABLE_TODOS = [
0 => "t1.id",
1 => "t1.updated_at",
2 => "t1.fecha_entrega_real",
3 => "t4.nombre",
4 => "t5.first_name",
5 => "t3.titulo",
6 => "t6.ubicacion",
7 => "t3.inc_rei",
8 => "t3.paginas",
9 => "t3.tirada",
10 => "t3.total_aceptado",
11 => "t1.estado"
];
protected $allowedFields = [
"total_precio",
"total_tirada",
"estado",
"user_created_id",
"user_updated_id",
"user_validated_id",
"fecha_entrega_real",
"fecha_impresion",
"fecha_encuadernado",
"fecha_entrega_externo",
"created_at",
"updated_at",
"validated_at",
];
protected $returnType = "App\Entities\Pedidos\PedidoEntity";
protected $useTimestamps = true;
protected $useSoftDeletes = false;
protected $createdField = "created_at";
protected $updatedField = "updated_at";
public static $labelField = "id";
public function obtenerDatosForm($pedido_id){
$builder = $this->db
->table($this->table . " t1")
->select(
"t4.id AS cliente_id, t4.nombre AS cliente, CONCAT(t5.first_name, ' ', t5.last_name) AS comercial");
$builder->join("pedidos_linea t2", "t2.pedido_id = t1.id", "left");
$builder->join("presupuestos t3", "t2.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");
return $builder->get()->getResultObject();
}
public function obtenerLineasPedido($pedido_id){
$builder = $this->db
->table($this->table . " t1")
->select(
"t2.presupuesto_id"
);
$builder->where("t1.id", $pedido_id);
$builder->join("pedidos_linea t2", "t2.pedido_id = t1.id", "left");
$model_presupuesto = model("App\Models\Presupuestos\PresupuestoModel");
$lineasPresupuesto = [];
foreach($builder->get()->getResultObject() as $row){
array_push($lineasPresupuesto, $model_presupuesto->generarLineaPedido($row->presupuesto_id)[0]);
}
return $lineasPresupuesto;
}
}

View File

@ -539,5 +539,172 @@ class PresupuestoModel extends \App\Models\BaseModel
return $json;
}
public function generarLineaPedido($presupuesto_id)
{
$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){
if($presupuesto->papel_formato_personalizado == 1){
$presupuesto->tamanio= $presupuesto->papel_formato_ancho . "x" . $presupuesto->papel_formato_alto;
}
$presupuesto->concepto = sprintf(lang('Pedidos.lineasTemplates.libro'),
$presupuesto->numero,
$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];
}
}
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;
$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;
$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->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;
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,
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,
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;
}
}