Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions modules/anagrafiche/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@
use Models\PrintTemplate;
use Models\Upload;
use Modules\Anagrafiche\Anagrafica;
//mod maulapi
use Modules\Anagrafiche\Referente;
//fine mod maulapi

switch (post('op')) {
case 'restore':
$anagrafica->restore();
// Ripristina anche i referenti collegati
$dbo->query('UPDATE an_referenti SET deleted_at = NULL WHERE id_anagrafica = '.prepare($id_record));
flash()->info(tr('Anagrafica _NAME_ ripristinata correttamente!', [
'_NAME_' => post('ragione_sociale'),
]));
Expand Down Expand Up @@ -255,6 +260,34 @@

$anagrafica->save();

//mod maulapi
// Se sono stati forniti dati per il referente, li salvo nella tabella an_referenti
if (!empty(post('referente_nome')) || !empty(post('referente_cellulare')) || !empty(post('referente_email')) || !empty(post('referente_mansione'))) {
try {
// Normalizzo l'id mansione (se presente)
$id_mansione = (!empty(post('referente_mansione')) && post('referente_mansione') != '-1') ? post('referente_mansione') : null;

// Creo il referente (build salva già il record)
//$referente = Referente::build($anagrafica->id, post('referente_nome') ?: null,
//$id_mansione);
$referente = Referente::build($anagrafica->id, post('referente_nome') ?: null, $id_mansione, 0);

// Aggiungo i campi opzionali e salvo (build() esegue già save, ma risalvo per sicurezza dopo aver settato gli altri campi)
if (!empty(post('referente_cellulare'))) {
$referente->telefono = post('referente_cellulare');
}
if (!empty(post('referente_email'))) {
$referente->email = post('referente_email');
}
$referente->save();
} catch (\Throwable $e) {
// Non bloccare la creazione dell'anagrafica: loggare l'errore e mostrare warning
\error_log('Errore salvataggio referente: '.$e->getMessage());
flash()->warning(tr('Impossibile salvare il referente:').' '.$e->getMessage());
}
}
//fine mod maulapi

if ($anagrafica->isAzienda()) {
flash()->info(tr('Anagrafica Azienda impostata come predefinita').'. '.tr('Per ulteriori informazioni, visitare "Strumenti -> Impostazioni -> Generali"'));
}
Expand Down Expand Up @@ -344,6 +377,8 @@
if (!$anagrafica->isAzienda()) {
// $anagrafica->delete();
$dbo->query('UPDATE an_anagrafiche SET deleted_at = NOW() WHERE id = '.prepare($id_record));
// dopo la riga che setta deleted_at per l'anagrafica
$dbo->query('UPDATE an_referenti SET deleted_at = NOW() WHERE id_anagrafica = '.prepare($id_record));

// Se l'anagrafica è collegata ad un utente lo disabilito
$dbo->query('UPDATE zz_users SET enabled = 0 WHERE id_anagrafica = '.prepare($id_record));
Expand Down
34 changes: 34 additions & 0 deletions modules/anagrafiche/add.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,40 @@
</div>
</div>';

//mod maulapi
//<!-- INSERIRE questo blocco prima dei PULSANTI / modal-footer -->
echo '
<br>
<div class="card card-info collapsed-card">
<div class="card-header">
<h3 class="card-title">'.tr('Referente').'</h3>
<div class="card-tools pull-right">
<button type="button" class="btn btn-tool" data-card-widget="collapse">
<i class="fa fa-plus"></i>
</button>
</div>
</div>
<div class="card-body collapse">
<div class="row">
<div class="col-md-6">
{[ "type": "text", "label": "'.tr('Nominativo').'", "name": "referente_nome", "id": "referente_nome_add" ]}
</div>
<div class="col-md-6">
{[ "type": "telefono", "label": "'.tr('Cellulare').'", "name": "referente_cellulare", "id": "referente_cellulare_add", "class": "text-center" ]}
</div>
</div>
<div class="row" style="margin-top:10px;">
<div class="col-md-6">
{[ "type": "email", "label": "'.tr('E-mail').'", "name": "referente_email", "id": "referente_email_add", "placeholder":"referente@dominio.ext" ]}
</div>
<div class="col-md-6">
{[ "type": "select", "label": "'.tr('Mansione').'", "name": "referente_mansione", "id": "referente_mansione_add", "values": "query=SELECT `id` AS id, `nome` AS descrizione FROM `an_mansioni` ORDER BY `nome`", "include_blank": true ]}
</div>
</div>
</div>
</div>';
//fine mod maulapi

echo '
<!-- PULSANTI -->
<div class="modal-footer">
Expand Down
18 changes: 9 additions & 9 deletions modules/anagrafiche/src/Referente.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@

use Common\SimpleModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Referente extends Model
{
use SimpleModelTrait;
use SoftDeletes;

protected $table = 'an_referenti';

/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = [];

/**
* Crea un nuovo referente.
*
* @param string $nome
* @param int|null $id (id anagrafica)
* @param string|null $nome
* @param int|null $id_mansione
* @param int|null $id_sede
*
* @return self
*/
Expand All @@ -48,11 +48,11 @@ public static function build($id = null, $nome = null, $id_mansione = null, $id_
$model = new static();

$model->id_anagrafica = $id;

$model->nome = $nome;

$model->id_mansione = $id_mansione;
$model->id_sede = $id_sede;

// Se non è fornita id_sede, impostiamo 0 (sede legale / default)
$model->id_sede = $id_sede ?? 0;

$model->save();

Expand Down
11 changes: 11 additions & 0 deletions update/2_12.sql
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,14 @@ INSERT INTO `zz_settings` (`nome`, `valore`, `tipo`, `editable`, `sezione`, `ord
INSERT INTO `zz_settings_lang` (`id_lang`, `id_record`, `title`, `help`) VALUES
(1, (SELECT `id` FROM `zz_settings` WHERE `nome` = 'Tipologia anagrafica predefinita'), 'Tipologia anagrafica predefinita', 'Tipologia (Azienda, Ente pubblico o Privato) preselezionata automaticamente nella finestra di aggiunta di una nuova anagrafica. Se non impostata, nessuna tipologia viene preselezionata.'),
(2, (SELECT `id` FROM `zz_settings` WHERE `nome` = 'Tipologia anagrafica predefinita'), 'Default entity classification', 'Classification (Company, Public entity or Private) automatically preselected in the new entity creation window. If not set, no classification is preselected.');

-- Aggiunge il supporto a soft-delete per i referenti
-- Aggiunge la colonna deleted_at e un indice, quindi propaga lo stato deleted_at esistente dall'anagrafica ai referenti

ALTER TABLE `an_referenti` ADD COLUMN `deleted_at` DATETIME NULL AFTER `updated_at`;

CREATE INDEX `idx_an_referenti_deleted_at` ON `an_referenti` (`deleted_at`);

-- Se ci sono anagrafiche già soft-deleted, sincronizza lo stato sui referenti esistenti
UPDATE `an_referenti` r JOIN `an_anagrafiche` a ON r.`id_anagrafica` = a.`id` SET r.`deleted_at` = a.`deleted_at` WHERE a.`deleted_at` IS NOT NULL AND (r.`deleted_at` IS NULL OR r.`deleted_at` = '');

Loading