<?php

function write_job_cost_addition_header($type, &$trans_no, $reference, $project_id, 
                                       $site_id, $comments, $date) {
    global $Refs;
     
        if ($trans_no > 0) {
            $af_row = update_job_cost_addition_header($type, $trans_no, $reference, $comments, $date);
                if (! ($af_row > 0)) {
                    return 0;
                }
        } else {
            $trans_no = get_next_trans_no($type);
            $dii_no = addj_ob_cost_addition_header($type, $trans_no, $reference, $project_id, $site_id, $comments, $date);
        }
    
        $Refs->save($type, $trans_no, $reference);

        if (! ($trans_no > 0)) {
            return 0;
        }

        return $trans_no;
}

function write_job_cost_addition_header_by_type_trans_no($type, $trans_no, $reference, $project_id, $movement_type, $linked_field, $linked_field_id, 
                                                        $group_company_code, $location, $comments, $date, $source_id=0) {
    global $Refs;
     
        if ($trans_no > 0) {
            $af_row = update_job_cost_addition_header_by_type_trans_no($type, $trans_no, $reference, $group_company_code, $movement_type, $linked_field, 
                                                                      $linked_field_id, $location, $comments, $date);
                if (! ($af_row > 0)) {
                    return 0;
                }
        } else {
            $trans_no = get_next_trans_no($type);
            $inser_id = insert_job_cost_addition_header($type, $trans_no, $reference, $project_id, $group_company_code, $movement_type, $linked_field, 
                                                       $linked_field_id, $location, $comments, $date, $source_id);
                if (! ($inser_id > 0)) {
                    return 0;
                }
        }
    
        $Refs->save($type, $trans_no, $reference);

        return $trans_no;
}
     
function addj_ob_cost_addition_header($type, $trans_no, $reference, $project_id, $site_id, $comments, $date) {
    
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
        $sql = "INSERT INTO ".TB_PREF."job_cost_additions
                      ( type, trans_no, reference, project_id,  
                        project_site_id, comments, 
                        tran_date, created_by, created_on)         
                VALUES( ".db_escape($type).", ".db_escape($trans_no).", ".db_escape($reference).", ".db_escape($project_id).",  
                        ".db_escape($site_id).", ".db_escape($comments).", 
                        ".db_escape(date2sql($date)).", ".db_escape($created_by).", ".db_escape($created_on).")";

            db_query($sql, 'addj_ob_cost_addition_header');
            return db_insert_id();        
}

function update_job_cost_addition_header($type, $trans_no, $reference, $comments, $date) {   
    
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."job_cost_additions
                SET reference = ".db_escape($reference).",                      
                    tran_date = ".db_escape(date2sql($date)).",
                    comments = ".db_escape($comments).",  
                    updated_by = ".db_escape($updated_by).", 
                    updated_on = ".db_escape($updated_on)." 
                WHERE type = ".db_escape($type)." AND  trans_no = ".db_escape($trans_no); 

            db_query($sql, "update_job_cost_addition_header");
            return db_num_affected_rows();
}


function write_job_cost_addition_line($selected_id, $type, $trans_no, $item_description, 
                                     $cost, $line_comment) {
    if ($selected_id > 0) {
        return update_job_cost_addition_line($selected_id, $item_description, $cost, $line_comment);
    } else {
        return add_job_cost_addition_line($trans_no, $type, $item_description, $cost, $line_comment);
    }
    
    
}

function update_job_cost_addition_line($selected_id, $item_description, $cost, $line_comment) {
    
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."job_cost_addition_details
                SET item_description = ".db_escape($item_description).", 
                    cost = ".db_escape($cost).",
                    line_comment = ".db_escape($line_comment).",
                    updated_by = ".db_escape($updated_by).",
                    updated_on = ".db_escape($updated_on)." 
                WHERE trans_id = ".db_escape($selected_id);

            db_query($sql, 'update_job_cost_addition_line');
            return db_num_affected_rows();
}
        
function add_job_cost_addition_line($trans_no, $type, $item_description, $cost, $line_comment) {
    
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
        $sql = "INSERT INTO ".TB_PREF."job_cost_addition_details
                    (   trans_no, type,
                        item_description, cost, 
                        line_comment, 
                        created_by, created_on)        
                VALUE ( ".db_escape($trans_no).", ".db_escape($type).", 
                        ".db_escape($item_description).", ".db_escape($cost).",   
                        ".db_escape($line_comment).", 
                        ".db_escape($created_by).", ".db_escape($created_on)." )";      

            db_query($sql, 'add_job_cost_addition_line');
            return db_insert_id();  
}

