Skip to content
Open
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
89 changes: 89 additions & 0 deletions modules/contratti_fornitori/actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

include_once __DIR__.'/../../core.php';
require_once __DIR__.'/src/ContrattoFornitoreService.php';

use Modules\ContrattiFornitori\ContrattoFornitoreService;

$service = new ContrattoFornitoreService($dbo, (int) $id_module);

try {
switch (post('op')) {
case 'add':
$id_record = $service->create([
'id_segment' => post('id_segment'),
'nome' => post('nome'),
'id_fornitore' => post('id_fornitore'),
'id_categoria' => post('id_categoria'),
'data_inizio' => post('data_inizio'),
'validita' => post('validita'),
'tipo_validita' => post('tipo_validita'),
]);

flash()->info(tr('Contratto fornitore aggiunto.'));
break;

case 'update':
$service->update((int) $id_record, [
'numero' => post('numero'),
'nome' => post('nome'),
'id_fornitore' => post('id_fornitore'),
'id_referente' => post('id_referente'),
'idagente' => post('idagente'),
'id_stato' => post('id_stato'),
'id_categoria' => post('id_categoria'),
'numero_fornitore' => post('numero_fornitore'),
'data_stipula' => post('data_stipula'),
'data_inizio' => post('data_inizio'),
'validita' => post('validita'),
'tipo_validita' => post('tipo_validita'),
'data_scadenza' => post('data_scadenza'),
'giorni_preavviso' => post('giorni_preavviso'),
'rinnovo_automatico' => post('rinnovo_automatico'),
'mesi_rinnovo' => post('mesi_rinnovo'),
'condizioni_rinnovo' => post('condizioni_rinnovo'),
'importo' => post('importo'),
'periodicita' => post('periodicita'),
'note_economiche' => post('note_economiche'),
'note' => post('note'),
], !empty(post('force_state_transition')));

flash()->info(tr('Contratto fornitore aggiornato.'));
break;

case 'copy':
$id_record = $service->duplicate((int) $id_record);
flash()->info(tr('Contratto duplicato correttamente.'));
break;

case 'renew':
$id_record = $service->renew((int) $id_record);
flash()->info(tr('Contratto rinnovato correttamente.'));
break;

case 'delete':
$service->deleteDraft((int) $id_record);
flash()->info(tr('Contratto fornitore eliminato.'));
break;
}
} catch (Throwable $e) {
flash()->error($e->getMessage());
}
67 changes: 67 additions & 0 deletions modules/contratti_fornitori/add.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

include_once __DIR__.'/../../core.php';

$id_segment = getSegmentPredefined($id_module);

if (empty($id_segment)) {
echo '<div class="alert alert-warning">'.tr('Non è configurato un sezionale predefinito per Contratti fornitori. Configurarlo dal modulo Segmenti prima di creare il contratto.').'</div>';

return;
}

echo '
<form action="" method="post" id="add-form">
<input type="hidden" name="op" value="add">
<input type="hidden" name="backto" value="record-edit">
<input type="hidden" name="id_record" value="0">
<input type="hidden" name="id_segment" value="'.$id_segment.'">

<div class="row">
<div class="col-md-6">
{[ "type": "text", "label": "'.tr('Descrizione contratto').'", "name": "nome", "required": 1 ]}
</div>

<div class="col-md-6">
{[ "type": "select", "label": "'.tr('Fornitore').'", "name": "id_fornitore", "required": 1, "ajax-source": "fornitori" ]}
</div>
</div>

<div class="row">
<div class="col-md-4">
{[ "type": "select", "label": "'.tr('Categoria').'", "name": "id_categoria", "values": "query=SELECT `id`, `nome` AS descrizione FROM `ac_categorie_contratti_fornitori` WHERE `enabled` = 1 ORDER BY `nome`" ]}
</div>

<div class="col-md-4">
{[ "type": "date", "label": "'.tr('Data inizio').'", "name": "data_inizio", "value": "'.date('Y-m-d').'" ]}
</div>

<div class="col-md-4">
{[ "type": "number", "label": "'.tr('Validità contratto').'", "name": "validita", "decimals": "0", "min-value": "0", "icon-after": "choice|period|manual", "help": "'.tr('Il campo Validità contratto viene utilizzato per il calcolo della Data di scadenza del contratto.').'" ]}
</div>
</div>

