|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione |
| 5 | + * Copyright (C) DevCode s.r.l. |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +namespace Modules\PrimaNota; |
| 22 | + |
| 23 | +use Tasks\Manager; |
| 24 | + |
| 25 | +/** |
| 26 | + * Task dedicato alla registrazione automatica dei pagamenti alla scadenza delle fatture. |
| 27 | + */ |
| 28 | +class PagamentoAutomaticoTask extends Manager |
| 29 | +{ |
| 30 | + public function needsExecution() |
| 31 | + { |
| 32 | + // Il task viene eseguito sempre dallo scheduler |
| 33 | + return true; |
| 34 | + } |
| 35 | + |
| 36 | + public function execute() |
| 37 | + { |
| 38 | + $result = [ |
| 39 | + 'response' => 1, |
| 40 | + 'message' => tr('Pagamenti registrati correttamente!'), |
| 41 | + ]; |
| 42 | + |
| 43 | + $database = database(); |
| 44 | + |
| 45 | + // Recupero le scadenze scadute o in scadenza oggi con flag registra_pagamento attivo |
| 46 | + $scadenze = $database->fetchArray(" |
| 47 | + SELECT |
| 48 | + `co_scadenziario`.*, |
| 49 | + `co_documenti`.`idanagrafica`, |
| 50 | + `co_documenti`.`idtipodocumento`, |
| 51 | + `co_tipidocumento`.`dir`, |
| 52 | + `co_pagamenti`.`idconto_vendite`, |
| 53 | + `co_pagamenti`.`idconto_acquisti` |
| 54 | + FROM |
| 55 | + `co_scadenziario` |
| 56 | + INNER JOIN `co_documenti` ON `co_scadenziario`.`iddocumento` = `co_documenti`.`id` |
| 57 | + INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento` = `co_tipidocumento`.`id` |
| 58 | + INNER JOIN `co_pagamenti` ON `co_documenti`.`idpagamento` = `co_pagamenti`.`id` |
| 59 | + WHERE |
| 60 | + `co_pagamenti`.`registra_pagamento_automatico` = 1 |
| 61 | + AND ABS(`co_scadenziario`.`pagato`) < ABS(`co_scadenziario`.`da_pagare`) |
| 62 | + AND ( |
| 63 | + IF(`co_scadenziario`.`data_concordata`, `co_scadenziario`.`data_concordata`, `co_scadenziario`.`scadenza`) <= CURDATE() |
| 64 | + ) |
| 65 | + "); |
| 66 | + |
| 67 | + $conta_registrati = 0; |
| 68 | + $conta_errori = 0; |
| 69 | + |
| 70 | + foreach ($scadenze as $scadenza) { |
| 71 | + try { |
| 72 | + // Calcolo l'importo da registrare |
| 73 | + $importo_da_pagare = abs($scadenza['da_pagare']); |
| 74 | + $importo_gia_pagato = abs($scadenza['pagato']); |
| 75 | + $importo_da_registrare = $importo_da_pagare - $importo_gia_pagato; |
| 76 | + |
| 77 | + if ($importo_da_registrare <= 0) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + // Determino il conto cliente/fornitore |
| 82 | + $id_conto_anagrafica = null; |
| 83 | + if ($scadenza['dir'] == 'entrata') { |
| 84 | + // Fattura di vendita: conto cliente |
| 85 | + $id_conto_anagrafica = $database->selectOne('an_anagrafiche', 'idconto_cliente', ['idanagrafica' => $scadenza['idanagrafica']])['idconto_cliente']; |
| 86 | + } else { |
| 87 | + // Fattura di acquisto: conto fornitore |
| 88 | + $id_conto_anagrafica = $database->selectOne('an_anagrafiche', 'idconto_fornitore', ['idanagrafica' => $scadenza['idanagrafica']])['idconto_fornitore']; |
| 89 | + } |
| 90 | + |
| 91 | + // Determino il conto di contropartita |
| 92 | + $id_conto_contropartita = null; |
| 93 | + if ($scadenza['dir'] == 'entrata') { |
| 94 | + $id_conto_contropartita = $scadenza['idconto_vendite']; |
| 95 | + } else { |
| 96 | + $id_conto_contropartita = $scadenza['idconto_acquisti']; |
| 97 | + } |
| 98 | + |
| 99 | + // Verifica che i conti siano valorizzati |
| 100 | + if (empty($id_conto_anagrafica)) { |
| 101 | + $conta_errori++; |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + if (empty($id_conto_contropartita)) { |
| 106 | + $conta_errori++; |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + // Crea il movimento di prima nota |
| 111 | + Movimento::registraPagamentoAutomatico( |
| 112 | + $scadenza['id'], |
| 113 | + $importo_da_registrare, |
| 114 | + $id_conto_anagrafica, |
| 115 | + $id_conto_contropartita, |
| 116 | + $scadenza['dir'] |
| 117 | + ); |
| 118 | + |
| 119 | + $conta_registrati++; |
| 120 | + } catch (\Exception $e) { |
| 121 | + $conta_errori++; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if ($conta_registrati == 0 && $conta_errori == 0) { |
| 126 | + $result = [ |
| 127 | + 'response' => 1, |
| 128 | + 'message' => tr('Nessun pagamento da registrare'), |
| 129 | + ]; |
| 130 | + } elseif ($conta_errori > 0) { |
| 131 | + $result = [ |
| 132 | + 'response' => 2, |
| 133 | + 'message' => tr('Pagamenti registrati: _REGISTRATI_, Errori: _ERRORI_', [ |
| 134 | + '_REGISTRATI_' => $conta_registrati, |
| 135 | + '_ERRORI_' => $conta_errori, |
| 136 | + ]), |
| 137 | + ]; |
| 138 | + } else { |
| 139 | + $result = [ |
| 140 | + 'response' => 1, |
| 141 | + 'message' => tr('_NUM_ pagamenti registrati correttamente!', [ |
| 142 | + '_NUM_' => $conta_registrati, |
| 143 | + ]), |
| 144 | + ]; |
| 145 | + } |
| 146 | + |
| 147 | + return $result; |
| 148 | + } |
| 149 | +} |
0 commit comments