<?php

function display_job_cost_addition_header($type, $trans_no) {
    global $Refs;
    
//    if (isset($_POST['NewIssue']) || isset($_POST['ModifyIssue'])) {
//        $type = ST_DAILY_ITEM_ISSUE;
//    } 
    
        if ($trans_no > 0) {

            $header = get_job_cost_addition_header_type_trans_no($type, $trans_no);
            $_POST['project_id'] = $header['project_id'];
            hidden('project_id', $header['project_id']);
            hidden('site_id', $header['project_site_id']);

            $project_id = $header['project_id'];
            $user_id = $_SESSION['wa_current_user']->user;
            $cap = can_access_project($project_id, $user_id);
                if($cap['success'] !== true) {
                    display_error($cap["message"]);
                    set_focus('project_id');
                    display_footer_exit();
                    exit;
                }  

            if (! isset($_POST['reference'])) {
                $_POST['reference'] = $header['reference'];
            }

            if (! isset($_POST['comments'])){
                $_POST['comments'] = $header['comments'];
            }        


            if (! isset($_POST['tran_date'])) {
                $_POST['tran_date'] = sql2date($header['tran_date']);
            }

            if (! isset($_POST['project_description'])) {
                $_POST['project_description'] = $header['project_description'];
            }

            if (! isset($_POST['site_description'])) {
                $_POST['site_description'] = $header['site_description'];
            }
            
            
            
            if (! isset($_POST['approval_status'])) {
                $_POST['approval_status'] = $header['approval_status'];
            }

        } else {
            $_POST['reference'] = $Refs->get_next($type);            
            $_POST['approval_status'] = 0;
        }
        
        div_start('nsit_header');
  
            start_outer_table(TABLESTYLE2, "width=65%"); // outer table
                start_table(TABLESTYLE2);  
                    table_section(1, "50%");

                        if ($trans_no > 0) {
//                            $pd_request = get_job_cost_addition_header($dii_no);
                            label_row("Trans #:", $header['trans_no']);  
                            $_POST['trans_no'] = $trans_no;
                            hidden('trans_no', $_POST['trans_no']);
                            
                            $reference = $header['reference'];            
                            text_row("Reference:", 'reference', $reference, null, null);            
                        } else {            
                            text_row("Reference:", 'reference', $Refs->get_next($type), null, null);   
                        }

                        if ($trans_no > 0) {
                            label_row("Project:", $_POST['project_description']);   
                            label_row("Job:", $_POST['site_description']);   
                        } else {
                            project_list_row(_("Project:"), 'project_id', null, _('Select a project'), true);   
                            project_sites_list_row("Project Site:", 'site_id', $_POST['project_id'], null, '', false); 
                        } 
                        
                        
                        

                    table_section(2, "50%");
                        date_row("Date:", "tran_date");
                        
                        if ($trans_no > 0) {
                            $header = get_job_cost_addition_header_record_type_trans_no($type, $trans_no);
                                $type = $header['type']; 
 
                        } else {
                            

                        }


                        textarea_row("Comment:", 'comments', null, 35, 2); 

                        global $approval_statuses;
                        $s = get_project_trans_view_str(ST_APPROVAL_LOG, $trans_no."&source_type_id=".$type, "Approval Status");
                        label_row($s, $approval_statuses[$_POST['approval_status']]);

                        
                end_table();
            end_outer_table(2); // outer table
    
        div_end();
}

function display_job_cost_addition_details($type, $trans_no) 
{ 
    global $Ajax, $daily_item_types_array;
    
    display_heading("Details");
    
//    $trans_no = $type = -1;
    $header = get_job_cost_addition_header_record_type_trans_no($type, $trans_no);
        if (is_array($header)) {
            $trans_no = $header['trans_no'];
            $type = $header['type'];
        } 
    
    $Ajax->activate('nsit_details');
    div_start('nsit_details');
    
        start_table(TABLESTYLE, "width=60%");

            $th = array(_("ID"), _("Description"), _("Amount"), _('Line Comment'), _(""), _(""));  
            table_header($th);

            $k = 0; //row colour counter
                $result = get_job_cost_addition_details($trans_no, $type);      
                    while ($row = db_fetch($result)) {
                        alt_table_row_color($k);

                            label_cell($row['trans_id'], 'align=center');
                            label_cell($row["item_description"]); 

                            label_cell(number_format($row["cost"], 2), 'align=right');
                                     
                            label_cell(nl2br($row["line_comment"]));          

                            edit_button_cell("Edit".$row["trans_id"], _("Edit"));    
                            delete_button_cell("Delete".$row["trans_id"], _("Delete"));  

                        end_row();        
                    }

        end_table(3);
    
    div_end();
}

