<?php
/**********************************************************************
***********************************************************************/


function write_miscellaneous_pc_header($mpc_no, $reference, 
                                        $project_id, $date, 
                                        $approval_status, $header_comment)                          
{
    global $Refs;
    
    if($mpc_no > 0){        
        update_miscellaneous_pc_header($mpc_no, $reference, 
                                        $date, 
                                        $header_comment);
    }else{
        
        $mpc_no = add_miscellaneous_pc_header($reference, $project_id, $date, 
                                                $approval_status, $header_comment);
        
        $Refs->save(ST_MISCELLANEOUS_PROJECT_COST, $mpc_no, $reference);
    }

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


function write_miscellaneous_pc_detail($mpc_no, $mpc_line_id,
                                        $description, $unit_id, $quantity,
                                        $rate, $task_boq_group_id,
                                        $task_id, $mpc_type_id, $debit_account, $credit_account, $comment)
{            
    if($mpc_line_id > 0){         
        $n = update_miscellaneous_pc_detail($mpc_no, $mpc_line_id,
                                        $description, $unit_id, $quantity,
                                        $rate, $task_boq_group_id,
                                        $task_id, $mpc_type_id, $debit_account, $credit_account, $comment);
        if($n != 1){
           return array("success" => false, "message" => "Another user has updated miscellaneous project cost detail $mpc_line_id.");  
        }
    }
    else{
        add_miscellaneous_pc_detail($mpc_no, $description,
                                        $unit_id, $quantity,
                                        $rate, $task_boq_group_id,
                                        $task_id, $mpc_type_id, $debit_account, $credit_account, $comment);
    }
    
    update_miscellaneous_pc_header_total($mpc_no);
    
    return array("success" => true, "message" =>"Miscellaneous project cost $mpc_no has been updated.");
    
}


function update_miscellaneous_pc_header($mpc_no, $reference, 
                                        $date, $header_comment)
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
    $sql = "UPDATE ".TB_PREF."miscellaneous_project_costs
            SET 
                reference=".db_escape($reference).",
                date=".db_escape(date2sql($date)).", 
                comment=".db_escape($header_comment).", 
                updated_by=".db_escape($updated_by).",
                updated_on=".db_escape($updated_on)."       
                
            WHERE id=".db_escape($mpc_no);
    
    db_query($sql);
    return db_num_affected_rows();  
    
}


function add_miscellaneous_pc_header($reference, $project_id, $date, 
                                        $approval_status, $header_comment)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."miscellaneous_project_costs
                  ( reference, project_id,  
                    date, approval_status, comment,                  
                    created_by, created_on )         
                VALUES 
                (".db_escape($reference).", ".db_escape($project_id).", 
                    ".db_escape(date2sql($date)).",
                    ".db_escape($approval_status).",
                    ".db_escape($header_comment).", 
                    ".db_escape($created_by).", ".db_escape($created_on).")";
     
    db_query($sql);
    return db_insert_id();        
}


function add_miscellaneous_pc_detail($mpc_no, $description,
                                        $unit_id, $quantity,
                                        $rate, $task_boq_group_id,
                                        $task_id, $mpc_type_id,  $debit_account, $credit_account, $comment)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."miscellaneous_project_cost_details 
                      ( mpc_id, description,
                        unit, quantity, rate,
                        task_boq_group_id, task_id,
                        mpc_type_id, comment,
                        debit_account, credit_account,
                        created_by, created_on )                
            VALUES (".db_escape($mpc_no).", ".db_escape($description).",".db_escape($unit_id).","
                        .db_escape($quantity).", ".db_escape($rate).", "
                        .db_escape($task_boq_group_id).",".db_escape($task_id).","                      
                        .db_escape($mpc_type_id).", ".db_escape($comment).","                        
                        .db_escape($debit_account).", ".db_escape($credit_account).","                     
                        .db_escape($created_by).", ".db_escape($created_on).")";
             
    db_query($sql);
       
}