function get_job_cost_addition_header_record($id) {
    $sql = "SELECT * 
            FROM ".TB_PREF."job_cost_additions    
            WHERE id = ".db_escape($id);
    
        $result = db_query($sql, "get_job_cost_addition_header_record");
        return db_fetch($result);
}

function get_job_cost_addition_header($id) {
    
    $sql = "SELECT jca.*,
                pm.description project_description,
                ps.description site_description 
               
            FROM ".TB_PREF."job_cost_additions jca  
                JOIN ".TB_PREF."projects_master pm   
                    ON pm.project_id = jca.project_id 
                JOIN ".TB_PREF."project_sites ps   
                    ON ps.id = jca.project_site_id 
                
            WHERE jca.id = ".db_escape($id);
    
        $result = db_query($sql, "get_job_cost_addition_header");
        return db_fetch($result);
}

function get_job_cost_addition_header_type_trans_no($type, $trans_no) {
    $sql = "SELECT jca.*,
                pm.description project_description,
                ps.description site_description 
               
            FROM ".TB_PREF."job_cost_additions jca  
                JOIN ".TB_PREF."projects_master pm   
                    ON pm.project_id = jca.project_id 
                JOIN ".TB_PREF."project_sites ps   
                    ON ps.id = jca.project_site_id 
            WHERE jca.type = ".db_escape($type)." 
                AND jca.trans_no = ".db_escape($trans_no);
    
        $result = db_query($sql, "get_job_cost_addition_header_type_trans_no");
        return db_fetch($result);
}


function get_job_cost_addition_header_record_type_trans_no($type, $trans_no) {
    $sql = "SELECT * 
            FROM ".TB_PREF."job_cost_additions    
            WHERE type = ".db_escape($type)." 
                AND trans_no = ".db_escape($trans_no);
    
        $result = db_query($sql, "get_job_cost_addition_header_record_type_trans_no");
        return db_fetch($result);
}


function delete_job_cost_addition_details_record($selected_id) {
    $sql = "DELETE 
            FROM ".TB_PREF."job_cost_addition_details 
            WHERE trans_id = ".db_escape($selected_id);
    
        db_query($sql, 'delete_job_cost_addition_details_record');
        return db_num_affected_rows();
}

function get_job_cost_addition_details($trans_no, $type) {
    $sql = "SELECT ard.* 
            FROM ".TB_PREF."job_cost_addition_details ard  
            WHERE ard.trans_no = ".db_escape($trans_no)." 
                AND ard.type = ".db_escape($type);
    
        return db_query($sql, "get_job_cost_addition_details");
}


function get_job_cost_addition_details_record($id) {
    $sql = "SELECT * 
            FROM ".TB_PREF."job_cost_addition_details  
            WHERE trans_id = ".db_escape($id);
    
        $result = db_query($sql, "get_job_cost_addition_details_record");
        return db_fetch($result);
}

function get_job_cost_addition_details_lines_count($trans_no, $type) {
    $sql = "SELECT COUNT(trans_id) lines_count 
            FROM ".TB_PREF."job_cost_addition_details 
            WHERE trans_no = ".db_escape($trans_no)." 
                AND type = ".db_escape($type);
    
        $result = db_query($sql, 'get_job_cost_addition_details_lines_count');
        return db_result_value($result, 'lines_count');
}

function get_job_cost_addition_details_total($type, $trans_no) {
    $sql = "SELECT SUM(cost) lines_total 
            FROM ".TB_PREF."job_cost_addition_details 
            WHERE type = ".db_escape($type)." 
                AND trans_no = ".db_escape($trans_no);
    
        $result = db_query($sql, 'get_job_cost_addition_details_total');
        return db_result_value($result, 'lines_total');
}


function update_job_cost_addition_header_total($type, $trans_no) {

    $total = get_job_cost_addition_details_total($type, $trans_no);
    
    $sql = "UPDATE ".TB_PREF."job_cost_additions
            SET total = ".db_escape($total)." 
            WHERE type = ".db_escape($type)."   
                AND trans_no = ".db_escape($trans_no);    

    db_query($sql, "update_job_cost_addition_header_total"); 
}