function display_job_cost_addition_details_edit_controls($selected_id, &$Mode) {
    global $Ajax, $daily_item_types_array;
    $dec = 2;

    if ($selected_id > 0 && $Mode != 'Delete') {   
        $row = get_job_cost_addition_details_record($selected_id); 
            if ($Mode == 'Edit') {
                $_POST['item_description'] = $row["item_description"];
                $_POST['cost'] = $row["cost"];
                $_POST['line_comment'] = $row["line_comment"];
            }
 
            if (! (strlen($_POST['line_comment']) > 0)) {
                $_POST['line_comment'] = $row["line_comment"];
            }
    }
  
    $Ajax->activate("control_items");    
    div_start('control_items');
        start_table(TABLESTYLE2);
        
            if ($selected_id > 0 && $Mode != 'Delete') {  
                label_row('Rec Id:', $selected_id);
            } else {
                
            }
            

 

            text_row_ex('Description:', 'item_description', 50, 250, 'Item Description');

            amount_row("Cost:", 'cost', null, null, null, 0);
            
            
            
//            text_row_ex('Line Comment:', 'line_comment', 50, 400, 'Line Comment');
            textarea_row("Comment:", 'line_comment', null, 35, 2); 

        end_table(2);
    div_end();
}

//function build_all_item_units_array() {
//    $units_array = array();
//    
//        $result = get_all_item_units_units();
//            while ($row = db_fetch($result)) {
//                $units_array[$row['unit_code']] = $row['unit_name'];
//            }
//            
//        return $units_array;
//}

function can_process_job_cost_addition_header($type, $trans_no) {
    global $Refs;
    
//    if (isset($_POST['NewIssue']) || isset($_POST['ModifyIssue'])) {
//        $type = ST_DAILY_ITEM_ISSUE;
//    } else {
//        $set_focus = '';
//        $message = ("Invalid transaction type.");
//        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
//    }
    
    $header = get_job_cost_addition_header_record_type_trans_no($type, $trans_no);
      
    if (is_array($header)) {
        if (! ($header['approval_status'] == AS_NEW || $header['approval_status'] == AS_WITH_THE_APPROVER || 
               $header['approval_status'] == AS_AWAITING_APPROVAL || $header['approval_status'] == AS_BACK_TO_ORIGINATOR)) {
        
            $set_focus = '';
            $message = ("This document status does not allow further updating.");
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
        }
    } 
    
    if (! $Refs->is_valid($_POST['reference'])) {
        $message = ("Please enter a reference.");
        $set_focus = 'reference';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);      
    }    
 
    $ref_msg = reference_exists($_POST['reference'], $type, $trans_no);
        if ($ref_msg) {
            $message = $ref_msg;
            $set_focus = 'reference';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);       
        }
      
    if (! ($trans_no > 0)) {
        if (! ($_POST['project_id'] > 0)) {
            $message = "Please select a Project.";
            $set_focus = 'project_id';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
        }
    }
    
    if (! is_date($_POST['tran_date'])) {
        $message = "Date entered is not a valid date.";
        $set_focus = 'tran_date';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);        
    }

        
    $row_state = 0;
    if ($trans_no > 0) {
        $row_state = $header['row_state'];
    }
    
    return array("success" => true, 'row_state' => $row_state, "message" => "");    
}

function can_process_job_cost_addition_line() {

    


        if (! (strlen($_POST['item_description']) > 0)) {
            $message = "description cannot be empty.";
            $set_focus = 'item_description';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);  
        }
       

        if (! (check_num('cost', 0))) {
            $message = "Cost should be greater than zero.";
            $set_focus = 'item_cost';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);  
        }
    

        return array("success" => true, "focus_to" => '', "message" => "");  
}

