<?php

function cs_get_tasks($no){
    
    $sql = "SELECT task_id  
            FROM ".TB_PREF."project_tasks  
            WHERE task_id > ".db_escape($no)." 
            ORDER BY task_id  
            LIMIT 10000 ";
    
    return db_query($sql, "cs_get_updated_tasks");    
}

function cs_get_task_allocation_details($task_id){
    
    $sql = "SELECT *   
            FROM ".TB_PREF."task_cost_allocations  
            WHERE task_id = ".db_escape($task_id)." 
            ORDER BY id ";
    
    return db_query($sql, "cs_get_task_allocation_details");    
}


function cs_update_task_allocation_totals()
{
    $message = "==================Script Sarted==========================";
    write_custom_script_log("cs_update_task_allocation_totals", $message);
    
    $no = 30105;
    
    $opr_result = cs_get_tasks($no);
    $recs = db_num_rows($opr_result);
    write_custom_script_log("cs_update_task_allocation_totals", $recs." records selected");
    
    while($row = db_fetch($opr_result)){        
        cs_update_task_allocation_total($row['task_id']);        
    }
        
    $message = "------------------Script Completed----------------------------";
    write_custom_script_log("cs_update_task_allocation_totals", $message);
}


function cs_update_task_allocation_total($task_id){
    
//    $message = "Task: ".$task_id;
//    write_custom_script_log("cs_update_task_allocation_totals", $message);
        
    $task = get_task_record($task_id);
    $row_state = $task['row_state'];
    
    $total_allocation = 0;
    $pr_details = cs_get_task_allocation_details($task_id);
    
    begin_transaction();
    
    while ($myrow = db_fetch($pr_details))
    {
        if($myrow['amount'] < 0){
            cancel_transaction();
            $message = "Negative allocation found: ".$myrow['amount']." : Task: ".$task_id." : Description: ".$task['description'];
            write_custom_script_log("cs_update_task_allocation_totals", $message);
            return;
        }
        elseif($myrow['amount'] == 0){
            
        }
        else{
            $dd = 22;
        }
        $total_allocation += $myrow['amount'];
        $id = $myrow['id'];
//        cs_update_task_allocation_detail_total($id, $total_allocation);
    }

    if($total_allocation > 0 && $total_allocation != $task['budget_allocated']){
        $message = "Old Total Allocation: ".$task['budget_allocated'].": New Total Allocation: ".$total_allocation." : Task: ".$task_id." : Description: ".$task['description'];
        write_custom_script_log("cs_update_task_allocation_totals", $message);
    
    
        $utta_msg = cs_update_task_total_allocation($task_id, $row_state, $total_allocation);

        if($utta_msg['success'] !== true)
        {
            cancel_transaction();
            $message = "Task: ".$task_id." ".$utta_msg['message'];
            write_custom_script_log("cs_update_task_allocation_totals", $message);
            return array("success" => $utta_msg['success'], "tran_no" => $task_id, "message" => $message);        
        }
    }
    
//    cs_update_updated_task_completed($task_id);
    
    commit_transaction();
    
//    return array("success" => $utta_msg['success'], "message" => "");
    return array("success" => true, "message" => "");
}


function cs_update_updated_task_completed($task_id)
{
        
        $sql = "UPDATE ".TB_PREF."updated_tasks  
                SET completed = 1 
                WHERE task_id = ".db_escape($task_id); 
        
        $text = "cs_update_updated_task_completed";
    	db_query($sql, $text);

}


function cs_update_task_allocation_detail_total($id, $total_allocation)
{
        
        $sql = "UPDATE ".TB_PREF."task_cost_allocations 
                SET total_allocation = ".db_escape($total_allocation)." 
                WHERE id = ".db_escape($id); 
        
        $text = "cs_update_task_allocation_detail_total";
    	db_query($sql, $text);

}

function cs_update_task_total_allocation($task_id, $row_state, $total_allocation)
{
        
        $sql = "UPDATE ".TB_PREF."project_tasks  
                SET budget_allocated = ".db_escape($total_allocation).", 
                    row_state = ". db_new_row_state($row_state)." 
                WHERE task_id = ".db_escape($task_id)." 
                    AND row_state = ".db_escape($row_state); 
        
        $text = "cs_update_task_total_allocation";
    	db_query($sql, $text);
        
        $n = db_num_affected_rows();
        if($n == 1){
            return array("success" => true, "message" => "");
        }
        else{
            $message = $n." records updated";
            return array("success" => false, "tran_no" => $task_id, "message" => $message);
        }

}



