<?php

namespace App\Dashboard\Widget;

class QuickReports_TopAgentsInvProfit 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_TopAgentsInvProfit']['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();
		}

		// last 3 full years
		$periodEnd = TODAY_YEAR;
		$periodStart = TODAY_YEAR - 3;

		// default filters
		$defaultFilters = [
			['column'=>'invoiceDateY', 'operation'=>'BETWEEN', 'value'=>[$periodStart, $periodEnd]],
			['column'=>'invoiceVoidDate', 'operation'=>'IS NULL'],
			['column'=>'invoiceIssueDate', 'operation'=>'IS NOT NULL'],
		];

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

		$pivotContentGen1->setRowsField('invoiceDateY');
		$pivotContentGen1->setRowsOrder('invoiceDateY', null, 'ASC');

		// if the currency is set to default
		if ($reportCurrency == 'default') {
			$pivotContentGen1->setDataField('rowProfitDef', 'SUM');
		} else {
			$pivotContentGen1->setDataField('rowProfitWrk', 'SUM');
		}

		$sh1 = new \App\Dashboard\Widget\Helper\QrSheet($pivotContentGen1, $lang['perYear']['name'], $lang['perYear']['description'], $lang['perYear']['title']);

		$sh1->setDisplayCurrency($currency);

		// add sheet
		$qrSheetsManager->addQrSheet($sh1, 'perYear');


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

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

		$pivotContentGen2->setRowsField('serviceIdUser');
		$pivotContentGen2->setRowsLimit(10, true);

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

		$sh2 = new \App\Dashboard\Widget\Helper\QrSheet($pivotContentGen2, $lang['agentsPerMonth']['name'], $lang['agentsPerMonth']['description'], $lang['agentsPerMonth']['title']);

		$sh2->setDisplayCurrency($currency);

		// add sheet
		$qrSheetsManager->addQrSheet($sh2, 'agentsPerMonth');


		// PIVOT 3 ////////////////////////////////////////////////////////////////////////////////
		$pivotContentGen3 = new \App\QuickReports\PivotTables\GenerateContent('ClientInvoiceRows');
		$pivotContentGen3->addFilters($defaultFilters);

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

		$pivotContentGen3->setRowsField('serviceCode');
		$pivotContentGen3->setRowsLimit(10, true);

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

		$sh3 = new \App\Dashboard\Widget\Helper\QrSheet($pivotContentGen3, $lang['agentDetailsPerMonth']['name'], $lang['agentDetailsPerMonth']['description'], $lang['agentDetailsPerMonth']['title']);

		$sh3->setDisplayCurrency($currency);

		// add sheet
		$qrSheetsManager->addQrSheet($sh3, 'agentDetailsPerMonth');


		// ADD REFERENCES /////////////////////////////////////////////////////////////////////////
		$qrSheetsManager->setIdDefaultSheet('perYear');

 		$qrSheetsManager->addReference('perYear', 'rowsFields', 'agentsPerMonth');
		$qrSheetsManager->addReference('agentsPerMonth', 'rowsFields', 'agentDetailsPerMonth');


	}

		/**
	 * 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);
	}

}