<?php

namespace App\Dashboard\Widget;

class QuickReports_BalanceProfit 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_BalanceProfit']['sheets'];

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

		// default filters
		$defaultFilters = [
			['column'=>'balanceVirtualDateY', 'operation'=>'BETWEEN', 'value'=>[$periodStart, $periodEnd]],
			['column'=>'balanced', 'operation'=>'=', 'value'=>'1'],
			['column'=>'statusOp', 'operation'=>'=', 'value'=>'OK'],
		];

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

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

		$pivotContentGen1->setDataField('balanceProfitDef', 'SUM');

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

		$sh1->setDisplayCurrency(\App\Config\Currencies::getInstance()->getDefaultCurrency());

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

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

		$pivotContentGen2->setRowsField('serviceName');
		$pivotContentGen2->setRowsOrder('balanceProfitDef', 'SUM', 'DESC');
		$pivotContentGen2->setRowsLimit(5, true);
		$pivotContentGen2->setColsField('balanceVirtualDateM');
		$pivotContentGen2->setColsOrder('balanceVirtualDateM', null, 'ASC');

		$pivotContentGen2->setDataField('balanceProfitDef', 'SUM');

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

		$sh2->setDisplayCurrency(\App\Config\Currencies::getInstance()->getDefaultCurrency());

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

		// ADD REFERENCES /////////////////////////////////////////////////////////////////////////
 		$qrSheetsManager->addReference('balanceProfiYearly', 'rowsFields', 'balanceProfiDetais');

		$qrSheetsManager->setIdDefaultSheet('balanceProfiYearly');

	}

}
