<?php

namespace App\Dashboard\Widget;

class QuickReports_TopSuppliers extends QuickReports_GeneralAbstract
{
	use Traits\FinancialWidget;

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Dashboard\Widget\QuickReports_GeneralAbstract::definePivotSheets()
	 */
	protected function definePivotSheets(\App\Dashboard\Widget\Helper\QrSheetsManager $qrSheetsManager)
	{

		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();
		$lang = $_LANG['dashboard']['widgetsList']['App\Dashboard\Widget\QuickReports_TopSuppliers']['sheets'];

		// manually selected currency, existing currency is work
		if ($this->getOption('reportCurrency')=='default') {
			$reportCurrency = 'default';
			$currency = \App\Config\Currencies::getInstance()->getDefaultCurrency();
		} else {
			// it might be possible that is not filled in
			$reportCurrency = 'work';
			$currency = \App\Config\Currencies::getInstance()->getWorkCurrency();
		}

		// service period
		$periodEnd = TODAY_YEAR;
		$periodStart = $periodEnd - 3;

		// default filters
		$defaultFilters = [
			['column'=>'invoiceDateY', 'operation'=>'BETWEEN', 'value'=>[$periodStart, $periodEnd] ],
			['column'=>'statusOp', 'operation'=>'=', 'value'=>'confirmed' ],
		];


		// PIVOT 1 ////////////////////////////////////////////////////////////////////////////////
		$pivotContentGen = new \App\QuickReports\PivotTables\GenerateContent('SupplierInvoiceRows');
		$pivotContentGen->addFilters($defaultFilters);

		$pivotContentGen->setRowsField('idSupplier');
		$pivotContentGen->setRowsLimit(10, true);

		$pivotContentGen->setColsField('invoiceDateY');
		$pivotContentGen->setColsOrder('invoiceDateY', null, 'ASC');

		// if the currency is set to default
		if ($reportCurrency == 'default') {
			$pivotContentGen->setRowsOrder('rowTtlDef', 'SUM', 'DESC');
			$pivotContentGen->setDataField('rowTtlDef', 'SUM');
		} else {
			$pivotContentGen->setRowsOrder('rowTtlWrk', 'SUM', 'DESC');
			$pivotContentGen->setDataField('rowTtlWrk', 'SUM');
		}

		$sh = new \App\Dashboard\Widget\Helper\QrSheet($pivotContentGen, $lang['suppPerYears']['name'], $lang['suppPerYears']['description'], $lang['suppPerYears']['title']);
		$sh->setDisplayCurrency($currency);

		// add sheet
		$qrSheetsManager->addQrSheet($sh, 'suppPerYears');


		// PIVOT 2 ////////////////////////////////////////////////////////////////////////////////
		$pivotContentGen = new \App\QuickReports\PivotTables\GenerateContent('SupplierInvoiceRows');
		$pivotContentGen->addFilters($defaultFilters);

		$pivotContentGen->setRowsField('idSupplier');
		$pivotContentGen->setRowsLimit(10, true);

		$pivotContentGen->setColsField('invoiceDateM');
		$pivotContentGen->setColsOrder('invoiceDateM', null, 'ASC');

		// if the currency is set to default
		if ($reportCurrency == 'default') {
			$pivotContentGen->setRowsOrder('rowTtlDef', 'SUM', 'DESC');
			$pivotContentGen->setDataField('rowTtlDef', 'SUM');
		} else {
			$pivotContentGen->setRowsOrder('rowTtlWrk', 'SUM', 'DESC');
			$pivotContentGen->setDataField('rowTtlWrk', 'SUM');
		}

		$sh = new \App\Dashboard\Widget\Helper\QrSheet($pivotContentGen, $lang['suppPerMonths']['name'], $lang['suppPerMonths']['description'], $lang['suppPerMonths']['title']);
		$sh->setDisplayCurrency($currency);

		// add sheet
		$qrSheetsManager->addQrSheet($sh, 'suppPerMonths');


		// ADD REFERENCES /////////////////////////////////////////////////////////////////////////
		$qrSheetsManager->addReference('suppPerYears', 'colsFields', 'suppPerMonths');
		$qrSheetsManager->setIdDefaultSheet('suppPerYears');

	}

	/**
	 * The form default options.
	 * {@inheritDoc}
	 * @see \App\Dashboard\Widget\QuickReports_GeneralAbstract::getDefaultOptions()
	 */
	protected function getDefaultOptions()
	{
		return [
			'reportCurrency' => 'work',
		];
	}

	/**
	 * Generate the options form
	 */
	protected function populateLocalOptionsForm(\Engine\Form\Container\GeneralAbstract $form)
	{
		// load lang
		$loaderLang = \App\Loader_Lang::getInstance();
		$loaderLang->loadAppFile("_customLoad/dashboard/widgets/_general.lang.php");
		$_LANG = $loaderLang->getAll();

		$fieldset = \Engine\Form\Container\Fieldset::create('');

		$defCurr = \App\Config\Currencies::getInstance()->getDefaultCurrency();
		$workCurr = \App\Config\Currencies::getInstance()->getWorkCurrency();

		$field = new \Engine\Form\Input\Radio($_LANG['dashboard']['widgets']['_GENERAL_']['qrExtra']['currency'], 'reportCurrency', $this->getOption('reportCurrency'));
		$field->setPossibleValues(['default'=>sprintf($_LANG['dashboard']['widgets']['_GENERAL_']['qrExtra']['defCurr'], $defCurr), 'work'=>sprintf($_LANG['dashboard']['widgets']['_GENERAL_']['qrExtra']['wrkCurr'], $workCurr)]);
		$fieldset->addElement($field);

		$form->addContainer($fieldset);
		return $form;
	}

	/**
	 * Extract data from the form return($R) and sets internal configs($this->options) accordingly
	 */
	protected function extractFormOptions($formData)
	{
		$options = ['reportCurrency' => $formData['reportCurrency'] ];
		$this->setOptions($options);
	}


}

