<?php

function save_import_asset_verification_count_details($asset_verification_count_no, $sheet_no, $asset_id, $good_qty, $damaged_qty, $message='') {
    $sql = "INSERT INTO ".TB_PREF."import_asset_verification_count_details
                (asset_verification_count_no, sheet_no, asset_id, 
                 good_qty, damaged_qty, message)
            
            VALUES (".db_escape($asset_verification_count_no).", ".db_escape($sheet_no).",
                    ".db_escape($asset_id).", 
                    ".db_escape($good_qty).", ".db_escape($damaged_qty).", 
                    ".db_escape($message).")";
    
        db_query($sql, 'save_import_asset_verification_count_details');
}

function delete_existing_import_asset_verification_count_details() {
    $sql = "DELETE FROM ".TB_PREF."import_asset_verification_count_details";
    
        db_query($sql, 'delete_existing_import_asset_verification_count_details');
}

function get_import_asset_verification_count_details() {
    $sql = "SELECT *
            FROM ".TB_PREF."import_asset_verification_count_details
            WHERE 1 = 1";
    
        return db_query($sql, "get_import_asset_verification_count_details");    
}

function get_import_asset_verification_count_details_with_asset_verification_count_header() {
    $sql = "SELECT iavd.*, avh.status_id avch_status_id 
            FROM ".TB_PREF."import_asset_verification_count_details iavd 
                LEFT JOIN ".TB_PREF."asset_verification_counts avh 
                    ON iavd.asset_verification_count_no = avh.asset_verification_count_no 
            WHERE 1 = 1";
    
        return db_query($sql, "get_import_asset_verification_count_details_with_asset_verification_count_header");    
}

function get_importing_asset_verification_counts() {
    $sql = "SELECT DISTINCT asset_verification_count_no 
            FROM ".TB_PREF."import_asset_verification_count_details
            WHERE 1 = 1";
    
        return db_query($sql, 'get_importing_asset_verification_counts');    
}

function check_asset_verification_count_number($stock_count_no) {
    global $stock_counting_statuses;
    
    $sql = "SELECT IFNULL(( SELECT status_id FROM ".TB_PREF."asset_verification_counts  
                            WHERE asset_verification_count_no = ".db_escape($stock_count_no)." ), '') as status_id";
    
    $result = db_query($sql, 'check_asset_verification_count_number');
    $myrow = db_fetch($result);
    
    if ($myrow['status_id'] == '') {
        $msg = "Unknown stock count number";
        return array("passed" => false, "message" => $msg);
    } else {
        if (! ($myrow['status_id'] == SC_NEW || $myrow['status_id'] == SC_COUNTING)) {
            $msg = "Can not import data for a asset verification count with status ".$stock_counting_statuses[$myrow['status_id']];
            return array("passed" => false, "message" => $msg);
        } else {
            return array("passed" => true, "message" => $stock_count_no);
        }
    }
}

function update_import_asset_verification_count_detail_record($id, $message) {
    $sql = "UPDATE ".TB_PREF."import_asset_verification_count_details 
            SET message = ".db_escape($message)." 
            WHERE id = ".db_escape($id);
    
        db_query($sql, 'update_stock_count_import_record');
}

function check_import_asset_verification_count_sheet_number($sheet_no) {    
    if (! (is_numeric($sheet_no) && $sheet_no > 0)) {
        $msg = "Asset verificaition count sheet number has to be numeric and greater than zero.";
        return array("passed" => false, "message" => $msg);
    } else {
        return array("passed" => true, "message" => $sheet_no);        
    }
}

function check_import_asset_verification_count_asset_id($asset_id) {
    $sql = "SELECT IFNULL((SELECT asset_id FROM ".TB_PREF."asset_master 
                            WHERE asset_id = ".db_escape($asset_id)."), '') as asset_id";
    
    $result = db_query($sql);
    $stock_id = db_fetch($result);
    
    if ($stock_id['asset_id'] == '') {
        $msg = "Unknown asset id";
        return array("passed" => false, "message" => $msg);
    } else {
        return array("passed" => true, "message" => $stock_id['asset_id']);
    }
}

function update_asset_verification_count_import_record($id, $message) {
    $sql = "UPDATE ".TB_PREF."import_asset_verification_count_details 
            SET message = ".db_escape($message)." 
            WHERE id = ".db_escape($id);
    
    db_query($sql, 'update_asset_verification_count_import_record');
}

?>