Index: src/include/lib/Garradin/Compta/Rapprochement.php ================================================================== --- src/include/lib/Garradin/Compta/Rapprochement.php +++ src/include/lib/Garradin/Compta/Rapprochement.php @@ -52,28 +52,50 @@ $solde_final = $solde; return $result; } - public function record($compte, $operations, $auteur) + public function record($compte, $journal, $cases, $auteur) { - if (!is_array($operations)) + if (!is_array($journal)) + { + throw new \UnexpectedValueException('$journal doit être un tableau.'); + } + + if (!is_array($cases) && empty($cases)) { - throw new \UnexpectedValueException('$operations doit être un tableau.'); + $cases = []; } $db = DB::getInstance(); $db->exec('BEGIN;'); + + // Synchro des trucs cochés $st = $db->prepare('INSERT OR REPLACE INTO compta_rapprochement (operation, auteur) VALUES (:operation, :auteur);'); $st->bindValue(':auteur', (int)$auteur, \SQLITE3_INTEGER); - foreach ($operations as $row) + foreach ($journal as $row) + { + if (!array_key_exists($row['id'], $cases)) + continue; + + $st->bindValue(':operation', (int)$row['id'], \SQLITE3_INTEGER); + $st->execute(); + } + + // Synchro des trucs NON cochés + $st = $db->prepare('DELETE FROM compta_rapprochement WHERE operation = :id;'); + + foreach ($journal as $row) { - $st->bindValue(':operation', (int)$row, \SQLITE3_INTEGER); + if (array_key_exists($row['id'], $cases)) + continue; + + $st->bindValue(':id', (int)$row['id'], \SQLITE3_INTEGER); $st->execute(); } $db->exec('END;'); return true; } } Index: src/templates/admin/compta/banques/rapprocher.tpl ================================================================== --- src/templates/admin/compta/banques/rapprocher.tpl +++ src/templates/admin/compta/banques/rapprocher.tpl @@ -86,10 +86,11 @@
+ {csrf_field key="compta_rapprocher_`$compte.id`"}
{include file="admin/_foot.tpl"} Index: src/www/admin/compta/banques/rapprocher.php ================================================================== --- src/www/admin/compta/banques/rapprocher.php +++ src/www/admin/compta/banques/rapprocher.php @@ -20,29 +20,11 @@ throw new UserException("Le compte demandé n'existe pas."); } $error = false; -if (Utils::post('save')) -{ - if (!Utils::CSRF_check('compta_rapprocher_' . $compte['id'])) - { - $error = 'Une erreur est survenue, merci de renvoyer le formulaire.'; - } - else - { - try - { - $rapprochement->record($compte['id'], Utils::post('rapprocher'), $user['id']); - Utils::redirect('/admin/compta/banques/rapprocher.php?id='); - } - catch (UserException $e) - { - $error = $e->getMessage(); - } - } -} +$solde_initial = $solde_final = 0; $debut = Utils::get('debut'); $fin = Utils::get('fin'); if ($debut && $fin) @@ -66,26 +48,47 @@ { $date = $exercice['fin']; $debut = date('Y-m-01', $date); $fin = date('Y-m-t', $date); } + +$journal = $rapprochement->getJournal($compte['id'], $debut, $fin, $solde_initial, $solde_final); + +// Enregistrement des cases cochées +if (Utils::post('save')) +{ + if (!Utils::CSRF_check('compta_rapprocher_' . $compte['id'])) + { + $error = 'Une erreur est survenue, merci de renvoyer le formulaire.'; + } + else + { + try + { + $rapprochement->record($compte['id'], $journal, Utils::post('rapprocher'), $user['id']); + Utils::redirect(Utils::getSelfURL()); + } + catch (UserException $e) + { + $error = $e->getMessage(); + } + } +} if (substr($debut, 0, 7) == substr($fin, 0, 7)) { $tpl->assign('prev', Utils::modifyDate($debut, '-1 month', true)); $tpl->assign('next', Utils::modifyDate($fin, '+1 month', true)); } -$solde_initial = $solde_final = 0; - $tpl->assign('compte', $compte); $tpl->assign('debut', $debut); $tpl->assign('fin', $fin); -$tpl->assign('journal', $rapprochement->getJournal($compte['id'], $debut, $fin, $solde_initial, $solde_final)); +$tpl->assign('journal', $journal); $tpl->assign('solde_initial', $solde_initial); $tpl->assign('solde_final', $solde_final); $tpl->assign('error', $error); $tpl->display('admin/compta/banques/rapprocher.tpl'); Index: src/www/admin/static/scripts/global.js ================================================================== --- src/www/admin/static/scripts/global.js +++ src/www/admin/static/scripts/global.js @@ -170,11 +170,16 @@ if (elm.checked) parent.className += ' checked'; else parent.className = parent.className.replace(/ checked/, ''); }; + + if (checkBoxes[j].checked) + { + checkBoxes[j].onchange({target: checkBoxes[j]}); + } } } }); } })();