<?php

function complete_asset_verification_count($asset_verification_count_no) {
    $total_adjustment_value = 0;
    
    $n = update_asset_verification_count_header_status($asset_verification_count_no, SC_COMPLETING, SC_COUNTED);
        if ($n != 1) {
            return array("success" => false, "message" => "Another user has updated asset verification #$asset_verification_count_no.");
        }

    $sc_sheets_result = get_asset_verification_count_sheets($asset_verification_count_no);    
        while ($sc_sheet = db_fetch($sc_sheets_result)){

            $sc_line_result = get_asset_verification_counted_details($asset_verification_count_no, $sc_sheet['sheet_no']); 
                while ($sc_line = db_fetch($sc_line_result)){
                    $good_qty = $sc_line['good_qty'];
                    $damaged_qty = $sc_line['damaged_qty'];
                    $verified_qty = $good_qty + $damaged_qty;

                    $verifying_current_value = $sc_line['current_value'];
                    $qoh = $sc_line['system_qty'];

                    $status_id = SC_COMPLETED;

                    $sql = "UPDATE ".TB_PREF."asset_verification_count_details 
                            SET status_id = ".db_escape($status_id)."  
                            WHERE id = ".db_escape($sc_line['id'])." 
                                AND status_id = ".db_escape(SC_COUNTED);

                    db_query($sql, "get_asset_verification_counted_details ".$sc_line['asset_id']);

                    $adj_value = ($verified_qty - $qoh) * $verifying_current_value;

                    $total_adjustment_value += $adj_value;
                }
        }
    
    $error_item_verification = check_asset_verification_count_complete($asset_verification_count_no);
        if ($error_item_verification > 0) {
            return array("success" => false, "message" => "Could not complete asset verification count for $error_item_verification items.");
        }

    $n2 = update_asset_verification_count_completed($asset_verification_count_no, SC_COMPLETED, SC_COMPLETING, $total_adjustment_value);
        if ($n2 == 1) {
            return array("success" => true, "message" => "Counting completed for asset verification count #$asset_verification_count_no.");
        } else {
            return array("success" => false, "message" => "Could not complete asset verification count #$asset_verification_count_no.");
        }
}

function check_asset_verification_count_complete($asset_verification_count_no) {
    $sql = "SELECT COUNT(id) verification 
            FROM ".TB_PREF."asset_verification_count_details 
            WHERE asset_verification_count_no = ".db_escape($asset_verification_count_no)."  
                AND status_id <> ".db_escape(SC_COMPLETED);

        $result = db_query($sql, "check_asset_verification_count_complete");
            $sc = db_fetch($result);
                if (isset($sc['verification']) && $sc['verification'] > 0) {
                    return $sc['verification'];
                } else {
                    return 0;
                }
}

function check_asset_verification_count_processed($asset_verification_count_no) {
    $sql = "SELECT COUNT(status_id) rec_verification  
            FROM ".TB_PREF."asset_verification_count_details
            WHERE asset_verification_count_no = ".db_escape($asset_verification_count_no)." 
                AND status_id <> ".db_escape(SC_PROCESSED)." 
            LIMIT 1";
     
        $result = db_query($sql, "check_asset_verification_count_processed");
        $row = db_fetch($result);
            if (isset($row['rec_verification']) && $row['rec_verification'] > 0) {
                return false;
            } else {
                return true;
            }
}

function update_asset_verification_count_item_processed($row_id, $asset_id) {
    $sql = "UPDATE ".TB_PREF."asset_verification_count_details 
                SET status_id = ".SC_PROCESSED." 
            WHERE asset_id = ".db_escape($asset_id)." 
                AND id = ".db_escape($row_id);

	db_query($sql, "update_asset_verification_count_item_processed");
}

?>