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

function get_sql_for_asset_quantity_adjustment_request_inquiry($asset_qty_ajt_req_id = 0, $location=null, $project_id=-1){
    $sql = "SELECT aqar.id,
                    aqar.reference,
                    pm.description project_description,
                    alo.location_name location_name,
                    aqar.is_deduction,
                    aqar.total,
                    aqar.comment,
                    aqar.approval_status 
                    
            FROM ".TB_PREF."asset_quantity_adjustment_requests aqar 
                    LEFT JOIN ".TB_PREF."projects_master pm 
                        ON aqar.project_id = pm.project_id 
                    LEFT JOIN ".TB_PREF."asset_locations alo 
                        ON aqar.asset_location = alo.loc_code 
                 
            WHERE 1 = 1 ";
   
    if($asset_qty_ajt_req_id > 0){
        $sql .= "AND aqar.id =".db_escape($asset_qty_ajt_req_id);
    }
    if(strlen($location) > 0){
        $sql .= "AND aqar.asset_location = ".db_escape($location);
    } 
    if($project_id > 0){
        $sql .= "AND aqar.project_id = ".db_escape($project_id);
    }

    return $sql;
}
        
function view_asset_quantity_adjustment_request($asset_qty_ajt_req_id){
    $sql = "SELECT aqar.*,
                    pm.description project_description,
                    lo.location_name location_name 
                            
            FROM ".TB_PREF."asset_quantity_adjustment_requests aqar
                LEFT JOIN ".TB_PREF."projects_master pm 
                    ON aqar.project_id = pm.project_id
                LEFT JOIN ".TB_PREF."asset_locations lo 
                    ON aqar.asset_location = lo.loc_code 
                
            WHERE aqar.id =".db_escape($asset_qty_ajt_req_id);
    
    $result = db_query($sql, "could not view_asset_quantity_adjustment_request");
    return db_fetch($result);
}

function view_asset_quantity_adjustment_request_details($asset_qty_ajt_req_id){
    
    $sql = "SELECT aqard.*,
                    am.asset_code,
                    am.description asset_description,
                    am.asset_id,
                    ps.description site_description,
                    pt.description task_description,
                    pt.task_code 

            FROM ".TB_PREF."asset_quantity_adjustment_request_details aqard 
                LEFT JOIN ".TB_PREF."asset_master am 
                    ON aqard.asset_id = am.asset_id 
                LEFT JOIN ".TB_PREF."project_sites ps 
                    ON aqard.site_id = ps.id 
                LEFT JOIN ".TB_PREF."project_tasks pt  
                    ON aqard.task_id = pt.task_id 
            WHERE aqard.aqar_id = ".db_escape($asset_qty_ajt_req_id);

    return db_query($sql, "could not view_asset_quantity_adjustment_request_details");  
}

function get_sql_for_approve_asset_quantity_adjustment_request_inquiry($current_user_id){        
    $sql = "SELECT aqar.*,
                    pm.description project_description,
                    lo.location_name  
            
            FROM ".TB_PREF."asset_quantity_adjustment_requests aqar  
                LEFT JOIN ".TB_PREF."projects_master pm 
                    ON aqar.project_id = pm.project_id 
                LEFT JOIN ".TB_PREF."asset_locations lo  
                    ON aqar.asset_location = lo.loc_code 
                JOIN ".TB_PREF."approvers apr 
                    ON aqar.approved_by = apr.user_id                 
                JOIN ".TB_PREF."users c 
                    ON aqar.created_by = c.id
                JOIN ".TB_PREF."user_data_access_groups udag 
                    ON pm.dag_id = udag.dag_id
                JOIN ".TB_PREF."users u 
                    ON apr.user_id = u.id                                 
                                 
            WHERE ( aqar.approval_status = ".AS_AWAITING_APPROVAL." OR aqar.approval_status = ".AS_WITH_THE_APPROVER." ) AND 
                    aqar.next_approval_group_id = apr.approval_group_id AND aqar.created_by = c.id AND 
                    udag.user_id = apr.user_id AND apr.user_id = ".db_escape($current_user_id);                    
    
    $sql = "SELECT aqar.*,
                    pm.description project_description,
                    lo.location_name

            FROM ".TB_PREF."asset_quantity_adjustment_requests aqar  
                JOIN ".TB_PREF."projects_master pm 
                    ON aqar.project_id = pm.project_id     
                JOIN ".TB_PREF."asset_locations lo 
                    ON aqar.asset_location = lo.loc_code  
                JOIN ".TB_PREF."users c 
                    ON aqar.created_by = c.id  
                JOIN ".TB_PREF."approvers as apr 
                    ON aqar.next_approval_group_id = apr.approval_group_id  
                JOIN ".TB_PREF."user_data_access_groups as udag ON 
                    pm.dag_id = udag.dag_id AND 
                    udag.user_id = apr.user_id  
                JOIN ".TB_PREF."users u ON 
                    apr.user_id = u.id 
            
            WHERE (aqar.approval_status = ".AS_AWAITING_APPROVAL." OR aqar.approval_status = ".AS_WITH_THE_APPROVER." ) 
                    AND apr.user_id = ".db_escape($current_user_id); 
    
    return $sql; 
}
        
        
?>