function can_delete_job_cost_addition_line($selected_id) {
    
    $detail = get_job_cost_addition_details_record($selected_id);
    $header = get_job_cost_addition_header_record_type_trans_no($detail['type'], $detail['trans_no']);
   
    if (is_array($header)) {
        if (! ($header['approval_status'] == AS_NEW || $header['approval_status'] == AS_WITH_THE_APPROVER || 
                $header['approval_status'] == AS_AWAITING_APPROVAL || $header['approval_status'] == AS_BACK_TO_ORIGINATOR)){
            $message = ("This document status does not allow further updating.");
            $set_focus = '';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
        }
    } 
    
    $lines_count = get_job_cost_addition_details_lines_count($header['trans_no'], $header['type']);
        if (! ($lines_count > 1)) {
            $message = ("This is the last line. Can not delete this line.");
            $set_focus = '';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
        }  
    
    return array("success" => true, "focus_to" => '', "message" => "");       
}

function save_job_cost_addition_header($type, &$trans_no) {
    global $systypes_array;
      
    
    write_job_cost_addition_header($type, $trans_no, $_POST['reference'], $_POST['project_id'],  
                                            $_POST['site_id'], $_POST['comments'], $_POST['tran_date']); 
    
        if ($trans_no > 0) {
            set_session_variable($type."_no", $trans_no);
            $_POST['JCA_No'] = $trans_no;
            return array("success" => true, 'trans_no' => $trans_no, "message" => $systypes_array[$type]." header #$trans_no has been saved.");
        } else {
            return array("success" => false, 'trans_no' => '', "message" => "Could not be saved ".$systypes_array[$type].' header.');
        }
}

function save_job_cost_addition_line($type, $trans_no, $selected_id) {    
    global $systypes_array;
    
    $header = get_job_cost_addition_header_record_type_trans_no($type, $trans_no);
//    $trans_no = $header['trans_no'];
//    $type = $header['type'];
    
    $af_no = write_job_cost_addition_line($selected_id, $type, $trans_no, $_POST['item_description'], 
                                            input_num('cost'), $_POST['line_comment']); 
    
        if ($af_no > 0) {
            update_job_cost_addition_header_total($type, $trans_no);
            return array("success" => true, 'trans_no' => $trans_no, "message" => $systypes_array[$header['type']]." line saved.");
        } else {
            if ($selected_id > 0) {
                $ln_ms = $systypes_array[$header['type']]." #$selected_id line could not be updated.";
            } else {
                $ln_ms = $systypes_array[$header['type']]." line could not be saved.";
            }

                return array("success" => false, 'trans_no' => $trans_no, "message" => $ln_ms);
        }
}

function save_job_cost_addition($type, &$trans_no, $selected_id) {
    global $systypes_array;
    
//    if (isset($_POST['NewIssue']) || isset($_POST['ModifyIssue'])) {
//        $type = ST_JOB_COST_ADDITION;
//    } 
    
    $hd_msg = can_process_job_cost_addition_header($type, $trans_no);   
        if ($hd_msg["success"] !== true) {          
            if (isset($hd_msg["focus_to"])) {
                set_focus($hd_msg["focus_to"]);            
            }
            
            return array("success" => false, "message" => $hd_msg["message"]);
        }
    
    $ln_msg = can_process_job_cost_addition_line();
    if ($ln_msg['success'] !== true) {
        if (isset($ln_msg["focus_to"])) {
            set_focus($ln_msg["focus_to"]);            
        }

        return array("success" => false, 'trans_no' => $trans_no, "message" => $ln_msg['message']);        
    }
       
    begin_transaction();

        $header_msg = save_job_cost_addition_header($type, $trans_no);
            if ($header_msg["success"] !== true) {
                cancel_transaction();
                return array("success" => false, 'trans_no' => $trans_no, "message" => $header_msg['message']);
            }

        $line_msg = save_job_cost_addition_line($type, $trans_no, $selected_id);
            if ($line_msg['success'] !== true) {
                cancel_transaction();
                return array("success" => false, 'trans_no' => $trans_no, "message" => $line_msg['message']);
            }

    commit_transaction();

    return array("success" => true, 'trans_no' => $trans_no, "message" => $systypes_array[$type]." #$trans_no has been saved.");
}

