<?php
if (!empty($packageHistoryRecordObj) && $packageHistoryRecordObj->status == 0) {
    print '<form method="POST" id="dailyclosingform" action="' . $url . '">' . "\n";
    print '<input type="hidden" name="token" value="' . newToken() . '">';
    print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
    print '<input type="hidden" name="action" value="list">';
    print '<input id="stornochange" type="hidden" name="stornochange" value="' . $stornochange . '">';
    print '<input type="hidden" name="sortfield" value="' . $sortfield . '">';
    print '<input type="hidden" name="sortorder" value="' . $sortorder . '">';
    print '<input type="hidden" name="page" value="' . $page . '">';

    $from = date('Y-m-d H:i:s', $packageHistoryRecordObj->date_creation);
    $to = date('Y-m-d H:i:s', dol_now());

    print '<div id="DCuserData" style="width: 50%; float:left;">';
    print '<table style="width:100%;">';
    print '<tr><td colspan="4" class="dailyClosingHeader">' . $name . '</td></tr>';
    print '<tr><td colspan="4" class="center" style="height:50px;">' . $from . ' - ' . $to . '</td></tr>';
    print '<tr><td colspan="4">' . $langs->trans('UserGroupName') . ':  <b>' . $helper->getUserGroupRefById($packageHistoryRecordObj->fk_groupuser_id) . '</b></td></tr>
    <tr><td colspan="4">' . $langs->trans('ToolGroupName') . ':  <b> ' . $helper->getGroupLabelById($packageHistoryRecordObj->fk_group_id) . '</b></td></tr>
    <tr><td colspan="4">' . $langs->trans('PackageName') . ': <b>' . $helper->getPackageLabelById($packageHistoryRecordObj->package_id) . '</b></td></tr>
    &nbsp;';

    #
    #   Jutalék Lezárt
    #
    $commissionClosed = $commmissionhandler->getTotalCommissionFromThisPeriod($user_id, $from, $to);
    print '<tr><td colspan="4">
                <table class="dailyclosingTableLeftSide">
                <tr><td style="text-align: center; background-color: lightgrey;" colspan="4">' . $langs->trans('CommissionClosed') . '</td></tr>';
    if (count($commissionClosed) > 0) {
        foreach ($commissionClosed as $commission) {
            print '<tr style="height: 35px; text-align: center;"><td>' . $commission['date_creation'] . '</td><td>' . $commission['label'] . '</td><td style="width:350px;"><b>' . $helper->correctNumber(intval($commission['amount'])) . ' HUF</b></td></tr>';
        }
    } else {
        print '<tr style="height: 35px; text-align: center;"><td colspan="3" style="color: red;">' . $langs->trans('NoCommission') . '</td></tr>';
    }
    print '</table>
                &nbsp;
    </td></tr>';
    #
    #   Jutalék (kalkulált)
    #
    $commission = $commmissionhandler->getTotalCommission($user_id, $from, $to);
    print '<tr><td colspan="4">
    <table class="dailyclosingTableLeftSide">
                <tr><td style="text-align: center; background-color: lightgrey;" colspan="4">' . $langs->trans('ExpectedCommission') . '</td></tr>
                <tr style="height: 35px; text-align: center;"><td style="width:350px;"><b>' . $helper->correctNumber(intval($commission)) . ' HUF</b></td></tr>
                </table>
                &nbsp;
    </td></tr>';

    $withholdingTD = '<td style="text-align: center; background-color: rgb(163, 0, 51); color:white;" colspan="2">' . $langs->trans('Withhold') . '</td>';

    print '<tr><td style="text-align: center; background-color: lightgrey;" colspan="2">' . $langs->trans('DevicesList') . '</td>' . $withholdingTD . '</tr>';
    #
    # Eszközök listája
    #

    $counter = 1;

    //print_r($withholdingsArray); exit;
    //print_r($withholdingsChkArray);

    if ($BLock2ndData) {
        foreach ($BLock2ndData as $device) {
            $displayConfirmButton = 'display:inline-block';
            $displayPrinterButton = 'display:none';

            # checkboxes

            $checkedDevice = $dailyClosing->getChecked($deviceArray, $device);
            $deviceWHChk = $dailyClosing->getDeviceWHChk($deviceArray, $device);
            $deviceAmount = $dailyClosing->getDeviceAmount($amountArray, $device);
            //print_r($checkedDevice);
            $checkedDailyIncomeHUF = $dailyClosing->getCheckedDailyIncomeHUF($deviceArray);
            $dailyAmountHUFChk = $dailyClosing->getDailyAmountHUFChk($deviceArray);
            $checkedDailyIncomeEUR = $dailyClosing->getCheckedDailyIncomeEUR($deviceArray);
            $dailyAmountEURChk = $dailyClosing->getDailyAmountEURChk($deviceArray);
            $witholdingIdentifier = 'd' . $device['device_id'] . '_amount';
            $witholdingChkIdentifier = 'd' . $device['device_id'];
            if (isset($withholdingsArray[$witholdingIdentifier]) && $withholdingsArray[$witholdingIdentifier] !== '') {
                $deviceAmount = $withholdingsArray[$witholdingIdentifier];
            }
            if ($withholdingsChkArray[$witholdingChkIdentifier] == '1') {
                $checkedDevice = 'checked';
                $deviceWHChk = "display:none;";
            }
            //print_r($checkedDevice);

            print '<input type="hidden" name="withholding[withholdingchk][d' . $device['device_id'] . ']" value="x">';
            print '<tr style="height: 35px;"><td style="width:200px;">' . $device['ref'] . '</td><td style="width:350px;">' . $device['title'] . '</td><td class="withholding"><input class="DCcheckbox" type="checkbox" name="withholding[withholdingchk][d' . $device['device_id'] . ']" data-chk="wh_' . $counter . '" data-input="wh_input_' . $counter . '" onclick="checkboxClick(event)" value="1" ' . $checkedDevice . ' ' . $disabled . '/></td><td class="fields withholding" id="wh_' . $counter . '" style="' . $deviceWHChk . '"><input class="inputfield" id="wh_input_' . $counter . '" name="withholding[withholdingAmount][d' . $device['device_id'] . '_amount]" style="display:inline-block;" type="field" value="' . $deviceAmount . '"' . $disabled . '/> HUF</td></tr>';
            $counter++;
        }
    }
    if (!empty($addititonalDevices)) {
        foreach ($addititonalDevices as $key => $value) {
            $device['device_id'] = $key;
            $checkedAdditional = $dailyClosing->getChecked($deviceArray, $device);
            $deviceWHChk = $dailyClosing->getDeviceWHChk($deviceArray, $device);
            $deviceAmount = $dailyClosing->getDeviceAmount($amountArray, $device);
            $witholdingIdentifier = 'd' . $key . '_amount';
            $witholdingChkIdentifier = 'd' . $key;
            if (isset($withholdingsArray[$witholdingIdentifier]) && $withholdingsArray[$witholdingIdentifier] !== '') {
                $deviceAmount = $withholdingsArray[$witholdingIdentifier];
            }
            if ($withholdingsChkArray[$witholdingChkIdentifier] == '1') {   
                $checkedAdditional = 'checked';
                $deviceWHChk = "display:none;";
            }
            print '<input type="hidden" name="withholding[withholdingchk][d' . $key . ']" value="x">';
            print '<tr style="height: 35px;"><td style="width:200px;">' . $helper->getAdditionalTitle($key) . '</td><td style="width:350px;">' . $value . '</td><td class="withholding" id="checkboxtdwh_' . $counter . '"><input class="DCcheckbox" type="checkbox" name="withholding[withholdingchk][d' . $key . ']" data-chk="wh_' . $counter . '" data-input="wh_input_' . $counter . '" onclick="checkboxClick(event)" value="1" ' . $checkedAdditional . ' ' . $disabled . '/></td><td class="fields withholding" id="wh_' . $counter . '" style="' . $deviceWHChk . '"><input class="inputfield" id="wh_input_' . $counter . '" name="withholding[withholdingAmount][d' . $key . '_amount]" style="display:inline-block;" type="field" value="' . $deviceAmount . '"' . $disabled . '/> HUF</td></tr>';
            $counter++;
        }
    }
    print '</table>&nbsp;';
    #
    # Napi bevétel KP
    #

    if (isset($withholdingsArray['_daily_HUFcash_amount']) && $withholdingsArray['_daily_HUFcash_amount'] !== '') {
        $_daily_HUFcash_amount = $withholdingsArray['_daily_HUFcash_amount'];
    }else{
        $_daily_HUFcash_amount = $amountArray['_daily_HUFcash_amount'];
    }
    
    if($withholdingsChkArray['_daily_HUFcash'] == 1){
        $checkedDailyIncomeHUF = 'checked';
        $dailyAmountHUFChk = "display:none";
    }


    if (isset($withholdingsArray['_daily_EURcash_amount']) && $withholdingsArray['_daily_EURcash_amount'] !== '') {
        $_daily_EURcash_amount = $withholdingsArray['_daily_EURcash_amount'];
    }else{
        $_daily_EURcash_amount = $amountArray['_daily_EURcash_amount'];
    }

    if($withholdingsChkArray['_daily_EURcash'] == 1){
        $checkedDailyIncomeEUR = 'checked';
        $dailyAmountEURChk = "display:none";
    }

    print '<table class="dailyclosingTableLeftSide">
                <tr><td style="text-align: center; background-color: lightgrey;" colspan="2">' . $langs->trans('DailyIncomeCash') . '</td>' . $withholdingTD . '</tr>
                <input type="hidden" name="withholding[withholdingchk][_daily_HUFcash]" value="x">
                <tr style="height: 35px;"><td style="width:200px;">' . $langs->trans('DailyIncomeCash') . ' HUF:</td><td style="width:350px; color:green; font-weight: bold;">' . $helper->correctNumber($BLock3rdData['HUFcash']) . ' HUF</td><td class="withholding"><input class="DCcheckbox" type="checkbox" name="withholding[withholdingchk][_daily_HUFcash]" data-chk="wh_' . $counter . '" data-input="wh_input_' . $counter . '" onclick="checkboxClick(event)" value="1"' . $checkedDailyIncomeHUF . ' ' . $disabled . '/></td><td class="fields withholding" id="wh_' . $counter . '" style="' . $dailyAmountHUFChk . '"><input class="inputfield" id="wh_input_' . $counter . '" name="withholding[withholdingAmount][_daily_HUFcash_amount]" style="display:inline-block;" type="field" value="' . $_daily_HUFcash_amount . '" ' . $disabled . '/> HUF</td></tr>';
    $counter++;
    print '<input type="hidden" name="withholding[withholdingchk][_daily_EURcash]" value="x">
                <tr style="height: 35px;"><td style="width:200px;">' . $langs->trans('DailyIncomeCash') . ' EUR:</td><td style="width:350px; color:green; font-weight: bold;">' . $helper->correctNumber($BLock3rdData['EURcash']) . ' EUR</td><td class="withholding"><input class="DCcheckbox" type="checkbox"  name="withholding[withholdingchk][_daily_EURcash]" data-chk="wh_' . $counter . '" data-input="wh_input_' . $counter . '" onclick="checkboxClick(event)" value="1"' . $checkedDailyIncomeEUR . ' ' . $disabled . '/></td><td class="fields withholding" id="wh_' . $counter . '" style="' . $dailyAmountEURChk . '"><input class="inputfield" id="wh_input_' . $counter . '" name="withholding[withholdingAmount][_daily_EURcash_amount]" style="display:inline-block;" type="field" value="' . $_daily_EURcash_amount . '" ' . $disabled . '/> HUF</td></tr>
                </table>';

    #
    # Napi bevétel kártya
    #                
    print '<table class="dailyclosingTableLeftSide">
                <tr><td style="text-align: center; background-color: lightgrey;" colspan="4">' . $langs->trans('DailyIncomeCard') . '</td></tr>
                <tr style="height: 35px;"><td style="width:200px;"> HUF:</td><td style="width:350px;"><b>' . $helper->correctNumber(intval($BLock3rdData['HUFcard'])) . ' HUF</b></td><td style="width:10px;"></td><td></td></tr>
                <tr style="height: 35px;"><td style="width:200px;"> EUR:</td><td style=""><b>' . $helper->correctNumber((intval($BLock3rdData['EURcard']))) . ' EUR </b></td><td></td><td></td></tr>
                </table>
                &nbsp;';
    #
    # Teljes összeg KP + váltó
    #
    print '<table class="dailyclosingTableLeftSide">
                <tr><td style="text-align: center; background-color: lightgrey;" colspan="4">' . $langs->trans('TotalAmountCash') . '</td></tr>
                <tr style="text-align: center;"><td style="width:200px;">&nbsp;</td><td>' . $langs->trans('DailyIncomeCash') . '</td><td>' . $langs->trans('Change') . '</td><td>' . $langs->trans('TotalAmountCash') . '</td></tr>
                <tr style="height: 35px;"><td style="width:200px;"> HUF:</td><td style="text-align: center;">' . $helper->correctNumber($BLock3rdData['HUFcash']) . ' HUF </td><td style="text-align: center;">' . $helper->correctNumber(intval($addititonalChangesDevices['change_huf'])) . ' HUF </td><td style="text-align: center;"><b>' . $helper->correctNumber(intval($BLock3rdData['HUFcash'] + intval($addititonalChangesDevices['change_huf']))) . ' HUF</b></td><td style="width:10px;"></td></tr>
                <tr style="height: 35px;"><td style="width:200px;"> EUR:</td><td style="text-align: center;">' . $helper->correctNumber($BLock3rdData['EURcash']) . ' EUR </td><td style="text-align: center;">' . $helper->correctNumber(intval($addititonalChangesDevices['change_eur'])) . ' EUR </td><td style="text-align: center;"><b>' . $helper->correctNumber((intval($BLock3rdData['EURcash']) + intval($addititonalChangesDevices['change_eur']))) . ' EUR</b></td></tr>
                </table>
                &nbsp;';
    #
    # Teljes összeg KP + kartya
    #
    print '<table class="dailyclosingTableLeftSide">
                <tr><td style="text-align: center; background-color: lightgrey;" colspan="4">' . $langs->trans('TotalAmount') . '</td></tr>
                <tr style="text-align: center;"><td style="width:200px;">&nbsp;</td><td>' . $langs->trans('DailyIncomeCash') . '</td><td>' . $langs->trans('DailyIncomeCard') . '</td><td>' . $langs->trans('TotalAmount') . '</td></tr>
                <tr style="height: 35px;"><td style="width:200px;"> HUF:</td><td style="text-align: center;">' . $helper->correctNumber($BLock3rdData['HUFcash']) . ' HUF </td><td style="text-align: center;">' . $helper->correctNumber($BLock3rdData['HUFcard']) . ' HUF </td><td style="text-align: center;"><b>' . $helper->correctNumber(intval($BLock3rdData['HUFcash'] + intval($BLock3rdData['HUFcard']))) . ' HUF</b></td></tr>
                <tr style="height: 35px;"><td style="width:200px;"> EUR:</td><td style="text-align: center;">' . $helper->correctNumber($BLock3rdData['EURcash']) . ' EUR </td><td style="text-align: center;">' . $helper->correctNumber($BLock3rdData['EURcard']) . ' EUR </td><td style="text-align: center;"><b>' . $helper->correctNumber(intval($BLock3rdData['EURcash']) + intval($BLock3rdData['EURcard'])) . ' EUR</b></td></tr>
                </table>
                &nbsp;';

    #
    #   Unprinted factures
    #                
    print '<table class="dailyclosingTableLeftSide">';
    print '<tr onclick="showFactures(event)"><td colspan="4" class="unprintedfactures">Unprinted factures<span id="unprintedFactureSpan" class="fa fa-chevron-down" style="float:right; padding-right: 10px; padding-top:3px;"></span></td></tr>';
    print '</table>';
    print $dailyClosing->unprintedFactures($packageHistoryRecordObj->fk_group_id, $user_id, $from);
    #
    # Checkbox
    #
    print '<div>&nbsp;</div>';
    print '<div style="' . $displayConfirmButton . '">
            <div><input type="checkbox" onclick="checkall(event)" />' . $langs->trans('EverythingOK') . '</div>&nbsp;';

    #
    # Confirm buttons
    #
    print '<div><button type="submit" name"confirmdailyclosing" value="1">' . $langs->trans('DailyClosing') . '</button></div>';
    print '</div></div>';
    print '&nbsp;';
    print '<div id="DCFactureData" style="width: 50%; float:left;">
        <table style="width:100%;">
            <tr>
                <td colspan="4" class="dailyClosingHeader">' . $langs->trans('FactureList') . '</td>
                </tr>';
    print '<tr><td>';
    print '<table>';
    $printedFactures = $dailyClosing->printedFactures($user_id, $packageHistoryRecordObj->date_creation);
    $rownum = 0;
    print '<tr>
    <td colspan="2">&nbsp;<span class="fa fa-arrow-down"></span> <b>Mark for storno</b></td>
    <td colspan="2">&nbsp;<span class="fa fa-arrow-down"></span> <b>No commission</b></td>
    <td></td>
    <td></td>
    <td colspan="3" style="text-align:right;"><b>Number of prints </b><span style="margin-right: 10px;" class="fa fa-arrow-down"></span></td>
    </tr>';
    foreach ($factures as $facture) {
        $checked = $facture['marked_for_storno'] ? 'checked' : '';
        $checkedCommission = $facture['commission_deduction'] ? 'checked' : '';
        $bgcolor = $dailyClosing->setRowColor($rownum);
        $printed = '';
        $numberOfPrints = 0;
        //$disabled = $facture['marked_for_storno'] ? 'disabled' : '';

        if (in_array($facture['rowid'], $printedFactures)) {
            $printed = '<div style="color:green;">Printed</div>';
            $numberOfPrints = $dailyClosing->getTheNumberOfPrints($facture['rowid']);
        }
        print '<tr style="background-color: ' . $bgcolor . '">
        <td style="width: 5%"><input type="checkbox" name="storno" value="' . $facture['rowid'] . '" ' . $checked . ' ' . $disabled . ' onclick="setStorno(event)"></input></td>
        <td style="width: 25%"><a href="/compta/facture/card.php?facid=' . $facture['rowid'] . '&save_lastsearch_values=1">' . $facture['ref'] . ' <span class="fa fa-link"></span></a></td>
        <td style="width: 5%"><input type="checkbox" name="commission" value="' . $facture['rowid'] . '" ' . $checkedCommission . ' ' . $disabled . ' onclick="setCommission(event)"></input></td>
        <td style="width: 20%">' . $facture['date_closing'] . '</td>
        <td style="width: 15%" class="center">' . $facture['libelle'] . '</td>
        <td style="width: 10%" class="center">' . $helper->correctNumber(intval($facture['multicurrency_total_ttc'])) . '</td>
        <td style="width: 10%" class="center">' . $facture['multicurrency_code'] . '</td>
        <td style="width: 5%" class="center">' . $printed . '</td>
        <td style="width: 5%" class="center">' . $numberOfPrints . '</td>
        </tr>';
        $rownum++;
    }

    print '</table></td></tr>';
    print '</table>
    </div>';
    print '</form>';
} else {
    print '<div id="DCuserData" style="width:50%;">';
    print '<table style="width:100%;"><tr><td colspan="4" class="dailyClosingHeader">' . $name . '</td></tr></table>';
    print '<div style="color: red; margin-top: 50px;">' . $langs->trans('NoDailyClosing') . '</div>';
    print '<div style="margin-top: 20px;"><a href="settlementtoolssettingsindex.php?idmenu=1594&mainmenu=settlements&leftmenu=&id=' . $packageHistoryRecordObj->fk_group_id . '&nopackageuser=1">' . $langs->trans('BackToGroup') . '</a></div>';
    print '</div>';
}