+ function handler_adm_transfers(&$page, $action = null, $id = null) {
+ // list/log all bank transfers and link them to individual transactions
+
+ if (Post::has('generate')) {
+ $recon_ids = array_keys(Post::v('recon_id'));
+
+ // generate a new reconcilation group ID
+ $res = XDB::query("SELECT MAX(recongroup_id)+1 FROM payment_reconcilations");
+ $recongp_id = $res->fetchOneCell();
+ if ($recongp_id == null) $recongp_id = 1;
+
+ // add reconcilations to group
+ // FIXME: should check if reconcilations are in good status
+ XDB::execute("UPDATE payment_reconcilations SET recongroup_id={?}, status='closed'
+ WHERE id IN {?}",
+ $recongp_id, $recon_ids);
+
+ // create transfers
+ XDB::execute("INSERT INTO payment_transfers
+ SELECT NULL, {?}, t.ref, SUM(t.amount+t.commission), NULL, p.text, NULL
+ FROM payment_transactions AS t
+ LEFT JOIN payments AS p ON t.ref = p.id
+ LEFT JOIN groups AS g ON p.asso_id = g.id
+ WHERE t.recon_id IN {?}
+ GROUP BY t.ref",
+ $recongp_id, $recon_ids);
+
+ //$res = XDB::query("SELECT * FROM payment_reconcilations WHERE id IN {?}", $recon_ids);
+ //$recons = $res->fetchAllAssoc();
+
+ $page->trigSuccess("Les virements ont été générés pour ".count($recon_ids)." réconciliations.");
+ $this->handler_adm_reconcile($page);
+
+ } elseif ($action == "delgroup") {
+ S::assert_xsrf_token();
+ XDB::execute("UPDATE payment_reconcilations SET status='transfering', recongroup_id=NULL WHERE recongroup_id={?}", $id);
+ XDB::execute("DELETE FROM payment_transfers WHERE recongroup_id={?} AND date IS NULL", $id);
+
+ $page->trigSuccess("Les virements non réalisés ont été supprimé du groupe ".$id.".");
+ $this->handler_adm_reconcile($page);
+
+ } elseif ($action == "confirm") {
+ S::assert_xsrf_token();
+ XDB::execute("UPDATE payment_transfers SET date=NOW() WHERE id={?}", $id);
+
+ $page->trigSuccess("Virement ".$id." confirmé.");
+ $this->handler_adm_reconcile($page);
+
+ } else {
+ pl_redirect("admin/reconcile");
+ }