<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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.
//
// Moodle 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 Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Simulatore contabilità
 *
 * @package    mod_mtcontabilita
 * @copyright  2026 Mediatouch 2000 Srl <https://www.mediatouch.it>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @author     Marco Cappuccio <m.cappuccio@mediatouch.it>
 */

require_once('../../../config.php');
require_login();

$attemptid = required_param('attemptid', PARAM_INT);
$mtcontabilitaid = required_param('mtcontabilitaid', PARAM_INT);
$itemnum = required_param('itemnum', PARAM_RAW);
$listid = required_param('listid', PARAM_INT);
$sort = required_param('sort', PARAM_INT);

$params = [
    'attemptid' => $attemptid,
    'mtcontabilitaid' => $mtcontabilitaid,
    'itemnum' => 0,
    'listid' => $listid,
    'bitem' => 'i',
];

$sql = "SELECT ai.id, ai.da, d.amount
FROM {mtcontabilita_attempt_items} ai, {mtcontabilita_itemdet} d
where ai.itemdetid=d.id
AND ai.attemptid=:attemptid AND ai.mtcontabilitaid=:mtcontabilitaid
AND d.itemnum=:itemnum AND d.listid=:listid AND ai.bitem=:bitem";
$record = $DB->get_record_sql($sql, $params, IGNORE_MULTIPLE);
header('Content-Type: application/json');

$rit = [[ 'd' => 0, 'a' => 0, 'aid' => 0]];

if ($record) {
    // Situazione iniziale.
    $rit[0] = [
        'd' => $record->da == "d" ? floatval($record->amount) : 0,
        'a' => $record->da == "a" ? floatval($record->amount) : 0,
        'aid' => $record->id,
    ];
    // Scritture con parentid = situazione iniziale.
    $sql = "SELECT ai.id, ai.daamount, ai.itemnum FROM {mtcontabilita_attempt_items} ai
            WHERE ai.attemptid=:attemptid AND ai.mtcontabilitaid=:mtcontabilitaid
            AND ai.itemnum!=:itemnum AND ai.sort<:sort AND ai.bitem=:bitem AND ai.parentid=:parentid";
    $params = ['attemptid' => $attemptid, 'mtcontabilitaid' => $mtcontabilitaid, 'itemnum' => $itemnum, 'bitem' => 's', 'parentid' => $record->id, 'sort' => $sort];
    if ($parent = $DB->get_records_sql($sql, $params)) {
        foreach ($parent as $scrittura) {
            $rows = json_decode($scrittura->daamount);
            // Appena trovo x lo metto e non devo metterlo due volte.
            $x = false;
            foreach ($rows as $nnserver => $r) {
                // var_dump($key,$val); die();
                if ($r->d == "" && $r->a == "") {
                    continue;
                }
                $rit[] = [
                    'd' => $r->d,
                    'a' => $r->a,
                    'aid' => $scrittura->id,
                    'itemnum' => $scrittura->itemnum,
                ];
            }
        }

    }
}
echo json_encode($rit);
