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

function write_asset_quantity_adjustment_header($aqar_id, $reference, $company_code, $employee_code, $tran_date, $comment, $lines_total, $is_deduction){
    
    global $Refs;

        $msg = insert_asset_quantity_adjustment($aqar_id, $reference, $company_code, $employee_code, $tran_date, $comment, $lines_total, $is_deduction);
        if($msg['success'] !== true){
            return $msg;
        }

        $Refs->save(ST_ASSET_QUANTITY_ADJUSTMENT, $msg['aqa_id'], $reference);

    return array("success" => true, "message" => "", "aqa_id" => $msg['aqa_id']);
}
         
function insert_asset_quantity_adjustment($aqar_id, $reference, $company_code, $employee_code, $tran_date, $comment, $lines_total, $is_deduction){
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."asset_quantity_adjustments 
                  ( aqar_id, reference, 
                    company_code, employee_code, 
                    tran_date, 
                    comment, total, is_deduction,
                    created_by, created_on )         
            VALUES( ".db_escape($aqar_id).", ".db_escape($reference).", 
                    ".db_escape($company_code).", ".db_escape($employee_code).", 
                    ".db_escape(date2sql($tran_date)).", 
                    ".db_escape($comment).", ".db_escape($lines_total).", ".db_escape($is_deduction).",                             
                    ".db_escape($created_by).", ".db_escape($created_on).")";
     
    db_query($sql, "could not insert_asset_quantity_adjustment");

    if(!(db_insert_id() > 0)){
        cancel_transaction();
        return array("success" => false, "message" => "Could not Insert an Asset Quantity Adjustment Header.", "aqar_id" => $aqar_id);
    }else{
        return array("success" => true, "message" => "", "aqa_id" => db_insert_id());
    }
}
        
function add_asset_quantity_adjustment_line($asset_qty_ajt_no, $aqa_line_id, $asset_id, $quantity, $asset_value, $task_id){

    $msg = insert_asset_quantity_adjustment_line($asset_qty_ajt_no, $aqa_line_id, $asset_id, $quantity, $asset_value, $task_id);
    if($msg['success'] !== true){
        return $msg;
    }

    return array("success" => true, "message" => "", "aqa_id" => $asset_qty_ajt_no);
}

function update_asset_transfer_line($aqa_line_id, $asset_id, $quantity, $current_value, $from_site_id, $to_site_id, $line_comment){
    $sql = "UPDATE ".TB_PREF."asset_quantity_adjustment_details 
                SET quantity = ".db_escape($quantity).", 
                    asset_id = ".db_escape($asset_id).",
                    asset_value = ".db_escape($current_value).",
                    from_site_id = ".db_escape($from_site_id).",
                    to_site_id = ".db_escape($to_site_id).",
                    line_comment = ".db_escape($line_comment)."
                
            WHERE id = ".db_escape($aqa_line_id);
    
    db_query($sql, "Could not update_asset_transfer_line");
    
    if(db_num_affected_rows() != 1){
        cancel_transaction();
        return array("success" => false, "message" => "Could not update Asset Quantity Adjustment Request Details Line #$aqa_line_id");
    }else{
        return array("success" => true, "message" => "", "aqa_line_id" => $aqa_line_id);
    }
}        

function insert_asset_quantity_adjustment_line($asset_qty_ajt_no, $aqa_line_id, $asset_id, $quantity, $asset_value, $task_id){
    $sql = "INSERT INTO ".TB_PREF."asset_quantity_adjustment_details 
                        ( aqa_id, aqar_line_id, asset_id, 
                          quantity, asset_value, task_id )        
            VALUE       ( ".db_escape($asset_qty_ajt_no).", ".db_escape($aqa_line_id).", ".db_escape($asset_id).",
                          ".db_escape($quantity).", ".db_escape($asset_value).", ".db_escape($task_id).")";
    
    db_query($sql, "could not insert_asset_quantity_adjustment_line");
    
    if(!(db_insert_id() > 0)){
        cancel_transaction();
        return array("success" => false, "message" => "Could not Insert a New Asset Quantity Adjustment Details Line.");
    }else{
        return array("success" => true, "message" => "", "aqa_line_id" => db_insert_id());
    }
}

