<?php
///////////////////////////////////////////////////////////////////////////////////////////////////
// CREATE SQL FOR COPYING TASKS LIST //////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// refresh report status
refreshReportStatus('extractMainDataFromDb', 'runningMainSQL', 0);
$reportCopyInfo = new \App\Reports\Db\ManageRecords\Insert($reportTable);
$reportCopyInfo->setMainTable($DBTRd_tasks,			'task');

$cols = [
	'task_id'					=>'task.id',
	'task_statusCode'			=>'task.status',
	'task_statusDescription'	=>'task.statusDescription',
	'task_statusReasonCode'		=>'task.statusReason',
	'task_date'					=>'task.taskDate',
	'task_description'			=>'task.description',

	'task_idUser'			=>'task.idResponsibleUser',
	'task_idDepartment'		=>'task.idDepartment',
	'task_idOffice'			=>'task.idOffice',
	'task_assignorIdUser'	=>'task.idAssignorUser',

	'task_idOperationType'	=>'task.idOperationType',
	'task_idCategory'		=>'task.idCategory',
	'task_priorityCode'		=>'task.priority',

	'task_comments'					=>'task.comments',
	'task_free'						=>'task.free',
	'task_deadlineTime'				=>'task.deadline',
	'task_estimatedFinishTime'		=>'task.estimatedFinishTime',

	'pivot_code'			=>'task.pivot',
	'pivot_id'				=>'task.idPivot',

	'task_idAddUser'			=>'task.idAddUser',
	'task_idUpdateUser'			=>'task.idUpdateUser',
	'task_addTime'				=>'task.addTime',
	'task_lastUpdateTime'		=>'task.lastUpdateTime',
	'task_updatesCount'			=>'task.updatesCount',
];

$reportCopyInfo->setStructureColumns($cols);

$reportCopyInfo->addStructureColumn("task_dateYM",						"DATE_FORMAT(task.taskDate, '%Y-%m-01')",    	"task");
$reportCopyInfo->addStructureColumn("task_dateY",						"DATE_FORMAT(task.taskDate, '%Y-01-01')",    	"task");
$reportCopyInfo->addStructureColumn("task_deadlineDate",				"DATE_FORMAT(task.deadline, '%Y-%m-%d')",    	"task");
$reportCopyInfo->addStructureColumn("task_estimatedDurationMinutes",	"task.estimatedDuration/60",    				"task");

$reportCopyInfo->setActiveColumns($availableTableColumns);

// ADD FILTERS ////////////////////////////////////////////////////////////////////////////////////
// task date
if ($reportFilters['taskDate']['start']) {
	$reportCopyInfo->addFilter('task.taskDate', '>=', $reportFilters['taskDate']['start']);
}
if ($reportFilters['taskDate']['end']) {
	$reportCopyInfo->addFilter('task.taskDate', '<=', $reportFilters['taskDate']['end']);
}

// task status
if ($reportFilters['taskStatus']) {
	$reportCopyInfo->addFilter('task.status', 'IN', $reportFilters['taskStatus']);
}

// office
if ($reportFilters['idOffice']) {
	$reportCopyInfo->addFilter('task.idOffice', '=', $reportFilters['idOffice']);
}

// department
if ($reportFilters['idDepartment']) {
	$reportCopyInfo->addFilter('task.idDepartment', '=', $reportFilters['idDepartment']);
}

// responsible
if ($reportFilters['idResponsibleUser']) {
	$reportCopyInfo->addFilter('task.idResponsibleUser', '=', $reportFilters['idResponsibleUser']);
}

// assignor
if ($reportFilters['idAssignorUser']) {
	$reportCopyInfo->addFilter('task.idAssignorUser', '=', $reportFilters['idAssignorUser']);
}

// filter deleted suppliers as the deleted ones can also be found in rd tables
$reportCopyInfo->addFilter('task.deleted', '=', 0);

$reportCopyInfo->populate();