function delete_job_cost_addition_line($selected_id) {
    
    $cd_msg = can_delete_job_cost_addition_line($selected_id);
        if ($cd_msg['success'] !== true) {
            return $cd_msg;
        }
        
        $detail = get_job_cost_addition_details_record($selected_id);
        $header = get_job_cost_addition_header_record_type_trans_no($detail['type'], $detail['trans_no']); 
        
        begin_transaction();
            delete_job_cost_addition_details_record($selected_id);
            update_job_cost_addition_header_total($header['type'], $header['trans_no']);
        commit_transaction();

    return array("success" => true, 'trans_no' => $header['trans_no'], "message" => "Record #$selected_id has been deleted.");
}

function save_job_cost_addition_header_only($type, $trans_no) {
    $hd_msg = can_process_job_cost_addition_header($type, $trans_no);   
        if ($hd_msg["success"] !== true) {          
            if (isset($hd_msg["focus_to"])) {
                set_focus($hd_msg["focus_to"]);            
            }
            
            return array("success" => false, "message" => $hd_msg["message"]);
        }
    
        begin_transaction();
        
        $msg = save_job_cost_addition_header($type, $trans_no);
        if ($msg["success"] !== true) {          
            cancel_transaction();
        }else{
            commit_transaction();
        } 

        return $msg;
                
}

function approve_job_cost_addition($trans_no, $type, $tran_date) {
    $tran_date_ = sql2date($tran_date);
    
    $jc_deduction_h = get_job_cost_addition_header_record_type_trans_no($type, $trans_no);
    $result = get_job_cost_addition_details($trans_no, $type);      
        while ($row = db_fetch($result)) {
            $qty = -$row["qty"];
            $tran_qty = -$row["tran_qty"];
            $cost = $row["cost"];
            
            if ($row['daily_item_type'] == NSIT_STOCK_ITEM) {
                add_stock_adjustment_item($trans_no, $row["stock_id"], $row["loc_code"], $tran_date_, $row["reference"], $qty, $cost, '', 
                                          $row["tran_unit"], $tran_qty, $row["conversion_factor"], $type, $row["task_id"], $row["project_site_id"], 
                                          $row["trans_id"], $jc_deduction_h['project_id']);
            } else {
                add_non_stock_move($type, $row["stock_id"], $trans_no, $row["loc_code"], $tran_date_, $row["reference"], $qty, $cost, 
                                   $row["person_id"], $row["visible"], $row["price"], $row["discount_percent"], $row["tran_unit"], $tran_qty, 
                                   $row["conversion_factor"], $row["task_id"], $row["project_site_id"], $row["trans_id"], 
                                    $row["item_description"], $jc_deduction_h['project_id']);
            }
        }  
        
            return array("success" => true, 'trans_no' => $trans_no, "message" => "");
}

function clear_job_cost_addition_details() {

    unset($_POST['item_description']);
    unset($_POST['cost']);
    unset($_POST['line_comment']);

}

function view_job_cost_addition_header($type, $trans_no) {
    global $document_approval_statuses;
    
    $row = get_job_cost_addition_header_type_trans_no($type, $trans_no);
    
        start_outer_table(TABLESTYLE2, "width=60%");

            table_section(1, "50%");
                label_row(_("Reference:"), $row['reference'], "class='tableheader2'");
                label_row(_("Porject:"), $row['project_description'], "class='tableheader2'");
                label_row(_("Date:"), sql2date($row['tran_date']), "class='tableheader2'");

            table_section(2, "50%");
                label_row(_("Location:"), $row['location_name'], "class='tableheader2'");
                label_row(_("Approval Status:"), $document_approval_statuses[$row['approval_status']], "class='tableheader2'");

        end_outer_table(2);

        if (strlen($row['comments']) > 0) {
            start_table(TABLESTYLE2);
                label_row(_("Comment:"), nl2br($row['comments']), "class='tableheader2'");
            end_table(3);
        }
}

