<?php

namespace App\Reports\Sources\BalancesList;


class PopulateInitialImport extends \App\Reports\Sources\PopulateAbstract
{

	/**
	 * {@inheritDoc}
	 * @see \App\Reports\Sources\PopulateAbstract::populate()
	 */
	public function populate() :void
	{

		// INITS //////////////////////////////////////////////////////////////////////////////////

		$reportTable = $this->tableManager->getTableName();

		global $DBTRd_balances, $DBTRd_users;

		$activeColumns = array_keys( $this->columnsManager->getStdColumns() );

		$stdFilters = $this->filtersManager->getStdFilters();


		// CREATE SQL FOR COPYING BALANCES LIST ///////////////////////////////////////////////////

		// refresh report status
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'runningCashingSQL', 0);

		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Insert($reportTable);
		$reportCopyInfo->setMainTable($DBTRd_balances,	'bal');
		$reportCopyInfo->addJoinTable($DBTRd_users,		'respUser',	'bal.idUser=respUser.id');

		$cols = [
			'bal_id'					=> 'bal.id',
			'bal_typeCode'				=> 'bal.balanceType',
			'bal_statusCode'			=> 'bal.balanceStatus',
			'bal_number'				=> 'bal.balanceNumber',
			'bal_date'					=> 'bal.balanceDate',
			'bal_code'					=> 'bal.balanceCode',
			'bal_name'					=> 'bal.balanceName',
			'bal_generationType'		=> 'bal.generation',
			'bal_comments'				=> 'bal.comments',
			'bal_virtualDate'			=> 'bal.virtualDate',
			'bal_remarks'				=> 'bal.remarks',
			'bal_processedRemarks'		=> 'bal.processedRemarks',
			'bal_totalIncomeDefCurr'	=> 'bal.totalIncomeDefCurr',
			'bal_totalExpensesDefCurr'	=> 'bal.totalExpensesDefCurr',
			'bal_totalProfitDefCurr'	=> 'bal.totalProfitDefCurr',
			'bal_profitPercent'			=> 'bal.profitPercent',

			'bal_userId'				=> 'bal.idUser',
			'bal_officeId'				=> 'bal.idOffice',
			'bal_deptId'				=> 'respUser.idDepartment',
			'bal_addUserId' 			=> 'bal.idAddUser',
			'bal_confirmUserId'			=> 'bal.idConfirmUser',

			'bal_addTime' 				=> 'bal.addTime',
			'bal_confirmTime'			=> 'bal.confirmTime',
		];

		$reportCopyInfo->setStructureColumns($cols);
		$reportCopyInfo->addStructureColumn("bal_dateYM", 			    "DATE_FORMAT(bal.balanceDate, '%Y-%m-01')");
		$reportCopyInfo->addStructureColumn("bal_dateY", 	            "YEAR(bal.balanceDate)");

		$reportCopyInfo->addStructureColumn("bal_virtualDateYM",  		"DATE_FORMAT(bal.virtualDate, '%Y-%m-01')");
		$reportCopyInfo->addStructureColumn("bal_virtualDateY", 	    "YEAR(bal.virtualDate)");
		$reportCopyInfo->addStructureColumn("bal_userName", 			"CONCAT(respUser.lastName, ' ', respUser.firstName)", 'respUser');
		$reportCopyInfo->addStructureColumn("bal_confirmed",     		"IF(bal.confirmTime IS NOT NULL, 1,0)");

		$reportCopyInfo->setActiveColumns($activeColumns);

		// ADD FILTERS ////////////////////////////////////////////////////////////////////////////
		$reportCopyInfo->addFilter('bal.deleted', '=', 0);

		// add date filter
		if ($stdFilters['balanceDate']['start']) {
			$reportCopyInfo->addFilter('bal.balanceDate', '>=', $stdFilters['balanceDate']['start']);
		}
		if ($stdFilters['balanceDate']['end']) {
			$reportCopyInfo->addFilter('bal.balanceDate', '<=', $stdFilters['balanceDate']['end']);
		}

		// filter by user/office/dept
		if ($stdFilters['idRespUsers']) {
			$reportCopyInfo->addFilter('bal.idUser' , 'IN', $stdFilters['idRespUsers']);
		}
		if ($stdFilters['idRespOffices']) {
			$reportCopyInfo->addFilter('bal.idOffice' , 'IN', $stdFilters['idRespOffices']);
		}

		if ($stdFilters['idRespDepartments']) {
			$reportCopyInfo->addFilter('respUser.idDepartment' , 'IN', $stdFilters['idRespDepartments']);

		}

		$reportCopyInfo->populate();

	}


}