<?php

function check_process_asset_verification_count_data($asset_verification_count_no) {
    $sch = get_asset_verification_count_header_record($asset_verification_count_no);
        if (! ($sch['status_id'] == SC_COMPLETED || $sch['status_id'] == SC_PROCESSING)) {
            display_error(_("Status of this asset count does not allow further processing."));
            set_focus('');
            return false;
        }

        return true;
}

function display_asset_verification_count_counted_details($asset_verification_count_no) {
    $sheet_no = $_POST['sheet_no'];
    
    start_table(TABLESTYLE, "width=98%");

        $th = array(_("Asset Id"), _("Description"), _("Good Qty"), _("Damaged Qty"), _("System Qty"), _("Cost"), _("Adj. Qty"), _("Adj Value"));
        table_header($th);

        $j = 1;
        $k = $adj_value_total = 0;
    
            $result = get_asset_verification_count_details($asset_verification_count_no, $sheet_no);
                while ($myrow = db_fetch($result)) {
                    $dec = '2';
                    alt_table_row_color($k);

                        label_cell($myrow["asset_id"], 'align=center');
                        label_cell($myrow["asset_code"].' - '.$myrow["description"]);

                        label_cell(number_format($myrow["good_qty"], $dec), "align=right");
                        label_cell(number_format($myrow["damaged_qty"], $dec), "align=right");
                        label_cell(number_format($myrow["system_qty"], $dec), "align=right");

                        label_cell(number_format($myrow["current_value"], 2), "align=right");
                        $adj_qty = $myrow["good_qty"] + $myrow["damaged_qty"] - $myrow["system_qty"];
                        label_cell(number_format($adj_qty, $dec), "align=right");
                        $adj_value = $adj_qty * $myrow["current_value"];
                        label_cell(number_format($adj_value, 2), "align=right");

                        $adj_value_total += $adj_value;

                    end_row();
                    
                    $j++;
                    if ($j == 25) {
                        $j = 1;
                        table_header($th);
                    }
                }

                start_row();
                    label_cell(text_bold("Total:"), "colspan=7 align=right");
                    label_cell(text_bold(number_format($adj_value_total, 2)), "align=right");
                end_row();

    end_table(2);
}

function process_asset_verification_count($asset_verification_count_no) {
    $msg_psc = process_asset_verification_count_job($asset_verification_count_no);
        if ($msg_psc['success'] === true) {
            $trans_no = $asset_verification_count_no;
            unset($_SESSION['asset_verification_counts']);
            meta_forward($_SERVER['PHP_SELF'], "ProcessedID=$trans_no");
        } else {
            display_error($msg_psc['message']);
        }
}

function process_asset_verification_count_job($asset_verification_count_no) { 
    global $stock_counting_statuses;
        
    $sch = get_asset_verification_count_header($asset_verification_count_no);
        $status_id = $sch['status_id'];

        if ($status_id == SC_PROCESSED) {
            return array("success" => false, "message" => "This asset verification has been processed already.");
        } elseif($status_id == SC_COMPLETED) {
            update_asset_verification_count_header_status($asset_verification_count_no, SC_PROCESSING, SC_COMPLETED);
        } elseif($status_id == SC_PROCESSING) {
            update_asset_verification_count_header_status($asset_verification_count_no, SC_PROCESSING, SC_PROCESSING);
        } else {
            return array("success" => false, "message" => "Not possible to process this asset verification as it has not been $stock_counting_statuses[SC_COMPLETED] yet.");
        }

            return add_asset_verification_count_adjustments($asset_verification_count_no);
}

function add_asset_verification_count_adjustments($asset_verification_count_no) {
    $args = func_get_args();
    $args = (object)array_combine(array('asset_verification_count_no'), $args);
    $args->trans_no = 0;
        
    hook_db_prewrite($args, ST_ASSET_VERIFICATION);
        
        $sc_header = get_asset_verification_count_header($asset_verification_count_no);
        $tran_date = sql2date($sc_header['tran_date']);

        $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 ($avc_item = db_fetch($sc_line_result)) {
                        if ($avc_item['status_id'] != SC_COMPLETED) {
                            continue;
                        }

                        begin_transaction();

                        $msg_scia = add_asset_verification_count_item_adjustment($asset_verification_count_no, $sc_header, $avc_item);

                        if ($msg_scia['success'] === true) {
                            commit_transaction();
                        } else {
                            cancel_transaction();
                            return array("success"=>false, "message" => "Could not process asset : ".$avc_item['asset_id']);
                        }
                    }            
            }
        
    $processed = check_asset_verification_count_processed($asset_verification_count_no);
        if ($processed) {
            begin_transaction();
                add_audit_trail(ST_ASSET_VERIFICATION, $asset_verification_count_no, $tran_date);
                $args->trans_no = $asset_verification_count_no;
                hook_db_postwrite($args, ST_ASSET_VERIFICATION);
            commit_transaction();

            $n = update_asset_verification_count_header_status($asset_verification_count_no, SC_PROCESSED, SC_PROCESSING);
                if ($n != 1) {
                    return array("success" => false, "message" => "Another user has processed asset verification.");
                } else {
                    return array("success" => true, "message" =>  "Asset verification $asset_verification_count_no has been processed.");
                }
        } else {
            begin_transaction();
                add_audit_trail(ST_ASSET_VERIFICATION, $asset_verification_count_no, $tran_date);
                $args->trans_no = $asset_verification_count_no;
                hook_db_postwrite($args, ST_ASSET_VERIFICATION);
            commit_transaction();

            return array("success" => false, "message" => "Could not process all asset verification items.");
        }
}

function add_asset_verification_count_item_adjustment($asset_verification_count_no, $sc_header, $avc_item) {
    $adj_qty = ($avc_item['good_qty'] + $avc_item['damaged_qty']) - $avc_item['system_qty'];
    $asset_id = $avc_item['asset_id'];
    
    if ($adj_qty == 0) {
        update_asset_verification_count_item_processed($avc_item['id'], $asset_id);
        return array("success" => true, "message"=> '');
    }
    
        $date_ = sql2date($sc_header['tran_date']);
        $location_code = $sc_header['loc_code'];
            
	add_asset_move(ST_ASSET_VERIFICATION, $asset_verification_count_no, $avc_item['id'], $sc_header['reference'], $date_, $asset_id, 
                       $location_code, 0, $sc_header['task_id'], $adj_qty, $sc_header['project_id'], $avc_item['current_value']);
            
    update_asset_verification_count_item_processed($avc_item['id'], $asset_id);
    
    
    return array("success" => true, "message"=> '');
}

?>