<div class="modal-footer">
<button type="submit" class="btn btn-primary">
<i class="fa fa-plus"></i> '.tr('Aggiungi').'
</button>
</div>
</form>';
197 changes: 197 additions & 0 deletions modules/contratti_fornitori/bulk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
<?php

/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

include_once __DIR__.'/../../core.php';

require_once __DIR__.'/src/ContrattoFornitoreService.php';

use Modules\ContrattiFornitori\ContrattoFornitoreService;

$service = new ContrattoFornitoreService($dbo, (int) $id_module);
$operations = [];

function contrattiFornitoriBulkErrors(array $errors): string
{
$total = count($errors);
$visible = array_slice($errors, 0, 5);
$message = implode(' | ', $visible);

if ($total > count($visible)) {
$message .= ' | '.tr('altri _NUM_ errori', [
'_NUM_' => $total - count($visible),
]);
}

return $message;
}

$selectedCount = count($id_records ?? []);

if (!empty(post('op')) && $selectedCount > ContrattoFornitoreService::MAX_BULK_RECORDS) {
flash()->error(tr('È possibile elaborare al massimo _MAX_ contratti per volta.', [
'_MAX_' => ContrattoFornitoreService::MAX_BULK_RECORDS,
]));

return [];
}

switch (post('op')) {
case 'change_status':
$updated = 0;
$errors = [];

foreach ($id_records as $id) {
try {
$service->changeState((int) $id, (int) post('id_stato'), !empty(post('force_state_transition')));
++$updated;
} catch (Throwable $e) {
$errors[] = '#'.(int) $id.': '.$e->getMessage();
}
}

if ($updated > 0) {
flash()->info(tr('Stato aggiornato per _NUM_ contratti fornitori.', ['_NUM_' => $updated]));
}

if (!empty($errors)) {
flash()->warning(tr('Alcuni contratti non sono stati aggiornati: _ERRORS_', [
'_ERRORS_' => contrattiFornitoriBulkErrors($errors),
]));
}
break;

case 'copy_bulk':
$copied = 0;
$errors = [];

foreach ($id_records as $id) {
try {
$service->duplicate((int) $id);
++$copied;
} catch (Throwable $e) {
$errors[] = '#'.(int) $id.': '.$e->getMessage();
}
}

if ($copied > 0) {
flash()->info(tr('_NUM_ contratti fornitori duplicati.', ['_NUM_' => $copied]));
}

if (!empty($errors)) {
flash()->warning(tr('Alcuni contratti non sono stati duplicati: _ERRORS_', [
'_ERRORS_' => contrattiFornitoriBulkErrors($errors),
]));
}
break;

case 'renew_bulk':
$renewed = 0;
$errors = [];

foreach ($id_records as $id) {
try {
$service->renew((int) $id);
++$renewed;
} catch (Throwable $e) {
$errors[] = '#'.(int) $id.': '.$e->getMessage();
}
}

if ($renewed > 0) {
flash()->info(tr('_NUM_ contratti fornitori rinnovati.', ['_NUM_' => $renewed]));
}

if (!empty($errors)) {
flash()->warning(tr('Alcuni contratti non sono stati rinnovati: _ERRORS_', [
'_ERRORS_' => contrattiFornitoriBulkErrors($errors),
]));
}
break;

case 'delete_drafts_bulk':
$deleted = 0;
$errors = [];

foreach ($id_records as $id) {
try {
$service->deleteDraft((int) $id);
++$deleted;
} catch (Throwable $e) {
$errors[] = '#'.(int) $id.': '.$e->getMessage();
}
}

if ($deleted > 0) {
flash()->info(tr('_NUM_ bozze eliminate.', ['_NUM_' => $deleted]));
}

if (!empty($errors)) {
flash()->warning(tr('Alcune bozze non sono state eliminate: _ERRORS_', [
'_ERRORS_' => contrattiFornitoriBulkErrors($errors),
]));
}
break;
}

