<?php

function cs_get_pr_issues(){
    $sql = "SELECT * 
            FROM ".TB_PREF."purchase_requisition_issues    
            ORDER BY pri_no 
            LIMIT 10000 ";
    
    return db_query($sql, "cs_get_issues");
}

function cs_update_pr_issue_total($iou_id, $paid_total)
{    
    $sql = "UPDATE ".TB_PREF."purchase_requisition_issues    
                SET total = ".db_escape($paid_total)."   
            WHERE pri_no = ".db_escape($iou_id);
    
    db_query($sql, "cs_update_pr_issue_total");
}

function cs_custom_script_update_pr_issue_total(){
    $message = "==================Script Sarted==========================";
    write_custom_script_log("cs_custom_script_update_pr_issue_total", $message);
    
    $ious_result = cs_get_pr_issues();
    $recs = db_num_rows($ious_result);
    write_custom_script_log("cs_custom_script_update_pr_issue_total", $recs." Records selected");
    
    while($row = db_fetch($ious_result)){
        
        $paid_total = get_pr_issue_header_details_lines_total($row['pri_no']);
        
        if($row['total'] == $paid_total){
            $message = "Totals (".$row['total'].") equal. No need to update PRI #".$row['pri_no']." total.";
            write_custom_script_log("cs_custom_script_update_pr_issue_total", $message);
            continue;
        }
        
        cs_update_pr_issue_total($row['pri_no'], $paid_total);
        
        $n = db_num_affected_rows();
        if($n==1){
            $message = "PR Issue #".$row['pri_no']." total updated.";
        }else{
            $message = "Could not update PR Issue #".$row['pri_no']." total.";
        }
        write_custom_script_log("cs_custom_script_update_pr_issue_total", $message);
    }
    
    $message = "------------------Script Completed----------------------------";
    write_custom_script_log("cs_custom_script_update_pr_issue_total", $message);
}

?>