function view_job_cost_addition_details($type, $trans_no) {
    global $daily_item_types_array;
    
    $header = get_job_cost_addition_header_type_trans_no($type, $trans_no);
    
    start_table(TABLESTYLE, "width=98%");

        $th = array(_("ID"), _("Item Type"), _("Description"), _("Unit"), _("Issued Qty"), _("Returned Qty"), _('Budget'), _('Cost'), _('Person'), 
                    _('Job'), _('Comment'));  
        table_header($th);

        $k = 0; //row colour counter
            $result = get_job_cost_addition_details($trans_no, $type);      
                while ($row = db_fetch($result)) {
                    alt_table_row_color($k);

                        label_cell($row['trans_id'], 'align=center');
                        label_cell($daily_item_types_array[$row['daily_item_type']]);
                        if ($row['daily_item_type'] == NSIT_NON_STOCK_ITEM || $row['daily_item_type'] == NSIT_STOCK_ITEM) {
                            label_cell($row["stock_id"]." - ".$row["item_description"]);  
                        } else {
                            label_cell($row["item_description"]);
                        }
                        label_cell($row["unit_name"]);
                        label_cell(number_format($row["tran_qty"], 2), 'align=right');
                        label_cell(number_format($row["returned_quantity"] / $row["conversion_factor"], 2), 'align=right');
                        
                        label_cell($row["task_description"]);
                        $tran_unit_cost = $row["conversion_factor"] * $row["cost"];
                            if ($tran_unit_cost > 0) {
                                label_cell(number_format($tran_unit_cost, 2), 'align=right');
                            } else {
                                label_cell('');
                            }
                            
                        $lf_name = get_movement_type_linked_field_values($header['linked_field_type'], $row['linked_field_id'], $header['group_company_code']);
                            label_cell($lf_name["value"]); 
                        
                        label_cell($row["site_description"]);          
                        label_cell(nl2br($row["line_comment"]));          

                    end_row();        
                }

    end_table(3);
}

function view_daily_item_return_headers_by_request_trans_no($trans_no) {
    $result = get_daily_item_return_headers_by_request_trans_no($trans_no);   
        if (! (db_num_rows($result) > 0)) {
            return;
        }
    
    global $systypes_array;
    
    display_heading("Returns for this ".$systypes_array[ST_DAILY_ITEM_ISSUE]);
    
        start_table(TABLESTYLE, "width=40%");

            $th = array(_("Trans #"), _("Trans Date"), _("Location"));  
            table_header($th);

            $k = 0; //row colour counter
                while ($row = db_fetch($result)) {
                    alt_table_row_color($k);
                        label_cell(get_inventory_trans_view_str($row['type'], $row['trans_no']), 'align=center');
                        label_cell(sql2date($row["tran_date"]), 'align=center');
                        label_cell($row['location_name']);
                    end_row();        
                }

        end_table(3);
}


function set_job_cost_addition_loading_parameteres($type, &$trans_no, &$dii_no,&$page_title)
{
    global $systypes_array;
    
    if (isset($_GET['NewIssue']) || isset($_POST['NewIssue'])) {
        $_POST['NewIssue'] = isset($_GET['NewIssue']) ? $_GET['NewIssue'] : $_POST['NewIssue'];
        $dii_no = $_POST['NewIssue'];
        $trans_no = -1;

        $page_title = _($systypes_array[$type]); 

    } elseif (isset($_GET['ModifyIssue']) || isset($_POST['ModifyIssue'])) {
        $_POST['ModifyIssue'] = isset($_GET['ModifyIssue']) ? $_GET['ModifyIssue'] : $_POST['ModifyIssue'];
        $dii_no = $_POST['ModifyIssue'];

        $header = get_job_cost_addition_header_record($dii_no);
            if (is_array($header)) {
//                if ($header['type'] != $type) {
//                    page(_($help_context = ''), false, false, "", $js);
//                    display_error(("Invalid transaction type."));
//                    display_footer_exit();
//                } 

                $trans_no = $header['trans_no'];
            } else {
//                page(_($help_context = ''), false, false, "", $js);
//                display_error(("Invalid transaction ID."));
//                display_footer_exit();
            }

        $page_title = _('Modify '.$systypes_array[$type]." #$trans_no" );

    } elseif (isset($_GET['type']) && isset($_GET['ApprovedID'])) {
        $page_title = _($systypes_array[$_GET['type']]);
    } else {
//        page(_($help_context = ''), false, false, "", $js);
//        display_error(("Invalid transaction type."));
//        display_footer_exit();
    }
}


function handle_save_job_cost_addition_header($type, $trans_no, &$Mode) {
    $msg = save_job_cost_addition_header_only($type, $trans_no);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            $_POST['JCA_No'] = $trans_no;
            display_notification($msg['message']);
            $Mode = 'RESET';
        }
}

