<?php
///////////////////////////////////////////////////////////////////////////////////////////////////
// UPDATE TASK DETAILS ////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
refreshReportStatus('extractMainDataFromDb', 'updateTaskDetails', 0);

// update status name
$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $availableTableColumns);
$reportCopyInfo->addCaseStructureColumn("task_statusName",   "task_statusCode",   (Array)$_LANG['tasks']['statusName']);
$reportCopyInfo->populate();

// update status reason name ( $_LANG['tasks']['reasonForStatus'][$row['statusCode']][$wow['statusReasonCode']] )
if (in_array('task_statusReasonName', $availableTableColumns)) {

	\Entity\Task::loadCFG();
	$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

	foreach ($CFG['globalEntity']['::Task']['_special']['statusReasonsList'] as $status=>$reason) {
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $availableTableColumns);
		$reportCopyInfo->addCaseStructureColumn("task_statusReasonName",   "task_statusReasonCode",   (Array)$_LANG['tasks']['reasonForStatus'][$status]);
		$reportCopyInfo->addFilter("rep.task_statusCode", "=", $status);
		$reportCopyInfo->populate();
	}
}

// update operation type name
$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $availableTableColumns);
$reportCopyInfo->populateSimple($DBTRd_taskOperationTypes, 'task_operationTypeName', 'task_idOperationType', 'description');

// update category name
$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $availableTableColumns);
$reportCopyInfo->populateSimple($DBTRd_taskCategories, 'task_categoryName', 'task_idCategory', 'category');

// update priority name
$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $availableTableColumns);
$reportCopyInfo->addCaseStructureColumn("task_priorityName",   "task_priorityCode",   (Array)$_LANG['tasks']['priorities']);
$reportCopyInfo->populate();

// update office name
$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $availableTableColumns);
$reportCopyInfo->populateSimple($DBTRd_offices, 'task_officeName', 'task_idOffice', "name");

// update department name
$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $availableTableColumns);
$reportCopyInfo->populateSimple($DBTRd_departments, 'task_departmentName', 'task_idDepartment', "name");

// update reponsible user name
$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $availableTableColumns);
$reportCopyInfo->populateSimple($DBTRd_users, 'task_userName', 'task_idUser', "CONCAT(joinTable.lastName, ' ', joinTable.firstName)");

// update assignor user name
$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $availableTableColumns);
$reportCopyInfo->populateSimple($DBTRd_users, 'task_assignorUserName', 'task_assignorIdUser', "CONCAT(joinTable.lastName, ' ', joinTable.firstName)");

// update add user name
$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $availableTableColumns);
$reportCopyInfo->populateSimple($DBTRd_users, 'task_addUserName', 'task_idAddUser', "CONCAT(joinTable.lastName, ' ', joinTable.firstName)");

// update update user name
$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $availableTableColumns);
$reportCopyInfo->populateSimple($DBTRd_users, 'task_updateUserName', 'task_idUpdateUser', "CONCAT(joinTable.lastName, ' ', joinTable.firstName)");

///////////////////////////////////////////////////////////////////////////////////////////////////
// UPDATE PIVOT DETAILS ///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
refreshReportStatus('extractMainDataFromDb', 'updatePivotDetails', 0);

// update pivot name
$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $availableTableColumns);
$reportCopyInfo->addCaseStructureColumn("pivot_name",   "pivot_code",   (Array)$_LANG['tasks']['pivotsArr']);
$reportCopyInfo->populate();

// update client name /////////////////////////////////////////////////////////////////////////////
if (in_array('pivot_clientName', $availableTableColumns)) {
	// pivotCode = corporate
	$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $availableTableColumns);
	$reportCopyInfo->addJoinTable($DBTRd_corporates,		'corp',			'rep.pivot_id=corp.id',	'LEFT');
	$reportCopyInfo->addStructureColumn("pivot_clientName", 	"corp.alias", 	"corp");
	$reportCopyInfo->addFilter("rep.pivot_code", "=", "corporate");
	$reportCopyInfo->populate();

	// pivotCode = invoice && idCorporate
	$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $availableTableColumns);
	$reportCopyInfo->addJoinTable($DBTRd_clientInvoices,	'inv',		'rep.pivot_id=inv.id',	'LEFT');
	$reportCopyInfo->addJoinTable($DBTRd_corporates,		'corp',		'inv.idCorporate=corp.id',	'LEFT');
	$reportCopyInfo->addStructureColumn("pivot_clientName", 	"corp.alias", 	"corp");
	$reportCopyInfo->addFilter("rep.pivot_code", "=", "invoice");
	$reportCopyInfo->addFilter("inv.idCorporate", "!=", "0");
	$reportCopyInfo->populate();

	// pivotCode = invoice && idCustomer
	$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $availableTableColumns);
	$reportCopyInfo->addJoinTable($DBTRd_clientInvoices,	'inv',		'rep.pivot_id=inv.id',	'LEFT');
	$reportCopyInfo->addJoinTable($DBTRd_customers,			'cust',		'inv.idCustomer=cust.id',	'LEFT');
	$reportCopyInfo->addStructureColumn("pivot_clientName", 	"CONCAT(cust.lastNameStd, ' ', cust.firstNameStd)", 	"cust");
	$reportCopyInfo->addFilter("rep.pivot_code", "=", "invoice");
	$reportCopyInfo->addFilter("inv.idCustomer", "!=", "0");
	$reportCopyInfo->populate();
}


