Skip to content

Commit 6a8598d

Browse files
feat: Gestione spese d'incasso in fattura
1 parent 042deff commit 6a8598d

6 files changed

Lines changed: 108 additions & 6 deletions

File tree

modules/fatture/actions.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,9 @@
714714
$riga = null;
715715
}
716716

717+
// Ricalcolo inps, ritenuta e bollo
718+
ricalcola_costiagg_fattura($id_record);
719+
717720
flash()->info(tr('Righe duplicate!'));
718721

719722
break;
@@ -1064,6 +1067,9 @@
10641067

10651068
flash()->info(tr('Nuovo articolo aggiunto!'));
10661069
}
1070+
1071+
// Ricalcolo inps, ritenuta e bollo
1072+
ricalcola_costiagg_fattura($id_record);
10671073
} else {
10681074
$response['error'] = tr('Nessun articolo corrispondente a magazzino');
10691075
echo json_encode($response);
@@ -1113,6 +1119,9 @@
11131119
}
11141120
}
11151121

1122+
// Ricalcolo inps, ritenuta e bollo
1123+
ricalcola_costiagg_fattura($id_record);
1124+
11161125
if ($numero_totale > 1) {
11171126
flash()->info(tr('_NUM_ prezzi modificati!', [
11181127
'_NUM_' => $numero_totale,
@@ -1169,6 +1178,9 @@
11691178
++$numero_totale;
11701179
}
11711180

1181+
// Ricalcolo inps, ritenuta e bollo
1182+
ricalcola_costiagg_fattura($id_record);
1183+
11721184
if ($numero_totale > 1) {
11731185
flash()->info(tr('_NUM_ prezzi modificati!', [
11741186
'_NUM_' => $numero_totale,
@@ -1203,6 +1215,9 @@
12031215
flash()->info(tr('Riga aggiornata!'));
12041216
}
12051217

1218+
// Ricalcolo inps, ritenuta e bollo
1219+
ricalcola_costiagg_fattura($id_record);
1220+
12061221
break;
12071222

12081223
case 'cambia_stato':

modules/fatture/row-list.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
$mancanti = 0;
6767
$delete = 'delete_riga';
6868

69+
$row_disable = in_array($riga->id, [$fattura->rigaBollo->id, $fattura->id_riga_spese_incasso]);
70+
6971
// Individuazione dei seriali
7072
if ($riga->isArticolo() && !empty($riga->abilita_serial)) {
7173
$serials = $riga->serials;
@@ -115,7 +117,7 @@
115117
echo '
116118
<tr data-id="'.$riga->id.'" data-type="'.$riga::class.'" '.$extra.'>
117119
<td class="text-center">';
118-
if (!$block_edit) {
120+
if (!$block_edit && !$row_disable) {
119121
echo '
120122
<input class="check" type="checkbox"/>';
121123
}
@@ -220,7 +222,7 @@
220222
// Quantità e unità di misura
221223
echo '
222224
<td>
223-
{[ "type": "number", "name": "qta_'.$riga->id.'", "value": "'.$riga->qta.'", "min-value": "0", "onchange": "aggiornaInline($(this).closest(\'tr\').data(\'id\'))", "disabled": "'.($riga->isSconto() ? 1 : 0).'", "disabled": "'.($block_edit || $riga->isSconto()).'", "decimals": "qta" ]}
225+
{[ "type": "number", "name": "qta_'.$riga->id.'", "value": "'.$riga->qta.'", "min-value": "0", "onchange": "aggiornaInline($(this).closest(\'tr\').data(\'id\'))", "disabled": "'.($riga->isSconto() ? 1 : 0).'", "disabled": "'.($block_edit || $riga->isSconto() || $row_disable).'", "decimals": "qta" ]}
224226
</td>';
225227

226228
if ($riga->isArticolo()) {
@@ -236,7 +238,7 @@
236238
} else {
237239
echo '
238240
<td>
239-
{[ "type": "number", "name": "costo_'.$riga->id.'", "value": "'.$riga->costo_unitario.'", "onchange": "aggiornaInline($(this).closest(\'tr\').data(\'id\'))", "icon-after": "'.currency().'", "disabled": "'.$block_edit.'" ]}
241+
{[ "type": "number", "name": "costo_'.$riga->id.'", "value": "'.$riga->costo_unitario.'", "onchange": "aggiornaInline($(this).closest(\'tr\').data(\'id\'))", "icon-after": "'.currency().'", "disabled": "'.($block_edit || $row_disable).'" ]}
240242
</td>';
241243
}
242244
}
@@ -249,7 +251,7 @@
249251
echo '
250252
<td class="text-center">
251253
'.($show_notifica['show_notifica_prezzo'] ? '<i class="fa fa-info-circle notifica-prezzi"></i>' : '').'
252-
{[ "type": "number", "name": "prezzo_'.$riga->id.'", "value": "'.$riga->prezzo_unitario_corrente.'", "onchange": "aggiornaInline($(this).closest(\'tr\').data(\'id\'))", "icon-before": "'.(abs($riga->provvigione_unitaria) > 0 ? '<span class=\'tip text-info\' title=\''.provvigioneInfo($riga).'\'><small><i class=\'fa fa-handshake-o\'></i></small></span>' : '').'", "icon-after": "'.currency().'", "disabled": "'.$block_edit.'" ]}';
254+
{[ "type": "number", "name": "prezzo_'.$riga->id.'", "value": "'.$riga->prezzo_unitario_corrente.'", "onchange": "aggiornaInline($(this).closest(\'tr\').data(\'id\'))", "icon-before": "'.(abs($riga->provvigione_unitaria) > 0 ? '<span class=\'tip text-info\' title=\''.provvigioneInfo($riga).'\'><small><i class=\'fa fa-handshake-o\'></i></small></span>' : '').'", "icon-after": "'.currency().'", "disabled": "'.($block_edit || $row_disable).'" ]}';
253255

254256
// Prezzo inferiore al minimo consigliato
255257
if ($riga->isArticolo()) {
@@ -303,7 +305,7 @@
303305
}
304306

305307
if ($record['stato'] != 'Pagato' && $record['stato'] != 'Emessa') {
306-
if ($riga->id != $fattura->rigaBollo->id) {
308+
if (!$row_disable) {
307309
echo '
308310
<a class="btn btn-xs btn-info" title="'.tr('Aggiungi informazioni FE per questa riga').'" onclick="apriInformazioniFE(this)">
309311
<i class="fa fa-file-code-o"></i>

modules/fatture/src/Fattura.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,12 @@ public function rigaBollo()
441441
return $this->hasOne(Components\Riga::class, 'iddocumento')->where('id', $this->id_riga_bollo);
442442
}
443443

444+
public function rigaSpeseIncasso()
445+
{
446+
return $this->hasOne(Components\Riga::class, 'iddocumento')->where('id', $this->id_riga_spese_incasso);
447+
}
448+
449+
444450
public function scadenze()
445451
{
446452
return $this->hasMany(Scadenza::class, 'iddocumento')->orderBy('scadenza');
@@ -585,6 +591,7 @@ public function save(array $options = [])
585591

586592
// Fix dei campi statici
587593
$this->id_riga_bollo = $this->gestoreBollo->manageRigaMarcaDaBollo();
594+
$this->id_riga_spese_incasso = $this->manageRigaSpeseIncasso();
588595

589596
// Generazione numero fattura se non presente (Bozza -> Emessa)
590597
if ((($id_stato_precedente == $id_stato_bozza && $id_stato_attuale == $id_stato_emessa) or (!$is_fiscale)) && empty($this->numero_esterno)) {
@@ -723,6 +730,46 @@ public function replicate(?array $except = null)
723730
return $new;
724731
}
725732

733+
public function manageRigaSpeseIncasso() {
734+
$riga = $this->rigaSpeseIncasso;
735+
$first_riga_fattura = $this->getRighe()->where('id', '!=', $riga->id)->where('is_descrizione', '0')->first();
736+
737+
// Elimino la riga se non c'è più la descrizione dell'incasso o se la fattura non ha righe
738+
if (!$this->pagamento->descrizione_incasso || !$first_riga_fattura) {
739+
if (!empty($riga)) {
740+
$riga->delete();
741+
}
742+
743+
return null;
744+
}
745+
746+
// Creazione riga se non presente
747+
if (empty($riga)) {
748+
$riga = Components\Riga::build($this);
749+
}
750+
751+
$prezzo_unitario = $this->pagamento->importo_fisso_incasso;
752+
if ($this->pagamento->importo_percentuale_incasso && ($this->totale - $riga->totale)) {
753+
$prezzo_unitario += ($this->totale - $riga->totale) * $this->pagamento->importo_percentuale_incasso / 100;
754+
}
755+
756+
if ($riga->tipo_sconto == 'PRC') {
757+
$sconto = $riga->sconto_percentuale ?: 0;
758+
} else {
759+
$sconto = $riga->sconto_unitario;
760+
}
761+
762+
$riga->qta = 1;
763+
$riga->descrizione = $this->pagamento->descrizione_incasso;
764+
$riga->id_iva = $first_riga_fattura->idiva;
765+
$riga->idconto = setting("Conto predefinito per le spese d'incasso");
766+
$riga->setPrezzoUnitario($prezzo_unitario, $first_riga_fattura->idiva);
767+
$riga->setSconto($sconto, $riga->tipo_sconto);
768+
$riga->save();
769+
770+
return $riga->id;
771+
}
772+
726773
/**
727774
* Restituisce l'elenco delle note di credito collegate.
728775
*

modules/pagamenti/actions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
$pagamento->idconto_vendite = post('idconto_vendite') ?: null;
6161
$pagamento->idconto_acquisti = post('idconto_acquisti') ?: null;
6262
$pagamento->setTranslation('title', $descrizione);
63+
$pagamento->descrizione_incasso = post('descrizione_incasso');
64+
$pagamento->importo_fisso_incasso = post('importo_fisso_incasso');
65+
$pagamento->importo_percentuale_incasso = post('importo_percentuale_incasso');
6366
$pagamento->save();
6467
}
6568

modules/pagamenti/edit.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@
5252
</div>
5353
</div>
5454

55+
<div class="card card-primary">
56+
<div class="card-header">
57+
<h3 class="card-title"><?php echo tr('Spese di incasso in fattura'); ?></h3>
58+
</div>
59+
60+
<div class="card-body">
61+
<div class="row">
62+
<div class="col-md-6">
63+
{[ "type": "text", "label": "<?php echo tr('Descrizione riga'); ?>", "name": "descrizione_incasso", "value": "$descrizione_incasso$" ]}
64+
</div>
65+
66+
<div class="col-md-3">
67+
{[ "type": "number", "label": "<?php echo tr('Importo fisso'); ?>", "name": "importo_fisso_incasso", "value": "$importo_fisso_incasso$", "icon-after": "<?php echo currency(); ?>" ]}
68+
</div>
69+
70+
<div class="col-md-3">
71+
{[ "type": "number", "label": "<?php echo tr('Importo percentuale'); ?>", "name": "importo_percentuale_incasso", "value": "$importo_percentuale_incasso$", "icon-after": "%" ]}
72+
</div>
73+
</div>
74+
</div>
75+
</div>
76+
5577
<div class="card card-primary">
5678
<div class="card-header">
5779
<h3 class="card-title"><?php echo tr('Rate'); ?></h3>

update/2_7.sql

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,17 @@ INSERT INTO `zz_views_lang` (`id_lang`, `id_record`, `title`) VALUES
121121
(1, (SELECT `id` FROM `zz_views` WHERE `name` = 'Da rinnovare' AND `id_module` = @id_module), 'Da rinnovare'),
122122
(2, (SELECT `id` FROM `zz_views` WHERE `name` = 'Da rinnovare' AND `id_module` = @id_module), 'Renewable');
123123

124-
UPDATE `zz_views` SET `query` = "IF(`co_contratti`.`rinnovabile`=1, 'Sì', 'No')" WHERE `zz_views`.`name` = "Rinnovabile" AND `id_module` = (SELECT `id` FROM `zz_modules` WHERE `name` = "Contratti");
124+
UPDATE `zz_views` SET `query` = "IF(`co_contratti`.`rinnovabile`=1, 'Sì', 'No')" WHERE `zz_views`.`name` = "Rinnovabile" AND `id_module` = (SELECT `id` FROM `zz_modules` WHERE `name` = "Contratti");
125+
126+
-- Gestione spese d'incasso
127+
ALTER TABLE `co_pagamenti` ADD `descrizione_incasso` TEXT NOT NULL, ADD `importo_fisso_incasso` DECIMAL(15,6) NOT NULL, ADD `importo_percentuale_incasso` DECIMAL(15,6) NOT NULL;
128+
129+
ALTER TABLE `co_documenti` ADD `id_riga_spese_incasso` INT NOT NULL;
130+
131+
INSERT INTO `zz_settings` (`nome`, `valore`, `tipo`, `editable`, `sezione`, `order`, `is_user_setting`) VALUES
132+
("Conto predefinito per le spese d'incasso", (SELECT `id` FROM `co_pianodeiconti3` WHERE `descrizione`='Ricavi vari'), "query=SELECT id, descrizione FROM co_pianodeiconti3 WHERE idpianodeiconti2=(SELECT id FROM co_pianodeiconti2 WHERE descrizione='Ricavi')", "1", "Fatturazione", NULL, 0);
133+
134+
SELECT @id_record := `id` FROM `zz_settings` WHERE `nome` = "Conto predefinito per le spese d'incasso";
135+
INSERT INTO `zz_settings_lang` (`id_lang`, `id_record`, `title`, `help`) VALUES
136+
('1', @id_record, "Conto predefinito per le spese d'incasso", ''),
137+
('2', @id_record, 'Default account for collection costs', '');

0 commit comments

Comments
 (0)