<?php
chdir(dirname(__FILE__));
define("CONSOLE_MODE", true);
define('PUBLIC_FOLDER', 'public');
include "./init.php";
header("Content-type: text/plain");
ini_set('memory_limit', '4G');

ini_set("log_errors", 1);
ini_set("error_log", dirname(__FILE__)."/tmp/php-error.log");

// login with superadmin
$admin = Contacts::findOne(array('conditions' => 'user_type = 1'));
CompanyWebsite::instance()->logUserIn($admin);

// Get helper functions
Env::useHelper('dimension');
Env::useHelper('functions', 'advanced_billing');
Env::useHelper('persisting_member_totals_functions', 'advanced_billing');

function has_parent_with_quoted_price($task, $parent_id = null) {
    if(($task instanceof ProjectTask && $task->getParentId() == 0) && ($parent_id == null || $parent_id == 0)) {
        return false;
    }

    if($task instanceof ProjectTask && $task->getParentId() != 0) {
        $parents = getAllParents($task);
    } else {
        $parents = array(ProjectTasks::instance()->findById($parent_id));
        $more_parents = getAllParents($parents[0]);
        $parents = array_merge($parents, $more_parents);
    }
    foreach($parents as $parent){
        $is_calculated_estimated_price = $parent->getColumnValue('is_calculated_estimated_price');
        if($is_calculated_estimated_price == 0) return true;
    }

    return false;
}

/**
 * Return all parent tasks that this task belongs to
 *
 * @param void
 * @return array of ProjectTasks objects or empty array
 */
function getAllParents($task) {
    $parents = array();
    $current_task = $task;
    while($current_task->getParentId()!=0) {
        $parent = ProjectTasks::instance()->findById($current_task->getParentId());
        if($parent instanceof ProjectTask) {
            $parents[] = $parent;
            $current_task = $parent;
        } else {
            break;
        }
    }
    return $parents;
} // getAllParents

// get the tasks
$tasks = ProjectTasks::instance()->findAll(array(
    "order" => "`depth` DESC"
));

$total_task_count = count($tasks);

ECHO "TOTAL TASKS: ".$total_task_count."\n";

// Update tasks that have parent with quoted price
// Collect task ids to update
$task_ids_to_update = array();
$task_ids_estimated_price_to_update = array();
$task_ids_is_calculated_estimated_price_to_update = array();
foreach ($tasks as $task) {
    $has_parent_with_quoted_price = has_parent_with_quoted_price($task);
    $estimated_price = $task->getColumnValue('estimated_price');
    $is_calculated_estimated_price = $task->getColumnValue('is_calculated_estimated_price');
    if ($has_parent_with_quoted_price) {
        $task_ids_to_update[] = $task->getId();
        if($estimated_price > 0) {
            $task_ids_estimated_price_to_update[] = $task->getId();
        }
        if($is_calculated_estimated_price == 0 && $estimated_price > 0) {
            $task_ids_is_calculated_estimated_price_to_update[] = $task->getId();
        }
    }
}

$count_tasks = count($task_ids_to_update);

ECHO "SUBTASKS TO UPDATE: ".$count_tasks."\n";
//ECHO "SUBTASKS WITH ESTIMATED PRICE  > ZERO: ".count($task_ids_estimated_price_to_update)."\n";
ECHO "SUBTASKS THAT HAVE QUOTED PRICE PARENT AND ITSELF HAS QUOTED PRICE && ESTIMATED PRICE > 0: ".count($task_ids_is_calculated_estimated_price_to_update)."\n";


