<?php
/**********************************************************************
***********************************************************************/

function add_project_cost_adjustment($project_id, $task_id, 
                                    $amount, $deduction,
                                    $ref_tran_type, $ref_tran_no,  
                                    $tran_date, $comment)
{
    if($deduction){
        $amount = $amount * -1;
    }

    begin_transaction();
    
    $pca_no = add_project_cost_adjustment_header($project_id, $task_id, 
                                                $amount, $deduction,
                                                $ref_tran_type, $ref_tran_no,
                                                $tran_date, $comment);
     
    $current_user_id = $_SESSION['wa_current_user']->user;
    
    $task_costs_summary = array();
    
    add_to_task_cost($task_costs_summary, $task_id, $amount, $amount, $amount);
        
    get_task_cost_details($task_costs_summary);
    
//    $task_costs_summary = calculate_task_remaining_totals($mr_details_array);


//    add_to_task_cost($task_costs_summary, $iou['task_id'], $iou['amount'], $iou['amount'], $iou['amount']);
        
    $cct_msg = charge_cost_to_tasks(ST_PROJECT_COST_ADJUST, $pca_no, 
                    $task_costs_summary, $current_user_id);

    if($cct_msg['success'] !== true)
    {
        cancel_transaction();
        return array("success" => $cct_msg['success'], "tran_no" => $pca_no, "message" => $cct_msg['message']);
    }
        

    
    commit_transaction();
    return array("success" => true, "tran_no" => $pca_no, "message" => $cct_msg['message']);
}


//------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------

function add_project_cost_adjustment_header($project_id, $task_id, 
                                                $amount, $deduction,
                                                $ref_tran_type, $ref_tran_no,
                                                $tran_date, $comment)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');

    $sql = "INSERT INTO ".TB_PREF."task_cost_adjustments (
                                    project_id, task_id, 
                                    amount, deduction,
                                    ref_tran_type, ref_tran_no,
                                    tran_date, comment,
                                    added_by,added_on) 
            VALUES (".db_escape($project_id).", ".db_escape($task_id).", "
                    .db_escape($amount).", ".db_escape($deduction).", "
                    .db_escape($ref_tran_type).", ".db_escape($ref_tran_no).", "
                    .db_escape($tran_date).", ".db_escape($comment).", "
                    .db_escape($id_user).", ".db_escape($datetime).")";

    db_query($sql,"adjustment could not be added");
    
     $id = db_insert_id();
     return $id;
}

?>