function handle_save_job_cost_addition($type, &$trans_no, &$selected_id, &$Mode){
    
    $msg = save_job_cost_addition($type, $trans_no, $selected_id);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            $dii_no = $msg['trans_no'];
            $_POST['JCA_No'] = $trans_no;
            
            $_POST['JCA_No'] = $trans_no;
            set_session_variable($type."_no", $_POST['JCA_No']);
            display_notification($msg['message']);
            clear_job_cost_addition_details();
            $Mode = 'RESET';
        }
}

function handle_delete_job_cost_addition_line($type, &$selected_id, &$Mode)
{
    $msg = delete_job_cost_addition_line($selected_id);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            clear_job_cost_addition_details();
            $Mode = 'RESET';
        }
}



function can_release_job_cost_addition($type, $trans_no)
{
    $line_count = get_job_cost_addition_details_lines_count($trans_no, $type);
    if (!($line_count > 0))
    {
        display_error("There are no lines entered. Can not release this document.");
        return false;
    }
    
    return true;
}


function release_job_cost_addition_for_approval($type, $trans_no)
{    
    if(!can_release_job_cost_addition($type, $trans_no)){
        return;
    }    
            
    $h_msg = can_process_job_cost_addition_header($type, $trans_no);
    if($h_msg["success"] !== true) 
    {          
        if(isset($h_msg["focus_to"])){
            set_focus($h_msg["focus_to"]);
        }
        display_error($h_msg["message"]);
        return;
    }
    
    begin_transaction();

    $msg = save_job_cost_addition_header($trans_no, $type);
    if($msg["success"] !== true){
        cancel_transaction();
        display_error($msg["message"]);
        return;
    }
    
    $header = get_job_cost_addition_header_record_type_trans_no($type, $trans_no);
    $current_user_id = $_SESSION['wa_current_user']->user;
    
    $task_costs = $header['total'];
    $info = approve_document($type, $trans_no, $header['project_id'], $task_costs, 
                                    $header['next_approval_group_id'], $current_user_id, $header['row_state'], $header['created_by']);
        
    if($info['success'] !== true){
//        return array("success" => $info['success'],"trans_no" => $trans_no, "message" => $info['message']);
        display_error($info["message"]);
        return;
    }
    
//    if($info['approval_status'] != AS_APPROVED){
//        return array("success" => $info['success'], "trans_no" => $trans_no, "message" => $info['message']);
//    }   
    

    commit_transaction();
    set_session_variable($type."_messages", $info['message']);
    meta_forward($_SERVER['PHP_SELF'], "type=$type&Processed_JC_No=$trans_no");
    
    // meta forward
}

function send_job_cost_addition_back_to_originator($type, $trans_no)
{        
    begin_transaction();
     
    $header = get_job_cost_addition_header_record_type_trans_no($type, $trans_no);

    $current_user_id = $_SESSION['wa_current_user']->user;

    $sbdo_msg = send_back_to_document_originator($type, $trans_no, $header['project_id'],
                                    $header['next_approval_group_id'], $current_user_id, $header['row_state'], 
                                    $header['created_by']);
    
    if($sbdo_msg['success'] !== true){
        display_error_cancel($sbdo_msg['message']);
        return;
    }

    commit_transaction();
    set_session_variable($type."_messages", $sbdo_msg['message']);
    meta_forward($_SERVER['PHP_SELF'], "type=$type&Processed_JC_No=$trans_no");
        
}


function reject_job_cost_addition($type, $trans_no)
{
    begin_transaction();
    $header = get_job_cost_addition_header_record_type_trans_no($type, $trans_no);

    $current_user_id = $_SESSION['wa_current_user']->user;
    
    $rj_msg = reject_document($type, $trans_no, $header['project_id'],
                    $header['next_approval_group_id'], $current_user_id, $header['row_state']);
    
    if($rj_msg['success'] !== true){
        display_error_cancel($rj_msg['message']);
        return;
    }

    commit_transaction(); 
    set_session_variable($type."_messages", $rj_msg['message']);
    meta_forward($_SERVER['PHP_SELF'], "type=$type&Processed_JC_No=$trans_no");
    

}