$operations['change_status'] = [
'text' => '<span><i class="fa fa-refresh"></i> '.tr('Cambia stato').'</span>',
'data' => [
'title' => tr('Aggiornare lo stato dei contratti selezionati?'),
'msg' => '<div class="alert alert-warning"><i class="fa fa-exclamation-triangle"></i> '.tr('Alcuni passaggi di stato possono riaprire o modificare contratti già attivi o conclusi. Verificare attentamente i record selezionati.').'</div><br>{[ "type": "select", "label": "'.tr('Stato').'", "name": "id_stato", "required": 1, "values": "query=SELECT `id`, `nome` AS descrizione, `colore` AS _bgcolor_ FROM `ac_stati_contratti_fornitori` WHERE `enabled` = 1 AND `nome` != \"In scadenza\" ORDER BY `ordine`, `nome`" ]}<br>{[ "type": "checkbox", "label": "'.tr('Confermo anche eventuali transizioni di stato non standard').'", "name": "force_state_transition", "value": "1" ]}',
'button' => tr('Procedi'),
'class' => 'btn btn-lg btn-warning',
'blank' => false,
],
];

$operations['copy_bulk'] = [
'text' => '<span><i class="fa fa-copy"></i> '.tr('Duplica contratti').'</span>',
'data' => [
'title' => tr('Duplicare i contratti selezionati?'),
'msg' => tr('Ogni copia sarà creata in stato Bozza con una nuova numerazione. Gli allegati non saranno copiati.'),
'button' => tr('Procedi'),
'class' => 'btn btn-lg btn-warning',
'blank' => false,
],
];

$operations['renew_bulk'] = [
'text' => '<span><i class="fa fa-repeat"></i> '.tr('Rinnova contratti').'</span>',
'data' => [
'title' => tr('Rinnovare i contratti selezionati?'),
'msg' => tr('Saranno rinnovati soltanto i contratti Attivi, non già rinnovati e con una data di scadenza. Gli allegati non saranno copiati.'),
'button' => tr('Procedi'),
'class' => 'btn btn-lg btn-warning',
'blank' => false,
],
];

$operations['delete_drafts_bulk'] = [
'text' => '<span class="text-danger"><i class="fa fa-trash"></i> '.tr('Elimina bozze').'</span>',
'data' => [
'title' => tr('Eliminare le bozze selezionate?'),
'msg' => tr('Saranno eliminati soltanto i contratti in stato Bozza senza allegati.'),
'button' => tr('Elimina'),
'class' => 'btn btn-lg btn-danger',
'blank' => false,
],
];

return $operations;
28 changes: 28 additions & 0 deletions modules/contratti_fornitori/buttons.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/

include_once __DIR__.'/../../core.php';
require_once __DIR__.'/src/ContrattoFornitoreService.php';

use Modules\ContrattiFornitori\ContrattoFornitoreService;

if (!empty($id_record)) {
$manualWithoutDuration = ($record['tipo_validita'] ?? null) === 'manual' && empty($record['mesi_rinnovo']);
$isActive = ($record['stato_nome'] ?? null) === ContrattoFornitoreService::STATO_ATTIVO;
$canRenew = $isActive && !empty($record['data_scadenza']) && empty($record['id_contratto_successivo']) && !$manualWithoutDuration;

echo '<div class="tip d-inline-block" data-widget="tooltip" title="'.tr('Il rinnovo è disponibile solo per contratti Attivi, con data di scadenza, senza un contratto successivo e, per le scadenze manuali, con una durata di rinnovo.').'">';
echo '<button type="button" class="btn btn-warning ask '.($canRenew ? '' : 'disabled').'" data-title="'.tr('Rinnovare questo contratto?').'" data-msg="'.tr('Verrà creato un nuovo contratto in stato Bozza, collegato a quello corrente.').'" data-op="renew" data-button="'.tr('Rinnova').'" data-class="btn btn-lg btn-warning" data-backto="record-edit"><i class="fa fa-refresh"></i> '.tr('Rinnova').'...</button>';
echo '</div> ';

echo '<button type="button" class="btn btn-primary ask" data-title="'.tr('Duplicare questo contratto?').'" data-msg="'.tr('Verrà creato un nuovo contratto in stato Bozza con un nuovo numero.').'" data-op="copy" data-button="'.tr('Duplica').'" data-class="btn btn-lg btn-primary" data-backto="record-edit"><i class="fa fa-copy"></i> '.tr('Duplica contratto').'</button>';
}
Loading