Skip to content

Commit 0c3be1a

Browse files
committed
feat: funzione copia/incolla delle righe tra i documenti
1 parent d9fd466 commit 0c3be1a

12 files changed

Lines changed: 1063 additions & 0 deletions

File tree

modules/contratti/actions.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,104 @@
340340

341341
break;
342342

343+
// Recupero dati righe per copia negli appunti
344+
case 'get_righe_data':
345+
$id_righe = (array) post('righe');
346+
$righe_data = [];
347+
348+
foreach ($id_righe as $id_riga) {
349+
$riga = Articolo::find($id_riga) ?: Riga::find($id_riga);
350+
$riga = $riga ?: Descrizione::find($id_riga);
351+
$riga = $riga ?: Sconto::find($id_riga);
352+
353+
if ($riga) {
354+
$riga_array = [
355+
'type' => get_class($riga),
356+
'descrizione' => $riga->descrizione,
357+
'qta' => $riga->qta,
358+
'um' => $riga->um,
359+
'prezzo_unitario' => $riga->prezzo_unitario,
360+
'sconto_unitario' => $riga->sconto_unitario,
361+
'sconto_percentuale' => $riga->sconto_percentuale,
362+
'tipo_sconto' => $riga->tipo_sconto,
363+
'idiva' => $riga->idiva,
364+
'id_conto' => $riga->id_conto,
365+
'note' => $riga->note,
366+
];
367+
368+
if ($riga->isArticolo()) {
369+
$riga_array['idarticolo'] = $riga->idarticolo;
370+
$riga_array['codice'] = $riga->codice;
371+
$riga_array['costo_unitario'] = $riga->costo_unitario;
372+
}
373+
374+
$righe_data[] = $riga_array;
375+
}
376+
}
377+
378+
echo json_encode([
379+
'data' => $righe_data,
380+
]);
381+
382+
break;
383+
384+
// Incolla righe dagli appunti
385+
case 'paste_righe':
386+
$righe_data = json_decode(post('righe_data'), true);
387+
388+
if (is_array($righe_data)) {
389+
foreach ($righe_data as $riga_data) {
390+
$type = $riga_data['type'];
391+
$class_name = substr($type, strrpos($type, '\\') + 1);
392+
393+
if ($class_name == 'Articolo' && !empty($riga_data['idarticolo'])) {
394+
$articolo_originale = ArticoloOriginale::find($riga_data['idarticolo']);
395+
if ($articolo_originale) {
396+
$riga = Articolo::build($contratto, $articolo_originale);
397+
$riga->costo_unitario = $riga_data['costo_unitario'];
398+
} else {
399+
$riga = Riga::build($contratto);
400+
}
401+
} elseif ($class_name == 'Descrizione') {
402+
$riga = Descrizione::build($contratto);
403+
} elseif ($class_name == 'Sconto') {
404+
$riga = Sconto::build($contratto);
405+
} else {
406+
$riga = Riga::build($contratto);
407+
}
408+
409+
$riga->descrizione = $riga_data['descrizione'];
410+
$riga->qta = $riga_data['qta'];
411+
$riga->um = $riga_data['um'];
412+
413+
if (!$riga->isDescrizione()) {
414+
$riga->prezzo_unitario = $riga_data['prezzo_unitario'];
415+
$riga->sconto_unitario = $riga_data['sconto_unitario'];
416+
$riga->sconto_percentuale = $riga_data['sconto_percentuale'];
417+
$riga->tipo_sconto = $riga_data['tipo_sconto'];
418+
$riga->idiva = $riga_data['idiva'];
419+
$riga->id_conto = $riga_data['id_conto'];
420+
}
421+
422+
$riga->note = $riga_data['note'];
423+
$riga->save();
424+
}
425+
426+
flash()->info(tr('Righe incollate correttamente!'));
427+
428+
echo json_encode([
429+
'status' => 'success',
430+
]);
431+
} else {
432+
flash()->error(tr('Errore durante l\'incollaggio delle righe'));
433+
434+
echo json_encode([
435+
'status' => 'error',
436+
]);
437+
}
438+
439+
break;
440+
343441
// Scollegamento intervento da contratto
344442
case 'unlink':
345443
if (!empty(get('idcontratto')) && !empty(get('idintervento'))) {

modules/contratti/row-list.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,22 @@
434434
<button type="button" class="btn btn-xs btn-default disabled" id="modifica_iva_righe" onclick="modificaIvaRighe(getSelectData());">
435435
<i class="fa fa-percent"></i> '.tr('Modifica IVA').'
436436
</button>
437+
438+
<button type="button" class="btn btn-xs btn-primary disabled" id="copia_righe" onclick="copiaRighe(getSelectData());" title="'.tr('Copia righe selezionate negli appunti').'">
439+
<i class="fa fa-clipboard"></i> '.tr('Copia').'
440+
</button>
441+
442+
<button type="button" class="btn btn-xs btn-primary" id="incolla_righe" onclick="incollaRighe();" title="'.tr('Incolla righe dagli appunti').'">
443+
<i class="fa fa-paste"></i> '.tr('Incolla').'
444+
</button>
445+
</div>';
446+
}
447+
if (!$block_edit && sizeof($righe) == 0) {
448+
echo '
449+
<div class="btn-group">
450+
<button type="button" class="btn btn-xs btn-primary" id="incolla_righe" onclick="incollaRighe();" title="'.tr('Incolla righe dagli appunti').'">
451+
<i class="fa fa-paste"></i> '.tr('Incolla').'
452+
</button>
437453
</div>';
438454
}
439455
echo '
@@ -609,13 +625,15 @@ function duplicaRiga(id) {
609625
$("#confronta_righe").removeClass("disabled");
610626
$("#aggiorna_righe").removeClass("disabled");
611627
$("#modifica_iva_righe").removeClass("disabled");
628+
$("#copia_righe").removeClass("disabled");
612629
$("#elimina").addClass("disabled");
613630
} else {
614631
$("#elimina_righe").addClass("disabled");
615632
$("#duplica_righe").addClass("disabled");
616633
$("#confronta_righe").addClass("disabled");
617634
$("#aggiorna_righe").addClass("disabled");
618635
$("#modifica_iva_righe").addClass("disabled");
636+
$("#copia_righe").addClass("disabled");
619637
$("#elimina").removeClass("disabled");
620638
}
621639
});
@@ -702,4 +720,107 @@ function modificaIvaRighe(righe) {
702720
openModal("'.tr('Modifica IVA').'", "'.$module->fileurl('modals/modifica_iva.php').'?id_module=" + globals.id_module + "&id_record=" + globals.id_record + "&righe=" + righe.join(','));
703721
}
704722
}
723+
724+
function copiaRighe(righe) {
725+
if (righe.length === 0) {
726+
return;
727+
}
728+
729+
$.ajax({
730+
url: globals.rootdir + "/actions.php",
731+
type: "POST",
732+
dataType: "json",
733+
data: {
734+
id_module: globals.id_module,
735+
id_record: globals.id_record,
736+
op: "get_righe_data",
737+
righe: righe,
738+
},
739+
success: function (response) {
740+
if (response && response.data) {
741+
navigator.clipboard.writeText(JSON.stringify(response.data)).then(function() {
742+
swal({
743+
title: "'.tr('Righe copiate!').'",
744+
text: "'.tr('Le righe selezionate sono state copiate negli appunti').'",
745+
type: "success",
746+
timer: 2000,
747+
showConfirmButton: false
748+
});
749+
}).catch(function(err) {
750+
swal({
751+
title: "'.tr('Errore').'",
752+
text: "'.tr('Impossibile copiare negli appunti').': " + err,
753+
type: "error"
754+
});
755+
});
756+
}
757+
},
758+
error: function() {
759+
swal({
760+
title: "'.tr('Errore').'",
761+
text: "'.tr('Errore durante il recupero dei dati delle righe').'",
762+
type: "error"
763+
});
764+
}
765+
});
766+
}
767+
768+
function incollaRighe() {
769+
navigator.clipboard.readText().then(function(text) {
770+
try {
771+
let righe_data = JSON.parse(text);
772+
773+
swal({
774+
title: "'.tr('Incollare le righe?').'",
775+
html: "'.tr('Sei sicuro di voler incollare').' " + righe_data.length + " '.tr('righe in questo documento?').'",
776+
type: "warning",
777+
showCancelButton: true,
778+
confirmButtonText: "'.tr('').'"
779+
}).then(function () {
780+
$.ajax({
781+
url: globals.rootdir + "/actions.php",
782+
type: "POST",
783+
dataType: "json",
784+
data: {
785+
id_module: globals.id_module,
786+
id_record: globals.id_record,
787+
op: "paste_righe",
788+
righe_data: JSON.stringify(righe_data),
789+
},
790+
success: function (response) {
791+
renderMessages();
792+
caricaRighe(null);
793+
swal({
794+
title: "'.tr('Righe incollate!').'",
795+
text: "'.tr('Le righe sono state incollate con successo').'",
796+
type: "success",
797+
timer: 2000,
798+
showConfirmButton: false
799+
});
800+
},
801+
error: function() {
802+
renderMessages();
803+
swal({
804+
title: "'.tr('Errore').'",
805+
text: "'.tr('Errore durante l\'incollaggio delle righe').'",
806+
type: "error"
807+
});
808+
}
809+
});
810+
}).catch(swal.noop);
811+
} catch (e) {
812+
swal({
813+
title: "'.tr('Errore').'",
814+
text: "'.tr('I dati negli appunti non sono validi').'",
815+
type: "error"
816+
});
817+
}
818+
}).catch(function(err) {
819+
swal({
820+
title: "'.tr('Errore').'",
821+
text: "'.tr('Impossibile leggere dagli appunti').': " + err,
822+
type: "error"
823+
});
824+
});
825+
}
705826
</script>';

