<?php
/* Copyright (C) 2017  Laurent Destailleur  <eldy@users.sourceforge.net>
 *
 * 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/>.
 *
 * Need to have following variables defined:
 * $object (invoice, order, ...)
 * $action
 * $conf
 * $langs
 *
 * $keyforbreak may be defined to key to switch on second column
 */

// Protection to avoid direct call of template
if (empty ($conf) || !is_object($conf)) {
	print "Error, template page can't be called as URL";
	exit;
}
if (!is_object($form)) {
	$form = new Form($db);
}

?>
<!-- BEGIN PHP TEMPLATE commonfields_view.tpl.php -->
<?php

$object->fields = dol_sort_array($object->fields, 'position');

$titleArray = [
	'interval' => $langs->trans('interval'),
	'sumHUFCash' => $langs->trans('CashHUFGross'),
	'sumEURCash' => $langs->trans('CashEURGross'),
	'arraySumSale' => $langs->trans('TotalSalesByVAT'),
	'arraySumCash' => $langs->trans('TotalSalesCash'),
	'arraySumCard' => $langs->trans('TotalSalesCard'),
	'arraystornoSumCash' => $langs->trans('MarkedAccountsCash'),
	'arraystornoSumCard' => $langs->trans('MarkedAccountsCard'),
];

foreach ($object->fields as $key => $val) {
	if (!empty ($keyforbreak) && $key == $keyforbreak) {
		break; // key used for break on second column
	}

	// Discard if extrafield is a hidden field on form
	if (abs($val['visible']) != 1 && abs($val['visible']) != 3 && abs($val['visible']) != 4 && abs($val['visible']) != 5) {
		continue;
	}

	if (array_key_exists('enabled', $val) && isset ($val['enabled']) && !verifCond($val['enabled'])) {
		continue; // We don't want this field
	}
	if (in_array($key, array('ref', 'status'))) {
		continue; // Ref and status are already in dol_banner
	}

	$value = $object->$key;

	print '<tr class="field_' . $key . '"><td';
	print ' class="' . (empty ($val['tdcss']) ? 'titlefield' : $val['tdcss']) . ' fieldname_' . $key;
	//if ($val['notnull'] > 0) print ' fieldrequired';     // No fieldrequired on the view output
	if ($val['type'] == 'text' || $val['type'] == 'html') {
		print ' tdtop';
	}
	print '">';
	if (!empty ($val['help'])) {
		print $form->textwithpicto($langs->trans($val['label']), $langs->trans($val['help']));
	} else {
		if (isset ($val['copytoclipboard']) && $val['copytoclipboard'] == 1) {
			print showValueWithClipboardCPButton($value, 0, $langs->transnoentitiesnoconv($val['label']));
		} else {
			print $langs->trans($val['label']);
		}
	}
	print '</td>';
	print '<td class="valuefield fieldname_' . $key;
	if ($val['type'] == 'text') {
		print ' wordbreak';
	}
	if (!empty ($val['cssview'])) {
		print ' ' . $val['cssview'];
	}
	print '">';
	if (in_array($val['type'], array('text', 'html'))) {
		print '<div class="longmessagecut">';
	}
	if ($key == 'lang') {
		$langs->load("languages");
		$labellang = ($value ? $langs->trans('Language_' . $value) : '');
		print picto_from_langcode($value, 'class="paddingrightonly saturatemedium opacitylow"');
		print $labellang;
	} elseif ($key == 'checkout_json') {
	} else {
		if (isset ($val['copytoclipboard']) && $val['copytoclipboard'] == 2) {
			$out = $object->showOutputField($val, $key, $value, '', '', '', 0);
			print showValueWithClipboardCPButton($out, 0, $out);
		} else {
			print $object->showOutputField($val, $key, $value, '', '', '', 0);
		}
	}
	//print dol_escape_htmltag($object->$key, 1, 1);
	if (in_array($val['type'], array('text', 'html'))) {
		print '</div>';
	}
	print '</td>';
	print '</tr>';
}

$array = json_decode($value);
print '<tr><td colspan="2"><table>
		<tr><td>' . $titleArray['interval'] . ': </td><td colspan="3"> ' . $array->from . ' - ' . $array->to . '</td></tr>
		<tr><td>' . $titleArray['sumHUFCash'] . ': </td><td colspan="3"><b> ' . $helper->correctNumber(intval($array->sumHUFCash)) . ' HUF</b></td></tr>
		<tr><td>' . $titleArray['sumEURCash'] . ': </td><td colspan="3"><b> ' . $helper->correctNumber(intval($array->sumEURCash)) . ' EUR</b></td></tr>';
foreach ($array->sumByVAT as $k1 => $v1) {
	if ($k1 == 'arraySumSale') {
		print '<tr><td colspan="7" style="text-align:center; font-weight: bold; background-color: grey; color: white;">' . $titleArray[$k1] . '</td></tr>';
		print '<tr style="text-align: center; font-weight: bold; background-color: lightgrey;">
		<td>&nbsp;</td>
		<td>' . $langs->trans('Net') . '</td>
		<td>' . $langs->trans('Vat') . '</td>
		<td>' . $langs->trans('Gross') . '</td>
		<td>' . $langs->trans('Net') . '</td>
		<td>' . $langs->trans('Vat') . '</td>
		<td>' . $langs->trans('Gross') . '</td>
	</tr>';
	
	foreach ($v1 as $k2 => $v2) {
		$result = '';
		if ($k1 == 'arraySumSale') {
			$result .= '<tr style="text-align: center;"><td style="width: 25%;">(' . $k2 . '%) ' . $langs->trans('Vat') . ': </td>';
			foreach ($v2 as $k3 => $v3) {
				$result .= '<td> ' . $helper->correctNumber(intval($v3->multicurrency_total_ht)) . ' ' . $k3 . '</td>';
				$result .= '<td>' . $helper->correctNumber(intval($v3->multicurrency_total_tva)) . ' ' . $k3 . '</td>';
				$result .= '<td>' . $helper->correctNumber(intval($v3->multicurrency_total_ttc)) . ' ' . $k3 . '</td>';
			}
		}
		$result .= '</tr>';
		print $result;
	}
	print '<tr><td>&nbsp;</td></tr>';
	}else{
		$result = '';
		$result .= '<tr><td>' . $titleArray[$k1] . '</td>';
		foreach ($v1 as $k4 => $v4) {
			$result .= '<td>&nbsp;</td><td>&nbsp;</td><td style="text-align: center;">' . $helper->correctNumber(intval($v4->multicurrency_total_ttc)) . ' ' . $k4 . '</td>';
		}
		print $result;
	}
}
print '</table>';

print '</td></tr></table>';

// We close div and reopen for second column
print '</div>';


print '<div class="fichehalfright">';
print '<div class="underbanner clearboth"></div>';

print '<table class="border centpercent tableforfield">';

//print $rightpart;

?>
<!-- END PHP TEMPLATE commonfields_view.tpl.php -->