function get_asset_quantity_adjustment_header($asset_qty_ajt_req_no, $status=null){
    $sql = "SELECT aqar.*,
                    aqa.reference aqa_reference, 
                    aqa.id aqa_id, 
                    aqa.tran_date aqa_tran_date, 
                    aqa.comment aqa_comment, 
                    pm.description project_description,  
                    aloc.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_quantity_adjustments aqa  
                    ON aqa.aqar_id = aqar.id  
                LEFT JOIN ".TB_PREF."asset_locations aloc  
                    ON aqar.asset_location = aloc.loc_code    
            WHERE aqar.id = ".db_escape($asset_qty_ajt_req_no);
    
    if($status !== null){
        $sql .= " AND aqar.approval_status = ".db_escape($status);
    }
    
    $result = db_query($sql, "Could not get_asset_quantity_adjustment_header");
    return db_fetch($result);
}
 
function get_asset_transfer_detail_line($selected_id){
    $sql = "SELECT aqad.*         
            FROM ".TB_PREF."asset_quantity_adjustment_details aqad
            WHERE aqad.id = ".db_escape($selected_id);
    
    $result = db_query($sql, "could not get_asset_transfer_detail_line");
    return db_fetch($result);
}

function delete_asset_transfer_detail($selected_id){
    $sql = "DELETE FROM ".TB_PREF."asset_quantity_adjustment_details WHERE id=".db_escape($selected_id);
    db_query($sql, "could not delete_asset_transfer_detail");
    return db_num_affected_rows();
}

function get_asset_quantity_adjustment_details($asset_qty_ajt_req_no){
    $sql = "SELECT aqard.*,
                    aqard.quantity req_quantity, 
                    aqard.row_state, 
                    am.asset_code,
                    am.description asset_description,
                    am.asset_id,
                    am.multiple_items,
                    ps.description site_description,
                    aqad.quantity aqa_quantity, 
                    pt.description task_description,
                    aqar.is_deduction 

            FROM ".TB_PREF."asset_quantity_adjustment_requests aqar 
                JOIN ".TB_PREF."asset_quantity_adjustment_request_details aqard 
                    ON aqar.id = aqard.aqar_id 
                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."asset_quantity_adjustment_details aqad 
                    ON aqard.id = aqad.aqar_line_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_no)." 
            GROUP BY aqard.id";
    
    return db_query($sql, "Could not get_asset_quantity_adjustment_details");
}

function get_asset_quantity_adjustment_details_line_count($asset_qty_ajt_no){
    $sql = "SELECT COUNT(id) line_count 
            FROM ".TB_PREF."asset_quantity_adjustment_details  
            WHERE aqa_id = ".db_escape($asset_qty_ajt_no);
    
    $result = db_query($sql, "could not get_asset_quantity_adjustment_details_line_count");
    $count =  db_fetch($result);   
    if(isset($count['line_count']) && $count['line_count'] > 0){
        return $count['line_count'];
    }    
    return 0;
}


function get_asset_quantity_adjustment_detail_records($asset_qty_ajt_no){
    $sql = "SELECT * 
            FROM ".TB_PREF."asset_quantity_adjustment_details  
            WHERE aqa_id = ".db_escape($asset_qty_ajt_no);
    
    return db_query($sql, "could not get_asset_quantity_adjustment_details_line_count");
    
}

   
function release_asset_transfer_for_approval($asset_qty_ajt_no){
    $current_user_id = $_SESSION['wa_current_user']->user;
    $aqah = get_asset_transfer_header($asset_qty_ajt_no);
    
    $info = approve_document(ST_ASSET_QUANTITY_ADJUSTMENT, $asset_qty_ajt_no, $aqah['to_project_id'], $aqah['total'], $aqah['next_approval_group_id'], 
                             $current_user_id, $aqah['row_state']);

    if($info['success'] !== true){
        cancel_transaction();
        return array("success" => $info['success'],"tran_no" => $asset_qty_ajt_no, "message" => $info['message']);
    }

    return array("success" => true, "aqa_id" => $asset_qty_ajt_no, "message" => $info['message']);  
}

function get_asset_transfer_lines_total($asset_qty_ajt_no){
    $sql = "SELECT SUM(quantity) total_quantity  
            FROM ".TB_PREF."asset_quantity_adjustment_details 
            WHERE aqar_line_id = ".db_escape($asset_qty_ajt_no);    
    $result = db_query($sql, "could not get_asset_transfer_lines_total");
    $total_quantity = db_fetch($result);
    return $total_quantity['total_quantity'];
}

?>