<?php
namespace App\Smarty\Functions;

/**
 * works only on mvc
 * @author dcs
 *
 */
class CashBookDocumentLabel
{

	static public function create($params, $smarty)
	{

		// CHECK INPUT ----------------------------------------------------------------------------
		$defaultOutput = '';
		if ($params['default']) {
			$defaultOutput = $params['default'];
		}
		// GET CASH BOOK DOCUMENT DETAILS ---------------------------------------------------------
		// CashBook document object reference
		if (is_object($params['ref']) and get_class($params['ref'])=='Entity\CashBookDocument') {
			$cashBookDocument = $params['ref'];
		}
		// supplier invoice id
		elseif ( is_numeric($params['ref']) and $params['ref']>0 ) {
			$cashBookDocument = \Entity\CashBookDocument::select($params['ref']);
			if (!$cashBookDocument->recordExists()) {
				return $defaultOutput;
			}
		}
		// something invalid
		else {
			return $defaultOutput;
		}

		// STORE SUPPLIER INVOICE DETAILS IN AN OUTPUT PARAMETER ($params['assign']) --------------
		if ($params['assign']) {
			$smarty->assign($params['assign'], $cashBookDocument);
			return '';
		}

		// CHECK OUTPUT FORMAT --------------------------------------------------------------------
		if (!$params['format']) {
			$title = "#{$cashBookDocument->getId()}";
		} else {
			$replaceArray = [];
			$replaceArray['%number%'] = $cashBookDocument->number." / ".\Engine\DateTime::createFromIsoDate($cashBookDocument->date)->formatDateStd();

			$title = str_replace(array_keys($replaceArray), array_values($replaceArray), $params['format']);
		}

		if ($params['hideLink']) {
			return $title;
		}
		else {
			return sprintf("<span class=\"remoteLoadLink\" onclick=\"RemoteLoad.loadEntity(this, '%s', '%s')\">%s</span>",
				'cashBookDocument',
				$cashBookDocument->getId(),
				$title);
		}
	}

}