<?php

function cs_get_reimbursement_ids()
{
    $sql = "SELECT id 
            FROM ".TB_PREF."i_owe_you_float_reimbursements 
            WHERE id NOT IN (SELECT DISTINCT reimbursement_id FROM ".TB_PREF."float_reimbursement_details)

            LIMIT 10000";
    
    return db_query($sql, "cs_get_reimbursement_ids");
}


function cs_create_float_reimbursement_details()
{
    $message = "==================Script Sarted==========================";
    write_custom_script_log("cs_create_float_reimbursement_details", $message);
    
    $reimb_ids_result = cs_get_reimbursement_ids();
    $recs = db_num_rows($reimb_ids_result);
    write_custom_script_log("cs_create_float_reimbursement_details", $recs." Records selected");
    
    while($row = db_fetch($reimb_ids_result)){
        
        $msg = cs_create_reimbursement_details_for_reimbursement_id($row['id']);
        if($msg['success'] !== true){
            $message = $msg['message'];
            write_custom_script_log("cs_create_float_reimbursement_details", $message);
            return;
        }
        $message = "reimbursement_id ".$row['id']." completed";
        write_custom_script_log("cs_create_float_reimbursement_details", $message);
    }
    
    $message = "------------------Script Completed----------------------------";
    write_custom_script_log("cs_update_pr_line_total_issued_qty", $message);
}

function cs_create_reimbursement_details_for_reimbursement_id($id){

    $result = view_petty_cash_float_reimbursement_iou_expense_batches($id);
    $msg = cs_create_reimbursement_details($id, $result);
    if($msg['success'] !== true){
        return $msg;
    }
    
    $result = view_petty_cash_float_reimbursement_iou_payments($id);
    $msg = cs_create_reimbursement_details($id, $result);
    if($msg['success'] !== true){
        return $msg;
    }
    
    $result = view_petty_cash_float_reimbursement_petty_cash_payments($id);
    $msg = cs_create_reimbursement_details($id, $result);
    if($msg['success'] !== true){
        return $msg;
    }
    
    $result = view_petty_cash_float_reimbursement_direct_petty_cash_payments($id);
    $msg = cs_create_reimbursement_details($id, $result);
    if($msg['success'] !== true){
        return $msg;
    }
    
    $result = view_petty_cash_float_reimbursement_float_adjustments($id);
    $msg = cs_create_reimbursement_details($id, $result);
    if($msg['success'] !== true){
        return $msg;
    }
    
    $result = view_petty_cash_float_reimbursement_float_reimbursements($id);
    $msg = cs_create_reimbursement_details($id, $result);
    if($msg['success'] !== true){
        return $msg;
    }
    
    $result = view_petty_cash_float_reimbursement_iou_advances($id);
    $msg = cs_create_reimbursement_details($id, $result);
    if($msg['success'] !== true){
        return $msg;
    }
    
    $result = view_petty_cash_float_reimbursement_project_float_advances($id);
    $msg = cs_create_reimbursement_details($id, $result);
    if($msg['success'] !== true){
        return $msg;
    }
    
    $result = view_petty_cash_float_reimbursement_supplier_payments($id);
    $msg = cs_create_reimbursement_details($id, $result);
    if($msg['success'] !== true){
        return $msg;
    }
    
    return array("success"=>true, "message"=>"");
    
}


function cs_create_reimbursement_details($id, $result)
{

    while ($myrow = db_fetch($result)) {
        
        $batch_tran_type = $myrow['type_id'];
        $batch_type_no = $myrow['id'];
            
        if($myrow['type_id'] == ST_I_OWE_YOU_ADVANCES_BATCH){            
            $tran_type = ST_I_OWE_YOU;
            $type_no = $myrow['iou_id'];
        }
        if($myrow['type_id'] == ST_PROJECT_ADVANCE){            
            $tran_type = ST_PROJECT_ADVANCE;
            $type_no = $myrow['line_id'];
        }
        else{
            $tran_type = $tran_type = $myrow['type_id'];
            $type_no = $trans_no = $myrow['id'];
        }
        
        $n = add_float_reimbursement_details($id, 
                                            $batch_tran_type, $batch_type_no, 
                                            $tran_type, $type_no, 
                                            $myrow['amount']);
        
        if(!($n > 0)){
            return array("success"=>false, "message"=>"Could not add float_reimbursement_details for Reimb. Id ".$id." Type ".$myrow['type_id']." and #".$myrow["id"]);
        }
    }
    
    return array("success"=>true, "message"=>"");
}