modules/ddt/actions.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,74 @@
437437

438438
break;
439439

440+
case 'get_righe_data':
441+
$id_righe = (array) post('righe');
442+
$righe_data = [];
443+
foreach ($id_righe as $id_riga) {
444+
$riga = Articolo::find($id_riga) ?: Riga::find($id_riga);
445+
$riga = $riga ?: Descrizione::find($id_riga);
446+
$riga = $riga ?: Sconto::find($id_riga);
447+
if ($riga) {
448+
$riga_array = [
449+
'type' => get_class($riga), 'descrizione' => $riga->descrizione, 'qta' => $riga->qta, 'um' => $riga->um,
450+
'prezzo_unitario' => $riga->prezzo_unitario, 'sconto_unitario' => $riga->sconto_unitario,
451+
'sconto_percentuale' => $riga->sconto_percentuale, 'tipo_sconto' => $riga->tipo_sconto,
452+
'idiva' => $riga->idiva, 'id_conto' => $riga->id_conto, 'note' => $riga->note,
453+
];
454+
if ($riga->isArticolo()) {
455+
$riga_array['idarticolo'] = $riga->idarticolo;
456+
$riga_array['codice'] = $riga->codice;
457+
$riga_array['costo_unitario'] = $riga->costo_unitario;
458+
}
459+
$righe_data[] = $riga_array;
460+
}
461+
}
462+
echo json_encode(['data' => $righe_data]);
463+
break;
464+
465+
case 'paste_righe':
466+
$righe_data = json_decode(post('righe_data'), true);
467+
if (is_array($righe_data)) {
468+
foreach ($righe_data as $riga_data) {
469+
$type = $riga_data['type'];
470+
$class_name = substr($type, strrpos($type, '\\') + 1);
471+
if ($class_name == 'Articolo' && !empty($riga_data['idarticolo'])) {
472+
$articolo_originale = ArticoloOriginale::find($riga_data['idarticolo']);
473+
if ($articolo_originale) {
474+
$riga = Articolo::build($ddt, $articolo_originale);
475+
$riga->costo_unitario = $riga_data['costo_unitario'];
476+
} else {
477+
$riga = Riga::build($ddt);
478+
}
479+
} elseif ($class_name == 'Descrizione') {
480+
$riga = Descrizione::build($ddt);
481+
} elseif ($class_name == 'Sconto') {
482+
$riga = Sconto::build($ddt);
483+
} else {
484+
$riga = Riga::build($ddt);
485+
}
486+
$riga->descrizione = $riga_data['descrizione'];
487+
$riga->qta = $riga_data['qta'];
488+
$riga->um = $riga_data['um'];
489+
if (!$riga->isDescrizione()) {
490+
$riga->prezzo_unitario = $riga_data['prezzo_unitario'];
491+
$riga->sconto_unitario = $riga_data['sconto_unitario'];
492+
$riga->sconto_percentuale = $riga_data['sconto_percentuale'];
493+
$riga->tipo_sconto = $riga_data['tipo_sconto'];
494+
$riga->idiva = $riga_data['idiva'];
495+
$riga->id_conto = $riga_data['id_conto'];
496+
}
497+
$riga->note = $riga_data['note'];
498+
$riga->save();
499+
}
500+
flash()->info(tr('Righe incollate correttamente!'));
501+
echo json_encode(['status' => 'success']);
502+
} else {
503+
flash()->error(tr('Errore durante l\'incollaggio delle righe'));
504+
echo json_encode(['status' => 'error']);
505+
}
506+
break;
507+
440508
// eliminazione ddt
441509
case 'delete':
442510
try {

0 commit comments

Comments
 (0)