function update_miscellaneous_pc_detail($mpc_no, $mpc_line_id,
                                        $description, $unit_id, $quantity,
                                        $rate, $task_boq_group_id,
                                        $task_id, $mpc_type_id,  $debit_account, $credit_account, $comment)
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
     $sql = "UPDATE ".TB_PREF."miscellaneous_project_cost_details
                SET                    
                    description=".db_escape($description).",                  
                    unit=".db_escape($unit_id).",
                    quantity=".db_escape($quantity).",
                    rate=".db_escape($rate).",       
                    task_boq_group_id=".db_escape($task_boq_group_id).",   
                    task_id=".db_escape($task_id).",
                    mpc_type_id=".db_escape($mpc_type_id).",
                    debit_account=".db_escape($debit_account).",
                    credit_account=".db_escape($credit_account).",
                    comment=".db_escape($comment).",
                    updated_by=".db_escape($updated_by).",
                    updated_on=".db_escape($updated_on)."                  
                        
                WHERE id=".db_escape($mpc_line_id);
     
    db_query($sql);
    return db_num_affected_rows();  
    
}


function get_miscellaneous_pc_header($mpc_no)
{
    $sql = "SELECT mpc.*,
                    pm.description project_description                                 
        
            FROM ".TB_PREF."miscellaneous_project_costs mpc
                LEFT JOIN ".TB_PREF."projects_master pm ON
                    mpc.project_id = pm.project_id       
                            
            WHERE mpc.id=".db_escape($mpc_no);
    
    $result = db_query($sql);
    return db_fetch($result);   
    
}


function get_miscellaneous_pc_details($order_no)
{
    $sql = "SELECT mpcd.*,
                    iu.name unit_name,
                    pbig.description boq_item_group_description,
                    pt.description budget_name,
                    lv.value_name miscalleneous_type,
                    da.account_name debit_account_name,
                    ca.account_name credit_account_name 
                    
                         
            FROM ".TB_PREF."miscellaneous_project_cost_details mpcd  
                LEFT JOIN ".TB_PREF."item_units iu ON
                    mpcd.unit = iu.abbr
                    
                LEFT JOIN ".TB_PREF."project_boq_item_groups pbig ON
                    mpcd.task_boq_group_id = pbig.id
                    
                LEFT JOIN ".TB_PREF."project_tasks pt ON
                    mpcd.task_id = pt.task_id
                    
                LEFT JOIN ".TB_PREF."list_values lv ON
                    mpcd.mpc_type_id = lv.id 
                LEFT JOIN ".TB_PREF."chart_master da ON 
                    mpcd.debit_account = da.account_code  
                LEFT JOIN ".TB_PREF."chart_master ca ON 
                    mpcd.credit_account = ca.account_code  
                 
            WHERE mpcd.mpc_id=".db_escape($order_no)." 
            ORDER BY id";
    
    return db_query($sql, "Could not get_miscellaneous_pc_details.");
    
}

function get_miscellaneous_pc_line($selected_id)
{
     $sql = "SELECT *
         
            FROM ".TB_PREF."miscellaneous_project_cost_details  
                                   
            WHERE id=".db_escape($selected_id);
     
     $result = db_query($sql);
     return db_fetch($result);
    
}


function delete_miscellaneous_pc_detail($mpc_no, $mpc_line_id)
{  
    $sql = "DELETE FROM ".TB_PREF."miscellaneous_project_cost_details WHERE id=".db_escape($mpc_line_id);
    db_query($sql);
    
    update_miscellaneous_pc_header_total($mpc_no);
    
}


function get_miscellaneous_pc_line_count($mpc_no)
{
    $sql = "SELECT COUNT(id) line_count 
            FROM ".TB_PREF."miscellaneous_project_cost_details 
            WHERE mpc_id = ".db_escape($mpc_no);
    
    $result = db_query($sql);
    $count =  db_fetch($result);   
    if(isset($count['line_count']) && $count['line_count'] > 0){
        return $count['line_count'];
    }
    
    return 0;
}


function update_miscellaneous_pc_header_total($mpc_no)
{
    $total = get_miscellaneous_pc_detail_total($mpc_no);
    
    $sql = "UPDATE ".TB_PREF."miscellaneous_project_costs
            SET 
                total=".db_escape($total)."
            WHERE id=".db_escape($mpc_no);
    
    db_query($sql); 
    
}


function get_miscellaneous_pc_detail_total($mpc_no)
{
    $sql = "SELECT SUM(quantity * rate) total                          
            FROM ".TB_PREF."miscellaneous_project_cost_details 
                 
            WHERE mpc_id=".db_escape($mpc_no);
    
    $result = db_query($sql, "Could not get_miscellaneous_pc_detail_total");
    
    if($result){
        $t = db_fetch($result);
        return $t['total'];
    }
    else{
        return 0;
    }
    
}

?>