From ee45baec3b7f7fef9ba2bdf367bdd3bdc4c3591c Mon Sep 17 00:00:00 2001 From: amazuecos Date: Mon, 19 Aug 2024 08:23:39 +0200 Subject: [PATCH] xml file generation --- ci4/app/Controllers/Pedidos/Pedido.php | 7 +- ci4/app/Models/Pedidos/PedidoModel.php | 40 ++- .../Models/Presupuestos/PresupuestoModel.php | 268 +++++++++++------- ci4/app/Services/PedidoXMLService.php | 127 ++++++--- 4 files changed, 297 insertions(+), 145 deletions(-) diff --git a/ci4/app/Controllers/Pedidos/Pedido.php b/ci4/app/Controllers/Pedidos/Pedido.php index 345d9807..af2dbfc7 100755 --- a/ci4/app/Controllers/Pedidos/Pedido.php +++ b/ci4/app/Controllers/Pedidos/Pedido.php @@ -328,11 +328,10 @@ class Pedido extends \App\Controllers\BaseResourceController } public function get_xml_pedido($pedido_id) { - $data_pedido = $this->model->getPedidoClientePresupuesto($pedido_id); - $data_pedido_presupuesto = $this->model->getPedidoPresupuestoLineas($pedido_id); - // PedidoXMLService::generate_xml($data); + + $data = PedidoXMLService::generate_xml($pedido_id); // $xml_service = new PedidoXMLService($this->model); - return $this->respond(["pedido_presupuesto" => $data_pedido,"pedido_presupuesto_linea" => $data_pedido_presupuesto]); + return $this->respond($data); } } \ No newline at end of file diff --git a/ci4/app/Models/Pedidos/PedidoModel.php b/ci4/app/Models/Pedidos/PedidoModel.php index 37b40e6c..3d238a84 100644 --- a/ci4/app/Models/Pedidos/PedidoModel.php +++ b/ci4/app/Models/Pedidos/PedidoModel.php @@ -97,25 +97,31 @@ class PedidoModel extends \App\Models\BaseModel 'pedidos.id as pedidoId', 'clientes.nombre as customerName', 'presupuestos.total_aceptado as totalAceptado', + 'presupuestos.id as presupuestoId', 'presupuestos.margen', 'presupuestos.inc_rei', 'presupuestos.tirada', 'presupuestos.titulo', 'presupuestos.paginas', + 'presupuestos.solapas', + 'presupuestos.solapas_ancho', + 'presupuestos.marcapaginas', + 'presupuestos.comentarios_cliente', + 'presupuestos.comentarios_safekat', 'presupuestos.papel_formato_personalizado', 'presupuestos.papel_formato_ancho as papelAnchoPersonalidado ', 'presupuestos.papel_formato_alto as papelAltoPersonalidado', 'lg_papel_formato.ancho as lgPapelFormatoAncho ', 'lg_papel_formato.alto as lgPapelFormatoAlto', - 'lg_tarifa_acabado.nombre as lgTarifaAcabadoNombre', + + ]) ->join('pedidos_linea', 'pedidos_linea.id = pedidos.id', 'left') ->join('presupuestos', 'presupuestos.id = pedidos_linea.presupuesto_id', 'left') + ->join('presupuesto_ficheros', 'presupuesto_ficheros.presupuesto_id = presupuestos.id', 'left') // ->join('presupuesto_linea','presupuestos.id = presupuesto_linea.presupuesto_id','left') ->join('clientes', 'clientes.id = presupuestos.cliente_id', 'left') ->join('lg_papel_formato', 'lg_papel_formato.id = presupuestos.papel_formato_id', 'left') - ->join('lg_tarifa_acabado', 'lg_tarifa_acabado.id = presupuestos.acabado_cubierta_id', 'left') - ->where('pedidos.id', $pedido_id); $cliente_presupuesto = $query->get()->getFirstRow(); return $cliente_presupuesto; @@ -124,17 +130,18 @@ class PedidoModel extends \App\Models\BaseModel { $query = $this->db->table($this->table) ->select([ - 'presupuesto_linea.*', + 'pedidos.id as pedidoId', + 'presupuesto_linea.tipo', + 'presupuesto_linea.paginas', + 'presupuesto_linea.gramaje', 'lg_papel_generico.code as papelCode', - ]) ->join('pedidos_linea', 'pedidos_linea.id = pedidos.id', 'left') ->join('presupuestos', 'presupuestos.id = pedidos_linea.presupuesto_id', 'left') ->join('presupuesto_linea', 'presupuestos.id = presupuesto_linea.presupuesto_id', 'left') ->join('lg_papel_generico', 'lg_papel_generico.id = presupuesto_linea.papel_id', 'left') - - ->where('pedidos.id', $pedido_id); + $pedido_presupuesto_lineas = $query->get()->getResultObject(); return $pedido_presupuesto_lineas; } @@ -142,12 +149,15 @@ class PedidoModel extends \App\Models\BaseModel { $query = $this->db->table($this->table) ->select([ + 'pedidos.id as pedidoId', + 'presupuestos.id as presupuestoId', 'clientes.nombre as customerName', 'presupuesto_direcciones.*', - 'lg_paises.code3' + 'lg_paises.code3 as paisCode3' ]) ->join('pedidos_linea', 'pedidos_linea.id = pedidos.id', 'left') ->join('presupuestos', 'presupuestos.id = pedidos_linea.presupuesto_id', 'left') + ->join('presupuesto_direcciones', 'presupuestos.id = presupuesto_direcciones.presupuesto_id', 'left') ->join('clientes', 'clientes.id = presupuestos.cliente_id', 'left') ->join('cliente_direcciones', 'clientes.id = cliente_direcciones.cliente_id', 'left') ->join('lg_paises', 'lg_paises.id = presupuesto_direcciones.pais_id', 'left') @@ -155,4 +165,18 @@ class PedidoModel extends \App\Models\BaseModel $pedido_cliente_direcciones = $query->get()->getResultObject(); return $pedido_cliente_direcciones; } + public function getPedidoPresupuestoFicheros($pedido_id) + { + $query = $this->db->table($this->table) + ->select([ + 'presupuesto_ficheros.nombre as fileName', + 'presupuesto_ficheros.file_path as filePath' + ]) + ->join('pedidos_linea', 'pedidos_linea.id = pedidos.id', 'left') + ->join('presupuestos', 'presupuestos.id = pedidos_linea.presupuesto_id', 'left') + ->join('presupuesto_ficheros', 'presupuesto_ficheros.presupuesto_id = presupuestos.id', 'left') + ->where('pedidos.id', $pedido_id); + $presupuesto_ficheros = $query->get()->getFirstRow(); + return $presupuesto_ficheros; + } } diff --git a/ci4/app/Models/Presupuestos/PresupuestoModel.php b/ci4/app/Models/Presupuestos/PresupuestoModel.php index 424ba107..6c8bc26a 100755 --- a/ci4/app/Models/Presupuestos/PresupuestoModel.php +++ b/ci4/app/Models/Presupuestos/PresupuestoModel.php @@ -326,7 +326,8 @@ class PresupuestoModel extends \App\Models\BaseModel return $builder; } - function getListaPresupuestosCliente($search = [] , $clienteId){ + function getListaPresupuestosCliente($search = [], $clienteId) + { $builder = $this->db ->table($this->table . " t1") @@ -343,7 +344,7 @@ class PresupuestoModel extends \App\Models\BaseModel $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) + if ($clienteId != 0) $builder->where("t1.cliente_id", $clienteId); $builder->where("t1.is_deleted", 0); @@ -363,7 +364,7 @@ class PresupuestoModel extends \App\Models\BaseModel } $builder->groupEnd(); return $builder; - } + } } function confirmarPresupuesto($presupuesto_id) @@ -377,16 +378,16 @@ class PresupuestoModel extends \App\Models\BaseModel 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['coste_envio']; + $totalCostes = $resumen_totales['totalPapel'] + $resumen_totales['totalImpresion'] + + $resumen_totales['totalServicios'] + $resumen_totales['coste_envio']; $totalMargenes = $resumen_totales['margenPapel'] + $resumen_totales['margenImpresion'] + $resumen_totales['margenServicios'] + $resumen_totales['margen_envio']; @@ -400,22 +401,22 @@ class PresupuestoModel extends \App\Models\BaseModel 'faja_color' => in_array(16, $data['servicios']) ? 1 : 0, 'ferro' => in_array(24, $data['servicios']) ? 1 : 0, 'prototipo' => in_array(9, $data['servicios']) ? 1 : 0, - '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, + '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'], 'referencia_cliente' => $data_cabecera['referenciaCliente'], 'paginas' => $data['interior']['paginas'], 'tirada' => $tirada, - 'solapas' => $data['cubierta']['solapasCubierta']>0 ? 1 : 0, - 'solapas_ancho' => $data['cubierta']['solapasCubierta']>0 ? $data['cubierta']['solapasCubierta'] : 0, - 'solapas_sobrecubierta' => is_null($data['sobrecubierta']) ? 0 :1, + 'solapas' => $data['cubierta']['solapasCubierta'] > 0 ? 1 : 0, + 'solapas_ancho' => $data['cubierta']['solapasCubierta'] > 0 ? $data['cubierta']['solapasCubierta'] : 0, + 'solapas_sobrecubierta' => is_null($data['sobrecubierta']) ? 0 : 1, 'solapas_ancho_sobrecubierta' => is_null($data['sobrecubierta']) ? 0 : $data['sobrecubierta']['solapas'], 'cosido' => $is_cosido, 'merma' => $extra_info['merma'], 'merma_cubierta' => $extra_info['merma'], - + 'lomo_cubierta' => $extra_info['lomo_cubierta'], 'lomo_sobrecubierta' => $extra_info['lomo_sobrecubierta'], @@ -424,12 +425,12 @@ class PresupuestoModel extends \App\Models\BaseModel 'acabado_cubierta_id' => $data['acabadoCubierta'], 'acabado_sobrecubierta_id' => is_null($data['sobrecubierta']) ? 0 : $data['sobrecubierta']['acabado'], - 'comp_tipo_impresion' => $data['isHq']? ($data['isColor']? 'colorhq':'negrohq'):($data['isColor']? 'color':'negro'), + '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), @@ -445,32 +446,31 @@ class PresupuestoModel extends \App\Models\BaseModel '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, 2), 'total_descuento' => 0, 'total_descuentoPercent' => 0, - 'total_precio_unidad' => round(($totalCostes + $totalMargenes)/$tirada, 4), - 'total_presupuesto' => round($totalCostes + $totalMargenes, 2), - 'total_aceptado' => round($totalCostes + $totalMargenes, 2), + 'total_precio_unidad' => round(($totalCostes + $totalMargenes) / $tirada, 4), + 'total_presupuesto' => round($totalCostes + $totalMargenes, 2), + 'total_aceptado' => round($totalCostes + $totalMargenes, 2), - 'total_factor' => round(($totalCostes + $totalMargenes-$resumen_totales['coste_envio']-$resumen_totales['margen_envio'])/$resumen_totales['sumForFactor'], 2), - 'total_factor_ponderado' => round(($totalCostes + $totalMargenes-$resumen_totales['coste_envio']-$resumen_totales['margen_envio'])/$resumen_totales['sumForFactorPonderado'], 2), + 'total_factor' => round(($totalCostes + $totalMargenes - $resumen_totales['coste_envio'] - $resumen_totales['margen_envio']) / $resumen_totales['sumForFactor'], 2), + 'total_factor_ponderado' => round(($totalCostes + $totalMargenes - $resumen_totales['coste_envio'] - $resumen_totales['margen_envio']) / $resumen_totales['sumForFactorPonderado'], 2), 'iva_reducido' => $iva_reducido, 'excluir_rotativa' => $excluir_rotativa, ]; - if($id != 0){ + if ($id != 0) { $fields['id'] = $id; $fields['updated_at'] = date('Y-m-d H:i:s', now()); } - if($id != 0){ + if ($id != 0) { $this->db->table($this->table)->where('id', $id)->update($fields); return $id; - } - else{ + } else { $this->db->table($this->table)->insert($fields); return $this->db->insertID(); } @@ -482,27 +482,27 @@ class PresupuestoModel extends \App\Models\BaseModel if (is_array($data)) { // -- INTERIOR -- // Si hay negro - if($data['interior']['paginas'] > $data['interior']['paginas_color']){ + if ($data['interior']['paginas'] > $data['interior']['paginas_color']) { - if($data['isHq']) + if ($data['isHq']) $key = 'bnhq'; else $key = 'bn'; $values[$key] = array( - 'paginas'=> intval($data['interior']['paginas'])-intval($data['interior']['paginas_color']), + 'paginas' => intval($data['interior']['paginas']) - intval($data['interior']['paginas_color']), 'papel_id' => intval($data['interior']['papel_generico']['id']), 'gramaje' => intval($data['interior']['gramaje']), ); } - // Si hay color - if($data['interior']['paginas_color']>0){ + // Si hay color + if ($data['interior']['paginas_color'] > 0) { - if($data['isHq']) + if ($data['isHq']) $key = 'colorhq'; else $key = 'color'; $values[$key] = array( - 'paginas'=> intval($data['interior']['paginas_color']), + 'paginas' => intval($data['interior']['paginas_color']), 'papel_id' => intval($data['interior']['papel_generico']['id']), 'gramaje' => intval($data['interior']['gramaje']), ); @@ -516,7 +516,7 @@ class PresupuestoModel extends \App\Models\BaseModel ); // -- SOBRECUBIERTA -- - if(!is_null($data['sobrecubierta'])){ + if (!is_null($data['sobrecubierta'])) { $values['sobrecubierta'] = array( 'papel_id' => intval($data['sobrecubierta']['papel']), 'gramaje' => intval($data['sobrecubierta']['gramaje']), @@ -525,14 +525,13 @@ class PresupuestoModel extends \App\Models\BaseModel } // -- GUARDAS -- - if($data['datos_guardas'] != 0){ + if ($data['datos_guardas'] != 0) { $values['guardas'] = array( 'papel_id' => intval($data['datos_guardas']['papel']), 'gramaje' => intval($data['datos_guardas']['gramaje']), 'paginas' => intval($data['datos_guardas']['caras']), ); } - } $json = json_encode($values); return $json; @@ -557,32 +556,33 @@ class PresupuestoModel extends \App\Models\BaseModel $builder->where("t1.is_deleted", 0); $builder->where("t1.id", $presupuesto_id); $presupuesto = $builder->get()->getResultObject(); - if(count($presupuesto) > 0){ + if (count($presupuesto) > 0) { $modelLinea = model('App\Models\Presupuestos\PresupuestoLineaModel'); - $lineas = $modelLinea->where('presupuesto_id', $presupuesto_id)->findAll(); + $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 ($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){ + + if ($forFactura) { $presupuesto->concepto = sprintf(lang('Pedidos.lineasTemplates.pedido'), $pedido_id); - } - else{ + } else { $presupuesto->concepto = sprintf(lang('Pedidos.lineasTemplates.presupuesto'), $presupuesto->numero); } - $presupuesto->concepto .= sprintf(lang('Pedidos.lineasTemplates.libro'), + $presupuesto->concepto .= sprintf( + lang('Pedidos.lineasTemplates.libro'), $presupuesto->unidades, $presupuesto->paginas, $presupuesto->titulo, $presupuesto->autor, $presupuesto->isbn, - $presupuesto->tamanio); + $presupuesto->tamanio + ); $presupuesto->concepto .= $this->generarConceptoLineasPresupuestoLibro($lineas, $presupuesto); $presupuesto = (object)[ @@ -594,10 +594,67 @@ class PresupuestoModel extends \App\Models\BaseModel } return [$presupuesto]; } - } + public function getServiciosPresupuesto($presupuesto_id) + { + $queryAcabado = $this->db->table($this->table) + ->select( + [ + 'lg_tarifa_acabado.id', + 'lg_tarifa_acabado.nombre', - private function generarConceptoLineasPresupuestoLibro($lineas, $presupuesto){ + + ] + ) + ->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; + } + private function generarConceptoLineasPresupuestoLibro($lineas, $presupuesto) + { $model_papel = model('App\Models\Configuracion\PapelImpresionModel'); $description_interior = ""; @@ -610,105 +667,118 @@ class PresupuestoModel extends \App\Models\BaseModel $gramaje_negro = 0; $gramaje_color = 0; - $lp_bn_lines = array_filter($lineas, function($linea) { + $lp_bn_lines = array_filter($lineas, function ($linea) { return strpos($linea->tipo, 'lp_bn') === 0; }); - $lp_color_lines = array_filter($lineas, function($linea) { + $lp_color_lines = array_filter($lineas, function ($linea) { return strpos($linea->tipo, 'lp_color') === 0; }); - $lp_rot_bn = array_filter($lineas, function($linea) { + $lp_rot_bn = array_filter($lineas, function ($linea) { return strpos($linea->tipo, 'lp_rot_bn') === 0; }); - $lp_rot_color = array_filter($lineas, function($linea) { + $lp_rot_color = array_filter($lineas, function ($linea) { return strpos($linea->tipo, 'lp_rot_color') === 0; }); - - if(count($lp_bn_lines) > 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'), + $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)) . ". "; + strval($gramaje_negro) + ) . ". "; } - if(count($lp_color_lines) > 0){ + 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'), + $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)) . ". "; + strval($gramaje_color) + ) . ". "; } - if(count($lp_rot_bn) > 0){ + 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'), + $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)) . ". "; + strval($gramaje_negro) + ) . ". "; } - if(count($lp_rot_color) > 0){ + 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); + $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)) . ". "; + $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'), + $description_interior .= sprintf( + lang('Pedidos.lineasTemplates.libro_linea_interior'), strval($lp_rot_color->rotativa_pag_color), $papel, - strval($gramaje)) . ". "; + strval($gramaje) + ) . ". "; } - $lp_cubierta = array_filter($lineas, function($linea) { + $lp_cubierta = array_filter($lineas, function ($linea) { return strpos($linea->tipo, 'lp_cubierta') === 0; }); - $lp_sobrecubierta = array_filter($lineas, function($linea) { + $lp_sobrecubierta = array_filter($lineas, function ($linea) { return strpos($linea->tipo, 'lp_sobrecubierta') === 0; }); - if(count($lp_cubierta) > 0){ + if (count($lp_cubierta) > 0) { $lp_cubierta = array_values($lp_cubierta)[0]; - if($lp_cubierta->paginas == 2){ + if ($lp_cubierta->paginas == 2) { $lp_cubierta->caras = lang('Pedidos.unaCara'); - } - else{ + } else { $lp_cubierta->caras = lang('Pedidos.dosCaras'); } - $description_cubierta = sprintf(lang('Pedidos.lineasTemplates.libro_linea_cubierta'), + $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){ + $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):". "); - } + $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; + $acabado = sprintf( + lang('Pedidos.lineasTemplates.libro_encuadernacion'), + lang('Presupuestos.' . $presupuesto->codigo_encuadernacion) + ); + return $description_interior . $description_cubierta . $description_sobrecubierta . $acabado; } - } diff --git a/ci4/app/Services/PedidoXMLService.php b/ci4/app/Services/PedidoXMLService.php index 55751ba1..b09edaa1 100644 --- a/ci4/app/Services/PedidoXMLService.php +++ b/ci4/app/Services/PedidoXMLService.php @@ -6,45 +6,104 @@ use App\Entities\Pedidos\PedidoEntity; use CodeIgniter\Config\BaseService; use App\Models\Pedidos\PedidoModel; use App\Models\Pedidos\PedidoLineaModel; +use App\Models\Presupuestos\PresupuestoModel; use DOMDocument; use DOMNode; class PedidoXMLService extends BaseService { - public static function generate_xml($data) + public static function get_pedido_presupuesto(int $pedido_id) + { + $data_xml = []; + $pedidoModel = model(PedidoModel::class); + $presupuestoModel = model(PresupuestoModel::class); + $data_xml['pedido_cliente_presupuesto'] = $pedidoModel->getPedidoClientePresupuesto($pedido_id); + $data_xml['pedido_presupuesto_direcciones'] = $pedidoModel->getPedidoPresupuestoDirecciones($pedido_id); + $data_xml['pedido_presupuesto_lineas'] = $pedidoModel->getPedidoPresupuestoLineas($pedido_id); + $servicios = $presupuestoModel->getServiciosPresupuesto($data_xml['pedido_cliente_presupuesto']->presupuestoId); + $data_xml['servicios'] = $servicios; + $data_xml['preimpresion'] = PedidoXMLService::parse_servicio_preimpresion($servicios['preimpresion']); + $data_xml["acabado"] = PedidoXMLService::parse_servicio_acabado($servicios['acabado']); + return $data_xml; + + } + protected static function parse_servicio_acabado(array $data_xml_servicios_acabado) + { + $xml_element = []; + $service_xml_key_value = [ + "ShrinkWrapping" => fn($nombre) => str_contains($nombre,"retractilado"), + "Finish" => fn($nombre) => str_contains($nombre,"brillo"), + "PlakeneT" =>fn($nombre) => str_contains($nombre,"plakene traslúcido"),]; + foreach($data_xml_servicios_acabado as $servicio_acabado) + { + $service_name = strtolower($servicio_acabado->nombre); + foreach ($service_xml_key_value as $key => $value) { + $xml_element[$key] = $value($service_name) ? 1 : 0 ; + } + + } + return $xml_element; + } + protected static function parse_servicio_preimpresion(array $data_xml_servicios_preimpresion) + { + $xml_element = []; + $service_xml_key_value = [ + "Urgent" => fn($nombre) => str_contains($nombre,"Pedido urgente"), + "Prototype" => fn($nombre) => str_contains($nombre,"Prototipo"), + "Layout" =>fn($nombre) => str_contains($nombre,"Maquetación"), + "Correction" =>fn($nombre) => str_contains($nombre,"Corrección ortográfica"), + // "Review" =>fn($nombre) => str_contains($nombre,"Revisión Profesional de archivo"), + "Design" =>fn($nombre) => str_contains($nombre,'Diseño de Cubierta'), + ]; + foreach($data_xml_servicios_preimpresion as $servicio_pre) + { + $service_name = $servicio_pre->nombre; + foreach ($service_xml_key_value as $key => $value) { + $value_service = $value($service_name) ? 1 : 0 ; + if( $value_service){ + $xml_element[$key] = $servicio_pre->precio ; + }else if(!isset($xml_element[$key])){ + $xml_element[$key] = $value_service; + } + } + } + return $xml_element; + } + public static function generate_xml($pedido_id) { $papel_formato_ancho = 0; $papel_formato_alto = 0; + $data = PedidoXMLService::get_pedido_presupuesto($pedido_id); $xml = new DOMDocument('1.0', 'utf-8'); $xml_order_el = $xml->createElement('Order'); $xml_header_el = $xml->createElement('Header'); $xml_header_el->appendChild($xml->createElement('CustomerCode', 988)); $xml_header_el->appendChild($xml->createElement('CodeNode', "false")); - $xml_header_el->appendChild($xml->createElement('ExternId', $data->pedido_presupuesto->pedidoId)); + $xml_header_el->appendChild($xml->createElement('ExternId', $data["pedido_cliente_presupuesto"]->pedidoId)); $xml_header_el->appendChild($xml->createElement('NumProducts', 988)); $xml_header_el->appendChild($xml->createElement('Date', now_db())); $xml_order_el->appendChild($xml_header_el); $xml_products_el = $xml->createElement('Products'); - $xml_products_el->appendChild($xml->createElement('ItemId', $data->pedido_presupuesto->pedidoId)); - $xml_products_el->appendChild($xml->createElement('Quantity', $data->pedido_presupuesto->tirada)); - $xml_products_el->appendChild($xml->createElement('Title', $data->pedido_presupuesto->titulo)); - $xml_products_el->appendChild($xml->createElement('Pages', $data->pedido_presupuesto->paginas)); - $xml_products_el->appendChild($xml->createElement('Reprint', $data->pedido_presupuesto->inc_rei)); + $xml_products_el->appendChild($xml->createElement('ItemId', $data["pedido_cliente_presupuesto"]->pedidoId)); + $xml_products_el->appendChild($xml->createElement('Quantity', $data["pedido_cliente_presupuesto"]->tirada)); + $xml_products_el->appendChild($xml->createElement('Title', $data["pedido_cliente_presupuesto"]->titulo)); + $xml_products_el->appendChild($xml->createElement('Pages', $data["pedido_cliente_presupuesto"]->paginas)); + $xml_products_el->appendChild($xml->createElement('Reprint', $data["pedido_cliente_presupuesto"]->inc_rei)); - if ($data->pedido_presupuesto->papel_formato_personalizado) { - $papel_formato_ancho = $data->pedido_presupuesto->papelAnchoPersonalidado; - $papel_formato_alto = $data->pedido_presupuesto->papelAltoPersonalidado; + if ($data["pedido_cliente_presupuesto"]->papel_formato_personalizado) { + $papel_formato_ancho = $data["pedido_cliente_presupuesto"]->papelAnchoPersonalidado; + $papel_formato_alto = $data["pedido_cliente_presupuesto"]->papelAltoPersonalidado; } else { - $papel_formato_ancho = $data->pedido_presupuesto->lgPapelFormatoAncho; - $papel_formato_alto = $data->pedido_presupuesto->lgPapelFormatoAlto; + $papel_formato_ancho = $data["pedido_cliente_presupuesto"]->lgPapelFormatoAncho; + $papel_formato_alto = $data["pedido_cliente_presupuesto"]->lgPapelFormatoAlto; } $xml_products_el->appendChild($xml->createElement('Width', $papel_formato_ancho)); $xml_products_el->appendChild($xml->createElement('Height', $papel_formato_alto)); $presupuestoLineaTipoCubierta = null; $xml_presupuesto_lineas_el = $xml->createElement('Lines'); ## Iterate throught presupuesto_lineas - foreach ($data->pedido_presupuesto_lineas as $row) { + foreach ($data["pedido_presupuesto_lineas"] as $row) { if (str_contains($row->tipo, "rot") || str_contains($row->tipo, "bn") || str_contains($row->tipo, "color")) { $colorInterior = PedidoXMLService::get_color_interior($row); $xmlInside = $xml->createElement('Inside'); @@ -63,8 +122,8 @@ class PedidoXMLService extends BaseService $xml_products_el->appendChild($xml_presupuesto_lineas_el); if ($presupuestoLineaTipoCubierta) { - $lgTarifaAcabadoNombre = $data->pedido_presupuesto->lgTarifaAcabadoNombre; - if (str_contains('brillo', $lgTarifaAcabadoNombre)) { + $containsTarifaAcabadoBrillo = isset($data['acabado']['Finish']) ? true : false; + if ($containsTarifaAcabadoBrillo) { $acabado = "brillo"; } else { $acabado = "mate"; @@ -73,43 +132,43 @@ class PedidoXMLService extends BaseService $xmlCover->appendChild($xml->createElement('Sides', $presupuestoLineaTipoCubierta->paginas / 2)); //! PAGINAS CUBIERTA $xmlCover->appendChild($xml->createElement('Paper', $presupuestoLineaTipoCubierta->papelCode)); //? $xmlCover->appendChild($xml->createElement('Weight', $presupuestoLineaTipoCubierta->gramaje)); - $xmlCover->appendChild($xml->createElement('Flaps', $data->pedido_presupuesto->solapas)); - $xmlCover->appendChild($xml->createElement('WidthFlaps', $data->pedido_presupuesto->solapas_ancho)); + $xmlCover->appendChild($xml->createElement('Flaps', $data["pedido_cliente_presupuesto"]->solapas)); + $xmlCover->appendChild($xml->createElement('WidthFlaps', $data["pedido_cliente_presupuesto"]->solapas_ancho)); $xmlCover->appendChild($xml->createElement('Finish', $acabado)); $xml_products_el->appendChild($xmlCover); } $xml_services_el = $xml->createElement('Services'); - $xml_services_el->appendChild($xml->createElement('Bookmark', $data->pedido_presupuesto->marcapaginas)); - //? Retractilado5 y Retractilado o ambas - //! MIRAR - $xml_services_el->appendChild($xml->createElement('ShrinkWrapping', $data->pedido_presupuesto->retractilado)); - //? $xml_services_el->appendChild($xml->createElement('PlakeneT', $data->pedido_presupuesto->plakeneT)); ?? - //? $xml_services_el->appendChild($xml->createElement('Urgent', $data->pedido_presupuesto->urgent)); PEDIDO URGENTE - $xml_services_el->appendChild($xml->createElement('Prototype', $data->pedido_presupuesto->prototipo)); // - //? $xml_services_el->appendChild($xml->createElement('Layout', $data->pedido_presupuesto->maquetacion)); , MAQUETACION - //? $xml_services_el->appendChild($xml->createElement('Correction', $data->pedido_presupuesto->correccion)); CORRECCION ORTOGRAFICA - //? $xml_services_el->appendChild($xml->createElement('Review', $data->pedido_presupuesto->revision)); REVISION ?? - //? $xml_services_el->appendChild($xml->createElement('Design', $data->pedido_presupuesto->diseno)); DISEÑO DE CUBIERTA + $xml_services_el->appendChild($xml->createElement('Bookmark', $data["pedido_cliente_presupuesto"]->marcapaginas)); + foreach ($data['preimpresion'] as $key => $value) { + $xml_services_el->appendChild($xml->createElement($key, $value)); + } + foreach ($data['acabado'] as $key => $value) { + $xml_services_el->appendChild($xml->createElement($key, $value)); + } + $xml_products_el->appendChild($xml_services_el); $xml_envios_el = $xml->createElement('Shipments'); - foreach ($data->pedido_presupuesto_direcciones as $pedido_presupuesto_direccion) { + foreach ($data["pedido_presupuesto_direcciones"] as $pedido_presupuesto_direccion) { $xml_envio_el = $xml->createElement('Shipment'); $xml_envio_el->appendChild($xml->createElement('Qty', $pedido_presupuesto_direccion->cantidad)); - $xml_envio_el->appendChild($xml->createElement('Price', $data->pedido_presupuesto_direccion->precio)); + $xml_envio_el->appendChild($xml->createElement('Price', $pedido_presupuesto_direccion->precio)); $xml_envio_el->appendChild($xml->createElement('Attention', $pedido_presupuesto_direccion->att)); $xml_envio_el->appendChild($xml->createElement('Email', $pedido_presupuesto_direccion->email)); $xml_envio_el->appendChild($xml->createElement('Address', $pedido_presupuesto_direccion->direccion)); $xml_envio_el->appendChild($xml->createElement('Province', $pedido_presupuesto_direccion->provincia)); - $xml_envio_el->appendChild($xml->createElement('City', $pedido_presupuesto_direccion->ciudad)); + $xml_envio_el->appendChild($xml->createElement('City', $pedido_presupuesto_direccion->municipio)); $xml_envio_el->appendChild($xml->createElement('Zip', $pedido_presupuesto_direccion->cp)); - $xml_envio_el->appendChild($xml->createElement('CountryCode', $pedido_presupuesto_direccion->code3)); + $xml_envio_el->appendChild($xml->createElement('CountryCode', $pedido_presupuesto_direccion->paisCode3)); $xml_envio_el->appendChild($xml->createElement('Telephone', $pedido_presupuesto_direccion->telefono)); $xml_envios_el->appendChild($xml_envio_el); } $xml_products_el->appendChild($xml_envios_el); - $xml_products_el->appendChild($xml->createElement('Comments', $data->pedido_presupuesto->comentarios_safekat)); - $xml_products_el->appendChild($xml->createElement('CommentsClient', $data->pedido_presupuesto->comentarios_cliente)); + $xml_products_el->appendChild($xml->createElement('Comments', $data["pedido_cliente_presupuesto"]->comentarios_safekat)); + $xml_products_el->appendChild($xml->createElement('CommentsClient', $data["pedido_cliente_presupuesto"]->comentarios_cliente)); + $xml->appendChild($xml_products_el); + $file_has_suffix = hash('sha512',$data["pedido_cliente_presupuesto"]->pedidoId); + return $data; } protected static function get_color_interior($pre_linea): ?string {