openstamanager/modules/scadenzario/actions.php

122 lines
4.4 KiB
PHP
Raw Normal View History

<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* 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';
switch (post('op')) {
case 'add':
2019-05-04 00:32:28 +02:00
$data = post('data');
$tipo = post('tipo');
$da_pagare = post('da_pagare');
$descrizione = post('descrizione');
2021-03-08 11:23:20 +01:00
$dbo->query('INSERT INTO co_scadenziario(descrizione, tipo, data_emissione, scadenza, da_pagare, pagato) VALUES('.prepare($descrizione).', '.prepare($tipo).', CURDATE(), '.prepare($data).', '.prepare($da_pagare).", '0')");
$id_record = $dbo->lastInsertedID();
2019-05-04 00:32:28 +02:00
flash()->info(tr('Scadenza inserita!'));
break;
case 'update':
2019-07-25 18:05:47 +02:00
$tipo = post('tipo');
$descrizione = post('descrizione');
$iddocumento = post('iddocumento') ?: 0;
2019-09-28 11:25:20 +02:00
$totale_pagato = 0;
$id_scadenza_non_completa = null;
foreach (post('da_pagare') as $id => $da_pagare) {
$pagato = post('pagato')[$id];
$scadenza = post('scadenza')[$id];
$data_concordata = post('data_concordata')[$id];
$pagato = floatval($pagato);
$da_pagare = floatval($da_pagare);
2021-02-18 18:48:44 +01:00
if (!empty($iddocumento)) {
2021-02-04 17:25:46 +01:00
$id_tipo = $dbo->selectOne('co_documenti', 'idtipodocumento', ['id' => $iddocumento])['idtipodocumento'];
$tipo_documento = $dbo->selectOne('co_tipidocumento', '*', ['id' => $id_tipo]);
2021-02-18 18:48:44 +01:00
if ($tipo_documento['dir'] == 'uscita') {
if ($pagato > 0) {
2021-02-04 17:25:46 +01:00
$pagato = -$pagato;
}
2021-02-18 18:48:44 +01:00
if ($da_pagare > 0) {
2021-02-04 17:25:46 +01:00
$da_pagare = -$da_pagare;
}
2021-02-18 18:48:44 +01:00
} else {
if ($pagato < 0) {
2021-02-04 17:25:46 +01:00
$pagato = -$pagato;
}
2021-02-18 18:48:44 +01:00
if ($da_pagare < 0) {
2021-02-04 17:25:46 +01:00
$da_pagare = -$da_pagare;
}
}
2021-02-18 18:48:44 +01:00
if (!empty($tipo_documento['reversed'])) {
2021-02-04 17:25:46 +01:00
$pagato = -$pagato;
$da_pagare = -$da_pagare;
}
}
2021-02-18 18:48:44 +01:00
$totale_pagato = sum($totale_pagato, $pagato);
$id_scadenza = post('id_scadenza')[$id];
if (!empty($id_scadenza)) {
$database->update('co_scadenziario', [
'descrizione' => $descrizione,
'da_pagare' => $da_pagare,
'pagato' => $pagato,
'scadenza' => $scadenza,
'data_concordata' => $data_concordata,
2020-01-28 14:20:14 +01:00
'note' => post('note'),
], ['id' => $id_scadenza]);
if ($da_pagare == 0) {
$database->delete('co_scadenziario', ['id' => $id]);
2019-09-28 11:25:20 +02:00
}
} else {
$database->insert('co_scadenziario', [
'descrizione' => $descrizione,
'tipo' => $tipo,
'iddocumento' => $iddocumento,
'da_pagare' => $da_pagare,
'pagato' => $pagato,
'scadenza' => $scadenza,
'data_concordata' => $data_concordata,
'data_emissione' => date('Y-m-d'),
2020-01-28 14:20:14 +01:00
'note' => post('note'),
]);
$id_scadenza = $database->lastInsertedID();
}
2019-09-28 11:25:20 +02:00
if ($pagato != $da_pagare) {
$id_scadenza_non_completa = $id_scadenza;
}
}
flash()->info(tr('Scadenze aggiornate!'));
2019-09-28 11:25:20 +02:00
break;
2019-05-04 00:32:28 +02:00
case 'delete':
$dbo->query("DELETE FROM co_scadenziario WHERE id='".$id_record."'");
flash()->info(tr('Scadenza eliminata!'));
break;
}