<?php

function display_asset_verification_count_header($asset_verification_count_no) {
    global $stock_counting_statuses;
    
    $header_row = get_asset_verification_count_header($asset_verification_count_no);      

	start_table(TABLESTYLE2, "width=80%"); // outer table
            start_row();
                label_cells(_("Project"), $header_row['project_description'], "class='tableheader2'");
                label_cells(_("Reference"), $header_row['reference'], "class='tableheader2'");
                label_cells(_("Date"), $header_row['tran_date'], "class='tableheader2'");
            end_row();

            start_row();
                label_cells(_("Budget"), $header_row['task_description'], "class='tableheader2'");
                label_cells(_("Location"), $header_row['location_name'], "class='tableheader2'");
                label_cells(_("Status"), $stock_counting_statuses[$header_row['status_id']], "class='tableheader2'");
            end_row();
            
            start_row();
                label_cells(_("Total Adjustement Value"), $header_row['total_adj_value'], "class='tableheader2'");
                label_cells(_("Comments"), $header_row['comments'], "class='tableheader2'", "colspan=3");
            end_row();

    end_table(1);     

    hidden('asset_verification_count_no', $asset_verification_count_no);
}

function can_process_asset_verification_count() {
    if (($_POST['reference'] == "")) {
        display_error(_("Reference can not be empty."));
        set_focus('reference');
        return false;
    }

    if (! ($_POST['project_id'] > 0)) {
        display_error("Please select a Project.");
        set_focus('project_id');
        return false;
    }

    if (! ($_POST['task_id'] > 0)) {
        display_error("Please select a budget.");
        set_focus('task_id');
        return false;
    }
    
    if (! (strlen($_POST['location']) > 0)) {
        display_error("Please select an asset location.");
        set_focus('location');
        return false;
    }

    if (! (is_date($_POST['tran_date']))) {
        display_error("Entered date is not valid.");
        set_focus('tran_date');
        return false;
    }     

	return true;
}

function display_asset_verification_count_edit_controls($asset_verification_count_no) {
    if ($asset_verification_count_no > 0) {
        $row = get_asset_verification_count_header($asset_verification_count_no);
            if (! (isset($_POST['project_id']) && $_POST['project_id'] > 0)) {
                $_POST['project_id'] = $row['project_id'];
            }

            if (! (isset($_POST['task_id']) && $_POST['task_id'] > 0)) {
                $_POST['task_id'] = $row['task_id'];
            }

            if (! (isset($_POST['reference']))) {
                $_POST['reference'] = $row['reference'];
            }

            if (! (isset($_POST['location']))) {
                $_POST['location'] = $row['loc_code'];
            }

            if (! (isset($_POST['tran_date']))) {
                $_POST['tran_date'] = sql2date($row['tran_date']);
            }

            if (! (isset($_POST['comments']))) {
                $_POST['comments'] = $row['comments'];
            }
    }
    
    start_table(TABLESTYLE2); 

        if ($asset_verification_count_no > 0) {
            label_row('Id:', get_asset_trans_view_str(ST_ASSET_VERIFICATION, $asset_verification_count_no));
        }
        ref_row(_("Reference:"), 'reference', '');
        project_list_row(_("Project:"), 'project_id', null, _('Select project'), true);
        hidden('ModifyStockCountNo', $asset_verification_count_no);
        tasks_list_row(_("Budget:"), 'task_id', $_POST['project_id'], null, _('Select a budget'));
        asset_locations_list_row(_("Asset Location:"), 'location', null, false, false);
        date_row(_("Date:"), 'tran_date');
        stock_count_status_list_cells(_("Status:"), 'sid', null, false);
        textarea_row("Comments", "comments", '', 35, 3);

    end_table(2);
}

function create_new_asset_verification_count() {
    $trans_no = add_asset_verification_count_header($_POST['location'], $_POST['reference'], SC_NEW, $_POST['tran_date'], $_POST['comments'], 
                                                    $_POST['project_id'], $_POST['task_id'] );
    
        return $trans_no;
}

function edit_asset_verification_count_header($asset_verification_count_no) {
    $n = update_asset_verification_count_header($asset_verification_count_no, $_POST['location'], $_POST['reference'], SC_NEW, $_POST['tran_date'], 
                                                $_POST['comments'], $_POST['project_id'],$_POST['task_id'] );
    
    if ($n == 1) {
        return true;
    } else {
        return false;
    }
}

?>