<?php

function cs_get_ious(){
    $sql = "SELECT * 
            FROM ".TB_PREF."i_owe_you  
            WHERE payments_total = 0  
            LIMIT 10000 ";
    
    return db_query($sql, "cs_get_ious");
}

function cs_update_iou_paid_total($iou_id, $paid_total)
{    
    $sql = "UPDATE ".TB_PREF."i_owe_you    
                SET payments_total = ".db_escape($paid_total)."   
            WHERE id = ".db_escape($iou_id);
    
    db_query($sql, "cs_update_iou_paid_total");
}

function cs_custom_script_update_iou_paid_total(){
    $message = "==================Script Sarted==========================";
    write_custom_script_log("cs_custom_script_update_iou_paid_total", $message);
    
    $ious_result = cs_get_ious();
    $recs = db_num_rows($ious_result);
    write_custom_script_log("cs_custom_script_update_iou_paid_total", $recs." Records selected");
    
    while($row = db_fetch($ious_result)){
        
        $paid_total = get_i_owe_you_payments_total($row['id'], -1, 1);
        cs_update_iou_paid_total($row['id'], $paid_total);
        
        $n = db_num_affected_rows();
        if($n==1){
            $message = "IOU #".$row['id']." paid total updated.";
        }else{
            $message = "Could not update IOU #".$row['id']." paid total.";
        }
        write_custom_script_log("cs_custom_script_update_iou_paid_total", $message);
    }
    
    $message = "------------------Script Completed----------------------------";
    write_custom_script_log("cs_custom_script_update_iou_paid_total", $message);
}

?>