mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
terminado
This commit is contained in:
@ -165,12 +165,12 @@ class EtiquetasTitulosService
|
||||
->selectMax('numero_caja')
|
||||
->where('etiqueta_titulos_id', $etiqueta_id)
|
||||
->first();
|
||||
$next_caja = $next_caja->numero_caja ?? 0;
|
||||
$next_caja = $next_caja->numero_caja ?? 0;
|
||||
$next_caja++;
|
||||
|
||||
$user_id = auth()->user()->id;
|
||||
|
||||
while($unidades_restantes > 0){
|
||||
while ($unidades_restantes > 0) {
|
||||
$modelEtitquetaLinea->insert([
|
||||
'etiqueta_titulos_id' => $etiqueta_id,
|
||||
'ot_id' => $ot_id,
|
||||
@ -188,4 +188,177 @@ class EtiquetasTitulosService
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
public static function reordenarCajas($etiqueta_id)
|
||||
{
|
||||
$model = model('App\Models\Etiquetas\EtiquetasTitulosLineasModel');
|
||||
|
||||
// 1. Obtener todas las líneas ordenadas por numero_caja e id
|
||||
$lineas = $model
|
||||
->where('etiqueta_titulos_id', $etiqueta_id)
|
||||
->orderBy('numero_caja ASC')
|
||||
->orderBy('id ASC')
|
||||
->findAll();
|
||||
|
||||
// 2. Agrupar por caja
|
||||
$grupos = [];
|
||||
foreach ($lineas as $linea) {
|
||||
$grupos[$linea->numero_caja][] = $linea;
|
||||
}
|
||||
|
||||
// 3. Reasignar números de caja de forma consecutiva
|
||||
$nuevoNumeroCaja = 1;
|
||||
foreach ($grupos as $grupo) {
|
||||
foreach ($grupo as $linea) {
|
||||
$model->update($linea->id, ['numero_caja' => $nuevoNumeroCaja]);
|
||||
|
||||
}
|
||||
$nuevoNumeroCaja++;
|
||||
}
|
||||
|
||||
return [
|
||||
'status' => true,
|
||||
'message' => lang('Logistica.success.successReordenarCajas'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function imprimirEtiquetas($etiqueta_id = null, $ids = [], $printer = null){
|
||||
|
||||
$model = model('App\Models\Etiquetas\EtiquetasTitulosModel');
|
||||
$modelLineas = model('App\Models\Etiquetas\EtiquetasTitulosLineasModel');
|
||||
|
||||
if ($etiqueta_id) {
|
||||
$etiquetas = $model->where('id', $etiqueta_id)->first();
|
||||
$etiquetas_lineas = $modelLineas->whereIn('id', $ids)->orderBy('numero_caja', 'ASC')->findAll();
|
||||
}
|
||||
|
||||
// buscar el maximo numero_cajas en el array de objetos etiquetas_lineas
|
||||
$max_num_cajas = 0;
|
||||
foreach ($etiquetas_lineas as $linea) {
|
||||
if ($linea->numero_caja > $max_num_cajas) {
|
||||
$max_num_cajas = $linea->numero_caja;
|
||||
}
|
||||
}
|
||||
|
||||
// se obtienen los numero_caja diferentes en un array
|
||||
$numero_cajas = [];
|
||||
foreach ($etiquetas_lineas as $linea) {
|
||||
if (!in_array($linea->numero_caja, $numero_cajas)) {
|
||||
$numero_cajas[] = $linea->numero_caja;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($etiquetas)) {
|
||||
return [
|
||||
'status' => false,
|
||||
'message' => lang('Logistica.errors.noEtiqueta'),
|
||||
];
|
||||
}
|
||||
if (empty($etiquetas_lineas)) {
|
||||
return [
|
||||
'status' => false,
|
||||
'message' => lang('Logistica.errors.noEtiquetaLineas'),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
$data = [
|
||||
"printer" => $printer->name,
|
||||
"header" => [
|
||||
"_FORMAT" => "E:MULTI.ZPL",
|
||||
"_QUANTITY" => 1,
|
||||
"_PRINBTERNAME" => $printer->name,
|
||||
"_JOBNAME" => "LBL101"
|
||||
],
|
||||
'direccion' => $etiquetas->direccion,
|
||||
'grupos' => [],
|
||||
'notas' => $etiquetas->comentarios,
|
||||
'nombre' => $etiquetas->att,
|
||||
];
|
||||
|
||||
|
||||
$index_etiqueta = 0;
|
||||
foreach ($numero_cajas as $caja) {
|
||||
|
||||
array_push($data['grupos'], [
|
||||
]);
|
||||
|
||||
$prefix = 1;
|
||||
$lineas = array_filter($etiquetas_lineas, function ($linea) use ($caja) {
|
||||
return $linea->numero_caja == $caja;
|
||||
});
|
||||
|
||||
$lineaCounter = 0;
|
||||
foreach($lineas as $linea){
|
||||
|
||||
|
||||
if($lineaCounter >= 5){
|
||||
$lineaCounter = 0;
|
||||
$index_etiqueta++;
|
||||
array_push($data['grupos'], []);
|
||||
}
|
||||
|
||||
$modelPresupuestos = model('App\Models\OrdenTrabajo\OrdenTrabajoModel');
|
||||
$datos_etiqueta = $modelPresupuestos->select('
|
||||
pr.titulo as titulo,
|
||||
pr.isbn as isbn,
|
||||
pr.referencia_cliente as referencia_cliente,
|
||||
p.id as id_pedido,
|
||||
ordenes_trabajo.total_tirada as total_tirada,')
|
||||
->join('pedidos p', 'p.id = ordenes_trabajo.pedido_id')
|
||||
->join('pedidos_linea pl', 'pl.pedido_id = p.id')
|
||||
->join('presupuestos pr', 'pr.id = pl.presupuesto_id')
|
||||
->where('ordenes_trabajo.id',$linea->ot_id)->first();
|
||||
|
||||
|
||||
|
||||
$data['grupos'][$index_etiqueta][] = [
|
||||
'prefix' => $caja,
|
||||
'titulo' => mb_substr($datos_etiqueta->titulo, 0, 40),
|
||||
'cantidad' => $linea->unidades,
|
||||
'tirada' => $datos_etiqueta->total_tirada,
|
||||
'ean' => str_replace('-', '', $datos_etiqueta->isbn),
|
||||
'npedido' => $datos_etiqueta->id,
|
||||
'refcliente' => $datos_etiqueta->referencia_cliente,
|
||||
];
|
||||
|
||||
$lineaCounter++;
|
||||
}
|
||||
$index_etiqueta++;
|
||||
}
|
||||
|
||||
$servicioImpresora = new ImpresoraEtiquetaService();
|
||||
$xml = $servicioImpresora->createEtiquetaTitulos($data);
|
||||
if ($xml == null) {
|
||||
return [
|
||||
'status' => false,
|
||||
'message' => lang('Logistica.errors.noEtiquetas'),
|
||||
];
|
||||
}
|
||||
$sk_environment = getenv('SK_ENVIRONMENT');
|
||||
if ($sk_environment == 'production') {
|
||||
|
||||
$status = $servicioImpresora->sendToImpresoraEtiqueta("ETIQUETA", $xml, $printer);
|
||||
if ($status) {
|
||||
return [
|
||||
'status' => true,
|
||||
'message' => lang('Logistica.success.imprimirEtiquetas'),
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'status' => false,
|
||||
'message' => lang('Logistica.errors.noEtiquetas'),
|
||||
];
|
||||
}
|
||||
|
||||
} else {
|
||||
return [
|
||||
'status' => true,
|
||||
'message' => lang('Logistica.success.imprimirEtiquetas'),
|
||||
'data' => $xml
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@ -24,9 +24,9 @@ class ImpresoraEtiquetaService extends BaseService
|
||||
"labels" => [
|
||||
[
|
||||
"cliente" => "Cliente Potencial",
|
||||
"titulo" => "[1234] TEST OLIVEROS",
|
||||
"titulo" => "[1234] TEST OLIVEROS",
|
||||
"cantidad" => 100,
|
||||
"tirada" => 50,
|
||||
"tirada" => 50,
|
||||
"cajas" => 1,
|
||||
"ean" => null,
|
||||
"nombre" => "___Nombre___",
|
||||
@ -76,6 +76,66 @@ class ImpresoraEtiquetaService extends BaseService
|
||||
$xml->appendChild($labels);
|
||||
return $xml->saveXML();
|
||||
}
|
||||
|
||||
|
||||
public function createEtiquetaTitulos(array $data_label = []): ?string
|
||||
{
|
||||
$xml = new DOMDocument('1.0', 'utf-8');
|
||||
|
||||
// Crear nodo raíz "labels"
|
||||
$labels = $xml->createElement("labels");
|
||||
|
||||
// Establecer atributos de "labels" desde "header"
|
||||
foreach ($data_label["header"] as $key => $value) {
|
||||
$labels->setAttribute($key, $value);
|
||||
}
|
||||
|
||||
// Recorrer grupos de etiquetas
|
||||
foreach ($data_label["grupos"] as $grupo) {
|
||||
$labelChild = $xml->createElement('label');
|
||||
|
||||
// Crear variables específicas del grupo
|
||||
foreach ($grupo as $libro) {
|
||||
$prefix = $libro['prefix'];
|
||||
|
||||
$variables = [
|
||||
"titulo$prefix" => $libro['titulo'],
|
||||
"tirada$prefix" => $libro['tirada'],
|
||||
"ean$prefix" => $libro['ean'],
|
||||
"npedido$prefix" => $libro['npedido'],
|
||||
"refcliente$prefix" => $libro['refcliente'],
|
||||
"textpedido$prefix" => 'Pedido',
|
||||
"textcantidad$prefix" => 'Cantidad:',
|
||||
"textref$prefix" => 'Ref:',
|
||||
"textean$prefix" => 'ISBN',
|
||||
];
|
||||
|
||||
foreach ($variables as $varName => $varValue) {
|
||||
$variableChild = $xml->createElement('variable');
|
||||
$variableChild->setAttribute("name", $varName);
|
||||
$variableChild->appendChild($xml->createTextNode((string) $varValue));
|
||||
$labelChild->appendChild($variableChild);
|
||||
}
|
||||
}
|
||||
|
||||
// Variables comunes del grupo
|
||||
$nombreVariable = $xml->createElement('variable', htmlspecialchars($data_label['nombre']));
|
||||
$nombreVariable->setAttribute('name', 'nombre');
|
||||
$labelChild->appendChild($nombreVariable);
|
||||
|
||||
$direccionVariable = $xml->createElement('variable', htmlspecialchars($data_label['direccion']));
|
||||
$direccionVariable->setAttribute('name', 'direccion');
|
||||
$labelChild->appendChild($direccionVariable);
|
||||
|
||||
$labels->appendChild($labelChild);
|
||||
}
|
||||
|
||||
$xml->appendChild($labels);
|
||||
return $xml->saveXML();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function sendToImpresoraEtiqueta(string $name, string $content, ImpresoraEtiquetaEntity $impresoraEtiqueta): bool
|
||||
{
|
||||
$tmpFile = tmpfile();
|
||||
@ -84,8 +144,8 @@ class ImpresoraEtiquetaService extends BaseService
|
||||
rewind($tmpFile);
|
||||
|
||||
$tmpMetaData = stream_get_meta_data($tmpFile);
|
||||
$conn = @ftp_connect($impresoraEtiqueta->ip,$impresoraEtiqueta->port);
|
||||
if(!$conn){
|
||||
$conn = @ftp_connect($impresoraEtiqueta->ip, $impresoraEtiqueta->port);
|
||||
if (!$conn) {
|
||||
throw new Exception('Error al establecer conexión FTP');
|
||||
}
|
||||
$isLoginSuccess = @ftp_login($conn, $impresoraEtiqueta->user, $impresoraEtiqueta->pass);
|
||||
@ -99,10 +159,10 @@ class ImpresoraEtiquetaService extends BaseService
|
||||
if (ftp_put($conn, $name, $tmpMetaData['uri'], FTP_ASCII) === FALSE) {
|
||||
$status = false;
|
||||
ftp_close($conn);
|
||||
}else{
|
||||
} else {
|
||||
$status = true;
|
||||
}
|
||||
|
||||
|
||||
return $status;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user