<?php
/* Copyright (C) 2026		Eduard Wisch				<data@data-it-solution.de>
 *
 * 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/>.
 */

/**
 * \file    importzugferd/lib/importzugferd.lib.php
 * \ingroup importzugferd
 * \brief   Library files with common functions for ImportZugferd
 */

/**
 * Prepare admin pages header
 *
 * @return array<array{string,string,string}>
 */
function importzugferdAdminPrepareHead()
{
	global $langs, $conf;

	// global $db;
	// $extrafields = new ExtraFields($db);
	// $extrafields->fetch_name_optionals_label('myobject');

	$langs->load("importzugferd@importzugferd");

	$h = 0;
	$head = array();

	$head[$h][0] = dol_buildpath("/importzugferd/admin/setup.php", 1);
	$head[$h][1] = $langs->trans("Settings");
	$head[$h][2] = 'settings';
	$h++;

	/*
	$head[$h][0] = dol_buildpath("/importzugferd/admin/myobject_extrafields.php", 1);
	$head[$h][1] = $langs->trans("ExtraFields");
	$nbExtrafields = (isset($extrafields->attributes['myobject']['label']) && is_countable($extrafields->attributes['myobject']['label'])) ? count($extrafields->attributes['myobject']['label']) : 0;
	if ($nbExtrafields > 0) {
		$head[$h][1] .= '<span class="badge marginleftonlyshort">' . $nbExtrafields . '</span>';
	}
	$head[$h][2] = 'myobject_extrafields';
	$h++;

	$head[$h][0] = dol_buildpath("/importzugferd/admin/myobjectline_extrafields.php", 1);
	$head[$h][1] = $langs->trans("ExtraFieldsLines");
	$nbExtrafields = (isset($extrafields->attributes['myobjectline']['label']) && is_countable($extrafields->attributes['myobjectline']['label'])) ? count($extrafields->attributes['myobject']['label']) : 0;
	if ($nbExtrafields > 0) {
		$head[$h][1] .= '<span class="badge marginleftonlyshort">' . $nbExtrafields . '</span>';
	}
	$head[$h][2] = 'myobject_extrafieldsline';
	$h++;
	*/

	$head[$h][0] = dol_buildpath("/importzugferd/admin/about.php", 1);
	$head[$h][1] = $langs->trans("About");
	$head[$h][2] = 'about';
	$h++;

	// Show more tabs from modules
	// Entries must be declared in modules descriptor with line
	//$this->tabs = array(
	//	'entity:+tabname:Title:@importzugferd:/importzugferd/mypage.php?id=__ID__'
	//); // to add new tab
	//$this->tabs = array(
	//	'entity:-tabname:Title:@importzugferd:/importzugferd/mypage.php?id=__ID__'
	//); // to remove a tab
	complete_head_from_modules($conf, $langs, null, $head, $h, 'importzugferd@importzugferd');

	complete_head_from_modules($conf, $langs, null, $head, $h, 'importzugferd@importzugferd', 'remove');

	return $head;
}

/**
 * Get readable label for UN/ECE Recommendation 20 unit codes
 *
 * @param string $code  UN/ECE unit code (e.g. C62, MTR, LTR)
 * @return string       Readable label or original code if not found
 */
function zugferdGetUnitLabel($code)
{
	// UN/ECE Recommendation 20 - Common unit codes used in ZUGFeRD/Factur-X
	$units = array(
		// Pieces / Count
		'C62' => 'Stk.',      // One (piece/unit)
		'PCE' => 'Stk.',      // Piece
		'EA'  => 'Stk.',      // Each
		'H87' => 'Stk.',      // Piece
		'XPP' => 'Stk.',      // Piece
		'NAR' => 'Stk.',      // Number of articles
		'NMP' => 'Stk.',      // Number of packs
		'NPR' => 'Paar',      // Number of pairs
		'SET' => 'Set',       // Set
		'PR'  => 'Paar',      // Pair

		// Length
		'MTR' => 'm',         // Metre
		'CMT' => 'cm',        // Centimetre
		'MMT' => 'mm',        // Millimetre
		'KMT' => 'km',        // Kilometre
		'INH' => 'Zoll',      // Inch
		'FOT' => 'Fuß',       // Foot
		'LM'  => 'lfm',       // Linear metre

		// Area
		'MTK' => 'm²',        // Square metre
		'CMK' => 'cm²',       // Square centimetre
		'MMK' => 'mm²',       // Square millimetre

		// Volume
		'MTQ' => 'm³',        // Cubic metre
		'LTR' => 'l',         // Litre
		'MLT' => 'ml',        // Millilitre
		'HLT' => 'hl',        // Hectolitre
		'CMQ' => 'cm³',       // Cubic centimetre

		// Mass / Weight
		'KGM' => 'kg',        // Kilogram
		'GRM' => 'g',         // Gram
		'MGM' => 'mg',        // Milligram
		'TNE' => 't',         // Tonne (metric ton)
		'LBR' => 'lb',        // Pound

		// Time
		'HUR' => 'Std.',      // Hour
		'MIN' => 'Min.',      // Minute
		'SEC' => 'Sek.',      // Second
		'DAY' => 'Tag',       // Day
		'WEE' => 'Woche',     // Week
		'MON' => 'Monat',     // Month
		'ANN' => 'Jahr',      // Year

		// Packaging
		'XBX' => 'Karton',    // Box
		'XCT' => 'Karton',    // Carton
		'XPK' => 'Paket',     // Package
		'XPA' => 'Palette',   // Pallet
		'XSA' => 'Sack',      // Sack
		'XBG' => 'Beutel',    // Bag
		'XBO' => 'Flasche',   // Bottle
		'XCA' => 'Dose',      // Can
		'XRO' => 'Rolle',     // Roll
		'XTU' => 'Tube',      // Tube

		// Other
		'P1'  => '%',         // Percent
		'KWH' => 'kWh',       // Kilowatt hour
		'MWH' => 'MWh',       // Megawatt hour
		'WTT' => 'W',         // Watt
		'KWT' => 'kW',        // Kilowatt
	);

	$code = strtoupper(trim($code));

	if (isset($units[$code])) {
		return $units[$code];
	}

	return $code;
}
